-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[mlir][arith] arith.floordivsi -(2^n -1), -1
generates wrong code with UB
#83079
Comments
arith.floordivsi -(2^n -1), -1
generates code that crashesarith.floordivsi -(2^n -1), -1
generates (wrong) code that crashes
A more streamlined test is as follows:
with command:
git commit: |
@lipracer the bug only seems to occur when the functions aren't inlined. Otherwise, the erroneous code is never generated, as the Here's the godbolt for reproduction: https://godbolt.org/z/59xrxY5oz I don't know how to execute |
I think crashing is a normal behavior because
I will fix this fold.Thanks. |
arith.floordivsi -(2^n -1), -1
generates (wrong) code that crashesarith.floordivsi -(2^n -1), -1
should not fold
@lipracer I believe the correct behaviour isn't a crash, and the constant folder is correct here. The valid value range for The original code computes Looking at the compiled code, // floordiv a bfloordiv
%a, %b = call @func1() : () -> (i64, i64)
%1 = arith.constant 1 : i64
%0 = arith.constant 0 : i64
%n1 = arith.constant -1 : i64
%bLt0 = arith.cmpi slt, %b, %0 : i64
%c = arith.select %bLt0, %1, %n1 : i64 // c == 1
%a-c = arith.subi %c, %a : i64 // a - c == a - 1 == -2^n
%error = arith.divsi %3, %b : i64 // (a-c)/b == -2^n / -1 ----- UB: OVERFLOW
// ... more (variables renamed for readability), and it's apparent when |
@pingshiyu You are right, there is indeed an issue with the expand here. I will add it later in the PR, but there is also an issue with the folding here. |
thank you! appreciate your help :) |
arith.floordivsi -(2^n -1), -1
should not foldarith.floordivsi -(2^n -1), -1
generates wrong code with UB
1) fix floordivsi error expand logic 2) fix floordivsi fold did't check overflow stat Fixs llvm#83079
1) fix floordivsi error expand logic 2) fix floordivsi fold did't check overflow stat Fixs llvm#83079
1) fix floordivsi error expand logic 2) fix floordivsi fold did't check overflow stat Fixs llvm#83079
1) fix floordivsi error expand logic 2) fix floordivsi fold did't check overflow stat Fixs llvm#83079
The code below tries to calculate:
arith.floordivsi (1 - 2^63), -1
, which should give a well defined result equalling2^63 - 1
(as far as I can tell from the documentation). But instead generates code that crashes when compiled with:mlir-opt --arith-expand --test-lower-to-llvm out.mlir | mlir-cpu-runner -e main --shared-libs ~/lib/mlir_c_runner --entry-point-result void
The text was updated successfully, but these errors were encountered: