llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Assertion `Denominator > 0 && "Denominator cannot be 0!"' failed.

Open danilaml opened this issue 3 years ago • 9 comments

Looks like LoopPredication doesn't properly handle the case where all of branch weights are zero. Run with opt -loop-predication-skip-profitability-checks=false -passes='require<scalar-evolution>,loop-mssa(loop-predication)'

target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nocallback nofree nosync willreturn
declare void @llvm.experimental.guard(i1, ...) #0

define void @foo() {
entry:
  br label %Header

Header:                                           ; preds = %Latch, %entry
  %j2 = phi i64 [ 0, %entry ], [ %j.next, %Latch ]
  call void (i1, ...) @llvm.experimental.guard(i1 false, i32 0) [ "deopt"() ]
  %j.next = add i64 %j2, 1
  br i1 false, label %Latch, label %exit

Latch:                                            ; preds = %Header
  %speculate_trip_count = icmp ult i64 %j2, 0
  br i1 %speculate_trip_count, label %Header, label %common.ret, !prof !0

common.ret:                                       ; preds = %exit, %Latch
  ret void

exit:                                             ; preds = %Header
  br label %common.ret
}

attributes #0 = { nocallback nofree nosync willreturn }

!0 = !{!"branch_weights", i32 0, i32 0}

https://godbolt.org/z/oWGjWTaTY

danilaml avatar Sep 14 '23 14:09 danilaml

@dexonsmith just to confirm - all-zero branch weights are perfectly legal construct, right? They don't make much sense, but if you want to attach branch weights metadata to the basic block that hasn't been executed, I guess they can come up.

danilaml avatar Sep 14 '23 15:09 danilaml

@dexonsmith just to confirm - all-zero branch weights are perfectly legal construct, right? They don't make much sense, but if you want to attach branch weights metadata to the basic block that hasn't been executed, I guess they can come up.

I don't think that's expected, no. If a basic block hasn't been executed, then no branch weights should be attached, so that BranchProbabilityInfo can make a guess.

Probably worth adding an IR verifier check.

dexonsmith avatar Sep 14 '23 15:09 dexonsmith

@dexonsmith BPI appears to handle zero branch weight case here: https://github.com/llvm/llvm-project/blob/74724902ba2fc0dc2734cdb5fae15cb338f1b635/llvm/lib/Analysis/BranchProbabilityInfo.cpp#L430-L434

Seems to be and old concern: https://github.com/llvm/llvm-project/commit/6a2c71af0bc5beebf954e1970233fbeb3be0bf1c

danilaml avatar Sep 14 '23 15:09 danilaml

@dexonsmith BPI appears to handle zero branch weight case here: https://github.com/llvm/llvm-project/blob/74724902ba2fc0dc2734cdb5fae15cb338f1b635/llvm/lib/Analysis/BranchProbabilityInfo.cpp#L430-L434

Seems to be and old concern: https://github.com/llvm/llvm-project/commit/6a2c71af0bc5beebf954e1970233fbeb3be0bf1c

Error: Command failed due to missing milestone.

llvmbot avatar Sep 14 '23 15:09 llvmbot

@dexonsmith BPI appears to handle zero branch weight case here:

https://github.com/llvm/llvm-project/blob/74724902ba2fc0dc2734cdb5fae15cb338f1b635/llvm/lib/Analysis/BranchProbabilityInfo.cpp#L430-L434

Seems to be and old concern: 6a2c71a

Perhaps that commit wasn't implemented fully, then! Seems like a bug.

dexonsmith avatar Sep 14 '23 15:09 dexonsmith

@dexonsmith BPI appears to handle zero branch weight case here:

https://github.com/llvm/llvm-project/blob/74724902ba2fc0dc2734cdb5fae15cb338f1b635/llvm/lib/Analysis/BranchProbabilityInfo.cpp#L430-L434

Seems to be and old concern: 6a2c71a

Perhaps that commit wasn't implemented fully, then! Seems like a bug.

Error: Command failed due to missing milestone.

llvmbot avatar Sep 14 '23 15:09 llvmbot

@dexonsmith not sure what you mean by commit not being fully implemented. And what exactly do you count as a bug here? It looks like BPI was handling the case of zero branch weight since at least 2015. I was planning to update another pass that didn't handle this case assuming that zero branch weights are legal (if unexpected). As such, I hesitate to declare them illegal, since it's unclear how many places can produce such weights.

danilaml avatar Sep 14 '23 16:09 danilaml

It looks like BPI was handling the case of zero branch weight since at least 2015. I was planning to update another pass that didn't handle this case assuming that zero branch weights are legal (if unexpected). As such, I hesitate to declare them illegal, since it's unclear how many places can produce such weights.

I agree; your plan sounds good to me.

dexonsmith avatar Sep 14 '23 16:09 dexonsmith

@dexonsmith BPI appears to handle zero branch weight case here:

https://github.com/llvm/llvm-project/blob/74724902ba2fc0dc2734cdb5fae15cb338f1b635/llvm/lib/Analysis/BranchProbabilityInfo.cpp#L430-L434

Seems to be and old concern: 6a2c71a

Perhaps that commit wasn't implemented fully, then! Seems like a bug.

Error: Command failed due to missing milestone.

llvmbot avatar Sep 14 '23 16:09 llvmbot