[llvm] [ArgPromotion] Pass context instruction to dereferenceability check (PR #202305)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 03:01:00 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

This is necessary for argument promotion to work under deref-at-point semantics. For the primary case, specify the context instruction as the first instruction in the function, which should effectively disable the check for frees.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/IPO/ArgumentPromotion.cpp (+3-2) 
- (modified) llvm/test/Transforms/ArgumentPromotion/align.ll (+1) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
index cfbf9dec01619..6ef7323364907 100644
--- a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
+++ b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
@@ -456,7 +456,8 @@ static bool allCallersPassValidPointerForArgument(
   APInt Bytes(64, NeededDerefBytes);
 
   // Check if the argument itself is marked dereferenceable and aligned.
-  if (isDereferenceableAndAlignedPointer(Arg, NeededAlign, Bytes, DL))
+  if (isDereferenceableAndAlignedPointer(Arg, NeededAlign, Bytes, DL,
+                                         &Callee->getEntryBlock().front()))
     return true;
 
   // Look at all call sites of the function.  At this point we know we only have
@@ -491,7 +492,7 @@ static bool allCallersPassValidPointerForArgument(
       return true;
 
     return isDereferenceableAndAlignedPointer(CB.getArgOperand(Arg->getArgNo()),
-                                              NeededAlign, Bytes, DL);
+                                              NeededAlign, Bytes, DL, &CB);
   });
 }
 
diff --git a/llvm/test/Transforms/ArgumentPromotion/align.ll b/llvm/test/Transforms/ArgumentPromotion/align.ll
index 656c7c9da5b4a..62dd1dcc478c4 100644
--- a/llvm/test/Transforms/ArgumentPromotion/align.ll
+++ b/llvm/test/Transforms/ArgumentPromotion/align.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --scrub-attributes
 ; RUN: opt -S -passes=argpromotion < %s | FileCheck %s
+; RUN: opt -S -passes=argpromotion -use-dereferenceable-at-point-semantics < %s | FileCheck %s
 
 define internal i32 @callee_must_exec(ptr %p) {
 ; CHECK-LABEL: define {{[^@]+}}@callee_must_exec

``````````

</details>


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


More information about the llvm-commits mailing list