Skip to content

Commit 1ae0d90

Browse files
authored
Rollup merge of #124797 - beetrees:primitive-float, r=davidtwco
Refactor float `Primitive`s to a separate `Float` type Now there are 4 of them, it makes sense to refactor `F16`, `F32`, `F64` and `F128` out of `Primitive` and into a separate `Float` type (like integers already are). This allows patterns like `F16 | F32 | F64 | F128` to be simplified into `Float(_)`, and is consistent with `ty::FloatTy`. As a side effect, this PR also makes the `Ty::primitive_size` method work with `f16` and `f128`. Tracking issue: #116909 `@rustbot` label +F-f16_and_f128
2 parents f605174 + 3769fdd commit 1ae0d90

File tree

23 files changed

+155
-95
lines changed

23 files changed

+155
-95
lines changed

Diff for: compiler/rustc_abi/src/lib.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,41 @@ impl Integer {
926926
}
927927
}
928928

929+
/// Floating-point types.
930+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
931+
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
932+
pub enum Float {
933+
F16,
934+
F32,
935+
F64,
936+
F128,
937+
}
938+
939+
impl Float {
940+
pub fn size(self) -> Size {
941+
use Float::*;
942+
943+
match self {
944+
F16 => Size::from_bits(16),
945+
F32 => Size::from_bits(32),
946+
F64 => Size::from_bits(64),
947+
F128 => Size::from_bits(128),
948+
}
949+
}
950+
951+
pub fn align<C: HasDataLayout>(self, cx: &C) -> AbiAndPrefAlign {
952+
use Float::*;
953+
let dl = cx.data_layout();
954+
955+
match self {
956+
F16 => dl.f16_align,
957+
F32 => dl.f32_align,
958+
F64 => dl.f64_align,
959+
F128 => dl.f128_align,
960+
}
961+
}
962+
}
963+
929964
/// Fundamental unit of memory access and layout.
930965
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
931966
#[cfg_attr(feature = "nightly", derive(HashStable_Generic))]
@@ -938,10 +973,7 @@ pub enum Primitive {
938973
/// a negative integer passed by zero-extension will appear positive in
939974
/// the callee, and most operations on it will produce the wrong values.
940975
Int(Integer, bool),
941-
F16,
942-
F32,
943-
F64,
944-
F128,
976+
Float(Float),
945977
Pointer(AddressSpace),
946978
}
947979

@@ -952,10 +984,7 @@ impl Primitive {
952984

953985
match self {
954986
Int(i, _) => i.size(),
955-
F16 => Size::from_bits(16),
956-
F32 => Size::from_bits(32),
957-
F64 => Size::from_bits(64),
958-
F128 => Size::from_bits(128),
987+
Float(f) => f.size(),
959988
// FIXME(erikdesjardins): ignoring address space is technically wrong, pointers in
960989
// different address spaces can have different sizes
961990
// (but TargetDataLayout doesn't currently parse that part of the DL string)
@@ -969,10 +998,7 @@ impl Primitive {
969998

970999
match self {
9711000
Int(i, _) => i.align(dl),
972-
F16 => dl.f16_align,
973-
F32 => dl.f32_align,
974-
F64 => dl.f64_align,
975-
F128 => dl.f128_align,
1001+
Float(f) => f.align(dl),
9761002
// FIXME(erikdesjardins): ignoring address space is technically wrong, pointers in
9771003
// different address spaces can have different alignments
9781004
// (but TargetDataLayout doesn't currently parse that part of the DL string)

Diff for: compiler/rustc_codegen_cranelift/src/common.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::layout::{
77
use rustc_middle::ty::TypeFoldable;
88
use rustc_span::source_map::Spanned;
99
use rustc_target::abi::call::FnAbi;
10-
use rustc_target::abi::{Integer, Primitive};
10+
use rustc_target::abi::{Float, Integer, Primitive};
1111
use rustc_target::spec::{HasTargetSpec, Target};
1212

1313
use crate::constant::ConstantCx;
@@ -32,10 +32,12 @@ pub(crate) fn scalar_to_clif_type(tcx: TyCtxt<'_>, scalar: Scalar) -> Type {
3232
Integer::I64 => types::I64,
3333
Integer::I128 => types::I128,
3434
},
35-
Primitive::F16 => unimplemented!("f16_f128"),
36-
Primitive::F32 => types::F32,
37-
Primitive::F64 => types::F64,
38-
Primitive::F128 => unimplemented!("f16_f128"),
35+
Primitive::Float(float) => match float {
36+
Float::F16 => unimplemented!("f16_f128"),
37+
Float::F32 => types::F32,
38+
Float::F64 => types::F64,
39+
Float::F128 => unimplemented!("f16_f128"),
40+
},
3941
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
4042
Primitive::Pointer(_) => pointer_ty(tcx),
4143
}

Diff for: compiler/rustc_codegen_gcc/src/type_of.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
88
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
99
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
1010
use rustc_target::abi::{
11-
self, Abi, Align, FieldsShape, Int, Integer, PointeeInfo, Pointer, Size, TyAbiInterface,
12-
Variants, F128, F16, F32, F64,
11+
self, Abi, Align, FieldsShape, Float, Int, Integer, PointeeInfo, Pointer, Size, TyAbiInterface,
12+
Variants,
1313
};
1414

1515
use crate::abi::{FnAbiGcc, FnAbiGccExt, GccType};
@@ -283,10 +283,7 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
283283
match scalar.primitive() {
284284
Int(i, true) => cx.type_from_integer(i),
285285
Int(i, false) => cx.type_from_unsigned_integer(i),
286-
F16 => cx.type_f16(),
287-
F32 => cx.type_f32(),
288-
F64 => cx.type_f64(),
289-
F128 => cx.type_f128(),
286+
Float(f) => cx.type_from_float(f),
290287
Pointer(address_space) => {
291288
// If we know the alignment, pick something better than i8.
292289
let pointee = if let Some(pointee) = self.pointee_info_at(cx, offset) {

Diff for: compiler/rustc_codegen_llvm/src/asm.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,8 @@ fn llvm_asm_scalar_type<'ll>(cx: &CodegenCx<'ll, '_>, scalar: Scalar) -> &'ll Ty
904904
Primitive::Int(Integer::I16, _) => cx.type_i16(),
905905
Primitive::Int(Integer::I32, _) => cx.type_i32(),
906906
Primitive::Int(Integer::I64, _) => cx.type_i64(),
907-
Primitive::F32 => cx.type_f32(),
908-
Primitive::F64 => cx.type_f64(),
907+
Primitive::Float(Float::F32) => cx.type_f32(),
908+
Primitive::Float(Float::F64) => cx.type_f64(),
909909
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
910910
Primitive::Pointer(_) => cx.type_from_integer(dl.ptr_sized_integer()),
911911
_ => unreachable!(),
@@ -950,7 +950,7 @@ fn llvm_fixup_input<'ll, 'tcx>(
950950
bx.shuffle_vector(value, bx.const_undef(vec_ty), bx.const_vector(&indices))
951951
}
952952
(InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd), Abi::Scalar(s))
953-
if s.primitive() == Primitive::F64 =>
953+
if s.primitive() == Primitive::Float(Float::F64) =>
954954
{
955955
bx.bitcast(value, bx.cx.type_i64())
956956
}
@@ -986,8 +986,8 @@ fn llvm_fixup_input<'ll, 'tcx>(
986986
match s.primitive() {
987987
// MIPS only supports register-length arithmetics.
988988
Primitive::Int(Integer::I8 | Integer::I16, _) => bx.zext(value, bx.cx.type_i32()),
989-
Primitive::F32 => bx.bitcast(value, bx.cx.type_i32()),
990-
Primitive::F64 => bx.bitcast(value, bx.cx.type_i64()),
989+
Primitive::Float(Float::F32) => bx.bitcast(value, bx.cx.type_i32()),
990+
Primitive::Float(Float::F64) => bx.bitcast(value, bx.cx.type_i64()),
991991
_ => value,
992992
}
993993
}
@@ -1027,7 +1027,7 @@ fn llvm_fixup_output<'ll, 'tcx>(
10271027
bx.shuffle_vector(value, bx.const_undef(vec_ty), bx.const_vector(&indices))
10281028
}
10291029
(InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd), Abi::Scalar(s))
1030-
if s.primitive() == Primitive::F64 =>
1030+
if s.primitive() == Primitive::Float(Float::F64) =>
10311031
{
10321032
bx.bitcast(value, bx.cx.type_f64())
10331033
}
@@ -1064,8 +1064,8 @@ fn llvm_fixup_output<'ll, 'tcx>(
10641064
// MIPS only supports register-length arithmetics.
10651065
Primitive::Int(Integer::I8, _) => bx.trunc(value, bx.cx.type_i8()),
10661066
Primitive::Int(Integer::I16, _) => bx.trunc(value, bx.cx.type_i16()),
1067-
Primitive::F32 => bx.bitcast(value, bx.cx.type_f32()),
1068-
Primitive::F64 => bx.bitcast(value, bx.cx.type_f64()),
1067+
Primitive::Float(Float::F32) => bx.bitcast(value, bx.cx.type_f32()),
1068+
Primitive::Float(Float::F64) => bx.bitcast(value, bx.cx.type_f64()),
10691069
_ => value,
10701070
}
10711071
}
@@ -1100,7 +1100,7 @@ fn llvm_fixup_output_type<'ll, 'tcx>(
11001100
cx.type_vector(elem_ty, count * 2)
11011101
}
11021102
(InlineAsmRegClass::X86(X86InlineAsmRegClass::reg_abcd), Abi::Scalar(s))
1103-
if s.primitive() == Primitive::F64 =>
1103+
if s.primitive() == Primitive::Float(Float::F64) =>
11041104
{
11051105
cx.type_i64()
11061106
}
@@ -1136,8 +1136,8 @@ fn llvm_fixup_output_type<'ll, 'tcx>(
11361136
match s.primitive() {
11371137
// MIPS only supports register-length arithmetics.
11381138
Primitive::Int(Integer::I8 | Integer::I16, _) => cx.type_i32(),
1139-
Primitive::F32 => cx.type_i32(),
1140-
Primitive::F64 => cx.type_i64(),
1139+
Primitive::Float(Float::F32) => cx.type_i32(),
1140+
Primitive::Float(Float::F64) => cx.type_i64(),
11411141
_ => layout.llvm_type(cx),
11421142
}
11431143
}

Diff for: compiler/rustc_codegen_llvm/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
576576
}
577577
}
578578
}
579-
abi::F16 | abi::F32 | abi::F64 | abi::F128 => {}
579+
abi::Float(_) => {}
580580
}
581581
}
582582

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ fn tag_base_type<'ll, 'tcx>(
122122
// Niche tags are always normalized to unsized integers of the correct size.
123123
match tag.primitive() {
124124
Primitive::Int(t, _) => t,
125-
Primitive::F16 => Integer::I16,
126-
Primitive::F32 => Integer::I32,
127-
Primitive::F64 => Integer::I64,
128-
Primitive::F128 => Integer::I128,
125+
Primitive::Float(f) => Integer::from_size(f.size()).unwrap(),
129126
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
130127
Primitive::Pointer(_) => {
131128
// If the niche is the NULL value of a reference, then `discr_enum_ty` will be

Diff for: compiler/rustc_codegen_llvm/src/intrinsic.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf};
1818
use rustc_middle::ty::{self, GenericArgsRef, Ty};
1919
use rustc_middle::{bug, span_bug};
2020
use rustc_span::{sym, Span, Symbol};
21-
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size};
21+
use rustc_target::abi::{self, Align, Float, HasDataLayout, Primitive, Size};
2222
use rustc_target::spec::{HasTargetSpec, PanicStrategy};
2323

2424
use std::cmp::Ordering;
@@ -231,13 +231,17 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
231231
emit_va_arg(self, args[0], ret_ty)
232232
}
233233
}
234-
Primitive::F16 => bug!("the va_arg intrinsic does not work with `f16`"),
235-
Primitive::F64 | Primitive::Pointer(_) => {
234+
Primitive::Float(Float::F16) => {
235+
bug!("the va_arg intrinsic does not work with `f16`")
236+
}
237+
Primitive::Float(Float::F64) | Primitive::Pointer(_) => {
236238
emit_va_arg(self, args[0], ret_ty)
237239
}
238240
// `va_arg` should never be used with the return type f32.
239-
Primitive::F32 => bug!("the va_arg intrinsic does not work with `f32`"),
240-
Primitive::F128 => {
241+
Primitive::Float(Float::F32) => {
242+
bug!("the va_arg intrinsic does not work with `f32`")
243+
}
244+
Primitive::Float(Float::F128) => {
241245
bug!("the va_arg intrinsic does not work with `f128`")
242246
}
243247
}

Diff for: compiler/rustc_codegen_llvm/src/type_of.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
66
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
77
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
88
use rustc_target::abi::{Abi, Align, FieldsShape};
9-
use rustc_target::abi::{Int, Pointer, F128, F16, F32, F64};
9+
use rustc_target::abi::{Float, Int, Pointer};
1010
use rustc_target::abi::{Scalar, Size, Variants};
1111

1212
use std::fmt::Write;
@@ -272,10 +272,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyAndLayout<'tcx> {
272272
fn scalar_llvm_type_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>, scalar: Scalar) -> &'a Type {
273273
match scalar.primitive() {
274274
Int(i, _) => cx.type_from_integer(i),
275-
F16 => cx.type_f16(),
276-
F32 => cx.type_f32(),
277-
F64 => cx.type_f64(),
278-
F128 => cx.type_f128(),
275+
Float(f) => cx.type_from_float(f),
279276
Pointer(address_space) => cx.type_ptr_ext(address_space),
280277
}
281278
}

Diff for: compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
306306
self.assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
307307

308308
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
309-
(Int(..) | F16 | F32 | F64 | F128, Int(..) | F16 | F32 | F64 | F128) => {
310-
bx.bitcast(imm, to_backend_ty)
311-
}
309+
(Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
312310
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
313311
(Int(..), Pointer(..)) => bx.ptradd(bx.const_null(bx.type_ptr()), imm),
314312
(Pointer(..), Int(..)) => bx.ptrtoint(imm, to_backend_ty),
315-
(F16 | F32 | F64 | F128, Pointer(..)) => {
313+
(Float(_), Pointer(..)) => {
316314
let int_imm = bx.bitcast(imm, bx.cx().type_isize());
317315
bx.ptradd(bx.const_null(bx.type_ptr()), int_imm)
318316
}
319-
(Pointer(..), F16 | F32 | F64 | F128) => {
317+
(Pointer(..), Float(_)) => {
320318
let int_imm = bx.ptrtoint(imm, bx.cx().type_isize());
321319
bx.bitcast(int_imm, to_backend_ty)
322320
}

Diff for: compiler/rustc_codegen_ssa/src/traits/type_.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::bug;
77
use rustc_middle::ty::layout::TyAndLayout;
88
use rustc_middle::ty::{self, Ty};
99
use rustc_target::abi::call::{ArgAbi, CastTarget, FnAbi, Reg};
10-
use rustc_target::abi::{AddressSpace, Integer};
10+
use rustc_target::abi::{AddressSpace, Float, Integer};
1111

1212
// This depends on `Backend` and not `BackendTypes`, because consumers will probably want to use
1313
// `LayoutOf` or `HasTyCtxt`. This way, they don't have to add a constraint on it themselves.
@@ -65,6 +65,16 @@ pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {
6565
}
6666
}
6767

68+
fn type_from_float(&self, f: Float) -> Self::Type {
69+
use Float::*;
70+
match f {
71+
F16 => self.type_f16(),
72+
F32 => self.type_f32(),
73+
F64 => self.type_f64(),
74+
F128 => self.type_f128(),
75+
}
76+
}
77+
6878
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
6979
ty.needs_drop(self.tcx(), ty::ParamEnv::reveal_all())
7080
}

Diff for: compiler/rustc_middle/src/ty/layout.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,35 @@ impl Integer {
114114
}
115115
}
116116

117-
#[extension(pub trait PrimitiveExt)]
118-
impl Primitive {
117+
#[extension(pub trait FloatExt)]
118+
impl Float {
119119
#[inline]
120120
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
121121
match *self {
122-
Int(i, signed) => i.to_ty(tcx, signed),
123122
F16 => tcx.types.f16,
124123
F32 => tcx.types.f32,
125124
F64 => tcx.types.f64,
126125
F128 => tcx.types.f128,
126+
}
127+
}
128+
129+
fn from_float_ty(fty: ty::FloatTy) -> Self {
130+
match fty {
131+
ty::FloatTy::F16 => F16,
132+
ty::FloatTy::F32 => F32,
133+
ty::FloatTy::F64 => F64,
134+
ty::FloatTy::F128 => F128,
135+
}
136+
}
137+
}
138+
139+
#[extension(pub trait PrimitiveExt)]
140+
impl Primitive {
141+
#[inline]
142+
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
143+
match *self {
144+
Int(i, signed) => i.to_ty(tcx, signed),
145+
Float(f) => f.to_ty(tcx),
127146
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
128147
Pointer(_) => Ty::new_mut_ptr(tcx, tcx.types.unit),
129148
}
@@ -140,7 +159,7 @@ impl Primitive {
140159
let signed = false;
141160
tcx.data_layout().ptr_sized_integer().to_ty(tcx, signed)
142161
}
143-
F16 | F32 | F64 | F128 => bug!("floats do not have an int type"),
162+
Float(_) => bug!("floats do not have an int type"),
144163
}
145164
}
146165
}

