Skip to content

Commit d3cba25

Browse files
committed
Auto merge of #71192 - oli-obk:eager_alloc_id_canonicalization, r=wesleywiser
Make TLS accesses explicit in MIR r? @rust-lang/wg-mir-opt cc @RalfJung @vakaras for miri thread locals cc @bjorn3 for cranelift fixes #70685
2 parents b85e3fe + 0aa7f4d commit d3cba25

File tree

31 files changed

+157
-23
lines changed

31 files changed

+157
-23
lines changed

src/librustc_codegen_llvm/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
259259
GlobalAlloc::Function(fn_instance) => self.get_fn_addr(fn_instance),
260260
GlobalAlloc::Static(def_id) => {
261261
assert!(self.tcx.is_static(def_id));
262+
assert!(!self.tcx.is_thread_local_static(def_id));
262263
self.get_static(def_id)
263264
}
264265
};

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
522522
let operand = OperandRef { val: OperandValue::Immediate(val), layout: box_layout };
523523
(bx, operand)
524524
}
525+
mir::Rvalue::ThreadLocalRef(def_id) => {
526+
assert!(bx.cx().tcx().is_static(def_id));
527+
let static_ = bx.get_static(def_id);
528+
let layout = bx.layout_of(bx.cx().tcx().static_ptr_ty(def_id));
529+
let operand = OperandRef::from_immediate_or_packed_pair(&mut bx, static_, layout);
530+
(bx, operand)
531+
}
525532
mir::Rvalue::Use(ref operand) => {
526533
let operand = self.codegen_operand(&mut bx, operand);
527534
(bx, operand)
@@ -745,6 +752,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
745752
mir::Rvalue::UnaryOp(..) |
746753
mir::Rvalue::Discriminant(..) |
747754
mir::Rvalue::NullaryOp(..) |
755+
mir::Rvalue::ThreadLocalRef(_) |
748756
mir::Rvalue::Use(..) => // (*)
749757
true,
750758
mir::Rvalue::Repeat(..) |

src/librustc_middle/mir/interpret/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ pub enum UnsupportedOpInfo {
513513
//
514514
/// Encountered raw bytes where we needed a pointer.
515515
ReadBytesAsPointer,
516+
/// Accessing thread local statics
517+
ThreadLocalStatic(DefId),
516518
}
517519

518520
impl fmt::Display for UnsupportedOpInfo {
@@ -526,6 +528,7 @@ impl fmt::Display for UnsupportedOpInfo {
526528
NoMirFor(did) => write!(f, "no MIR body is available for {:?}", did),
527529
ReadPointerAsBytes => write!(f, "unable to turn pointer into raw bytes",),
528530
ReadBytesAsPointer => write!(f, "unable to turn bytes into a pointer"),
531+
ThreadLocalStatic(did) => write!(f, "accessing thread local static {:?}", did),
529532
}
530533
}
531534
}

src/librustc_middle/mir/interpret/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
209209
fn_instance.encode(encoder)?;
210210
}
211211
GlobalAlloc::Static(did) => {
212+
assert!(!tcx.is_thread_local_static(did));
212213
// References to statics doesn't need to know about their allocations,
213214
// just about its `DefId`.
214215
AllocDiscriminant::Static.encode(encoder)?;

src/librustc_middle/mir/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,11 @@ pub enum Rvalue<'tcx> {
22142214
/// &x or &mut x
22152215
Ref(Region<'tcx>, BorrowKind, Place<'tcx>),
22162216

2217+
/// Accessing a thread local static. This is inherently a runtime operation, even if llvm
2218+
/// treats it as an access to a static. This `Rvalue` yields a reference to the thread local
2219+
/// static.
2220+
ThreadLocalRef(DefId),
2221+
22172222
/// Create a raw pointer to the given place
22182223
/// Can be generated by raw address of expressions (`&raw const x`),
22192224
/// or when casting a reference to a raw pointer.
@@ -2353,6 +2358,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23532358
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
23542359
Discriminant(ref place) => write!(fmt, "discriminant({:?})", place),
23552360
NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
2361+
ThreadLocalRef(did) => ty::tls::with(|tcx| {
2362+
let muta = tcx.static_mutability(did).unwrap().prefix_str();
2363+
write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
2364+
}),
23562365
Ref(region, borrow_kind, ref place) => {
23572366
let kind_str = match borrow_kind {
23582367
BorrowKind::Shared => "",
@@ -2506,7 +2515,10 @@ impl Constant<'tcx> {
25062515
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
25072516
match self.literal.val.try_to_scalar() {
25082517
Some(Scalar::Ptr(ptr)) => match tcx.global_alloc(ptr.alloc_id) {
2509-
GlobalAlloc::Static(def_id) => Some(def_id),
2518+
GlobalAlloc::Static(def_id) => {
2519+
assert!(!tcx.is_thread_local_static(def_id));
2520+
Some(def_id)
2521+
}
25102522
_ => None,
25112523
},
25122524
_ => None,

src/librustc_middle/mir/tcx.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ impl<'tcx> Rvalue<'tcx> {
151151
Rvalue::Repeat(ref operand, count) => {
152152
tcx.mk_ty(ty::Array(operand.ty(local_decls, tcx), count))
153153
}
154+
Rvalue::ThreadLocalRef(did) => {
155+
if tcx.is_mutable_static(did) {
156+
tcx.mk_mut_ptr(tcx.type_of(did))
157+
} else {
158+
tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.type_of(did))
159+
}
160+
}
154161
Rvalue::Ref(reg, bk, ref place) => {
155162
let place_ty = place.ty(local_decls, tcx).ty;
156163
tcx.mk_ref(reg, ty::TypeAndMut { ty: place_ty, mutbl: bk.to_mutbl_lossy() })

src/librustc_middle/mir/type_foldable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
177177
match *self {
178178
Use(ref op) => Use(op.fold_with(folder)),
179179
Repeat(ref op, len) => Repeat(op.fold_with(folder), len),
180+
ThreadLocalRef(did) => ThreadLocalRef(did.fold_with(folder)),
180181
Ref(region, bk, ref place) => {
181182
Ref(region.fold_with(folder), bk, place.fold_with(folder))
182183
}
@@ -220,6 +221,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
220221
match *self {
221222
Use(ref op) => op.visit_with(visitor),
222223
Repeat(ref op, _) => op.visit_with(visitor),
224+
ThreadLocalRef(did) => did.visit_with(visitor),
223225
Ref(region, _, ref place) => region.visit_with(visitor) || place.visit_with(visitor),
224226
AddressOf(_, ref place) => place.visit_with(visitor),
225227
Len(ref place) => place.visit_with(visitor),

src/librustc_middle/mir/visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ macro_rules! make_mir_visitor {
601601
self.visit_operand(value, location);
602602
}
603603

604+
Rvalue::ThreadLocalRef(_) => {}
605+
604606
Rvalue::Ref(r, bk, path) => {
605607
self.visit_region(r, location);
606608
let ctx = match bk {

src/librustc_mir/borrow_check/invalidation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
301301
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
302302
}
303303

304+
Rvalue::ThreadLocalRef(_) => {}
305+
304306
Rvalue::Use(ref operand)
305307
| Rvalue::Repeat(ref operand, _)
306308
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)

src/librustc_mir/borrow_check/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12961296
);
12971297
}
12981298

1299+
Rvalue::ThreadLocalRef(_) => {}
1300+
12991301
Rvalue::Use(ref operand)
13001302
| Rvalue::Repeat(ref operand, _)
13011303
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)

0 commit comments

Comments
 (0)