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

regression: @divExact no longer emits *div exact LLVM IR #19527

Closed
xdBronch opened this issue Apr 3, 2024 · 3 comments · Fixed by #19547
Closed

regression: @divExact no longer emits *div exact LLVM IR #19527

xdBronch opened this issue Apr 3, 2024 · 3 comments · Fixed by #19547
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@xdBronch
Copy link
Contributor

xdBronch commented Apr 3, 2024

Zig Version

0.12.0-dev.3518+d2be725e4

Steps to Reproduce and Observed Behavior

export fn div(a: i32) i32 {
    return @divExact(a, 2);
}

https://godbolt.org/z/zEhYTY1h9
because of @divExacts semantics with safety disabled this should be turned into a shift which happened until 0.12 where it emits %1 = sdiv i32 %0, 2 LLVM IR rather than %1 = ashr exact i32 %0, 1

Expected Behavior

zig should emit a right shift to llvm in unsafe modes

@xdBronch xdBronch added the bug Observed behavior contradicts documented or intended behavior label Apr 3, 2024
@xdBronch
Copy link
Contributor Author

xdBronch commented Apr 3, 2024

caused by #19031

@xdBronch
Copy link
Contributor Author

xdBronch commented Apr 3, 2024

actually this isnt exclusive to powers of 2, https://godbolt.org/z/9czo197Mh other denominators used to emit sdiv exact now its just sdiv

@xdBronch xdBronch changed the title regression: missed optimization on divExact with signed int and power of two denominator regression: @divExact no longer emits *div exact LLVM IR Apr 3, 2024
@Vexu Vexu added the backend-llvm The LLVM backend outputs an LLVM IR Module. label Apr 4, 2024
@Vexu Vexu added this to the 0.13.0 milestone Apr 4, 2024
@Vexu Vexu added the regression It worked in a previous version of Zig, but stopped working. label Apr 4, 2024
@xdBronch
Copy link
Contributor Author

xdBronch commented Apr 5, 2024

im investigating this a little more and im a bit confused. the ir from -femit-llvm-ir gives sdiv while the ir from --verbose-llvm-ir gives sdiv exact which i believe is somewhat expected since the latter is post optimizations? i dont fully understand when/how this is optimized, is it zig or an earlier stage in llvm? whats confusing me is that manually running them thru llc, the output from --verbose-llvm-ir turns this into a shift as you'd expect, so why isn't that llvm ir getting to llvm during the normal compilation process? is that the underlying cause of the bug or am i misunderstanding something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior regression It worked in a previous version of Zig, but stopped working.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants