Skip to content

Commit d66d34a

Browse files
[SPIR-V] implement vk::spvexecutionmode attribute for inline SPIR-V (microsoft#5927)
Implements `vk::spvexecutionmode` from https://github.com/microsoft/hlsl-specs/blob/main/proposals/0011-inline-spirv.md#execution-modes.
1 parent c6cf5c9 commit d66d34a

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,14 @@ def VKExtensionExt : InheritableAttr {
11421142
let Documentation = [Undocumented];
11431143
}
11441144

1145+
def VKSpvExecutionMode : InheritableAttr {
1146+
let Spellings = [CXX11<"vk", "spvexecutionmode">];
1147+
let Subjects = SubjectList<[Function], ErrorDiag>;
1148+
let Args = [UnsignedArgument<"ExecutionMode">];
1149+
let LangOpts = [SPIRV];
1150+
let Documentation = [Undocumented];
1151+
}
1152+
11451153
def VKStorageClassExt : InheritableAttr {
11461154
let Spellings = [CXX11<"vk", "ext_storage_class">];
11471155
let Subjects = SubjectList<[Var, ParmVar], ErrorDiag>;

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12313,6 +12313,19 @@ SpirvEmitter::getSpirvShaderStage(hlsl::ShaderModel::Kind smk,
1231312313
}
1231412314
}
1231512315

12316+
void SpirvEmitter::processInlineSpirvAttributes(const FunctionDecl *decl) {
12317+
if (!decl->hasAttrs())
12318+
return;
12319+
12320+
for (auto &attr : decl->getAttrs()) {
12321+
if (auto *modeAttr = dyn_cast<VKSpvExecutionModeAttr>(attr)) {
12322+
spvBuilder.addExecutionMode(
12323+
entryFunction, spv::ExecutionMode(modeAttr->getExecutionMode()), {},
12324+
modeAttr->getLocation());
12325+
}
12326+
}
12327+
}
12328+
1231612329
bool SpirvEmitter::processGeometryShaderAttributes(const FunctionDecl *decl,
1231712330
uint32_t *arraySize) {
1231812331
bool success = true;
@@ -12907,6 +12920,8 @@ bool SpirvEmitter::emitEntryFunctionWrapper(const FunctionDecl *decl,
1290712920
assert(entryInfo->isEntryFunction);
1290812921
entryInfo->entryFunction = entryFunction;
1290912922

12923+
processInlineSpirvAttributes(decl);
12924+
1291012925
if (spvContext.isRay()) {
1291112926
return emitEntryFunctionWrapperForRayTracing(decl, entryFuncInstr,
1291212927
debugFunction);

tools/clang/lib/SPIRV/SpirvEmitter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,9 @@ class SpirvEmitter : public ASTConsumer {
808808
static spv::ExecutionModel getSpirvShaderStage(hlsl::ShaderModel::Kind smk,
809809
bool);
810810

811+
/// \brief Handle inline SPIR-V attributes for the entry function.
812+
void processInlineSpirvAttributes(const FunctionDecl *entryFunction);
813+
811814
/// \brief Adds necessary execution modes for the hull/domain shaders based on
812815
/// the HLSL attributes of the entry point function.
813816
/// In the case of hull shaders, also writes the number of output control

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12581,6 +12581,11 @@ void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A,
1258112581
A.getRange(), S.Context, ValidateAttributeStringArg(S, A, nullptr),
1258212582
A.getAttributeSpellingListIndex());
1258312583
break;
12584+
case AttributeList::AT_VKSpvExecutionMode:
12585+
declAttr = ::new (S.Context) VKSpvExecutionModeAttr(
12586+
A.getRange(), S.Context, ValidateAttributeIntArg(S, A),
12587+
A.getAttributeSpellingListIndex());
12588+
break;
1258412589
case AttributeList::AT_VKInstructionExt:
1258512590
declAttr = ::new (S.Context) VKInstructionExtAttr(
1258612591
A.getRange(), S.Context, ValidateAttributeIntArg(S, A),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK: OpExecutionMode %main DepthLess
4+
// CHECK-NEXT: OpExecutionMode %main PostDepthCoverage
5+
[[vk::spvexecutionmode(4446),vk::spvexecutionmode(15)]]
6+
void main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: not %dxc -T ps_6_0 -E main -fcgl %s -spirv 2>&1 | FileCheck %s
2+
3+
// CHECK: Invalid execution mode operand: 999999
4+
[[vk::spvexecutionmode(999999)]]
5+
void main() {}

0 commit comments

Comments
 (0)