Skip to content

Commit 3ebe867

Browse files
committed
Introduce OperandValue::nontemporal_store and use it in the intrinsics
We use a new MemFlags bitflags type to merge some store code paths.
1 parent b638f11 commit 3ebe867

File tree

9 files changed

+65
-78
lines changed

9 files changed

+65
-78
lines changed

src/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_trans/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ crate-type = ["dylib"]
1010
test = false
1111

1212
[dependencies]
13+
bitflags = "1.0.1"
1314
cc = "1.0.1"
1415
flate2 = "1.0"
1516
jobserver = "0.1.5"

src/librustc_trans/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use llvm::{self, ValueRef, AttributePlace};
1212
use base;
13-
use builder::Builder;
13+
use builder::{Builder, MemFlags};
1414
use common::{ty_fn_sig, C_usize};
1515
use context::CodegenCx;
1616
use mir::place::PlaceRef;
@@ -221,7 +221,7 @@ impl<'a, 'tcx> ArgTypeExt<'a, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
221221
bx.pointercast(llscratch, Type::i8p(cx)),
222222
C_usize(cx, self.layout.size.bytes()),
223223
self.layout.align.min(scratch_align),
224-
false);
224+
MemFlags::empty());
225225

226226
bx.lifetime_end(llscratch, scratch_size);
227227
}

src/librustc_trans/base.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_incremental;
5353
use allocator;
5454
use mir::place::PlaceRef;
5555
use attributes;
56-
use builder::Builder;
56+
use builder::{Builder, MemFlags};
5757
use callee;
5858
use common::{C_bool, C_bytes_in_context, C_i32, C_usize};
5959
use rustc_mir::monomorphize::collector::{self, MonoItemCollectionMode};
@@ -320,7 +320,7 @@ pub fn coerce_unsized_into<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
320320

321321
if src_f.layout.ty == dst_f.layout.ty {
322322
memcpy_ty(bx, dst_f.llval, src_f.llval, src_f.layout,
323-
src_f.align.min(dst_f.align), false);
323+
src_f.align.min(dst_f.align), MemFlags::empty());
324324
} else {
325325
coerce_unsized_into(bx, src_f, dst_f);
326326
}
@@ -409,7 +409,14 @@ pub fn call_memcpy(bx: &Builder,
409409
src: ValueRef,
410410
n_bytes: ValueRef,
411411
align: Align,
412-
volatile: bool) {
412+
flags: MemFlags) {
413+
if flags.contains(MemFlags::NONTEMPORAL) {
414+
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
415+
let val = bx.load(src, align);
416+
let ptr = bx.pointercast(dst, val_ty(val).ptr_to());
417+
bx.store_with_flags(val, ptr, align, flags);
418+
return;
419+
}
413420
let cx = bx.cx;
414421
let ptr_width = &cx.sess().target.target.target_pointer_width;
415422
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
@@ -418,7 +425,7 @@ pub fn call_memcpy(bx: &Builder,
418425
let dst_ptr = bx.pointercast(dst, Type::i8p(cx));
419426
let size = bx.intcast(n_bytes, cx.isize_ty, false);
420427
let align = C_i32(cx, align.abi() as i32);
421-
let volatile = C_bool(cx, volatile);
428+
let volatile = C_bool(cx, flags.contains(MemFlags::VOLATILE));
422429
bx.call(memcpy, &[dst_ptr, src_ptr, size, align, volatile], None);
423430
}
424431

@@ -428,14 +435,14 @@ pub fn memcpy_ty<'a, 'tcx>(
428435
src: ValueRef,
429436
layout: TyLayout<'tcx>,
430437
align: Align,
431-
volatile: bool,
438+
flags: MemFlags,
432439
) {
433440
let size = layout.size.bytes();
434441
if size == 0 {
435442
return;
436443
}
437444

438-
call_memcpy(bx, dst, src, C_usize(bx.cx, size), align, volatile);
445+
call_memcpy(bx, dst, src, C_usize(bx.cx, size), align, flags);
439446
}
440447

441448
pub fn call_memset<'a, 'tcx>(bx: &Builder<'a, 'tcx>,

src/librustc_trans/builder.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ fn noname() -> *const c_char {
5050
&CNULL
5151
}
5252

53+
bitflags! {
54+
pub struct MemFlags: u8 {
55+
const VOLATILE = 1 << 0;
56+
const NONTEMPORAL = 1 << 1;
57+
}
58+
}
59+
5360
impl<'a, 'tcx> Builder<'a, 'tcx> {
5461
pub fn new_block<'b>(cx: &'a CodegenCx<'a, 'tcx>, llfn: ValueRef, name: &'b str) -> Self {
5562
let bx = Builder::with_cx(cx);
@@ -579,30 +586,39 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
579586
}
580587

581588
pub fn store(&self, val: ValueRef, ptr: ValueRef, align: Align) -> ValueRef {
582-
debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
589+
self.store_with_flags(val, ptr, align, MemFlags::empty())
590+
}
591+
592+
pub fn store_with_flags(
593+
&self,
594+
val: ValueRef,
595+
ptr: ValueRef,
596+
align: Align,
597+
flags: MemFlags,
598+
) -> ValueRef {
599+
debug!("Store {:?} -> {:?} ({:?})", Value(val), Value(ptr), flags);
583600
assert!(!self.llbuilder.is_null());
584601
self.count_insn("store");
585602
let ptr = self.check_store(val, ptr);
586603
unsafe {
587604
let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
588605
llvm::LLVMSetAlignment(store, align.abi() as c_uint);
606+
if flags.contains(MemFlags::VOLATILE) {
607+
llvm::LLVMSetVolatile(store, llvm::True);
608+
}
609+
if flags.contains(MemFlags::NONTEMPORAL) {
610+
// According to LLVM [1] building a nontemporal store must
611+
// *always* point to a metadata value of the integer 1.
612+
//
613+
// [1]: http://llvm.org/docs/LangRef.html#store-instruction
614+
let one = C_i32(self.cx, 1);
615+
let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
616+
llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
617+
}
589618
store
590619
}
591620
}
592621

593-
pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef, align: Align) -> ValueRef {
594-
debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
595-
assert!(!self.llbuilder.is_null());
596-
self.count_insn("store.volatile");
597-
let ptr = self.check_store(val, ptr);
598-
unsafe {
599-
let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
600-
llvm::LLVMSetAlignment(insn, align.abi() as c_uint);
601-
llvm::LLVMSetVolatile(insn, llvm::True);
602-
insn
603-
}
604-
}
605-
606622
pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef,
607623
order: AtomicOrdering, align: Align) {
608624
debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
@@ -616,29 +632,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
616632
}
617633
}
618634

