Skip to content

Commit 998c10d

Browse files
committed
Add singlethreaded fence intrinsics.
These new intrinsics are comparable to `atomic_signal_fence` in C++, ensuring the compiler will not reorder memory accesses across the barrier, nor will it emit any machine instructions for it. Closes #24118, implementing RFC 888.
1 parent 0d8309e commit 998c10d

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-11
lines changed

src/libcore/intrinsics.rs

+15
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ extern "rust-intrinsic" {
139139
pub fn atomic_fence_rel();
140140
pub fn atomic_fence_acqrel();
141141

142+
/// A compiler-only memory barrier.
143+
///
144+
/// Memory accesses will never be reordered across this barrier by the compiler,
145+
/// but no instructions will be emitted for it. This is appropriate for operations
146+
/// on the same thread that may be preempted, such as when interacting with signal
147+
/// handlers.
148+
#[cfg(not(stage0))] // SNAP 5520801
149+
pub fn atomic_singlethreadfence();
150+
#[cfg(not(stage0))] // SNAP 5520801
151+
pub fn atomic_singlethreadfence_acq();
152+
#[cfg(not(stage0))] // SNAP 5520801
153+
pub fn atomic_singlethreadfence_rel();
154+
#[cfg(not(stage0))] // SNAP 5520801
155+
pub fn atomic_singlethreadfence_acqrel();
156+
142157
/// Aborts the execution of the process.
143158
pub fn abort() -> !;
144159

src/librustc_llvm/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub use self::RealPredicate::*;
4343
pub use self::TypeKind::*;
4444
pub use self::AtomicBinOp::*;
4545
pub use self::AtomicOrdering::*;
46+
pub use self::SynchronizationScope::*;
4647
pub use self::FileType::*;
4748
pub use self::MetadataType::*;
4849
pub use self::AsmDialect::*;
@@ -361,6 +362,13 @@ pub enum AtomicOrdering {
361362
SequentiallyConsistent = 7
362363
}
363364

365+
#[repr(C)]
366+
#[derive(Copy, Clone)]
367+
pub enum SynchronizationScope {
368+
SingleThread = 0,
369+
CrossThread = 1
370+
}
371+
364372
// Consts for the LLVMCodeGenFileType type (in include/llvm/c/TargetMachine.h)
365373
#[repr(C)]
366374
#[derive(Copy, Clone)]
@@ -1534,7 +1542,9 @@ extern {
15341542
SingleThreaded: Bool)
15351543
-> ValueRef;
15361544

1537-
pub fn LLVMBuildAtomicFence(B: BuilderRef, Order: AtomicOrdering);
1545+
pub fn LLVMBuildAtomicFence(B: BuilderRef,
1546+
Order: AtomicOrdering,
1547+
Scope: SynchronizationScope);
15381548

15391549

15401550
/* Selected entries from the downcasts. */

src/librustc_trans/trans/build.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#![allow(non_snake_case)]
1313

1414
use llvm;
15-
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect, AttrBuilder};
15+
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, SynchronizationScope, AsmDialect, AttrBuilder};
1616
use llvm::{Opcode, IntPredicate, RealPredicate};
1717
use llvm::{ValueRef, BasicBlockRef};
1818
use trans::common::*;
@@ -965,9 +965,9 @@ pub fn CallWithConv(cx: Block,
965965
B(cx).call_with_conv(fn_, args, conv, attributes)
966966
}
967967

968-
pub fn AtomicFence(cx: Block, order: AtomicOrdering) {
968+
pub fn AtomicFence(cx: Block, order: AtomicOrdering, scope: SynchronizationScope) {
969969
if cx.unreachable.get() { return; }
970-
B(cx).atomic_fence(order)
970+
B(cx).atomic_fence(order, scope)
971971
}
972972

973973
pub fn Select(cx: Block, if_: ValueRef, then: ValueRef, else_: ValueRef) -> ValueRef {

src/librustc_trans/trans/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![allow(dead_code)] // FFI wrappers
1212

1313
use llvm;
14-
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect, AttrBuilder};
14+
use llvm::{CallConv, AtomicBinOp, AtomicOrdering, SynchronizationScope, AsmDialect, AttrBuilder};
1515
use llvm::{Opcode, IntPredicate, RealPredicate, False};
1616
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef};
1717
use trans::base;
@@ -989,9 +989,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
989989
}
990990
}
991991

992-
pub fn atomic_fence(&self, order: AtomicOrdering) {
992+
pub fn atomic_fence(&self, order: AtomicOrdering, scope: SynchronizationScope) {
993993
unsafe {
994-
llvm::LLVMBuildAtomicFence(self.llbuilder, order);
994+
llvm::LLVMBuildAtomicFence(self.llbuilder, order, scope);
995995
}
996996
}
997997
}

src/librustc_trans/trans/intrinsic.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,12 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
763763
}
764764

765765
"fence" => {
766-
AtomicFence(bcx, order);
766+
AtomicFence(bcx, order, llvm::CrossThread);
767+
C_nil(ccx)
768+
}
769+
770+
"singlethreadfence" => {
771+
AtomicFence(bcx, order, llvm::SingleThread);
767772
C_nil(ccx)
768773
}
769774

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4812,7 +4812,7 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
48124812
(1, vec!(ty::mk_mut_ptr(tcx, param(ccx, 0)), param(ccx, 0)),
48134813
param(ccx, 0))
48144814
}
4815-
"fence" => {
4815+
"fence" | "singlethreadfence" => {
48164816
(0, Vec::new(), ty::mk_nil(tcx))
48174817
}
48184818
op => {

src/rustllvm/RustWrapper.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ extern "C" LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B,
189189
failure_order
190190
));
191191
}
192-
extern "C" LLVMValueRef LLVMBuildAtomicFence(LLVMBuilderRef B, AtomicOrdering order) {
193-
return wrap(unwrap(B)->CreateFence(order));
192+
extern "C" LLVMValueRef LLVMBuildAtomicFence(LLVMBuilderRef B,
193+
AtomicOrdering order,
194+
SynchronizationScope scope) {
195+
return wrap(unwrap(B)->CreateFence(order, scope));
194196
}
195197

196198
extern "C" void LLVMSetDebug(int Enabled) {

0 commit comments

Comments
 (0)