[clang] [clang][NVPTX] exclude feature attributes from target-features (PR #215451)
Alex MacLean via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 10 20:24:34 PDT 2026
https://github.com/AlexMaclean created https://github.com/llvm/llvm-project/pull/215451
This info is already carried in `target-cpu` and the fact that these named features exist at all is something I hope to change.
>From bb61f93776ac2534c023a5f3bc87ffc33252871c Mon Sep 17 00:00:00 2001
From: Alex Maclean <amaclean at nvidia.com>
Date: Mon, 10 Aug 2026 20:21:40 -0700
Subject: [PATCH] [clang][NVPTX] exclude feature attributes from
target-features
---
clang/lib/Basic/Targets/NVPTX.cpp | 6 ++++++
clang/lib/CodeGen/CodeGenModule.cpp | 8 +++++---
clang/test/CodeGen/nvptx_attributes.c | 2 +-
3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Basic/Targets/NVPTX.cpp b/clang/lib/Basic/Targets/NVPTX.cpp
index 11577b75236a6..04b95dd19ed05 100644
--- a/clang/lib/Basic/Targets/NVPTX.cpp
+++ b/clang/lib/Basic/Targets/NVPTX.cpp
@@ -66,6 +66,12 @@ NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
NoAsmVariants = true;
GPU = OffloadArch::getUnused();
+ // Architectures are in the feature map only to gate builtins; the backend
+ // takes the architecture from `target-cpu`.
+#define NVPTX_GPU(NAME, KIND, VIRTUAL, SM_ID, MIN_VER, MAX_VER, SUFFIX) \
+ ReadOnlyFeatures.insert(NAME);
+#include "llvm/TargetParser/NVPTXTargetParser.def"
+
// PTX supports f16 as a fundamental type.
HasFastHalfType = true;
HasFloat16 = true;
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 5ed5385c90f26..0c0d1c272b386 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3417,9 +3417,11 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
llvm::erase_if(Features, [&](const std::string& F) {
return getTarget().isReadOnlyFeature(F.substr(1));
});
- llvm::sort(Features);
- Attrs.addAttribute("target-features", llvm::join(Features, ","));
- AddedAttr = true;
+ if (!Features.empty()) {
+ llvm::sort(Features);
+ Attrs.addAttribute("target-features", llvm::join(Features, ","));
+ AddedAttr = true;
+ }
}
// Add metadata for AArch64 Function Multi Versioning.
if (getTarget().getTriple().isAArch64()) {
diff --git a/clang/test/CodeGen/nvptx_attributes.c b/clang/test/CodeGen/nvptx_attributes.c
index c49a68b89bc64..fbba84ff05d5a 100644
--- a/clang/test/CodeGen/nvptx_attributes.c
+++ b/clang/test/CodeGen/nvptx_attributes.c
@@ -16,7 +16,7 @@ __attribute__((nvptx_kernel)) void foo(int *ret) {
}
//.
-// CHECK: attributes #[[ATTR0]] = { convergent noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="sm_61" "target-features"="+sm_61" }
+// CHECK: attributes #[[ATTR0]] = { convergent noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="sm_61" }
//.
// CHECK: [[META0:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
//.
More information about the cfe-commits
mailing list