[PATCH] D62989: [Unroll] Do NOT unroll a loop with small runtime upperbound

Z. Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 16:01:17 PDT 2019


zzheng updated this revision to Diff 203636.
zzheng added a comment.

  define dso_local void @hoge_3(i8 %arg) {
  entry:
    %x = load i32, i32* @global, align 4
    %y = load i8*, i8** @global.1, align 4
    %0 = icmp ult i32 %x, 17
    br i1 %0, label %loop, label %exit
  
  loop:
    %iv = phi i32 [ %x, %entry ], [ %iv.next, %loop ]
    %ptr = phi i8* [ %y, %entry ], [ %ptr.next, %loop ]
    %iv.next = add nuw i32 %iv, 8
    %ptr.next = getelementptr inbounds i8, i8* %ptr, i32 1
    store i8 %arg, i8* %ptr.next, align 1
    %1 = icmp ult i32 %iv.next, 17
    br i1 %1, label %loop, label %exit
  
  exit:
    ret void
  }

This loop has a runtime trip count upper bound of 3.

It's NOT guaranteed this loop will run either 0 or 3 iterations, so computeUnrollCount() chooses not to full-unroll it since icmp and br in each duplicated loop body cannot be removed.

computeUnrollCount() then decides to unroll it by 8 (runtime unrolling). But the unrolled loop will never execute, wasting code size, and runtime check.

If user/TTI really want it unrolled, we need to limit unroll factor by the upper bound.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D62989/new/

https://reviews.llvm.org/D62989

Files:
  include/llvm/Transforms/Utils/UnrollLoop.h
  lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp
  lib/Transforms/Scalar/LoopUnrollPass.cpp
  test/Transforms/LoopUnroll/runtime-small-upperbound.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62989.203636.patch
Type: text/x-patch
Size: 9765 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190607/11691e35/attachment.bin>


More information about the llvm-commits mailing list