[PATCH] D104636: [LoopIdiom] [LoopNest] let the pass deal with runtime memset size

Yueh-Ting Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 23 09:38:21 PDT 2021


eopXD added a comment.

>> Assume we don't need runtime check for size overflow, we still need runtime checks for the assumptions made to fold SCEV expressions that enable the optimization. So versioning would still be needed.
>
> Why can't we use the SCEV expression for the trip count as-is?

The SCEV of trip count may contain `smax` which would make the memset size not comparable in `processLoopMemset`. To conservatively fold the `smax` expression a runtime check is added.

In a do-while loop of the following, the trip-count SCEV would be `(1 smax (sext i32 %n to i64))`. To fold the SCEV we add run-check condition for n >= 1, and fold the expression into `sext i32 %n to i64`.

  int i = 0;
  do {
    ++i;
  }  while (i < n);



---

An example would be the 2nd test case in `memset-runtime.ll`.
For the inner-loop, the optimized memset instruction would have the memsetSizeSCEV contain `smax` expression since `NumBytesSCEV = TripCountSCEV * StoreSizeSCEV`

  Calculate NumBytesSCEV = TripCountSCEV * StoreSizeSCEV
    TripCountSCEV: (1 smax (sext i32 %m to i64))
    StoreSizeSCEV: (4 * (sext i32 %o to i64))<nsw>
    NumBytesSCEV: (4 * (sext i32 %o to i64) * (1 smax (sext i32 %m to i64)))

Then in the outer-loop when the pass want to compare check whether `memsetSizeSCEV == pointerStrideSCEV` we need to fold the expression to eliminate `smax` so that the comparison can be performed. If the folded expression is successful then the pass would proceed to perform the optimization for the outer loop.

  MemsetSizeSCEV: (4 * (sext i32 %o to i64) * (1 smax (sext i32 %m to i64)))
  PositiveStrideSCEV: (4 * (sext i32 %m to i64) * (sext i32 %o to i64))
  Try to convert SCEV expression and compare again
    MemsetSCEVConv: (4 * (zext i32 %m to i64) * (zext i32 %o to i64))
    PositiveStrideSCEVConv: (4 * (zext i32 %m to i64) * (zext i32 %o to i64))


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D104636



More information about the llvm-commits mailing list