[llvm] [LoopInterchange] Improve profitability check for vectorization (PR #133672)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 07:57:40 PDT 2025


================
@@ -180,10 +197,22 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
       // Track Output, Flow, and Anti dependencies.
       if (auto D = DI->depends(Src, Dst)) {
         assert(D->isOrdered() && "Expected an output, flow or anti dep.");
+        bool IsForward = true;
+
+        // If Src and Dst are in the same BB, Src is always executed before Dst
+        // in the same loop iteration. If not, we must check whether one BB
+        // dominates the other to determine if Src and Dst are executed in this
+        // order. At the moment, we don't perform such check.
+        if (Src->getParent() != Dst->getParent())
+          IsForward = false;
+
         // If the direction vector is negative, normalize it to
         // make it non-negative.
-        if (D->normalize(SE))
+        bool Normalized = D->normalize(SE);
+        if (Normalized) {
           LLVM_DEBUG(dbgs() << "Negative dependence vector normalized.\n");
+          IsForward = false;
----------------
Meinersbur wrote:

There are some deduction steps needed for this, please add some explandation into a comment:
1. If Src and Dst are not in the same BB, line 207 will consider it "not forward"
2. If Src and Dst are in the same BB, `DI->depends` will be called with `Src` being the first, `Dst` the second in the BB due to how we iterate over the instructions, i.e. assuming a forward dependency
2a. dependence vector is positve: assumption was true
2b. dependence vector is negative: If it actually is not a forward dependency, the dependence vector will be negative, and `D->normalize` reverse the dependency (to make the dependence vector positive). That is, it becomes a backward dependence and  `D->normalize`  returns true.
2c. If the dependency vector is 0, i.e. the dependency is not loop carried, `D->normalize` will not reverse the dependency. Because we called `DI->depends` with execution order of Src/Dst, we have a forward dependency
3. If Src==Dst (e.g two StoreInst of a WAW-dependency), the concept of forward/backward dependency is ill-defined. I think we should optimistically assume a forward dependency 
3a. dependence vector is positve: `DI->depends(Src, Dst)` probably can only return a positive dependence vector(?) that does not need to be normalized
3b. dependence vector is negative: probably cannot happend as discussed (add assertion?)
3c. dependence vector is zero: By atomicity of an instruction, cannot happen

https://github.com/llvm/llvm-project/pull/133672


More information about the llvm-commits mailing list