Skip to content

Commit 71aec00

Browse files
authored
[SPIR-V] Best effort const eval of vk::ext_literal (#6604)
If a parameter annotated with `[[vk::ext_literal]]` is not lowered to a SpirvConstant, make a best-effort attempt to evaluate it to a constant literal value. If unsuccessful, fail gracefully rather than crash. Fixes #6586
1 parent ccdc4fd commit 71aec00

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14021,9 +14021,17 @@ SpirvEmitter::processSpvIntrinsicCallExpr(const CallExpr *expr) {
1402114021
spvArgs.push_back(argInst);
1402214022
} else if (param->hasAttr<VKLiteralExtAttr>()) {
1402314023
auto constArg = dyn_cast<SpirvConstant>(argInst);
14024-
assert(constArg != nullptr);
14024+
if (constArg == nullptr) {
14025+
constArg = constEvaluator.tryToEvaluateAsConst(arg, isSpecConstantMode);
14026+
}
14027+
if (constArg == nullptr) {
14028+
emitError("vk::ext_literal may only be applied to parameters that can "
14029+
"be evaluated to a literal value",
14030+
expr->getExprLoc());
14031+
return nullptr;
14032+
}
1402514033
constArg->setLiteral();
14026-
spvArgs.push_back(argInst);
14034+
spvArgs.push_back(constArg);
1402714035
} else {
1402814036
spvArgs.push_back(loadIfGLValue(arg, argInst));
1402914037
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: not %dxc -T cs_6_6 -spirv -fspv-target-env=vulkan1.3 -fcgl %s 2>&1 | FileCheck %s
2+
3+
static uint Reduce;
4+
5+
[[vk::ext_instruction(/* OpGroupNonUniformBallotBitCount */ 342)]]
6+
uint OpGroupNonUniformBallotBitCount(uint scope, [[vk::ext_literal]] uint groupOperation, uint4 ballot);
7+
8+
[numthreads(1, 1, 1)]
9+
void main()
10+
{
11+
// CHECK: error: vk::ext_literal may only be applied to parameters that can be evaluated to a literal value
12+
OpGroupNonUniformBallotBitCount(vk::SubgroupScope, Reduce, 0);
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %dxc -T cs_6_6 -spirv -fspv-target-env=vulkan1.3 -fcgl %s | FileCheck %s
2+
3+
static const uint Reduce = 0;
4+
5+
#define InclusiveScan 1
6+
7+
enum
8+
{
9+
ExclusiveScan = 2,
10+
};
11+
12+
[[vk::ext_instruction(/* OpGroupNonUniformBallotBitCount */ 342)]]
13+
uint OpGroupNonUniformBallotBitCount(uint scope, [[vk::ext_literal]] uint groupOperation, uint4 ballot);
14+
15+
[numthreads(1, 1, 1)]
16+
void main()
17+
{
18+
// CHECK: [[ballot:%[0-9]+]] = OpConstantComposite %v4uint %uint_0 %uint_0 %uint_0 %uint_0
19+
20+
// CHECK: {{%[0-9]+}} = OpGroupNonUniformBallotBitCount %uint %uint_3 Reduce [[ballot]]
21+
OpGroupNonUniformBallotBitCount(vk::SubgroupScope, Reduce, 0);
22+
// CHECK: {{%[0-9]+}} = OpGroupNonUniformBallotBitCount %uint %uint_3 InclusiveScan [[ballot]]
23+
OpGroupNonUniformBallotBitCount(vk::SubgroupScope, InclusiveScan, 0);
24+
// CHECK: {{%[0-9]+}} = OpGroupNonUniformBallotBitCount %uint %uint_3 ExclusiveScan [[ballot]]
25+
OpGroupNonUniformBallotBitCount(vk::SubgroupScope, ExclusiveScan, 0);
26+
}

0 commit comments

Comments
 (0)