You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Incorrect optimization for tir.floordiv/floormod #18684
The direct cause is incorrect optimization of TIR by the LowerIntrin pass.
after LowerIntrin pass, tir became: T.Cast("int64", T.Div(n - 2147483631, 10) - -214748364)
This is obviously wrong.
In src/tir/transforms/lower_intrin.cc:156 PrimExpr ceildiv = truncdiv(-min + (op->b - 1), op->b);
In my test case, min is -2147483648+9.
b-1 is 9. -min + (op->b - 1) = 2147483648, after const fold in src/arith/const_fold.h:80, the return value is -2147483648, because 2147483648 exceeds the upper limit of int32.
This resulted in a non-equivalent transformation.
Possible fix method
To avoid overflow of '(op->b - 1) - min' , we can assert (b-1-a_min) < MAX_VALUE+1.
That is (b_max-a_min) < MAX_VALUE + 2.
We can avoid this issue by changing the condition of the if statement in L122.
Of course, this may not be the best fix.
Because the floormod node uses similar optimization,it also has the same problem, in src/tir/transforms/lower_intrin.cc:217.
Activity
tqchen commentedon Jan 27, 2026
thanks @guocju yourp proposed fix by adding check looks good to me. can you send a PR with regression test (for both floordiv and floormod?
cc @spectrometerHBH @tlopex @Kathryn-cat
[BugFix][TIR] Fix incorrect optimization when lowering floordiv and f…
[BugFix][TIR] Fix incorrect optimization when lowering floordiv and f…
[BugFix][TIR] Fix incorrect optimization when lowering floordiv and f… (
tqchen commentedon Jan 30, 2026
#18699 contains some followups to further robustify the scenario
[BugFix][TIR] Fix incorrect optimization when lowering floordiv and f… (
[BugFix][TIR] Fix incorrect optimization when lowering floordiv and f… (