[llvm] [EarlyCSE] Rematerialize alignment assumption. (PR #109131)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 15 03:31:05 PST 2025


https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/109131

>From 1a4de545c20fd0070348373baed48069a9d8d139 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 18 Sep 2024 13:38:52 +0100
Subject: [PATCH 1/5] [EarlyCSE] Rematerialize alignment assumption.

---
 llvm/lib/Transforms/Scalar/EarlyCSE.cpp             | 13 +++++++++++++
 .../EarlyCSE/materialize-align-assumptions.ll       |  3 +++
 2 files changed, 16 insertions(+)

diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
index 3a0ae6b01a1144..4f8a2e06e6b451 100644
--- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
+++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -31,6 +31,7 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
@@ -1599,6 +1600,18 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
         if (InVal.IsLoad)
           if (auto *I = dyn_cast<Instruction>(Op))
             combineMetadataForCSE(I, &Inst, false);
+
+        // If the load has align metadata, preserve it via an alignment
+        // assumption. Note that this doesn't use salavageKnowledge, as we need
+        // to create the assumption for the value we replaced the load with.
+        if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
+          auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
+          if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() != 0) {
+            IRBuilder B(&Inst);
+            B.CreateAlignmentAssumption(SQ.DL, Op, A);
+          }
+        }
+
         if (!Inst.use_empty())
           Inst.replaceAllUsesWith(Op);
         salvageKnowledge(&Inst, &AC);
diff --git a/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll b/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
index ea63376957162b..628577b0975071 100644
--- a/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
+++ b/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
@@ -10,6 +10,7 @@ define ptr @align_replacement_does_not_have_align_metadata(ptr noalias %p) {
 ; CHECK-NEXT:    call void @foo(ptr [[L_1]])
 ; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, ptr [[L_1]], i64 4
 ; CHECK-NEXT:    store ptr [[GEP]], ptr [[P]], align 8
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i64 4) ]
 ; CHECK-NEXT:    ret ptr [[GEP]]
 ;
   %l.1 = load ptr, ptr %p, align 8
@@ -27,6 +28,7 @@ define ptr @align_replacement_does_not_have_align_metadata2(ptr noalias %p) {
 ; CHECK-NEXT:    [[L_1:%.*]] = load ptr, ptr [[P]], align 8
 ; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, ptr [[L_1]], i64 4
 ; CHECK-NEXT:    store ptr [[GEP]], ptr [[P]], align 8
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "align"(ptr [[GEP]], i64 4) ]
 ; CHECK-NEXT:    ret ptr [[GEP]]
 ;
   %l.1 = load ptr, ptr %p, align 8
@@ -54,6 +56,7 @@ define ptr @align_replacement_has_smaller_alignment(ptr noalias %p) {
 ; CHECK-SAME: ptr noalias [[P:%.*]]) {
 ; CHECK-NEXT:    [[L_1:%.*]] = load ptr, ptr [[P]], align 8, !align [[META0]]
 ; CHECK-NEXT:    call void @foo(ptr [[L_1]])
+; CHECK-NEXT:    call void @llvm.assume(i1 true) [ "align"(ptr [[L_1]], i64 8) ]
 ; CHECK-NEXT:    ret ptr [[L_1]]
 ;
   %l.1 = load ptr, ptr %p, align 8, !align !0

>From c176752c7cfbe14a3de24ad13bd4dbdf6228dd78 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 19 Sep 2024 21:33:40 +0100
Subject: [PATCH 2/5] !fixup check noundef

---
 llvm/lib/Transforms/Scalar/EarlyCSE.cpp       | 20 ++++++-----
 .../EarlyCSE/materialize-align-assumptions.ll | 35 ++++++++++++++-----
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
index 4f8a2e06e6b451..3745311e0ff307 100644
--- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
+++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -1601,14 +1601,18 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
           if (auto *I = dyn_cast<Instruction>(Op))
             combineMetadataForCSE(I, &Inst, false);
 
-        // If the load has align metadata, preserve it via an alignment
-        // assumption. Note that this doesn't use salavageKnowledge, as we need
-        // to create the assumption for the value we replaced the load with.
-        if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
-          auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
-          if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() != 0) {
-            IRBuilder B(&Inst);
-            B.CreateAlignmentAssumption(SQ.DL, Op, A);
+        // If the load has align and noundef metadata, preserve it via an
+        // alignment assumption. Note that this doesn't use salavageKnowledge,
+        // as we need to create the assumption for the value we replaced the
+        // load with.
+        if (Inst.hasMetadata(LLVMContext::MD_noundef)) {
+          if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
+            auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
+            if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() !=
+                0) {
+              IRBuilder B(&Inst);
+              B.CreateAlignmentAssumption(SQ.DL, Op, A);
+            }
           }
         }
 
diff --git a/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll b/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
index 628577b0975071..837a73a00d6431 100644
--- a/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
+++ b/llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
@@ -3,6 +3,24 @@
 
 declare void @foo(ptr)
 
