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

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


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

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.

>From 4e0f02cd2c1b6cc622b4cacc7a523cece9d8ee88 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 8 Jun 2026 11:58:24 +0200
Subject: [PATCH] [ArgPromotion] Pass context instruction to dereferenceability
 check

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.
---
 llvm/lib/Transforms/IPO/ArgumentPromotion.cpp   | 5 +++--
 llvm/test/Transforms/ArgumentPromotion/align.ll | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)

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



More information about the llvm-commits mailing list