Skip to content

Commit 1198c30

Browse files
authored
[SPIRV] Fix constant value in function (#7415)
We will get a crash when use spirv intrinsic to create a constant value. ```fundamental fatal error: generated SPIR-V is invalid: Constant cannot appear in a function declaration %spirvIntrinsicType_42 = OpConstant %spirvIntrinsicType 42 ```
1 parent 9536291 commit 1198c30

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tools/clang/lib/SPIRV/EmitVisitor.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,13 @@ bool EmitVisitor::visit(SpirvIntrinsicInstruction *inst) {
20002000
}
20012001
}
20022002

2003-
finalizeInstruction(&mainBinary);
2003+
auto opcode = static_cast<spv::Op>(inst->getInstruction());
2004+
if ((opcode == spv::Op::OpSpecConstant || opcode == spv::Op::OpConstant) &&
2005+
!inst->getInstructionSet()) {
2006+
finalizeInstruction(&typeConstantBinary);
2007+
} else {
2008+
finalizeInstruction(&mainBinary);
2009+
}
20042010
return true;
20052011
}
20062012

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %dxc -Od -T cs_6_8 -spirv -fcgl %s | FileCheck %s
2+
3+
// CHECK: %spirvIntrinsicType = OpTypeInt 8 0
4+
using uint8_t [[vk::ext_capability(/* Int8 */ 39)]] =
5+
vk::SpirvType</* OpTypeInt */ 21, 8, 8, vk::Literal<vk::integral_constant<uint, 8> >,
6+
vk::Literal<vk::integral_constant<bool, false> > >;
7+
8+
[[vk::ext_instruction(/* OpConstant */ 43)]] uint8_t mkconsant([[vk::ext_literal]] int v);
9+
10+
// CHECK: OpConstant %spirvIntrinsicType 42
11+
static const uint8_t K = mkconsant(42);
12+
13+
[numthreads(1, 1, 1)] void main() {}

0 commit comments

Comments
 (0)