[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
s-perron wrote: https://lab.llvm.org/buildbot/#/builders/24/builds/9606 https://github.com/llvm/llvm-project/pull/143544 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
https://github.com/Keenuts approved this pull request. https://github.com/llvm/llvm-project/pull/143544 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
https://github.com/s-perron updated
https://github.com/llvm/llvm-project/pull/143544
>From 954a86f3c8da88c4ac276231a40cd88e6f628253 Mon Sep 17 00:00:00 2001
From: Steven Perron
Date: Fri, 30 May 2025 12:32:21 -0400
Subject: [PATCH 1/7] [HLSL][SPIRV] Add vk::constant_id attribute.
The vk::constant_id attribute is used to indicate that a global const variable
represents a specialization constant in SPIR-V. This PR adds this
attribute to clang.
The documetation for the attribute is
[here](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#specialization-constants).
The strategy is to to modify the initializer to get the value of a
specialize constant for a builtin defined in the SPIR-V backend.
---
clang/include/clang/Basic/Attr.td | 8 +
clang/include/clang/Basic/AttrDocs.td | 15 ++
clang/include/clang/Basic/Builtins.td | 13 ++
.../clang/Basic/DiagnosticSemaKinds.td| 12 +
clang/include/clang/Sema/SemaHLSL.h | 5 +-
clang/lib/Basic/Attributes.cpp| 3 +-
clang/lib/CodeGen/CGHLSLBuiltins.cpp | 72 ++
clang/lib/CodeGen/CodeGenFunction.h | 11 +
clang/lib/Sema/SemaDecl.cpp | 14 ++
clang/lib/Sema/SemaDeclAttr.cpp | 3 +
clang/lib/Sema/SemaHLSL.cpp | 120 +-
.../test/AST/HLSL/vk.spec-constant.usage.hlsl | 130 +++
.../SpirvType.alignment.hlsl | 0
.../SpirvType.hlsl| 0
.../vk-features/vk.spec-constant.hlsl | 210 ++
.../test/SemaHLSL/vk.spec-constant.error.hlsl | 37 +++
16 files changed, 650 insertions(+), 3 deletions(-)
create mode 100644 clang/test/AST/HLSL/vk.spec-constant.usage.hlsl
rename clang/test/CodeGenHLSL/{inline-spirv =>
vk-features}/SpirvType.alignment.hlsl (100%)
rename clang/test/CodeGenHLSL/{inline-spirv => vk-features}/SpirvType.hlsl
(100%)
create mode 100644 clang/test/CodeGenHLSL/vk-features/vk.spec-constant.hlsl
create mode 100644 clang/test/SemaHLSL/vk.spec-constant.error.hlsl
diff --git a/clang/include/clang/Basic/Attr.td
b/clang/include/clang/Basic/Attr.td
index f889e41c8699f..d3f39de6a3e85 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4993,6 +4993,14 @@ def HLSLVkExtBuiltinInput : InheritableAttr {
let Documentation = [HLSLVkExtBuiltinInputDocs];
}
+def HLSLVkConstantId : InheritableAttr {
+ let Spellings = [CXX11<"vk", "constant_id">];
+ let Args = [IntArgument<"Id">];
+ let Subjects = SubjectList<[ExternalGlobalVar]>;
+ let LangOpts = [HLSL];
+ let Documentation = [VkConstantIdDocs];
+}
+
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 ea3c43f38d9fe..b3eafb79c5d4a 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8252,6 +8252,21 @@ and
https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def VkConstantIdDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``vk::constant_id`` attribute specify the id for a SPIR-V specialization
+constant. The attribute applies to const global scalar variables. The variable
must be initialized with a C++11 constexpr.
+In SPIR-V, the
+variable will be replaced with an `OpSpecConstant` with the given id.
+The syntax is:
+
+.. code-block:: text
+
+ ``[[vk::constant_id()]] const T Name = ``
+}];
+}
+
def RootSignatureDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Basic/Builtins.td
b/clang/include/clang/Basic/Builtins.td
index 68cd3d790e78a..d65b3a5d2f447 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5065,6 +5065,19 @@ def HLSLGroupMemoryBarrierWithGroupSync:
LangBuiltin<"HLSL_LANG"> {
let Prototype = "void()";
}
+class HLSLScalarTemplate
+: Template<["bool", "char", "short", "int", "long long int",
+"unsigned short", "unsigned int", "unsigned long long int",
+"__fp16", "float", "double"],
+ ["_bool", "_char", "_short", "_int", "_longlong", "_ushort",
+"_uint", "_ulonglong", "_half", "_float", "_double"]>;
+
+def HLSLGetSpirvSpecConstant : LangBuiltin<"HLSL_LANG">, HLSLScalarTemplate {
+ let Spellings = ["__builtin_get_spirv_spec_constant"];
+ let Attributes = [NoThrow, Const, Pure];
+ let Prototype = "T(unsigned int, T)";
+}
+
// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1f283b776a02c..23a490225dd19 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
@@ -12919,6 +12919,18 @@ def err_spirv_enum_not_int : Error<
def err_spirv_enum_not_valid : Error<
"invalid value for %select{storage class}0 argument">;
+def err_specialization_const_lit_init
+: Error<"variable with 'vk::constant_id' attribute cannot have an "
+"initializer that is not a literal">;
Keenuts wrote:
nit, more aligned with C++ errors about const init.
```suggestion
def err_specialization_const_lit_init
: Error<"variable with 'vk::constant_id' attribute must be initialized by a
int/float/enum/bool literal.">;
```
nit nit: maybe this message could also be used to replace the 2 other messages:
this way you have a full fix suggested instead of first "this is not
initialized", then "now it's initialized but not a literal", and finally "this
is a literal, but not a float/int/enum/bool".
https://github.com/llvm/llvm-project/pull/143544
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
@@ -213,7 +213,8 @@ getScopeFromNormalizedScopeName(StringRef ScopeName) {
.Case("vk", AttributeCommonInfo::Scope::VK)
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
.Case("omp", AttributeCommonInfo::Scope::OMP)
- .Case("riscv", AttributeCommonInfo::Scope::RISCV);
+ .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+ .Case("vk", AttributeCommonInfo::Scope::HLSL);
s-perron wrote:
I see you added the on above with a PR you merged last week. I was probably
working on a branch that did not have your change, so I had to add my own. When
I merged with the latest main, I did not get a merge conflict. I'll remove this
change.
https://github.com/llvm/llvm-project/pull/143544
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
@@ -774,6 +775,77 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned
BuiltinID,
return EmitRuntimeCall(
Intrinsic::getOrInsertDeclaration(&CGM.getModule(), ID));
}
+ case Builtin::BI__builtin_get_spirv_spec_constant_bool:
+ case Builtin::BI__builtin_get_spirv_spec_constant_short:
+ case Builtin::BI__builtin_get_spirv_spec_constant_ushort:
+ case Builtin::BI__builtin_get_spirv_spec_constant_int:
+ case Builtin::BI__builtin_get_spirv_spec_constant_uint:
+ case Builtin::BI__builtin_get_spirv_spec_constant_longlong:
+ case Builtin::BI__builtin_get_spirv_spec_constant_ulonglong:
+ case Builtin::BI__builtin_get_spirv_spec_constant_half:
+ case Builtin::BI__builtin_get_spirv_spec_constant_float:
+ case Builtin::BI__builtin_get_spirv_spec_constant_double: {
s-perron wrote:
Maybe, but I like this method because it becomes impossible to create calls to
the builtin with invalid types. wave_read_lane_at is defined with a prototype:
`void(...)`. I believe we can add type checking in other places, but I feel
this is more robust.
https://github.com/llvm/llvm-project/pull/143544
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
https://github.com/s-perron updated
https://github.com/llvm/llvm-project/pull/143544
>From 954a86f3c8da88c4ac276231a40cd88e6f628253 Mon Sep 17 00:00:00 2001
From: Steven Perron
Date: Fri, 30 May 2025 12:32:21 -0400
Subject: [PATCH 1/3] [HLSL][SPIRV] Add vk::constant_id attribute.
The vk::constant_id attribute is used to indicate that a global const variable
represents a specialization constant in SPIR-V. This PR adds this
attribute to clang.
The documetation for the attribute is
[here](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#specialization-constants).
The strategy is to to modify the initializer to get the value of a
specialize constant for a builtin defined in the SPIR-V backend.
---
clang/include/clang/Basic/Attr.td | 8 +
clang/include/clang/Basic/AttrDocs.td | 15 ++
clang/include/clang/Basic/Builtins.td | 13 ++
.../clang/Basic/DiagnosticSemaKinds.td| 12 +
clang/include/clang/Sema/SemaHLSL.h | 5 +-
clang/lib/Basic/Attributes.cpp| 3 +-
clang/lib/CodeGen/CGHLSLBuiltins.cpp | 72 ++
clang/lib/CodeGen/CodeGenFunction.h | 11 +
clang/lib/Sema/SemaDecl.cpp | 14 ++
clang/lib/Sema/SemaDeclAttr.cpp | 3 +
clang/lib/Sema/SemaHLSL.cpp | 120 +-
.../test/AST/HLSL/vk.spec-constant.usage.hlsl | 130 +++
.../SpirvType.alignment.hlsl | 0
.../SpirvType.hlsl| 0
.../vk-features/vk.spec-constant.hlsl | 210 ++
.../test/SemaHLSL/vk.spec-constant.error.hlsl | 37 +++
16 files changed, 650 insertions(+), 3 deletions(-)
create mode 100644 clang/test/AST/HLSL/vk.spec-constant.usage.hlsl
rename clang/test/CodeGenHLSL/{inline-spirv =>
vk-features}/SpirvType.alignment.hlsl (100%)
rename clang/test/CodeGenHLSL/{inline-spirv => vk-features}/SpirvType.hlsl
(100%)
create mode 100644 clang/test/CodeGenHLSL/vk-features/vk.spec-constant.hlsl
create mode 100644 clang/test/SemaHLSL/vk.spec-constant.error.hlsl
diff --git a/clang/include/clang/Basic/Attr.td
b/clang/include/clang/Basic/Attr.td
index f889e41c8699f..d3f39de6a3e85 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4993,6 +4993,14 @@ def HLSLVkExtBuiltinInput : InheritableAttr {
let Documentation = [HLSLVkExtBuiltinInputDocs];
}
+def HLSLVkConstantId : InheritableAttr {
+ let Spellings = [CXX11<"vk", "constant_id">];
+ let Args = [IntArgument<"Id">];
+ let Subjects = SubjectList<[ExternalGlobalVar]>;
+ let LangOpts = [HLSL];
+ let Documentation = [VkConstantIdDocs];
+}
+
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 ea3c43f38d9fe..b3eafb79c5d4a 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8252,6 +8252,21 @@ and
https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def VkConstantIdDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``vk::constant_id`` attribute specify the id for a SPIR-V specialization
+constant. The attribute applies to const global scalar variables. The variable
must be initialized with a C++11 constexpr.
+In SPIR-V, the
+variable will be replaced with an `OpSpecConstant` with the given id.
+The syntax is:
+
+.. code-block:: text
+
+ ``[[vk::constant_id()]] const T Name = ``
+}];
+}
+
def RootSignatureDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Basic/Builtins.td
b/clang/include/clang/Basic/Builtins.td
index 68cd3d790e78a..d65b3a5d2f447 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5065,6 +5065,19 @@ def HLSLGroupMemoryBarrierWithGroupSync:
LangBuiltin<"HLSL_LANG"> {
let Prototype = "void()";
}
+class HLSLScalarTemplate
+: Template<["bool", "char", "short", "int", "long long int",
+"unsigned short", "unsigned int", "unsigned long long int",
+"__fp16", "float", "double"],
+ ["_bool", "_char", "_short", "_int", "_longlong", "_ushort",
+"_uint", "_ulonglong", "_half", "_float", "_double"]>;
+
+def HLSLGetSpirvSpecConstant : LangBuiltin<"HLSL_LANG">, HLSLScalarTemplate {
+ let Spellings = ["__builtin_get_spirv_spec_constant"];
+ let Attributes = [NoThrow, Const, Pure];
+ let Prototype = "T(unsigned int, T)";
+}
+
// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1f283b776a02c..23a490225dd19 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
@@ -213,7 +213,8 @@ getScopeFromNormalizedScopeName(StringRef ScopeName) {
.Case("vk", AttributeCommonInfo::Scope::VK)
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
.Case("omp", AttributeCommonInfo::Scope::OMP)
- .Case("riscv", AttributeCommonInfo::Scope::RISCV);
+ .Case("riscv", AttributeCommonInfo::Scope::RISCV)
+ .Case("vk", AttributeCommonInfo::Scope::HLSL);
s-perron wrote:
I'll have to look into that. I added it because I was getting an error. I may
have something else wrong.
https://github.com/llvm/llvm-project/pull/143544
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
llvmbot wrote:
@llvm/pr-subscribers-hlsl
Author: Steven Perron (s-perron)
Changes
The vk::constant_id attribute is used to indicate that a global const variable
represents a specialization constant in SPIR-V. This PR adds this
attribute to clang.
The documetation for the attribute is
[here](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#specialization-constants).
The strategy is to to modify the initializer to get the value of a
specialize constant for a builtin defined in the SPIR-V backend.
---
Patch is 39.25 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/143544.diff
16 Files Affected:
- (modified) clang/include/clang/Basic/Attr.td (+8)
- (modified) clang/include/clang/Basic/AttrDocs.td (+15)
- (modified) clang/include/clang/Basic/Builtins.td (+13)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+12)
- (modified) clang/include/clang/Sema/SemaHLSL.h (+4-1)
- (modified) clang/lib/Basic/Attributes.cpp (+2-1)
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (+72)
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+11)
- (modified) clang/lib/Sema/SemaDecl.cpp (+14)
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+3)
- (modified) clang/lib/Sema/SemaHLSL.cpp (+119-1)
- (added) clang/test/AST/HLSL/vk.spec-constant.usage.hlsl (+130)
- (renamed) clang/test/CodeGenHLSL/vk-features/SpirvType.alignment.hlsl ()
- (renamed) clang/test/CodeGenHLSL/vk-features/SpirvType.hlsl ()
- (added) clang/test/CodeGenHLSL/vk-features/vk.spec-constant.hlsl (+210)
- (added) clang/test/SemaHLSL/vk.spec-constant.error.hlsl (+37)
``diff
diff --git a/clang/include/clang/Basic/Attr.td
b/clang/include/clang/Basic/Attr.td
index f889e41c8699f..d3f39de6a3e85 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4993,6 +4993,14 @@ def HLSLVkExtBuiltinInput : InheritableAttr {
let Documentation = [HLSLVkExtBuiltinInputDocs];
}
+def HLSLVkConstantId : InheritableAttr {
+ let Spellings = [CXX11<"vk", "constant_id">];
+ let Args = [IntArgument<"Id">];
+ let Subjects = SubjectList<[ExternalGlobalVar]>;
+ let LangOpts = [HLSL];
+ let Documentation = [VkConstantIdDocs];
+}
+
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 ea3c43f38d9fe..b3eafb79c5d4a 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8252,6 +8252,21 @@ and
https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def VkConstantIdDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``vk::constant_id`` attribute specify the id for a SPIR-V specialization
+constant. The attribute applies to const global scalar variables. The variable
must be initialized with a C++11 constexpr.
+In SPIR-V, the
+variable will be replaced with an `OpSpecConstant` with the given id.
+The syntax is:
+
+.. code-block:: text
+
+ ``[[vk::constant_id()]] const T Name = ``
+}];
+}
+
def RootSignatureDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Basic/Builtins.td
b/clang/include/clang/Basic/Builtins.td
index 68cd3d790e78a..d65b3a5d2f447 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5065,6 +5065,19 @@ def HLSLGroupMemoryBarrierWithGroupSync:
LangBuiltin<"HLSL_LANG"> {
let Prototype = "void()";
}
+class HLSLScalarTemplate
+: Template<["bool", "char", "short", "int", "long long int",
+"unsigned short", "unsigned int", "unsigned long long int",
+"__fp16", "float", "double"],
+ ["_bool", "_char", "_short", "_int", "_longlong", "_ushort",
+"_uint", "_ulonglong", "_half", "_float", "_double"]>;
+
+def HLSLGetSpirvSpecConstant : LangBuiltin<"HLSL_LANG">, HLSLScalarTemplate {
+ let Spellings = ["__builtin_get_spirv_spec_constant"];
+ let Attributes = [NoThrow, Const, Pure];
+ let Prototype = "T(unsigned int, T)";
+}
+
// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1f283b776a02c..23a490225dd19 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -12919,6 +12919,18 @@ def err_spirv_enum_not_int : Error<
def err_spirv_enum_not_valid : Error<
"invalid value for %select{storage class}0 argument">;
+def err_specialization_const_lit_init
+: Error<"variable with 'vk::constant_id' attribute cannot have an "
+"initializer that is not a constexpr">;
+def err_specialization_const_missing_initializer
+: Error<
+
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
https://github.com/s-perron edited https://github.com/llvm/llvm-project/pull/143544 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
https://github.com/s-perron edited https://github.com/llvm/llvm-project/pull/143544 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
llvmbot wrote:
@llvm/pr-subscribers-clang-codegen
Author: Steven Perron (s-perron)
Changes
The vk::constant_id attribute is used to indicate that a global const variable
represents a specialization constant in SPIR-V. This PR adds this
attribute to clang.
The documetation for the attribute is
[here](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#specialization-constants).
The strategy is to to modify the initializer to get the value of a
specialize constant for a builtin defined in the SPIR-V backend.
---
Patch is 39.25 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/143544.diff
16 Files Affected:
- (modified) clang/include/clang/Basic/Attr.td (+8)
- (modified) clang/include/clang/Basic/AttrDocs.td (+15)
- (modified) clang/include/clang/Basic/Builtins.td (+13)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+12)
- (modified) clang/include/clang/Sema/SemaHLSL.h (+4-1)
- (modified) clang/lib/Basic/Attributes.cpp (+2-1)
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (+72)
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+11)
- (modified) clang/lib/Sema/SemaDecl.cpp (+14)
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+3)
- (modified) clang/lib/Sema/SemaHLSL.cpp (+119-1)
- (added) clang/test/AST/HLSL/vk.spec-constant.usage.hlsl (+130)
- (renamed) clang/test/CodeGenHLSL/vk-features/SpirvType.alignment.hlsl ()
- (renamed) clang/test/CodeGenHLSL/vk-features/SpirvType.hlsl ()
- (added) clang/test/CodeGenHLSL/vk-features/vk.spec-constant.hlsl (+210)
- (added) clang/test/SemaHLSL/vk.spec-constant.error.hlsl (+37)
``diff
diff --git a/clang/include/clang/Basic/Attr.td
b/clang/include/clang/Basic/Attr.td
index f889e41c8699f..d3f39de6a3e85 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4993,6 +4993,14 @@ def HLSLVkExtBuiltinInput : InheritableAttr {
let Documentation = [HLSLVkExtBuiltinInputDocs];
}
+def HLSLVkConstantId : InheritableAttr {
+ let Spellings = [CXX11<"vk", "constant_id">];
+ let Args = [IntArgument<"Id">];
+ let Subjects = SubjectList<[ExternalGlobalVar]>;
+ let LangOpts = [HLSL];
+ let Documentation = [VkConstantIdDocs];
+}
+
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 ea3c43f38d9fe..b3eafb79c5d4a 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8252,6 +8252,21 @@ and
https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def VkConstantIdDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``vk::constant_id`` attribute specify the id for a SPIR-V specialization
+constant. The attribute applies to const global scalar variables. The variable
must be initialized with a C++11 constexpr.
+In SPIR-V, the
+variable will be replaced with an `OpSpecConstant` with the given id.
+The syntax is:
+
+.. code-block:: text
+
+ ``[[vk::constant_id()]] const T Name = ``
+}];
+}
+
def RootSignatureDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Basic/Builtins.td
b/clang/include/clang/Basic/Builtins.td
index 68cd3d790e78a..d65b3a5d2f447 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5065,6 +5065,19 @@ def HLSLGroupMemoryBarrierWithGroupSync:
LangBuiltin<"HLSL_LANG"> {
let Prototype = "void()";
}
+class HLSLScalarTemplate
+: Template<["bool", "char", "short", "int", "long long int",
+"unsigned short", "unsigned int", "unsigned long long int",
+"__fp16", "float", "double"],
+ ["_bool", "_char", "_short", "_int", "_longlong", "_ushort",
+"_uint", "_ulonglong", "_half", "_float", "_double"]>;
+
+def HLSLGetSpirvSpecConstant : LangBuiltin<"HLSL_LANG">, HLSLScalarTemplate {
+ let Spellings = ["__builtin_get_spirv_spec_constant"];
+ let Attributes = [NoThrow, Const, Pure];
+ let Prototype = "T(unsigned int, T)";
+}
+
// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1f283b776a02c..23a490225dd19 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -12919,6 +12919,18 @@ def err_spirv_enum_not_int : Error<
def err_spirv_enum_not_valid : Error<
"invalid value for %select{storage class}0 argument">;
+def err_specialization_const_lit_init
+: Error<"variable with 'vk::constant_id' attribute cannot have an "
+"initializer that is not a constexpr">;
+def err_specialization_const_missing_initializer
+: Error<
+
[clang] [HLSL][SPIRV] Add vk::constant_id attribute. (PR #143544)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: Steven Perron (s-perron)
Changes
The vk::constant_id attribute is used to indicate that a global const variable
represents a specialization constant in SPIR-V. This PR adds this
attribute to clang.
The documetation for the attribute is
[here](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#specialization-constants).
The strategy is to to modify the initializer to get the value of a
specialize constant for a builtin defined in the SPIR-V backend.
---
Patch is 39.25 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/143544.diff
16 Files Affected:
- (modified) clang/include/clang/Basic/Attr.td (+8)
- (modified) clang/include/clang/Basic/AttrDocs.td (+15)
- (modified) clang/include/clang/Basic/Builtins.td (+13)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+12)
- (modified) clang/include/clang/Sema/SemaHLSL.h (+4-1)
- (modified) clang/lib/Basic/Attributes.cpp (+2-1)
- (modified) clang/lib/CodeGen/CGHLSLBuiltins.cpp (+72)
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+11)
- (modified) clang/lib/Sema/SemaDecl.cpp (+14)
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+3)
- (modified) clang/lib/Sema/SemaHLSL.cpp (+119-1)
- (added) clang/test/AST/HLSL/vk.spec-constant.usage.hlsl (+130)
- (renamed) clang/test/CodeGenHLSL/vk-features/SpirvType.alignment.hlsl ()
- (renamed) clang/test/CodeGenHLSL/vk-features/SpirvType.hlsl ()
- (added) clang/test/CodeGenHLSL/vk-features/vk.spec-constant.hlsl (+210)
- (added) clang/test/SemaHLSL/vk.spec-constant.error.hlsl (+37)
``diff
diff --git a/clang/include/clang/Basic/Attr.td
b/clang/include/clang/Basic/Attr.td
index f889e41c8699f..d3f39de6a3e85 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4993,6 +4993,14 @@ def HLSLVkExtBuiltinInput : InheritableAttr {
let Documentation = [HLSLVkExtBuiltinInputDocs];
}
+def HLSLVkConstantId : InheritableAttr {
+ let Spellings = [CXX11<"vk", "constant_id">];
+ let Args = [IntArgument<"Id">];
+ let Subjects = SubjectList<[ExternalGlobalVar]>;
+ let LangOpts = [HLSL];
+ let Documentation = [VkConstantIdDocs];
+}
+
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 ea3c43f38d9fe..b3eafb79c5d4a 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8252,6 +8252,21 @@ and
https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
}];
}
+def VkConstantIdDocs : Documentation {
+ let Category = DocCatFunction;
+ let Content = [{
+The ``vk::constant_id`` attribute specify the id for a SPIR-V specialization
+constant. The attribute applies to const global scalar variables. The variable
must be initialized with a C++11 constexpr.
+In SPIR-V, the
+variable will be replaced with an `OpSpecConstant` with the given id.
+The syntax is:
+
+.. code-block:: text
+
+ ``[[vk::constant_id()]] const T Name = ``
+}];
+}
+
def RootSignatureDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Basic/Builtins.td
b/clang/include/clang/Basic/Builtins.td
index 68cd3d790e78a..d65b3a5d2f447 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -5065,6 +5065,19 @@ def HLSLGroupMemoryBarrierWithGroupSync:
LangBuiltin<"HLSL_LANG"> {
let Prototype = "void()";
}
+class HLSLScalarTemplate
+: Template<["bool", "char", "short", "int", "long long int",
+"unsigned short", "unsigned int", "unsigned long long int",
+"__fp16", "float", "double"],
+ ["_bool", "_char", "_short", "_int", "_longlong", "_ushort",
+"_uint", "_ulonglong", "_half", "_float", "_double"]>;
+
+def HLSLGetSpirvSpecConstant : LangBuiltin<"HLSL_LANG">, HLSLScalarTemplate {
+ let Spellings = ["__builtin_get_spirv_spec_constant"];
+ let Attributes = [NoThrow, Const, Pure];
+ let Prototype = "T(unsigned int, T)";
+}
+
// Builtins for XRay.
def XRayCustomEvent : Builtin {
let Spellings = ["__xray_customevent"];
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1f283b776a02c..23a490225dd19 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -12919,6 +12919,18 @@ def err_spirv_enum_not_int : Error<
def err_spirv_enum_not_valid : Error<
"invalid value for %select{storage class}0 argument">;
+def err_specialization_const_lit_init
+: Error<"variable with 'vk::constant_id' attribute cannot have an "
+"initializer that is not a constexpr">;
+def err_specialization_const_missing_initializer
+: Error<
+
