summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir-peephole.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-08-24 16:32:33 -0700
committerGitHub <noreply@github.com>2023-08-24 16:32:33 -0700
commit0470ea05a42d6c3f35d81a433fefdd440500cdbd (patch)
tree25feb7bfd539013bfa64d8ff7698262932e39110 /source/slang/slang-ir-peephole.cpp
parentc515bf9edf0ceefa9a0c9b36626ea7c8f72ce36f (diff)
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 <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-ir-peephole.cpp')
-rw-r--r--source/slang/slang-ir-peephole.cpp77
1 files changed, 77 insertions, 0 deletions
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<IRVectorType>(inst->getOperand(0)->getDataType());
+ auto resultType = as<IRVectorType>(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<IRIntLit>(fromType->getElementCount());
+ if (!fromCount)
+ break;
+ auto toCount = as<IRIntLit>(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<IRMatrixType>(inst->getOperand(0)->getDataType());
+ auto resultType = as<IRMatrixType>(inst->getDataType());
+ SLANG_ASSERT(fromType && resultType);
+ auto fromRows = as<IRIntLit>(fromType->getRowCount());
+ if (!fromRows) break;
+ auto fromCols = as<IRIntLit>(fromType->getColumnCount());
+ if (!fromCols) break;
+ auto toRows = as<IRIntLit>(resultType->getRowCount());
+ if (!toRows) break;
+ auto toCols = as<IRIntLit>(resultType->getColumnCount());
+ if (!toCols) break;
+ List<IRInst*> 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: