Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add f16 and f128 #114607

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ dependencies = [
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"rustc-demangle 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
Expand Down Expand Up @@ -792,7 +792,7 @@ dependencies = [
"md-5",
"miniz_oxide",
"regex",
"rustc-demangle",
"rustc-demangle 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
Expand Down Expand Up @@ -3266,7 +3266,7 @@ name = "rust-demangler"
version = "0.0.1"
dependencies = [
"regex",
"rustc-demangle",
"rustc-demangle 0.1.23 (git+https://github.com/tgross35/rustc-demangle.git?branch=f16-f128)",
]

[[package]]
Expand Down Expand Up @@ -3299,6 +3299,11 @@ dependencies = [
"rustc-std-workspace-core",
]

[[package]]
name = "rustc-demangle"
version = "0.1.23"
source = "git+https://github.com/tgross35/rustc-demangle.git?branch=f16-f128#07968cb12259fe10ff779d6fec8bc4e58f108df8"

[[package]]
name = "rustc-hash"
version = "1.1.0"
Expand Down Expand Up @@ -3557,7 +3562,7 @@ dependencies = [
"libc",
"measureme",
"object",
"rustc-demangle",
"rustc-demangle 0.1.23 (git+https://github.com/tgross35/rustc-demangle.git?branch=f16-f128)",
"rustc_ast",
"rustc_attr",
"rustc_codegen_ssa",
Expand Down Expand Up @@ -4523,7 +4528,7 @@ version = "0.0.0"
dependencies = [
"bitflags 1.3.2",
"punycode",
"rustc-demangle",
"rustc-demangle 0.1.23 (git+https://github.com/tgross35/rustc-demangle.git?branch=f16-f128)",
"rustc_data_structures",
"rustc_errors",
"rustc_hir",
Expand Down Expand Up @@ -5093,7 +5098,7 @@ dependencies = [
"r-efi-alloc",
"rand",
"rand_xorshift",
"rustc-demangle",
"rustc-demangle 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"std_detect",
"unwind",
"wasi",
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ pub struct TargetDataLayout {
pub i32_align: AbiAndPrefAlign,
pub i64_align: AbiAndPrefAlign,
pub i128_align: AbiAndPrefAlign,
pub f16_align: AbiAndPrefAlign,
pub f32_align: AbiAndPrefAlign,
pub f64_align: AbiAndPrefAlign,
pub f128_align: AbiAndPrefAlign,
pub pointer_size: Size,
pub pointer_align: AbiAndPrefAlign,
pub aggregate_align: AbiAndPrefAlign,
Expand Down Expand Up @@ -190,8 +192,10 @@ impl Default for TargetDataLayout {
i32_align: AbiAndPrefAlign::new(align(32)),
i64_align: AbiAndPrefAlign { abi: align(32), pref: align(64) },
i128_align: AbiAndPrefAlign { abi: align(32), pref: align(64) },
f16_align: AbiAndPrefAlign::new(align(16)),
f32_align: AbiAndPrefAlign::new(align(32)),
f64_align: AbiAndPrefAlign::new(align(64)),
f128_align: AbiAndPrefAlign::new(align(128)),
pointer_size: Size::from_bits(64),
pointer_align: AbiAndPrefAlign::new(align(64)),
aggregate_align: AbiAndPrefAlign { abi: align(0), pref: align(64) },
Expand Down Expand Up @@ -271,8 +275,10 @@ impl TargetDataLayout {
dl.instruction_address_space = parse_address_space(&p[1..], "P")?
}
["a", ref a @ ..] => dl.aggregate_align = parse_align(a, "a")?,
["f16", ref a @ ..] => dl.f16_align = parse_align(a, "f16")?,
["f32", ref a @ ..] => dl.f32_align = parse_align(a, "f32")?,
["f64", ref a @ ..] => dl.f64_align = parse_align(a, "f64")?,
["f128", ref a @ ..] => dl.f128_align = parse_align(a, "f128")?,
// FIXME(erikdesjardins): we should be parsing nonzero address spaces
// this will require replacing TargetDataLayout::{pointer_size,pointer_align}
// with e.g. `fn pointer_size_in(AddressSpace)`
Expand Down Expand Up @@ -909,8 +915,10 @@ pub enum Primitive {
/// a negative integer passed by zero-extension will appear positive in
/// the callee, and most operations on it will produce the wrong values.
Int(Integer, bool),
F16,
F32,
F64,
F128,
Pointer(AddressSpace),
}

Expand All @@ -921,8 +929,10 @@ impl Primitive {

match self {
Int(i, _) => i.size(),
F16 => Size::from_bits(16),
F32 => Size::from_bits(32),
F64 => Size::from_bits(64),
F128 => Size::from_bits(128),
// FIXME(erikdesjardins): ignoring address space is technically wrong, pointers in
// different address spaces can have different sizes
// (but TargetDataLayout doesn't currently parse that part of the DL string)
Expand All @@ -936,8 +946,10 @@ impl Primitive {

match self {
Int(i, _) => i.align(dl),
F16 => dl.f16_align,
F32 => dl.f32_align,
F64 => dl.f64_align,
F128 => dl.f128_align,
// FIXME(erikdesjardins): ignoring address space is technically wrong, pointers in
// different address spaces can have different alignments
// (but TargetDataLayout doesn't currently parse that part of the DL string)
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,22 +1909,28 @@ pub struct FnSig {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum FloatTy {
F16,
F32,
F64,
F128,
}

impl FloatTy {
pub fn name_str(self) -> &'static str {
match self {
FloatTy::F16 => "f16",
FloatTy::F32 => "f32",
FloatTy::F64 => "f64",
FloatTy::F128 => "f128",
}
}

pub fn name(self) -> Symbol {
match self {
FloatTy::F16 => sym::f16,
FloatTy::F32 => sym::f32,
FloatTy::F64 => sym::f64,
FloatTy::F128 => sym::f128,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/util/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,10 @@ fn filtered_float_lit(
Some(suf) => LitKind::Float(
symbol,
ast::LitFloatType::Suffixed(match suf {
sym::f16 => ast::FloatTy::F16,
sym::f32 => ast::FloatTy::F32,
sym::f64 => ast::FloatTy::F64,
sym::f128 => ast::FloatTy::F128,
_ => return Err(LitError::InvalidFloatSuffix),
}),
),
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

fn visit_ty(&mut self, ty: &'a ast::Ty) {
use rustc_span::Symbol;

match &ty.kind {
ast::TyKind::BareFn(bare_fn_ty) => {
// Function pointers cannot be `const`
Expand All @@ -343,6 +345,12 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
ast::TyKind::Never => {
gate!(&self, never_type, ty.span, "the `!` type is experimental");
}
ast::TyKind::Path(_, x) if x == &Symbol::intern("f16") => {
gate!(&self, f16, ty.span, "the f16 primitive type is experimental");
}
ast::TyKind::Path(_, x) if x == &Symbol::intern("f128") => {
gate!(&self, f128, ty.span, "the f128 primitive type is experimental");
}
Comment on lines +348 to +353
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a gate here but this doesn't seem right since it can't catch inferred types. I'm not sure where to do a check after types are resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you need to specify the type somewhere to be able to infer it, so the gate will apply at the definition place. I think constructing a f128 via inference if the type is specified in a different crate is the only possible problem here, and I'm not sure how much of an issue that is since the feature(...) still has to exist somewhere in the crate graph.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compiler-errors do you have any suggestion here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the gating is also incorrect the other way, it will deny valid code like struct f16; f16;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that is unfortunate. What is the better way to do this?

_ => {}
}
visit::walk_ty(self, ty)
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_cranelift/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ pub(crate) fn scalar_to_clif_type(tcx: TyCtxt<'_>, scalar: Scalar) -> Type {
Integer::I64 => types::I64,
Integer::I128 => types::I128,
},
Primitive::F16 => unimplemented!("f16 is not yet implemented"),
Primitive::F32 => types::F32,
Primitive::F64 => types::F64,
Primitive::F128 => unimplemented!("f128 is not yet implemented"),
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
Primitive::Pointer(_) => pointer_ty(tcx),
}
Expand All @@ -61,8 +63,10 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Typ
},
ty::Char => types::I32,
ty::Float(size) => match size {
FloatTy::F16 => unimplemented!("f16 is not yet implemented"),
FloatTy::F32 => types::F32,
FloatTy::F64 => types::F64,
FloatTy::F128 => unimplemented!("f128 is not yet implemented"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this cause panics when compiling the standard library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was assuming not because the cranelift tests seem to still pass when I run locally. But I don't know what the exact combination of rustc, cranelift, and bootstrap settings is.

Is there something better to do here? If you have a patch I will apply it during my next rebase, or I can make the change if it's something easy.

@antoyo I do a similar thing for cg_gcc, what do you suggest there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cg_clif inside the rust repo reuses the llvm built standard library but the standalone repo uses builds the standard library using cg_clif. cg_gcc can't reuse the llvm built standard library as it depends on all crates being compiled with -Csymbol-mangling-version=v0 which is not the default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if the CI for cg_gcc indeed fails, you could try using the supported types (F32, F64) and I'll fix this on my side when doing the next sync.
If the CI still fails, I can add the missing stuff to libgccjit: it shouldn't be that hard.

},
ty::FnPtr(_) => pointer_ty(tcx),
ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => {
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_codegen_gcc/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {

pub fn type_float_from_ty(&self, t: ty::FloatTy) -> Type<'gcc> {
match t {
ty::FloatTy::F16 => self.type_f16(),
ty::FloatTy::F32 => self.type_f32(),
ty::FloatTy::F64 => self.type_f64(),
ty::FloatTy::F128 => self.type_f128(),
}
}
}
Expand Down Expand Up @@ -118,6 +120,10 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
self.isize_type
}

fn type_f16(&self) -> Type<'gcc> {
unimplemented!("f16 is not yet supported")
}

fn type_f32(&self) -> Type<'gcc> {
self.float_type
}
Expand All @@ -126,6 +132,10 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
self.double_type
}

fn type_f128(&self) -> Type<'gcc> {
unimplemented!("f128 is not yet supported")
}

fn type_func(&self, params: &[Type<'gcc>], return_type: Type<'gcc>) -> Type<'gcc> {
self.context.new_function_pointer_type(None, return_type, params, false)
}
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_gcc/src/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_middle::bug;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_target::abi::{self, Abi, Align, F32, F64, FieldsShape, Int, Integer, Pointer, PointeeInfo, Size, TyAbiInterface, Variants};
use rustc_target::abi::{self, Abi, Align, F16, F32, F64, F128, FieldsShape, Int, Integer, Pointer, PointeeInfo, Size, TyAbiInterface, Variants};
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};

use crate::abi::{FnAbiGcc, FnAbiGccExt, GccType};
Expand Down Expand Up @@ -257,8 +257,10 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
match scalar.primitive() {
Int(i, true) => cx.type_from_integer(i),
Int(i, false) => cx.type_from_unsigned_integer(i),
F16 => cx.type_f16(),
F32 => cx.type_f32(),
F64 => cx.type_f64(),
F128 => cx.type_f128(),
Pointer(address_space) => {
// If we know the alignment, pick something better than i8.
let pointee =
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ itertools = "0.11"
libc = "0.2"
measureme = "10.0.0"
object = { version = "0.32.0", default-features = false, features = ["std", "read"] }
rustc-demangle = "0.1.21"
# TODO: remove git dependency
# rustc-demangle = "0.1.21"
rustc-demangle = { git = "https://github.com/tgross35/rustc-demangle.git", branch = "f16-f128" }
rustc_ast = { path = "../rustc_ast" }
rustc_attr = { path = "../rustc_attr" }
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
}
}
}
abi::F32 | abi::F64 => {}
abi::F16 | abi::F32 | abi::F64 | abi::F128 => {}
}
}

Expand Down
Loading
Loading