Skip to content

[BPF] treat compiler fence as codegen no-op#196734

Merged
yonghong-song merged 1 commit into
llvm:mainfrom
blueshift-gg:BPF_compiler_fence
May 19, 2026
Merged

[BPF] treat compiler fence as codegen no-op#196734
yonghong-song merged 1 commit into
llvm:mainfrom
blueshift-gg:BPF_compiler_fence

Conversation

@bidhan-a

@bidhan-a bidhan-a commented May 9, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown

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 @ followed by their GitHub username.

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.

@bidhan-a

Copy link
Copy Markdown
Contributor Author

cc @yonghong-song This PR fixes a crash, please take a look.

@yonghong-song yonghong-song requested review from 4ast and eddyz87 May 11, 2026 14:38
@yonghong-song

Copy link
Copy Markdown
Contributor

Beyond the above rust example. The below are two C examples:

  void memory_barrier(void) {
      __sync_synchronize();                                                                                        
  }

or

#include <stdatomic.h>
void memory_barrier(void) {
  __c11_atomic_thread_fence(__ATOMIC_ACQUIRE);
}

where __sync_synchronize() will generate IR 'fence seq_cst',
and __c11_atomic_thread_fence(__ATOMIC_ACQUIRE) will generate IR 'fence acquire'.

@yonghong-song yonghong-song left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@4ast 4ast left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@clairechingching

Copy link
Copy Markdown
Contributor

@4ast

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.

@4ast

4ast commented May 12, 2026

Copy link
Copy Markdown
Member

@4ast

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
#define smp_mb()
({
volatile unsigned long __val;
__sync_fetch_and_add(&__val, 0);
})

iirc x86 does 'lock or (rsp)' which is same idea.
__sync_fetch_and_add in BPF will eventually be JITed as 'lock xadd' on x86.

@bidhan-a bidhan-a force-pushed the BPF_compiler_fence branch from ff7569d to 8fb0b19 Compare May 13, 2026 11:16
@bidhan-a bidhan-a changed the title [BPF] treat compiler fence as codegen no-op [BPF] Lower ATOMIC_FENCE: real barrier for cross-thread seq_cst, no-op otherwise May 13, 2026
@bidhan-a

Copy link
Copy Markdown
Contributor Author

@4ast
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 #define smp_mb() ({ volatile unsigned long __val; __sync_fetch_and_add(&__val, 0); })

iirc x86 does 'lock or (rsp)' which is same idea. __sync_fetch_and_add in BPF will eventually be JITed as 'lock xadd' on x86.

@4ast Updated the handling for fence seq_cst as per your suggestion. Could you please take a look?

@4ast 4ast left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@eddyz87

eddyz87 commented May 13, 2026

Copy link
Copy Markdown
Contributor

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?

Comment thread llvm/lib/Target/BPF/BPFISelLowering.cpp
@bidhan-a bidhan-a force-pushed the BPF_compiler_fence branch 2 times, most recently from 282e5c0 to e3a19d9 Compare May 15, 2026 04:04
@bidhan-a bidhan-a force-pushed the BPF_compiler_fence branch from e3a19d9 to 831c610 Compare May 16, 2026 12:44
@bidhan-a bidhan-a changed the title [BPF] Lower ATOMIC_FENCE: real barrier for cross-thread seq_cst, no-op otherwise [BPF] treat compiler fence as codegen no-op May 16, 2026
@clairechingching

Copy link
Copy Markdown
Contributor

@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

@yonghong-song yonghong-song merged commit 571b226 into llvm:main May 19, 2026
10 checks passed
@github-actions

Copy link
Copy Markdown

@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!

dyung pushed a commit that referenced this pull request Jun 10, 2026
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.
daunabomba pushed a commit to daunabomba/llvm-project that referenced this pull request Jun 17, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants