[clang] [clang][AMDGPU] Don't let builtin bitcode enable missing image insts (PR #242146)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 19:14:52 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
Author: Daniil Fukalov (dfukalov)
<details>
<summary>Changes</summary>
This is a follow-up to #<!-- -->238123.
ROCm device-libs are built without a target processor and mark their image functions with "image-insts" or "extended-image-insts". Functions linked with -mlink-builtin-bitcode keep their own target features, so on processors without image instructions they still claim image support. The backend then may crash, emit image instructions that the processor does not support, or silently delete the functions that need extended-image-insts.
Add TargetInfo::overridesBuiltinBitcodeFeature(). AMDGPU uses it to drop both features from linked functions when the AMDGCN processor is known and has no image instructions. Image operations such as write_imagef then produce an error on these processors.
Fixes: SC1-7154.
Assisted-by: Claude Opus 5.5
---
Full diff: https://github.com/llvm/llvm-project/pull/242146.diff
9 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+6)
- (modified) clang/include/clang/Basic/TargetInfo.h (+6)
- (modified) clang/include/clang/CodeGen/ModuleLinker.h (+6-6)
- (modified) clang/lib/Basic/Targets/AMDGPU.h (+7)
- (modified) clang/lib/CIR/FrontendAction/CIRGenAction.cpp (+1-1)
- (modified) clang/lib/CodeGen/CGCall.cpp (+14-11)
- (modified) clang/lib/CodeGen/CodeGenAction.cpp (+1-1)
- (modified) clang/lib/CodeGen/ModuleLinker.cpp (+2-3)
- (added) clang/test/CodeGen/link-builtin-bitcode-image-insts.c (+79)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 0f9e7e01636d7..f397aa9f76970 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1315,6 +1315,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- `__builtin_amdgcn_fcmp`
- `__builtin_amdgcn_fcmpf`
+- When linking device libraries (for example, ROCm device-libs) with
+ `-mlink-builtin-bitcode`, Clang no longer enables `image-insts` or
+ `extended-image-insts` on AMDGCN processors without image instructions, such
+ as gfx942, gfx950 and gfx1250. Image operations from these libraries are now
+ diagnosed instead of crashing the compiler or producing invalid code.
+
#### BPF Support
- Added `-mcpu=v5` and the `__BPF_FEATURE_HMUL` predefined macro for unsigned
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index a6523e8eaaa43..7eb89d578bbc4 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1591,6 +1591,12 @@ class TargetInfo : public TransferrableTargetInfo,
return ReadOnlyFeatures.count(Feature);
}
+ /// Determine whether the target's value of the given feature overrides the
+ /// one requested by builtin bitcode linked with -mlink-builtin-bitcode.
+ virtual bool overridesBuiltinBitcodeFeature(StringRef Feature) const {
+ return false;
+ }
+
/// Identify whether this target supports multiversioning of functions,
/// which requires support for cpu_supports and cpu_is functionality.
bool supportsMultiVersioning() const {
diff --git a/clang/include/clang/CodeGen/ModuleLinker.h b/clang/include/clang/CodeGen/ModuleLinker.h
index 6d973a46ce3da..cab587f8744a0 100644
--- a/clang/include/clang/CodeGen/ModuleLinker.h
+++ b/clang/include/clang/CodeGen/ModuleLinker.h
@@ -23,7 +23,7 @@ class BackendDiagnosticConsumer;
class CodeGenOptions;
class CompilerInstance;
class LangOptions;
-class TargetOptions;
+class TargetInfo;
/// Info about a module to link into the module currently being generated.
/// Shared between the classic clang CodeGen path and the ClangIR path.
@@ -50,13 +50,13 @@ bool loadLinkModules(CompilerInstance &CI, llvm::LLVMContext &Ctx,
bool linkInModules(llvm::Module &M,
llvm::SmallVectorImpl<LinkModule> &LinkModules,
const CodeGenOptions &CodeGenOpts,
- const LangOptions &LangOpts, const TargetOptions &TargetOpts,
+ const LangOptions &LangOpts, const TargetInfo &Target,
BackendDiagnosticConsumer &DiagConsumer);
namespace CodeGen {
-/// Adds attributes to \p F according to our \p CodeGenOpts and \p LangOpts, as
-/// though we had emitted it ourselves. We remove any attributes on F that
-/// conflict with the attributes we add here.
+/// Adds attributes to \p F according to our \p CodeGenOpts, \p LangOpts and
+/// \p Target, as though we had emitted it ourselves. We remove any attributes
+/// on F that conflict with the attributes we add here.
///
/// This is useful for adding attrs to bitcode modules that you want to link
/// with but don't control, such as CUDA's libdevice. When linking with such
@@ -74,7 +74,7 @@ namespace CodeGen {
void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F,
const CodeGenOptions &CodeGenOpts,
const LangOptions &LangOpts,
- const TargetOptions &TargetOpts,
+ const TargetInfo &Target,
bool WillInternalize);
} // namespace CodeGen
diff --git a/clang/lib/Basic/Targets/AMDGPU.h b/clang/lib/Basic/Targets/AMDGPU.h
index bf4ad80b291ec..ec2fa2bdbf50b 100644
--- a/clang/lib/Basic/Targets/AMDGPU.h
+++ b/clang/lib/Basic/Targets/AMDGPU.h
@@ -492,6 +492,13 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
bool hasHIPImageSupport() const override { return HasImage; }
+ // Builtin bitcode must not enable images on processors without them.
+ bool overridesBuiltinBitcodeFeature(StringRef Feature) const override {
+ return getTriple().isAMDGCN() && GPUKind != llvm::AMDGPU::GK_NONE &&
+ !HasImage &&
+ (Feature == "image-insts" || Feature == "extended-image-insts");
+ }
+
std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
// This is imprecise as the value can vary between 64, 128 (even 256!) bytes
// depending on the level of cache and the target architecture. We select
diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
index 9acaa6025fa07..046100f58b519 100644
--- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
+++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
@@ -192,7 +192,7 @@ static void emitCIRModuleOutput(
AddFunctionSourceLocations(*LLVMModule);
if (linkInModules(*LLVMModule, LinkModules, CGO, CI.getLangOpts(),
- CI.getTargetOpts(), DiagConsumer))
+ CI.getTarget(), DiagConsumer))
return;
const LangOptions &LangOpts = CI.getLangOpts();
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 1d7f9f742b87e..1b9222cf61408 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2488,27 +2488,28 @@ static void getTrivialDefaultFunctionAttributes(
}
}
-/// Merges `target-features` from \TargetOpts and \F, and sets the result in
-/// \FuncAttr
-/// * features from \F are always kept
-/// * a feature from \TargetOpts is kept if itself and its opposite are absent
-/// from \F
+/// Merge `target-features` from \p Target and \p F into \p FuncAttr.
+/// Keep function features that the target does not override, then add target
+/// features whose names are absent from the retained function features.
static void
overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder &FuncAttr,
const llvm::Function &F,
- const TargetOptions &TargetOpts) {
+ const TargetInfo &Target) {
+ const TargetOptions &TargetOpts = Target.getTargetOpts();
auto FFeatures = F.getFnAttribute("target-features");
llvm::StringSet<> MergedNames;
SmallVector<StringRef> MergedFeatures;
MergedFeatures.reserve(TargetOpts.Features.size());
- auto AddUnmergedFeatures = [&](auto &&FeatureRange) {
+ auto AddUnmergedFeatures = [&](auto &&FeatureRange, bool FromFunction) {
for (StringRef Feature : FeatureRange) {
if (Feature.empty())
continue;
assert(Feature[0] == '+' || Feature[0] == '-');
StringRef Name = Feature.drop_front(1);
+ if (FromFunction && Target.overridesBuiltinBitcodeFeature(Name))
+ continue;
bool Merged = !MergedNames.insert(Name).second;
if (!Merged)
MergedFeatures.push_back(Feature);
@@ -2516,8 +2517,9 @@ overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder &FuncAttr,
};
if (FFeatures.isValid())
- AddUnmergedFeatures(llvm::split(FFeatures.getValueAsString(), ','));
- AddUnmergedFeatures(TargetOpts.Features);
+ AddUnmergedFeatures(llvm::split(FFeatures.getValueAsString(), ','),
+ /*FromFunction=*/true);
+ AddUnmergedFeatures(TargetOpts.Features, /*FromFunction=*/false);
if (!MergedFeatures.empty()) {
llvm::sort(MergedFeatures);
@@ -2527,8 +2529,9 @@ overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder &FuncAttr,
void CodeGen::mergeDefaultFunctionDefinitionAttributes(
llvm::Function &F, const CodeGenOptions &CodeGenOpts,
- const LangOptions &LangOpts, const TargetOptions &TargetOpts,
+ const LangOptions &LangOpts, const TargetInfo &Target,
bool WillInternalize) {
+ const TargetOptions &TargetOpts = Target.getTargetOpts();
llvm::AttrBuilder FuncAttrs(F.getContext());
// Here we only extract the options that are relevant compared to the version
@@ -2567,7 +2570,7 @@ void CodeGen::mergeDefaultFunctionDefinitionAttributes(
F.removeFnAttrs(AttrsToRemove);
- overrideFunctionFeaturesWithTargetFeatures(FuncAttrs, F, TargetOpts);
+ overrideFunctionFeaturesWithTargetFeatures(FuncAttrs, F, Target);
F.addFnAttrs(FuncAttrs);
}
diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index c54cc093218c9..11ff06ce1a4d5 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -180,7 +180,7 @@ bool BackendConsumer::LinkInModules(llvm::Module *M) {
/*OnlyInternalized=*/true))
return true;
- return linkInModules(*M, LinkModules, CodeGenOpts, LangOpts, TargetOpts,
+ return linkInModules(*M, LinkModules, CodeGenOpts, LangOpts, CI.getTarget(),
DiagConsumer);
}
diff --git a/clang/lib/CodeGen/ModuleLinker.cpp b/clang/lib/CodeGen/ModuleLinker.cpp
index c8fdc5ad32605..0e844594788e2 100644
--- a/clang/lib/CodeGen/ModuleLinker.cpp
+++ b/clang/lib/CodeGen/ModuleLinker.cpp
@@ -58,8 +58,7 @@ bool clang::loadLinkModules(CompilerInstance &CI, llvm::LLVMContext &Ctx,
bool clang::linkInModules(llvm::Module &M,
llvm::SmallVectorImpl<LinkModule> &LinkModules,
const CodeGenOptions &CodeGenOpts,
- const LangOptions &LangOpts,
- const TargetOptions &TargetOpts,
+ const LangOptions &LangOpts, const TargetInfo &Target,
BackendDiagnosticConsumer &DiagConsumer) {
for (LinkModule &LM : LinkModules) {
assert(LM.Module && "LinkModule does not actually have a module");
@@ -71,7 +70,7 @@ bool clang::linkInModules(llvm::Module &M,
if (F.isIntrinsic())
continue;
CodeGen::mergeDefaultFunctionDefinitionAttributes(
- F, CodeGenOpts, LangOpts, TargetOpts, LM.Internalize);
+ F, CodeGenOpts, LangOpts, Target, LM.Internalize);
}
DiagConsumer.setCurLinkModule(LM.Module.get());
diff --git a/clang/test/CodeGen/link-builtin-bitcode-image-insts.c b/clang/test/CodeGen/link-builtin-bitcode-image-insts.c
new file mode 100644
index 0000000000000..7ba1ac38c19eb
--- /dev/null
+++ b/clang/test/CodeGen/link-builtin-bitcode-image-insts.c
@@ -0,0 +1,79 @@
+// Builtin bitcode is built without a processor and may enable image features on
+// its own. On a processor without images the linked copy follows the processor;
+// otherwise, and for other features, the library's features are kept.
+
+// RUN: %clang_cc1 -triple amdgpu-- -DBITCODE -emit-llvm-bc -o %t-lib.bc %s
+
+// RUN: %clang_cc1 -triple amdgpu12.50-- -emit-llvm -o - %s \
+// RUN: -mlink-builtin-bitcode %t-lib.bc | FileCheck %s \
+// RUN: --check-prefixes=NOIMAGE,GWS --implicit-check-not=+image-insts \
+// RUN: --implicit-check-not=+extended-image-insts
+// RUN: %clang_cc1 -triple amdgpu9.42-- -emit-llvm -o - %s \
+// RUN: -mlink-builtin-bitcode %t-lib.bc | FileCheck %s \
+// RUN: --check-prefix=NOIMAGE --implicit-check-not=+image-insts \
+// RUN: --implicit-check-not=+extended-image-insts
+// RUN: %clang_cc1 -triple amdgpu12.50-- -emit-llvm -o - %s \
+// RUN: -mlink-builtin-bitcode-postopt -mlink-builtin-bitcode %t-lib.bc \
+// RUN: | FileCheck %s --check-prefixes=NOIMAGE,GWS \
+// RUN: --implicit-check-not=+image-insts \
+// RUN: --implicit-check-not=+extended-image-insts
+// RUN: %clang_cc1 -triple amdgpu9.0a-- -emit-llvm -o - %s \
+// RUN: -mlink-builtin-bitcode %t-lib.bc | FileCheck %s --check-prefix=IMAGE
+// RUN: %clang_cc1 -triple amdgpu-- -emit-llvm -o - %s \
+// RUN: -mlink-builtin-bitcode %t-lib.bc | FileCheck %s --check-prefix=IMAGE
+// RUN: %clang_cc1 -triple r600-- -DBITCODE -emit-llvm-bc -o %t-r600-lib.bc %s
+// RUN: %clang_cc1 -triple r600-- -target-cpu cypress -emit-llvm -o - %s \
+// RUN: -mlink-builtin-bitcode %t-r600-lib.bc \
+// RUN: | FileCheck %s --check-prefix=IMAGE
+// RUN: %if cir-enabled %{ %clang_cc1 -triple amdgpu12.50-- -fclangir \
+// RUN: -emit-llvm -o - %s -mlink-builtin-bitcode %t-lib.bc | FileCheck %s \
+// RUN: --check-prefixes=NOIMAGE,GWS --implicit-check-not=+image-insts \
+// RUN: --implicit-check-not=+extended-image-insts %}
+
+// The linked image operation is then diagnosed instead of being selected.
+// RUN: %clang_cc1 -triple amdgpu-- -DBITCODE -DIMAGE_OP -emit-llvm-bc \
+// RUN: -o %t-op.bc %s
+// RUN: %if amdgpu-registered-target %{ not %clang_cc1 -triple amdgpu12.50-- \
+// RUN: -DIMAGE_OP -S -o /dev/null %s -mlink-builtin-bitcode %t-op.bc 2>&1 \
+// RUN: | FileCheck %s --check-prefix=ERR %}
+
+#ifdef IMAGE_OP
+typedef float float4 __attribute__((ext_vector_type(4)));
+#ifdef BITCODE
+__attribute__((target("image-insts")))
+float4 image_op(int x, __amdgpu_texture_t tex) {
+ return __builtin_amdgcn_image_load_2d_v4f32_i32(15, x, x, tex, 0, 0);
+}
+#else
+float4 image_op(int x, __amdgpu_texture_t tex);
+float4 foo(int x, __amdgpu_texture_t tex) { return image_op(x, tex); }
+#endif
+#elif defined(BITCODE)
+__attribute__((target("image-insts,16-bit-insts")))
+int image_fn(void) { return 1; }
+__attribute__((target("extended-image-insts"))) int ext_fn(void) { return 2; }
+__attribute__((target("gws"))) int gws_fn(void) { return 3; }
+#else
+int image_fn(void);
+int ext_fn(void);
+int gws_fn(void);
+int foo(void) { return image_fn() + ext_fn() + gws_fn(); }
+#endif
+
+// ERR: error: llvm.amdgcn.image.load.2d requires target feature 'image-insts'
+
+// NOIMAGE-LABEL: define internal i32 @image_fn(
+// NOIMAGE-SAME: #[[CPU:[0-9]+]]
+// NOIMAGE-LABEL: define internal i32 @ext_fn(
+// NOIMAGE-SAME: #[[CPU]]
+// GWS-LABEL: define internal i32 @gws_fn(
+// GWS-SAME: #[[GWS:[0-9]+]]
+// NOIMAGE: attributes #[[CPU]] = { {{.*}}"target-features"="{{.*}}+gfx9-insts
+// GWS: attributes #[[GWS]] = { {{.*}}"target-features"="{{.*}}+gws
+
+// IMAGE-LABEL: define internal i32 @image_fn(
+// IMAGE-SAME: #[[IMAGE:[0-9]+]]
+// IMAGE-LABEL: define internal i32 @ext_fn(
+// IMAGE-SAME: #[[EXT:[0-9]+]]
+// IMAGE: attributes #[[IMAGE]] = { {{.*}}"target-features"="{{.*}}+image-insts
+// IMAGE: attributes #[[EXT]] = { {{.*}}"target-features"="{{.*}}+extended-image-insts
``````````
</details>
https://github.com/llvm/llvm-project/pull/242146
More information about the cfe-commits
mailing list