Skip to content

Commit 92db44c

Browse files
committed
[Clang] Add standalone AMDGPU SPIR-V toolchain
Summary: The AMDGPU toolchain uses a different set of tools than the standard SPIR-V toolchain. The linker wrapper prefers to invoke a linker via a clang toolchain. To make that work we introduce `--target=spirv64-amd-amdhsa` so that it creates the linking phases that HIP prefers. Additionally, this can be used to make LLVM-IR / SPIR-V from C/C++ that can be linked with the HIP output.
1 parent e008538 commit 92db44c

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6849,11 +6849,17 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
68496849
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
68506850
break;
68516851
case llvm::Triple::AMDHSA: {
6852-
bool DL =
6853-
usesInput(Args, types::isOpenCL) || usesInput(Args, types::isLLVMIR);
6854-
TC = DL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
6855-
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6856-
Args);
6852+
if (Target.getArch() == llvm::Triple::spirv64) {
6853+
TC = std::make_unique<toolchains::SPIRVAMDToolChain>(*this, Target,
6854+
Args);
6855+
} else {
6856+
bool DL = usesInput(Args, types::isOpenCL) ||
6857+
usesInput(Args, types::isLLVMIR);
6858+
TC = DL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target,
6859+
Args)
6860+
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
6861+
Args);
6862+
}
68576863
break;
68586864
}
68596865
case llvm::Triple::AMDPAL:

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,17 @@ void HIPAMDToolChain::checkTargetID(
417417
getDriver().Diag(clang::diag::err_drv_bad_target_id)
418418
<< *PTID.OptionalTargetID;
419419
}
420+
421+
SPIRVAMDToolChain::SPIRVAMDToolChain(const Driver &D,
422+
const llvm::Triple &Triple,
423+
const ArgList &Args)
424+
: ROCMToolChain(D, Triple, Args) {
425+
// Lookup binaries into the driver directory, this is used to
426+
// discover the clang-offload-bundler executable.
427+
getProgramPaths().push_back(getDriver().Dir);
428+
}
429+
430+
Tool *SPIRVAMDToolChain::buildLinker() const {
431+
assert(getTriple().getArch() == llvm::Triple::spirv64);
432+
return new tools::AMDGCN::Linker(*this);
433+
}

clang/lib/Driver/ToolChains/HIPAMD.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ class LLVM_LIBRARY_VISIBILITY HIPAMDToolChain final : public ROCMToolChain {
9797
Tool *buildLinker() const override;
9898
};
9999

100+
class LLVM_LIBRARY_VISIBILITY SPIRVAMDToolChain final : public ROCMToolChain {
101+
public:
102+
SPIRVAMDToolChain(const Driver &D, const llvm::Triple &Triple,
103+
const llvm::opt::ArgList &Args);
104+
105+
protected:
106+
Tool *buildLinker() const override;
107+
};
108+
100109
} // end namespace toolchains
101110
} // end namespace driver
102111
} // end namespace clang
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang -### -ccc-print-phases --target=spirv64-amd-amdhsa %s 2>&1 \
2+
// RUN: | FileCheck %s --check-prefix=PHASES
3+
// PHASES: 0: input, "[[INPUT:.+]]", c
4+
// PHASES: 1: preprocessor, {0}, cpp-output
5+
// PHASES: 2: compiler, {1}, ir
6+
// PHASES: 3: backend, {2}, assembler
7+
// PHASES: 4: assembler, {3}, object
8+
// PHASES: 5: linker, {4}, image
9+
10+
// RUN: %clang -### -ccc-print-bindings --target=spirv64-amd-amdhsa %s 2>&1 \
11+
// RUN: | FileCheck %s --check-prefix=BINDINGS
12+
// BINDINGS: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[OUTPUT:.+]]"
13+
// BINDINGS: # "spirv64-amd-amdhsa" - "AMDGCN::Linker", inputs: ["[[OUTPUT]]"], output: "a.out"
14+
15+
// RUN: %clang -### --target=spirv64-amd-amdhsa %s -nogpulib -nogpuinc 2>&1 \
16+
// RUN: | FileCheck %s --check-prefix=INVOCATION
17+
// INVOCATION: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-o" "[[OUTPUT:.+]]" "-x" "c"
18+
// INVOCATION: "{{.*}}llvm-link" "-o" "a.out" "[[OUTPUT]]"
19+
// INVOCATION: "{{.*}}llvm-spirv" "--spirv-max-version=1.6" "--spirv-ext=+all" "--spirv-allow-unknown-intrinsics" "--spirv-lower-const-expr" "--spirv-preserve-auxdata" "--spirv-debug-info-version=nonsemantic-shader-200" "a.out" "-o" "a.out"

0 commit comments

Comments
 (0)