From 488ddd3bbc2bb1d5956b3d677f3012694610a8c6 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Thu, 16 May 2024 04:05:34 -0500 Subject: [PATCH] Fix assertion when attempting to convert `f16` and `f128` with `as` These types are currently rejected for `as` casts by the compiler. Remove this incorrect check and add codegen tests for all conversions involving these types. --- .../rustc_codegen_ssa/src/traits/builder.rs | 5 +- tests/codegen/float/f128.rs | 194 +++++++++++++++++ tests/codegen/float/f16.rs | 202 +++++++++++++++++- 3 files changed, 397 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/traits/builder.rs b/compiler/rustc_codegen_ssa/src/traits/builder.rs index fdeccb90700..9fd6eb8edab 100644 --- a/compiler/rustc_codegen_ssa/src/traits/builder.rs +++ b/compiler/rustc_codegen_ssa/src/traits/builder.rs @@ -247,7 +247,10 @@ fn cast_float_to_int( } else { (in_ty, dest_ty) }; - assert!(matches!(self.cx().type_kind(float_ty), TypeKind::Float | TypeKind::Double)); + assert!(matches!( + self.cx().type_kind(float_ty), + TypeKind::Half | TypeKind::Float | TypeKind::Double | TypeKind::FP128 + )); assert_eq!(self.cx().type_kind(int_ty), TypeKind::Integer); if let Some(false) = self.cx().sess().opts.unstable_opts.saturating_float_casts { diff --git a/tests/codegen/float/f128.rs b/tests/codegen/float/f128.rs index 97d545e0283..32c5be1ec65 100644 --- a/tests/codegen/float/f128.rs +++ b/tests/codegen/float/f128.rs @@ -2,6 +2,7 @@ #![crate_type = "lib"] #![feature(f128)] +#![feature(f16)] #![feature(core_intrinsics)] // CHECK-LABEL: i1 @f128_eq( @@ -127,3 +128,196 @@ pub fn f128_rem_assign(a: &mut f128, b: f128) { // CHECK-NEXT: store fp128 %{{.+}}, ptr %{{.+}} *a %= b } + +/* float to float conversions */ + +// CHECK-LABEL: half @f128_as_f16( +#[no_mangle] +pub fn f128_as_f16(a: f128) -> f16 { + // CHECK: fptrunc fp128 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: float @f128_as_f32( +#[no_mangle] +pub fn f128_as_f32(a: f128) -> f32 { + // CHECK: fptrunc fp128 %{{.+}} to float + a as f32 +} + +// CHECK-LABEL: double @f128_as_f64( +#[no_mangle] +pub fn f128_as_f64(a: f128) -> f64 { + // CHECK: fptrunc fp128 %{{.+}} to double + a as f64 +} + +// CHECK-LABEL: fp128 @f128_as_self( +#[no_mangle] +pub fn f128_as_self(a: f128) -> f128 { + // CHECK: ret fp128 %{{.+}} + a as f128 +} + +// CHECK-LABEL: fp128 @f16_as_f128( +#[no_mangle] +pub fn f16_as_f128(a: f16) -> f128 { + // CHECK: fpext half %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @f32_as_f128( +#[no_mangle] +pub fn f32_as_f128(a: f32) -> f128 { + // CHECK: fpext float %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @f64_as_f128( +#[no_mangle] +pub fn f64_as_f128(a: f64) -> f128 { + // CHECK: fpext double %{{.+}} to fp128 + a as f128 +} + +/* float to int conversions */ + +// CHECK-LABEL: i8 @f128_as_u8( +#[no_mangle] +pub fn f128_as_u8(a: f128) -> u8 { + // CHECK: call i8 @llvm.fptoui.sat.i8.f128(fp128 %{{.+}}) + a as u8 +} + +#[no_mangle] +pub fn f128_as_u16(a: f128) -> u16 { + // CHECK: call i16 @llvm.fptoui.sat.i16.f128(fp128 %{{.+}}) + a as u16 +} + +// CHECK-LABEL: i32 @f128_as_u32( +#[no_mangle] +pub fn f128_as_u32(a: f128) -> u32 { + // CHECK: call i32 @llvm.fptoui.sat.i32.f128(fp128 %{{.+}}) + a as u32 +} + +// CHECK-LABEL: i64 @f128_as_u64( +#[no_mangle] +pub fn f128_as_u64(a: f128) -> u64 { + // CHECK: call i64 @llvm.fptoui.sat.i64.f128(fp128 %{{.+}}) + a as u64 +} + +// CHECK-LABEL: i128 @f128_as_u128( +#[no_mangle] +pub fn f128_as_u128(a: f128) -> u128 { + // CHECK: call i128 @llvm.fptoui.sat.i128.f128(fp128 %{{.+}}) + a as u128 +} + +// CHECK-LABEL: i8 @f128_as_i8( +#[no_mangle] +pub fn f128_as_i8(a: f128) -> i8 { + // CHECK: call i8 @llvm.fptosi.sat.i8.f128(fp128 %{{.+}}) + a as i8 +} + +// CHECK-LABEL: i16 @f128_as_i16( +#[no_mangle] +pub fn f128_as_i16(a: f128) -> i16 { + // CHECK: call i16 @llvm.fptosi.sat.i16.f128(fp128 %{{.+}}) + a as i16 +} +// CHECK-LABEL: i32 @f128_as_i32( +#[no_mangle] +pub fn f128_as_i32(a: f128) -> i32 { + // CHECK: call i32 @llvm.fptosi.sat.i32.f128(fp128 %{{.+}}) + a as i32 +} + +// CHECK-LABEL: i64 @f128_as_i64( +#[no_mangle] +pub fn f128_as_i64(a: f128) -> i64 { + // CHECK: call i64 @llvm.fptosi.sat.i64.f128(fp128 %{{.+}}) + a as i64 +} + +// CHECK-LABEL: i128 @f128_as_i128( +#[no_mangle] +pub fn f128_as_i128(a: f128) -> i128 { + // CHECK: call i128 @llvm.fptosi.sat.i128.f128(fp128 %{{.+}}) + a as i128 +} + +/* int to float conversions */ + +// CHECK-LABEL: fp128 @u8_as_f128( +#[no_mangle] +pub fn u8_as_f128(a: u8) -> f128 { + // CHECK: uitofp i8 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @u16_as_f128( +#[no_mangle] +pub fn u16_as_f128(a: u16) -> f128 { + // CHECK: uitofp i16 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @u32_as_f128( +#[no_mangle] +pub fn u32_as_f128(a: u32) -> f128 { + // CHECK: uitofp i32 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @u64_as_f128( +#[no_mangle] +pub fn u64_as_f128(a: u64) -> f128 { + // CHECK: uitofp i64 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @u128_as_f128( +#[no_mangle] +pub fn u128_as_f128(a: u128) -> f128 { + // CHECK: uitofp i128 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @i8_as_f128( +#[no_mangle] +pub fn i8_as_f128(a: i8) -> f128 { + // CHECK: sitofp i8 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @i16_as_f128( +#[no_mangle] +pub fn i16_as_f128(a: i16) -> f128 { + // CHECK: sitofp i16 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @i32_as_f128( +#[no_mangle] +pub fn i32_as_f128(a: i32) -> f128 { + // CHECK: sitofp i32 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @i64_as_f128( +#[no_mangle] +pub fn i64_as_f128(a: i64) -> f128 { + // CHECK: sitofp i64 %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: fp128 @i128_as_f128( +#[no_mangle] +pub fn i128_as_f128(a: i128) -> f128 { + // CHECK: sitofp i128 %{{.+}} to fp128 + a as f128 +} diff --git a/tests/codegen/float/f16.rs b/tests/codegen/float/f16.rs index d1f75cc3b68..96daac869c2 100644 --- a/tests/codegen/float/f16.rs +++ b/tests/codegen/float/f16.rs @@ -1,9 +1,12 @@ // Verify that our intrinsics generate the correct LLVM calls for f16 #![crate_type = "lib"] +#![feature(f128)] #![feature(f16)] #![feature(core_intrinsics)] +/* arithmetic */ + // CHECK-LABEL: i1 @f16_eq( #[no_mangle] pub fn f16_eq(a: f16, b: f16) -> bool { @@ -109,7 +112,7 @@ pub fn f16_sub_assign(a: &mut f16, b: f16) { pub fn f16_mul_assign(a: &mut f16, b: f16) { // CHECK: fmul half %{{.+}}, %{{.+}} // CHECK-NEXT: store half %{{.+}}, ptr %{{.+}} - *a *= b + *a *= b; } // CHECK-LABEL: void @f16_div_assign( @@ -117,7 +120,7 @@ pub fn f16_mul_assign(a: &mut f16, b: f16) { pub fn f16_div_assign(a: &mut f16, b: f16) { // CHECK: fdiv half %{{.+}}, %{{.+}} // CHECK-NEXT: store half %{{.+}}, ptr %{{.+}} - *a /= b + *a /= b; } // CHECK-LABEL: void @f16_rem_assign( @@ -125,5 +128,198 @@ pub fn f16_div_assign(a: &mut f16, b: f16) { pub fn f16_rem_assign(a: &mut f16, b: f16) { // CHECK: frem half %{{.+}}, %{{.+}} // CHECK-NEXT: store half %{{.+}}, ptr %{{.+}} - *a %= b + *a %= b; +} + +/* float to float conversions */ + +// CHECK-LABEL: half @f16_as_self( +#[no_mangle] +pub fn f16_as_self(a: f16) -> f16 { + // CHECK: ret half %{{.+}} + a as f16 +} + +// CHECK-LABEL: float @f16_as_f32( +#[no_mangle] +pub fn f16_as_f32(a: f16) -> f32 { + // CHECK: fpext half %{{.+}} to float + a as f32 +} + +// CHECK-LABEL: double @f16_as_f64( +#[no_mangle] +pub fn f16_as_f64(a: f16) -> f64 { + // CHECK: fpext half %{{.+}} to double + a as f64 +} + +// CHECK-LABEL: fp128 @f16_as_f128( +#[no_mangle] +pub fn f16_as_f128(a: f16) -> f128 { + // CHECK: fpext half %{{.+}} to fp128 + a as f128 +} + +// CHECK-LABEL: half @f32_as_f16( +#[no_mangle] +pub fn f32_as_f16(a: f32) -> f16 { + // CHECK: fptrunc float %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @f64_as_f16( +#[no_mangle] +pub fn f64_as_f16(a: f64) -> f16 { + // CHECK: fptrunc double %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @f128_as_f16( +#[no_mangle] +pub fn f128_as_f16(a: f128) -> f16 { + // CHECK: fptrunc fp128 %{{.+}} to half + a as f16 +} + +/* float to int conversions */ + +// CHECK-LABEL: i8 @f16_as_u8( +#[no_mangle] +pub fn f16_as_u8(a: f16) -> u8 { + // CHECK: call i8 @llvm.fptoui.sat.i8.f16(half %{{.+}}) + a as u8 +} + +#[no_mangle] +pub fn f16_as_u16(a: f16) -> u16 { + // CHECK: call i16 @llvm.fptoui.sat.i16.f16(half %{{.+}}) + a as u16 +} + +// CHECK-LABEL: i32 @f16_as_u32( +#[no_mangle] +pub fn f16_as_u32(a: f16) -> u32 { + // CHECK: call i32 @llvm.fptoui.sat.i32.f16(half %{{.+}}) + a as u32 +} + +// CHECK-LABEL: i64 @f16_as_u64( +#[no_mangle] +pub fn f16_as_u64(a: f16) -> u64 { + // CHECK: call i64 @llvm.fptoui.sat.i64.f16(half %{{.+}}) + a as u64 +} + +// CHECK-LABEL: i128 @f16_as_u128( +#[no_mangle] +pub fn f16_as_u128(a: f16) -> u128 { + // CHECK: call i128 @llvm.fptoui.sat.i128.f16(half %{{.+}}) + a as u128 +} + +// CHECK-LABEL: i8 @f16_as_i8( +#[no_mangle] +pub fn f16_as_i8(a: f16) -> i8 { + // CHECK: call i8 @llvm.fptosi.sat.i8.f16(half %{{.+}}) + a as i8 +} + +// CHECK-LABEL: i16 @f16_as_i16( +#[no_mangle] +pub fn f16_as_i16(a: f16) -> i16 { + // CHECK: call i16 @llvm.fptosi.sat.i16.f16(half %{{.+}}) + a as i16 +} +// CHECK-LABEL: i32 @f16_as_i32( +#[no_mangle] +pub fn f16_as_i32(a: f16) -> i32 { + // CHECK: call i32 @llvm.fptosi.sat.i32.f16(half %{{.+}}) + a as i32 +} + +// CHECK-LABEL: i64 @f16_as_i64( +#[no_mangle] +pub fn f16_as_i64(a: f16) -> i64 { + // CHECK: call i64 @llvm.fptosi.sat.i64.f16(half %{{.+}}) + a as i64 +} + +// CHECK-LABEL: i128 @f16_as_i128( +#[no_mangle] +pub fn f16_as_i128(a: f16) -> i128 { + // CHECK: call i128 @llvm.fptosi.sat.i128.f16(half %{{.+}}) + a as i128 +} + +/* int to float conversions */ + +// CHECK-LABEL: half @u8_as_f16( +#[no_mangle] +pub fn u8_as_f16(a: u8) -> f16 { + // CHECK: uitofp i8 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @u16_as_f16( +#[no_mangle] +pub fn u16_as_f16(a: u16) -> f16 { + // CHECK: uitofp i16 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @u32_as_f16( +#[no_mangle] +pub fn u32_as_f16(a: u32) -> f16 { + // CHECK: uitofp i32 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @u64_as_f16( +#[no_mangle] +pub fn u64_as_f16(a: u64) -> f16 { + // CHECK: uitofp i64 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @u128_as_f16( +#[no_mangle] +pub fn u128_as_f16(a: u128) -> f16 { + // CHECK: uitofp i128 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @i8_as_f16( +#[no_mangle] +pub fn i8_as_f16(a: i8) -> f16 { + // CHECK: sitofp i8 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @i16_as_f16( +#[no_mangle] +pub fn i16_as_f16(a: i16) -> f16 { + // CHECK: sitofp i16 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @i32_as_f16( +#[no_mangle] +pub fn i32_as_f16(a: i32) -> f16 { + // CHECK: sitofp i32 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @i64_as_f16( +#[no_mangle] +pub fn i64_as_f16(a: i64) -> f16 { + // CHECK: sitofp i64 %{{.+}} to half + a as f16 +} + +// CHECK-LABEL: half @i128_as_f16( +#[no_mangle] +pub fn i128_as_f16(a: i128) -> f16 { + // CHECK: sitofp i128 %{{.+}} to half + a as f16 }