Skip to content

Commit 6809e65

Browse files
authored
Rollup merge of #165804 - Zalathar:llval, r=tmiasko
cg_llvm: Use an invariant lifetime brand for LLVM values This PR changes the type of LLVM value references from `&'ll Value` to `Value<'ll>` with an invariant lifetime brand, to help uphold soundness of the FFI bindings. There should be no change to compiler output. --- Value is one of the more widely-used types, much more so than Type, so unfortunately this step ends up needing to touch quite a lot of code. Thankfully almost all of the changes are trivial.
2 parents ee8268e + 6c0a3df commit 6809e65

32 files changed

Lines changed: 1123 additions & 1085 deletions

‎compiler/rustc_codegen_llvm/src/abi.rs‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ use crate::llvm::{self, Attribute, AttributePlace, Type, Value};
2727
use crate::type_of::LayoutLlvmExt;
2828

2929
trait ArgAttributesExt {
30-
fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value);
31-
fn apply_attrs_to_callsite(
30+
fn apply_attrs_to_llfn<'ll>(
3231
&self,
3332
idx: AttributePlace,
34-
cx: &CodegenCx<'_, '_>,
35-
callsite: &Value,
33+
cx: &CodegenCx<'ll, '_>,
34+
llfn: Value<'ll>,
35+
);
36+
fn apply_attrs_to_callsite<'ll>(
37+
&self,
38+
idx: AttributePlace,
39+
cx: &CodegenCx<'ll, '_>,
40+
callsite: Value<'ll>,
3641
);
3742
}
3843

@@ -116,16 +121,21 @@ fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'
116121
}
117122

118123
impl ArgAttributesExt for ArgAttributes {
119-
fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value) {
124+
fn apply_attrs_to_llfn<'ll>(
125+
&self,
126+
idx: AttributePlace,
127+
cx: &CodegenCx<'ll, '_>,
128+
llfn: Value<'ll>,
129+
) {
120130
let attrs = get_attrs(self, cx);
121131
attributes::apply_to_llfn(llfn, idx, &attrs);
122132
}
123133

