summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-util.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-05-10 09:11:36 -0700
committerGitHub <noreply@github.com>2023-05-10 09:11:36 -0700
commitc8e6a6452f4e531dca09152178bae2f9a2fb999a (patch)
treedab0f646dc520d2a187f64885e7b7fe152b49f5e /source/slang/slang-ir-util.cpp
parentddebd60853b3f34bfd8e89de804fd15808abf75d (diff)
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 <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-ir-util.cpp')
-rw-r--r--source/slang/slang-ir-util.cpp68
1 files changed, 68 insertions, 0 deletions
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<IRIntLit>(inst)->getValue() == 0;
+ case kIROp_FloatLit:
+ return as<IRFloatLit>(inst)->getValue() == 0.0;
+ case kIROp_BoolLit:
+ return as<IRBoolLit>(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<IRIntLit>(inst)->getValue() == 1;
+ case kIROp_FloatLit:
+ return as<IRFloatLit>(inst)->getValue() == 1.0;
+ case kIROp_BoolLit:
+ return as<IRBoolLit>(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;