[llvm] [LoopPredication] Fix division by zero in case of zero branch weights (PR #66506)

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 07:45:52 PDT 2023


================
@@ -1,12 +1,31 @@
-; XFAIL: *
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
 ; RUN: opt -S -loop-predication-skip-profitability-checks=false -passes='require<scalar-evolution>,loop-mssa(loop-predication)' %s | FileCheck %s
 
 target triple = "x86_64-unknown-linux-gnu"
 
 ; Function Attrs: nocallback nofree nosync willreturn
 declare void @llvm.experimental.guard(i1, ...) #0
 
+; Check that LoopPredication doesn't crash on all-zero branch weights
 define void @foo() {
+; CHECK-LABEL: define void @foo() {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[HEADER:%.*]]
+; CHECK:       Header:
+; CHECK-NEXT:    [[J2:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[J_NEXT:%.*]], [[LATCH:%.*]] ]
+; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 false, i32 0) [ "deopt"() ]
+; CHECK-NEXT:    [[J_NEXT]] = add i64 [[J2]], 1
+; CHECK-NEXT:    br i1 false, label [[LATCH]], label [[EXIT:%.*]]
+; CHECK:       Latch:
+; CHECK-NEXT:    [[SPECULATE_TRIP_COUNT:%.*]] = icmp ult i64 [[J2]], 0
+; CHECK-NEXT:    br i1 [[SPECULATE_TRIP_COUNT]], label [[HEADER]], label [[COMMON_RET_LOOPEXIT:%.*]], !prof [[PROF0:![0-9]+]]
+; CHECK:       common.ret.loopexit:
+; CHECK-NEXT:    br label [[COMMON_RET:%.*]]
+; CHECK:       common.ret:
+; CHECK-NEXT:    ret void
+; CHECK:       exit:
+; CHECK-NEXT:    br label [[COMMON_RET]]
+;
----------------
dexonsmith wrote:

It's not clear to me if this is checking anything relevant about the expected output of the pass, in the context of the interpretation of the branch weights as an even distribution. If it is, can you explain?

If not, is there a way to do that? Maybe you can observe somehow that the branch weights are correctly interpreted as "even" by looking at DEBUG output (the `DEBUG_TYPE` for this pass is `loop-predication`), or maybe STATISTIC?

For example, I see that there's a command-line option `-loop-predication-latch-probability-scale`, which is a scaling factor applied to the latch probability. This affects how the branch weights are used, ultimately changing the return of `LoopPredication::isLoopProfitableToPredicate`. Can you construct a test case where, if the branch probability is even (the correct interpretation of `branch_weights` of 0), then two RUN lines with different `-loop-predication-latch-probability-scale` will give you different output for STATISTIC and/or DEBUG? If so, then we could have see CHECK lines on the STATISTIC/DEBUG output.

https://github.com/llvm/llvm-project/pull/66506


More information about the llvm-commits mailing list