Skip to content

Commit 2afa325

Browse files
authored
[SPIR-V] Fix an issue where OpConstantComposite is being used with spec constants (#5263)
Fixes #4849
1 parent 40e3d02 commit 2afa325

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

tools/clang/lib/SPIRV/EmitVisitor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ bool EmitVisitor::visit(SpirvIntrinsicInstruction *inst) {
19631963
return true;
19641964
}
19651965

1966-
bool EmitVisitor::visit(SpirvEmitMeshTasksEXT *inst) {
1966+
bool EmitVisitor::visit(SpirvEmitMeshTasksEXT *inst) {
19671967
initInstruction(inst);
19681968

19691969
curInst.push_back(getOrAssignResultId<SpirvInstruction>(inst->getXDimension()));
@@ -2310,7 +2310,8 @@ EmitTypeHandler::getOrCreateConstantComposite(SpirvConstantComposite *inst) {
23102310
} else {
23112311
// Constant wasn't emitted in the past.
23122312
const uint32_t typeId = emitType(inst->getResultType());
2313-
initTypeInstruction(spv::Op::OpConstantComposite);
2313+
initTypeInstruction(isSpecConst ? spv::Op::OpSpecConstantComposite
2314+
: spv::Op::OpConstantComposite);
23142315
curTypeInst.push_back(typeId);
23152316
curTypeInst.push_back(getOrAssignResultId<SpirvInstruction>(inst));
23162317
for (auto constituent : inst->getConstituents())

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,7 +6938,8 @@ SpirvInstruction *SpirvEmitter::createVectorSplat(const Expr *scalarExpr,
69386938
// Should find a more meaningful one.
69396939
if (auto *constVal = dyn_cast<SpirvConstant>(scalarVal)) {
69406940
llvm::SmallVector<SpirvConstant *, 4> elements(size_t(size), constVal);
6941-
auto *value = spvBuilder.getConstantComposite(vecType, elements);
6941+
const bool isSpecConst = constVal->getopcode() == spv::Op::OpSpecConstant;
6942+
auto *value = spvBuilder.getConstantComposite(vecType, elements, isSpecConst);
69426943
value->setRValue();
69436944
return value;
69446945
} else {
@@ -12018,7 +12019,7 @@ SpirvEmitter::getSpirvShaderStage(hlsl::ShaderModel::Kind smk, bool extMeshShadi
1201812019
return spv::ExecutionModel::CallableNV;
1201912020
case hlsl::ShaderModel::Kind::Mesh:
1202012021
return extMeshShading ?
12021-
spv::ExecutionModel::MeshEXT:
12022+
spv::ExecutionModel::MeshEXT:
1202212023
spv::ExecutionModel::MeshNV;
1202312024
case hlsl::ShaderModel::Kind::Amplification:
1202412025
return extMeshShading ?
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %dxc -T cs_6_0 -E main
2+
3+
[[vk::constant_id(0)]] const uint kConst = 1;
4+
5+
RWTexture2D<float3> img;
6+
7+
[numthreads(1, 1, 1)]
8+
void main(uint3 svDispatchThreadId : SV_DISPATCHTHREADID)
9+
{
10+
// CHECK: OpSpecConstantComposite %v2uint %kConst %kConst
11+
img[svDispatchThreadId.xy * kConst] = 0;
12+
}

tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,9 @@ TEST_F(FileTest, VulkanSpecConstantError5) {
22642264
TEST_F(FileTest, VulkanSpecConstantErrorNotSegfault) {
22652265
runFileTest("vk.spec-constant.error.not.segfault.hlsl", Expect::Failure);
22662266
}
2267+
TEST_F(FileTest, VulkanSpecConstantComposite) {
2268+
runFileTest("vk.spec-constant.composite.hlsl");
2269+
}
22672270

22682271
TEST_F(FileTest, VulkanLayoutCBufferMatrixZpr) {
22692272
runFileTest("vk.layout.cbuffer.zpr.hlsl");

0 commit comments

Comments
 (0)