From 8822d548a95c69094e1c96c6c51df35c04ffae61 Mon Sep 17 00:00:00 2001 From: Ryotaro Kasuga Date: Fri, 28 Feb 2025 02:41:55 +0000 Subject: [PATCH 1/2] [LoopInterchange] Move some processes to another function (NFC) Some post-processing involved in exchanging a pair of loops has been done separately from `processLoop`, which is a main function that does the transformation. It's better to consolidate these processes into the same function. This patch is a preparation of #127474. --- .../lib/Transforms/Scalar/LoopInterchange.cpp | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index f45d90ff13e14..50a527c1a9247 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -508,21 +508,11 @@ struct LoopInterchange { // and do interchange based on a bubble-sort fasion. We start from // the innermost loop, move it outwards to the best possible position // and repeat this process. - for (unsigned j = SelecLoopId; j > 0; j--) { + for (unsigned J = SelecLoopId; J > 0; J--) { bool ChangedPerIter = false; - for (unsigned i = SelecLoopId; i > SelecLoopId - j; i--) { - bool Interchanged = processLoop(LoopList[i], LoopList[i - 1], i, i - 1, - DependencyMatrix, CostMap); - if (!Interchanged) - continue; - // Loops interchanged, update LoopList accordingly. - std::swap(LoopList[i - 1], LoopList[i]); - // Update the DependencyMatrix - interChangeDependencies(DependencyMatrix, i, i - 1); - - LLVM_DEBUG(dbgs() << "Dependency matrix after interchange:\n"; - printDepMatrix(DependencyMatrix)); - + for (unsigned I = SelecLoopId; I > SelecLoopId - J; I--) { + bool Interchanged = + processLoop(LoopList, I, I - 1, DependencyMatrix, CostMap); ChangedPerIter |= Interchanged; Changed |= Interchanged; } @@ -534,10 +524,12 @@ struct LoopInterchange { return Changed; } - bool processLoop(Loop *InnerLoop, Loop *OuterLoop, unsigned InnerLoopId, + bool processLoop(SmallVectorImpl &LoopList, unsigned InnerLoopId, unsigned OuterLoopId, std::vector> &DependencyMatrix, const DenseMap &CostMap) { + Loop *OuterLoop = LoopList[OuterLoopId]; + Loop *InnerLoop = LoopList[InnerLoopId]; LLVM_DEBUG(dbgs() << "Processing InnerLoopId = " << InnerLoopId << " and OuterLoopId = " << OuterLoopId << "\n"); LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, ORE); @@ -566,6 +558,15 @@ struct LoopInterchange { LoopsInterchanged++; llvm::formLCSSARecursively(*OuterLoop, *DT, LI, SE); + + // Loops interchanged, update LoopList accordingly. + std::swap(LoopList[OuterLoopId], LoopList[InnerLoopId]); + // Update the DependencyMatrix + interChangeDependencies(DependencyMatrix, InnerLoopId, OuterLoopId); + + LLVM_DEBUG(dbgs() << "Dependency matrix after interchange:\n"; + printDepMatrix(DependencyMatrix)); + return true; } }; From a0458dafc072eaba09128c7bed57b43e6b694bac Mon Sep 17 00:00:00 2001 From: Ryotaro Kasuga Date: Tue, 4 Mar 2025 06:36:31 +0000 Subject: [PATCH 2/2] Revert variable name changes --- llvm/lib/Transforms/Scalar/LoopInterchange.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index 50a527c1a9247..967be109a7ba6 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -508,11 +508,11 @@ struct LoopInterchange { // and do interchange based on a bubble-sort fasion. We start from // the innermost loop, move it outwards to the best possible position // and repeat this process. - for (unsigned J = SelecLoopId; J > 0; J--) { + for (unsigned j = SelecLoopId; j > 0; j--) { bool ChangedPerIter = false; - for (unsigned I = SelecLoopId; I > SelecLoopId - J; I--) { + for (unsigned i = SelecLoopId; i > SelecLoopId - j; i--) { bool Interchanged = - processLoop(LoopList, I, I - 1, DependencyMatrix, CostMap); + processLoop(LoopList, i, i - 1, DependencyMatrix, CostMap); ChangedPerIter |= Interchanged; Changed |= Interchanged; }