Skip to content

Commit 94c1a8e

Browse files
nikictstellar
authored andcommitted
[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 #126181. (cherry picked from commit 2d31a12)
1 parent f33b128 commit 94c1a8e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

llvm/docs/LangRef.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,10 @@ Currently, only the following parameter attributes are defined:
17251725
and negative values are allowed in case the argument points partway into
17261726
an allocation. An empty list is not allowed.
17271727

1728+
On a ``byval`` argument, ``initializes`` refers to the given parts of the
1729+
callee copy being overwritten. A ``byval`` callee can never initialize the
1730+
original caller memory passed to the ``byval`` argument.
1731+
17281732
``dead_on_unwind``
17291733
At a high level, this attribute indicates that the pointer argument is dead
17301734
if the call unwinds, in the sense that the caller will not depend on the

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,9 @@ DSEState::getInitializesArgMemLoc(const Instruction *I) {
22812281
for (unsigned Idx = 0, Count = CB->arg_size(); Idx < Count; ++Idx) {
22822282
ConstantRangeList Inits;
22832283
Attribute InitializesAttr = CB->getParamAttr(Idx, Attribute::Initializes);
2284-
if (InitializesAttr.isValid())
2284+
// initializes on byval arguments refers to the callee copy, not the
2285+
// original memory the caller passed in.
2286+
if (InitializesAttr.isValid() && !CB->isByValArgument(Idx))
22852287
Inits = InitializesAttr.getValueAsConstantRangeList();
22862288

22872289
Value *CurArg = CB->getArgOperand(Idx);

llvm/test/Transforms/DeadStoreElimination/inter-procedural.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,17 @@ define i16 @global_var_alias() {
338338
ret i16 %l
339339
}
340340

341+
declare void @byval_fn(ptr byval(i32) initializes((0, 4)) %am)
342+
343+
define void @test_byval() {
344+
; CHECK-LABEL: @test_byval(
345+
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
346+
; CHECK-NEXT: store i32 0, ptr [[A]], align 4
347+
; CHECK-NEXT: call void @byval_fn(ptr [[A]])
348+
; CHECK-NEXT: ret void
349+
;
350+
%a = alloca i32
351+
store i32 0, ptr %a
352+
call void @byval_fn(ptr %a)
353+
ret void
354+
}

0 commit comments

Comments
 (0)