[clang] [Clang] [Driver] Normalize target features forwarded to external tools (PR #245200)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 23 03:28:12 PST 2026
https://github.com/Ris-Bali created https://github.com/llvm/llvm-project/pull/245200
Use unifyTargetFeatures() before constructing -mattr= arguments for Hexagon external assembly and AMDGPU linker-plugin forwarding.
Keep the last selection for each feature and preserve the relative order of surviving entries, matching Clang's normal target-feature emission. Preserve existing empty-list handling; no overall behavior change is expected.
Fixes #245199
>From 6f950f4cc07b38ee3307ad799700da153504c6a1 Mon Sep 17 00:00:00 2001
From: Rishabh Bali <rbali at hu-rbali-hyd.qualcomm.com>
Date: Mon, 23 Nov 2026 16:55:53 +0530
Subject: [PATCH] [Clang] [Driver] Normalize target features forwarded to
external tools
Use unifyTargetFeatures() before constructing -mattr= arguments for
Hexagon external assembly and AMDGPU linker-plugin forwarding.
Keep the last selection for each feature and preserve the relative order
of surviving entries, matching Clang's normal target-feature emission.
Preserve existing empty-list handling; no overall behavior change is
expected.
Signed-off-by: Rishabh Bali <rbali at qti.qualcomm.com>
---
clang/lib/Driver/ToolChains/AMDGPU.cpp | 9 ++++---
clang/lib/Driver/ToolChains/Hexagon.cpp | 7 +++--
.../Driver/amdgpu-linker-target-features.c | 26 +++++++++++++++++++
clang/test/Driver/hexagon-hlx.c | 10 +++++--
clang/test/Driver/hexagon-hmx.c | 16 +++++++++---
5 files changed, 58 insertions(+), 10 deletions(-)
create mode 100644 clang/test/Driver/amdgpu-linker-target-features.c
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 7946383941aca1..3820bb613d7c7a 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -661,9 +661,12 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
std::vector<StringRef> Features;
getAMDGPUTargetFeatures(C.getDriver(), getToolChain().getEffectiveTriple(),
Args, Features);
- if (!Features.empty()) {
- CmdArgs.push_back(
- Args.MakeArgString("-plugin-opt=-mattr=" + llvm::join(Features, ",")));
+ // Keep only the last selection for each feature so the linker receives the
+ // same normalized list as Clang's target feature emission.
+ auto UnifiedFeatures = unifyTargetFeatures(Features);
+ if (!UnifiedFeatures.empty()) {
+ CmdArgs.push_back(Args.MakeArgString("-plugin-opt=-mattr=" +
+ llvm::join(UnifiedFeatures, ",")));
}
getToolChain().addProfileRTLibs(Args, CmdArgs);
diff --git a/clang/lib/Driver/ToolChains/Hexagon.cpp b/clang/lib/Driver/ToolChains/Hexagon.cpp
index 24fe4ef4a71968..e50247a9a0d5ef 100644
--- a/clang/lib/Driver/ToolChains/Hexagon.cpp
+++ b/clang/lib/Driver/ToolChains/Hexagon.cpp
@@ -249,9 +249,12 @@ void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
// -mattr=.
std::vector<StringRef> Features;
hexagon::getHexagonTargetFeatures(D, HTC.getTriple(), Args, Features);
- if (!Features.empty())
+ // Keep only the last selection for each feature, matching Clang's target
+ // feature emission instead of forwarding redundant entries to llvm-mc.
+ auto UnifiedFeatures = unifyTargetFeatures(Features);
+ if (!UnifiedFeatures.empty())
CmdArgs.push_back(
- Args.MakeArgString("-mattr=" + llvm::join(Features, ",")));
+ Args.MakeArgString("-mattr=" + llvm::join(UnifiedFeatures, ",")));
if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
CmdArgs.push_back(Args.MakeArgString("-gpsize=" + Twine(*G)));
diff --git a/clang/test/Driver/amdgpu-linker-target-features.c b/clang/test/Driver/amdgpu-linker-target-features.c
new file mode 100644
index 00000000000000..1654db6f066c59
--- /dev/null
+++ b/clang/test/Driver/amdgpu-linker-target-features.c
@@ -0,0 +1,26 @@
+// Normalize target features before forwarding them to the linker.
+// RUN: %clang -### --target=amdgcn-amd-amdpal -mcpu=gfx1010 -nostdlib \
+// RUN: -mno-cumode -mcumode %s 2>&1 | FileCheck %s --check-prefix=ON
+// RUN: %clang -### --target=amdgcn-amd-amdpal -mcpu=gfx1010 -nostdlib \
+// RUN: -mcumode -mno-cumode %s 2>&1 | FileCheck %s --check-prefix=OFF
+// RUN: %clang -### --target=amdgcn-amd-amdpal -mcpu=gfx1010 -nostdlib \
+// RUN: -mcumode -mcumode %s 2>&1 | FileCheck %s --check-prefix=ON
+// RUN: %clang -### --target=amdgcn-amd-amdpal -mcpu=gfx1010 -nostdlib \
+// RUN: -mno-cumode -mno-cumode %s 2>&1 | FileCheck %s --check-prefix=OFF
+
+// ON: "-target-feature" "+cumode"
+// ON: "-plugin-opt=-mattr=+cumode"
+// OFF: "-target-feature" "-cumode"
+// OFF: "-plugin-opt=-mattr=-cumode"
+
+// Preserve the relative order of surviving, independently selected features.
+// RUN: %clang -### --target=amdgcn-amd-amdpal -mcpu=gfx1010 -nostdlib \
+// RUN: -mcumode -mtgsplit -mno-cumode %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=ORDER
+// ORDER: "-plugin-opt=-mattr=+tgsplit,-cumode"
+
+// Do not emit an empty feature argument.
+// RUN: %clang -### --target=amdgcn-amd-amdpal -mcpu=gfx1010 -nostdlib \
+// RUN: %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// DEFAULT: ld.lld
+// DEFAULT-NOT: "-plugin-opt=-mattr=
diff --git a/clang/test/Driver/hexagon-hlx.c b/clang/test/Driver/hexagon-hlx.c
index 662260d0d1eb27..64ae6293eec673 100644
--- a/clang/test/Driver/hexagon-hlx.c
+++ b/clang/test/Driver/hexagon-hlx.c
@@ -35,6 +35,10 @@
// RUN: -fno-integrated-as 2>&1 | FileCheck -check-prefix=CHECK-AS-LAST-HLX %s
// RUN: %clang -c %s -### --target=hexagon-unknown-elf -mv85 -mhlx -mno-hlx \
// RUN: -fno-integrated-as 2>&1 | FileCheck -check-prefix=CHECK-AS-LAST-NOHLX %s
+// RUN: %clang -c %s -### --target=hexagon-unknown-elf -mv85 -mhlx -mhlx \
+// RUN: -fno-integrated-as 2>&1 | FileCheck -check-prefix=CHECK-AS-LAST-HLX %s
+// RUN: %clang -c %s -### --target=hexagon-unknown-elf -mv85 -mno-hlx -mno-hlx \
+// RUN: -fno-integrated-as 2>&1 | FileCheck -check-prefix=CHECK-AS-LAST-NOHLX %s
// CHECK-HLX-ON: "-target-feature" "+hlx"
// CHECK-HLX-OFF: "-target-feature" "-hlx"
@@ -46,9 +50,11 @@
// CHECK-AS-HLX-NOT: "-mhlx"
// CHECK-AS-NOHLX: llvm-mc{{.*}}"-mattr={{[^"]*}}-hlx{{[^"]*}}"
// CHECK-AS-NOHLX-NOT: "-mhlx"
-// CHECK-AS-LAST-HLX: llvm-mc{{.*}}"-mattr={{[^"]*}}-hlx,+hlx{{[^"]*}}"
+// CHECK-AS-LAST-HLX: llvm-mc{{.*}}"-mattr=+hlx,
+// CHECK-AS-LAST-HLX-NOT: {{[+-]}}hlx{{[,"]}}
// CHECK-AS-LAST-HLX-NOT: "-mhlx"
-// CHECK-AS-LAST-NOHLX: llvm-mc{{.*}}"-mattr={{[^"]*}}+hlx,-hlx{{[^"]*}}"
+// CHECK-AS-LAST-NOHLX: llvm-mc{{.*}}"-mattr=-hlx,
+// CHECK-AS-LAST-NOHLX-NOT: {{[+-]}}hlx{{[,"]}}
// CHECK-AS-LAST-NOHLX-NOT: "-mhlx"
// CHECK-HLX-V79: error: unsupported option '-mhlx' for target 'v79'
// CHECK-NOHLX-V79: error: unsupported option '-mno-hlx' for target 'v79'
diff --git a/clang/test/Driver/hexagon-hmx.c b/clang/test/Driver/hexagon-hmx.c
index 4bb1a457119807..4c3d9425d95117 100644
--- a/clang/test/Driver/hexagon-hmx.c
+++ b/clang/test/Driver/hexagon-hmx.c
@@ -20,7 +20,7 @@
// RUN: -fno-integrated-as 2>&1 | FileCheck \
// RUN: -check-prefix=CHECK-NOIAS-NO-HMX %s
-// Preserve option order in the external assembler's feature list.
+// Normalize the external assembler's feature list: last option wins.
// RUN: %clang -c %s -### --target=hexagon -mno-hmx -mhmx \
// RUN: -fno-integrated-as 2>&1 | FileCheck \
// RUN: -check-prefix=CHECK-NOIAS-LAST-HMX %s
@@ -28,6 +28,14 @@
// RUN: -fno-integrated-as 2>&1 | FileCheck \
// RUN: -check-prefix=CHECK-NOIAS-LAST-NO-HMX %s
+// Repeated identical options produce only one feature entry.
+// RUN: %clang -c %s -### --target=hexagon -mhmx -mhmx \
+// RUN: -fno-integrated-as 2>&1 | FileCheck \
+// RUN: -check-prefix=CHECK-NOIAS-LAST-HMX %s
+// RUN: %clang -c %s -### --target=hexagon -mno-hmx -mno-hmx \
+// RUN: -fno-integrated-as 2>&1 | FileCheck \
+// RUN: -check-prefix=CHECK-NOIAS-LAST-NO-HMX %s
+
// RUN: %clang --target=hexagon --help 2>&1 \
// RUN: | FileCheck %s -check-prefix CHECK-HELP
@@ -45,9 +53,11 @@
// CHECK-NOIAS-HMX-NOT: "-mhmx"
// CHECK-NOIAS-NO-HMX: llvm-mc{{.*}}"-mattr={{[^"]*}}-hmx{{[^"]*}}"
// CHECK-NOIAS-NO-HMX-NOT: "-mhmx"
-// CHECK-NOIAS-LAST-HMX: llvm-mc{{.*}}"-mattr={{[^"]*}}-hmx,+hmx{{[^"]*}}"
+// CHECK-NOIAS-LAST-HMX: llvm-mc{{.*}}"-mattr=+hmx,
+// CHECK-NOIAS-LAST-HMX-NOT: {{[+-]}}hmx{{[,"]}}
// CHECK-NOIAS-LAST-HMX-NOT: "-mhmx"
-// CHECK-NOIAS-LAST-NO-HMX: llvm-mc{{.*}}"-mattr={{[^"]*}}+hmx,-hmx{{[^"]*}}"
+// CHECK-NOIAS-LAST-NO-HMX: llvm-mc{{.*}}"-mattr=-hmx,
+// CHECK-NOIAS-LAST-NO-HMX-NOT: {{[+-]}}hmx{{[,"]}}
// CHECK-NOIAS-LAST-NO-HMX-NOT: "-mhmx"
// CHECK-HELP: hmx
More information about the cfe-commits
mailing list