[clang] 1be60cb - [Clang] [Driver] Normalize target features forwarded to external tools (#245200)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 21:16:17 PST 2026


Author: Rishabh
Date: 2026-11-25T23:16:12-06:00
New Revision: 1be60cbf47e9b102a653f9cb54fc965fe71a1ec4

URL: https://github.com/llvm/llvm-project/commit/1be60cbf47e9b102a653f9cb54fc965fe71a1ec4
DIFF: https://github.com/llvm/llvm-project/commit/1be60cbf47e9b102a653f9cb54fc965fe71a1ec4.diff

LOG: [Clang] [Driver] Normalize target features forwarded to external tools (#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

Signed-off-by: Rishabh Bali <rbali at qti.qualcomm.com>
Co-authored-by: Rishabh Bali <rbali at hu-rbali-hyd.qualcomm.com>

Added: 
    clang/test/Driver/amdgpu-linker-target-features.c

Modified: 
    clang/lib/Driver/ToolChains/AMDGPU.cpp
    clang/lib/Driver/ToolChains/Hexagon.cpp
    clang/test/Driver/hexagon-hlx.c
    clang/test/Driver/hexagon-hmx.c

Removed: 
    


################################################################################
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..9e2828a017afaf
--- /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=amdgpu10.1-amd-amdhsa -nostdlib \
+// RUN:   -mno-cumode -mcumode %s 2>&1 | FileCheck %s --check-prefix=ON
+// RUN: %clang -### --target=amdgpu10.1-amd-amdhsa -nostdlib \
+// RUN:   -mcumode -mno-cumode %s 2>&1 | FileCheck %s --check-prefix=OFF
+// RUN: %clang -### --target=amdgpu10.1-amd-amdhsa -nostdlib \
+// RUN:   -mcumode -mcumode %s 2>&1 | FileCheck %s --check-prefix=ON
+// RUN: %clang -### --target=amdgpu10.1-amd-amdhsa -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=amdgpu10.1-amd-amdhsa -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=amdgpu10.1-amd-amdhsa -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..03b498ffe3287a 100644
--- a/clang/test/Driver/hexagon-hlx.c
+++ b/clang/test/Driver/hexagon-hlx.c
@@ -31,11 +31,19 @@
 // RUN:  -fno-integrated-as 2>&1 | FileCheck -check-prefix=CHECK-AS-HLX %s
 // RUN: %clang -c %s -### --target=hexagon-unknown-elf -mv85 -mno-hlx \
 // RUN:  -fno-integrated-as 2>&1 | FileCheck -check-prefix=CHECK-AS-NOHLX %s
+
+// Normalize the external assembler's feature list: last option wins.
 // RUN: %clang -c %s -### --target=hexagon-unknown-elf -mv85 -mno-hlx -mhlx \
 // 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
 
+// Repeated identical options produce only one feature entry.
+// 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"
 // CHECK-HLX-DEFAULT-NOT: "-target-feature" "{{[+-]}}hlx"
@@ -46,9 +54,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