-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Cranelift: handle carry flags as bools in backends #2860
Comments
using this as an excuse to (finally) familiarize myself with the MachInst backend, i'm not sure why and if that's so, i'd expect |
Greetings @Hypersonic and thanks for the report! The plan we made when starting to design the new backend was to phase out add+carry (and sub+borrow) opcodes in the CLIF, instead directly providing the wider adds/subs that one would normally lower into carry-using ops. In the legalization-based old backend, The reason for this design choice was that representing carry flags as normal values is fragile performance-wise: to get the correct (fast) machine code, which implicitly passes the bool in the carry flag, one has to pattern-match on the two instructions. The case where the Given that we just recently switched the default backend and haven't started the work of removing the old one, we haven't yet cleaned up the opcode space, but we intend to do so eventually. That does leave the question: what if one actually wants to generate an add-with-carry? I'm definitely open to understanding use-cases better. Are you hoping to take an arbitrary |
One use it to check for overflow to crash. In cg_clif I currently have to manually check if an overflow has happened even though at least on x86 the processor already provides this information in the form of a flag. |
@bjorn3 there is the |
My main use case is detecting the carry-out -- I don't really have any particular need for the carry-in, but this was the instruction I found that would give me the output I needed. If there's a different instruction that can give me that, I'm more than happy to use it. |
@Hypersonic the |
I think I'm covered by |
I'm not sure if this issues should be re-opened or I should make a new one, but Example Cranelift IR that doesn't compile:
(Ignore the weird block0) Changing it to use There was a comment asking about what contexts would need this: I'm writing an x86_64 recompilation/partial application engine using Cranelift as a lifting target. That Cranelift snippet is what I am generating for
or the compiled assembly function of
Notably, I don't have access to the un-optimized assembly code that is more "natural" and doesn't depend on the CF behavior. I've had several other issues with Cranelift IFLAGS (and the assumption that all ALU operations clobber all flags means this project is probably dead in the water), but this issue (or a similar one) should probably be re-opened to let future users know. |
Hi @chc4 -- we can definitely talk about how to support this use-case; I'll re-open the issue. (As a general principle, we want Cranelift to be as generally applicable as is practical; so "here's my use-case and I can't figure out how to express it in CLIF" is always a valid issue!) We've been discussing recently how to clean up the IR (see e.g. #3205 for discussion of booleans, which overlaps somewhat with the discussion of |
I'm still not sure it would fit my use-case, since I need correct implementation of all other processor flags, not just CF. Using a B1 and codegen'ing it back to iflags at instruction selection would work for CF, but I'd probably also need code motion and disjoint flag tracking on the selection backend so that e.g. If it is usable that would be great, but I assume I would either have to wait for the iflags design to be stabilized further or write optimization and selection passes myself, which might be a bit too much work for a side-project. My current thought is to switch to dynasm-rs so I can lift instructions to the same flags behavior (and re-implement my own IR and register allocation...) We can continue this discussion on Zulip to avoid clogging this issue further. |
For others who want to follow along: link |
Is part of this fixed with #5784 ? |
I'm not entirely sure what this issue is about at this point, so it's hard to say whether it's fixed. 😅 If it's about addition with carry not having a usable lowering on x86, then I think that was fixed either by #5784 or earlier work. Cranelift has changed a lot since this issue was opened so it's hard to even compare. If I consider only the issue title ("handle carry flags as bools in backends") then I think this was fixed by #5406 and earlier work. For chc4's use case of encoding the behavior of x86 flags into CLIF, I think we've gone the opposite direction since we've removed the notion of flags from the IR entirely. (Again, in #5406 and earlier.) It's always possible to encode values equivalent to x86's flags in integer values and carry those around, though we may not generate particularly efficient code from that encoding. Regardless of which interpretation we consider, I think this issue is no longer serving a useful purpose. So I'm going to close it. I'd suggest that anyone who encounters issues which sound like this should open a new issue, explaining exactly what you're seeing. Anything that comes up now is probably not related to the difficulties described in this issue. |
Hi! While using Cranelift as a backend for a project, I ran into a panic compiling iadd_carry instructions on x86_64. Please let me know if there is any other information I can provide that would be helpful.
.clif
Test CaseSteps to Reproduce
cargo run -p cranelift-tools -- compile test.clif --target x86_64
Expected Results
Code successfully generates without a panic.
Actual Results
Panic:
thread 'main' panicked at 'ALU+imm and ALU+carry ops should not appear here!', cranelift/codegen/src/isa/x64/lower.rs:5740:13
Versions and Environment
Cranelift version or commit: main HEAD (4a830b at time of writing)
Operating system: Linux
Architecture: x86_64
The text was updated successfully, but these errors were encountered: