Skip to content

[Clang] Add standalone AMDGPU SPIR-V toolchain #144576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6849,11 +6849,17 @@ const ToolChain &Driver::getToolChain(const ArgList &Args,
TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
break;
case llvm::Triple::AMDHSA: {
bool DL =
usesInput(Args, types::isOpenCL) || usesInput(Args, types::isLLVMIR);
TC = DL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args)
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
Args);
if (Target.getArch() == llvm::Triple::spirv64) {
TC = std::make_unique<toolchains::SPIRVAMDToolChain>(*this, Target,
Args);
} else {
bool DL = usesInput(Args, types::isOpenCL) ||
usesInput(Args, types::isLLVMIR);
TC = DL ? std::make_unique<toolchains::ROCMToolChain>(*this, Target,
Args)
: std::make_unique<toolchains::AMDGPUToolChain>(*this, Target,
Args);
}
break;
}
case llvm::Triple::AMDPAL:
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Driver/ToolChains/HIPAMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,15 @@ void HIPAMDToolChain::checkTargetID(
getDriver().Diag(clang::diag::err_drv_bad_target_id)
<< *PTID.OptionalTargetID;
}

SPIRVAMDToolChain::SPIRVAMDToolChain(const Driver &D,
const llvm::Triple &Triple,
const ArgList &Args)
: ROCMToolChain(D, Triple, Args) {
getProgramPaths().push_back(getDriver().Dir);
}

Tool *SPIRVAMDToolChain::buildLinker() const {
assert(getTriple().getArch() == llvm::Triple::spirv64);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we use SPIRVAMDToolChain for spirv32 or spirv64 but we assert it's spirv64 here. Can spirv32 never hit this?

Should we add a spirv32 test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

32-bit SPIR-V isn't supported by AMD as far as I know, maybe @AlexVlx can clarify. Definitely shouldn't be creating the toolchain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert will do nothing w/o assertion enabled. if it definitely shouldn't be creating the toolchain, probably do a llvm_unreachable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm_unreachable will also do nothing without asserts enabled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied from elsewhere, and is mostly just a sanity check since it's impossible to hit this code path with 32-bit triples.

return new tools::AMDGCN::Linker(*this);
}
9 changes: 9 additions & 0 deletions clang/lib/Driver/ToolChains/HIPAMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ class LLVM_LIBRARY_VISIBILITY HIPAMDToolChain final : public ROCMToolChain {
Tool *buildLinker() const override;
};

class LLVM_LIBRARY_VISIBILITY SPIRVAMDToolChain final : public ROCMToolChain {
public:
SPIRVAMDToolChain(const Driver &D, const llvm::Triple &Triple,
const llvm::opt::ArgList &Args);

protected:
Tool *buildLinker() const override;
};

} // end namespace toolchains
} // end namespace driver
} // end namespace clang
Expand Down
19 changes: 19 additions & 0 deletions clang/test/Driver/spirv-amd-toolchain.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang -### -ccc-print-phases --target=spirv64-amd-amdhsa %s 2>&1 \
// RUN: | FileCheck %s --check-prefix=PHASES
// PHASES: 0: input, "[[INPUT:.+]]", c
// PHASES: 1: preprocessor, {0}, cpp-output
// PHASES: 2: compiler, {1}, ir
// PHASES: 3: backend, {2}, assembler
// PHASES: 4: assembler, {3}, object
// PHASES: 5: linker, {4}, image

// RUN: %clang -### -ccc-print-bindings --target=spirv64-amd-amdhsa %s 2>&1 \
// RUN: | FileCheck %s --check-prefix=BINDINGS
// BINDINGS: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[OUTPUT:.+]]"
// BINDINGS: # "spirv64-amd-amdhsa" - "AMDGCN::Linker", inputs: ["[[OUTPUT]]"], output: "a.out"

// RUN: %clang -### --target=spirv64-amd-amdhsa %s -nogpulib -nogpuinc 2>&1 \
// RUN: | FileCheck %s --check-prefix=INVOCATION
// INVOCATION: "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-o" "[[OUTPUT:.+]]" "-x" "c"
// INVOCATION: "{{.*}}llvm-link" "-o" "a.out" "[[OUTPUT]]"
// 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"
Loading