[llvm] [ValueTracking] Conservative NoSync check prevents vectorization (PR #199002)
Kshitij Paranjape via llvm-commits
llvm-commits at lists.llvm.org
Thu May 21 04:09:48 PDT 2026
https://github.com/kshitijvp created https://github.com/llvm/llvm-project/pull/199002
Conservative nosync check in ValueTracking.cpp returns false causing potentially faulting load preventing vectorization. Instead check if any instructions between Assume Instruction and Ctx Instruction are synchronizing.
>From a45e23bbab455d49a1fd40234f89130fe4191a1f Mon Sep 17 00:00:00 2001
From: Kshitij Paranjape <kshitijvparanjape at gmail.com>
Date: Thu, 21 May 2026 16:22:20 +0530
Subject: [PATCH] [ValueTracking] Conservative NoSync check prevents
vectorization
Conservative nosync check in ValueTracking.cpp returns false
causing potentially faulting load preventing vectorization.
Instead check if any instructions between Assume Instruction
and Ctx Instruction are synchronizing.
---
llvm/include/llvm/Analysis/ValueTracking.h | 2 +-
llvm/lib/Analysis/ValueTracking.cpp | 22 +++++++++++++++++-----
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index b2f664a9c9c0d..79ff896779ce2 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -639,7 +639,7 @@ inline bool isValidAssumeForContext(const Instruction *I,
}
/// Returns true, if no instruction between \p Assume and \p CtxI may free
-/// memory and the function is marked as NoSync. The latter ensures the current
+/// or synchronize memory. The latter ensures the current
/// function cannot arrange for another thread to free on its behalf.
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume,
const Instruction *CtxI);
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 3be0db5ad9c7e..b59bed0cc7f23 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -703,14 +703,26 @@ bool llvm::isValidAssumeForContext(const Instruction *Inv,
bool llvm::willNotFreeBetween(const Instruction *Assume,
const Instruction *CtxI) {
- // Helper to check if there are any calls in the range that may free memory.
+ // Helper to make sure the current function cannot arrange for
+ // another thread to free on its behalf and to check if there
+ // are any calls in the range that may free memory.
auto hasNoFreeCalls = [](auto Range) {
for (const auto &[Idx, I] : enumerate(Range)) {
if (Idx > MaxInstrsToCheckForFree)
return false;
- if (const auto *CB = dyn_cast<CallBase>(&I))
- if (!CB->hasFnAttr(Attribute::NoFree))
- return false;
+ if (I.isVolatile())
+ return false;
+
+ if (I.maySynchronize())
+ return false;
+
+ auto *CB = dyn_cast<CallBase>(&I);
+ if (CB) {
+ // Non call site cases covered by the two checks above
+ if (!CB->hasFnAttr(Attribute::NoSync) ||
+ !CB->hasFnAttr(Attribute::NoFree))
+ return false;
+ }
}
return true;
};
@@ -739,7 +751,7 @@ bool llvm::willNotFreeBetween(const Instruction *Assume,
}
// Check if there are any calls between Assume and CtxIter that may free
- // memory.
+ // memory or synchronize.
return hasNoFreeCalls(make_range(Assume->getIterator(), CtxIter));
}
More information about the llvm-commits
mailing list