[DSE] Don't use initializes on byval argument (#126259)

There are two ways we can fix this problem, depending on how the
semantics of byval and initializes should interact:

* Don't infer initializes on byval arguments. initializes on byval
refers to the original caller memory (or having both attributes is made
a verifier error).
* Infer initializes on byval, but don't use it in DSE. initializes on
byval refers to the callee copy. This matches the semantics of readonly
on byval. This is slightly more powerful, for example, we could do a
backend optimization where byval + initializes will allocate the full
size of byval on the stack but not copy over the parts covered by
initializes.

I went with the second variant here, skipping byval + initializes in DSE
(FunctionAttrs already doesn't propagate initializes past byval). I'm
open to going in the other direction though.

Fixes https://github.com/llvm/llvm-project/issues/126181.
This commit is contained in:
Nikita Popov 2025-02-10 10:34:03 +01:00 committed by GitHub
parent 317a644ae6
commit 2d31a12dbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 1 deletions

View File

@ -1707,6 +1707,10 @@ Currently, only the following parameter attributes are defined:
and negative values are allowed in case the argument points partway into
an allocation. An empty list is not allowed.
On a ``byval`` argument, ``initializes`` refers to the given parts of the
callee copy being overwritten. A ``byval`` callee can never initialize the
original caller memory passed to the ``byval`` argument.
``dead_on_unwind``
At a high level, this attribute indicates that the pointer argument is dead
if the call unwinds, in the sense that the caller will not depend on the

View File

@ -2283,7 +2283,9 @@ DSEState::getInitializesArgMemLoc(const Instruction *I) {
for (unsigned Idx = 0, Count = CB->arg_size(); Idx < Count; ++Idx) {
ConstantRangeList Inits;
Attribute InitializesAttr = CB->getParamAttr(Idx, Attribute::Initializes);
if (InitializesAttr.isValid())
// initializes on byval arguments refers to the callee copy, not the
// original memory the caller passed in.
if (InitializesAttr.isValid() && !CB->isByValArgument(Idx))
Inits = InitializesAttr.getValueAsConstantRangeList();
Value *CurArg = CB->getArgOperand(Idx);

View File

@ -338,3 +338,17 @@ define i16 @global_var_alias() {
ret i16 %l
}
declare void @byval_fn(ptr byval(i32) initializes((0, 4)) %am)
define void @test_byval() {
; CHECK-LABEL: @test_byval(
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: store i32 0, ptr [[A]], align 4
; CHECK-NEXT: call void @byval_fn(ptr [[A]])
; CHECK-NEXT: ret void
;
%a = alloca i32
store i32 0, ptr %a
call void @byval_fn(ptr %a)
ret void
}