[Clang] Always use --no-undefined when linking AMDGPU images
authorJoseph Huber <jhuber6@vols.utk.edu>
Mon, 13 Mar 2023 14:33:08 +0000 (09:33 -0500)
committerJoseph Huber <jhuber6@vols.utk.edu>
Tue, 14 Mar 2023 18:11:33 +0000 (13:11 -0500)
AMDGPU uses ELF shared libraries to implement their executable device
images. One downside to this method is that it disables regular warnings
on undefined symbols. This is because shared libraries expect these to
be resolves by later loads. However, the GPU images do not support
dynamic linking so any undefined symbol is going to cause a runtime
error. This patch adds `--no-undefined` to the `ld.lld` invocation to guarantee
that undefined symbols are always caught as linking errors rather than
runtime errors.

Reviewed By: arsenm, MaskRay, #amdgpu

Differential Revision: https://reviews.llvm.org/D145941

clang/docs/ReleaseNotes.rst
clang/lib/Driver/ToolChains/AMDGPU.cpp
clang/test/Driver/amdgpu-toolchain-opencl.cl
clang/test/Driver/amdgpu-toolchain.c

index 5fae7ce..4a5d3f8 100644 (file)
@@ -231,6 +231,14 @@ Miscellaneous Clang Crashes Fixed
 Target Specific Changes
 -----------------------
 
+AMDGPU Support
+^^^^^^^^^^^^^^
+
+- Linking for AMDGPU now uses ``--no-undefined`` by default. This causes
+  undefined symbols in the created module to be a linker error. To prevent this,
+  pass ``-Wl,--undefined`` if compiling directly, or ``-Xoffload-linker
+  --undefined`` if using an offloading language.
+
 X86 Support
 ^^^^^^^^^^^
 
index 08496c1..99e2fb8 100644 (file)
@@ -543,6 +543,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   if (C.getDriver().isUsingLTO())
     addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
                   C.getDriver().getLTOMode() == LTOK_Thin);
+  CmdArgs.push_back("--no-undefined");
   CmdArgs.push_back("-shared");
   CmdArgs.push_back("-o");
   CmdArgs.push_back(Output.getFilename());
index 78c477b..c4038e0 100644 (file)
@@ -25,3 +25,6 @@
 
 // CHK-INC: "-cc1" {{.*}}"-finclude-default-header" "-fdeclare-opencl-builtins" {{.*}}"-x" "cl"
 // CHK-INC-NOT: "-cc1" {{.*}}"-finclude-default-header" "-fdeclare-opencl-builtins" {{.*}}"-x" "cpp-output"
+
+// RUN: %clang -### --target=amdgcn-amd-amdhsa-opencl -x cl -emit-llvm -mcpu=fiji %s 2>&1 | FileCheck -check-prefix=CHK-LINK %s
+// CHK-LINK: ld.lld{{.*}} "--no-undefined" "-shared"
index a00c75b..3477d50 100644 (file)
@@ -6,7 +6,7 @@
 // RUN: %clang -### -g --target=amdgcn-mesa-mesa3d -mcpu=kaveri %s 2>&1 | FileCheck -check-prefix=DWARF_VER %s
 
 // AS_LINK: "-cc1as"
-// AS_LINK: ld.lld{{.*}} "-shared"
+// AS_LINK: ld.lld{{.*}} "--no-undefined" "-shared"
 
 // DWARF_VER: "-dwarf-version=5"