[clang] clang/AMDGPU: Forward xnack/sramecc mode to the assembler (PR #212955)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 30 01:30:04 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
When assembling a .s file with no target ID directive, the requested
xnack/sramecc mode has no module flag to carry it. Forward the mode
requested via -mxnack/-msramecc (or the -mcpu target ID modifiers) to the
assembler as a target feature so it is recorded in the object's e_flags.
Co-authored-by: Claude (Opus 4.8) <noreply@<!-- -->anthropic.com>
---
Full diff: https://github.com/llvm/llvm-project/pull/212955.diff
4 Files Affected:
- (modified) clang/lib/Driver/ToolChains/AMDGPU.cpp (+18-1)
- (modified) clang/lib/Driver/ToolChains/AMDGPU.h (+2-1)
- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+1-1)
- (added) clang/test/Driver/amdgpu-assembler-xnack-sramecc.s (+33)
``````````diff
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 67981cd55d5e8..7bce060de0596 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -661,7 +661,8 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
const llvm::Triple &Triple,
const llvm::opt::ArgList &Args,
- std::vector<StringRef> &Features) {
+ std::vector<StringRef> &Features,
+ bool ForAS) {
if (Args.hasFlag(options::OPT_mwavefrontsize64,
options::OPT_mno_wavefrontsize64, false))
Features.push_back("+wavefrontsize64");
@@ -670,6 +671,22 @@ void amdgpu::getAMDGPUTargetFeatures(const Driver &D,
options::OPT_mno_amdgpu_precise_memory_op, false))
Features.push_back("+precise-memory");
+ // When assembling, the xnack/sramecc mode cannot come from a module flag
+ // (there is no module), so forward it to the assembler as a feature.
+ if (ForAS) {
+ if (Arg *A = Args.getLastArg(options::OPT_mxnack, options::OPT_mno_xnack)) {
+ Features.push_back(
+ A->getOption().matches(options::OPT_mxnack) ? "+xnack" : "-xnack");
+ }
+
+ if (Arg *A =
+ Args.getLastArg(options::OPT_msramecc, options::OPT_mno_sramecc)) {
+ Features.push_back(A->getOption().matches(options::OPT_msramecc)
+ ? "+sramecc"
+ : "-sramecc");
+ }
+ }
+
handleTargetFeaturesGroup(D, Triple, Args, Features,
options::OPT_m_amdgpu_Features_Group);
}
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.h b/clang/lib/Driver/ToolChains/AMDGPU.h
index 229c077a3943f..027a6e3b47dca 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.h
+++ b/clang/lib/Driver/ToolChains/AMDGPU.h
@@ -39,7 +39,8 @@ class LLVM_LIBRARY_VISIBILITY Linker final : public Tool {
void getAMDGPUTargetFeatures(const Driver &D, const llvm::Triple &Triple,
const llvm::opt::ArgList &Args,
- std::vector<StringRef> &Features);
+ std::vector<StringRef> &Features,
+ bool ForAS = false);
void addFullLTOPartitionOption(const Driver &D, const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 579e21d9c882f..a2b99dffc383e 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -906,7 +906,7 @@ void tools::getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
break;
case llvm::Triple::amdgpu:
case llvm::Triple::r600:
- amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features);
+ amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features, ForAS);
break;
case llvm::Triple::nvptx:
case llvm::Triple::nvptx64:
diff --git a/clang/test/Driver/amdgpu-assembler-xnack-sramecc.s b/clang/test/Driver/amdgpu-assembler-xnack-sramecc.s
new file mode 100644
index 0000000000000..d1c495e681f06
--- /dev/null
+++ b/clang/test/Driver/amdgpu-assembler-xnack-sramecc.s
@@ -0,0 +1,33 @@
+// REQUIRES: amdgpu-registered-target
+
+/// Assembling a .s file that has no target ID directive (.amdgcn_target). The
+/// xnack/sramecc mode requested on the command line, either with
+/// -mxnack/-msramecc or via the -mcpu target ID modifiers, is forwarded to the
+/// assembler as a target feature and recorded in the object's e_flags.
+
+/// The mode is forwarded to the assembler as a target feature.
+// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -mno-xnack -c %s 2>&1 | \
+// RUN: FileCheck -check-prefix=XNACK-OFF %s
+// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906:xnack- -c %s 2>&1 | \
+// RUN: FileCheck -check-prefix=XNACK-OFF %s
+// XNACK-OFF: "-cc1as"
+// XNACK-OFF-SAME: "-target-feature" "-xnack"
+
+// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -msramecc -c %s 2>&1 | \
+// RUN: FileCheck -check-prefix=SRAMECC-ON %s
+// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906:sramecc+ -c %s 2>&1 | \
+// RUN: FileCheck -check-prefix=SRAMECC-ON %s
+// SRAMECC-ON: "-cc1as"
+// SRAMECC-ON-SAME: "-target-feature" "+sramecc"
+
+/// End to end: the requested mode is reflected in the object's e_flags.
+// RUN: %clang --target=amdgcn-amd-amdhsa -mcpu=gfx906 -mno-xnack -c %s -o %t.o
+// RUN: llvm-readobj --file-headers %t.o | FileCheck -check-prefix=OBJ-XNACK-OFF %s
+// RUN: %clang --target=amdgcn-amd-amdhsa -mcpu=gfx906:xnack+ -c %s -o %t.o
+// RUN: llvm-readobj --file-headers %t.o | FileCheck -check-prefix=OBJ-XNACK-ON %s
+// RUN: %clang --target=amdgcn-amd-amdhsa -mcpu=gfx906:sramecc- -c %s -o %t.o
+// RUN: llvm-readobj --file-headers %t.o | FileCheck -check-prefix=OBJ-SRAMECC-OFF %s
+
+// OBJ-XNACK-OFF: EF_AMDGPU_FEATURE_XNACK_OFF_V4 (0x200)
+// OBJ-XNACK-ON: EF_AMDGPU_FEATURE_XNACK_ON_V4 (0x300)
+// OBJ-SRAMECC-OFF: EF_AMDGPU_FEATURE_SRAMECC_OFF_V4 (0x800)
``````````
</details>
https://github.com/llvm/llvm-project/pull/212955
More information about the cfe-commits
mailing list