Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
danilaml opened this issue Sep 14, 2023 · 9 comments · Fixed by #66506
Closed

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

danilaml opened this issue Sep 14, 2023 · 9 comments · Fixed by #66506
Assignees
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] loopoptim

Comments

@danilaml
Copy link
Collaborator

danilaml commented Sep 14, 2023

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 danilaml added loopoptim crash Prefer [crash-on-valid] or [crash-on-invalid] labels Sep 14, 2023
@danilaml danilaml self-assigned this Sep 14, 2023
@danilaml
Copy link
Collaborator Author

@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.

@dexonsmith
Copy link
Collaborator

@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.

@danilaml
Copy link
Collaborator Author

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

if (WeightSum == 0 || ReachableIdxs.size() == 0) {
for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
Weights[I] = 1;
WeightSum = TI->getNumSuccessors();
}

Seems to be and old concern: 6a2c71a

@llvmbot
Copy link
Member

llvmbot commented Sep 14, 2023

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

if (WeightSum == 0 || ReachableIdxs.size() == 0) {
for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
Weights[I] = 1;
WeightSum = TI->getNumSuccessors();
}

Seems to be and old concern: 6a2c71a

Error: Command failed due to missing milestone.

@dexonsmith
Copy link
Collaborator

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

if (WeightSum == 0 || ReachableIdxs.size() == 0) {
for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
Weights[I] = 1;
WeightSum = TI->getNumSuccessors();
}

Seems to be and old concern: 6a2c71a

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

@llvmbot
Copy link
Member

llvmbot commented Sep 14, 2023

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

if (WeightSum == 0 || ReachableIdxs.size() == 0) {
for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
Weights[I] = 1;
WeightSum = TI->getNumSuccessors();
}

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.

@danilaml
Copy link
Collaborator Author

@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.

@dexonsmith
Copy link
Collaborator

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.

@llvmbot
Copy link
Member

llvmbot commented Sep 14, 2023

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

if (WeightSum == 0 || ReachableIdxs.size() == 0) {
for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I)
Weights[I] = 1;
WeightSum = TI->getNumSuccessors();
}

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.

danilaml added a commit that referenced this issue Sep 14, 2023
@Endilll Endilll removed the new issue label Sep 15, 2023
danilaml added a commit to danilaml/llvm-project that referenced this issue Sep 18, 2023
Treat the case where all branch weights are zero as if there was no profile.
Fixes llvm#66382
danilaml added a commit that referenced this issue Sep 19, 2023
…#66506)

Treat the case where all branch weights are zero as if there was no
profile.
Fixes #66382
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this issue Sep 19, 2023
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this issue Sep 19, 2023
…llvm#66506)

Treat the case where all branch weights are zero as if there was no
profile.
Fixes llvm#66382
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] loopoptim
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants