Skip to content

Commit cdb012e

Browse files
authored
Rollup merge of #115240 - RalfJung:llvm-no-type, r=bjorn3
codegen_llvm/llvm_type: avoid matching on the Rust type This `match` is highly suspicious. Looking at `scalar_llvm_type_at` I think it makes no difference. But if it were to make a difference that would be a huge problem, since it doesn't look through `repr(transparent)`! Cc `@eddyb` `@bjorn3`
2 parents ec4a85e + 0d2cd6f commit cdb012e

File tree

3 files changed

+17
-37
lines changed

3 files changed

+17
-37
lines changed

Diff for: src/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
125125
PassMode::Ignore => continue,
126126
PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
127127
PassMode::Pair(..) => {
128-
argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0, true));
129-
argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1, true));
128+
argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0));
129+
argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1));
130130
continue;
131131
}
132132
PassMode::Indirect { extra_attrs: Some(_), .. } => {

Diff for: src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
821821

822822
let mut load = |i, scalar: &abi::Scalar, align| {
823823
let llptr = self.struct_gep(pair_type, place.llval, i as u64);
824-
let llty = place.layout.scalar_pair_element_gcc_type(self, i, false);
824+
let llty = place.layout.scalar_pair_element_gcc_type(self, i);
825825
let load = self.load(llty, llptr, align);
826826
scalar_load_metadata(self, load, scalar);
827827
if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }

Diff for: src/type_of.rs

+14-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use gccjit::{Struct, Type};
44
use crate::rustc_codegen_ssa::traits::{BaseTypeMethods, DerivedTypeMethods, LayoutTypeMethods};
55
use rustc_middle::bug;
66
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
7-
use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
7+
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
88
use rustc_middle::ty::print::with_no_trimmed_paths;
99
use rustc_target::abi::{self, Abi, Align, F32, F64, FieldsShape, Int, Integer, Pointer, PointeeInfo, Size, TyAbiInterface, Variants};
1010
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
@@ -74,8 +74,8 @@ fn uncached_gcc_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout
7474
Abi::ScalarPair(..) => {
7575
return cx.type_struct(
7676
&[
77-
layout.scalar_pair_element_gcc_type(cx, 0, false),
78-
layout.scalar_pair_element_gcc_type(cx, 1, false),
77+
layout.scalar_pair_element_gcc_type(cx, 0),
78+
layout.scalar_pair_element_gcc_type(cx, 1),
7979
],
8080
false,
8181
);
@@ -150,7 +150,7 @@ pub trait LayoutGccExt<'tcx> {
150150
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
151151
fn immediate_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
152152
fn scalar_gcc_type_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, scalar: &abi::Scalar, offset: Size) -> Type<'gcc>;
153-
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc>;
153+
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize) -> Type<'gcc>;
154154
fn gcc_field_index(&self, index: usize) -> u64;
155155
fn pointee_info_at<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, offset: Size) -> Option<PointeeInfo>;
156156
}
@@ -182,23 +182,16 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
182182
/// of that field's type - this is useful for taking the address of
183183
/// that field and ensuring the struct has the right alignment.
184184
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
185+
// This must produce the same result for `repr(transparent)` wrappers as for the inner type!
186+
// In other words, this should generally not look at the type at all, but only at the
187+
// layout.
185188
if let Abi::Scalar(ref scalar) = self.abi {
186189
// Use a different cache for scalars because pointers to DSTs
187190
// can be either fat or thin (data pointers of fat pointers).
188191
if let Some(&ty) = cx.scalar_types.borrow().get(&self.ty) {
189192
return ty;
190193
}
191-
let ty =
192-
match *self.ty.kind() {
193-
ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
194-
cx.type_ptr_to(cx.layout_of(ty).gcc_type(cx))
195-
}
196-
ty::Adt(def, _) if def.is_box() => {
197-
cx.type_ptr_to(cx.layout_of(self.ty.boxed_ty()).gcc_type(cx))
198-
}
199-
ty::FnPtr(sig) => cx.fn_ptr_backend_type(&cx.fn_abi_of_fn_ptr(sig, ty::List::empty())),
200-
_ => self.scalar_gcc_type_at(cx, scalar, Size::ZERO),
201-
};
194+
let ty = self.scalar_gcc_type_at(cx, scalar, Size::ZERO);
202195
cx.scalar_types.borrow_mut().insert(self.ty, ty);
203196
return ty;
204197
}
@@ -272,23 +265,10 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
272265
}
273266
}
274267

275-
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
276-
// TODO(antoyo): remove llvm hack:
277-
// HACK(eddyb) special-case fat pointers until LLVM removes
278-
// pointee types, to avoid bitcasting every `OperandRef::deref`.
279-
match self.ty.kind() {
280-
ty::Ref(..) | ty::RawPtr(_) => {
281-
return self.field(cx, index).gcc_type(cx);
282-
}
283-
// only wide pointer boxes are handled as pointers
284-
// thin pointer boxes with scalar allocators are handled by the general logic below
285-
ty::Adt(def, args) if def.is_box() && cx.layout_of(args.type_at(1)).is_zst() => {
286-
let ptr_ty = Ty::new_mut_ptr(cx.tcx,self.ty.boxed_ty());
287-
return cx.layout_of(ptr_ty).scalar_pair_element_gcc_type(cx, index, immediate);
288-
}
289-
_ => {}
290-
}
291-
268+
fn scalar_pair_element_gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, 'tcx>, index: usize) -> Type<'gcc> {
269+
// This must produce the same result for `repr(transparent)` wrappers as for the inner type!
270+
// In other words, this should generally not look at the type at all, but only at the
271+
// layout.
292272
let (a, b) = match self.abi {
293273
Abi::ScalarPair(ref a, ref b) => (a, b),
294274
_ => bug!("TyAndLayout::scalar_pair_element_llty({:?}): not applicable", self),
@@ -367,8 +347,8 @@ impl<'gcc, 'tcx> LayoutTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
367347
layout.gcc_field_index(index)
368348
}
369349

370-
fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, immediate: bool) -> Type<'gcc> {
371-
layout.scalar_pair_element_gcc_type(self, index, immediate)
350+
fn scalar_pair_element_backend_type(&self, layout: TyAndLayout<'tcx>, index: usize, _immediate: bool) -> Type<'gcc> {
351+
layout.scalar_pair_element_gcc_type(self, index)
372352
}
373353

374354
fn cast_backend_type(&self, ty: &CastTarget) -> Type<'gcc> {

0 commit comments

Comments
 (0)