From bd6306cdaa4a49344658bd026721b6532e103d09 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 24 Feb 2023 10:01:47 -0800 Subject: More control flow simplifications. (#2673) * More control flow and Phi param simplifications. * Fix. * Fix gcc error. * Fix. * More IR cleanup. * Fix bug in phi param dce + ifelse simplify. * Propagate and DCE side-effect-free functions. * Enhance CFG simplifcation to remove loops with no side effects. * Fix. * Fixes. * Fix tests. Add [__AlwaysFoldIntoUseSite] for rayPayloadLocation. * More cleanup. * Fixes. * Fix. --------- Co-authored-by: Yong He --- source/slang/slang-ir.cpp | 51 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) (limited to 'source/slang/slang-ir.cpp') diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index accefc0c9..fd211d05c 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -43,7 +43,10 @@ namespace Slang case kIROp_PreciseDecoration: case kIROp_PublicDecoration: case kIROp_HLSLExportDecoration: - case kIROp_ReadNoneDecoration: + case kIROp_ReadNoneDecoration: + case kIROp_NoSideEffectDecoration: + case kIROp_ForwardDifferentiableDecoration: + case kIROp_BackwardDifferentiableDecoration: case kIROp_RequiresNVAPIDecoration: case kIROp_TriangleAdjInputPrimitiveTypeDecoration: case kIROp_TriangleInputPrimitiveTypeDecoration: @@ -695,6 +698,21 @@ namespace Slang } } + void IRUnconditionalBranch::removeArgument(UInt index) + { + switch (getOp()) + { + case kIROp_unconditionalBranch: + removeOperand(1 + index); + break; + case kIROp_loop: + removeOperand(3 + index); + break; + default: + SLANG_UNEXPECTED("unhandled unconditional branch opcode"); + } + } + IRInst* IRUnconditionalBranch::getArg(UInt index) { return getArgs()[index].usedValue; @@ -5109,6 +5127,17 @@ namespace Slang return inst; } + IRInst* IRBuilder::emitNot(IRType* type, IRInst* value) + { + auto inst = createInst( + this, + kIROp_Not, + type, + value); + addInst(inst); + return inst; + } + IRInst* IRBuilder::emitAdd(IRType* type, IRInst* left, IRInst* right) { auto inst = createInst( @@ -6792,6 +6821,17 @@ namespace Slang } } + void IRInst::removeOperand(Index index) + { + for (Index i = index; i < (Index)operandCount - 1; i++) + { + getOperands()[i].set(getOperand(i + 1)); + } + getOperands()[operandCount - 1].clear(); + operandCount--; + return; + } + // Remove this instruction from its parent block, // and then destroy it (it had better have no uses!) void IRInst::removeAndDeallocate() @@ -6879,6 +6919,8 @@ namespace Slang // common subexpression elimination, etc. // auto call = cast(this); + // If the call has been marked as no-side-effect, we + // will treat it so, by-passing all other checks. if (call->findDecoration()) return false; return !isPureFunctionalCall(call); @@ -6894,6 +6936,7 @@ namespace Slang case kIROp_Func: case kIROp_Generic: case kIROp_Var: + case kIROp_Param: case kIROp_GlobalVar: // Note: the IRGlobalVar represents the *address*, so only a load/store would have side effects case kIROp_GlobalConstant: case kIROp_GlobalParam: @@ -7003,12 +7046,6 @@ namespace Slang case kIROp_BackwardDifferentiatePropagate: return false; } - - // Check if the calle has been marked with a catch-all no-side-effect decoration. - if (findDecoration()) - { - return false; - } return true; } -- cgit v1.2.3