[llvm] [LoopInterchange] Add metadata to control loop-interchange (PR #127474)
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 7 10:17:13 PST 2025
================
@@ -569,6 +609,152 @@ struct LoopInterchange {
return true;
}
+
+ bool processEnabledLoop(SmallVectorImpl<Loop *> &LoopList,
+ std::vector<std::vector<char>> &DependencyMatrix,
+ const DenseMap<const Loop *, unsigned> &CostMap) {
+ bool Changed = false;
+
+ // Manage the index so that LoopList[Loop2Index[L]] == L for each loop L.
+ DenseMap<Loop *, unsigned> Loop2Index;
+ for (unsigned I = 0; I != LoopList.size(); I++)
+ Loop2Index[LoopList[I]] = I;
+
+ // Hold outer loops to be exchanged (i.e., loops that have
+ // "llvm.loop.interchange.enable" is true), in the current nest order.
+ SmallVector<Loop *, 4> Worklist;
+
+ // Helper funciton to try to add a new loop into the Worklist. Return false
+ // if there is a duplicate in the loop to be interchanged.
+ auto AddLoopIfEnabled = [&](Loop *L) {
+ if (findMetadata(L) == true) {
+ if (!Worklist.empty()) {
+ // Because the loops are sorted in the order of the current nest, it
+ // is sufficient to compare with the last element.
+ unsigned InnerLoopId = Loop2Index[Worklist.back()] + 1;
+ unsigned OuterLoopId = Loop2Index[L];
+ if (OuterLoopId <= InnerLoopId) {
+ ORE->emit([&]() {
+ return OptimizationRemarkMissed(DEBUG_TYPE, "AmbiguousOrder",
+ L->getStartLoc(), L->getHeader())
+ << "The loops to be interchanged are overlapping.";
+ });
+ return false;
+ }
+ }
+ Worklist.push_back(L);
+ }
+ return true;
+ };
+
+ // Initialize Worklist. To process the loops in inner-loop-first order, add
+ // them to the worklist in the outer-loop-first order.
+ for (unsigned I = 0; I != LoopList.size(); I++)
+ if (!AddLoopIfEnabled(LoopList[I]))
+ return Changed;
+
+ // Set an upper bound of the number of transformations to avoid infinite
+ // loop. There is no deep meaning behind the current value (square of the
+ // size of LoopList).
+ // TODO: Is this really necessary?
----------------
Meinersbur wrote:
If the metadata is properly remove after having applied the interchange, then not. Consider an assertion for debug builds when that happens.
https://github.com/llvm/llvm-project/pull/127474
More information about the llvm-commits
mailing list