From c8e6a6452f4e531dca09152178bae2f9a2fb999a Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 10 May 2023 09:11:36 -0700 Subject: Generate faster derivative for div by const operations. (#2877) * Generate faster derivative for div by const operations. * Increase `kMaxIterationsToAttempt` to 256. --------- Co-authored-by: Yong He --- source/slang/slang-ir-util.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'source/slang/slang-ir-util.cpp') diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp index 55c2b18c0..a978edc48 100644 --- a/source/slang/slang-ir-util.cpp +++ b/source/slang/slang-ir-util.cpp @@ -846,6 +846,74 @@ bool isGlobalOrUnknownMutableAddress(IRGlobalValueWithCode* parentFunc, IRInst* return (addrInstParent != parentFunc); } +bool isZero(IRInst* inst) +{ + switch (inst->getOp()) + { + case kIROp_IntLit: + return as(inst)->getValue() == 0; + case kIROp_FloatLit: + return as(inst)->getValue() == 0.0; + case kIROp_BoolLit: + return as(inst)->getValue() == false; + case kIROp_MakeVector: + case kIROp_MakeVectorFromScalar: + case kIROp_MakeMatrix: + case kIROp_MakeMatrixFromScalar: + case kIROp_MatrixReshape: + case kIROp_VectorReshape: + { + for (UInt i = 0; i < inst->getOperandCount(); i++) + { + if (!isZero(inst->getOperand(i))) + { + return false; + } + } + return true; + } + case kIROp_CastIntToFloat: + case kIROp_CastFloatToInt: + return isZero(inst->getOperand(0)); + default: + return false; + } +} + +bool isOne(IRInst* inst) +{ + switch (inst->getOp()) + { + case kIROp_IntLit: + return as(inst)->getValue() == 1; + case kIROp_FloatLit: + return as(inst)->getValue() == 1.0; + case kIROp_BoolLit: + return as(inst)->getValue(); + case kIROp_MakeVector: + case kIROp_MakeVectorFromScalar: + case kIROp_MakeMatrix: + case kIROp_MakeMatrixFromScalar: + case kIROp_MatrixReshape: + case kIROp_VectorReshape: + { + for (UInt i = 0; i < inst->getOperandCount(); i++) + { + if (!isOne(inst->getOperand(i))) + { + return false; + } + } + return true; + } + case kIROp_CastIntToFloat: + case kIROp_CastFloatToInt: + return isOne(inst->getOperand(0)); + default: + return false; + } +} + struct GenericChildrenMigrationContextImpl { IRCloneEnv cloneEnv; -- cgit v1.2.3