Skip to content

Commit f898179

Browse files
committed
Auto merge of #52461 - irinagpopa:safe-llvm, r=nikomatsakis
rustc_codegen_llvm: use safe references for LLVM FFI types. Part of #45274.
2 parents ed8d14d + baff67d commit f898179

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3982
-4321
lines changed

Diff for: src/Cargo.lock

-3
Original file line numberDiff line numberDiff line change
@@ -2222,11 +2222,8 @@ dependencies = [
22222222
name = "rustc_llvm"
22232223
version = "0.0.0"
22242224
dependencies = [
2225-
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
22262225
"build_helper 0.1.0",
22272226
"cc 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
2228-
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
2229-
"rustc_cratesio_shim 0.0.0",
22302227
]
22312228

22322229
[[package]]

Diff for: src/librustc_codegen_llvm/abi.rs

+39-38
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use llvm::{self, ValueRef, AttributePlace};
11+
use llvm::{self, AttributePlace};
1212
use base;
1313
use builder::{Builder, MemFlags};
1414
use common::{ty_fn_sig, C_usize};
@@ -17,6 +17,7 @@ use mir::place::PlaceRef;
1717
use mir::operand::OperandValue;
1818
use type_::Type;
1919
use type_of::{LayoutLlvmExt, PointerKind};
20+
use value::Value;
2021

2122
use rustc_target::abi::{LayoutOf, Size, TyLayout};
2223
use rustc::ty::{self, Ty};
@@ -46,12 +47,12 @@ impl ArgAttributeExt for ArgAttribute {
4647
}
4748

4849
pub trait ArgAttributesExt {
49-
fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef);
50-
fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef);
50+
fn apply_llfn(&self, idx: AttributePlace, llfn: &Value);
51+
fn apply_callsite(&self, idx: AttributePlace, callsite: &Value);
5152
}
5253

5354
impl ArgAttributesExt for ArgAttributes {
54-
fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
55+
fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) {
5556
let mut regular = self.regular;
5657
unsafe {
5758
let deref = self.pointee_size.bytes();
@@ -76,7 +77,7 @@ impl ArgAttributesExt for ArgAttributes {
7677
}
7778
}
7879

79-
fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
80+
fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) {
8081
let mut regular = self.regular;
8182
unsafe {
8283
let deref = self.pointee_size.bytes();
@@ -103,11 +104,11 @@ impl ArgAttributesExt for ArgAttributes {
103104
}
104105

105106
pub trait LlvmType {
106-
fn llvm_type(&self, cx: &CodegenCx) -> Type;
107+
fn llvm_type(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type;
107108
}
108109

109110
impl LlvmType for Reg {
110-
fn llvm_type(&self, cx: &CodegenCx) -> Type {
111+
fn llvm_type(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
111112
match self.kind {
112113
RegKind::Integer => Type::ix(cx, self.size.bits()),
113114
RegKind::Float => {
@@ -118,14 +119,14 @@ impl LlvmType for Reg {
118119
}
119120
}
120121
RegKind::Vector => {
121-
Type::vector(&Type::i8(cx), self.size.bytes())
122+
Type::vector(Type::i8(cx), self.size.bytes())
122123
}
123124
}
124125
}
125126
}
126127

127128
impl LlvmType for CastTarget {
128-
fn llvm_type(&self, cx: &CodegenCx) -> Type {
129+
fn llvm_type(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
129130
let rest_ll_unit = self.rest.unit.llvm_type(cx);
130131
let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
131132
(0, 0)
@@ -142,7 +143,7 @@ impl LlvmType for CastTarget {
142143

143144
// Simplify to array when all chunks are the same size and type
144145
if rem_bytes == 0 {
145-
return Type::array(&rest_ll_unit, rest_count);
146+
return Type::array(rest_ll_unit, rest_count);
146147
}
147148
}
148149

@@ -164,24 +165,24 @@ impl LlvmType for CastTarget {
164165
}
165166
}
166167

167-
pub trait ArgTypeExt<'a, 'tcx> {
168-
fn memory_ty(&self, cx: &CodegenCx<'a, 'tcx>) -> Type;
169-
fn store(&self, bx: &Builder<'a, 'tcx>, val: ValueRef, dst: PlaceRef<'tcx>);
170-
fn store_fn_arg(&self, bx: &Builder<'a, 'tcx>, idx: &mut usize, dst: PlaceRef<'tcx>);
168+
pub trait ArgTypeExt<'ll, 'tcx> {
169+
fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
170+
fn store(&self, bx: &Builder<'_, 'll, 'tcx>, val: &'ll Value, dst: PlaceRef<'ll, 'tcx>);
171+
fn store_fn_arg(&self, bx: &Builder<'_, 'll, 'tcx>, idx: &mut usize, dst: PlaceRef<'ll, 'tcx>);
171172
}
172173

173-
impl<'a, 'tcx> ArgTypeExt<'a, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
174+
impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
174175
/// Get the LLVM type for a place of the original Rust type of
175176
/// this argument/return, i.e. the result of `type_of::type_of`.
176-
fn memory_ty(&self, cx: &CodegenCx<'a, 'tcx>) -> Type {
177+
fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
177178
self.layout.llvm_type(cx)
178179
}
179180

180181
/// Store a direct/indirect value described by this ArgType into a
181182
/// place for the original Rust type of this argument/return.
182183
/// Can be used for both storing formal arguments into Rust variables
183184
/// or results of call/invoke instructions into their destinations.
184-
fn store(&self, bx: &Builder<'a, 'tcx>, val: ValueRef, dst: PlaceRef<'tcx>) {
185+
fn store(&self, bx: &Builder<'_, 'll, 'tcx>, val: &'ll Value, dst: PlaceRef<'ll, 'tcx>) {
185186
if self.is_ignore() {
186187
return;
187188
}
@@ -234,7 +235,7 @@ impl<'a, 'tcx> ArgTypeExt<'a, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
234235
}
235236
}
236237

237-
fn store_fn_arg(&self, bx: &Builder<'a, 'tcx>, idx: &mut usize, dst: PlaceRef<'tcx>) {
238+
fn store_fn_arg(&self, bx: &Builder<'a, 'll, 'tcx>, idx: &mut usize, dst: PlaceRef<'ll, 'tcx>) {
238239
let mut next = || {
239240
let val = llvm::get_param(bx.llfn(), *idx as c_uint);
240241
*idx += 1;
@@ -252,48 +253,48 @@ impl<'a, 'tcx> ArgTypeExt<'a, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
252253
}
253254
}
254255

255-
pub trait FnTypeExt<'a, 'tcx> {
256-
fn of_instance(cx: &CodegenCx<'a, 'tcx>, instance: &ty::Instance<'tcx>)
256+
pub trait FnTypeExt<'tcx> {
257+
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>)
257258
-> Self;
258-
fn new(cx: &CodegenCx<'a, 'tcx>,
259+
fn new(cx: &CodegenCx<'ll, 'tcx>,
259260
sig: ty::FnSig<'tcx>,
260261
extra_args: &[Ty<'tcx>]) -> Self;
261-
fn new_vtable(cx: &CodegenCx<'a, 'tcx>,
262+
fn new_vtable(cx: &CodegenCx<'ll, 'tcx>,
262263
sig: ty::FnSig<'tcx>,
263264
extra_args: &[Ty<'tcx>]) -> Self;
264265
fn new_internal(
265-
cx: &CodegenCx<'a, 'tcx>,
266+
cx: &CodegenCx<'ll, 'tcx>,
266267
sig: ty::FnSig<'tcx>,
267268
extra_args: &[Ty<'tcx>],
268269
mk_arg_type: impl Fn(Ty<'tcx>, Option<usize>) -> ArgType<'tcx, Ty<'tcx>>,
269270
) -> Self;
270271
fn adjust_for_abi(&mut self,
271-
cx: &CodegenCx<'a, 'tcx>,
272+
cx: &CodegenCx<'ll, 'tcx>,
272273
abi: Abi);
273-
fn llvm_type(&self, cx: &CodegenCx<'a, 'tcx>) -> Type;
274+
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
274275
fn llvm_cconv(&self) -> llvm::CallConv;
275-
fn apply_attrs_llfn(&self, llfn: ValueRef);
276-
fn apply_attrs_callsite(&self, bx: &Builder<'a, 'tcx>, callsite: ValueRef);
276+
fn apply_attrs_llfn(&self, llfn: &'ll Value);
277+
fn apply_attrs_callsite(&self, bx: &Builder<'a, 'll, 'tcx>, callsite: &'ll Value);
277278
}
278279

279-
impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
280-
fn of_instance(cx: &CodegenCx<'a, 'tcx>, instance: &ty::Instance<'tcx>)
280+
impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
281+
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>)
281282
-> Self {
282283
let fn_ty = instance.ty(cx.tcx);
283284
let sig = ty_fn_sig(cx, fn_ty);
284285
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
285286
FnType::new(cx, sig, &[])
286287
}
287288

288-
fn new(cx: &CodegenCx<'a, 'tcx>,
289+
fn new(cx: &CodegenCx<'ll, 'tcx>,
289290
sig: ty::FnSig<'tcx>,
290291
extra_args: &[Ty<'tcx>]) -> Self {
291292
FnType::new_internal(cx, sig, extra_args, |ty, _| {
292293
ArgType::new(cx.layout_of(ty))
293294
})
294295
}
295296

296-
fn new_vtable(cx: &CodegenCx<'a, 'tcx>,
297+
fn new_vtable(cx: &CodegenCx<'ll, 'tcx>,
297298
sig: ty::FnSig<'tcx>,
298299
extra_args: &[Ty<'tcx>]) -> Self {
299300
FnType::new_internal(cx, sig, extra_args, |ty, arg_idx| {
@@ -316,7 +317,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
316317
}
317318

318319
fn new_internal(
319-
cx: &CodegenCx<'a, 'tcx>,
320+
cx: &CodegenCx<'ll, 'tcx>,
320321
sig: ty::FnSig<'tcx>,
321322
extra_args: &[Ty<'tcx>],
322323
mk_arg_type: impl Fn(Ty<'tcx>, Option<usize>) -> ArgType<'tcx, Ty<'tcx>>,
@@ -497,7 +498,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
497498
}
498499

499500
fn adjust_for_abi(&mut self,
500-
cx: &CodegenCx<'a, 'tcx>,
501+
cx: &CodegenCx<'ll, 'tcx>,
501502
abi: Abi) {
502503
if abi == Abi::Unadjusted { return }
503504

@@ -564,7 +565,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
564565
}
565566
}
566567

567-
fn llvm_type(&self, cx: &CodegenCx<'a, 'tcx>) -> Type {
568+
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
568569
let args_capacity: usize = self.args.iter().map(|arg|
569570
if arg.pad.is_some() { 1 } else { 0 } +
570571
if let PassMode::Pair(_, _) = arg.mode { 2 } else { 1 }
@@ -606,9 +607,9 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
606607
}
607608

608609
if self.variadic {
609-
Type::variadic_func(&llargument_tys, &llreturn_ty)
610+
Type::variadic_func(&llargument_tys, llreturn_ty)
610611
} else {
611-
Type::func(&llargument_tys, &llreturn_ty)
612+
Type::func(&llargument_tys, llreturn_ty)
612613
}
613614
}
614615

@@ -629,7 +630,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
629630
}
630631
}
631632

632-
fn apply_attrs_llfn(&self, llfn: ValueRef) {
633+
fn apply_attrs_llfn(&self, llfn: &'ll Value) {
633634
let mut i = 0;
634635
let mut apply = |attrs: &ArgAttributes| {
635636
attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn);
@@ -659,7 +660,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
659660
}
660661
}
661662

662-
fn apply_attrs_callsite(&self, bx: &Builder<'a, 'tcx>, callsite: ValueRef) {
663+
fn apply_attrs_callsite(&self, bx: &Builder<'a, 'll, 'tcx>, callsite: &'ll Value) {
663664
let mut i = 0;
664665
let mut apply = |attrs: &ArgAttributes| {
665666
attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite);

Diff for: src/librustc_codegen_llvm/allocator.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use std::ffi::CString;
12-
use std::ptr;
1312

1413
use attributes;
1514
use libc::c_uint;
@@ -21,8 +20,8 @@ use ModuleLlvm;
2120
use llvm::{self, False, True};
2221

2322
pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
24-
let llcx = mods.llcx;
25-
let llmod = mods.llmod;
23+
let llcx = &*mods.llcx;
24+
let llmod = mods.llmod();
2625
let usize = match &tcx.sess.target.target.target_pointer_width[..] {
2726
"16" => llvm::LLVMInt16TypeInContext(llcx),
2827
"32" => llvm::LLVMInt32TypeInContext(llcx),
@@ -90,7 +89,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind
9089
callee,
9190
args.as_ptr(),
9291
args.len() as c_uint,
93-
ptr::null_mut(),
92+
None,
9493
"\0".as_ptr() as *const _);
9594
llvm::LLVMSetTailCall(ret, True);
9695
if output.is_some() {

Diff for: src/librustc_codegen_llvm/asm.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use llvm::{self, ValueRef};
11+
use llvm;
1212
use common::*;
1313
use type_::Type;
1414
use type_of::LayoutLlvmExt;
1515
use builder::Builder;
16+
use value::Value;
1617

1718
use rustc::hir;
1819

@@ -24,11 +25,11 @@ use syntax::ast::AsmDialect;
2425
use libc::{c_uint, c_char};
2526

2627
// Take an inline assembly expression and splat it out via LLVM
27-
pub fn codegen_inline_asm<'a, 'tcx>(
28-
bx: &Builder<'a, 'tcx>,
28+
pub fn codegen_inline_asm(
29+
bx: &Builder<'a, 'll, 'tcx>,
2930
ia: &hir::InlineAsm,
30-
outputs: Vec<PlaceRef<'tcx>>,
31-
mut inputs: Vec<ValueRef>
31+
outputs: Vec<PlaceRef<'ll, 'tcx>>,
32+
mut inputs: Vec<&'ll Value>
3233
) {
3334
let mut ext_constraints = vec![];
3435
let mut output_types = vec![];
@@ -111,7 +112,7 @@ pub fn codegen_inline_asm<'a, 'tcx>(
111112
let kind = llvm::LLVMGetMDKindIDInContext(bx.cx.llcx,
112113
key.as_ptr() as *const c_char, key.len() as c_uint);
113114

114-
let val: llvm::ValueRef = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
115+
let val: &'ll Value = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
115116

116117
llvm::LLVMSetMetadata(r, kind,
117118
llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));

Diff for: src/librustc_codegen_llvm/attributes.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ use rustc_data_structures::fx::FxHashMap;
2222
use rustc_target::spec::PanicStrategy;
2323

2424
use attributes;
25-
use llvm::{self, Attribute, ValueRef};
25+
use llvm::{self, Attribute};
2626
use llvm::AttributePlace::Function;
2727
use llvm_util;
2828
pub use syntax::attr::{self, InlineAttr};
29+
2930
use context::CodegenCx;
31+
use value::Value;
3032

3133
/// Mark LLVM function to use provided inline heuristic.
3234
#[inline]
33-
pub fn inline(val: ValueRef, inline: InlineAttr) {
35+
pub fn inline(val: &'ll Value, inline: InlineAttr) {
3436
use self::InlineAttr::*;
3537
match inline {
3638
Hint => Attribute::InlineHint.apply_llfn(Function, val),
@@ -46,38 +48,38 @@ pub fn inline(val: ValueRef, inline: InlineAttr) {
4648

4749
/// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
4850
#[inline]
49-
pub fn emit_uwtable(val: ValueRef, emit: bool) {
51+
pub fn emit_uwtable(val: &'ll Value, emit: bool) {
5052
Attribute::UWTable.toggle_llfn(Function, val, emit);
5153
}
5254

5355
/// Tell LLVM whether the function can or cannot unwind.
5456
#[inline]
55-
pub fn unwind(val: ValueRef, can_unwind: bool) {
57+
pub fn unwind(val: &'ll Value, can_unwind: bool) {
5658
Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
5759
}
5860

5961
/// Tell LLVM whether it should optimize function for size.
6062
#[inline]
6163
#[allow(dead_code)] // possibly useful function
62-
pub fn set_optimize_for_size(val: ValueRef, optimize: bool) {
64+
pub fn set_optimize_for_size(val: &'ll Value, optimize: bool) {
6365
Attribute::OptimizeForSize.toggle_llfn(Function, val, optimize);
6466
}
6567

6668
/// Tell LLVM if this function should be 'naked', i.e. skip the epilogue and prologue.
6769
#[inline]
68-
pub fn naked(val: ValueRef, is_naked: bool) {
70+
pub fn naked(val: &'ll Value, is_naked: bool) {
6971
Attribute::Naked.toggle_llfn(Function, val, is_naked);
7072
}
7173

72-
pub fn set_frame_pointer_elimination(cx: &CodegenCx, llfn: ValueRef) {
74+
pub fn set_frame_pointer_elimination(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
7375
if cx.sess().must_not_eliminate_frame_pointers() {
7476
llvm::AddFunctionAttrStringValue(
7577
llfn, llvm::AttributePlace::Function,
7678
cstr("no-frame-pointer-elim\0"), cstr("true\0"));
7779
}
7880
}
7981

80-
pub fn set_probestack(cx: &CodegenCx, llfn: ValueRef) {
82+
pub fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
8183
// Only use stack probes if the target specification indicates that we
8284
// should be using stack probes
8385
if !cx.sess().target.target.options.stack_probes {
@@ -123,7 +125,7 @@ pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
123125

124126
/// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
125127
/// attributes.
126-
pub fn from_fn_attrs(cx: &CodegenCx, llfn: ValueRef, id: DefId) {
128+
pub fn from_fn_attrs(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value, id: DefId) {
127129
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(id);
128130

129131
inline(llfn, codegen_fn_attrs.inline);

0 commit comments

Comments
 (0)