619-
pub fn nontemporal_store(&self, val: ValueRef, ptr: ValueRef) -> ValueRef {
620-
debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
621-
assert!(!self.llbuilder.is_null());
622-
self.count_insn("store.nontemporal");
623-
let ptr = self.check_store(val, ptr);
624-
unsafe {
625-
let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
626-
627-
// According to LLVM [1] building a nontemporal store must *always*
628-
// point to a metadata value of the integer 1. Who knew?
629-
//
630-
// [1]: http://llvm.org/docs/LangRef.html#store-instruction
631-
let one = C_i32(self.cx, 1);
632-
let node = llvm::LLVMMDNodeInContext(self.cx.llcx,
633-
&one,
634-
1);
635-
llvm::LLVMSetMetadata(insn,
636-
llvm::MD_nontemporal as c_uint,
637-
node);
638-
insn
639-
}
640-
}
641-
642635
pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
643636
self.count_insn("gep");
644637
unsafe {

src/librustc_trans/intrinsic.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
248248
},
249249
"volatile_store" => {
250250
let dst = args[0].deref(bx.cx);
251-
if dst.layout.is_zst() {
252-
return;
253-
}
254251
args[1].val.volatile_store(bx, dst);
255252
return;
256253
},
@@ -536,19 +533,9 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
536533
}
537534

538535
"nontemporal_store" => {
539-
let tp_ty = substs.type_at(0);
540536
let dst = args[0].deref(bx.cx);
541-
let val = if let OperandValue::Ref(ptr, align) = args[1].val {
542-
bx.load(ptr, align)
543-
} else {
544-
from_immediate(bx, args[1].immediate())
545-
};
546-
let ptr = bx.pointercast(dst.llval, val_ty(val).ptr_to());
547-
let store = bx.nontemporal_store(val, ptr);
548-
unsafe {
549-
llvm::LLVMSetAlignment(store, cx.align_of(tp_ty).abi() as u32);
550-
}
551-
return
537+
args[1].val.nontemporal_store(bx, dst);
538+
return;
552539
}
553540

554541
_ => {

src/librustc_trans/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use rustc::dep_graph::WorkProduct;
3535
use syntax_pos::symbol::Symbol;
3636

37+
#[macro_use] extern crate bitflags;
3738
extern crate flate2;
3839
extern crate libc;
3940
#[macro_use] extern crate rustc;

src/librustc_trans/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::mir::interpret::EvalErrorKind;
1717
use abi::{Abi, ArgType, ArgTypeExt, FnType, FnTypeExt, LlvmType, PassMode};
1818
use base;
1919
use callee;
20-
use builder::Builder;
20+
use builder::{Builder, MemFlags};
2121
use common::{self, C_bool, C_str_slice, C_struct, C_u32, C_uint_big, C_undef};
2222
use consts;
2323
use meth;
@@ -626,7 +626,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
626626
// have scary latent bugs around.
627627

628628
let scratch = PlaceRef::alloca(bx, arg.layout, "arg");
629-
base::memcpy_ty(bx, scratch.llval, llval, op.layout, align, false);
629+
base::memcpy_ty(bx, scratch.llval, llval, op.layout, align, MemFlags::empty());
630630
(scratch.llval, scratch.align, true)
631631
} else {
632632
(llval, align, true)

src/librustc_trans/mir/operand.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::indexed_vec::Idx;
1818

1919
use base;
2020
use common::{self, CodegenCx, C_null, C_undef, C_usize};
21-
use builder::Builder;
21+
use builder::{Builder, MemFlags};
2222
use value::Value;
2323
use type_of::LayoutLlvmExt;
2424
use type_::Type;
@@ -275,31 +275,32 @@ impl<'a, 'tcx> OperandRef<'tcx> {
275275

276276
impl<'a, 'tcx> OperandValue {
277277
pub fn store(self, bx: &Builder<'a, 'tcx>, dest: PlaceRef<'tcx>) {
278-
self.store_maybe_volatile(bx, dest, false);
278+
self.store_with_flags(bx, dest, MemFlags::empty());
279279
}
280280

281281
pub fn volatile_store(self, bx: &Builder<'a, 'tcx>, dest: PlaceRef<'tcx>) {
282-
self.store_maybe_volatile(bx, dest, true);
282+
self.store_with_flags(bx, dest, MemFlags::VOLATILE);
283283
}
284284

285-
fn store_maybe_volatile(self, bx: &Builder<'a, 'tcx>, dest: PlaceRef<'tcx>, volatile: bool) {
285+
pub fn nontemporal_store(self, bx: &Builder<'a, 'tcx>, dest: PlaceRef<'tcx>) {
286+
self.store_with_flags(bx, dest, MemFlags::NONTEMPORAL);
287+
}
288+
289+
fn store_with_flags(self, bx: &Builder<'a, 'tcx>, dest: PlaceRef<'tcx>, flags: MemFlags) {
286290
debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest);
287291
// Avoid generating stores of zero-sized values, because the only way to have a zero-sized
288292
// value is through `undef`, and store itself is useless.
289293
if dest.layout.is_zst() {
290294
return;
291295
}
292296
match self {
293-
OperandValue::Ref(r, source_align) =>
297+
OperandValue::Ref(r, source_align) => {
294298
base::memcpy_ty(bx, dest.llval, r, dest.layout,
295-
source_align.min(dest.align), volatile),
299+
source_align.min(dest.align), flags)
300+
}
296301
OperandValue::Immediate(s) => {
297302
let val = base::from_immediate(bx, s);
298-
if !volatile {
299-
bx.store(val, dest.llval, dest.align);
300-
} else {
301-
bx.volatile_store(val, dest.llval, dest.align);
302-
}
303+
bx.store_with_flags(val, dest.llval, dest.align, flags);
303304
}
304305
OperandValue::Pair(a, b) => {
305306
for (i, &x) in [a, b].iter().enumerate() {
@@ -309,11 +310,7 @@ impl<'a, 'tcx> OperandValue {
309310
llptr = bx.pointercast(llptr, Type::i8p(bx.cx));
310311
}
311312
let val = base::from_immediate(bx, x);
312-
if !volatile {
313-
bx.store(val, llptr, dest.align);
314-
} else {
315-
bx.volatile_store(val, llptr, dest.align);
316-
}
313+
bx.store_with_flags(val, llptr, dest.align, flags);
317314
}
318315
}
319316
}

0 commit comments

Comments
 (0)