[clang] 6ff86f2 - [AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly (#99687)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 27 08:35:32 PST 2025


Author: Joseph Huber
Date: 2025-01-27T10:35:27-06:00
New Revision: 6ff86f2c0a5b58b07921ee895f0d16d8cd3d4015

URL: https://github.com/llvm/llvm-project/commit/6ff86f2c0a5b58b07921ee895f0d16d8cd3d4015
DIFF: https://github.com/llvm/llvm-project/commit/6ff86f2c0a5b58b07921ee895f0d16d8cd3d4015.diff

LOG: [AMDGPU] Use the AMDGPUToolChain when targeting C/C++ directly (#99687)

Summary:
The `getToolChain` pass uses the triple to determine which toolchain to
create. Currently the `amdgcn-amd-amdhsa` triple maps to the
`ROCmToolChain` which uses things expected to be provided by `ROCm`.
This is neded for OpenCL, but directly targeting C++ does not want this
since it's primarily being used for creating GPU runtime code. As far as
I know I'm the only user of this, so this shouldn't change anything.

Unfortunately, there's no good logic for detercting this, so I simply
checked ahead of time if the input is either `foo.cl` or `-x cl foo.c`
to choose between the two. This allows us to use the AMDGPU target
normally, as otherwise it will error without passing `-nogpulib`.

Added: 
    

Modified: 
    clang/lib/Driver/Driver.cpp
    clang/test/Driver/amdgpu-toolchain.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 87855fdb799710..a9bd52920d823f 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6669,9 +6669,21 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
     case llvm::Triple::CUDA:
       TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
       break;
-    case llvm::Triple::AMDHSA:
-      TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
+    case llvm::Triple::AMDHSA: {
+      bool CL = llvm::any_of(Args, [&](Arg *A) {
+        return (A->getOption().matches(options::OPT_x) &&
+                types::isOpenCL(
+                    types::lookupTypeForExtension(A->getValue()))) ||
+               (A->getOption().getKind() == Option::InputClass &&
+                StringRef(A->getValue()).rfind('.') != StringRef::npos &&
+                types::isOpenCL(types::lookupTypeForExtension(
+                    &A->getValue()[StringRef(A->getValue()).rfind('.') + 1])));
+      });
+      TC = CL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
+              : std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
+                                                              Args);
       break;
+    }
     case llvm::Triple::AMDPAL:
     case llvm::Triple::Mesa3D:
       TC = std::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args);

diff  --git a/clang/test/Driver/amdgpu-toolchain.c b/clang/test/Driver/amdgpu-toolchain.c
index c1c5aa8e90e686..3299930ee40dbb 100644
--- a/clang/test/Driver/amdgpu-toolchain.c
+++ b/clang/test/Driver/amdgpu-toolchain.c
@@ -36,3 +36,6 @@
 // RUN: %clang -target amdgcn-amd-amdhsa -march=gfx90a -stdlib -startfiles \
 // RUN:   -nogpulib -nogpuinc -### %s 2>&1 | FileCheck -check-prefix=STARTUP %s
 // STARTUP: ld.lld{{.*}}"-lc" "-lm" "{{.*}}crt1.o"
+//
+// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 %s 2>&1 | FileCheck -check-prefix=ROCM %s
+// ROCM-NOT: -mlink-builtin-bitcode


        


More information about the cfe-commits mailing list