Skip to content

Commit cad4095

Browse files
committed
Use layout field of OperandRef and PlaceRef in codegen_intrinsic_call
This avoids having to get the function signature.
1 parent 1c0849d commit cad4095

File tree

3 files changed

+41
-51
lines changed

3 files changed

+41
-51
lines changed

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,9 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
212212
_ => bug!("expected fn item type, found {}", callee_ty),
213213
};
214214

215-
let sig = callee_ty.fn_sig(tcx);
216-
let sig = tcx.normalize_erasing_late_bound_regions(self.typing_env(), sig);
217-
let arg_tys = sig.inputs();
218-
let ret_ty = sig.output();
219215
let name = tcx.item_name(def_id);
220216
let name_str = name.as_str();
221217

222-
let llret_ty = self.layout_of(ret_ty).gcc_type(self);
223-
224218
let simple = get_simple_intrinsic(self, name);
225219
let simple_func = get_simple_function(self, name);
226220

@@ -320,8 +314,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
320314
| sym::rotate_right
321315
| sym::saturating_add
322316
| sym::saturating_sub => {
323-
let ty = arg_tys[0];
324-
match int_type_width_signed(ty, self) {
317+
match int_type_width_signed(args[0].layout.ty, self) {
325318
Some((width, signed)) => match name {
326319
sym::ctlz | sym::cttz => {
327320
let func = self.current_func.borrow().expect("func");
@@ -400,7 +393,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
400393
tcx.dcx().emit_err(InvalidMonomorphization::BasicIntegerType {
401394
span,
402395
name,
403-
ty,
396+
ty: args[0].layout.ty,
404397
});
405398
return Ok(());
406399
}
@@ -492,7 +485,15 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
492485
}
493486

494487
_ if name_str.starts_with("simd_") => {
495-
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
488+
match generic_simd_intrinsic(
489+
self,
490+
name,
491+
callee_ty,
492+
args,
493+
result.layout.ty,
494+
result.layout.gcc_type(self),
495+
span,
496+
) {
496497
Ok(value) => value,
497498
Err(()) => return Ok(()),
498499
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,8 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
175175
bug!("expected fn item type, found {}", callee_ty);
176176
};
177177

178-
let sig = callee_ty.fn_sig(tcx);
179-
let sig = tcx.normalize_erasing_late_bound_regions(self.typing_env(), sig);
180-
let arg_tys = sig.inputs();
181-
let ret_ty = sig.output();
182178
let name = tcx.item_name(def_id);
183179

184-
let llret_ty = self.layout_of(ret_ty).llvm_type(self);
185-
186180
let simple = get_simple_intrinsic(self, name);
187181
let llval = match name {
188182
_ if simple.is_some() => {
@@ -265,22 +259,22 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
265259
BackendRepr::Scalar(scalar) => {
266260
match scalar.primitive() {
267261
Primitive::Int(..) => {
268-
if self.cx().size_of(ret_ty).bytes() < 4 {
262+
if self.cx().size_of(result.layout.ty).bytes() < 4 {
269263
// `va_arg` should not be called on an integer type
270264
// less than 4 bytes in length. If it is, promote
271265
// the integer to an `i32` and truncate the result
272266
// back to the smaller type.
273267
let promoted_result = emit_va_arg(self, args[0], tcx.types.i32);
274-
self.trunc(promoted_result, llret_ty)
268+
self.trunc(promoted_result, result.layout.llvm_type(self))
275269
} else {
276-
emit_va_arg(self, args[0], ret_ty)
270+
emit_va_arg(self, args[0], result.layout.ty)
277271
}
278272
}
279273
Primitive::Float(Float::F16) => {
280274
bug!("the va_arg intrinsic does not work with `f16`")
281275
}
282276
Primitive::Float(Float::F64) | Primitive::Pointer(_) => {
283-
emit_va_arg(self, args[0], ret_ty)
277+
emit_va_arg(self, args[0], result.layout.ty)
284278
}
285279
// `va_arg` should never be used with the return type f32.
286280
Primitive::Float(Float::F32) => {
@@ -384,7 +378,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
384378
| sym::rotate_right
385379
| sym::saturating_add
386380
| sym::saturating_sub => {
387-
let ty = arg_tys[0];
381+
let ty = args[0].layout.ty;
388382
if !ty.is_integral() {
389383
tcx.dcx().emit_err(InvalidMonomorphization::BasicIntegerType {
390384
span,
@@ -403,26 +397,26 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
403397
&[args[0].immediate(), y],
404398
);
405399

406-
self.intcast(ret, llret_ty, false)
400+
self.intcast(ret, result.layout.llvm_type(self), false)
407401
}
408402
sym::ctlz_nonzero => {
409403
let y = self.const_bool(true);
410404
let llvm_name = &format!("llvm.ctlz.i{width}");
411405
let ret = self.call_intrinsic(llvm_name, &[args[0].immediate(), y]);
412-
self.intcast(ret, llret_ty, false)
406+
self.intcast(ret, result.layout.llvm_type(self), false)
413407
}
414408
sym::cttz_nonzero => {
415409
let y = self.const_bool(true);
416410
let llvm_name = &format!("llvm.cttz.i{width}");
417411
let ret = self.call_intrinsic(llvm_name, &[args[0].immediate(), y]);
418-
self.intcast(ret, llret_ty, false)
412+
self.intcast(ret, result.layout.llvm_type(self), false)
419413
}
420414
sym::ctpop => {
421415
let ret = self.call_intrinsic(
422416
&format!("llvm.ctpop.i{width}"),
423417
&[args[0].immediate()],
424418
);
425-
self.intcast(ret, llret_ty, false)
419+
self.intcast(ret, result.layout.llvm_type(self), false)
426420
}
427421
sym::bswap => {
428422
if width == 8 {
@@ -554,16 +548,16 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
554548
// Unpack non-power-of-2 #[repr(packed, simd)] arguments.
555549
// This gives them the expected layout of a regular #[repr(simd)] vector.
556550
let mut loaded_args = Vec::new();
557-
for (ty, arg) in arg_tys.iter().zip(args) {
551+
for arg in args {
558552
loaded_args.push(
559553
// #[repr(packed, simd)] vectors are passed like arrays (as references,
560554
// with reduced alignment and no padding) rather than as immediates.
561555
// We can use a vector load to fix the layout and turn the argument
562556
// into an immediate.
563-
if ty.is_simd()
557+
if arg.layout.ty.is_simd()
564558
&& let OperandValue::Ref(place) = arg.val
565559
{
566-
let (size, elem_ty) = ty.simd_size_and_type(self.tcx());
560+
let (size, elem_ty) = arg.layout.ty.simd_size_and_type(self.tcx());
567561
let elem_ll_ty = match elem_ty.kind() {
568562
ty::Float(f) => self.type_float_from_ty(*f),
569563
ty::Int(i) => self.type_int_from_ty(*i),
@@ -580,10 +574,10 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
580574
);
581575
}
582576

583-
let llret_ty = if ret_ty.is_simd()
584-
&& let BackendRepr::Memory { .. } = self.layout_of(ret_ty).layout.backend_repr
577+
let llret_ty = if result.layout.ty.is_simd()
578+
&& let BackendRepr::Memory { .. } = result.layout.backend_repr
585579
{
586-
let (size, elem_ty) = ret_ty.simd_size_and_type(self.tcx());
580+
let (size, elem_ty) = result.layout.ty.simd_size_and_type(self.tcx());
587581
let elem_ll_ty = match elem_ty.kind() {
588582
ty::Float(f) => self.type_float_from_ty(*f),
589583
ty::Int(i) => self.type_int_from_ty(*i),
@@ -593,7 +587,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
593587
};
594588
self.type_vector(elem_ll_ty, size)
595589
} else {
596-
llret_ty
590+
result.layout.llvm_type(self)
597591
};
598592

599593
match generic_simd_intrinsic(
@@ -602,7 +596,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
602596
callee_ty,
603597
fn_args,
604598
&loaded_args,
605-
ret_ty,
599+
result.layout.ty,
606600
llret_ty,
607601
span,
608602
) {

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6666
span_bug!(span, "expected fn item type, found {}", callee_ty);
6767
};
6868

69-
let sig = callee_ty.fn_sig(bx.tcx());
70-
let sig = bx.tcx().normalize_erasing_late_bound_regions(bx.typing_env(), sig);
71-
let arg_tys = sig.inputs();
72-
let ret_ty = sig.output();
7369
let name = bx.tcx().item_name(def_id);
7470
let name_str = name.as_str();
7571

@@ -97,8 +93,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9793
}
9894
}
9995

100-
let llret_ty = bx.backend_type(bx.layout_of(ret_ty));
101-
10296
let llval = match name {
10397
sym::abort => {
10498
bx.abort();
@@ -153,7 +147,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
153147
| sym::type_name
154148
| sym::variant_count => {
155149
let value = bx.tcx().const_eval_instance(bx.typing_env(), instance, span).unwrap();
156-
OperandRef::from_const(bx, value, ret_ty).immediate_or_packed_pair(bx)
150+
OperandRef::from_const(bx, value, result.layout.ty).immediate_or_packed_pair(bx)
157151
}
158152
sym::arith_offset => {
159153
let ty = fn_args.type_at(0);
@@ -237,7 +231,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
237231
bx.or_disjoint(a, b)
238232
}
239233
sym::exact_div => {
240-
let ty = arg_tys[0];
234+
let ty = args[0].layout.ty;
241235
match int_type_width_signed(ty, bx.tcx()) {
242236
Some((_width, signed)) => {
243237
if signed {
@@ -257,7 +251,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
257251
}
258252
}
259253
sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
260-
match float_type_width(arg_tys[0]) {
254+
match float_type_width(args[0].layout.ty) {
261255
Some(_width) => match name {
262256
sym::fadd_fast => bx.fadd_fast(args[0].immediate(), args[1].immediate()),
263257
sym::fsub_fast => bx.fsub_fast(args[0].immediate(), args[1].immediate()),
@@ -270,7 +264,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
270264
bx.tcx().dcx().emit_err(InvalidMonomorphization::BasicFloatType {
271265
span,
272266
name,
273-
ty: arg_tys[0],
267+
ty: args[0].layout.ty,
274268
});
275269
return Ok(());
276270
}
@@ -280,7 +274,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
280274
| sym::fsub_algebraic
281275
| sym::fmul_algebraic
282276
| sym::fdiv_algebraic
283-
| sym::frem_algebraic => match float_type_width(arg_tys[0]) {
277+
| sym::frem_algebraic => match float_type_width(args[0].layout.ty) {
284278
Some(_width) => match name {
285279
sym::fadd_algebraic => {
286280
bx.fadd_algebraic(args[0].immediate(), args[1].immediate())
@@ -303,31 +297,32 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
303297
bx.tcx().dcx().emit_err(InvalidMonomorphization::BasicFloatType {
304298
span,
305299
name,
306-
ty: arg_tys[0],
300+
ty: args[0].layout.ty,
307301
});
308302
return Ok(());
309303
}
310304
},
311305

312306
sym::float_to_int_unchecked => {
313-
if float_type_width(arg_tys[0]).is_none() {
307+
if float_type_width(args[0].layout.ty).is_none() {
314308
bx.tcx().dcx().emit_err(InvalidMonomorphization::FloatToIntUnchecked {
315309
span,
316-
ty: arg_tys[0],
310+
ty: args[0].layout.ty,
317311
});
318312
return Ok(());
319313
}
320-
let Some((_width, signed)) = int_type_width_signed(ret_ty, bx.tcx()) else {
314+
let Some((_width, signed)) = int_type_width_signed(result.layout.ty, bx.tcx())
315+
else {
321316
bx.tcx().dcx().emit_err(InvalidMonomorphization::FloatToIntUnchecked {
322317
span,
323-
ty: ret_ty,
318+
ty: result.layout.ty,
324319
});
325320
return Ok(());
326321
};
327322
if signed {
328-
bx.fptosi(args[0].immediate(), llret_ty)
323+
bx.fptosi(args[0].immediate(), bx.backend_type(result.layout))
329324
} else {
330-
bx.fptoui(args[0].immediate(), llret_ty)
325+
bx.fptoui(args[0].immediate(), bx.backend_type(result.layout))
331326
}
332327
}
333328

0 commit comments

Comments
 (0)