Skip to content

Commit b32b169

Browse files
[SPIR-V] Enable vk::ext_extension and vk::ext_capability for variables (microsoft#6052)
`ext_extension` and `ext_capability` can only be added to functions at the moment. This PR enables adding them to variable declarations, so that using the variable results in the extensions and capabilities being added to the module. This is useful for the builtin variable syntax proposed in microsoft/hlsl-specs#129. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 37a0826 commit b32b169

File tree

7 files changed

+55
-16
lines changed

7 files changed

+55
-16
lines changed

tools/clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ def VKBinding : InheritableAttr {
12941294

12951295
def VKCapabilityExt : InheritableAttr {
12961296
let Spellings = [CXX11<"vk", "ext_capability">];
1297-
let Subjects = SubjectList<[Function], ErrorDiag>;
1297+
let Subjects = SubjectList<[Function, Var], ErrorDiag>;
12981298
let Args = [IntArgument<"capability">];
12991299
let LangOpts = [SPIRV];
13001300
let Documentation = [Undocumented];
@@ -1334,7 +1334,7 @@ def VKDecorateStringExt : InheritableAttr {
13341334

13351335
def VKExtensionExt : InheritableAttr {
13361336
let Spellings = [CXX11<"vk", "ext_extension">];
1337-
let Subjects = SubjectList<[Function], ErrorDiag>;
1337+
let Subjects = SubjectList<[Function, Var], ErrorDiag>;
13381338
let Args = [StringArgument<"name">];
13391339
let LangOpts = [SPIRV];
13401340
let Documentation = [Undocumented];

tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ DeclResultIdMapper::getDeclSpirvInfo(const ValueDecl *decl) const {
956956
SpirvInstruction *DeclResultIdMapper::getDeclEvalInfo(const ValueDecl *decl,
957957
SourceLocation loc,
958958
SourceRange range) {
959+
if (decl->hasAttr<VKExtensionExtAttr>() ||
960+
decl->hasAttr<VKCapabilityExtAttr>()) {
961+
theEmitter.createSpirvIntrInstExt(decl->getAttrs(), QualType(),
962+
/* spvArgs */ {}, /* isInst */ false,
963+
loc);
964+
}
959965
if (hlsl::IsHLSLDynamicResourceType(decl->getType()) ||
960966
hlsl::IsHLSLDynamicSamplerType(decl->getType())) {
961967
emitError("HLSL object %0 not yet supported with -spirv",

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14011,19 +14011,20 @@ SpirvEmitter::processRayQueryIntrinsics(const CXXMemberCallExpr *expr,
1401114011
return retVal;
1401214012
}
1401314013

14014-
SpirvInstruction *SpirvEmitter::createSpirvIntrInstExt(
14015-
llvm::ArrayRef<const Attr *> attrs, QualType retType,
14016-
const llvm::SmallVectorImpl<SpirvInstruction *> &spvArgs, bool isInstr,
14017-
SourceLocation loc) {
14018-
llvm::SmallVector<uint32_t, 2> capbilities;
14014+
SpirvInstruction *
14015+
SpirvEmitter::createSpirvIntrInstExt(llvm::ArrayRef<const Attr *> attrs,
14016+
QualType retType,
14017+
llvm::ArrayRef<SpirvInstruction *> spvArgs,
14018+
bool isInstr, SourceLocation loc) {
14019+
llvm::SmallVector<uint32_t, 2> capabilities;
1401914020
llvm::SmallVector<llvm::StringRef, 2> extensions;
1402014021
llvm::StringRef instSet = "";
1402114022
// For [[vk::ext_type_def]], we use dummy OpNop with no semantic meaning,
1402214023
// with possible extension and capabilities.
1402314024
uint32_t op = static_cast<unsigned>(spv::Op::OpNop);
1402414025
for (auto &attr : attrs) {
1402514026
if (auto capAttr = dyn_cast<VKCapabilityExtAttr>(attr)) {
14026-
capbilities.push_back(capAttr->getCapability());
14027+
capabilities.push_back(capAttr->getCapability());
1402714028
} else if (auto extAttr = dyn_cast<VKExtensionExtAttr>(attr)) {
1402814029
extensions.push_back(extAttr->getName());
1402914030
}
@@ -14036,7 +14037,7 @@ SpirvInstruction *SpirvEmitter::createSpirvIntrInstExt(
1403614037
}
1403714038

1403814039
SpirvInstruction *retVal = spvBuilder.createSpirvIntrInstExt(
14039-
op, retType, spvArgs, extensions, instSet, capbilities, loc);
14040+
op, retType, spvArgs, extensions, instSet, capabilities, loc);
1404014041
if (!retVal)
1404114042
return nullptr;
1404214043

tools/clang/lib/SPIRV/SpirvEmitter.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ class SpirvEmitter : public ASTConsumer {
104104
: (var->getAttr<HLSLGroupSharedAttr>() != nullptr);
105105
}
106106

107+
/// Create SpirvIntrinsicInstruction for arbitrary SPIR-V instructions
108+
/// specified by [[vk::ext_instruction(..)]] or [[vk::ext_type_def(..)]]
109+
SpirvInstruction *
110+
createSpirvIntrInstExt(llvm::ArrayRef<const Attr *> attrs, QualType retType,
111+
llvm::ArrayRef<SpirvInstruction *> spvArgs,
112+
bool isInstr, SourceLocation loc);
113+
107114
private:
108115
void doFunctionDecl(const FunctionDecl *decl);
109116
void doVarDecl(const VarDecl *decl);
@@ -685,13 +692,6 @@ class SpirvEmitter : public ASTConsumer {
685692
/// Process ray query intrinsics
686693
SpirvInstruction *processRayQueryIntrinsics(const CXXMemberCallExpr *expr,
687694
hlsl::IntrinsicOp opcode);
688-
689-
/// Create SpirvIntrinsicInstruction for arbitrary SPIR-V instructions
690-
/// specified by [[vk::ext_instruction(..)]] or [[vk::ext_type_def(..)]]
691-
SpirvInstruction *createSpirvIntrInstExt(
692-
llvm::ArrayRef<const Attr *> attrs, QualType retType,
693-
const llvm::SmallVectorImpl<SpirvInstruction *> &spvArgs, bool isInstr,
694-
SourceLocation loc);
695695
/// Process spirv intrinsic instruction
696696
SpirvInstruction *processSpvIntrinsicCallExpr(const CallExpr *expr);
697697

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fcgl -Vd %s -spirv | FileCheck %s
2+
3+
// CHECK: OpCapability SampleMaskPostDepthCoverage
4+
5+
[[vk::ext_capability(/* SampleMaskPostDepthCoverageCapability */ 4447)]]
6+
int val;
7+
8+
void main() {
9+
int local = val;
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK: OpExtension "another_extension"
4+
// CHECK: OpExtension "some_extension"
5+
6+
[[vk::ext_extension("some_extension"), vk::ext_extension("another_extension")]]
7+
int val;
8+
9+
void main() {
10+
int local = val;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK-NOT: OpExtension "some_extension"
4+
// CHECK-NOT: OpCapability SampleMaskPostDepthCoverage
5+
6+
[[vk::ext_extension("some_extension")]]
7+
[[vk::ext_capability(/* SampleMaskPostDepthCoverageCapability */ 4447)]]
8+
int val;
9+
10+
void main() {
11+
}

0 commit comments

Comments
 (0)