Skip to content

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented May 9, 2025

As requested in #138961 (comment)

Copy link
Contributor Author

arsenm commented May 9, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@arsenm arsenm added the llvm:analysis Includes value tracking, cost tables and constant folding label May 9, 2025 — with Graphite App
@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: Matt Arsenault (arsenm)

Changes

As requested in #138961 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/139232.diff

5 Files Affected:

  • (modified) llvm/include/llvm/Analysis/AssumeBundleQueries.h (+7-6)
  • (modified) llvm/lib/Analysis/AssumeBundleQueries.cpp (+12-30)
  • (modified) llvm/lib/Analysis/Loads.cpp (+2-2)
  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+1-1)
  • (modified) llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp (+2-2)
diff --git a/llvm/include/llvm/Analysis/AssumeBundleQueries.h b/llvm/include/llvm/Analysis/AssumeBundleQueries.h
index f7a893708758c..98e9207e4435a 100644
--- a/llvm/include/llvm/Analysis/AssumeBundleQueries.h
+++ b/llvm/include/llvm/Analysis/AssumeBundleQueries.h
@@ -152,18 +152,19 @@ RetainedKnowledge getKnowledgeFromUse(const Use *U,
 /// in AttrKinds and it matches the Filter.
 RetainedKnowledge getKnowledgeForValue(
     const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
-    AssumptionCache *AC = nullptr,
+    AssumptionCache &AC,
     function_ref<bool(RetainedKnowledge, Instruction *,
-                            const CallBase::BundleOpInfo *)>
+                      const CallBase::BundleOpInfo *)>
         Filter = [](auto...) { return true; });
 
 /// Return a valid Knowledge associated to the Value V if its Attribute kind is
 /// in AttrKinds and the knowledge is suitable to be used in the context of
 /// CtxI.
-RetainedKnowledge getKnowledgeValidInContext(
-    const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
-    const Instruction *CtxI, const DominatorTree *DT = nullptr,
-    AssumptionCache *AC = nullptr);
+RetainedKnowledge
+getKnowledgeValidInContext(const Value *V,
+                           ArrayRef<Attribute::AttrKind> AttrKinds,
+                           AssumptionCache &AC, const Instruction *CtxI,
+                           const DominatorTree *DT = nullptr);
 
 /// This extracts the Knowledge from an element of an operand bundle.
 /// This is mostly for use in the assume builder.
