-
Notifications
You must be signed in to change notification settings - Fork 12.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
compiler_fence may emit machine code #62256
Comments
Yes, it does. |
So what is the right way in LLVM to emit a compiler barrier that definitely emits no machine code? |
clang++ emits the same LLVM instruction ( This is most likely a bug with the LLVM backends in question, rather than Rust’s lowering to LLVM. However |
I'm working on this from the LLVM side. I'll try to take a generic approach that supports both RISC-V and other targets, but if that doesn't pan out, we'll be looking at a RISC-V only patch in LLVM 12. |
Aside: I'm not sure either Rust or C++ can provide or enforce the guarantee that no instructions should be emitted. On OoO cores with relaxed memory models it is almost certain that the compiler will need to insert an instruction to tell the core to ensure all stores/loads are completed before proceeding, even if you don't need to synchronize outside of the currently executing core. While I think it is probably valid for RISC-V to not emit any instruction, the central semantic guarantee on the memory accesses is preserved by emitting the kind of |
But yes, if we were to ignore the documentation guarantee, it is not strictly incorrect from functional perspective for it to emit a more strict barrier. |
It's not clear to me that the C++ version does exclude OoO execution, even though the rust definition does, but I'm happy to follow that GCC leaves out the fence for RISC-V when compiling |
Note that this line: https://github.com/llvm/llvm-project/blob/eb75f250feb6822d57be95e8535e28724cde6e9d/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp#L3998 (found by @Lymia) means it might lower into a call to I also would consider it to be "strictly incorrect from functional perspective", since there's no guarantee that function is linked in. |
It is my assertion that we had no such contract from the codegen backend thus the contract we were forging with users was always erroneous, and that we should simply drop the promise of offering no machine code emission, even if we manage to persuade our backends to comply, as frustrating as that might be. It should be explained as the most minimal barrier possible, which should be zero machine instructions for all practical cases, not a guaranteed zero-machine-instructions barrier. |
I think it's still wrong to emit a call to a function we don't provide via compiler-builtins on that target. Admittedly looking at compiler-builtins now it's unclear why it's not provided, although ideally we wouldn't emit the call either way. |
Oh, I would absolutely agree that we shouldn't be linking in code on |
This is not a misuse, this is exactly the intended semantics -- compiler_fence is our name for C++'s atomic_signal_fence, it is explicitly and only intended to synchronize with operations that are "on the same logical thread" (i.e. in a signal handler). Arguably our name is not great, but the latest nightly docs at least should clarify that. That should AFAIK never need to emit any instructions, so indeed I would say this is an LLVM bug. |
As one of the inbound links to this issue points out, it also affects PowerPC: https://godbolt.org/z/qdK8bT68a It's possible that several LLVM targets have been overly conservative about this and none of them need to emit any codefor |
As discovered there
compiler_fence
producesatomic_fence(ordering, SingleThread)
construction, which in turn can produce a non-empty code sequence. In fact, LLVM backends for AVR, PowerPC, RISC-V and Spark do not treat SingleThread fence as something special. At the same time Rust docs tell that "compiler_fence does not emit any machine code".Seems like Rust misuses this "SingleThread means CompilerBarrier" semantics, but I could be wrong.
The text was updated successfully, but these errors were encountered: