diff options
| author | bors <bors@rust-lang.org> | 2024-06-23 14:14:48 +0000 | 
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-06-23 14:14:48 +0000 | 
| commit | aabbf84b45a5e7b868c33e959d7e5cc985097d19 (patch) | |
| tree | 7ef2e780f99167b9ef3889d52b2c457d223d5db4 /compiler | |
| parent | c3d7fb398569407350abe044e786bc7890c90397 (diff) | |
| parent | 28ce7cd03eebd3b28334624422dfa0c24fb89e54 (diff) | |
| download | rust-aabbf84b45a5e7b868c33e959d7e5cc985097d19.tar.gz rust-aabbf84b45a5e7b868c33e959d7e5cc985097d19.zip | |
Auto merge of #123088 - tgross35:f16-f128-pattern-analysis, r=Nadrieril
Replace `f16` and `f128` pattern matching stubs with real implementations This section of code depends on `rustc_apfloat` rather than our internal types, so this is one potential ICE that we should be able to melt now. r? `@Nadrieril`
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_middle/src/thir.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/util.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_pattern_analysis/src/constructor.rs | 27 | ||||
| -rw-r--r-- | compiler/rustc_pattern_analysis/src/rustc.rs | 50 | 
5 files changed, 86 insertions, 11 deletions
| diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index 7c8b0ec671a..c97af68c29e 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -1047,6 +1047,12 @@ impl<'tcx> PatRangeBoundary<'tcx> { let b = other.eval_bits(ty, tcx, param_env); match ty.kind() { + ty::Float(ty::FloatTy::F16) => { + use rustc_apfloat::Float; + let a = rustc_apfloat::ieee::Half::from_bits(a); + let b = rustc_apfloat::ieee::Half::from_bits(b); + a.partial_cmp(&b) + } ty::Float(ty::FloatTy::F32) => { use rustc_apfloat::Float; let a = rustc_apfloat::ieee::Single::from_bits(a); @@ -1059,6 +1065,12 @@ impl<'tcx> PatRangeBoundary<'tcx> { let b = rustc_apfloat::ieee::Double::from_bits(b); a.partial_cmp(&b) } + ty::Float(ty::FloatTy::F128) => { + use rustc_apfloat::Float; + let a = rustc_apfloat::ieee::Quad::from_bits(a); + let b = rustc_apfloat::ieee::Quad::from_bits(b); + a.partial_cmp(&b) + } ty::Int(ity) => { let size = rustc_target::abi::Integer::from_int_ty(&tcx, *ity).size(); let a = size.sign_extend(a) as i128; diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 1b5efcee903..9307e380681 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -1196,7 +1196,7 @@ impl<'tcx> Ty<'tcx> { /// Returns the minimum and maximum values for the given numeric type (including `char`s) or /// returns `None` if the type is not numeric. pub fn numeric_min_and_max_as_bits(self, tcx: TyCtxt<'tcx>) -> Option<(u128, u128)> { - use rustc_apfloat::ieee::{Double, Single}; + use rustc_apfloat::ieee::{Double, Half, Quad, Single}; Some(match self.kind() { ty::Int(_) | ty::Uint(_) => { let (size, signed) = self.int_size_and_signed(tcx); @@ -1206,12 +1206,14 @@ impl<'tcx> Ty<'tcx> { (min, max) } ty::Char => (0, std::char::MAX as u128), + ty::Float(ty::FloatTy::F16) => ((-Half::INFINITY).to_bits(), Half::INFINITY.to_bits()), ty::Float(ty::FloatTy::F32) => { ((-Single::INFINITY).to_bits(), Single::INFINITY.to_bits()) } ty::Float(ty::FloatTy::F64) => { ((-Double::INFINITY).to_bits(), Double::INFINITY.to_bits()) } + ty::Float(ty::FloatTy::F128) => ((-Quad::INFINITY).to_bits(), Quad::INFINITY.to_bits()), _ => return None, }) } diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index 192d706bce2..dda6c88008b 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -369,10 +369,10 @@ impl<'tcx> ConstToPat<'tcx> { ty::Float(flt) => { let v = cv.unwrap_leaf(); let is_nan = match flt { - ty::FloatTy::F16 => unimplemented!("f16_f128"), + ty::FloatTy::F16 => v.to_f16().is_nan(), ty::FloatTy::F32 => v.to_f32().is_nan(), ty::FloatTy::F64 => v.to_f64().is_nan(), - ty::FloatTy::F128 => unimplemented!("f16_f128"), + ty::FloatTy::F128 => v.to_f128().is_nan(), }; if is_nan { // NaNs are not ever equal to anything so they make no sense as patterns. diff --git a/compiler/rustc_pattern_analysis/src/constructor.rs b/compiler/rustc_pattern_analysis/src/constructor.rs index 44f09b66bf6..fb103705475 100644 --- a/compiler/rustc_pattern_analysis/src/constructor.rs +++ b/compiler/rustc_pattern_analysis/src/constructor.rs @@ -182,7 +182,7 @@ use std::iter::once; use smallvec::SmallVec; -use rustc_apfloat::ieee::{DoubleS, IeeeFloat, SingleS}; +use rustc_apfloat::ieee::{DoubleS, HalfS, IeeeFloat, QuadS, SingleS}; use rustc_index::bit_set::{BitSet, GrowableBitSet}; use rustc_index::IndexVec; @@ -692,8 +692,10 @@ pub enum Constructor<Cx: PatCx> { /// Ranges of integer literal values (`2`, `2..=5` or `2..5`). IntRange(IntRange), /// Ranges of floating-point literal values (`2.0..=5.2`). + F16Range(IeeeFloat<HalfS>, IeeeFloat<HalfS>, RangeEnd), F32Range(IeeeFloat<SingleS>, IeeeFloat<SingleS>, RangeEnd), F64Range(IeeeFloat<DoubleS>, IeeeFloat<DoubleS>, RangeEnd), + F128Range(IeeeFloat<QuadS>, IeeeFloat<QuadS>, RangeEnd), /// String literals. Strings are not quite the same as `&[u8]` so we treat them separately. Str(Cx::StrLit), /// Constants that must not be matched structurally. They are treated as black boxes for the @@ -735,8 +737,10 @@ impl<Cx: PatCx> Clone for Constructor<Cx> { Constructor::UnionField => Constructor::UnionField, Constructor::Bool(b) => Constructor::Bool(*b), Constructor::IntRange(range) => Constructor::IntRange(*range), + Constructor::F16Range(lo, hi, end) => Constructor::F16Range(lo.clone(), *hi, *end), Constructor::F32Range(lo, hi, end) => Constructor::F32Range(lo.clone(), *hi, *end), Constructor::F64Range(lo, hi, end) => Constructor::F64Range(lo.clone(), *hi, *end), + Constructor::F128Range(lo, hi, end) => Constructor::F128Range(lo.clone(), *hi, *end), Constructor::Str(value) => Constructor::Str(value.clone()), Constructor::Opaque(inner) => Constructor::Opaque(inner.clone()), Constructor::Or => Constructor::Or, @@ -812,6 +816,14 @@ impl<Cx: PatCx> Constructor<Cx> { (Bool(self_b), Bool(other_b)) => self_b == other_b, (IntRange(self_range), IntRange(other_range)) => self_range.is_subrange(other_range), + (F16Range(self_from, self_to, self_end), F16Range(other_from, other_to, other_end)) => { + self_from.ge(other_from) + && match self_to.partial_cmp(other_to) { + Some(Ordering::Less) => true, + Some(Ordering::Equal) => other_end == self_end, + _ => false, + } + } (F32Range(self_from, self_to, self_end), F32Range(other_from, other_to, other_end)) => { self_from.ge(other_from) && match self_to.partial_cmp(other_to) { @@ -828,6 +840,17 @@ impl<Cx: PatCx> Constructor<Cx> { _ => false, } } + ( + F128Range(self_from, self_to, self_end), + F128Range(other_from, other_to, other_end), + ) => { + self_from.ge(other_from) + && match self_to.partial_cmp(other_to) { + Some(Ordering::Less) => true, + Some(Ordering::Equal) => other_end == self_end, + _ => false, + } + } (Str(self_val), Str(other_val)) => { // FIXME Once valtrees are available we can directly use the bytes // in the `Str` variant of the valtree for the comparison here. @@ -906,8 +929,10 @@ impl<Cx: PatCx> Constructor<Cx> { Bool(b) => write!(f, "{b}")?, // Best-effort, will render signed ranges incorrectly IntRange(range) => write!(f, "{range:?}")?, + F16Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?, F32Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?, F64Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?, + F128Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?, Str(value) => write!(f, "{value:?}")?, Opaque(..) => write!(f, "<constant pattern>")?, Or => { diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index 8391c694c64..d4dd4dd858c 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -247,8 +247,9 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { } _ => bug!("bad slice pattern {:?} {:?}", ctor, ty), }, - Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..) - | Never | NonExhaustive | Hidden | Missing | PrivateUninhabited | Wildcard => &[], + Bool(..) | IntRange(..) | F16Range(..) | F32Range(..) | F64Range(..) + | F128Range(..) | Str(..) | Opaque(..) | Never | NonExhaustive | Hidden | Missing + | PrivateUninhabited | Wildcard => &[], Or => { bug!("called `Fields::wildcards` on an `Or` ctor") } @@ -275,8 +276,9 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { }, Ref => 1, Slice(slice) => slice.arity(), - Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..) - | Never | NonExhaustive | Hidden | Missing | PrivateUninhabited | Wildcard => 0, + Bool(..) | IntRange(..) | F16Range(..) | F32Range(..) | F64Range(..) + | F128Range(..) | Str(..) | Opaque(..) | Never | NonExhaustive | Hidden | Missing + | PrivateUninhabited | Wildcard => 0, Or => bug!("The `Or` constructor doesn't have a fixed arity"), } } @@ -546,6 +548,18 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { fields = vec![]; arity = 0; } + ty::Float(ty::FloatTy::F16) => { + ctor = match value.try_eval_bits(cx.tcx, cx.param_env) { + Some(bits) => { + use rustc_apfloat::Float; + let value = rustc_apfloat::ieee::Half::from_bits(bits); + F16Range(value, value, RangeEnd::Included) + } + None => Opaque(OpaqueId::new()), + }; + fields = vec![]; + arity = 0; + } ty::Float(ty::FloatTy::F32) => { ctor = match value.try_eval_bits(cx.tcx, cx.param_env) { Some(bits) => { @@ -570,6 +584,18 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { fields = vec![]; arity = 0; } + ty::Float(ty::FloatTy::F128) => { + ctor = match value.try_eval_bits(cx.tcx, cx.param_env) { + Some(bits) => { + use rustc_apfloat::Float; + let value = rustc_apfloat::ieee::Quad::from_bits(bits); + F128Range(value, value, RangeEnd::Included) + } + None => Opaque(OpaqueId::new()), + }; + fields = vec![]; + arity = 0; + } ty::Ref(_, t, _) if t.is_str() => { // We want a `&str` constant to behave like a `Deref` pattern, to be compatible // with other `Deref` patterns. This could have been done in `const_to_pat`, @@ -611,7 +637,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { let lo = lo.as_finite().map(|c| c.eval_bits(cx.tcx, cx.param_env)); let hi = hi.as_finite().map(|c| c.eval_bits(cx.tcx, cx.param_env)); match fty { - ty::FloatTy::F16 => unimplemented!("f16_f128"), + ty::FloatTy::F16 => { + use rustc_apfloat::ieee::Half; + let lo = lo.map(Half::from_bits).unwrap_or(-Half::INFINITY); + let hi = hi.map(Half::from_bits).unwrap_or(Half::INFINITY); + F16Range(lo, hi, end) + } ty::FloatTy::F32 => { use rustc_apfloat::ieee::Single; let lo = lo.map(Single::from_bits).unwrap_or(-Single::INFINITY); @@ -624,7 +655,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { let hi = hi.map(Double::from_bits).unwrap_or(Double::INFINITY); F64Range(lo, hi, end) } - ty::FloatTy::F128 => unimplemented!("f16_f128"), + ty::FloatTy::F128 => { + use rustc_apfloat::ieee::Quad; + let lo = lo.map(Quad::from_bits).unwrap_or(-Quad::INFINITY); + let hi = hi.map(Quad::from_bits).unwrap_or(Quad::INFINITY); + F128Range(lo, hi, end) + } } } _ => bug!("invalid type for range pattern: {}", ty.inner()), @@ -837,7 +873,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { "trying to convert a `Missing` constructor into a `Pat`; this is probably a bug, `Missing` should have been processed in `apply_constructors`" ), - F32Range(..) | F64Range(..) | Opaque(..) | Or => { + F16Range(..) | F32Range(..) | F64Range(..) | F128Range(..) | Opaque(..) | Or => { bug!("can't convert to pattern: {:?}", pat) } }; | 
