[llvm-bugs] [Bug 50744] New: LICM does not strip parameter attributes when hoisting calls

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jun 16 12:10:28 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50744

            Bug ID: 50744
           Summary: LICM does not strip parameter attributes when hoisting
                    calls
           Product: tools
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: opt
          Assignee: unassignedbugs at nondot.org
          Reporter: anna at azul.com
                CC: llvm-bugs at lists.llvm.org

While we strip metadata as we hoist instructions in LICM, we do not strip
attributes that may no longer be valid to the context the call is hoisted to.

Given this IR: 
cat input.ll
```
declare i32 @cc(i32* %p) argmemonly readonly nounwind

declare i32 @spec(i32* %p) readonly argmemonly nounwind speculatable

define void @test_load(i32* noalias %loc, i32* noalias %sink, i32* %q) {
entry:
  br label %loop

loop:
  %iv = phi i32 [0, %entry], [%iv.next, %isnull ]
  %ret = call i32 @cc(i32* %loc)
  %nullchk = icmp eq i32* %q, null
  br i1 %nullchk, label %isnull, label %nonnullbb

nonnullbb:  
  %ret2 = call i32 @spec(i32* nonnull %q) 
  br label %isnull

isnull:  
  store volatile i32 %ret, i32* %sink
  %iv.next = add i32 %iv, 1
  %cmp = icmp slt i32 %iv, 200 
  br i1 %cmp, label %loop, label %exit

exit:
  ret void
}
```


We hoist the @spec call into preheader. However, the `nonnull` attribute on the
parameter is no longer valid at the location it is hoisted to.


Reproducer:
 opt -aa-pipeline=basic-aa
-passes='require<aa>,require<targetir>,require<scalar-evolution>,require<opt-remark-emit>,loop(licm)'
< input.ll -S

output:
```
define void @test_load(i32* noalias %loc, i32* noalias %sink, i32* %q) {
entry:
  %ret = call i32 @load(i32* %loc)
  %nullchk = icmp eq i32* %q, null
  %ret2 = call i32 @spec(i32* nonnull %q)
  br label %loop

loop:                                             ; preds = %isnull, %entry
  %iv = phi i32 [ 0, %entry ], [ %iv.next, %isnull ]
  br i1 %nullchk, label %isnull, label %nonnullbb

nonnullbb:                                        ; preds = %loop
  br label %isnull

isnull:                                           ; preds = %nonnullbb, %loop
  store volatile i32 %ret, i32* %sink, align 4
  %iv.next = add i32 %iv, 1
  %cmp = icmp slt i32 %iv, 200
  br i1 %cmp, label %loop, label %exit

exit:                                             ; preds = %isnull
  ret void
}
```


Note that since `@spec` call is speculatable, we can speculatively run it in
preheader. However, we should strip such attributes which are present only at
callsite. One way to do this is to whitelist attributes that should be stripped
when hoisting such calls.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210616/e85dd8ea/attachment.html>


More information about the llvm-bugs mailing list