From 0470ea05a42d6c3f35d81a433fefdd440500cdbd Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 24 Aug 2023 16:32:33 -0700 Subject: Misc. SPIRV Fixes, Part 2. (#3147) * Misc. SPIRV Fixes, Part 2. * Fix up. * Fix. * Add system smenatic values. * 16 bit int and floats, matrix/vector reshape, bool ops. * Fix. * Fix. * Allow push constant entry point params. * entrypoint params. * swizzleSet and swizzledStore. * packoffset. * string hash. * Fix. * Matrix arithmetics. --------- Co-authored-by: Yong He --- source/slang/slang-ir-peephole.cpp | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'source/slang/slang-ir-peephole.cpp') diff --git a/source/slang/slang-ir-peephole.cpp b/source/slang/slang-ir-peephole.cpp index 600361b2f..d83c6dccd 100644 --- a/source/slang/slang-ir-peephole.cpp +++ b/source/slang/slang-ir-peephole.cpp @@ -683,6 +683,83 @@ struct PeepholeContext : InstPassBase } } break; + case kIROp_VectorReshape: + { + auto fromType = as(inst->getOperand(0)->getDataType()); + auto resultType = as(inst->getDataType()); + if (!resultType) + { + if (!fromType) + { + inst->replaceUsesWith(inst->getOperand(0)); + maybeRemoveOldInst(inst); + changed = true; + break; + } + IRBuilder builder(inst); + builder.setInsertBefore(inst); + UInt index = 0; + auto newInst = builder.emitSwizzle(resultType, inst->getOperand(0), 1, &index); + inst->replaceUsesWith(newInst); + maybeRemoveOldInst(inst); + changed = true; + break; + } + auto fromCount = as(fromType->getElementCount()); + if (!fromCount) + break; + auto toCount = as(resultType->getElementCount()); + if (!toCount) + break; + IRBuilder builder(inst); + builder.setInsertBefore(inst); + auto newInst = builder.emitVectorReshape(resultType, inst->getOperand(0)); + if (newInst != inst) + { + inst->replaceUsesWith(newInst); + maybeRemoveOldInst(inst); + changed = true; + } + } + break; + case kIROp_MatrixReshape: + { + auto fromType = as(inst->getOperand(0)->getDataType()); + auto resultType = as(inst->getDataType()); + SLANG_ASSERT(fromType && resultType); + auto fromRows = as(fromType->getRowCount()); + if (!fromRows) break; + auto fromCols = as(fromType->getColumnCount()); + if (!fromCols) break; + auto toRows = as(resultType->getRowCount()); + if (!toRows) break; + auto toCols = as(resultType->getColumnCount()); + if (!toCols) break; + List rows; + IRBuilder builder(inst); + builder.setInsertBefore(inst); + auto toRowType = builder.getVectorType(resultType->getElementType(), resultType->getColumnCount()); + for (IRIntegerValue i = 0; i < toRows->getValue(); i++) + { + if (i < fromRows->getValue()) + { + auto originalRow = builder.emitElementExtract(inst->getOperand(0), i); + auto resizedRow = builder.emitVectorReshape(toRowType, originalRow); + rows.add(resizedRow); + } + else + { + auto zero = builder.emitDefaultConstruct(resultType->getElementType()); + auto row = builder.emitMakeVectorFromScalar(toRowType, zero); + rows.add(row); + } + } + auto newInst = builder.emitMakeMatrix(resultType, (UInt)rows.getCount(), rows.getBuffer()); + inst->replaceUsesWith(newInst); + maybeRemoveOldInst(inst); + changed = true; + } + break; case kIROp_Add: case kIROp_Mul: case kIROp_Sub: -- cgit v1.2.3