[BPF] treat compiler fence as codegen no-op#196734
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
cc @yonghong-song This PR fixes a crash, please take a look. |
|
Beyond the above rust example. The below are two C examples: or where __sync_synchronize() will generate IR 'fence seq_cst', |
4ast
left a comment
There was a problem hiding this comment.
hold on. SeqCst needs a real barrier. On x86 it's a locked stack operation.
BPF backend should do the same.
and ISD::MEMBARRIER for everything but SeqCst.
XCore is a bad analogy here, since it's single core afaik.
|
Are you suggesting generating an atomic exchange (xchg) on a stack slot for SeqCst, similar to x86? We can do that but my understanding is that the BPF stack is per invocation and private to the current program execution, so I’m not sure what it would synchronize against. |
from libarena/include/bpf_atomic.h iirc x86 does 'lock or (rsp)' which is same idea. |
ff7569d to
8fb0b19
Compare
@4ast Updated the handling for |
|
I agree with the approach, but it looks like our kernel side documentation is lacking in this regard. E.g. nothing about seq_cst in the instruction set doc. Should it be extended? |
282e5c0 to
e3a19d9
Compare
e3a19d9 to
831c610
Compare
|
@yonghong-song all feedback is addressed and all checks have passed, can we merge this? I'm also wanting to back port this to fix the crash |
|
@bidhan-a Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
The BPF backend has no instruction-selection pattern for
`ISD::ATOMIC_FENCE`, so LLVM IR containing a fence instruction crashes
with `Cannot select: AtomicFence...`.
**Rust code snippet**
```
#[unsafe(no_mangle)]
pub fn entrypoint(_input: *mut u8) -> u64 {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
0
}
```
**LLVM IR**
```
; ModuleID = 'linked_module'
source_filename = "linked_module"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpfel"
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn
define dso_local noundef i64 @entrypoint(ptr noundef readnone captures(none) %0) unnamed_addr #0 {
fence syncscope("singlethread") seq_cst
ret i64 0
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn "target-cpu"="generic" }
!llvm.ident = !{!0}
!0 = !{!"rustc version 1.97.0-nightly (f53b654a8 2026-04-30)"}
```
**Error**
```
LLVM ERROR: Cannot select: 0x7fcf7701a1c0: ch = AtomicFence 0x7fcf75718df8, TargetConstant:i64<7>, TargetConstant:i64<0>
In function: entrypoint
Stack dump:
0. Running pass 'Function Pass Manager' on module 'linked_module'.
1. Running pass 'BPF DAG->DAG Pattern Instruction Selection' on function '@entrypoint'
```
-----
**Fix**
This patch lowers single-thread (compiler) fences to `ISD::MEMBARRIER`
(no-op) and produces an error for cross-thread (runtime) fences.
The BPF backend has no instruction-selection pattern for
`ISD::ATOMIC_FENCE`, so LLVM IR containing a fence instruction crashes
with `Cannot select: AtomicFence...`.
**Rust code snippet**
```
#[unsafe(no_mangle)]
pub fn entrypoint(_input: *mut u8) -> u64 {
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
0
}
```
**LLVM IR**
```
; ModuleID = 'linked_module'
source_filename = "linked_module"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpfel"
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn
define dso_local noundef i64 @entrypoint(ptr noundef readnone captures(none) %0) unnamed_addr #0 {
fence syncscope("singlethread") seq_cst
ret i64 0
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn "target-cpu"="generic" }
!llvm.ident = !{!0}
!0 = !{!"rustc version 1.97.0-nightly (f53b654a8 2026-04-30)"}
```
**Error**
```
LLVM ERROR: Cannot select: 0x7fcf7701a1c0: ch = AtomicFence 0x7fcf75718df8, TargetConstant:i64<7>, TargetConstant:i64<0>
In function: entrypoint
Stack dump:
0. Running pass 'Function Pass Manager' on module 'linked_module'.
1. Running pass 'BPF DAG->DAG Pattern Instruction Selection' on function '@entrypoint'
```
-----
**Fix**
This patch lowers single-thread (compiler) fences to `ISD::MEMBARRIER`
(no-op) and produces an error for cross-thread (runtime) fences.
The BPF backend has no instruction-selection pattern for
ISD::ATOMIC_FENCE, so LLVM IR containing a fence instruction crashes withCannot select: AtomicFence....Rust code snippet
LLVM IR
Error
Fix
This patch lowers single-thread (compiler) fences to
ISD::MEMBARRIER(no-op) and produces an error for cross-thread (runtime) fences.