Skip to content

Commit 0c71fdd

Browse files
authored
[NVPTX] Fix ctor / dtor lowering when NVPTX target is not enabled (#124116)
Summary: We pass the `-nvptx-lower-global-ctor-dtor` option to support the `libc` like use-case which needs global constructors sometimes. This only affects the backend. If the NVPTX target is not enabled this option will be unknown which prevents you from compiling generic IR for this.
1 parent e28e935 commit 0c71fdd

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
639639
CmdArgs.push_back(
640640
Args.MakeArgString("--plugin-opt=-mattr=" + llvm::join(Features, ",")));
641641

642+
// Enable ctor / dtor lowering for the direct / freestanding NVPTX target.
643+
CmdArgs.append({"-mllvm", "--nvptx-lower-global-ctor-dtor"});
644+
642645
// Add paths for the default clang library path.
643646
SmallString<256> DefaultLibPath =
644647
llvm::sys::path::parent_path(TC.getDriver().Dir);
@@ -783,7 +786,7 @@ void NVPTXToolChain::addClangTargetOptions(
783786
// If we are compiling with a standalone NVPTX toolchain we want to try to
784787
// mimic a standard environment as much as possible. So we enable lowering
785788
// ctor / dtor functions to global symbols that can be registered.
786-
if (Freestanding)
789+
if (Freestanding && !getDriver().isUsingLTO())
787790
CC1Args.append({"-mllvm", "--nvptx-lower-global-ctor-dtor"});
788791
}
789792

clang/test/Driver/cuda-cross-compiling.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@
6363
//
6464
// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_70 %s -### 2>&1 \
6565
// RUN: | FileCheck -check-prefix=LOWERING %s
66+
// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_70 -flto -c %s -### 2>&1 \
67+
// RUN: | FileCheck -check-prefix=LOWERING-LTO %s
6668

6769
// LOWERING: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-mllvm" "--nvptx-lower-global-ctor-dtor"
70+
// LOWERING: clang-nvlink-wrapper{{.*}} "-mllvm" "--nvptx-lower-global-ctor-dtor"
71+
// LOWERING-LTO-NOT: "--nvptx-lower-global-ctor-dtor"
6872

6973
//
7074
// Test passing arguments directly to nvlink.

0 commit comments

Comments
 (0)