124-
fn apply_attrs_to_callsite(
134+
fn apply_attrs_to_callsite<'ll>(
125135
&self,
126136
idx: AttributePlace,
127-
cx: &CodegenCx<'_, '_>,
128-
callsite: &Value,
137+
cx: &CodegenCx<'ll, '_>,
138+
callsite: Value<'ll>,
129139
) {
130140
let attrs = get_attrs(self, cx);
131141
attributes::apply_to_callsite(callsite, idx, &attrs);
@@ -236,14 +246,14 @@ trait ArgAbiExt<'ll, 'tcx> {
236246
fn store(
237247
&self,
238248
bx: &mut Builder<'_, 'll, 'tcx>,
239-
val: &'ll Value,
240-
dst: PlaceRef<'tcx, &'ll Value>,
249+
val: Value<'ll>,
250+
dst: PlaceRef<'tcx, Value<'ll>>,
241251
);
242252
fn store_fn_arg(
243253
&self,
244254
bx: &mut Builder<'_, 'll, 'tcx>,
245255
idx: &mut usize,
246-
dst: PlaceRef<'tcx, &'ll Value>,
256+
dst: PlaceRef<'tcx, Value<'ll>>,
247257
);
248258
}
249259

@@ -255,8 +265,8 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
255265
fn store(
256266
&self,
257267
bx: &mut Builder<'_, 'll, 'tcx>,
258-
val: &'ll Value,
259-
dst: PlaceRef<'tcx, &'ll Value>,
268+
val: Value<'ll>,
269+
dst: PlaceRef<'tcx, Value<'ll>>,
260270
) {
261271
match &self.mode {
262272
PassMode::Ignore => {}
@@ -310,7 +320,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
310320
&self,
311321
bx: &mut Builder<'_, 'll, 'tcx>,
312322
idx: &mut usize,
313-
dst: PlaceRef<'tcx, &'ll Value>,
323+
dst: PlaceRef<'tcx, Value<'ll>>,
314324
) {
315325
let mut next = || {
316326
let val = llvm::get_param(bx.llfn(), *idx as c_uint);
@@ -347,8 +357,8 @@ impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
347357
fn store_arg(
348358
&mut self,
349359
arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
350-
val: &'ll Value,
351-
dst: PlaceRef<'tcx, &'ll Value>,
360+
val: Value<'ll>,
361+
dst: PlaceRef<'tcx, Value<'ll>>,
352362
) {
353363
arg_abi.store(self, val, dst)
354364
}
@@ -363,12 +373,12 @@ pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
363373
fn apply_attrs_llfn(
364374
&self,
365375
cx: &CodegenCx<'ll, 'tcx>,
366-
llfn: &'ll Value,
376+
llfn: Value<'ll>,
367377
instance: Option<ty::Instance<'tcx>>,
368378
);
369379

370380
/// Apply attributes to a function call.
371-
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
381+
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: Value<'ll>);
372382
}
373383

374384
impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
@@ -470,7 +480,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
470480
fn apply_attrs_llfn(
471481
&self,
472482
cx: &CodegenCx<'ll, 'tcx>,
473-
llfn: &'ll Value,
483+
llfn: Value<'ll>,
474484
instance: Option<ty::Instance<'tcx>>,
475485
) {
476486
let mut func_attrs = SmallVec::<[_; 3]>::new();
@@ -648,7 +658,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
648658
}
649659
}
650660

651-
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
661+
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: Value<'ll>) {
652662
let mut func_attrs = SmallVec::<[_; 2]>::new();
653663
if self.ret.layout.is_uninhabited() {
654664
func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));
@@ -659,7 +669,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
659669
attributes::apply_to_callsite(callsite, llvm::AttributePlace::Function, &{ func_attrs });
660670

661671
let mut i = 0;
662-
let mut apply = |cx: &CodegenCx<'_, '_>, attrs: &ArgAttributes| {
672+
let mut apply = |cx: &CodegenCx<'ll, '_>, attrs: &ArgAttributes| {
663673
attrs.apply_attrs_to_callsite(llvm::AttributePlace::Argument(i), cx, callsite);
664674
i += 1;
665675
i - 1

‎compiler/rustc_codegen_llvm/src/asm.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ pub(crate) fn inline_asm_call<'ll>(
537537
bx: &mut Builder<'_, 'll, '_>,
538538
asm: &str,
539539
cons: &str,
540-
inputs: &[&'ll Value],
540+
inputs: &[Value<'ll>],
541541
output: llvm::Type<'ll>,
542542
labels: &[&'ll llvm::BasicBlock],
543543
volatile: bool,
@@ -547,7 +547,7 @@ pub(crate) fn inline_asm_call<'ll>(
547547
unwind: bool,
548548
dest: Option<&'ll llvm::BasicBlock>,
549549
catch_funclet: Option<(&'ll llvm::BasicBlock, Option<&Funclet<'ll>>)>,
550-
) -> Option<&'ll Value> {
550+
) -> Option<Value<'ll>> {
551551
let argtys = inputs
552552
.iter()
553553
.map(|v| {
@@ -1170,11 +1170,11 @@ fn any_target_feature_enabled(
11701170
/// Fix up an input value to work around LLVM bugs.
11711171
fn llvm_fixup_input<'ll, 'tcx>(
11721172
bx: &mut Builder<'_, 'll, 'tcx>,
1173-
mut value: &'ll Value,
1173+
mut value: Value<'ll>,
11741174
reg: InlineAsmRegClass,
11751175
layout: &TyAndLayout<'tcx>,
11761176
instance: Instance<'_>,
1177-
) -> &'ll Value {
1177+
) -> Value<'ll> {
11781178
use InlineAsmRegClass::*;
11791179
let dl = &bx.tcx.data_layout;
11801180
match (reg, layout.backend_repr) {
@@ -1373,11 +1373,11 @@ fn llvm_fixup_input<'ll, 'tcx>(
13731373
/// Fix up an output value to work around LLVM bugs.
13741374
fn llvm_fixup_output<'ll, 'tcx>(
13751375
bx: &mut Builder<'_, 'll, 'tcx>,
1376-
mut value: &'ll Value,
1376+
mut value: Value<'ll>,
13771377
reg: InlineAsmRegClass,
13781378
layout: &TyAndLayout<'tcx>,
13791379
instance: Instance<'_>,
1380-
) -> &'ll Value {
1380+
) -> Value<'ll> {
13811381
use InlineAsmRegClass::*;
13821382
match (reg, layout.backend_repr) {
13831383
(AArch64(AArch64InlineAsmRegClass::vreg), BackendRepr::Scalar(s)) => {

‎compiler/rustc_codegen_llvm/src/attributes.rs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ use crate::llvm::{
2626
};
2727
use crate::{attributes, llvm_util};
2828

29-
pub(crate) fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
29+
pub(crate) fn apply_to_llfn<'ll>(llfn: Value<'ll>, idx: AttributePlace, attrs: &[&'ll Attribute]) {
3030
if !attrs.is_empty() {
3131
llvm::AddFunctionAttributes(llfn, idx, attrs);
3232
}
3333
}
3434

35-
pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
35+
pub(crate) fn apply_to_callsite<'ll>(
36+
callsite: Value<'ll>,
37+
idx: AttributePlace,
38+
attrs: &[&'ll Attribute],
39+
) {
3640
if !attrs.is_empty() {
3741
llvm::AddCallSiteAttributes(callsite, idx, attrs);
3842
}
3943
}
4044

41-
pub(crate) fn has_string_attr(llfn: &Value, name: &str) -> bool {
45+
pub(crate) fn has_string_attr<'ll>(llfn: Value<'ll>, name: &str) -> bool {
4246
llvm::HasStringAttribute(llfn, name)
4347
}
4448

45-
pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
49+
pub(crate) fn remove_string_attr_from_llfn<'ll>(llfn: Value<'ll>, name: &str) {
4650
llvm::RemoveStringAttrFromFn(llfn, name);
4751
}
4852

@@ -502,7 +506,7 @@ fn create_alloc_family_attr<'ll>(llcx: llvm::Context<'ll>) -> &'ll llvm::Attribu
502506
pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
503507
cx: &SimpleCx<'ll>,
504508
tcx: TyCtxt<'tcx>,
505-
llfn: &'ll Value,
509+
llfn: Value<'ll>,
506510
codegen_fn_attrs: &CodegenFnAttrs,
507511
instance: Option<ty::Instance<'tcx>>,
508512
sanitizer_ignorelist: Option<&SanitizerIgnoreList>,

‎compiler/rustc_codegen_llvm/src/back/write.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ pub(crate) unsafe fn llvm_optimize<'ll>(
659659
// Returns the new function, or `None` if nothing was wrapped
660660
fn handle_offload<'ll>(
661661
cx: &SimpleCx<'ll>,
662-
old_fn: &'ll llvm::Value,
663-
) -> Option<&'ll llvm::Value> {
662+
old_fn: llvm::Value<'ll>,
663+
) -> Option<llvm::Value<'ll>> {
664664
let old_fn_ty = cx.get_type_of_global(old_fn);
665665
let old_param_types = cx.func_params_types(old_fn_ty);
666666
let old_param_count = old_param_types.len();

‎compiler/rustc_codegen_llvm/src/base.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ use crate::llvm::{self, Value};
3434
use crate::{ModuleLlvm, attributes};
3535

3636
pub(crate) struct ValueIter<'ll> {
37-
cur: Option<&'ll Value>,
38-
step: unsafe extern "C" fn(&'ll Value) -> Option<&'ll Value>,
37+
cur: Option<Value<'ll>>,
38+
step: unsafe extern "C" fn(Value<'ll>) -> Option<Value<'ll>>,
3939
}
4040

4141
impl<'ll> Iterator for ValueIter<'ll> {
42-
type Item = &'ll Value;
42+
type Item = Value<'ll>;
4343

44-
fn next(&mut self) -> Option<&'ll Value> {
44+
fn next(&mut self) -> Option<Value<'ll>> {
4545
let old = self.cur;
4646
if let Some(old) = old {
4747
self.cur = unsafe { (self.step)(old) };
@@ -221,7 +221,7 @@ pub(crate) fn compile_codegen_unit(
221221
(module, cost)
222222
}
223223

224-
pub(crate) fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
224+
pub(crate) fn set_link_section<'ll>(llval: Value<'ll>, attrs: &CodegenFnAttrs) {
225225
let Some(sect) = attrs.link_section else { return };
226226
let buf = SmallCStr::new(sect.as_str());
227227
llvm::set_section(llval, &buf);
@@ -248,7 +248,7 @@ pub(crate) fn visibility_to_llvm(linkage: Visibility) -> llvm::Visibility {
248248
}
249249
}
250250

251-
pub(crate) fn set_variable_sanitizer_attrs(llval: &Value, attrs: &CodegenFnAttrs) {
251+
pub(crate) fn set_variable_sanitizer_attrs<'ll>(llval: Value<'ll>, attrs: &CodegenFnAttrs) {
252252
if attrs.sanitizers.disabled.contains(SanitizerSet::ADDRESS)
253253
|| attrs.sanitizers.disabled.contains(SanitizerSet::KERNELADDRESS)
254254
{

0 commit comments

Comments
 (0)