From 54ff7fd879e71f51ed3c0fda16224cbbcf0831eb Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Mon, 12 May 2025 15:32:25 -0500 Subject: Ensure to emit 32-bit type for bitfield operations for SPIRV (#7046) Close #7014 --- source/slang/slang-ir-spirv-legalize.cpp | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'source') diff --git a/source/slang/slang-ir-spirv-legalize.cpp b/source/slang/slang-ir-spirv-legalize.cpp index 82b424daa..0287ae81a 100644 --- a/source/slang/slang-ir-spirv-legalize.cpp +++ b/source/slang/slang-ir-spirv-legalize.cpp @@ -1471,6 +1471,39 @@ struct SPIRVLegalizationContext : public SourceEmitterBase } } + // TODO: Currently SPIRV doesn't support non-32-bit integer types for bitfield extract and + // insert. We will relax this restriction once this is done: + // https://github.com/shader-slang/slang/issues/7015. + void processBitFieldOp(IRInst* inst) + { + auto dataType = inst->getDataType(); + IRVectorType* vectorType = as(dataType); + Slang::IRType* elementType = dataType; + if (vectorType) + elementType = vectorType->getElementType(); + + const IntInfo i = getIntTypeInfo(elementType); + + // SPIRV doesn't support non-32bit integer types, so we need to convert + if (i.width < 32) + { + IRBuilder builder(inst); + builder.setInsertBefore(inst); + IRType* intType = i.isSigned ? builder.getIntType() : builder.getUIntType(); + auto targetType = vectorType + ? builder.getVectorType(intType, vectorType->getElementCount()) + : intType; + auto baseInst = builder.emitCast(targetType, inst->getOperand(0)); + builder.replaceOperand(inst->getOperands(), baseInst); + if (inst->getOp() == kIROp_BitfieldInsert) + { + auto insertInst = builder.emitCast(targetType, inst->getOperand(1)); + builder.replaceOperand(inst->getOperands() + 1, insertInst); + } + inst->setFullType(intType); + } + } + void legalizeSPIRVEntryPoint(IRFunc* func, IREntryPointDecoration* entryPointDecor) { auto stage = entryPointDecor->getProfile().getStage(); @@ -1704,6 +1737,11 @@ struct SPIRVLegalizationContext : public SourceEmitterBase case kIROp_SPIRVAsm: processSPIRVAsm(as(inst)); break; + case kIROp_BitfieldExtract: + case kIROp_BitfieldInsert: + processBitFieldOp(inst); + break; + case kIROp_DebugValue: if (!isSimpleDataType(as(inst)->getDebugVar()->getDataType())) inst->removeAndDeallocate(); -- cgit v1.2.3