Diff for: compiler/rustc_middle/src/ty/util.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
44
use crate::query::{IntoQueryParam, Providers};
5-
use crate::ty::layout::IntegerExt;
5+
use crate::ty::layout::{FloatExt, IntegerExt};
66
use crate::ty::{
77
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
88
TypeVisitableExt,
@@ -20,7 +20,7 @@ use rustc_index::bit_set::GrowableBitSet;
2020
use rustc_macros::{extension, HashStable, TyDecodable, TyEncodable};
2121
use rustc_session::Limit;
2222
use rustc_span::sym;
23-
use rustc_target::abi::{Integer, IntegerType, Primitive, Size};
23+
use rustc_target::abi::{Float, Integer, IntegerType, Size};
2424
use rustc_target::spec::abi::Abi;
2525
use smallvec::{smallvec, SmallVec};
2626
use std::{fmt, iter};
@@ -1145,8 +1145,7 @@ impl<'tcx> Ty<'tcx> {
11451145
ty::Char => Size::from_bytes(4),
11461146
ty::Int(ity) => Integer::from_int_ty(&tcx, ity).size(),
11471147
ty::Uint(uty) => Integer::from_uint_ty(&tcx, uty).size(),
1148-
ty::Float(ty::FloatTy::F32) => Primitive::F32.size(&tcx),
1149-
ty::Float(ty::FloatTy::F64) => Primitive::F64.size(&tcx),
1148+
ty::Float(fty) => Float::from_float_ty(fty).size(),
11501149
_ => bug!("non primitive type"),
11511150
}
11521151
}

0 commit comments

Comments
 (0)