[clang] [llvm] [HLSL][SPIR-V] Implement vk::ext_builtin_input attribute (PR #138530)
Nathan Gauër via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 4 02:37:48 PDT 2025
https://github.com/Keenuts updated https://github.com/llvm/llvm-project/pull/138530
>From 8c405fefdb31200930b9a690df635aff7775f602 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Wed, 30 Apr 2025 11:06:55 +0200
Subject: [PATCH 1/8] [HLSL] Implement vk::ext_builtin_input attribute
This variable attribute is used in HLSL to add Vulkan specific builtins
in a shader.
The attribute is documented here:
https://github.com/microsoft/hlsl-specs/blob/17727e88fd1cb09013cb3a144110826af05f4dd5/proposals/0011-inline-spirv.md
Those variable, even if marked as `static` are externally initialized by
the pipeline/driver/GPU. This is handled by moving them to a specific
address space `hlsl_input`, also added by this commit.
The design for input variables in Clang can be found here:
https://github.com/llvm/wg-hlsl/blob/355771361ef69259fef39a65caef8bff9cb4046d/proposals/0019-spirv-input-builtin.md
Related to #136920
---
clang/include/clang/Basic/AddressSpaces.h | 1 +
clang/include/clang/Basic/Attr.td | 13 +++++++++++
clang/include/clang/Basic/AttrDocs.td | 22 +++++++++++++++++++
.../include/clang/Basic/AttributeCommonInfo.h | 2 +-
clang/include/clang/Sema/SemaHLSL.h | 2 ++
clang/lib/AST/Type.cpp | 1 +
clang/lib/AST/TypePrinter.cpp | 2 ++
clang/lib/Basic/Attributes.cpp | 1 +
clang/lib/Basic/TargetInfo.cpp | 2 ++
clang/lib/Basic/Targets/AArch64.h | 1 +
clang/lib/Basic/Targets/AMDGPU.cpp | 2 ++
clang/lib/Basic/Targets/DirectX.h | 1 +
clang/lib/Basic/Targets/NVPTX.h | 1 +
clang/lib/Basic/Targets/SPIR.h | 2 ++
clang/lib/Basic/Targets/SystemZ.h | 1 +
clang/lib/Basic/Targets/TCE.h | 1 +
clang/lib/Basic/Targets/WebAssembly.h | 1 +
clang/lib/Basic/Targets/X86.h | 1 +
clang/lib/CodeGen/CGHLSLRuntime.cpp | 11 ++++++++++
clang/lib/CodeGen/CGHLSLRuntime.h | 1 +
clang/lib/CodeGen/CodeGenModule.cpp | 17 ++++++++++++++
clang/lib/Sema/SemaDecl.cpp | 6 +++++
clang/lib/Sema/SemaDeclAttr.cpp | 3 +++
clang/lib/Sema/SemaHLSL.cpp | 14 ++++++++++++
clang/test/CodeGenHLSL/vk-input-builtin.hlsl | 14 ++++++++++++
.../SemaTemplate/address_space-dependent.cpp | 4 ++--
26 files changed, 124 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGenHLSL/vk-input-builtin.hlsl
diff --git a/clang/include/clang/Basic/AddressSpaces.h b/clang/include/clang/Basic/AddressSpaces.h
index 519d959bb636c..48e4a1c61fe02 100644
--- a/clang/include/clang/Basic/AddressSpaces.h
+++ b/clang/include/clang/Basic/AddressSpaces.h
@@ -61,6 +61,7 @@ enum class LangAS : unsigned {
hlsl_constant,
hlsl_private,
hlsl_device,
+ hlsl_input,
// Wasm specific address spaces.
wasm_funcref,
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 216084344c00d..917e6eec08e0f 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -140,6 +140,11 @@ def SharedVar : SubsetSubject<Var,
[{S->hasGlobalStorage() && !S->getTLSKind()}],
"global variables">;
+def HLSLInputBuiltin : SubsetSubject<Var, [{S->hasGlobalStorage() &&
+ S->getStorageClass()==StorageClass::SC_Static &&
+ S->getType().isConstQualified()}],
+ "input builtin">;
+
def GlobalVar : SubsetSubject<Var,
[{S->hasGlobalStorage()}], "global variables">;
@@ -4951,6 +4956,14 @@ def HLSLWaveSize: InheritableAttr {
let Documentation = [WaveSizeDocs];
}
+def HLSLVkExtBuiltinInput : InheritableAttr {
+ let Spellings = [CXX11<"vk", "ext_builtin_input">];
+ let Args = [IntArgument<"BuiltIn">];
+ let Subjects = SubjectList<[HLSLInputBuiltin], ErrorDiag>;
+ let LangOpts = [HLSL];
+ let Documentation = [HLSLVkExtBuiltinInputDocs];
+}
+
def RandomizeLayout : InheritableAttr {
let Spellings = [GCC<"randomize_layout">];
let Subjects = SubjectList<[Record]>;
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 65d66dd398ad1..1f8d8755e245c 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8508,6 +8508,28 @@ and copied back to the argument after the callee returns.
}];
}
+def HLSLVkExtBuiltinInputDocs : Documentation {
+ let Category = DocCatVariable;
+ let Content = [{
+Vulkan shaders have `Input` builtins. Those variables are externally
+initialized by the driver/pipeline, but each copy is private to the current
+lane.
+
+Those builtins can be declared using the `[[vk::ext_builtin_input]]` attribute
+like follows:
+
+.. code-block:: c++
+ [[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
+ static const uint3 groupid;
+
+This variable will be lowered into a module-level variable, with the `Input`
+storage class, and the `BuiltIn 26` decoration.
+
+The full documentation for this inline SPIR-V attribute can be found here:
+https://github.com/microsoft/hlsl-specs/blob/main/proposals/0011-inline-spirv.md
+ }];
+}
+
def AnnotateTypeDocs : Documentation {
let Category = DocCatType;
let Heading = "annotate_type";
diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h
index b4b8345b4ed40..34fc774362557 100644
--- a/clang/include/clang/Basic/AttributeCommonInfo.h
+++ b/clang/include/clang/Basic/AttributeCommonInfo.h
@@ -69,7 +69,7 @@ class AttributeCommonInfo {
IgnoredAttribute,
UnknownAttribute,
};
- enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
+ enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, VK, GSL, RISCV };
enum class AttrArgsInfo {
None,
Optional,
diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h
index 15182bb27bbdf..66d09f49680be 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -131,6 +131,8 @@ class SemaHLSL : public SemaBase {
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
+ void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
+
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
QualType ProcessResourceTypeAttributes(QualType Wrapped);
HLSLAttributedResourceLocInfo
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 1ab18a5c26c0f..47da52302ed5e 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -100,6 +100,7 @@ bool Qualifiers::isTargetAddressSpaceSupersetOf(LangAS A, LangAS B,
// address spaces to default to work around this problem.
(A == LangAS::Default && B == LangAS::hlsl_private) ||
(A == LangAS::Default && B == LangAS::hlsl_device) ||
+ (A == LangAS::Default && B == LangAS::hlsl_input) ||
// Conversions from target specific address spaces may be legal
// depending on the target information.
Ctx.getTargetInfo().isAddressSpaceSupersetOf(A, B);
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp
index 4793ef38c2c46..8375558b98ba9 100644
--- a/clang/lib/AST/TypePrinter.cpp
+++ b/clang/lib/AST/TypePrinter.cpp
@@ -2667,6 +2667,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
return "hlsl_private";
case LangAS::hlsl_device:
return "hlsl_device";
+ case LangAS::hlsl_input:
+ return "hlsl_input";
case LangAS::wasm_funcref:
return "__funcref";
default:
diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp
index bcd2c9bd45040..905046685934b 100644
--- a/clang/lib/Basic/Attributes.cpp
+++ b/clang/lib/Basic/Attributes.cpp
@@ -210,6 +210,7 @@ getScopeFromNormalizedScopeName(StringRef ScopeName) {
.Case("gnu", AttributeCommonInfo::Scope::GNU)
.Case("gsl", AttributeCommonInfo::Scope::GSL)
.Case("hlsl", AttributeCommonInfo::Scope::HLSL)
+ .Case("vk", AttributeCommonInfo::Scope::VK)
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
.Case("omp", AttributeCommonInfo::Scope::OMP)
.Case("riscv", AttributeCommonInfo::Scope::RISCV);
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index a82573b5b43f9..7b577632fdf55 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -49,6 +49,8 @@ static const LangASMap FakeAddrSpaceMap = {
13, // hlsl_groupshared
14, // hlsl_constant
15, // hlsl_private
+ 16, // hlsl_device
+ 17, // hlsl_input
20, // wasm_funcref
};
diff --git a/clang/lib/Basic/Targets/AArch64.h b/clang/lib/Basic/Targets/AArch64.h
index 7230f22d5bb86..a4c65361105e4 100644
--- a/clang/lib/Basic/Targets/AArch64.h
+++ b/clang/lib/Basic/Targets/AArch64.h
@@ -47,6 +47,7 @@ static const unsigned ARM64AddrSpaceMap[] = {
0, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/lib/Basic/Targets/AMDGPU.cpp b/clang/lib/Basic/Targets/AMDGPU.cpp
index 9a595c5e6c5c6..8d5489becc02c 100644
--- a/clang/lib/Basic/Targets/AMDGPU.cpp
+++ b/clang/lib/Basic/Targets/AMDGPU.cpp
@@ -62,6 +62,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
// will break loudly.
llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_private
llvm::AMDGPUAS::GLOBAL_ADDRESS, // hlsl_device
+ llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_input
};
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
@@ -89,6 +90,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
llvm::AMDGPUAS::CONSTANT_ADDRESS, // hlsl_constant
llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_private
llvm::AMDGPUAS::GLOBAL_ADDRESS, // hlsl_device
+ llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_input
};
} // namespace targets
} // namespace clang
diff --git a/clang/lib/Basic/Targets/DirectX.h b/clang/lib/Basic/Targets/DirectX.h
index 566acd6bb3cf3..1729a014c2dec 100644
--- a/clang/lib/Basic/Targets/DirectX.h
+++ b/clang/lib/Basic/Targets/DirectX.h
@@ -45,6 +45,7 @@ static const unsigned DirectXAddrSpaceMap[] = {
2, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/lib/Basic/Targets/NVPTX.h b/clang/lib/Basic/Targets/NVPTX.h
index fbb46001b0f90..33c29586359be 100644
--- a/clang/lib/Basic/Targets/NVPTX.h
+++ b/clang/lib/Basic/Targets/NVPTX.h
@@ -49,6 +49,7 @@ static const unsigned NVPTXAddrSpaceMap[] = {
0, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index ef511d045f576..34c5c67f1b739 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -50,6 +50,7 @@ static const unsigned SPIRDefIsPrivMap[] = {
12, // hlsl_constant
10, // hlsl_private
11, // hlsl_device
+ 12, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
@@ -85,6 +86,7 @@ static const unsigned SPIRDefIsGenMap[] = {
0, // hlsl_constant
10, // hlsl_private
11, // hlsl_device
+ 12, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/lib/Basic/Targets/SystemZ.h b/clang/lib/Basic/Targets/SystemZ.h
index 1f69530c4757f..6431be0b505ce 100644
--- a/clang/lib/Basic/Targets/SystemZ.h
+++ b/clang/lib/Basic/Targets/SystemZ.h
@@ -45,6 +45,7 @@ static const unsigned ZOSAddressMap[] = {
0, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
0 // wasm_funcref
};
diff --git a/clang/lib/Basic/Targets/TCE.h b/clang/lib/Basic/Targets/TCE.h
index f6e582120447f..005cab9819472 100644
--- a/clang/lib/Basic/Targets/TCE.h
+++ b/clang/lib/Basic/Targets/TCE.h
@@ -54,6 +54,7 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
0, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/lib/Basic/Targets/WebAssembly.h b/clang/lib/Basic/Targets/WebAssembly.h
index 04f0cb5df4601..d5aee5c0bd0eb 100644
--- a/clang/lib/Basic/Targets/WebAssembly.h
+++ b/clang/lib/Basic/Targets/WebAssembly.h
@@ -45,6 +45,7 @@ static const unsigned WebAssemblyAddrSpaceMap[] = {
0, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
20, // wasm_funcref
};
diff --git a/clang/lib/Basic/Targets/X86.h b/clang/lib/Basic/Targets/X86.h
index 780385f9c9bc5..babea81758d52 100644
--- a/clang/lib/Basic/Targets/X86.h
+++ b/clang/lib/Basic/Targets/X86.h
@@ -49,6 +49,7 @@ static const unsigned X86AddrSpaceMap[] = {
0, // hlsl_constant
0, // hlsl_private
0, // hlsl_device
+ 0, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index cfe9dc1192d9d..c00010c7dd1fb 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -623,6 +623,17 @@ void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl,
}
}
+void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
+ llvm::GlobalVariable *GV) {
+ if (auto Attr = VD->getAttr<HLSLVkExtBuiltinInputAttr>()) {
+ LLVMContext &Ctx = GV->getContext();
+ IRBuilder<> B(GV->getContext());
+ MDNode *Val = MDNode::get(
+ Ctx, {ConstantAsMetadata::get(B.getInt32(Attr->getBuiltIn()))});
+ GV->addMetadata("spv.builtin", *Val);
+ }
+}
+
llvm::Instruction *CGHLSLRuntime::getConvergenceToken(BasicBlock &BB) {
if (!CGM.shouldEmitConvergenceTokens())
return nullptr;
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index 34c6b8d0a336a..f7ca2077a576b 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -157,6 +157,7 @@ class CGHLSLRuntime {
void emitEntryFunction(const FunctionDecl *FD, llvm::Function *Fn);
void setHLSLFunctionAttributes(const FunctionDecl *FD, llvm::Function *Fn);
+ void handleGlobalVarDefinition(const VarDecl *VD, llvm::GlobalVariable *Var);
llvm::Instruction *getConvergenceToken(llvm::BasicBlock &BB);
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 9383c57d3c991..7dc6a1f46e473 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5638,6 +5638,8 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
else if (D->hasAttr<LoaderUninitializedAttr>())
Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
+ else if (GetGlobalVarAddressSpace(D) == LangAS::hlsl_input)
+ Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
else if (!InitExpr) {
// This is a tentative definition; tentative definitions are
// implicitly initialized with { 0 }.
@@ -5761,9 +5763,18 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
getCUDARuntime().internalizeDeviceSideVar(D, Linkage);
}
getCUDARuntime().handleVarRegistration(D, *GV);
+ } else if (LangOpts.HLSL &&
+ GetGlobalVarAddressSpace(D) == LangAS::hlsl_input) {
+ // HLSL Input variables are considered to be set by the driver/pipeline, but
+ // only visible to a single thread/wave.
+ GV->setExternallyInitialized(true);
}
GV->setInitializer(Init);
+
+ if (LangOpts.HLSL)
+ getHLSLRuntime().handleGlobalVarDefinition(D, GV);
+
if (emitter)
emitter->finalize(GV);
@@ -5806,6 +5817,12 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
!D->hasAttr<ConstInitAttr>())
Linkage = llvm::GlobalValue::InternalLinkage;
+ // HLSL variables in the input address space maps like memory-mapped
+ // variables. Even if they are 'static', they are externally initialized and
+ // read/write by the hardware/driver/pipeline.
+ if (LangOpts.HLSL && GetGlobalVarAddressSpace(D) == LangAS::hlsl_input)
+ Linkage = llvm::GlobalValue::ExternalLinkage;
+
GV->setLinkage(Linkage);
if (D->hasAttr<DLLImportAttr>())
GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 86b871396ec90..c662b0edbf2ac 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14419,6 +14419,12 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
if (getLangOpts().HLSL && HLSL().ActOnUninitializedVarDecl(Var))
return;
+ // HLSL input variables are expected to be externally initialized, even
+ // when marked `static`.
+ if (getLangOpts().HLSL &&
+ Var->getType().getAddressSpace() == LangAS::hlsl_input)
+ return;
+
// C++03 [dcl.init]p9:
// If no initializer is specified for an object, and the
// object is of (possibly cv-qualified) non-POD class type (or
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index a8e4023fb02ba..c829a4426b9f7 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7510,6 +7510,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_HLSLWaveSize:
S.HLSL().handleWaveSizeAttr(D, AL);
break;
+ case ParsedAttr::AT_HLSLVkExtBuiltinInput:
+ S.HLSL().handleVkExtBuiltinInputAttr(D, AL);
+ break;
case ParsedAttr::AT_HLSLSV_GroupThreadID:
S.HLSL().handleSV_GroupThreadIDAttr(D, AL);
break;
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 43db85594de3d..6058d3f135b51 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1117,6 +1117,12 @@ void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {
D->addAttr(NewAttr);
}
+void SemaHLSL::handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL) {
+ IntegerLiteral *IL = cast<IntegerLiteral>(AL.getArgAsExpr(0));
+ D->addAttr(::new (getASTContext()) HLSLVkExtBuiltinInputAttr(
+ getASTContext(), AL, IL->getValue().getZExtValue()));
+}
+
bool SemaHLSL::diagnoseInputIDType(QualType T, const ParsedAttr &AL) {
const auto *VT = T->getAs<VectorType>();
@@ -3158,6 +3164,14 @@ void SemaHLSL::deduceAddressSpace(VarDecl *Decl) {
return;
QualType Type = Decl->getType();
+
+ if (Decl->hasAttr<HLSLVkExtBuiltinInputAttr>()) {
+ LangAS ImplAS = LangAS::hlsl_input;
+ Type = SemaRef.getASTContext().getAddrSpaceQualType(Type, ImplAS);
+ Decl->setType(Type);
+ return;
+ }
+
if (Type->isSamplerT() || Type->isVoidType())
return;
diff --git a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
new file mode 100644
index 0000000000000..108aaaf9d78b9
--- /dev/null
+++ b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN: spirv-unknown-vulkan1.3-compute %s -emit-llvm -O3 -o - | FileCheck %s
+
+[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
+static const uint3 groupid;
+// CHECK: @_ZL7groupid = local_unnamed_addr addrspace(12) externally_initialized constant <3 x i32> undef, align 16, !spv.builtin [[META0:![0-9]+]]
+
+RWStructuredBuffer<int> output : register(u1, space0);
+
+[numthreads(1, 1, 1)]
+void main() {
+ output[0] = groupid;
+}
+// CHECK: [[META0]] = !{i32 26}
diff --git a/clang/test/SemaTemplate/address_space-dependent.cpp b/clang/test/SemaTemplate/address_space-dependent.cpp
index 4eabcfa0fcf21..e17bf60e6a200 100644
--- a/clang/test/SemaTemplate/address_space-dependent.cpp
+++ b/clang/test/SemaTemplate/address_space-dependent.cpp
@@ -43,7 +43,7 @@ void neg() {
template <long int I>
void tooBig() {
- __attribute__((address_space(I))) int *bounds; // expected-error {{address space is larger than the maximum supported (8388583)}}
+ __attribute__((address_space(I))) int *bounds; // expected-error {{address space is larger than the maximum supported (8388582)}}
}
template <long int I>
@@ -101,7 +101,7 @@ int main() {
car<1, 2, 3>(); // expected-note {{in instantiation of function template specialization 'car<1, 2, 3>' requested here}}
HasASTemplateFields<1> HASTF;
neg<-1>(); // expected-note {{in instantiation of function template specialization 'neg<-1>' requested here}}
- correct<0x7FFFE7>();
+ correct<0x7FFFE6>();
tooBig<8388650>(); // expected-note {{in instantiation of function template specialization 'tooBig<8388650L>' requested here}}
__attribute__((address_space(1))) char *x;
>From e6b5c72a7f6195698ec26be0f887d375b92c3dfb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Mon, 5 May 2025 15:44:59 +0200
Subject: [PATCH 2/8] fix address space mapping for SPIR
---
clang/lib/Basic/Targets/SPIR.h | 4 ++--
clang/test/CodeGenHLSL/vk-input-builtin.hlsl | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index 34c5c67f1b739..470e578520939 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -50,7 +50,7 @@ static const unsigned SPIRDefIsPrivMap[] = {
12, // hlsl_constant
10, // hlsl_private
11, // hlsl_device
- 12, // hlsl_input
+ 7, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
@@ -86,7 +86,7 @@ static const unsigned SPIRDefIsGenMap[] = {
0, // hlsl_constant
10, // hlsl_private
11, // hlsl_device
- 12, // hlsl_input
+ 7, // hlsl_input
// Wasm address space values for this target are dummy values,
// as it is only enabled for Wasm targets.
20, // wasm_funcref
diff --git a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
index 108aaaf9d78b9..2e8a854956100 100644
--- a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
+++ b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
@@ -3,7 +3,7 @@
[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
static const uint3 groupid;
-// CHECK: @_ZL7groupid = local_unnamed_addr addrspace(12) externally_initialized constant <3 x i32> undef, align 16, !spv.builtin [[META0:![0-9]+]]
+// CHECK: @_ZL7groupid = local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32> undef, align 16, !spv.builtin [[META0:![0-9]+]]
RWStructuredBuffer<int> output : register(u1, space0);
>From d74d523d2e8215be23bb394bda3bd2e2c7b93caf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Mon, 5 May 2025 15:58:33 +0200
Subject: [PATCH 3/8] reuse spirv.Decorations metadata
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 8 +++---
clang/test/CodeGenHLSL/vk-input-builtin.hlsl | 5 ++--
.../hlsl-intrinsics/vk-ext-builtin-input.ll | 26 +++++++++++++++++++
3 files changed, 34 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index c00010c7dd1fb..26b483fe8791f 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -628,9 +628,11 @@ void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
if (auto Attr = VD->getAttr<HLSLVkExtBuiltinInputAttr>()) {
LLVMContext &Ctx = GV->getContext();
IRBuilder<> B(GV->getContext());
- MDNode *Val = MDNode::get(
- Ctx, {ConstantAsMetadata::get(B.getInt32(Attr->getBuiltIn()))});
- GV->addMetadata("spv.builtin", *Val);
+ MDNode *Operands = MDNode::get(
+ Ctx, {ConstantAsMetadata::get(B.getInt32(11)),
+ ConstantAsMetadata::get(B.getInt32(Attr->getBuiltIn()))});
+ MDNode *Decoration = MDNode::get(Ctx, {Operands});
+ GV->addMetadata("spirv.Decorations", *Decoration);
}
}
diff --git a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
index 2e8a854956100..ff31005bc1bf3 100644
--- a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
+++ b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
@@ -3,7 +3,7 @@
[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
static const uint3 groupid;
-// CHECK: @_ZL7groupid = local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32> undef, align 16, !spv.builtin [[META0:![0-9]+]]
+// CHECK: @_ZL7groupid = local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32> undef, align 16, !spirv.Decorations [[META0:![0-9]+]]
RWStructuredBuffer<int> output : register(u1, space0);
@@ -11,4 +11,5 @@ RWStructuredBuffer<int> output : register(u1, space0);
void main() {
output[0] = groupid;
}
-// CHECK: [[META0]] = !{i32 26}
+// CHECK: [[META0]] = !{[[META1:![0-9]+]]}
+// CHECK: [[META1]] = !{i32 11, i32 26}
diff --git a/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll b/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
new file mode 100644
index 0000000000000..4387dc191cd12
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
@@ -0,0 +1,26 @@
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-vulkan-unknown %s -o - | FileCheck %s
+
+; FIXME(138268): Alignment decoration is emitted.
+; NO-RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK-DAG: OpDecorate %[[#WorkgroupId:]] BuiltIn WorkgroupId
+
+; CHECK-DAG: %[[#uint:]] = OpTypeInt 32 0
+; CHECK-DAG: %[[#uint_0:]] = OpConstant %[[#uint]] 0
+; CHECK-DAG: %[[#v3uint:]] = OpTypeVector %[[#uint]] 3
+; CHECK-DAG: %[[#ptr_Input_uint:]] = OpTypePointer Input %[[#uint]]
+; CHECK-DAG: %[[#ptr_Input_v3uint:]] = OpTypePointer Input %[[#v3uint]]
+; CHECK-DAG: %[[#WorkgroupId:]] = OpVariable %[[#ptr_Input_v3uint]] Input
+ at var = local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32> undef, align 16, !spirv.Decorations !0
+
+define i32 @foo() {
+entry:
+; CHECK: %[[#ptr:]] = OpAccessChain %[[#ptr_Input_uint]] %[[#WorkgroupId]] %[[#uint_0]]
+; CHECK: %[[#res:]] = OpLoad %[[#uint]] %[[#ptr]] Aligned 16
+; CHECK: OpReturnValue %[[#res]]
+ %0 = load i32, ptr addrspace(7) @var, align 16
+ ret i32 %0
+}
+
+!0 = !{!1}
+!1 = !{i32 11, i32 26}
>From 67ca3d4368f8f520db7c38a8999f502cddea87a4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Mon, 5 May 2025 16:15:47 +0200
Subject: [PATCH 4/8] fix docs
---
clang/include/clang/Basic/AttrDocs.td | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 1f8d8755e245c..5b58bbb510c54 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8519,6 +8519,7 @@ Those builtins can be declared using the `[[vk::ext_builtin_input]]` attribute
like follows:
.. code-block:: c++
+
[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
static const uint3 groupid;
>From 84d15cc27cd21834a7f2bda444391b952ccf41c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Mon, 5 May 2025 17:16:39 +0200
Subject: [PATCH 5/8] change run prefix for failure
---
llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll b/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
index 4387dc191cd12..131c3aba182b5 100644
--- a/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
+++ b/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
@@ -1,7 +1,7 @@
; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-vulkan-unknown %s -o - | FileCheck %s
; FIXME(138268): Alignment decoration is emitted.
-; NO-RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-unknown %s -o - -filetype=obj | spirv-val %}
+; FIXME: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-unknown %s -o - -filetype=obj | spirv-val %}
; CHECK-DAG: OpDecorate %[[#WorkgroupId:]] BuiltIn WorkgroupId
>From 91e8c29701030dcb9745a95586074a92dad58f37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Tue, 6 May 2025 16:46:39 +0200
Subject: [PATCH 6/8] remove undef usage
---
clang/lib/CodeGen/CodeGenModule.cpp | 11 +++++------
clang/test/CodeGenHLSL/vk-input-builtin.hlsl | 2 +-
.../SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll | 2 +-
3 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 7dc6a1f46e473..468fc6e0e5c56 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5638,8 +5638,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
else if (D->hasAttr<LoaderUninitializedAttr>())
Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
- else if (GetGlobalVarAddressSpace(D) == LangAS::hlsl_input)
- Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
else if (!InitExpr) {
// This is a tentative definition; tentative definitions are
// implicitly initialized with { 0 }.
@@ -5763,15 +5761,16 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
getCUDARuntime().internalizeDeviceSideVar(D, Linkage);
}
getCUDARuntime().handleVarRegistration(D, *GV);
- } else if (LangOpts.HLSL &&
- GetGlobalVarAddressSpace(D) == LangAS::hlsl_input) {
+ }
+
+ if (LangOpts.HLSL && GetGlobalVarAddressSpace(D) == LangAS::hlsl_input) {
// HLSL Input variables are considered to be set by the driver/pipeline, but
// only visible to a single thread/wave.
GV->setExternallyInitialized(true);
+ } else {
+ GV->setInitializer(Init);
}
- GV->setInitializer(Init);
-
if (LangOpts.HLSL)
getHLSLRuntime().handleGlobalVarDefinition(D, GV);
diff --git a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
index ff31005bc1bf3..1cc7963c0e289 100644
--- a/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
+++ b/clang/test/CodeGenHLSL/vk-input-builtin.hlsl
@@ -3,7 +3,7 @@
[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
static const uint3 groupid;
-// CHECK: @_ZL7groupid = local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32> undef, align 16, !spirv.Decorations [[META0:![0-9]+]]
+// CHECK: @_ZL7groupid = external local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32>, align 16, !spirv.Decorations [[META0:![0-9]+]]
RWStructuredBuffer<int> output : register(u1, space0);
diff --git a/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll b/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
index 131c3aba182b5..2253b9761d5bd 100644
--- a/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
+++ b/llvm/test/CodeGen/SPIRV/hlsl-intrinsics/vk-ext-builtin-input.ll
@@ -11,7 +11,7 @@
; CHECK-DAG: %[[#ptr_Input_uint:]] = OpTypePointer Input %[[#uint]]
; CHECK-DAG: %[[#ptr_Input_v3uint:]] = OpTypePointer Input %[[#v3uint]]
; CHECK-DAG: %[[#WorkgroupId:]] = OpVariable %[[#ptr_Input_v3uint]] Input
- at var = local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32> undef, align 16, !spirv.Decorations !0
+ at var = external local_unnamed_addr addrspace(7) externally_initialized constant <3 x i32>, align 16, !spirv.Decorations !0
define i32 @foo() {
entry:
>From 41165221a7a244ff63b8842cbfd2534a43e07279 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Tue, 6 May 2025 17:22:40 +0200
Subject: [PATCH 7/8] add sema tests
---
clang/include/clang/Basic/Attr.td | 4 +--
clang/lib/Sema/SemaHLSL.cpp | 8 +++--
clang/test/SemaHLSL/vk-ext-input-builtin.hlsl | 29 +++++++++++++++++++
3 files changed, 36 insertions(+), 5 deletions(-)
create mode 100644 clang/test/SemaHLSL/vk-ext-input-builtin.hlsl
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 917e6eec08e0f..a652aa5143a7e 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -143,7 +143,7 @@ def SharedVar : SubsetSubject<Var,
def HLSLInputBuiltin : SubsetSubject<Var, [{S->hasGlobalStorage() &&
S->getStorageClass()==StorageClass::SC_Static &&
S->getType().isConstQualified()}],
- "input builtin">;
+ "static const globals">;
def GlobalVar : SubsetSubject<Var,
[{S->hasGlobalStorage()}], "global variables">;
@@ -4958,7 +4958,7 @@ def HLSLWaveSize: InheritableAttr {
def HLSLVkExtBuiltinInput : InheritableAttr {
let Spellings = [CXX11<"vk", "ext_builtin_input">];
- let Args = [IntArgument<"BuiltIn">];
+ let Args = [UnsignedArgument<"BuiltIn">];
let Subjects = SubjectList<[HLSLInputBuiltin], ErrorDiag>;
let LangOpts = [HLSL];
let Documentation = [HLSLVkExtBuiltinInputDocs];
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 6058d3f135b51..9065cc5a1d4a5 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1118,9 +1118,11 @@ void SemaHLSL::handleWaveSizeAttr(Decl *D, const ParsedAttr &AL) {
}
void SemaHLSL::handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL) {
- IntegerLiteral *IL = cast<IntegerLiteral>(AL.getArgAsExpr(0));
- D->addAttr(::new (getASTContext()) HLSLVkExtBuiltinInputAttr(
- getASTContext(), AL, IL->getValue().getZExtValue()));
+ uint32_t ID;
+ if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), ID))
+ return;
+ D->addAttr(::new (getASTContext())
+ HLSLVkExtBuiltinInputAttr(getASTContext(), AL, ID));
}
bool SemaHLSL::diagnoseInputIDType(QualType T, const ParsedAttr &AL) {
diff --git a/clang/test/SemaHLSL/vk-ext-input-builtin.hlsl b/clang/test/SemaHLSL/vk-ext-input-builtin.hlsl
new file mode 100644
index 0000000000000..e636e146cf68a
--- /dev/null
+++ b/clang/test/SemaHLSL/vk-ext-input-builtin.hlsl
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple spirv-unkown-vulkan1.3-compute -x hlsl -hlsl-entry foo -finclude-default-header -o - %s -verify
+
+// expected-error at +1 {{'ext_builtin_input' attribute only applies to static const globals}}
+[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
+const uint3 groupid1;
+
+// expected-error at +1 {{'ext_builtin_input' attribute only applies to static const globals}}
+[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
+static uint3 groupid2;
+
+// expected-error at +1 {{'ext_builtin_input' attribute takes one argument}}
+[[vk::ext_builtin_input()]]
+// expected-error at +1 {{default initialization of an object of const type 'const hlsl_private uint3' (aka 'const hlsl_private vector<uint, 3>')}}
+static const uint3 groupid3;
+
+// expected-error at +1 {{'ext_builtin_input' attribute requires an integer constant}}
+[[vk::ext_builtin_input(0.4f)]]
+// expected-error at +1 {{default initialization of an object of const type 'const hlsl_private uint3' (aka 'const hlsl_private vector<uint, 3>')}}
+static const uint3 groupid4;
+
+// expected-error at +1 {{'ext_builtin_input' attribute only applies to static const globals}}
+[[vk::ext_builtin_input(1)]]
+void some_function() {
+}
+
+[numthreads(1,1,1)]
+void foo() {
+}
+
>From a0486c1e1d9046e6707d7274f8c7779b37172857 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Wed, 4 Jun 2025 11:35:28 +0200
Subject: [PATCH 8/8] add comment
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 26b483fe8791f..526139b456ea6 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -629,7 +629,8 @@ void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
LLVMContext &Ctx = GV->getContext();
IRBuilder<> B(GV->getContext());
MDNode *Operands = MDNode::get(
- Ctx, {ConstantAsMetadata::get(B.getInt32(11)),
+ Ctx, {ConstantAsMetadata::get(
+ B.getInt32(/* Spirv::Decoration::BuiltIn */ 11)),
ConstantAsMetadata::get(B.getInt32(Attr->getBuiltIn()))});
MDNode *Decoration = MDNode::get(Ctx, {Operands});
GV->addMetadata("spirv.Decorations", *Decoration);
More information about the llvm-commits
mailing list