From b9b2c03c6f714a842d4325c67edc09ce4e38e8f6 Mon Sep 17 00:00:00 2001 From: "Wang, Elvin" Date: Mon, 9 Oct 2023 18:42:44 +0000 Subject: [PATCH] Refactor constant folding pass Run the pass until nothing can be folded --- IGC/Compiler/CustomSafeOptPass.cpp | 106 +++++++++++++++-------------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/IGC/Compiler/CustomSafeOptPass.cpp b/IGC/Compiler/CustomSafeOptPass.cpp index 965a87f8e562..376f84428a9e 100644 --- a/IGC/Compiler/CustomSafeOptPass.cpp +++ b/IGC/Compiler/CustomSafeOptPass.cpp @@ -4258,72 +4258,78 @@ bool IGCConstProp::runOnFunction(Function& F) module = F.getParent(); // Initialize the worklist to all of the instructions ready to process... llvm::SetVector WorkList; - for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) - { - WorkList.insert(&*i); - } bool Changed = false; - m_TD = &F.getParent()->getDataLayout(); - m_TLI = &getAnalysis().getTLI(); - while (!WorkList.empty()) + bool NotClosed; + do // Fold constants until closure { - Instruction* I = WorkList.pop_back_val(); // Get an element from the worklist... - if (I->use_empty()) // Don't muck with dead instructions... + NotClosed = false; + for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) { - continue; + WorkList.insert(&*i); } - Constant* C = nullptr; - C = ConstantFoldInstruction(I, *m_TD, m_TLI); - - if (!C && isa(I)) + m_TD = &F.getParent()->getDataLayout(); + m_TLI = &getAnalysis().getTLI(); + while (!WorkList.empty()) { - C = ConstantFoldCallInstruction(cast(I)); - } + Instruction* I = WorkList.pop_back_val(); // Get an element from the worklist... + if (I->use_empty()) // Don't muck with dead instructions... + { + continue; + } + Constant* C = nullptr; + C = ConstantFoldInstruction(I, *m_TD, m_TLI); - // replace shader-constant load with the known value - if (!C && isa(I)) - { - C = replaceShaderConstant(cast(I)); - } - if (!C && isa(I)) - { - C = ConstantFoldCmpInst(cast(I)); - } - if (!C && isa(I)) - { - C = ConstantFoldExtractElement(cast(I)); - } - if (C) - { - // Add all of the users of this instruction to the worklist, they might - // be constant propagatable now... - for (Value::user_iterator UI = I->user_begin(), UE = I->user_end(); - UI != UE; ++UI) + if (!C && isa(I)) { - WorkList.insert(cast(*UI)); + C = ConstantFoldCallInstruction(cast(I)); } - // Replace all of the uses of a variable with uses of the constant. - I->replaceAllUsesWith(C); + // replace shader-constant load with the known value + if (!C && isa(I)) + { + C = replaceShaderConstant(cast(I)); + } + if (!C && isa(I)) + { + C = ConstantFoldCmpInst(cast(I)); + } + if (!C && isa(I)) + { + C = ConstantFoldExtractElement(cast(I)); + } - // Remove the dead instruction. - I->eraseFromParent(); + if (C) + { + // Add all of the users of this instruction to the worklist, they might + // be constant propagatable now... + for (Value::user_iterator UI = I->user_begin(), UE = I->user_end(); + UI != UE; ++UI) + { + WorkList.insert(cast(*UI)); + } - // We made a change to the function... - Changed = true; + // Replace all of the uses of a variable with uses of the constant. + I->replaceAllUsesWith(C); - // I is erased, continue to the next one. - continue; - } + // Remove the dead instruction. + I->eraseFromParent(); - if (GetElementPtrInst * GEP = dyn_cast(I)) - { - if ((m_enableSimplifyGEP || overrideEnableSimplifyGEP) && simplifyGEP(GEP)) + // We made a change to the function... + Changed = NotClosed = true; + + // I is erased, continue to the next one. + continue; + } + + if (GetElementPtrInst * GEP = dyn_cast(I)) { - Changed = true; + if ((m_enableSimplifyGEP || overrideEnableSimplifyGEP) && simplifyGEP(GEP)) + { + Changed = NotClosed = true; + } } } - } + } while (NotClosed); return Changed; }