Skip to content

Commit 2d31a12

Browse files
authored
[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.
1 parent 317a644 commit 2d31a12

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
@@ -1707,6 +1707,10 @@ Currently, only the following parameter attributes are defined:
17071707
and negative values are allowed in case the argument points partway into
17081708
an allocation. An empty list is not allowed.
17091709

1710+
On a ``byval`` argument, ``initializes`` refers to the given parts of the
1711+
callee copy being overwritten. A ``byval`` callee can never initialize the
1712+
original caller memory passed to the ``byval`` argument.
1713+
17101714
``dead_on_unwind``
17111715
At a high level, this attribute indicates that the pointer argument is dead
17121716
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
@@ -2283,7 +2283,9 @@ DSEState::getInitializesArgMemLoc(const Instruction *I) {
22832283
for (unsigned Idx = 0, Count = CB->arg_size(); Idx < Count; ++Idx) {
22842284
ConstantRangeList Inits;
22852285
Attribute InitializesAttr = CB->getParamAttr(Idx, Attribute::Initializes);
2286-
if (InitializesAttr.isValid())
2286+
// initializes on byval arguments refers to the callee copy, not the
2287+
// original memory the caller passed in.
2288+
if (InitializesAttr.isValid() && !CB->isByValArgument(Idx))
22872289
Inits = InitializesAttr.getValueAsConstantRangeList();
22882290

22892291
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)