Skip to content

Commit 1f717ae

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

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 ret_llval = |bx: &mut Bx, llval| {
10397
if result.layout.ty.is_bool() {
10498
OperandRef::from_immediate_or_packed_pair(bx, llval, result.layout)
@@ -164,7 +158,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
164158
| sym::type_name
165159
| sym::variant_count => {
166160
let value = bx.tcx().const_eval_instance(bx.typing_env(), instance, span).unwrap();
167-
OperandRef::from_const(bx, value, ret_ty).immediate_or_packed_pair(bx)
161+
OperandRef::from_const(bx, value, result.layout.ty).immediate_or_packed_pair(bx)
168162
}
169163
sym::arith_offset => {
170164
let ty = fn_args.type_at(0);
@@ -248,7 +242,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
248242
bx.or_disjoint(a, b)
249243
}
250244
sym::exact_div => {
251-
let ty = arg_tys[0];
245+
let ty = args[0].layout.ty;
252246
match int_type_width_signed(ty, bx.tcx()) {
253247
Some((_width, signed)) => {
254248
if signed {
@@ -268,7 +262,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
268262
}
269263
}
270264
sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
271-
match float_type_width(arg_tys[0]) {
265+
match float_type_width(args[0].layout.ty) {
272266
Some(_width) => match name {
273267
sym::fadd_fast => bx.fadd_fast(args[0].immediate(), args[1].immediate()),
274268
sym::fsub_fast => bx.fsub_fast(args[0].immediate(), args[1].immediate()),
@@ -281,7 +275,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
281275
bx.tcx().dcx().emit_err(InvalidMonomorphization::BasicFloatType {
282276
span,
283277
name,
284-
ty: arg_tys[0],
278+
ty: args[0].layout.ty,
285279
});
286280
return Ok(());
287281
}
@@ -291,7 +285,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
291285
| sym::fsub_algebraic
292286
| sym::fmul_algebraic
293287
| sym::fdiv_algebraic
294-
| sym::frem_algebraic => match float_type_width(arg_tys[0]) {
288+
| sym::frem_algebraic => match float_type_width(args[0].layout.ty) {
295289
Some(_width) => match name {
296290
sym::fadd_algebraic => {
297291
bx.fadd_algebraic(args[0].immediate(), args[1].immediate())
@@ -314,31 +308,32 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
314308
bx.tcx().dcx().emit_err(InvalidMonomorphization::BasicFloatType {
315309
span,
316310
name,
317-
ty: arg_tys[0],
311+
ty: args[0].layout.ty,
318312
});
319313
return Ok(());
320314
}
321315
},
322316

323317
sym::float_to_int_unchecked => {
324-
if float_type_width(arg_tys[0]).is_none() {
318+
if float_type_width(args[0].layout.ty).is_none() {
325319
bx.tcx().dcx().emit_err(InvalidMonomorphization::FloatToIntUnchecked {
326320
span,
327-
ty: arg_tys[0],
321+
ty: args[0].layout.ty,
328322
});
329323
return Ok(());
330324
}
331-
let Some((_width, signed)) = int_type_width_signed(ret_ty, bx.tcx()) else {
325+
let Some((_width, signed)) = int_type_width_signed(result.layout.ty, bx.tcx())
326+
else {
332327
bx.tcx().dcx().emit_err(InvalidMonomorphization::FloatToIntUnchecked {
333328
span,
334-
ty: ret_ty,
329+
ty: result.layout.ty,
335330
});
336331
return Ok(());
337332
};
338333
if signed {
339-
bx.fptosi(args[0].immediate(), llret_ty)
334+
bx.fptosi(args[0].immediate(), bx.backend_type(result.layout))
340335
} else {
341-
bx.fptoui(args[0].immediate(), llret_ty)
336+
bx.fptoui(args[0].immediate(), bx.backend_type(result.layout))
342337
}
343338
}
344339

0 commit comments

Comments
 (0)