From 81986781d3f5896c6e38d050dd7bf97b019268eb Mon Sep 17 00:00:00 2001 From: liyufei2025 Date: Fri, 10 Oct 2025 13:17:36 +0800 Subject: [PATCH 1/2] loopinterchange --- .../lib/Transforms/Scalar/LoopInterchange.cpp | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index 91286ebcea33..a7f0431233bd 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -54,7 +54,7 @@ using namespace llvm; STATISTIC(LoopsInterchanged, "Number of loops interchanged"); static cl::opt LoopInterchangeCostThreshold( - "loop-interchange-threshold", cl::init(0), cl::Hidden, + "loop-interchange-threshold", cl::init(1), cl::Hidden, cl::desc("Interchange if you gain more than this number")); namespace { @@ -114,6 +114,12 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level, for (I = MemInstr.begin(), IE = MemInstr.end(); I != IE; ++I) { for (J = I, JE = MemInstr.end(); J != JE; ++J) { + + if(I==J){ + LLVM_DEBUG(dbgs()<<"Skipping self-dependency analysis\n"); + continue; + } + std::vector Dep; Instruction *Src = cast(*I); Instruction *Dst = cast(*J); @@ -540,6 +546,11 @@ struct LoopInterchange { bool LoopInterchangeLegality::containsUnsafeInstructions(BasicBlock *BB) { return any_of(*BB, [](const Instruction &I) { + if (auto *St = dyn_cast(&I)) + return !St->isSimple(); + if(auto *Ld = dyn_cast(&I)) + return !Ld->isSimple(); + return I.mayHaveSideEffects() || I.mayReadFromMemory(); }); } @@ -561,15 +572,19 @@ bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) { for (BasicBlock *Succ : successors(OuterLoopHeaderBI)) if (Succ != InnerLoopPreHeader && Succ != InnerLoop->getHeader() && - Succ != OuterLoopLatch) - return false; + Succ != OuterLoopLatch){ + errs() << "first step error" << "\n"; + return false; + } LLVM_DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch\n"); // We do not have any basic block in between now make sure the outer header // and outer loop latch doesn't contain any unsafe instructions. if (containsUnsafeInstructions(OuterLoopHeader) || - containsUnsafeInstructions(OuterLoopLatch)) + containsUnsafeInstructions(OuterLoopLatch)){ + errs() << "second step error" << "\n"; return false; + } // Also make sure the inner loop preheader does not contain any unsafe // instructions. Note that all instructions in the preheader will be moved to @@ -1089,7 +1104,7 @@ int LoopInterchangeProfitability::getInstrOrderCost() { } } } - return GoodOrder - BadOrder; + return GoodOrder - BadOrder; } std::optional @@ -1123,7 +1138,7 @@ LoopInterchangeProfitability::isProfitablePerInstrOrderCost() { // reordering if number of bad orders is more than good. int Cost = getInstrOrderCost(); LLVM_DEBUG(dbgs() << "Cost = " << Cost << "\n"); - if (Cost < 0 && Cost < LoopInterchangeCostThreshold) + if (Cost < 1 && Cost < LoopInterchangeCostThreshold) return std::optional(true); return std::nullopt; @@ -1712,6 +1727,8 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U) { + + //errs()<<"the 5th try"<<"\n"; Function &F = *LN.getParent(); DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI); -- Gitee From 6138134c3ab94486f7c26d3d1cfe8d3edc50dda1 Mon Sep 17 00:00:00 2001 From: fanxuerun Date: Fri, 10 Oct 2025 13:25:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?loopinterchange=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- llvm/lib/Transforms/Scalar/LoopInterchange.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index a7f0431233bd..b761e5842f52 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -49,6 +49,7 @@ using namespace llvm; + #define DEBUG_TYPE "loop-interchange" STATISTIC(LoopsInterchanged, "Number of loops interchanged"); -- Gitee