+define ptr @align_replacement_does_not_have_align_metadata_missing_noundef(ptr noalias %p) {
+; CHECK-LABEL: define ptr @align_replacement_does_not_have_align_metadata_missing_noundef(
+; CHECK-SAME: ptr noalias [[P:%.*]]) {
+; CHECK-NEXT:    [[L_1:%.*]] = load ptr, ptr [[P]], align 8
+; CHECK-NEXT:    call void @foo(ptr [[L_1]])
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, ptr [[L_1]], i64 4
+; CHECK-NEXT:    store ptr [[GEP]], ptr [[P]], align 8
+; CHECK-NEXT:    ret ptr [[GEP]]
+;
+  %l.1 = load ptr, ptr %p, align 8
+  call void @foo(ptr %l.1)
+  %l.2 = load ptr, ptr %p, align 8
+  %gep = getelementptr i8, ptr %l.2, i64 4
+  store ptr %gep, ptr %p, align 8
+  %l.3 = load ptr, ptr %p, align 8, !align !0
+  ret ptr %l.3
+}
+
 define ptr @align_replacement_does_not_have_align_metadata(ptr noalias %p) {
 ; CHECK-LABEL: define ptr @align_replacement_does_not_have_align_metadata(
 ; CHECK-SAME: ptr noalias [[P:%.*]]) {
@@ -18,7 +36,7 @@ define ptr @align_replacement_does_not_have_align_metadata(ptr noalias %p) {
   %l.2 = load ptr, ptr %p, align 8
   %gep = getelementptr i8, ptr %l.2, i64 4
   store ptr %gep, ptr %p, align 8
-  %l.3 = load ptr, ptr %p, align 8, !align !0
+  %l.3 = load ptr, ptr %p, align 8, !align !0, !noundef !{}
   ret ptr %l.3
 }
 
@@ -34,7 +52,7 @@ define ptr @align_replacement_does_not_have_align_metadata2(ptr noalias %p) {
   %l.1 = load ptr, ptr %p, align 8
   %gep = getelementptr i8, ptr %l.1, i64 4
   store ptr %gep, ptr %p, align 8
-  %l.2 = load ptr, ptr %p, align 8, !align !0
+  %l.2 = load ptr, ptr %p, align 8, !align !0, !noundef !{}
   ret ptr %l.2
 }
 
@@ -61,7 +79,7 @@ define ptr @align_replacement_has_smaller_alignment(ptr noalias %p) {
 ;
   %l.1 = load ptr, ptr %p, align 8, !align !0
   call void @foo(ptr %l.1)
-  %l.2 = load ptr, ptr %p, align 8, !align !1
+  %l.2 = load ptr, ptr %p, align 8, !align !1, !noundef !{}
   ret ptr %l.2
 }
 
@@ -70,12 +88,12 @@ define ptr @align_replacement_has_larger_alignment(ptr %p) {
 ; CHECK-SAME: ptr [[P:%.*]]) {
 ; CHECK-NEXT:    [[L_1:%.*]] = load ptr, ptr [[P]], align 8, !align [[META1:![0-9]+]]
 ; CHECK-NEXT:    call void @foo(ptr [[L_1]])
-; CHECK-NEXT:    [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META0]]
+; CHECK-NEXT:    [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META0]], !noundef [[META2:![0-9]+]]
 ; CHECK-NEXT:    ret ptr [[L_2]]
 ;
   %l.1 = load ptr, ptr %p, align 8, !align !1
   call void @foo(ptr %l.1)
-  %l.2 = load ptr, ptr %p, align 8, !align !0
+  %l.2 = load ptr, ptr %p, align 8, !align !0, !noundef !{}
   ret ptr %l.2
 }
 
@@ -84,12 +102,12 @@ define ptr @align_1(ptr %p) {
 ; CHECK-SAME: ptr [[P:%.*]]) {
 ; CHECK-NEXT:    [[L_1:%.*]] = load ptr, ptr [[P]], align 8
 ; CHECK-NEXT:    call void @foo(ptr [[L_1]])
-; CHECK-NEXT:    [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META2:![0-9]+]]
+; CHECK-NEXT:    [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META3:![0-9]+]], !noundef [[META2]]
 ; CHECK-NEXT:    ret ptr [[L_2]]
 ;
   %l.1 = load ptr, ptr %p, align 8
   call void @foo(ptr %l.1)
-  %l.2 = load ptr, ptr %p, align 8, !align !2
+  %l.2 = load ptr, ptr %p, align 8, !align !2, !noundef !{}
   ret ptr %l.2
 }
 