diff --git a/llvm/lib/Analysis/AssumeBundleQueries.cpp b/llvm/lib/Analysis/AssumeBundleQueries.cpp
index b37b2270bbec5..4c890a5d21b54 100644
--- a/llvm/lib/Analysis/AssumeBundleQueries.cpp
+++ b/llvm/lib/Analysis/AssumeBundleQueries.cpp
@@ -157,51 +157,33 @@ llvm::getKnowledgeFromUse(const Use *U,
 RetainedKnowledge
 llvm::getKnowledgeForValue(const Value *V,
                            ArrayRef<Attribute::AttrKind> AttrKinds,
-                           AssumptionCache *AC,
+                           AssumptionCache &AC,
                            function_ref<bool(RetainedKnowledge, Instruction *,
                                              const CallBase::BundleOpInfo *)>
                                Filter) {
   NumAssumeQueries++;
-  if (AC) {
-    for (AssumptionCache::ResultElem &Elem : AC->assumptionsFor(V)) {
-      auto *II = cast_or_null<AssumeInst>(Elem.Assume);
-      if (!II || Elem.Index == AssumptionCache::ExprResultIdx)
-        continue;
-      if (RetainedKnowledge RK = getKnowledgeFromBundle(
-              *II, II->bundle_op_info_begin()[Elem.Index])) {
-        if (V != RK.WasOn)
-          continue;
-        if (is_contained(AttrKinds, RK.AttrKind) &&
-            Filter(RK, II, &II->bundle_op_info_begin()[Elem.Index])) {
-          NumUsefullAssumeQueries++;
-          return RK;
-        }
-      }
-    }
-    return RetainedKnowledge::none();
-  }
-
-  if (!V->hasUseList())
-    return RetainedKnowledge::none();
-
-  for (const auto &U : V->uses()) {
-    CallInst::BundleOpInfo* Bundle = getBundleFromUse(&U);
-    if (!Bundle)
+  for (AssumptionCache::ResultElem &Elem : AC.assumptionsFor(V)) {
+    auto *II = cast_or_null<AssumeInst>(Elem.Assume);
+    if (!II || Elem.Index == AssumptionCache::ExprResultIdx)
       continue;
-    if (RetainedKnowledge RK =
-            getKnowledgeFromBundle(*cast<AssumeInst>(U.getUser()), *Bundle))
+    if (RetainedKnowledge RK = getKnowledgeFromBundle(
+            *II, II->bundle_op_info_begin()[Elem.Index])) {
+      if (V != RK.WasOn)
+        continue;
       if (is_contained(AttrKinds, RK.AttrKind) &&
-          Filter(RK, cast<Instruction>(U.getUser()), Bundle)) {
+          Filter(RK, II, &II->bundle_op_info_begin()[Elem.Index])) {
         NumUsefullAssumeQueries++;
         return RK;
       }
+    }
   }
+
   return RetainedKnowledge::none();
 }
 
 RetainedKnowledge llvm::getKnowledgeValidInContext(
     const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
-    const Instruction *CtxI, const DominatorTree *DT, AssumptionCache *AC) {
+    AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT) {
   return getKnowledgeForValue(V, AttrKinds, AC,
                               [&](auto, Instruction *I, auto) {
                                 return isValidAssumeForContext(I, CtxI, DT);
diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp
index b461c41d29e84..425f3682122cd 100644
--- a/llvm/lib/Analysis/Loads.cpp
+++ b/llvm/lib/Analysis/Loads.cpp
@@ -174,14 +174,14 @@ static bool isDereferenceableAndAlignedPointer(
   // information for values that cannot be freed in the function.
   // TODO: More precisely check if the pointer can be freed between assumption
   // and use.
-  if (CtxI && !V->canBeFreed()) {
+  if (CtxI && AC && !V->canBeFreed()) {
     /// Look through assumes to see if both dereferencability and alignment can
     /// be proven by an assume if needed.
     RetainedKnowledge AlignRK;
     RetainedKnowledge DerefRK;
     bool IsAligned = V->getPointerAlignment(DL) >= Alignment;
     if (getKnowledgeForValue(
-            V, {Attribute::Dereferenceable, Attribute::Alignment}, AC,
+            V, {Attribute::Dereferenceable, Attribute::Alignment}, *AC,
             [&](RetainedKnowledge RK, Instruction *Assume, auto) {
               if (!isValidAssumeForContext(Assume, CtxI, DT))
                 return false;
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index ba6da4dd6d614..3d403531cea2f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8010,7 +8010,7 @@ static bool isGuaranteedNotToBeUndefOrPoison(
       Dominator = Dominator->getIDom();
     }
 
-  if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
+  if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
     return true;
 
   return false;
diff --git a/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp b/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp
index 78b9c7d06e183..7210fee9c7b7c 100644
--- a/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp
+++ b/llvm/lib/Transforms/Utils/AssumeBundleBuilder.cpp
@@ -114,12 +114,12 @@ struct AssumeBuilderState {
       : M(M), InstBeingModified(I), AC(AC), DT(DT) {}
 
   bool tryToPreserveWithoutAddingAssume(RetainedKnowledge RK) {
-    if (!InstBeingModified || !RK.WasOn)
+    if (!InstBeingModified || !RK.WasOn || !AC)
       return false;
     bool HasBeenPreserved = false;
     Use* ToUpdate = nullptr;
     getKnowledgeForValue(
-        RK.WasOn, {RK.AttrKind}, AC,
+        RK.WasOn, {RK.AttrKind}, *AC,
         [&](RetainedKnowledge RKOther, Instruction *Assume,
             const CallInst::BundleOpInfo *Bundle) {
           if (!isValidAssumeForContext(Assume, InstBeingModified, DT))

@arsenm arsenm marked this pull request as ready for review May 9, 2025 09:31
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@Ralender Ralender left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor Author

arsenm commented May 9, 2025

Merge activity

  • May 9, 6:22 AM EDT: A user started a stack merge that includes this pull request via Graphite.
  • May 9, 6:23 AM EDT: @arsenm merged this pull request with Graphite.

@arsenm arsenm merged commit 89d13f8 into main May 9, 2025
14 of 15 checks passed
@arsenm arsenm deleted the users/arsenm/analysis/remove-no-assumption-cache-path-getKnowledgeForValue branch May 9, 2025 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants