Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a barrier by rust asm?

In gcc, we can use asm volatile("":::"memory");

But I can't find a option similar to "memory" in document of rust inline asm.

Is there any way to do that?

like image 723
Haoan Avatar asked Oct 17 '25 19:10

Haoan


1 Answers

In Rust, memory clobbering is the default. You should use options(nomem) to opt it out.

For example:

pub unsafe fn no_nomem() {
    std::arch::asm!("");
}

pub unsafe fn nomem() {
    std::arch::asm!("", options(nomem));
}

LLVM IR:

define void @_ZN7example8no_nomem17h95b023e6c43118daE() unnamed_addr #0 !dbg !5 {
  call void asm sideeffect alignstack inteldialect "", "~{dirflag},~{fpsr},~{flags},~{memory}"(), !dbg !10, !srcloc !11
  br label %bb1, !dbg !10

bb1:                                              ; preds = %start
  ret void, !dbg !12
}

define void @_ZN7example5nomem17hc75cf2d808290004E() unnamed_addr #0 !dbg !13 {
  call void asm sideeffect alignstack inteldialect "", "~{dirflag},~{fpsr},~{flags}"() #1, !dbg !14, !srcloc !15
  br label %bb1, !dbg !14

bb1:                                              ; preds = %start
  ret void, !dbg !16
}

The function without nomem emits a ~{memory} barrier.

like image 90
Chayim Friedman Avatar answered Oct 19 '25 13:10

Chayim Friedman