@@ -99,5 +117,6 @@ define ptr @align_1(ptr %p) {
 ;.
 ; CHECK: [[META0]] = !{i64 4}
 ; CHECK: [[META1]] = !{i64 8}
-; CHECK: [[META2]] = !{i64 1}
+; CHECK: [[META2]] = !{}
+; CHECK: [[META3]] = !{i64 1}
 ;.

>From bbcbf7a8aea6f8d14c69b3641c61149b1962b761 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Mon, 13 Jan 2025 21:31:42 +0000
Subject: [PATCH 3/5] Use computeKnownBits.

---
 llvm/lib/Transforms/Scalar/EarlyCSE.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
index 3745311e0ff307..378c16f126f4de 100644
--- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
+++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -16,6 +16,7 @@
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/ScopedHashTable.h"
+#include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/AssumptionCache.h"
@@ -1607,9 +1608,11 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
         // load with.
         if (Inst.hasMetadata(LLVMContext::MD_noundef)) {
           if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
+            Inst.setMetadata(LLVMContext::MD_align, nullptr);
             auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
-            if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() !=
-                0) {
+            auto KB = computeKnownBits(Op, SQ.DL);
+            unsigned AlignFromKB = 1 << KB.countMinTrailingZeros();
+            if (AlignFromKB < A->getZExtValue()) {
               IRBuilder B(&Inst);
               B.CreateAlignmentAssumption(SQ.DL, Op, A);
             }

>From 9ac479d93269fe02ae0bb664e78b599f42c2fe0d Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Mon, 13 Jan 2025 21:32:09 +0000
Subject: [PATCH 4/5] Remove align assumptions if possible.

---
 .../InstCombine/InstCombineCalls.cpp          | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index dd5a4ba5a4724a..9af24c18b9531c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3226,6 +3226,42 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         MaybeSimplifyHint(OBU.Inputs[0]);
         MaybeSimplifyHint(OBU.Inputs[1]);
       }
+
+      if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
+        RetainedKnowledge RK = getKnowledgeFromBundle(
+            *cast<AssumeInst>(II), II->bundle_op_info_begin()[Idx]);
+        if (!RK || RK.AttrKind != Attribute::Alignment ||
+            !isPowerOf2_64(RK.ArgValue))
+          continue;
+        SetVector<const Instruction *> WorkList;
+        bool AlignNeeded = false;
+        WorkList.insert(II);
+        for (unsigned I = 0; I != WorkList.size(); ++I) {
+          if (auto *LI = dyn_cast<LoadInst>(WorkList[I])) {
+            if (auto *AlignMD = LI->getMetadata(LLVMContext::MD_align)) {
+              auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
+
+              if (A->getZExtValue() % RK.ArgValue != 0) {
+                AlignNeeded = true;
+                break;
+              }
+            }
+          }
+          if (isa<ICmpInst>(WorkList[I])) {
+            AlignNeeded = true;
+            break;
+          }
+          if (WorkList.size() > 16) {
+            AlignNeeded = true;
+            break;
+          }
+
+          for (const User *U : WorkList[I]->users())
+            WorkList.insert(cast<Instruction>(U));
+        }
+        auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
+        return New;
+      }
     }
 
     // Convert nonnull assume like:

>From e103d62999b8273d60a30f3c6db9881580338fde Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Tue, 14 Jan 2025 14:08:44 +0000
Subject: [PATCH 5/5] step

---
 .../InstCombine/InstCombineCalls.cpp          | 30 ++++++++++++++-----
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 9af24c18b9531c..28e9606bb76df0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3233,11 +3233,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         if (!RK || RK.AttrKind != Attribute::Alignment ||
             !isPowerOf2_64(RK.ArgValue))
           continue;
-        SetVector<const Instruction *> WorkList;
+        SetVector<const Value *> WorkList;
         bool AlignNeeded = false;
-        WorkList.insert(II);
+          for (const User *U : RK.WasOn->users())
+            WorkList.insert(cast<Instruction>(U));
+
         for (unsigned I = 0; I != WorkList.size(); ++I) {
-          if (auto *LI = dyn_cast<LoadInst>(WorkList[I])) {
+          auto *Curr = WorkList[I];
+          if (auto *LI = dyn_cast<LoadInst>(Curr)) {
             if (auto *AlignMD = LI->getMetadata(LLVMContext::MD_align)) {
               auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
 
@@ -3245,9 +3248,20 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
                 AlignNeeded = true;
                 break;
               }
+
+              if (LI->getAlign().value() < RK.ArgValue) {
+                AlignNeeded = true;
+                break;
+              }
+            }
+          }
+          if (auto *SI = dyn_cast<StoreInst>(Curr)) {
+            if (SI->getAlign().value() < RK.ArgValue) {
+              AlignNeeded = true;
+              break;
             }
           }
-          if (isa<ICmpInst>(WorkList[I])) {
+          if (isa<ICmpInst>(Curr) && !isa<Constant>(cast<Instruction>(Curr)->getOperand(0)) && !isa<Constant>(cast<Instruction>(Curr)->getOperand(1))) {
             AlignNeeded = true;
             break;
           }
@@ -3256,11 +3270,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
             break;
           }
 
-          for (const User *U : WorkList[I]->users())
+          for (const User *U : Curr->users())
             WorkList.insert(cast<Instruction>(U));
         }
-        auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
-        return New;
+        if (!AlignNeeded) {
+          auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
+          return New;
+        }
       }
     }
 



More information about the llvm-commits mailing list