Skip to content

Commit ce953ec

Browse files
committed
rustc_codegen_ssa: pass the builder through codegen_intrinsic_call by value.
1 parent 58402a9 commit ce953ec

File tree

5 files changed

+104
-81
lines changed

5 files changed

+104
-81
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: Symbol) -> Option<&'ll Va
7575

7676
impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
7777
fn codegen_intrinsic_call(
78-
&mut self,
78+
mut self,
7979
instance: ty::Instance<'tcx>,
8080
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
8181
args: &[OperandRef<'tcx, &'ll Value>],
8282
llresult: &'ll Value,
8383
span: Span,
84-
) {
84+
) -> Self {
8585
let tcx = self.tcx;
8686
let callee_ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
8787

@@ -97,10 +97,10 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
9797
let name = tcx.item_name(def_id);
9898
let name_str = &*name.as_str();
9999

100-
let llret_ty = self.layout_of(ret_ty).llvm_type(self);
100+
let llret_ty = self.layout_of(ret_ty).llvm_type(&self);
101101
let result = PlaceRef::new_sized(llresult, fn_abi.ret.layout);
102102

103-
let simple = get_simple_intrinsic(self, name);
103+
let simple = get_simple_intrinsic(&self, name);
104104
let llval = match name {
105105
_ if simple.is_some() => self.call(
106106
simple.unwrap(),
@@ -118,13 +118,13 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
118118
}
119119
kw::Try => {
120120
try_intrinsic(
121-
self,
121+
&mut self,
122122
args[0].immediate(),
123123
args[1].immediate(),
124124
args[2].immediate(),
125125
llresult,
126126
);
127-
return;
127+
return self;
128128
}
129129
sym::breakpoint => {
130130
let llfn = self.get_intrinsic(&("llvm.debugtrap"));
@@ -135,7 +135,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
135135
self.call(intrinsic, &[args[0].immediate(), args[1].immediate()], None, None)
136136
}
137137
sym::va_arg => {
138-
match fn_abi.ret.layout.abi {
138+
let (new_bx, val) = match fn_abi.ret.layout.abi {
139139
abi::Abi::Scalar(ref scalar) => {
140140
match scalar.value {
141141
Primitive::Int(..) => {
@@ -144,8 +144,10 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
144144
// less than 4 bytes in length. If it is, promote
145145
// the integer to a `i32` and truncate the result
146146
// back to the smaller type.
147-
let promoted_result = emit_va_arg(self, args[0], tcx.types.i32);
148-
self.trunc(promoted_result, llret_ty)
147+
let (mut new_bx, promoted_result) =
148+
emit_va_arg(self, args[0], tcx.types.i32);
149+
let val = new_bx.trunc(promoted_result, llret_ty);
150+
(new_bx, val)
149151
} else {
150152
emit_va_arg(self, args[0], ret_ty)
151153
}
@@ -158,14 +160,16 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
158160
}
159161
}
160162
_ => bug!("the va_arg intrinsic does not work with non-scalar types"),
161-
}
163+
};
164+
self = new_bx;
165+
val
162166
}
163167

164168
sym::volatile_load | sym::unaligned_volatile_load => {
165169
let tp_ty = substs.type_at(0);
166170
let mut ptr = args[0].immediate();
167171
if let PassMode::Cast(ty) = fn_abi.ret.mode {
168-
ptr = self.pointercast(ptr, self.type_ptr_to(ty.llvm_type(self)));
172+
ptr = self.pointercast(ptr, self.type_ptr_to(ty.llvm_type(&self)));
169173
}
170174
let load = self.volatile_load(ptr);
171175
let align = if name == sym::unaligned_volatile_load {
@@ -180,13 +184,13 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
180184
}
181185
sym::volatile_store => {
182186
let dst = args[0].deref(self.cx());
183-
args[1].val.volatile_store(self, dst);
184-
return;
187+
args[1].val.volatile_store(&mut self, dst);
188+
return self;
185189
}
186190
sym::unaligned_volatile_store => {
187191
let dst = args[0].deref(self.cx());
188-
args[1].val.unaligned_volatile_store(self, dst);
189-
return;
192+
args[1].val.unaligned_volatile_store(&mut self, dst);
193+
return self;
190194
}
191195
sym::prefetch_read_data
192196
| sym::prefetch_write_data
@@ -224,7 +228,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
224228
| sym::saturating_add
225229
| sym::saturating_sub => {
226230
let ty = arg_tys[0];
227-
match int_type_width_signed(ty, self) {
231+
match int_type_width_signed(ty, &self) {
228232
Some((width, signed)) => match name {
229233
sym::ctlz | sym::cttz => {
230234
let y = self.const_bool(false);
@@ -296,15 +300,17 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
296300
name, ty
297301
),
298302
);
299-
return;
303+
return self;
300304
}
301305
}
302306
}
303307

304308
_ if name_str.starts_with("simd_") => {
305-
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
309+
match generic_simd_intrinsic(
310+
&mut self, name, callee_ty, args, ret_ty, llret_ty, span,
311+
) {
306312
Ok(llval) => llval,
307-
Err(()) => return,
313+
Err(()) => return self,
308314
}
309315
}
310316

@@ -313,15 +319,17 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
313319

314320
if !fn_abi.ret.is_ignore() {
315321
if let PassMode::Cast(ty) = fn_abi.ret.mode {
316-
let ptr_llty = self.type_ptr_to(ty.llvm_type(self));
322+
let ptr_llty = self.type_ptr_to(ty.llvm_type(&self));
317323
let ptr = self.pointercast(result.llval, ptr_llty);
318324
self.store(llval, ptr, result.align);
319325
} else {
320-
OperandRef::from_immediate_or_packed_pair(self, llval, result.layout)
326+
OperandRef::from_immediate_or_packed_pair(&mut self, llval, result.layout)
321327
.val
322-
.store(self, result);
328+
.store(&mut self, result);
323329
}
324330
}
331+
332+
self
325333
}
326334

327335
fn abort(&mut self) {

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,39 +169,49 @@ fn emit_aapcs_va_arg(
169169
}
170170

171171
pub(super) fn emit_va_arg(
172-
bx: &mut Builder<'a, 'll, 'tcx>,
172+
mut bx: Builder<'a, 'll, 'tcx>,
173173
addr: OperandRef<'tcx, &'ll Value>,
174174
target_ty: Ty<'tcx>,
175-
) -> &'ll Value {
175+
) -> (Builder<'a, 'll, 'tcx>, &'ll Value) {
176176
// Determine the va_arg implementation to use. The LLVM va_arg instruction
177177
// is lacking in some instances, so we should only use it as a fallback.
178178
let target = &bx.cx.tcx.sess.target;
179179
let arch = &bx.cx.tcx.sess.target.arch;
180-
match &**arch {
180+
let val = match &**arch {
181181
// Windows x86
182182
"x86" if target.is_like_windows => {
183-
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), false)
183+
emit_ptr_va_arg(&mut bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), false)
184184
}
185185
// Generic x86
186-
"x86" => emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), true),
186+
"x86" => {
187+
emit_ptr_va_arg(&mut bx, addr, target_ty, false, Align::from_bytes(4).unwrap(), true)
188+
}
187189
// Windows AArch64
188190
"aarch64" if target.is_like_windows => {
189-
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), false)
191+
emit_ptr_va_arg(&mut bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), false)
190192
}
191193
// macOS / iOS AArch64
192194
"aarch64" if target.is_like_osx => {
193-
emit_ptr_va_arg(bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), true)
195+
emit_ptr_va_arg(&mut bx, addr, target_ty, false, Align::from_bytes(8).unwrap(), true)
194196
}
195-
"aarch64" => emit_aapcs_va_arg(bx, addr, target_ty),
197+
"aarch64" => emit_aapcs_va_arg(&mut bx, addr, target_ty),
196198
// Windows x86_64
197199
"x86_64" if target.is_like_windows => {
198200
let target_ty_size = bx.cx.size_of(target_ty).bytes();
199201
let indirect: bool = target_ty_size > 8 || !target_ty_size.is_power_of_two();
200-
emit_ptr_va_arg(bx, addr, target_ty, indirect, Align::from_bytes(8).unwrap(), false)
202+
emit_ptr_va_arg(
203+
&mut bx,
204+
addr,
205+
target_ty,
206+
indirect,
207+
Align::from_bytes(8).unwrap(),
208+
false,
209+
)
201210
}
202211
// For all other architecture/OS combinations fall back to using
203212
// the LLVM va_arg instruction.
204213
// https://llvm.org/docs/LangRef.html#va-arg-instruction
205214
_ => bx.va_arg(addr.immediate(), bx.cx.layout_of(target_ty).llvm_type(bx.cx)),
206-
}
215+
};
216+
(bx, val)
207217
}

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
654654
})
655655
.collect();
656656

657-
Self::codegen_intrinsic_call(
658-
&mut bx,
657+
bx = Self::codegen_intrinsic_call(
658+
bx,
659659
*instance.as_ref().unwrap(),
660660
&fn_abi,
661661
&args,

0 commit comments

Comments
 (0)