From 9f4590f1249efb02008cb2b009af696ac69a3af3 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 00:22:43 -0800 Subject: [PATCH] air.convert with a rounding suffix: parse it and lower each form Apple's OpenCL compiler spells every convert_* it emits as an air.convert with a rounding after the types (.rte, .rtz, .rtp, .rtn) and, to an integer, .sat after it (convert_float4_rte(uchar4) is air.convert.f.v4f32.u.v4i8.rte, convert_int_sat(float) is air.convert.s.i32.f.f32.rtz.sat; the forms are read from the OpenCL runtime library the macOS 27.2 Metal toolchain ships). Convert::parse took exactly four tokens, so each such name was an unknown intrinsic and the pipeline was refused. - metal2vulkan-air: Convert gains `rounding: Option` and `saturate`; a rounding is accepted between numbers only (no bool, no 8-bit float) and .sat only after one, to an integer. Static folding leaves the saturating and the directed forms alone. - Lowering (lower/arith/rounding.rs): - float to integer: RoundEven / Ceil / Floor first (rte / rtp / rtn; rtz is the conversion's own truncation), then the saturating conversion every air.convert to an integer takes; the NaN test reads the operand itself. Denormal notes as rint / ceil / floor take them. - integer to integer: plain; .sat clamps to the range both types share (SClamp/SMax/SMin of the sign extension, masked, or UMin; pairs on the wide path). - integer to float and float to narrower float, nearest even: the plain conversion. Where it can round to f32/f64 the entry declares RoundingModeRTE for the width and the widths the device's independence ties to it (narrow::rte_modes); a device that cannot declare them records the new PrecisionNote::NativeConversionRounding. - toward zero or an infinity: no Vulkan conversion rounds so per instruction, so the result is computed exactly by integer arithmetic: the magnitude's dropped bits with an increment where the lane rounds away (f32, f64 from 64-bit, the wide path through its sticky 32-bit fold), f32 to half on the bits (normal, overflow, subnormal, inf, NaN), double to float by stepping the device's faithful conversion, double to half through the round-to-odd f32. A conversion that cannot round is the plain one. - PrecisionNote::NativeConversionRounding (native-conversion-rounding): codec, runtime Degradation, comparison table (unbounded), fast_paths ledger class, docs/API.md. - SEMANTICS_VERSION 2021, ARTIFACT_FORMAT_VERSION 501 (the codec gains the note); api_signatures and the air api_surface blessed. - Fixture kernel_convert_rounding_modes (owned MSL, air-rename to 64 suffixed intrinsics). Unverified at this commit: build and tests follow in potluck jobs. --- .../metal2vulkan-air/src/canon/static_init.rs | 12 +- .../src/intrinsics/convert.rs | 57 +- crates/metal2vulkan-air/src/intrinsics/mod.rs | 2 +- .../metal2vulkan-air/src/intrinsics/tests.rs | 80 +- crates/metal2vulkan-air/tests/api_surface.txt | 1 + .../metal2vulkan-runtime/src/degradation.rs | 9 + .../fixtures/public/engine/TOOLCHAIN | 1 + .../kernel_convert_rounding_modes.air.ll | 718 ++++++++++++++++++ .../engine/kernel_convert_rounding_modes.ll | 718 ++++++++++++++++++ .../kernel_convert_rounding_modes.metal | 301 ++++++++ .../src/observation_contract/comparison.rs | 1 + .../observation_contract/comparison/tests.rs | 1 + crates/metal2vulkan/src/cache/codec.rs | 1 + crates/metal2vulkan/src/cache/mod.rs | 2 +- crates/metal2vulkan/src/lower/arith/math.rs | 47 +- crates/metal2vulkan/src/lower/arith/mod.rs | 164 +++- crates/metal2vulkan/src/lower/arith/narrow.rs | 24 + .../metal2vulkan/src/lower/arith/rounding.rs | 609 +++++++++++++++ crates/metal2vulkan/src/lower/arith/tests.rs | 15 +- .../src/lower/arith/tests/narrow.rs | 5 +- .../src/lower/arith/tests/wide.rs | 13 +- crates/metal2vulkan/src/lower/arith/wide.rs | 152 +++- crates/metal2vulkan/src/pipeline/artifact.rs | 12 + crates/metal2vulkan/src/tests/fast_paths.rs | 1 + crates/metal2vulkan/tests/api_signatures.tsv | 9 + docs/API.md | 11 + 26 files changed, 2901 insertions(+), 65 deletions(-) create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.air.ll create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.ll create mode 100644 crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.metal create mode 100644 crates/metal2vulkan/src/lower/arith/rounding.rs diff --git a/crates/metal2vulkan-air/src/canon/static_init.rs b/crates/metal2vulkan-air/src/canon/static_init.rs index 65eb4f8f9..2166ab72b 100644 --- a/crates/metal2vulkan-air/src/canon/static_init.rs +++ b/crates/metal2vulkan-air/src/canon/static_init.rs @@ -53,7 +53,7 @@ use std::collections::{BTreeMap, HashMap}; use crate::budget::Meter; use crate::error::{CanonError, DiagName, InputError, InputErrorKind, Location}; use crate::error::{Error, Feature, Unsupported}; -use crate::intrinsics::{AirIntrinsic, FunctionConstant, LlvmIntrinsic, NumKind}; +use crate::intrinsics::{AirIntrinsic, ConvertRounding, FunctionConstant, LlvmIntrinsic, NumKind}; use crate::ir::layout::Endian; use crate::ir::{AddrSpace, Arena, BlockId, Body, Callee, CastOp, Const, ConstExpr, ConstId}; use crate::ir::{Constant, FuncId, Function, Global, GlobalId, Idx, InstId, InstKind}; @@ -534,14 +534,18 @@ impl<'a> Eval<'a> { _ => return Err(not_evaluable(at)), }, // An integer or `bool` source, a scalar or each lane of a vector: to `bool` as - // `x != 0`, to another integer as C++ converts it; a scalar to a float rounded to - // nearest even. + // `x != 0`, to another integer as C++ converts it (no saturation: a rounding is + // nothing to an integer); a scalar to a float rounded to nearest even (unless the + // name rounds another way). Callee::Air { op: AirIntrinsic::Convert(c), .. } if c.src_kind != NumKind::Float && c.src_fp8.is_none() - && (c.dst_kind != NumKind::Float || !c.src.is_vector()) => + && !c.saturate + && (c.dst_kind != NumKind::Float + || (!c.src.is_vector() + && matches!(c.rounding, None | Some(ConvertRounding::Rte)))) => { let signed = c.src_kind == NumKind::Signed; let x = match call.args.first() { diff --git a/crates/metal2vulkan-air/src/intrinsics/convert.rs b/crates/metal2vulkan-air/src/intrinsics/convert.rs index 3b873d410..59f0fd783 100644 --- a/crates/metal2vulkan-air/src/intrinsics/convert.rs +++ b/crates/metal2vulkan-air/src/intrinsics/convert.rs @@ -1,4 +1,5 @@ -//! Conversions: `air.convert....` and `air.{pack,unpack}..`. +//! Conversions: `air.convert....[.[.sat]]` and +//! `air.{pack,unpack}..`. use super::sig::{Operand, Signature}; use super::ty::{Elem, NumTy}; @@ -71,6 +72,32 @@ impl Fp8 { } } +/// The rounding a conversion names after its types (OpenCL's `convert__`), which +/// Apple's OpenCL compiler spells on every `convert_*` it emits. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum ConvertRounding { + /// `rte`: to nearest, ties to even. + Rte, + /// `rtz`: toward zero. + Rtz, + /// `rtp`: toward positive infinity. + Rtp, + /// `rtn`: toward negative infinity. + Rtn, +} + +impl ConvertRounding { + fn parse(token: &str) -> Option { + Some(match token { + "rte" => ConvertRounding::Rte, + "rtz" => ConvertRounding::Rtz, + "rtp" => ConvertRounding::Rtp, + "rtn" => ConvertRounding::Rtn, + _ => return None, + }) + } +} + /// A numeric conversion between equal lane counts. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Convert { @@ -86,6 +113,13 @@ pub struct Convert { /// or to an integer other than `i1`. An 8-bit float destination does not decode /// ([`super::UNDECODED`]). pub src_fp8: Option, + /// The rounding the name spells after the types (`.`), or `None` for the + /// plain conversion. Neither side is a `bool` or an 8-bit float when it is spelled. + pub rounding: Option, + /// Whether the name ends in `.sat` after its rounding: a saturating conversion to an integer + /// (OpenCL's `convert__sat_`), out-of-range values clamped to the destination's + /// range and a NaN to zero. + pub saturate: bool, } impl Convert { @@ -93,6 +127,15 @@ impl Convert { pub(super) fn parse(rest: &str) -> Option { let mut t = rest.split('.'); let (dk, dt, sk, st) = (t.next()?, t.next()?, t.next()?, t.next()?); + let rounding = match t.next() { + Some(token) => Some(ConvertRounding::parse(token)?), + None => None, + }; + let saturate = match t.next() { + Some("sat") => true, + Some(_) => return None, + None => false, + }; if t.next().is_some() { return None; } @@ -114,12 +157,22 @@ impl Convert { src_kind: NumKind::parse(sk)?, src, src_fp8, + rounding, + saturate, }; let src_ok = match src_fp8 { Some(_) => c.dst.elem != Elem::I1, None => c.src_kind.admits(c.src.elem), }; - let ok = c.dst_kind.admits(c.dst.elem) && src_ok && c.dst.lanes == c.src.lanes; + // A rounding is spelled between numbers (no `bool`, no 8-bit float), and `.sat` only after + // one, to an integer. + let plain = c.dst.elem != Elem::I1 && c.src.elem != Elem::I1 && src_fp8.is_none(); + let suffix_ok = match (rounding, saturate) { + (None, _) => true, + (Some(_), false) => plain, + (Some(_), true) => plain && c.dst_kind != NumKind::Float, + }; + let ok = c.dst_kind.admits(c.dst.elem) && src_ok && suffix_ok && c.dst.lanes == c.src.lanes; ok.then_some(c) } diff --git a/crates/metal2vulkan-air/src/intrinsics/mod.rs b/crates/metal2vulkan-air/src/intrinsics/mod.rs index 40d8ed3b4..a600280aa 100644 --- a/crates/metal2vulkan-air/src/intrinsics/mod.rs +++ b/crates/metal2vulkan-air/src/intrinsics/mod.rs @@ -58,7 +58,7 @@ mod intersect_tests; mod tests; pub use atomic::{AccessKind, Atomic, AtomicOp, AtomicSpace, Coherent, MemoryOrder, ThreadScope}; -pub use convert::{Convert, Fp8, NumKind, Pack, PackDir, PackFormat}; +pub use convert::{Convert, ConvertRounding, Fp8, NumKind, Pack, PackDir, PackFormat}; pub use intersect::{Instancing, Intersect, IntersectTags, ResultField, Role as IntersectRole}; pub use layout::Layout; pub use llvm::{dropped_family, AgxSpace}; diff --git a/crates/metal2vulkan-air/src/intrinsics/tests.rs b/crates/metal2vulkan-air/src/intrinsics/tests.rs index a8977bf4a..41bdf049d 100644 --- a/crates/metal2vulkan-air/src/intrinsics/tests.rs +++ b/crates/metal2vulkan-air/src/intrinsics/tests.rs @@ -279,21 +279,83 @@ fn math_conversion_and_atomic_families_decode() { "air.convert.f8e5m2.f32.f.f32", // 8-bit float is not a destination type "air.convert.u.i1.f.f8e5m2", // an 8-bit float converts to no `bool` "air.convert.s.i32.s.f8e5m2", // an 8-bit float is read as a float - "air.convert.s.i32.u.v2i32", // lane counts differ - "air.convert.f.i32.s.i32", // kind disagrees with type - "air.convert.s.i1.f.f32", // booleans are `u.i1` - "air.pack.unorm4x8.v3f32", // lane count - "air.atomic.global.min.i32", // integer min spells its sign - "air.atomic.global.and.f32", // no float and - "air.atomic.global.add.v2i32", // no vector atomics - "air.atomic.global.xchg.u.i32", // exchange takes no sign - "air.atomic.region.add.u.i32", // unknown space + "air.convert.f.f32.s.i32.sat", // `.sat` follows a rounding + "air.convert.f.f32.s.i32.rte.sat", // a float saturates nothing + "air.convert.s.i32.f.f32.rtz.sat.sat", + "air.convert.s.i32.f.f32.rtu", // no such rounding + "air.convert.f.f32.u.i1.rte", // a `bool` rounds nothing + "air.convert.s.i32.f.f8e5m2.rtz", // nor an 8-bit float + "air.convert.s.i32.u.v2i32", // lane counts differ + "air.convert.f.i32.s.i32", // kind disagrees with type + "air.convert.s.i1.f.f32", // booleans are `u.i1` + "air.pack.unorm4x8.v3f32", // lane count + "air.atomic.global.min.i32", // integer min spells its sign + "air.atomic.global.and.f32", // no float and + "air.atomic.global.add.v2i32", // no vector atomics + "air.atomic.global.xchg.u.i32", // exchange takes no sign + "air.atomic.region.add.u.i32", // unknown space "air.load.somewhat_coherent.i32.p1i32", ] { assert_eq!(classify_self(name), DeclClass::Unknown, "{name}"); } } +/// The rounding Apple's OpenCL compiler spells after a conversion's types, and `.sat` after it to +/// an integer: every `convert_*` it emits (`convert_float4_rte(uchar4)` is +/// `air.convert.f.v4f32.u.v4i8.rte`, `convert_int_sat(float)` is +/// `air.convert.s.i32.f.f32.rtz.sat`). +#[test] +fn convert_parses_its_rounding_and_saturation() { + let parse = |name: &str| match air(name) { + AirIntrinsic::Convert(c) => c, + other => panic!("{name}: {other:?}"), + }; + for (name, rounding, saturate) in [ + ( + "air.convert.f.v4f32.u.v4i8.rte", + ConvertRounding::Rte, + false, + ), + ( + "air.convert.f.v4f32.s.v4i32.rte", + ConvertRounding::Rte, + false, + ), + ("air.convert.f.f16.s.i32.rtz", ConvertRounding::Rtz, false), + ("air.convert.f.f32.f.f64.rtp", ConvertRounding::Rtp, false), + ("air.convert.s.i32.f.f32.rtn", ConvertRounding::Rtn, false), + ( + "air.convert.s.i32.f.f32.rtz.sat", + ConvertRounding::Rtz, + true, + ), + ( + "air.convert.u.v2i16.s.v2i64.rte.sat", + ConvertRounding::Rte, + true, + ), + ("air.convert.u.i8.u.i32.rtz", ConvertRounding::Rtz, false), + ] { + let c: Convert = parse(name); + assert_eq!( + (c.rounding, c.saturate), + (Some(rounding), saturate), + "{name}" + ); + } + let plain = parse("air.convert.f.f32.s.i32"); + assert_eq!((plain.rounding, plain.saturate), (None, false)); + // The suffix changes nothing else: the types and kinds are the plain conversion's. + let rte = parse("air.convert.f.f32.s.i32.rte"); + assert_eq!( + Convert { + rounding: None, + ..rte + }, + plain + ); +} + #[test] fn format_and_operation_tables_are_total() { for &(format, want, lanes, _) in FORMATS { diff --git a/crates/metal2vulkan-air/tests/api_surface.txt b/crates/metal2vulkan-air/tests/api_surface.txt index d1331cb93..e12cb79b2 100644 --- a/crates/metal2vulkan-air/tests/api_surface.txt +++ b/crates/metal2vulkan-air/tests/api_surface.txt @@ -279,6 +279,7 @@ intrinsics Coherent use(atomic::Coherent) intrinsics CommandStage use(objects::CommandStage) intrinsics Component use(texture::Component) intrinsics Convert use(convert::Convert) +intrinsics ConvertRounding use(convert::ConvertRounding) intrinsics DeclClass enum(E) intrinsics Derivative use(stage::Derivative) intrinsics DerivativeOp use(stage::DerivativeOp) diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 68de0f7f0..34a9c7579 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -2583,6 +2583,15 @@ degradations! { /// flushes 16-bit denormals; a NaN's encoding (`0x7FFF` on the RTX 5080, where Metal /// answers `0x7E00`) and the sign of a zero result the device's. NativeHalfConversion = "native-half-conversion"; + /// Class: native device maths (a conversion). The device's correctly rounded conversion + /// answers an `air.convert` whose name rounds to nearest even (`.rte`, OpenCL's + /// `convert_float` of an `int`) and that can round (a 32- or 64-bit integer to `float`, a + /// 64-bit integer to `double`, a `double` to `float`), on a device whose float controls + /// cannot declare `RoundingModeRTE` for the result's width with the widths its + /// independence ties to it (MoltenVK). Recorded bound: the exact value's neighbour on + /// either side, one unit in the last place, the device's choice, where Metal answers the + /// nearer one, ties to even. + NativeConversionRounding = "native-conversion-rounding"; /// Class: native half rounding. The device's `OpQuantizeToF16` alone rounds the /// pipeline's `half` results computed in `float` on every device without `shaderFloat16`, /// its quantize measured or not; an integer's `half` rounds exactly. Recorded bound: a diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN index b79f2ba98..da8dd855f 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN +++ b/crates/metal2vulkan-validation/fixtures/public/engine/TOOLCHAIN @@ -175,6 +175,7 @@ fixture icb_vertex_encode_textured: metal: Apple metal version 32023.922 (metalf fixture ido_draw_order: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture kernel_bit_manipulation: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture kernel_block_read: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) +fixture kernel_convert_rounding_modes: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture kernel_frexp_ilogb: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425) fixture kernel_imageblock_wide_access: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) fixture kernel_inline_asm: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118) diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.air.ll b/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.air.ll new file mode 100644 index 000000000..7b63822f6 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.air.ll @@ -0,0 +1,718 @@ +; Generated by scripts/regen-air-fixtures from kernel_convert_rounding_modes.metal (typed pointers; xcrun metal-as reassembles it). Do not edit. +; ModuleID = '-' +source_filename = "-" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_int_to_float(i32 addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %0, i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %5 + %7 = load i32, i32 addrspace(1)* %6, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %8 = add i32 %2, 5 + %9 = urem i32 %8, %3 + %10 = zext i32 %9 to i64 + %11 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %10 + %12 = load i32, i32 addrspace(1)* %11, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %13 = insertelement <2 x i32> undef, i32 %12, i64 0 + %14 = insertelement <2 x i32> %13, i32 %7, i64 1 + %15 = bitcast <2 x i32> %14 to i64 + %16 = mul i32 %2, 31 + %17 = zext i32 %16 to i64 + %18 = getelementptr inbounds i32, i32 addrspace(1)* %1, i64 %17 + %19 = tail call fast float @air.convert.f.f32.s.i32.rte(i32 noundef %7) #2 + %20 = bitcast i32 addrspace(1)* %18 to float addrspace(1)* + store float %19, float addrspace(1)* %20, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %21 = tail call fast float @air.convert.f.f32.s.i32.rtz(i32 noundef %7) #2 + %22 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 1 + %23 = bitcast i32 addrspace(1)* %22 to float addrspace(1)* + store float %21, float addrspace(1)* %23, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %24 = tail call fast float @air.convert.f.f32.s.i32.rtp(i32 noundef %7) #2 + %25 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 2 + %26 = bitcast i32 addrspace(1)* %25 to float addrspace(1)* + store float %24, float addrspace(1)* %26, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %27 = tail call fast float @air.convert.f.f32.s.i32.rtn(i32 noundef %7) #2 + %28 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 3 + %29 = bitcast i32 addrspace(1)* %28 to float addrspace(1)* + store float %27, float addrspace(1)* %29, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %30 = tail call fast float @air.convert.f.f32.u.i32.rte(i32 noundef %7) #2 + %31 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 4 + %32 = bitcast i32 addrspace(1)* %31 to float addrspace(1)* + store float %30, float addrspace(1)* %32, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %33 = tail call fast float @air.convert.f.f32.u.i32.rtz(i32 noundef %7) #2 + %34 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 5 + %35 = bitcast i32 addrspace(1)* %34 to float addrspace(1)* + store float %33, float addrspace(1)* %35, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %36 = tail call fast float @air.convert.f.f32.u.i32.rtp(i32 noundef %7) #2 + %37 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 6 + %38 = bitcast i32 addrspace(1)* %37 to float addrspace(1)* + store float %36, float addrspace(1)* %38, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %39 = tail call fast float @air.convert.f.f32.u.i32.rtn(i32 noundef %7) #2 + %40 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 7 + %41 = bitcast i32 addrspace(1)* %40 to float addrspace(1)* + store float %39, float addrspace(1)* %41, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %42 = tail call fast half @air.convert.f.f16.s.i32.rte(i32 noundef %7) #2 + %43 = bitcast half %42 to i16 + %44 = zext i16 %43 to i32 + %45 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 8 + store i32 %44, i32 addrspace(1)* %45, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %46 = tail call fast half @air.convert.f.f16.s.i32.rtz(i32 noundef %7) #2 + %47 = bitcast half %46 to i16 + %48 = zext i16 %47 to i32 + %49 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 9 + store i32 %48, i32 addrspace(1)* %49, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %50 = tail call fast half @air.convert.f.f16.s.i32.rtp(i32 noundef %7) #2 + %51 = bitcast half %50 to i16 + %52 = zext i16 %51 to i32 + %53 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 10 + store i32 %52, i32 addrspace(1)* %53, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %54 = tail call fast half @air.convert.f.f16.s.i32.rtn(i32 noundef %7) #2 + %55 = bitcast half %54 to i16 + %56 = zext i16 %55 to i32 + %57 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 11 + store i32 %56, i32 addrspace(1)* %57, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %58 = trunc i32 %7 to i16 + %59 = tail call fast half @air.convert.f.f16.u.i16.rte(i16 noundef zeroext %58) #2 + %60 = bitcast half %59 to i16 + %61 = zext i16 %60 to i32 + %62 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 12 + store i32 %61, i32 addrspace(1)* %62, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %63 = tail call fast half @air.convert.f.f16.u.i16.rtz(i16 noundef zeroext %58) #2 + %64 = bitcast half %63 to i16 + %65 = zext i16 %64 to i32 + %66 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 13 + store i32 %65, i32 addrspace(1)* %66, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %67 = tail call fast half @air.convert.f.f16.u.i16.rtp(i16 noundef zeroext %58) #2 + %68 = bitcast half %67 to i16 + %69 = zext i16 %68 to i32 + %70 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 14 + store i32 %69, i32 addrspace(1)* %70, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %71 = tail call fast half @air.convert.f.f16.u.i16.rtn(i16 noundef zeroext %58) #2 + %72 = bitcast half %71 to i16 + %73 = zext i16 %72 to i32 + %74 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 15 + store i32 %73, i32 addrspace(1)* %74, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %75 = tail call fast float @air.convert.f.f32.s.i64.rte(i64 noundef %15) #2 + %76 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 16 + %77 = bitcast i32 addrspace(1)* %76 to float addrspace(1)* + store float %75, float addrspace(1)* %77, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %78 = tail call fast float @air.convert.f.f32.s.i64.rtz(i64 noundef %15) #2 + %79 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 17 + %80 = bitcast i32 addrspace(1)* %79 to float addrspace(1)* + store float %78, float addrspace(1)* %80, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %81 = tail call fast float @air.convert.f.f32.s.i64.rtp(i64 noundef %15) #2 + %82 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 18 + %83 = bitcast i32 addrspace(1)* %82 to float addrspace(1)* + store float %81, float addrspace(1)* %83, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %84 = tail call fast float @air.convert.f.f32.s.i64.rtn(i64 noundef %15) #2 + %85 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 19 + %86 = bitcast i32 addrspace(1)* %85 to float addrspace(1)* + store float %84, float addrspace(1)* %86, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %87 = tail call fast float @air.convert.f.f32.s.i16.rtz(i16 noundef signext %58) #2 + %88 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 20 + %89 = bitcast i32 addrspace(1)* %88 to float addrspace(1)* + store float %87, float addrspace(1)* %89, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %90 = trunc i32 %7 to i8 + %91 = tail call fast half @air.convert.f.f16.u.i8.rtp(i8 noundef zeroext %90) #2 + %92 = bitcast half %91 to i16 + %93 = zext i16 %92 to i32 + %94 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 21 + store i32 %93, i32 addrspace(1)* %94, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %95 = tail call fast half @air.convert.f.f16.s.i8.rtn(i8 noundef signext %90) #2 + %96 = bitcast half %95 to i16 + %97 = zext i16 %96 to i32 + %98 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 22 + store i32 %97, i32 addrspace(1)* %98, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %99 = bitcast i32 %7 to <4 x i8> + %100 = tail call fast <4 x float> @air.convert.f.v4f32.u.v4i8.rte(<4 x i8> noundef %99) #2 + %101 = insertelement <4 x i32> undef, i32 %7, i64 0 + %102 = add i32 %2, 1 + %103 = urem i32 %102, %3 + %104 = zext i32 %103 to i64 + %105 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %104 + %106 = load i32, i32 addrspace(1)* %105, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %107 = insertelement <4 x i32> %101, i32 %106, i64 1 + %108 = add i32 %2, 2 + %109 = urem i32 %108, %3 + %110 = zext i32 %109 to i64 + %111 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %110 + %112 = load i32, i32 addrspace(1)* %111, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %113 = insertelement <4 x i32> %107, i32 %112, i64 2 + %114 = add i32 %2, 3 + %115 = urem i32 %114, %3 + %116 = zext i32 %115 to i64 + %117 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %116 + %118 = load i32, i32 addrspace(1)* %117, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %119 = insertelement <4 x i32> %113, i32 %118, i64 3 + %120 = tail call fast <4 x float> @air.convert.f.v4f32.s.v4i32.rte(<4 x i32> noundef %119) #2 + %121 = bitcast <4 x float> %100 to <4 x i32> + %122 = extractelement <4 x i32> %121, i64 0 + %123 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 23 + store i32 %122, i32 addrspace(1)* %123, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %124 = extractelement <4 x i32> %121, i64 1 + %125 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 24 + store i32 %124, i32 addrspace(1)* %125, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %126 = extractelement <4 x i32> %121, i64 2 + %127 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 25 + store i32 %126, i32 addrspace(1)* %127, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %128 = extractelement <4 x i32> %121, i64 3 + %129 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 26 + store i32 %128, i32 addrspace(1)* %129, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %130 = bitcast <4 x float> %120 to <4 x i32> + %131 = extractelement <4 x i32> %130, i64 0 + %132 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 27 + store i32 %131, i32 addrspace(1)* %132, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %133 = extractelement <4 x i32> %130, i64 1 + %134 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 28 + store i32 %133, i32 addrspace(1)* %134, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %135 = extractelement <4 x i32> %130, i64 2 + %136 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 29 + store i32 %135, i32 addrspace(1)* %136, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %137 = extractelement <4 x i32> %130, i64 3 + %138 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 30 + store i32 %137, i32 addrspace(1)* %138, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + ret void +} + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rtp(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rtn(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rtp(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rtn(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rtp(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rtn(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rte(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rtz(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rtp(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rtn(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rte(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rtz(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rtp(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rtn(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i16.rtz(i16 noundef signext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i8.rtp(i8 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i8.rtn(i8 noundef signext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x float> @air.convert.f.v4f32.u.v4i8.rte(<4 x i8> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x float> @air.convert.f.v4f32.s.v4i32.rte(<4 x i32> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_float_to_int(i32 addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %0, i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %5 + %7 = bitcast i32 addrspace(1)* %6 to float addrspace(1)* + %8 = load float, float addrspace(1)* %7, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %9 = fptrunc float %8 to half + %10 = mul i32 %2, 24 + %11 = zext i32 %10 to i64 + %12 = getelementptr inbounds i32, i32 addrspace(1)* %1, i64 %11 + %13 = tail call i32 @air.convert.s.i32.f.f32.rte(float noundef %8) #2 + store i32 %13, i32 addrspace(1)* %12, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %14 = tail call i32 @air.convert.s.i32.f.f32.rtz(float noundef %8) #2 + %15 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 1 + store i32 %14, i32 addrspace(1)* %15, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %16 = tail call i32 @air.convert.s.i32.f.f32.rtp(float noundef %8) #2 + %17 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 2 + store i32 %16, i32 addrspace(1)* %17, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %18 = tail call i32 @air.convert.s.i32.f.f32.rtn(float noundef %8) #2 + %19 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 3 + store i32 %18, i32 addrspace(1)* %19, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %20 = tail call i32 @air.convert.s.i32.f.f32.rte.sat(float noundef %8) #2 + %21 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 4 + store i32 %20, i32 addrspace(1)* %21, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %22 = tail call i32 @air.convert.s.i32.f.f32.rtz.sat(float noundef %8) #2 + %23 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 5 + store i32 %22, i32 addrspace(1)* %23, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %24 = tail call i32 @air.convert.s.i32.f.f32.rtp.sat(float noundef %8) #2 + %25 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 6 + store i32 %24, i32 addrspace(1)* %25, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %26 = tail call i32 @air.convert.s.i32.f.f32.rtn.sat(float noundef %8) #2 + %27 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 7 + store i32 %26, i32 addrspace(1)* %27, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %28 = tail call i32 @air.convert.u.i32.f.f32.rtz(float noundef %8) #2 + %29 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 8 + store i32 %28, i32 addrspace(1)* %29, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %30 = tail call i32 @air.convert.u.i32.f.f32.rtz.sat(float noundef %8) #2 + %31 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 9 + store i32 %30, i32 addrspace(1)* %31, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %32 = tail call i32 @air.convert.u.i32.f.f32.rte.sat(float noundef %8) #2 + %33 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 10 + store i32 %32, i32 addrspace(1)* %33, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %34 = tail call zeroext i8 @air.convert.u.i8.f.f32.rte.sat(float noundef %8) #2 + %35 = zext i8 %34 to i32 + %36 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 11 + store i32 %35, i32 addrspace(1)* %36, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %37 = tail call signext i8 @air.convert.s.i8.f.f32.rtn.sat(float noundef %8) #2 + %38 = sext i8 %37 to i32 + %39 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 12 + store i32 %38, i32 addrspace(1)* %39, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %40 = tail call signext i16 @air.convert.s.i16.f.f16.rtp.sat(half noundef %9) #2 + %41 = sext i16 %40 to i32 + %42 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 13 + store i32 %41, i32 addrspace(1)* %42, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %43 = tail call zeroext i16 @air.convert.u.i16.f.f16.rte(half noundef %9) #2 + %44 = zext i16 %43 to i32 + %45 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 14 + store i32 %44, i32 addrspace(1)* %45, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %46 = tail call i64 @air.convert.s.i64.f.f32.rtz.sat(float noundef %8) #2 + %47 = bitcast i64 %46 to <2 x i32> + %48 = extractelement <2 x i32> %47, i64 0 + %49 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 15 + store i32 %48, i32 addrspace(1)* %49, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %50 = extractelement <2 x i32> %47, i64 1 + %51 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 16 + store i32 %50, i32 addrspace(1)* %51, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %52 = insertelement <4 x float> undef, float %8, i64 0 + %53 = add i32 %2, 1 + %54 = urem i32 %53, %3 + %55 = zext i32 %54 to i64 + %56 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %55 + %57 = bitcast i32 addrspace(1)* %56 to float addrspace(1)* + %58 = load float, float addrspace(1)* %57, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %59 = insertelement <4 x float> %52, float %58, i64 1 + %60 = add i32 %2, 2 + %61 = urem i32 %60, %3 + %62 = zext i32 %61 to i64 + %63 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %62 + %64 = bitcast i32 addrspace(1)* %63 to float addrspace(1)* + %65 = load float, float addrspace(1)* %64, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %66 = insertelement <4 x float> %59, float %65, i64 2 + %67 = add i32 %2, 3 + %68 = urem i32 %67, %3 + %69 = zext i32 %68 to i64 + %70 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %69 + %71 = bitcast i32 addrspace(1)* %70 to float addrspace(1)* + %72 = load float, float addrspace(1)* %71, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %73 = insertelement <4 x float> %66, float %72, i64 3 + %74 = tail call <4 x i32> @air.convert.s.v4i32.f.v4f32.rte(<4 x float> noundef %73) #2 + %75 = extractelement <4 x i32> %74, i64 0 + %76 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 17 + store i32 %75, i32 addrspace(1)* %76, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %77 = extractelement <4 x i32> %74, i64 1 + %78 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 18 + store i32 %77, i32 addrspace(1)* %78, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %79 = extractelement <4 x i32> %74, i64 2 + %80 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 19 + store i32 %79, i32 addrspace(1)* %80, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %81 = extractelement <4 x i32> %74, i64 3 + %82 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 20 + store i32 %81, i32 addrspace(1)* %82, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %83 = bitcast half %9 to i16 + %84 = zext i16 %83 to i32 + %85 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 21 + store i32 %84, i32 addrspace(1)* %85, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %86 = load i32, i32 addrspace(1)* %6, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %87 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 22 + store i32 %86, i32 addrspace(1)* %87, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %88 = getelementptr inbounds i32, i32 addrspace(1)* %12, i64 23 + store i32 1515870810, i32 addrspace(1)* %88, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + ret void +} + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rte(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtz(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtp(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtn(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rte.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtz.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtp.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtn.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.f.f32.rtz(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.f.f32.rtz.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.f.f32.rte.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.f.f32.rte.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i8 @air.convert.s.i8.f.f32.rtn.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i16 @air.convert.s.i16.f.f16.rtp.sat(half noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i16 @air.convert.u.i16.f.f16.rte(half noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i64 @air.convert.s.i64.f.f32.rtz.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x i32> @air.convert.s.v4i32.f.v4f32.rte(<4 x float> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_int_to_int(i32 addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %0, i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %5 + %7 = load i32, i32 addrspace(1)* %6, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %8 = add i32 %2, 7 + %9 = urem i32 %8, %3 + %10 = zext i32 %9 to i64 + %11 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %10 + %12 = load i32, i32 addrspace(1)* %11, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %13 = insertelement <2 x i32> undef, i32 %12, i64 0 + %14 = insertelement <2 x i32> %13, i32 %7, i64 1 + %15 = bitcast <2 x i32> %14 to i64 + %16 = mul i32 %2, 20 + %17 = zext i32 %16 to i64 + %18 = getelementptr inbounds i32, i32 addrspace(1)* %1, i64 %17 + %19 = tail call zeroext i8 @air.convert.u.i8.s.i32.rtz.sat(i32 noundef %7) #2 + %20 = zext i8 %19 to i32 + store i32 %20, i32 addrspace(1)* %18, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %21 = tail call signext i8 @air.convert.s.i8.s.i32.rtz.sat(i32 noundef %7) #2 + %22 = sext i8 %21 to i32 + %23 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 1 + store i32 %22, i32 addrspace(1)* %23, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %24 = tail call zeroext i8 @air.convert.u.i8.u.i32.rtz.sat(i32 noundef %7) #2 + %25 = zext i8 %24 to i32 + %26 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 2 + store i32 %25, i32 addrspace(1)* %26, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %27 = tail call signext i8 @air.convert.s.i8.u.i32.rtz.sat(i32 noundef %7) #2 + %28 = sext i8 %27 to i32 + %29 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 3 + store i32 %28, i32 addrspace(1)* %29, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %30 = tail call zeroext i16 @air.convert.u.i16.s.i32.rtz.sat(i32 noundef %7) #2 + %31 = zext i16 %30 to i32 + %32 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 4 + store i32 %31, i32 addrspace(1)* %32, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %33 = tail call signext i16 @air.convert.s.i16.u.i32.rtz.sat(i32 noundef %7) #2 + %34 = sext i16 %33 to i32 + %35 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 5 + store i32 %34, i32 addrspace(1)* %35, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %36 = tail call i32 @air.convert.u.i32.s.i32.rtz.sat(i32 noundef %7) #2 + %37 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 6 + store i32 %36, i32 addrspace(1)* %37, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %38 = tail call i32 @air.convert.s.i32.u.i32.rtz.sat(i32 noundef %7) #2 + %39 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 7 + store i32 %38, i32 addrspace(1)* %39, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %40 = tail call signext i16 @air.convert.s.i16.s.i32.rtz(i32 noundef %7) #2 + %41 = sext i16 %40 to i32 + %42 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 8 + store i32 %41, i32 addrspace(1)* %42, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %43 = tail call zeroext i8 @air.convert.u.i8.u.i32.rte(i32 noundef %7) #2 + %44 = zext i8 %43 to i32 + %45 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 9 + store i32 %44, i32 addrspace(1)* %45, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %46 = tail call i32 @air.convert.s.i32.s.i64.rtz.sat(i64 noundef %15) #2 + %47 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 10 + store i32 %46, i32 addrspace(1)* %47, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %48 = tail call i64 @air.convert.u.i64.s.i32.rtz.sat(i32 noundef %7) #2 + %49 = bitcast i64 %48 to <2 x i32> + %50 = extractelement <2 x i32> %49, i64 0 + %51 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 11 + store i32 %50, i32 addrspace(1)* %51, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %52 = extractelement <2 x i32> %49, i64 1 + %53 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 12 + store i32 %52, i32 addrspace(1)* %53, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %54 = tail call i64 @air.convert.s.i64.u.i64.rtz.sat(i64 noundef %15) #2 + %55 = bitcast i64 %54 to <2 x i32> + %56 = extractelement <2 x i32> %55, i64 0 + %57 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 13 + store i32 %56, i32 addrspace(1)* %57, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %58 = extractelement <2 x i32> %55, i64 1 + %59 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 14 + store i32 %58, i32 addrspace(1)* %59, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %60 = trunc i32 %7 to i8 + %61 = tail call i32 @air.convert.u.i32.s.i8.rtz.sat(i8 noundef signext %60) #2 + %62 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 15 + store i32 %61, i32 addrspace(1)* %62, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %63 = tail call i32 @air.convert.s.i32.u.i8.rtz.sat(i8 noundef zeroext %60) #2 + %64 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 16 + store i32 %63, i32 addrspace(1)* %64, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %65 = insertelement <4 x i32> undef, i32 %7, i64 0 + %66 = add i32 %2, 1 + %67 = urem i32 %66, %3 + %68 = zext i32 %67 to i64 + %69 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %68 + %70 = load i32, i32 addrspace(1)* %69, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %71 = insertelement <4 x i32> %65, i32 %70, i64 1 + %72 = add i32 %2, 2 + %73 = urem i32 %72, %3 + %74 = zext i32 %73 to i64 + %75 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %74 + %76 = load i32, i32 addrspace(1)* %75, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %77 = insertelement <4 x i32> %71, i32 %76, i64 2 + %78 = add i32 %2, 3 + %79 = urem i32 %78, %3 + %80 = zext i32 %79 to i64 + %81 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %80 + %82 = load i32, i32 addrspace(1)* %81, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %83 = insertelement <4 x i32> %77, i32 %82, i64 3 + %84 = tail call <4 x i8> @air.convert.u.v4i8.s.v4i32.rtz.sat(<4 x i32> noundef %83) #2 + %85 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 17 + %86 = bitcast i32 addrspace(1)* %85 to <4 x i8> addrspace(1)* + store <4 x i8> %84, <4 x i8> addrspace(1)* %86, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %87 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 18 + store i32 %7, i32 addrspace(1)* %87, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %88 = getelementptr inbounds i32, i32 addrspace(1)* %18, i64 19 + store i32 -1515870811, i32 addrspace(1)* %88, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + ret void +} + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i8 @air.convert.s.i8.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i8 @air.convert.s.i8.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i16 @air.convert.u.i16.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i16 @air.convert.s.i16.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i16 @air.convert.s.i16.s.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.u.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.s.i64.rtz.sat(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i64 @air.convert.u.i64.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i64 @air.convert.s.i64.u.i64.rtz.sat(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.s.i8.rtz.sat(i8 noundef signext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.u.i8.rtz.sat(i8 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x i8> @air.convert.u.v4i8.s.v4i32.rtz.sat(<4 x i32> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_float_to_float(i32 addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %0, i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %5 + %7 = bitcast i32 addrspace(1)* %6 to float addrspace(1)* + %8 = load float, float addrspace(1)* %7, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %9 = shl i32 %2, 3 + %10 = zext i32 %9 to i64 + %11 = getelementptr inbounds i32, i32 addrspace(1)* %1, i64 %10 + %12 = tail call fast half @air.convert.f.f16.f.f32.rte(float noundef %8) #2 + %13 = bitcast half %12 to i16 + %14 = zext i16 %13 to i32 + store i32 %14, i32 addrspace(1)* %11, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %15 = tail call fast half @air.convert.f.f16.f.f32.rtz(float noundef %8) #2 + %16 = bitcast half %15 to i16 + %17 = zext i16 %16 to i32 + %18 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 1 + store i32 %17, i32 addrspace(1)* %18, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %19 = tail call fast half @air.convert.f.f16.f.f32.rtp(float noundef %8) #2 + %20 = bitcast half %19 to i16 + %21 = zext i16 %20 to i32 + %22 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 2 + store i32 %21, i32 addrspace(1)* %22, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %23 = tail call fast half @air.convert.f.f16.f.f32.rtn(float noundef %8) #2 + %24 = bitcast half %23 to i16 + %25 = zext i16 %24 to i32 + %26 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 3 + store i32 %25, i32 addrspace(1)* %26, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %27 = tail call fast float @air.convert.f.f32.f.f16.rtz(half noundef %12) #2 + %28 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 4 + %29 = bitcast i32 addrspace(1)* %28 to float addrspace(1)* + store float %27, float addrspace(1)* %29, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %30 = insertelement <4 x float> undef, float %8, i64 0 + %31 = add i32 %2, 1 + %32 = urem i32 %31, %3 + %33 = zext i32 %32 to i64 + %34 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %33 + %35 = bitcast i32 addrspace(1)* %34 to float addrspace(1)* + %36 = load float, float addrspace(1)* %35, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %37 = insertelement <4 x float> %30, float %36, i64 1 + %38 = add i32 %2, 2 + %39 = urem i32 %38, %3 + %40 = zext i32 %39 to i64 + %41 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %40 + %42 = bitcast i32 addrspace(1)* %41 to float addrspace(1)* + %43 = load float, float addrspace(1)* %42, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %44 = insertelement <4 x float> %37, float %43, i64 2 + %45 = add i32 %2, 3 + %46 = urem i32 %45, %3 + %47 = zext i32 %46 to i64 + %48 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 %47 + %49 = bitcast i32 addrspace(1)* %48 to float addrspace(1)* + %50 = load float, float addrspace(1)* %49, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %51 = insertelement <4 x float> %44, float %50, i64 3 + %52 = tail call fast <4 x half> @air.convert.f.v4f16.f.v4f32.rtz(<4 x float> noundef %51) #2 + %53 = bitcast <4 x half> %52 to <2 x i32> + %54 = extractelement <2 x i32> %53, i64 0 + %55 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 5 + store i32 %54, i32 addrspace(1)* %55, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %56 = extractelement <2 x i32> %53, i64 1 + %57 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 6 + store i32 %56, i32 addrspace(1)* %57, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %58 = load i32, i32 addrspace(1)* %6, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %59 = getelementptr inbounds i32, i32 addrspace(1)* %11, i64 7 + store i32 %58, i32 addrspace(1)* %59, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + ret void +} + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rte(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rtz(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rtp(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rtn(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.f.f16.rtz(half noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x half> @air.convert.f.v4f16.f.v4f32.rtz(<4 x float> noundef) local_unnamed_addr #1 + +attributes #0 = { convergent mustprogress nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="128" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #1 = { convergent "approx-func-fp-math"="true" "frame-pointer"="all" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #2 = { convergent nobuiltin nounwind "no-builtins" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9, !16, !17, !18} +!air.compile_options = !{!19, !20, !21} +!llvm.ident = !{!22} +!air.version = !{!23} +!air.language_version = !{!24} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{void (i32 addrspace(1)*, i32 addrspace(1)*, i32, i32)* @kernel_convert_rounding_int_to_float, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15} +!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"in"} +!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"out"} +!14 = !{i32 2, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"i"} +!15 = !{i32 3, !"air.threads_per_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"n"} +!16 = !{void (i32 addrspace(1)*, i32 addrspace(1)*, i32, i32)* @kernel_convert_rounding_float_to_int, !10, !11} +!17 = !{void (i32 addrspace(1)*, i32 addrspace(1)*, i32, i32)* @kernel_convert_rounding_int_to_int, !10, !11} +!18 = !{void (i32 addrspace(1)*, i32 addrspace(1)*, i32, i32)* @kernel_convert_rounding_float_to_float, !10, !11} +!19 = !{!"air.compile.denorms_disable"} +!20 = !{!"air.compile.fast_math_enable"} +!21 = !{!"air.compile.framebuffer_fetch_enable"} +!22 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!23 = !{i32 2, i32 8, i32 0} +!24 = !{!"Metal", i32 4, i32 0, i32 0} +!25 = !{!26, !26, i64 0} +!26 = !{!"int", !27, i64 0} +!27 = !{!"omnipotent char", !28, i64 0} +!28 = !{!"Simple C++ TBAA"} +!29 = !{!30} +!30 = distinct !{!30, !31, !"air-alias-scope-arg(0)"} +!31 = distinct !{!31, !"air-alias-scopes(kernel_convert_rounding_int_to_float)"} +!32 = !{!33} +!33 = distinct !{!33, !31, !"air-alias-scope-arg(1)"} +!34 = !{!35} +!35 = distinct !{!35, !36, !"air-alias-scope-arg(0)"} +!36 = distinct !{!36, !"air-alias-scopes(kernel_convert_rounding_float_to_int)"} +!37 = !{!38} +!38 = distinct !{!38, !36, !"air-alias-scope-arg(1)"} +!39 = !{!40} +!40 = distinct !{!40, !41, !"air-alias-scope-arg(0)"} +!41 = distinct !{!41, !"air-alias-scopes(kernel_convert_rounding_int_to_int)"} +!42 = !{!43} +!43 = distinct !{!43, !41, !"air-alias-scope-arg(1)"} +!44 = !{!45} +!45 = distinct !{!45, !46, !"air-alias-scope-arg(0)"} +!46 = distinct !{!46, !"air-alias-scopes(kernel_convert_rounding_float_to_float)"} +!47 = !{!48} +!48 = distinct !{!48, !46, !"air-alias-scope-arg(1)"} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.ll b/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.ll new file mode 100644 index 000000000..36bce755d --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.ll @@ -0,0 +1,718 @@ +; Generated by scripts/regen-air-fixtures from kernel_convert_rounding_modes.metal (llvm-dis of kernel_convert_rounding_modes.air). Do not edit. +; ModuleID = '' +source_filename = "-" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_int_to_float(ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %5 + %7 = load i32, ptr addrspace(1) %6, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %8 = add i32 %2, 5 + %9 = urem i32 %8, %3 + %10 = zext i32 %9 to i64 + %11 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %10 + %12 = load i32, ptr addrspace(1) %11, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %13 = insertelement <2 x i32> undef, i32 %12, i64 0 + %14 = insertelement <2 x i32> %13, i32 %7, i64 1 + %15 = bitcast <2 x i32> %14 to i64 + %16 = mul i32 %2, 31 + %17 = zext i32 %16 to i64 + %18 = getelementptr inbounds i32, ptr addrspace(1) %1, i64 %17 + %19 = tail call fast float @air.convert.f.f32.s.i32.rte(i32 noundef %7) #2 + %20 = bitcast ptr addrspace(1) %18 to ptr addrspace(1) + store float %19, ptr addrspace(1) %20, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %21 = tail call fast float @air.convert.f.f32.s.i32.rtz(i32 noundef %7) #2 + %22 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 1 + %23 = bitcast ptr addrspace(1) %22 to ptr addrspace(1) + store float %21, ptr addrspace(1) %23, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %24 = tail call fast float @air.convert.f.f32.s.i32.rtp(i32 noundef %7) #2 + %25 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 2 + %26 = bitcast ptr addrspace(1) %25 to ptr addrspace(1) + store float %24, ptr addrspace(1) %26, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %27 = tail call fast float @air.convert.f.f32.s.i32.rtn(i32 noundef %7) #2 + %28 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 3 + %29 = bitcast ptr addrspace(1) %28 to ptr addrspace(1) + store float %27, ptr addrspace(1) %29, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %30 = tail call fast float @air.convert.f.f32.u.i32.rte(i32 noundef %7) #2 + %31 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 4 + %32 = bitcast ptr addrspace(1) %31 to ptr addrspace(1) + store float %30, ptr addrspace(1) %32, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %33 = tail call fast float @air.convert.f.f32.u.i32.rtz(i32 noundef %7) #2 + %34 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 5 + %35 = bitcast ptr addrspace(1) %34 to ptr addrspace(1) + store float %33, ptr addrspace(1) %35, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %36 = tail call fast float @air.convert.f.f32.u.i32.rtp(i32 noundef %7) #2 + %37 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 6 + %38 = bitcast ptr addrspace(1) %37 to ptr addrspace(1) + store float %36, ptr addrspace(1) %38, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %39 = tail call fast float @air.convert.f.f32.u.i32.rtn(i32 noundef %7) #2 + %40 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 7 + %41 = bitcast ptr addrspace(1) %40 to ptr addrspace(1) + store float %39, ptr addrspace(1) %41, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %42 = tail call fast half @air.convert.f.f16.s.i32.rte(i32 noundef %7) #2 + %43 = bitcast half %42 to i16 + %44 = zext i16 %43 to i32 + %45 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 8 + store i32 %44, ptr addrspace(1) %45, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %46 = tail call fast half @air.convert.f.f16.s.i32.rtz(i32 noundef %7) #2 + %47 = bitcast half %46 to i16 + %48 = zext i16 %47 to i32 + %49 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 9 + store i32 %48, ptr addrspace(1) %49, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %50 = tail call fast half @air.convert.f.f16.s.i32.rtp(i32 noundef %7) #2 + %51 = bitcast half %50 to i16 + %52 = zext i16 %51 to i32 + %53 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 10 + store i32 %52, ptr addrspace(1) %53, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %54 = tail call fast half @air.convert.f.f16.s.i32.rtn(i32 noundef %7) #2 + %55 = bitcast half %54 to i16 + %56 = zext i16 %55 to i32 + %57 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 11 + store i32 %56, ptr addrspace(1) %57, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %58 = trunc i32 %7 to i16 + %59 = tail call fast half @air.convert.f.f16.u.i16.rte(i16 noundef zeroext %58) #2 + %60 = bitcast half %59 to i16 + %61 = zext i16 %60 to i32 + %62 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 12 + store i32 %61, ptr addrspace(1) %62, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %63 = tail call fast half @air.convert.f.f16.u.i16.rtz(i16 noundef zeroext %58) #2 + %64 = bitcast half %63 to i16 + %65 = zext i16 %64 to i32 + %66 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 13 + store i32 %65, ptr addrspace(1) %66, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %67 = tail call fast half @air.convert.f.f16.u.i16.rtp(i16 noundef zeroext %58) #2 + %68 = bitcast half %67 to i16 + %69 = zext i16 %68 to i32 + %70 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 14 + store i32 %69, ptr addrspace(1) %70, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %71 = tail call fast half @air.convert.f.f16.u.i16.rtn(i16 noundef zeroext %58) #2 + %72 = bitcast half %71 to i16 + %73 = zext i16 %72 to i32 + %74 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 15 + store i32 %73, ptr addrspace(1) %74, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %75 = tail call fast float @air.convert.f.f32.s.i64.rte(i64 noundef %15) #2 + %76 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 16 + %77 = bitcast ptr addrspace(1) %76 to ptr addrspace(1) + store float %75, ptr addrspace(1) %77, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %78 = tail call fast float @air.convert.f.f32.s.i64.rtz(i64 noundef %15) #2 + %79 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 17 + %80 = bitcast ptr addrspace(1) %79 to ptr addrspace(1) + store float %78, ptr addrspace(1) %80, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %81 = tail call fast float @air.convert.f.f32.s.i64.rtp(i64 noundef %15) #2 + %82 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 18 + %83 = bitcast ptr addrspace(1) %82 to ptr addrspace(1) + store float %81, ptr addrspace(1) %83, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %84 = tail call fast float @air.convert.f.f32.s.i64.rtn(i64 noundef %15) #2 + %85 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 19 + %86 = bitcast ptr addrspace(1) %85 to ptr addrspace(1) + store float %84, ptr addrspace(1) %86, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %87 = tail call fast float @air.convert.f.f32.s.i16.rtz(i16 noundef signext %58) #2 + %88 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 20 + %89 = bitcast ptr addrspace(1) %88 to ptr addrspace(1) + store float %87, ptr addrspace(1) %89, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %90 = trunc i32 %7 to i8 + %91 = tail call fast half @air.convert.f.f16.u.i8.rtp(i8 noundef zeroext %90) #2 + %92 = bitcast half %91 to i16 + %93 = zext i16 %92 to i32 + %94 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 21 + store i32 %93, ptr addrspace(1) %94, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %95 = tail call fast half @air.convert.f.f16.s.i8.rtn(i8 noundef signext %90) #2 + %96 = bitcast half %95 to i16 + %97 = zext i16 %96 to i32 + %98 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 22 + store i32 %97, ptr addrspace(1) %98, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %99 = bitcast i32 %7 to <4 x i8> + %100 = tail call fast <4 x float> @air.convert.f.v4f32.u.v4i8.rte(<4 x i8> noundef %99) #2 + %101 = insertelement <4 x i32> undef, i32 %7, i64 0 + %102 = add i32 %2, 1 + %103 = urem i32 %102, %3 + %104 = zext i32 %103 to i64 + %105 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %104 + %106 = load i32, ptr addrspace(1) %105, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %107 = insertelement <4 x i32> %101, i32 %106, i64 1 + %108 = add i32 %2, 2 + %109 = urem i32 %108, %3 + %110 = zext i32 %109 to i64 + %111 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %110 + %112 = load i32, ptr addrspace(1) %111, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %113 = insertelement <4 x i32> %107, i32 %112, i64 2 + %114 = add i32 %2, 3 + %115 = urem i32 %114, %3 + %116 = zext i32 %115 to i64 + %117 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %116 + %118 = load i32, ptr addrspace(1) %117, align 4, !tbaa !25, !alias.scope !29, !noalias !32 + %119 = insertelement <4 x i32> %113, i32 %118, i64 3 + %120 = tail call fast <4 x float> @air.convert.f.v4f32.s.v4i32.rte(<4 x i32> noundef %119) #2 + %121 = bitcast <4 x float> %100 to <4 x i32> + %122 = extractelement <4 x i32> %121, i64 0 + %123 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 23 + store i32 %122, ptr addrspace(1) %123, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %124 = extractelement <4 x i32> %121, i64 1 + %125 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 24 + store i32 %124, ptr addrspace(1) %125, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %126 = extractelement <4 x i32> %121, i64 2 + %127 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 25 + store i32 %126, ptr addrspace(1) %127, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %128 = extractelement <4 x i32> %121, i64 3 + %129 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 26 + store i32 %128, ptr addrspace(1) %129, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %130 = bitcast <4 x float> %120 to <4 x i32> + %131 = extractelement <4 x i32> %130, i64 0 + %132 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 27 + store i32 %131, ptr addrspace(1) %132, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %133 = extractelement <4 x i32> %130, i64 1 + %134 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 28 + store i32 %133, ptr addrspace(1) %134, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %135 = extractelement <4 x i32> %130, i64 2 + %136 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 29 + store i32 %135, ptr addrspace(1) %136, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + %137 = extractelement <4 x i32> %130, i64 3 + %138 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 30 + store i32 %137, ptr addrspace(1) %138, align 4, !tbaa !25, !alias.scope !32, !noalias !29 + ret void +} + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rtp(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i32.rtn(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rtp(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.u.i32.rtn(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rtp(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i32.rtn(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rte(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rtz(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rtp(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i16.rtn(i16 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rte(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rtz(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rtp(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i64.rtn(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.s.i16.rtz(i16 noundef signext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.u.i8.rtp(i8 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.s.i8.rtn(i8 noundef signext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x float> @air.convert.f.v4f32.u.v4i8.rte(<4 x i8> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x float> @air.convert.f.v4f32.s.v4i32.rte(<4 x i32> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_float_to_int(ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %5 + %7 = bitcast ptr addrspace(1) %6 to ptr addrspace(1) + %8 = load float, ptr addrspace(1) %7, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %9 = fptrunc float %8 to half + %10 = mul i32 %2, 24 + %11 = zext i32 %10 to i64 + %12 = getelementptr inbounds i32, ptr addrspace(1) %1, i64 %11 + %13 = tail call i32 @air.convert.s.i32.f.f32.rte(float noundef %8) #2 + store i32 %13, ptr addrspace(1) %12, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %14 = tail call i32 @air.convert.s.i32.f.f32.rtz(float noundef %8) #2 + %15 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 1 + store i32 %14, ptr addrspace(1) %15, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %16 = tail call i32 @air.convert.s.i32.f.f32.rtp(float noundef %8) #2 + %17 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 2 + store i32 %16, ptr addrspace(1) %17, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %18 = tail call i32 @air.convert.s.i32.f.f32.rtn(float noundef %8) #2 + %19 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 3 + store i32 %18, ptr addrspace(1) %19, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %20 = tail call i32 @air.convert.s.i32.f.f32.rte.sat(float noundef %8) #2 + %21 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 4 + store i32 %20, ptr addrspace(1) %21, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %22 = tail call i32 @air.convert.s.i32.f.f32.rtz.sat(float noundef %8) #2 + %23 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 5 + store i32 %22, ptr addrspace(1) %23, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %24 = tail call i32 @air.convert.s.i32.f.f32.rtp.sat(float noundef %8) #2 + %25 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 6 + store i32 %24, ptr addrspace(1) %25, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %26 = tail call i32 @air.convert.s.i32.f.f32.rtn.sat(float noundef %8) #2 + %27 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 7 + store i32 %26, ptr addrspace(1) %27, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %28 = tail call i32 @air.convert.u.i32.f.f32.rtz(float noundef %8) #2 + %29 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 8 + store i32 %28, ptr addrspace(1) %29, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %30 = tail call i32 @air.convert.u.i32.f.f32.rtz.sat(float noundef %8) #2 + %31 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 9 + store i32 %30, ptr addrspace(1) %31, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %32 = tail call i32 @air.convert.u.i32.f.f32.rte.sat(float noundef %8) #2 + %33 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 10 + store i32 %32, ptr addrspace(1) %33, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %34 = tail call zeroext i8 @air.convert.u.i8.f.f32.rte.sat(float noundef %8) #2 + %35 = zext i8 %34 to i32 + %36 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 11 + store i32 %35, ptr addrspace(1) %36, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %37 = tail call signext i8 @air.convert.s.i8.f.f32.rtn.sat(float noundef %8) #2 + %38 = sext i8 %37 to i32 + %39 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 12 + store i32 %38, ptr addrspace(1) %39, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %40 = tail call signext i16 @air.convert.s.i16.f.f16.rtp.sat(half noundef %9) #2 + %41 = sext i16 %40 to i32 + %42 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 13 + store i32 %41, ptr addrspace(1) %42, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %43 = tail call zeroext i16 @air.convert.u.i16.f.f16.rte(half noundef %9) #2 + %44 = zext i16 %43 to i32 + %45 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 14 + store i32 %44, ptr addrspace(1) %45, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %46 = tail call i64 @air.convert.s.i64.f.f32.rtz.sat(float noundef %8) #2 + %47 = bitcast i64 %46 to <2 x i32> + %48 = extractelement <2 x i32> %47, i64 0 + %49 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 15 + store i32 %48, ptr addrspace(1) %49, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %50 = extractelement <2 x i32> %47, i64 1 + %51 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 16 + store i32 %50, ptr addrspace(1) %51, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %52 = insertelement <4 x float> undef, float %8, i64 0 + %53 = add i32 %2, 1 + %54 = urem i32 %53, %3 + %55 = zext i32 %54 to i64 + %56 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %55 + %57 = bitcast ptr addrspace(1) %56 to ptr addrspace(1) + %58 = load float, ptr addrspace(1) %57, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %59 = insertelement <4 x float> %52, float %58, i64 1 + %60 = add i32 %2, 2 + %61 = urem i32 %60, %3 + %62 = zext i32 %61 to i64 + %63 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %62 + %64 = bitcast ptr addrspace(1) %63 to ptr addrspace(1) + %65 = load float, ptr addrspace(1) %64, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %66 = insertelement <4 x float> %59, float %65, i64 2 + %67 = add i32 %2, 3 + %68 = urem i32 %67, %3 + %69 = zext i32 %68 to i64 + %70 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %69 + %71 = bitcast ptr addrspace(1) %70 to ptr addrspace(1) + %72 = load float, ptr addrspace(1) %71, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %73 = insertelement <4 x float> %66, float %72, i64 3 + %74 = tail call <4 x i32> @air.convert.s.v4i32.f.v4f32.rte(<4 x float> noundef %73) #2 + %75 = extractelement <4 x i32> %74, i64 0 + %76 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 17 + store i32 %75, ptr addrspace(1) %76, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %77 = extractelement <4 x i32> %74, i64 1 + %78 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 18 + store i32 %77, ptr addrspace(1) %78, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %79 = extractelement <4 x i32> %74, i64 2 + %80 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 19 + store i32 %79, ptr addrspace(1) %80, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %81 = extractelement <4 x i32> %74, i64 3 + %82 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 20 + store i32 %81, ptr addrspace(1) %82, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %83 = bitcast half %9 to i16 + %84 = zext i16 %83 to i32 + %85 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 21 + store i32 %84, ptr addrspace(1) %85, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %86 = load i32, ptr addrspace(1) %6, align 4, !tbaa !25, !alias.scope !34, !noalias !37 + %87 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 22 + store i32 %86, ptr addrspace(1) %87, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + %88 = getelementptr inbounds i32, ptr addrspace(1) %12, i64 23 + store i32 1515870810, ptr addrspace(1) %88, align 4, !tbaa !25, !alias.scope !37, !noalias !34 + ret void +} + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rte(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtz(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtp(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtn(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rte.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtz.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtp.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.f.f32.rtn.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.f.f32.rtz(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.f.f32.rtz.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.f.f32.rte.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.f.f32.rte.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i8 @air.convert.s.i8.f.f32.rtn.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i16 @air.convert.s.i16.f.f16.rtp.sat(half noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i16 @air.convert.u.i16.f.f16.rte(half noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i64 @air.convert.s.i64.f.f32.rtz.sat(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x i32> @air.convert.s.v4i32.f.v4f32.rte(<4 x float> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_int_to_int(ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %5 + %7 = load i32, ptr addrspace(1) %6, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %8 = add i32 %2, 7 + %9 = urem i32 %8, %3 + %10 = zext i32 %9 to i64 + %11 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %10 + %12 = load i32, ptr addrspace(1) %11, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %13 = insertelement <2 x i32> undef, i32 %12, i64 0 + %14 = insertelement <2 x i32> %13, i32 %7, i64 1 + %15 = bitcast <2 x i32> %14 to i64 + %16 = mul i32 %2, 20 + %17 = zext i32 %16 to i64 + %18 = getelementptr inbounds i32, ptr addrspace(1) %1, i64 %17 + %19 = tail call zeroext i8 @air.convert.u.i8.s.i32.rtz.sat(i32 noundef %7) #2 + %20 = zext i8 %19 to i32 + store i32 %20, ptr addrspace(1) %18, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %21 = tail call signext i8 @air.convert.s.i8.s.i32.rtz.sat(i32 noundef %7) #2 + %22 = sext i8 %21 to i32 + %23 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 1 + store i32 %22, ptr addrspace(1) %23, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %24 = tail call zeroext i8 @air.convert.u.i8.u.i32.rtz.sat(i32 noundef %7) #2 + %25 = zext i8 %24 to i32 + %26 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 2 + store i32 %25, ptr addrspace(1) %26, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %27 = tail call signext i8 @air.convert.s.i8.u.i32.rtz.sat(i32 noundef %7) #2 + %28 = sext i8 %27 to i32 + %29 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 3 + store i32 %28, ptr addrspace(1) %29, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %30 = tail call zeroext i16 @air.convert.u.i16.s.i32.rtz.sat(i32 noundef %7) #2 + %31 = zext i16 %30 to i32 + %32 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 4 + store i32 %31, ptr addrspace(1) %32, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %33 = tail call signext i16 @air.convert.s.i16.u.i32.rtz.sat(i32 noundef %7) #2 + %34 = sext i16 %33 to i32 + %35 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 5 + store i32 %34, ptr addrspace(1) %35, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %36 = tail call i32 @air.convert.u.i32.s.i32.rtz.sat(i32 noundef %7) #2 + %37 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 6 + store i32 %36, ptr addrspace(1) %37, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %38 = tail call i32 @air.convert.s.i32.u.i32.rtz.sat(i32 noundef %7) #2 + %39 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 7 + store i32 %38, ptr addrspace(1) %39, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %40 = tail call signext i16 @air.convert.s.i16.s.i32.rtz(i32 noundef %7) #2 + %41 = sext i16 %40 to i32 + %42 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 8 + store i32 %41, ptr addrspace(1) %42, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %43 = tail call zeroext i8 @air.convert.u.i8.u.i32.rte(i32 noundef %7) #2 + %44 = zext i8 %43 to i32 + %45 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 9 + store i32 %44, ptr addrspace(1) %45, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %46 = tail call i32 @air.convert.s.i32.s.i64.rtz.sat(i64 noundef %15) #2 + %47 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 10 + store i32 %46, ptr addrspace(1) %47, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %48 = tail call i64 @air.convert.u.i64.s.i32.rtz.sat(i32 noundef %7) #2 + %49 = bitcast i64 %48 to <2 x i32> + %50 = extractelement <2 x i32> %49, i64 0 + %51 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 11 + store i32 %50, ptr addrspace(1) %51, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %52 = extractelement <2 x i32> %49, i64 1 + %53 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 12 + store i32 %52, ptr addrspace(1) %53, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %54 = tail call i64 @air.convert.s.i64.u.i64.rtz.sat(i64 noundef %15) #2 + %55 = bitcast i64 %54 to <2 x i32> + %56 = extractelement <2 x i32> %55, i64 0 + %57 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 13 + store i32 %56, ptr addrspace(1) %57, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %58 = extractelement <2 x i32> %55, i64 1 + %59 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 14 + store i32 %58, ptr addrspace(1) %59, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %60 = trunc i32 %7 to i8 + %61 = tail call i32 @air.convert.u.i32.s.i8.rtz.sat(i8 noundef signext %60) #2 + %62 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 15 + store i32 %61, ptr addrspace(1) %62, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %63 = tail call i32 @air.convert.s.i32.u.i8.rtz.sat(i8 noundef zeroext %60) #2 + %64 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 16 + store i32 %63, ptr addrspace(1) %64, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %65 = insertelement <4 x i32> undef, i32 %7, i64 0 + %66 = add i32 %2, 1 + %67 = urem i32 %66, %3 + %68 = zext i32 %67 to i64 + %69 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %68 + %70 = load i32, ptr addrspace(1) %69, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %71 = insertelement <4 x i32> %65, i32 %70, i64 1 + %72 = add i32 %2, 2 + %73 = urem i32 %72, %3 + %74 = zext i32 %73 to i64 + %75 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %74 + %76 = load i32, ptr addrspace(1) %75, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %77 = insertelement <4 x i32> %71, i32 %76, i64 2 + %78 = add i32 %2, 3 + %79 = urem i32 %78, %3 + %80 = zext i32 %79 to i64 + %81 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %80 + %82 = load i32, ptr addrspace(1) %81, align 4, !tbaa !25, !alias.scope !39, !noalias !42 + %83 = insertelement <4 x i32> %77, i32 %82, i64 3 + %84 = tail call <4 x i8> @air.convert.u.v4i8.s.v4i32.rtz.sat(<4 x i32> noundef %83) #2 + %85 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 17 + %86 = bitcast ptr addrspace(1) %85 to ptr addrspace(1) + store <4 x i8> %84, ptr addrspace(1) %86, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %87 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 18 + store i32 %7, ptr addrspace(1) %87, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + %88 = getelementptr inbounds i32, ptr addrspace(1) %18, i64 19 + store i32 -1515870811, ptr addrspace(1) %88, align 4, !tbaa !25, !alias.scope !42, !noalias !39 + ret void +} + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i8 @air.convert.s.i8.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i8 @air.convert.s.i8.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i16 @air.convert.u.i16.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i16 @air.convert.s.i16.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.u.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare signext i16 @air.convert.s.i16.s.i32.rtz(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare zeroext i8 @air.convert.u.i8.u.i32.rte(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.s.i64.rtz.sat(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i64 @air.convert.u.i64.s.i32.rtz.sat(i32 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i64 @air.convert.s.i64.u.i64.rtz.sat(i64 noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.u.i32.s.i8.rtz.sat(i8 noundef signext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare i32 @air.convert.s.i32.u.i8.rtz.sat(i8 noundef zeroext) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x i8> @air.convert.u.v4i8.s.v4i32.rtz.sat(<4 x i32> noundef) local_unnamed_addr #1 + +; Function Attrs: convergent mustprogress nounwind +define void @kernel_convert_rounding_float_to_float(ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 { + %5 = zext i32 %2 to i64 + %6 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %5 + %7 = bitcast ptr addrspace(1) %6 to ptr addrspace(1) + %8 = load float, ptr addrspace(1) %7, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %9 = shl i32 %2, 3 + %10 = zext i32 %9 to i64 + %11 = getelementptr inbounds i32, ptr addrspace(1) %1, i64 %10 + %12 = tail call fast half @air.convert.f.f16.f.f32.rte(float noundef %8) #2 + %13 = bitcast half %12 to i16 + %14 = zext i16 %13 to i32 + store i32 %14, ptr addrspace(1) %11, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %15 = tail call fast half @air.convert.f.f16.f.f32.rtz(float noundef %8) #2 + %16 = bitcast half %15 to i16 + %17 = zext i16 %16 to i32 + %18 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 1 + store i32 %17, ptr addrspace(1) %18, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %19 = tail call fast half @air.convert.f.f16.f.f32.rtp(float noundef %8) #2 + %20 = bitcast half %19 to i16 + %21 = zext i16 %20 to i32 + %22 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 2 + store i32 %21, ptr addrspace(1) %22, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %23 = tail call fast half @air.convert.f.f16.f.f32.rtn(float noundef %8) #2 + %24 = bitcast half %23 to i16 + %25 = zext i16 %24 to i32 + %26 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 3 + store i32 %25, ptr addrspace(1) %26, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %27 = tail call fast float @air.convert.f.f32.f.f16.rtz(half noundef %12) #2 + %28 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 4 + %29 = bitcast ptr addrspace(1) %28 to ptr addrspace(1) + store float %27, ptr addrspace(1) %29, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %30 = insertelement <4 x float> undef, float %8, i64 0 + %31 = add i32 %2, 1 + %32 = urem i32 %31, %3 + %33 = zext i32 %32 to i64 + %34 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %33 + %35 = bitcast ptr addrspace(1) %34 to ptr addrspace(1) + %36 = load float, ptr addrspace(1) %35, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %37 = insertelement <4 x float> %30, float %36, i64 1 + %38 = add i32 %2, 2 + %39 = urem i32 %38, %3 + %40 = zext i32 %39 to i64 + %41 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %40 + %42 = bitcast ptr addrspace(1) %41 to ptr addrspace(1) + %43 = load float, ptr addrspace(1) %42, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %44 = insertelement <4 x float> %37, float %43, i64 2 + %45 = add i32 %2, 3 + %46 = urem i32 %45, %3 + %47 = zext i32 %46 to i64 + %48 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 %47 + %49 = bitcast ptr addrspace(1) %48 to ptr addrspace(1) + %50 = load float, ptr addrspace(1) %49, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %51 = insertelement <4 x float> %44, float %50, i64 3 + %52 = tail call fast <4 x half> @air.convert.f.v4f16.f.v4f32.rtz(<4 x float> noundef %51) #2 + %53 = bitcast <4 x half> %52 to <2 x i32> + %54 = extractelement <2 x i32> %53, i64 0 + %55 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 5 + store i32 %54, ptr addrspace(1) %55, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %56 = extractelement <2 x i32> %53, i64 1 + %57 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 6 + store i32 %56, ptr addrspace(1) %57, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + %58 = load i32, ptr addrspace(1) %6, align 4, !tbaa !25, !alias.scope !44, !noalias !47 + %59 = getelementptr inbounds i32, ptr addrspace(1) %11, i64 7 + store i32 %58, ptr addrspace(1) %59, align 4, !tbaa !25, !alias.scope !47, !noalias !44 + ret void +} + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rte(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rtz(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rtp(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare half @air.convert.f.f16.f.f32.rtn(float noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare float @air.convert.f.f32.f.f16.rtz(half noundef) local_unnamed_addr #1 + +; Function Attrs: convergent +declare <4 x half> @air.convert.f.v4f16.f.v4f32.rtz(<4 x float> noundef) local_unnamed_addr #1 + +attributes #0 = { convergent mustprogress nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="128" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #1 = { convergent "approx-func-fp-math"="true" "frame-pointer"="all" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" } +attributes #2 = { convergent nobuiltin nounwind "no-builtins" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9, !16, !17, !18} +!air.compile_options = !{!19, !20, !21} +!llvm.ident = !{!22} +!air.version = !{!23} +!air.language_version = !{!24} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{ptr @kernel_convert_rounding_int_to_float, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15} +!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"in"} +!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"out"} +!14 = !{i32 2, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"i"} +!15 = !{i32 3, !"air.threads_per_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"n"} +!16 = !{ptr @kernel_convert_rounding_float_to_int, !10, !11} +!17 = !{ptr @kernel_convert_rounding_int_to_int, !10, !11} +!18 = !{ptr @kernel_convert_rounding_float_to_float, !10, !11} +!19 = !{!"air.compile.denorms_disable"} +!20 = !{!"air.compile.fast_math_enable"} +!21 = !{!"air.compile.framebuffer_fetch_enable"} +!22 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!23 = !{i32 2, i32 8, i32 0} +!24 = !{!"Metal", i32 4, i32 0, i32 0} +!25 = !{!26, !26, i64 0} +!26 = !{!"int", !27, i64 0} +!27 = !{!"omnipotent char", !28, i64 0} +!28 = !{!"Simple C++ TBAA"} +!29 = !{!30} +!30 = distinct !{!30, !31, !"air-alias-scope-arg(0)"} +!31 = distinct !{!31, !"air-alias-scopes(kernel_convert_rounding_int_to_float)"} +!32 = !{!33} +!33 = distinct !{!33, !31, !"air-alias-scope-arg(1)"} +!34 = !{!35} +!35 = distinct !{!35, !36, !"air-alias-scope-arg(0)"} +!36 = distinct !{!36, !"air-alias-scopes(kernel_convert_rounding_float_to_int)"} +!37 = !{!38} +!38 = distinct !{!38, !36, !"air-alias-scope-arg(1)"} +!39 = !{!40} +!40 = distinct !{!40, !41, !"air-alias-scope-arg(0)"} +!41 = distinct !{!41, !"air-alias-scopes(kernel_convert_rounding_int_to_int)"} +!42 = !{!43} +!43 = distinct !{!43, !41, !"air-alias-scope-arg(1)"} +!44 = !{!45} +!45 = distinct !{!45, !46, !"air-alias-scope-arg(0)"} +!46 = distinct !{!46, !"air-alias-scopes(kernel_convert_rounding_float_to_float)"} +!47 = !{!48} +!48 = distinct !{!48, !46, !"air-alias-scope-arg(1)"} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.metal b/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.metal new file mode 100644 index 000000000..81f617db9 --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/engine/kernel_convert_rounding_modes.metal @@ -0,0 +1,301 @@ +// Owned fixture: `air.convert` with a rounding after its types (`.rte`, `.rtz`, `.rtp`, `.rtn`) +// and `.sat` after it, the forms Apple's OpenCL compiler emits for every `convert_[_sat] +// [_]`. MSL has no spelling for them, so the kernels call the `rc_*` functions this source +// declares and never defines, and the directives below rename every call to the intrinsic in both +// companions (scripts/regen-air-fixtures). Every operand is read from a buffer, so nothing folds. +#include +using namespace metal; + +// Integer to float. +float rc_f_si_rte(int x); +float rc_f_si_rtz(int x); +float rc_f_si_rtp(int x); +float rc_f_si_rtn(int x); +float rc_f_ui_rte(uint x); +float rc_f_ui_rtz(uint x); +float rc_f_ui_rtp(uint x); +float rc_f_ui_rtn(uint x); +half rc_h_si_rte(int x); +half rc_h_si_rtz(int x); +half rc_h_si_rtp(int x); +half rc_h_si_rtn(int x); +half rc_h_us_rte(ushort x); +half rc_h_us_rtz(ushort x); +half rc_h_us_rtp(ushort x); +half rc_h_us_rtn(ushort x); +float rc_f_sl_rte(long x); +float rc_f_sl_rtz(long x); +float rc_f_sl_rtp(long x); +float rc_f_sl_rtn(long x); +float rc_f_ss_rtz(short x); +half rc_h_uc_rtp(uchar x); +half rc_h_sc_rtn(char x); +float4 rc_f4_uc4_rte(uchar4 x); +float4 rc_f4_si4_rte(int4 x); + +// Float to integer. +int rc_si_f_rte(float x); +int rc_si_f_rtz(float x); +int rc_si_f_rtp(float x); +int rc_si_f_rtn(float x); +int rc_si_f_rte_sat(float x); +int rc_si_f_rtz_sat(float x); +int rc_si_f_rtp_sat(float x); +int rc_si_f_rtn_sat(float x); +uint rc_ui_f_rtz(float x); +uint rc_ui_f_rtz_sat(float x); +uint rc_ui_f_rte_sat(float x); +uchar rc_uc_f_rte_sat(float x); +char rc_sc_f_rtn_sat(float x); +short rc_ss_h_rtp_sat(half x); +ushort rc_us_h_rte(half x); +long rc_sl_f_rtz_sat(float x); +int4 rc_si4_f4_rte(float4 x); + +// Integer to integer. +uchar rc_uc_si_sat(int x); +char rc_sc_si_sat(int x); +uchar rc_uc_ui_sat(uint x); +char rc_sc_ui_sat(uint x); +ushort rc_us_si_sat(int x); +short rc_ss_ui_sat(uint x); +uint rc_ui_si_sat(int x); +int rc_si_ui_sat(uint x); +short rc_ss_si_rtz(int x); +uchar rc_uc_ui_rte(uint x); +int rc_si_sl_sat(long x); +ulong rc_ul_si_sat(int x); +long rc_sl_ul_sat(ulong x); +uint rc_ui_sc_sat(char x); +int rc_si_uc_sat(uchar x); +uchar4 rc_uc4_si4_sat(int4 x); + +// Float to float. +half rc_h_f_rte(float x); +half rc_h_f_rtz(float x); +half rc_h_f_rtp(float x); +half rc_h_f_rtn(float x); +float rc_f_h_rtz(half x); +half4 rc_h4_f4_rtz(float4 x); + +// air-rename: _Z11rc_f_si_rtei air.convert.f.f32.s.i32.rte +// air-rename: _Z11rc_f_si_rtzi air.convert.f.f32.s.i32.rtz +// air-rename: _Z11rc_f_si_rtpi air.convert.f.f32.s.i32.rtp +// air-rename: _Z11rc_f_si_rtni air.convert.f.f32.s.i32.rtn +// air-rename: _Z11rc_f_ui_rtej air.convert.f.f32.u.i32.rte +// air-rename: _Z11rc_f_ui_rtzj air.convert.f.f32.u.i32.rtz +// air-rename: _Z11rc_f_ui_rtpj air.convert.f.f32.u.i32.rtp +// air-rename: _Z11rc_f_ui_rtnj air.convert.f.f32.u.i32.rtn +// air-rename: _Z11rc_h_si_rtei air.convert.f.f16.s.i32.rte +// air-rename: _Z11rc_h_si_rtzi air.convert.f.f16.s.i32.rtz +// air-rename: _Z11rc_h_si_rtpi air.convert.f.f16.s.i32.rtp +// air-rename: _Z11rc_h_si_rtni air.convert.f.f16.s.i32.rtn +// air-rename: _Z11rc_h_us_rtet air.convert.f.f16.u.i16.rte +// air-rename: _Z11rc_h_us_rtzt air.convert.f.f16.u.i16.rtz +// air-rename: _Z11rc_h_us_rtpt air.convert.f.f16.u.i16.rtp +// air-rename: _Z11rc_h_us_rtnt air.convert.f.f16.u.i16.rtn +// air-rename: _Z11rc_f_sl_rtel air.convert.f.f32.s.i64.rte +// air-rename: _Z11rc_f_sl_rtzl air.convert.f.f32.s.i64.rtz +// air-rename: _Z11rc_f_sl_rtpl air.convert.f.f32.s.i64.rtp +// air-rename: _Z11rc_f_sl_rtnl air.convert.f.f32.s.i64.rtn +// air-rename: _Z11rc_f_ss_rtzs air.convert.f.f32.s.i16.rtz +// air-rename: _Z11rc_h_uc_rtph air.convert.f.f16.u.i8.rtp +// air-rename: _Z11rc_h_sc_rtnc air.convert.f.f16.s.i8.rtn +// air-rename: _Z13rc_f4_uc4_rteDv4_h air.convert.f.v4f32.u.v4i8.rte +// air-rename: _Z13rc_f4_si4_rteDv4_i air.convert.f.v4f32.s.v4i32.rte +// air-rename: _Z11rc_si_f_rtef air.convert.s.i32.f.f32.rte +// air-rename: _Z11rc_si_f_rtzf air.convert.s.i32.f.f32.rtz +// air-rename: _Z11rc_si_f_rtpf air.convert.s.i32.f.f32.rtp +// air-rename: _Z11rc_si_f_rtnf air.convert.s.i32.f.f32.rtn +// air-rename: _Z15rc_si_f_rte_satf air.convert.s.i32.f.f32.rte.sat +// air-rename: _Z15rc_si_f_rtz_satf air.convert.s.i32.f.f32.rtz.sat +// air-rename: _Z15rc_si_f_rtp_satf air.convert.s.i32.f.f32.rtp.sat +// air-rename: _Z15rc_si_f_rtn_satf air.convert.s.i32.f.f32.rtn.sat +// air-rename: _Z11rc_ui_f_rtzf air.convert.u.i32.f.f32.rtz +// air-rename: _Z15rc_ui_f_rtz_satf air.convert.u.i32.f.f32.rtz.sat +// air-rename: _Z15rc_ui_f_rte_satf air.convert.u.i32.f.f32.rte.sat +// air-rename: _Z15rc_uc_f_rte_satf air.convert.u.i8.f.f32.rte.sat +// air-rename: _Z15rc_sc_f_rtn_satf air.convert.s.i8.f.f32.rtn.sat +// air-rename: _Z15rc_ss_h_rtp_satDh air.convert.s.i16.f.f16.rtp.sat +// air-rename: _Z11rc_us_h_rteDh air.convert.u.i16.f.f16.rte +// air-rename: _Z15rc_sl_f_rtz_satf air.convert.s.i64.f.f32.rtz.sat +// air-rename: _Z13rc_si4_f4_rteDv4_f air.convert.s.v4i32.f.v4f32.rte +// air-rename: _Z12rc_uc_si_sati air.convert.u.i8.s.i32.rtz.sat +// air-rename: _Z12rc_sc_si_sati air.convert.s.i8.s.i32.rtz.sat +// air-rename: _Z12rc_uc_ui_satj air.convert.u.i8.u.i32.rtz.sat +// air-rename: _Z12rc_sc_ui_satj air.convert.s.i8.u.i32.rtz.sat +// air-rename: _Z12rc_us_si_sati air.convert.u.i16.s.i32.rtz.sat +// air-rename: _Z12rc_ss_ui_satj air.convert.s.i16.u.i32.rtz.sat +// air-rename: _Z12rc_ui_si_sati air.convert.u.i32.s.i32.rtz.sat +// air-rename: _Z12rc_si_ui_satj air.convert.s.i32.u.i32.rtz.sat +// air-rename: _Z12rc_ss_si_rtzi air.convert.s.i16.s.i32.rtz +// air-rename: _Z12rc_uc_ui_rtej air.convert.u.i8.u.i32.rte +// air-rename: _Z12rc_si_sl_satl air.convert.s.i32.s.i64.rtz.sat +// air-rename: _Z12rc_ul_si_sati air.convert.u.i64.s.i32.rtz.sat +// air-rename: _Z12rc_sl_ul_satm air.convert.s.i64.u.i64.rtz.sat +// air-rename: _Z12rc_ui_sc_satc air.convert.u.i32.s.i8.rtz.sat +// air-rename: _Z12rc_si_uc_sath air.convert.s.i32.u.i8.rtz.sat +// air-rename: _Z14rc_uc4_si4_satDv4_i air.convert.u.v4i8.s.v4i32.rtz.sat +// air-rename: _Z10rc_h_f_rtef air.convert.f.f16.f.f32.rte +// air-rename: _Z10rc_h_f_rtzf air.convert.f.f16.f.f32.rtz +// air-rename: _Z10rc_h_f_rtpf air.convert.f.f16.f.f32.rtp +// air-rename: _Z10rc_h_f_rtnf air.convert.f.f16.f.f32.rtn +// air-rename: _Z10rc_f_h_rtzDh air.convert.f.f32.f.f16.rtz +// air-rename: _Z12rc_h4_f4_rtzDv4_f air.convert.f.v4f16.f.v4f32.rtz + +static uint hbits(half h) { return uint(as_type(h)); } + +// Thread i of n converts in[i] (and, for the 64-bit and vector forms, its neighbours) every way, +// writing 31 words from out[31 * i]: the `float` of the signed word by rte, rtz, rtp, rtn; of the +// unsigned word; the `half` bits of the signed word; of its low 16 bits; the `float` of a 64-bit +// value (high word in[i], low word in[(i + 5) % n]) by rte, rtz, rtp, rtn; the `float` of its low +// 16 bits as a `short` (exact, by rtz); the `half` of its low byte (rtp, unsigned; rtn, signed); +// and `float4`s of its four bytes and of four words around it, by rte. +kernel void kernel_convert_rounding_int_to_float(device const uint *in [[buffer(0)]], + device uint *out [[buffer(1)]], + uint i [[thread_position_in_grid]], + uint n [[threads_per_grid]]) { + uint u = in[i]; + int s = as_type(u); + long l = as_type(uint2(in[(i + 5) % n], u)); + device uint *o = out + 31 * i; + o[0] = as_type(rc_f_si_rte(s)); + o[1] = as_type(rc_f_si_rtz(s)); + o[2] = as_type(rc_f_si_rtp(s)); + o[3] = as_type(rc_f_si_rtn(s)); + o[4] = as_type(rc_f_ui_rte(u)); + o[5] = as_type(rc_f_ui_rtz(u)); + o[6] = as_type(rc_f_ui_rtp(u)); + o[7] = as_type(rc_f_ui_rtn(u)); + o[8] = hbits(rc_h_si_rte(s)); + o[9] = hbits(rc_h_si_rtz(s)); + o[10] = hbits(rc_h_si_rtp(s)); + o[11] = hbits(rc_h_si_rtn(s)); + ushort us = ushort(u); + o[12] = hbits(rc_h_us_rte(us)); + o[13] = hbits(rc_h_us_rtz(us)); + o[14] = hbits(rc_h_us_rtp(us)); + o[15] = hbits(rc_h_us_rtn(us)); + o[16] = as_type(rc_f_sl_rte(l)); + o[17] = as_type(rc_f_sl_rtz(l)); + o[18] = as_type(rc_f_sl_rtp(l)); + o[19] = as_type(rc_f_sl_rtn(l)); + o[20] = as_type(rc_f_ss_rtz(short(u))); + o[21] = hbits(rc_h_uc_rtp(uchar(u))); + o[22] = hbits(rc_h_sc_rtn(char(u))); + float4 b = rc_f4_uc4_rte(as_type(u)); + float4 w = rc_f4_si4_rte(int4(s, as_type(in[(i + 1) % n]), as_type(in[(i + 2) % n]), + as_type(in[(i + 3) % n]))); + o[23] = as_type(b.x); + o[24] = as_type(b.y); + o[25] = as_type(b.z); + o[26] = as_type(b.w); + o[27] = as_type(w.x); + o[28] = as_type(w.y); + o[29] = as_type(w.z); + o[30] = as_type(w.w); +} + +// Thread i converts the `float` with bits in[i] to integers, writing 24 words from out[24 * i]: +// `int` by rte, rtz, rtp, rtn; the same saturating; `uint` by rtz, saturating rtz and rte; +// `uchar` (rte) and `char` (rtn) saturating; `short` saturating (rtp) and `ushort` (rte) of its +// `half`; the two words of a saturating `long` (rtz); an `int4` (rte) of it and its neighbours. +kernel void kernel_convert_rounding_float_to_int(device const uint *in [[buffer(0)]], + device uint *out [[buffer(1)]], + uint i [[thread_position_in_grid]], + uint n [[threads_per_grid]]) { + float x = as_type(in[i]); + half h = half(x); + device uint *o = out + 24 * i; + o[0] = as_type(rc_si_f_rte(x)); + o[1] = as_type(rc_si_f_rtz(x)); + o[2] = as_type(rc_si_f_rtp(x)); + o[3] = as_type(rc_si_f_rtn(x)); + o[4] = as_type(rc_si_f_rte_sat(x)); + o[5] = as_type(rc_si_f_rtz_sat(x)); + o[6] = as_type(rc_si_f_rtp_sat(x)); + o[7] = as_type(rc_si_f_rtn_sat(x)); + o[8] = rc_ui_f_rtz(x); + o[9] = rc_ui_f_rtz_sat(x); + o[10] = rc_ui_f_rte_sat(x); + o[11] = uint(rc_uc_f_rte_sat(x)); + o[12] = as_type(int(rc_sc_f_rtn_sat(x))); + o[13] = as_type(int(rc_ss_h_rtp_sat(h))); + o[14] = uint(rc_us_h_rte(h)); + uint2 q = as_type(rc_sl_f_rtz_sat(x)); + o[15] = q.x; + o[16] = q.y; + int4 v = rc_si4_f4_rte(float4(x, as_type(in[(i + 1) % n]), + as_type(in[(i + 2) % n]), as_type(in[(i + 3) % n]))); + o[17] = as_type(v.x); + o[18] = as_type(v.y); + o[19] = as_type(v.z); + o[20] = as_type(v.w); + o[21] = hbits(h); + o[22] = in[i]; + o[23] = 0x5a5a5a5au; +} + +// Thread i converts the word in[i] between integer types, writing 20 words from out[20 * i]: +// saturating to `uchar`, `char` from it as `int` and as `uint`; to `ushort` from `int`; to `short` +// from `uint`; to `uint` from `int` and back; the plain `short` of it (rtz) and `uchar` (rte); +// saturating `int` of a 64-bit value (high word in[i], low word in[(i + 7) % n]); the two words of +// the saturating `ulong` of its `int`; the two words of the saturating `long` of the 64-bit value +// as a `ulong`; saturating `uint` of its low byte as `char`; `int` of it as `uchar`; and the four +// saturating `uchar`s of it and its neighbours as `int`s, packed. +kernel void kernel_convert_rounding_int_to_int(device const uint *in [[buffer(0)]], + device uint *out [[buffer(1)]], + uint i [[thread_position_in_grid]], + uint n [[threads_per_grid]]) { + uint u = in[i]; + int s = as_type(u); + long l = as_type(uint2(in[(i + 7) % n], u)); + device uint *o = out + 20 * i; + o[0] = uint(rc_uc_si_sat(s)); + o[1] = as_type(int(rc_sc_si_sat(s))); + o[2] = uint(rc_uc_ui_sat(u)); + o[3] = as_type(int(rc_sc_ui_sat(u))); + o[4] = uint(rc_us_si_sat(s)); + o[5] = as_type(int(rc_ss_ui_sat(u))); + o[6] = rc_ui_si_sat(s); + o[7] = as_type(rc_si_ui_sat(u)); + o[8] = as_type(int(rc_ss_si_rtz(s))); + o[9] = uint(rc_uc_ui_rte(u)); + o[10] = as_type(rc_si_sl_sat(l)); + uint2 a = as_type(rc_ul_si_sat(s)); + o[11] = a.x; + o[12] = a.y; + uint2 b = as_type(rc_sl_ul_sat(as_type(l))); + o[13] = b.x; + o[14] = b.y; + o[15] = rc_ui_sc_sat(char(u)); + o[16] = as_type(rc_si_uc_sat(uchar(u))); + o[17] = as_type(rc_uc4_si4_sat(int4(s, as_type(in[(i + 1) % n]), + as_type(in[(i + 2) % n]), + as_type(in[(i + 3) % n])))); + o[18] = u; + o[19] = 0xa5a5a5a5u; +} + +// Thread i narrows the `float` with bits in[i] to `half` by rte, rtz, rtp and rtn, widens the +// first back by rtz, and narrows it and its neighbours as a `float4` by rtz: 8 words from +// out[8 * i], the `half`s as their bits. +kernel void kernel_convert_rounding_float_to_float(device const uint *in [[buffer(0)]], + device uint *out [[buffer(1)]], + uint i [[thread_position_in_grid]], + uint n [[threads_per_grid]]) { + float x = as_type(in[i]); + device uint *o = out + 8 * i; + half e = rc_h_f_rte(x); + o[0] = hbits(e); + o[1] = hbits(rc_h_f_rtz(x)); + o[2] = hbits(rc_h_f_rtp(x)); + o[3] = hbits(rc_h_f_rtn(x)); + o[4] = as_type(rc_f_h_rtz(e)); + half4 v = rc_h4_f4_rtz(float4(x, as_type(in[(i + 1) % n]), + as_type(in[(i + 2) % n]), as_type(in[(i + 3) % n]))); + uint2 p = as_type(v); + o[5] = p.x; + o[6] = p.y; + o[7] = in[i]; +} diff --git a/crates/metal2vulkan-validation/src/observation_contract/comparison.rs b/crates/metal2vulkan-validation/src/observation_contract/comparison.rs index 73ba0f0b5..292f70db1 100644 --- a/crates/metal2vulkan-validation/src/observation_contract/comparison.rs +++ b/crates/metal2vulkan-validation/src/observation_contract/comparison.rs @@ -329,6 +329,7 @@ pub fn allowance(note: PrecisionNote) -> Allowance { | N::DeviceTexelConversion | N::DeviceVertexConversion | N::NativeHalfConversion + | N::NativeConversionRounding | N::NativeHalfQuantize | N::NativeNanEncoding | N::NativeMinMax diff --git a/crates/metal2vulkan-validation/src/observation_contract/comparison/tests.rs b/crates/metal2vulkan-validation/src/observation_contract/comparison/tests.rs index b3b47f356..6bb224ae7 100644 --- a/crates/metal2vulkan-validation/src/observation_contract/comparison/tests.rs +++ b/crates/metal2vulkan-validation/src/observation_contract/comparison/tests.rs @@ -68,6 +68,7 @@ fn every_precision_note_has_its_table_row() { (N::DeviceColorConversion, Allowance::Recorded), (N::DeviceInterpolation, Allowance::Unbounded), (N::ExtrapolatedInterpolation, Allowance::Unbounded), + (N::NativeConversionRounding, Allowance::Unbounded), (N::QuadsSplitByClipping, Allowance::Unbounded), (N::VertexSimdgroupGrouping, Allowance::Unbounded), (N::FragmentSimdgroupGrouping, Allowance::Unbounded), diff --git a/crates/metal2vulkan/src/cache/codec.rs b/crates/metal2vulkan/src/cache/codec.rs index 90e064f32..6683755d9 100644 --- a/crates/metal2vulkan/src/cache/codec.rs +++ b/crates/metal2vulkan/src/cache/codec.rs @@ -1477,6 +1477,7 @@ codec_enum! { DeviceAlphaCoverage, DeviceInterpolationOffset, ExtrapolatedInterpolation, + NativeConversionRounding, } BindingSource { Buffer(f0), Texture(f0), Sampler(f0), CombinedTextureSampler { texture, sampler }, diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index bdd60cadb..1788a1630 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -70,7 +70,7 @@ pub const SEMANTICS_VERSION: u32 = 2023; /// re-typed in an encoded type, its `codec` body or its `envelope`. A variant removed while its tag /// stays unused needs no bump: an entry holding it fails to decode, which is a miss. The commit that /// bumps it says what changed; this comment keeps no history. -pub const ARTIFACT_FORMAT_VERSION: u32 = 500; +pub const ARTIFACT_FORMAT_VERSION: u32 = 501; /// The version of what this crate's runtime does with a compiled pipeline: instantiating a binding /// plan (`BindingPlan::instantiate_*`: descriptor ranges, push bytes, root, argument, and handle diff --git a/crates/metal2vulkan/src/lower/arith/math.rs b/crates/metal2vulkan/src/lower/arith/math.rs index 22a0042ed..5c9faf735 100644 --- a/crates/metal2vulkan/src/lower/arith/math.rs +++ b/crates/metal2vulkan/src/lower/arith/math.rs @@ -29,6 +29,7 @@ use crate::lower::arith::libm::{self, Lib}; use crate::lower::arith::nan::{self, NanTest}; use crate::lower::arith::narrow; use crate::lower::arith::ops::{fsplat, mask, ones, sext, splat, ty, CastKind}; +use crate::lower::arith::rounding; use crate::spirv::builder::FunctionBuilder; use crate::spirv::error::{BResult, BuildError, TypeRule}; use crate::spirv::ids::Value; @@ -2366,10 +2367,26 @@ pub enum ConvertKind { Float, /// Integer (`signed` source) to float. ToFloat { signed: bool }, - /// Float to integer, saturating: NaN to 0, out-of-range to the destination's bound. - ToInt { signed: bool }, + /// Float to integer, saturating: NaN to 0, out-of-range to the destination's bound, the float + /// first rounded to an integral value by `integral` (`air.convert`'s rounding). + ToInt { + signed: bool, + integral: rounding::Integral, + }, /// Integer (`signed` source) to integer. Int { signed: bool }, + /// Integer (`src_signed` source) to integer (`dst_signed`), saturating (`air.convert`'s + /// `.sat`): clamped to the range both share ([`rounding::saturate`]), then [`ConvertKind::Int`]. + SatInt { src_signed: bool, dst_signed: bool }, + /// Integer (`signed` source) to a float that cannot hold every value of it, rounded `dir` + /// (`air.convert`'s `.rtz`, `.rtp` and `.rtn`): exact by integer arithmetic + /// ([`rounding::int_to_float`]). + ToFloatDirected { + signed: bool, + dir: rounding::Directed, + }, + /// A float to a narrower float, rounded `dir` ([`rounding::narrow_float`]): exact. + FloatDirected(rounding::Directed), /// An 8-bit float (carried in an `i8`) to a float: exact. FromFp8(narrow::Fp8Layout), /// An 8-bit float (carried in an `i8`) to an integer: its value as an `f32` (exact, as @@ -2416,8 +2433,24 @@ pub fn convert( ConvertKind::Fp8ToInt { fp8, signed } => { let f = from.with(Held::Float(FloatFmt::F32)); let v = narrow::fp8_to_f32(fb, fp8, narrow::u32_form(from), f, x)?; - convert(fb, ConvertKind::ToInt { signed }, f, to, v, test) + let k = ConvertKind::ToInt { + signed, + integral: rounding::Integral::Truncate, + }; + convert(fb, k, f, to, v, test) } + ConvertKind::SatInt { + src_signed, + dst_signed, + } => { + let c = rounding::saturate(fb, (src_signed, dst_signed), from, to, x)?; + let k = ConvertKind::Int { signed: src_signed }; + convert(fb, k, from, to, c, test) + } + ConvertKind::ToFloatDirected { signed, dir } => { + rounding::int_to_float(fb, (signed, dir), from, to, x) + } + ConvertKind::FloatDirected(dir) => rounding::narrow_float(fb, from, to, x, dir), ConvertKind::Float => cast(fb, CastKind::FpResize, from, to, x), // A held integer is zero-extended in its container, so the container is zero exactly // when the value is. @@ -2460,7 +2493,9 @@ pub fn convert( } cast(fb, c, from, to, x) } - ConvertKind::ToInt { signed } => { + ConvertKind::ToInt { signed, integral } => { + // Rounded to an integral value first; the NaN test reads the operand itself. + let v = rounding::integral(fb, integral, x)?; let n = u32::from(to.bits()); // A narrow float is exactly its container's value: clamp in the container. let fw = from.container(); @@ -2475,7 +2510,7 @@ pub fn convert( }; let hi_bits = below_pow2_bits(fw, k); let hi = splat(fb, from, hi_bits)?; - let c = glsl(fb, GlslOp::NClamp, &[x, lo, hi])?; + let c = glsl(fb, GlslOp::NClamp, &[v, lo, hi])?; let t = ty(fb, to)?; let op = if signed { ConvOp::ConvertFToS @@ -2488,7 +2523,7 @@ pub fn convert( // to it); otherwise it is selected. if float_value(fw, hi_bits).trunc() < (2f64.powi(k as i32) - 1.0) { let limit = splat(fb, from, pow2_bits(fw, k))?; - let over = bin(fb, BinOp::FOrdGreaterThanEqual, x, limit)?; + let over = bin(fb, BinOp::FOrdGreaterThanEqual, v, limit)?; let max = splat(fb, to, ones(if signed { to.bits() - 1 } else { to.bits() }))?; r = fb.select(over, max, r)?; } diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs index 1065191ea..dcdf58343 100644 --- a/crates/metal2vulkan/src/lower/arith/mod.rs +++ b/crates/metal2vulkan/src/lower/arith/mod.rs @@ -81,6 +81,7 @@ pub mod narrow; pub mod ops; pub mod output_precision; pub mod pack; +pub mod rounding; pub mod srgb; pub mod texel_round; pub mod wide; @@ -448,6 +449,14 @@ impl FamilyStrategy for ArithStrategy { kind: ConvertKind::ToInt { .. }, .. } => "arith.convert.saturating", + S::Convert { + kind: ConvertKind::SatInt { .. }, + .. + } => "arith.convert.saturating_int", + S::Convert { + kind: ConvertKind::ToFloatDirected { .. } | ConvertKind::FloatDirected(_), + .. + } => "arith.convert.directed", S::Convert { kind: ConvertKind::FromFp8(_), .. @@ -536,6 +545,17 @@ impl FamilyStrategy for ArithStrategy { from, to, } => 320 + 160 + conversion_words(from.with(Held::Float(FloatFmt::F32)), *to), + // The clamp's (at most three) instructions and constants, and its mask. + S::Convert { + kind: ConvertKind::SatInt { .. }, + from, + to, + } => 240 + conversion_words(*from, *to), + S::Convert { + kind: ConvertKind::ToFloatDirected { .. } | ConvertKind::FloatDirected(_), + from, + to, + } => 160 + conversion_words(*from, *to) + rounding::directed_words(*from, *to), S::Convert { from, to, .. } => 160 + conversion_words(*from, *to), S::Pack { dir, @@ -732,6 +752,16 @@ pub struct ArithPlan { /// [`pack::measured`]), recorded at selection: the module then requires /// `VkProperty::MetalPackConversions`. pub metal_packs: bool, + /// The float widths (32 or 64) of the conversions that round to nearest even by their name + /// (`air.convert`'s `.rte`) and can round (an integer of more bits than the format's + /// significand, a `double` to `float`), recorded at selection; [`finish`] decides + /// [`ArithPlan::rounding_modes`] from them. + pub rte: BTreeSet, + /// The widths whose `RoundingModeRTE` execution mode the entry declares, decided by [`finish`]: + /// those [`ArithPlan::rte`] needs and the device's float controls tie to them + /// (`narrow::rte_modes`), where the profile supports them all; elsewhere the conversions are + /// the device's (`PrecisionNote::NativeConversionRounding`). + pub rounding_modes: BTreeSet, /// The precision notes the plan's operations record unless their entry's quotient and /// functions are Metal's own (the profile's `float` functions are Metal's and Metal compiles /// the entry with fast math), recorded at selection ([`OpNotes::unless_metal_fast`]); @@ -774,7 +804,10 @@ pub const FAST_DIVIDE: &str = "arith.fdiv.fast"; /// Complete the arith family's plan once every instruction and parameter is selected: declare /// `SignedZeroInfNanPreserve` for each required width the profile supports, and note /// `DeviceSignedZeroInfNan` for one it does not (the device's behavior is accepted and -/// reported); then, with the entry's execution modes known, decide whether precise `fmod` takes +/// reported); declare `RoundingModeRTE` for the widths whose conversions round to nearest even by +/// their name where the device has it ([`ArithPlan::rounding_modes`]), and note +/// `NativeConversionRounding` where it does not; then, with the entry's execution modes known, +/// decide whether precise `fmod` takes /// its routine ([`ArithPlan::fmod`]: on a profile whose `float` functions are Metal's, not in an /// entry Metal compiles with precise functions, [`libm::module_mode`]), record the notes of the /// operations that are Metal's own in a fast-math entry only where it is not @@ -815,6 +848,18 @@ pub fn finish(cx: &mut SelectCx<'_>) -> Result<(), Error> { cx.precision.insert(PrecisionNote::DeviceSignedZeroInfNan); } } + // A conversion that rounds to nearest even by its name: under `RoundingModeRTE` for its width + // (and the widths the device ties to it) where the device has them all, the device's + // correctly rounded conversion elsewhere. + let rte: Vec = cx.families.arith.rte.iter().copied().collect(); + for w in rte { + match narrow::rte_modes(cx.profile, w) { + Some(widths) => cx.families.arith.rounding_modes.extend(widths), + None => { + cx.precision.insert(PrecisionNote::NativeConversionRounding); + } + } + } let arith = &mut cx.families.arith; // The measured device fact, never the driver's name. arith.metal_functions = match cx.profile.fields().float_functions { @@ -852,8 +897,8 @@ pub struct Declared { } /// Declare the arith family's module-scope objects and entry execution modes: one function per -/// shared division routine the plan calls, and one `SignedZeroInfNanPreserve` per width the plan -/// selected. +/// shared division routine the plan calls, one `SignedZeroInfNanPreserve` per width the plan +/// selected, and one `RoundingModeRTE` per width of [`ArithPlan::rounding_modes`]. pub fn declare( mb: &mut ModuleBuilder, plan: &Plan, @@ -862,6 +907,9 @@ pub fn declare( for w in &plan.families.arith.modes { modes.push(ExecMode::SignedZeroInfNanPreserve { width: *w }); } + for w in &plan.families.arith.rounding_modes { + modes.push(ExecMode::RoundingModeRte { width: *w }); + } let mut decl = Declared::default(); for d in &plan.families.arith.divides { decl.divides.insert(*d, wide::define_divide(mb, *d)?); @@ -2734,22 +2782,38 @@ fn select_convert( return Ok(ArithStrategy::Convert { kind, from, to }); } let kind = match (c.dst_kind, c.src_kind) { - (NumKind::Float, NumKind::Float) => ConvertKind::Float, - (NumKind::Float, s) => ConvertKind::ToFloat { - signed: s == NumKind::Signed, - }, - (d, NumKind::Float) => ConvertKind::ToInt { - signed: d == NumKind::Signed, + (NumKind::Float, NumKind::Float) => float_convert(cx, c, from, to)?, + (NumKind::Float, s) => int_to_float_convert(cx, c, s == NumKind::Signed, from, to)?, + (d, NumKind::Float) => { + let integral = rounding::Integral::of(c.rounding); + // `RoundEven` reads a denormal as `rint` does, `Ceil` and `Floor` as they do. + match integral { + rounding::Integral::Truncate => {} + rounding::Integral::Even => leave_denormals(cx, from, denormal::Covered::Deviation), + rounding::Integral::Up | rounding::Integral::Down => { + leave_denormals(cx, from, denormal::Covered::Unary) + } + } + ConvertKind::ToInt { + signed: d == NumKind::Signed, + integral, + } + } + (d, s) if c.saturate => ConvertKind::SatInt { + src_signed: s == NumKind::Signed, + dst_signed: d == NumKind::Signed, }, (_, s) => ConvertKind::Int { signed: s == NumKind::Signed, }, }; let consistent = match kind { - ConvertKind::Float => from.is_float() && to.is_float(), - ConvertKind::ToFloat { .. } => !from.is_float() && to.is_float(), + ConvertKind::Float | ConvertKind::FloatDirected(_) => from.is_float() && to.is_float(), + ConvertKind::ToFloat { .. } | ConvertKind::ToFloatDirected { .. } => { + !from.is_float() && to.is_float() + } ConvertKind::ToInt { .. } => from.is_float() && to.is_int(), - ConvertKind::Int { .. } => !from.is_float() && to.is_int(), + ConvertKind::Int { .. } | ConvertKind::SatInt { .. } => !from.is_float() && to.is_int(), ConvertKind::FromFp8(_) | ConvertKind::Fp8ToInt { .. } | ConvertKind::ToBool @@ -2771,6 +2835,82 @@ fn select_convert( Ok(with_nan(cx, s, nan)) } +/// The kind of a float-to-float `air.convert` from `from` to `to` with `c`'s rounding: the plain +/// conversion, which is exact for a widening and rounds to nearest even as Metal's `fptrunc` does +/// for a narrowing (`.rte` asks no more of it, but that a `double` narrowed to `float` rounds so +/// only where the entry declares `RoundingModeRTE` for 32 bits, [`ArithPlan::rte`]); or, for a +/// narrowing toward zero or an infinity, [`ConvertKind::FloatDirected`], which Vulkan has no +/// conversion of. +fn float_convert( + cx: &mut SelectCx<'_>, + c: &Convert, + from: Form, + to: Form, +) -> Result { + let (Some(src), Some(dst)) = (from.float_fmt(), to.float_fmt()) else { + return Err(cx.invariant("a float air.convert of a non-float form")); + }; + let widening = matches!( + (src, dst), + ( + FloatFmt::Half | FloatFmt::BFloat, + FloatFmt::F32 | FloatFmt::F64 + ) | (FloatFmt::F32, FloatFmt::F64) + ); + if src == dst || widening { + return Ok(ConvertKind::Float); + } + match rounding::Directed::of(c.rounding) { + None => { + if c.rounding.is_some() && (src, dst) == (FloatFmt::F64, FloatFmt::F32) { + cx.families.arith.rte.insert(32); + } + Ok(ConvertKind::Float) + } + Some(dir) if rounding::narrows(src, dst) => Ok(ConvertKind::FloatDirected(dir)), + // A `bfloat` narrowing by a directed rounding: Apple's OpenCL compiler emits none. + Some(_) => Err(not_lowered()), + } +} + +/// The kind of an integer-to-float `air.convert` (a `signed` source) from `from` to `to` with +/// `c`'s rounding. One whose every source value is a value of the destination cannot round: the +/// plain conversion, whatever the rounding. Elsewhere, nearest even is the plain conversion too, +/// which rounds so where the entry declares `RoundingModeRTE` for the result's width +/// ([`ArithPlan::rte`]; a `half` or `bfloat` result is rounded by integer arithmetic already); and +/// toward zero or an infinity is [`ConvertKind::ToFloatDirected`]. +fn int_to_float_convert( + cx: &mut SelectCx<'_>, + c: &Convert, + signed: bool, + from: Form, + to: Form, +) -> Result { + let plain = ConvertKind::ToFloat { signed }; + let Some(dst) = to.float_fmt() else { + return Err(cx.invariant("an integer air.convert to a non-float form")); + }; + if c.rounding.is_none() || rounding::converts_exactly(from, signed, dst) { + return Ok(plain); + } + match (rounding::Directed::of(c.rounding), dst) { + (None, FloatFmt::F32 | FloatFmt::F64) => { + cx.families.arith.rte.insert(dst.container()); + Ok(plain) + } + (None, _) => Ok(plain), + (Some(dir), FloatFmt::Half | FloatFmt::F32) => { + Ok(ConvertKind::ToFloatDirected { signed, dir }) + } + // Only a 64-bit integer rounds to `double`. + (Some(dir), FloatFmt::F64) if from.container() == 64 => { + Ok(ConvertKind::ToFloatDirected { signed, dir }) + } + // A `bfloat` result by a directed rounding: Apple's OpenCL compiler emits none. + (Some(_), _) => Err(not_lowered()), + } +} + // ---- Emission ---- fn operand(cx: &mut EmitCx<'_>, fb: &mut FunctionBuilder<'_>, v: Value) -> BResult { diff --git a/crates/metal2vulkan/src/lower/arith/narrow.rs b/crates/metal2vulkan/src/lower/arith/narrow.rs index 800f97607..af8e1b595 100644 --- a/crates/metal2vulkan/src/lower/arith/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/narrow.rs @@ -275,6 +275,30 @@ pub fn controlled_modes(p: &DeviceProfile) -> Option> { Some(modes) } +/// The widths whose `RoundingModeRTE` an entry declares on profile `p` so that a conversion with a +/// `width`-bit float result rounds to nearest even, or `None` where the profile lacks one: `width` +/// itself, and the widths the device's rounding-mode independence ties to it (RuntimeSpirv VUIDs +/// 06291 and 06292): 16 and 64 together where rounding modes are independent for 32 bits only and +/// `width` is not 32, every width where they are not independent. The Vulkan spec ("Precision and +/// Operation of SPIR-V Instructions") rounds a correctly rounded operation "according to IEEE 754 +/// roundTiesToEven" in an entry that declares it, and to either neighbour, the device's choice, +/// elsewhere. +pub fn rte_modes(p: &DeviceProfile, width: u8) -> Option> { + let fc = p.fields().float_controls; + let has = |w: u8| match w { + 16 => fc.f16.rounding_rte, + 32 => fc.f32.rounding_rte, + 64 => fc.f64.rounding_rte, + _ => false, + }; + let widths = match (fc.rounding_independence, width) { + (Independence::All, w) | (Independence::Only32Bit, w @ 32) => vec![w], + (Independence::Only32Bit, _) => vec![16, 64], + (Independence::None, _) => vec![16, 32, 64], + }; + (has(width) && widths.iter().all(|w| has(*w))).then_some(widths) +} + /// Whether module `m` rounds to `half` through [`converted`] or [`controlled`]: an `OpFConvert` of /// an `f32` to 16 bits whose result another widens back. The module then records /// `PrecisionNote::NativeHalfConversion` ([`QuantizeForm::Converted`]) or declares diff --git a/crates/metal2vulkan/src/lower/arith/rounding.rs b/crates/metal2vulkan/src/lower/arith/rounding.rs new file mode 100644 index 000000000..85034d0e5 --- /dev/null +++ b/crates/metal2vulkan/src/lower/arith/rounding.rs @@ -0,0 +1,609 @@ +//! `air.convert` with a rounding after its types (`.rte`, `.rtz`, `.rtp`, `.rtn`) and `.sat` after +//! it: the forms Apple's OpenCL compiler emits for every `convert_[_sat][_]`. +//! +//! - **To an integer** from a float: the float rounded to an integral value first ([`Integral`]: +//! the conversion's own truncation, or `RoundEven`, `Ceil` or `Floor`, each exact), then the +//! saturating conversion every `air.convert` to an integer takes (`math::ConvertKind::ToInt`). +//! `.sat` adds nothing to it. +//! - **To an integer** from an integer: a rounding is nothing to an integer, and `.sat` clamps to +//! the range both types share ([`saturate`], [`sat_bounds`]). +//! - **To a float**, rounding to nearest even (`.rte`): the conversion every `air.convert` takes, +//! which rounds that way where the entry declares `RoundingModeRTE` for the result's width +//! (`narrow::rte_modes`) and else is the device's choice of the two neighbours +//! (`PrecisionNote::NativeConversionRounding`). A conversion that cannot round needs neither. +//! - **To a float**, rounding toward zero or an infinity ([`Directed`]): Vulkan has no such +//! conversion (its `FPRoundingMode` decoration is for 16-bit stores only, and an entry's +//! `RoundingModeRTZ` would round its arithmetic too), so the result is computed exactly by +//! integer arithmetic on the operand's bits ([`int_to_float`], [`narrow_float`]); a conversion +//! that cannot round is the plain one. + +use crate::air::intrinsics::ConvertRounding; +use crate::lower::arith::legal::{FloatFmt, Form, Held}; +use crate::lower::arith::narrow::{self, u32_form}; +use crate::lower::arith::ops::{self, fsplat, mask, sext, splat, ty}; +use crate::spirv::builder::FunctionBuilder; +use crate::spirv::error::{BResult, BuildError, OperandRule}; +use crate::spirv::ids::Value; +use crate::spirv::ir::{BinOp, ConvOp, GlslOp, UnOp}; + +/// How a float is rounded to an integral value before `air.convert`'s saturating conversion to an +/// integer. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Integral { + /// The conversion's own truncation: `rtz`, and the conversion without a rounding. + Truncate, + /// `RoundEven` first: `rte`. + Even, + /// `Ceil` first: `rtp`. + Up, + /// `Floor` first: `rtn`. + Down, +} + +impl Integral { + /// The rounding a conversion to an integer names. + pub fn of(rounding: Option) -> Integral { + match rounding { + None | Some(ConvertRounding::Rtz) => Integral::Truncate, + Some(ConvertRounding::Rte) => Integral::Even, + Some(ConvertRounding::Rtp) => Integral::Up, + Some(ConvertRounding::Rtn) => Integral::Down, + } + } +} + +/// `x` (a float of any form) rounded to an integral value by `k`: the device's `RoundEven`, +/// `Ceil` or `Floor`, each exact; `x` itself for [`Integral::Truncate`]. +pub fn integral(fb: &mut FunctionBuilder<'_>, k: Integral, x: Value) -> BResult { + let op = match k { + Integral::Truncate => return Ok(x), + Integral::Even => GlslOp::RoundEven, + Integral::Up => GlslOp::Ceil, + Integral::Down => GlslOp::Floor, + }; + fb.glsl(op, &[x]) +} + +/// A rounding of a conversion to a float toward zero or an infinity, which Vulkan gives no +/// conversion of its own. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Directed { + /// `rtz`: toward zero. + Zero, + /// `rtp`: toward positive infinity. + Up, + /// `rtn`: toward negative infinity. + Down, +} + +impl Directed { + /// The directed rounding a conversion to a float names, or `None` for nearest even and for + /// the conversion without a rounding. + pub fn of(rounding: Option) -> Option { + match rounding { + None | Some(ConvertRounding::Rte) => None, + Some(ConvertRounding::Rtz) => Some(Directed::Zero), + Some(ConvertRounding::Rtp) => Some(Directed::Up), + Some(ConvertRounding::Rtn) => Some(Directed::Down), + } + } +} + +/// Where a magnitude rounds away from zero: never, always, or in the lanes a `bool` value holds. +#[derive(Clone, Copy)] +enum Away { + Never, + Always, + Where(Value), +} + +/// Where a lane rounded `dir` moves its magnitude away from zero, given which lanes are negative +/// (`None` for an unsigned source, never negative). +fn away(fb: &mut FunctionBuilder<'_>, dir: Directed, neg: Option) -> BResult { + Ok(match (dir, neg) { + (Directed::Zero, _) | (Directed::Down, None) => Away::Never, + (Directed::Up, None) => Away::Always, + (Directed::Up, Some(n)) => Away::Where(fb.unary(UnOp::LogicalNot, n)?), + (Directed::Down, Some(n)) => Away::Where(n), + }) +} + +fn bin(fb: &mut FunctionBuilder<'_>, op: BinOp, a: Value, b: Value) -> BResult { + fb.binary(op, a, b) +} + +/// A pair of forms selection never hands a directed conversion. +fn unconverted() -> BuildError { + BuildError::Operand { + op: crate::spv::Op::ConvertUToF, + rule: OperandRule::Width(0), + } +} + +/// `t` (`m >> s`, lanes of form `f`) plus one in the lanes that round away and lost a set bit of +/// `m` to the shift. +fn bump( + fb: &mut FunctionBuilder<'_>, + f: Form, + (m, t, s): (Value, Value, Value), + away: Away, +) -> BResult { + let lost = match away { + Away::Never => return Ok(t), + _ => { + let kept = bin(fb, BinOp::ShiftLeftLogical, t, s)?; + bin(fb, BinOp::INotEqual, kept, m)? + } + }; + let up = match away { + Away::Where(c) => bin(fb, BinOp::LogicalAnd, c, lost)?, + _ => lost, + }; + let (one, zero) = (splat(fb, f, 1)?, splat(fb, f, 0)?); + let d = fb.select(up, one, zero)?; + bin(fb, BinOp::IAdd, t, d) +} + +/// The significand bits of float format `fmt` (the leading one counted). +fn significand(fmt: FloatFmt) -> u8 { + match fmt { + FloatFmt::Half => 11, + FloatFmt::BFloat => 8, + FloatFmt::F32 => 24, + FloatFmt::F64 => 53, + } +} + +/// Whether every integer of form `from` (read as signed when `signed`) is a value of float format +/// `to`, so no conversion to it rounds: its magnitude has at most the format's significand bits +/// (`half`'s largest such integer, 2048, is within its range). +pub fn converts_exactly(from: Form, signed: bool, to: FloatFmt) -> bool { + let magnitude = if signed { + from.bits().saturating_sub(1) + } else { + from.bits() + }; + magnitude <= significand(to) +} + +/// Unsigned magnitude `m` (`u32` lanes of form `u`) as an `f32` (lanes of form `to`) rounded +/// toward zero, or away from it where `away`: the bits below the 24 the significand keeps are +/// dropped, and one is added to what is kept where any of them was set and the lane rounds away. +/// The kept part (at most `2^24`) converts exactly and scales back exactly by `2^s`. +fn magnitude32( + fb: &mut FunctionBuilder<'_>, + u: Form, + to: Form, + m: Value, + away: Away, +) -> BResult { + let msb = fb.glsl(GlslOp::FindUMsb, &[m])?; + let k23 = splat(fb, u, 23)?; + let s = bin(fb, BinOp::ISub, msb, k23)?; + let z = splat(fb, u, 0)?; + let s = fb.glsl(GlslOp::SMax, &[s, z])?; + let t = bin(fb, BinOp::ShiftRightLogical, m, s)?; + let t = bump(fb, u, (m, t, s), away)?; + let ft = ty(fb, to.with(Held::Float(FloatFmt::F32)))?; + let f = fb.convert(ConvOp::ConvertUToF, t, ft)?; + fb.glsl(GlslOp::Ldexp, &[f, s]) +} + +/// Unsigned magnitude `m` (`u32` lanes of form `u`) of an integer that is negative in the lanes +/// `neg` holds (`None`: none) as the `f32` (lanes of form `to`) of its magnitude rounded `dir`, its +/// sign not applied ([`magnitude32`]). +pub fn magnitude_f32( + fb: &mut FunctionBuilder<'_>, + u: Form, + to: Form, + (m, neg): (Value, Option), + dir: Directed, +) -> BResult { + let a = away(fb, dir, neg)?; + magnitude32(fb, u, to, m, a) +} + +/// Unsigned 64-bit magnitude `m` (`u64` lanes of form `w`) as a float of form `to` (`f32` or +/// `f64`) rounded toward zero, or away from it where `away`, as [`magnitude32`] does for 32 bits: +/// the most significant bit is found from the two halves, the bits below the format's significand +/// are dropped (one added where any was set and the lane rounds away), and the kept part converts +/// exactly and scales back exactly. +fn magnitude64( + fb: &mut FunctionBuilder<'_>, + w: Form, + to: Form, + m: Value, + away: Away, +) -> BResult { + let keep = match to.float_fmt() { + Some(fmt @ (FloatFmt::F32 | FloatFmt::F64)) => u64::from(significand(fmt)), + _ => return Err(unconverted()), + }; + let u = u32_form(w); + let ut = ty(fb, u)?; + let wt = ty(fb, w)?; + let lo = fb.convert(ConvOp::UConvert, m, ut)?; + let k32w = splat(fb, w, 32)?; + let hi = bin(fb, BinOp::ShiftRightLogical, m, k32w)?; + let hi = fb.convert(ConvOp::UConvert, hi, ut)?; + let mlo = fb.glsl(GlslOp::FindUMsb, &[lo])?; + let mhi = fb.glsl(GlslOp::FindUMsb, &[hi])?; + let k32 = splat(fb, u, 32)?; + let mhi = bin(fb, BinOp::IAdd, mhi, k32)?; + let z = splat(fb, u, 0)?; + let hz = bin(fb, BinOp::IEqual, hi, z)?; + let msb = fb.select(hz, mlo, mhi)?; + let below = splat(fb, u, keep - 1)?; + let s = bin(fb, BinOp::ISub, msb, below)?; + let s = fb.glsl(GlslOp::SMax, &[s, z])?; + let s64 = fb.convert(ConvOp::UConvert, s, wt)?; + let t = bin(fb, BinOp::ShiftRightLogical, m, s64)?; + let t = bump(fb, w, (m, t, s64), away)?; + let ft = ty(fb, to)?; + let f = fb.convert(ConvOp::ConvertUToF, t, ft)?; + fb.glsl(GlslOp::Ldexp, &[f, s]) +} + +/// Integer `x` of form `from` (read as signed when `signed`) converted to float form `to` (`half`, +/// `float`, or `double` from a 64-bit integer), rounded `dir`, exactly on every device. +/// +/// A `half` is the integer's `f32` (exact below `2^24`, and at least `2^24` elsewhere, past +/// `half`'s range whichever way the device rounds it) rounded to `half` by [`to_half`]. A `float` +/// or `double` is the magnitude rounded by integer arithmetic ([`magnitude32`], [`magnitude64`]), +/// given its sign. +pub fn int_to_float( + fb: &mut FunctionBuilder<'_>, + (signed, dir): (bool, Directed), + from: Form, + to: Form, + x: Value, +) -> BResult { + if to.float_fmt() == Some(FloatFmt::Half) { + let f32f = to.with(Held::Float(FloatFmt::F32)); + let v = ops::int_to_float(fb, signed, from, f32f, x)?; + return to_half(fb, f32f, v, dir); + } + let (m, neg) = if signed { + let s = sext(fb, from, x)?; + let z = splat(fb, from, 0)?; + let neg = bin(fb, BinOp::SLessThan, s, z)?; + (fb.glsl(GlslOp::SAbs, &[s])?, Some(neg)) + } else { + (x, None) + }; + let a = away(fb, dir, neg)?; + let r = if from.container() == 64 { + let w = from.with(Held::Int { + bits: 64, + container: 64, + }); + magnitude64(fb, w, to, m, a)? + } else if to.float_fmt() == Some(FloatFmt::F32) { + magnitude32(fb, u32_form(from), to, m, a)? + } else { + return Err(unconverted()); + }; + match neg { + Some(n) => { + let nr = fb.unary(UnOp::FNegate, r)?; + fb.select(n, nr, r) + } + None => Ok(r), + } +} + +/// The widened `half` NaN of a conversion that rounds a NaN `float` to `half` here: the canonical +/// `0x7e00`, Metal's answer to a `float` narrowed to `half` (`nan::convert_answer`). +const HALF_NAN: u64 = 0x7FC0_0000; + +/// `x` (`f32` lanes of form `f`) rounded to `half` `dir`, held widened in `f32`, by integer +/// arithmetic on its bits, exactly on every device: a normal `half` keeps the top 11 of the 24 +/// significand bits (adding one unit below them first where the lane rounds away), a result past +/// 65504 is the infinity when the lane rounds away and 65504 when it does not, and below `2^-14` +/// the value is the multiple of `2^-24` the exact significand gives, shifted right and rounded the +/// same way (an `f32` denormal operand counted by its value). A zero's sign and an infinity are +/// kept; a NaN is [`HALF_NAN`]. +pub fn to_half(fb: &mut FunctionBuilder<'_>, f: Form, x: Value, dir: Directed) -> BResult { + let u = u32_form(f); + let ut = ty(fb, u)?; + let b = fb.convert(ConvOp::Bitcast, x, ut)?; + let sign_mask = splat(fb, u, 0x8000_0000)?; + let sign = bin(fb, BinOp::BitwiseAnd, b, sign_mask)?; + let abs_mask = splat(fb, u, 0x7FFF_FFFF)?; + let a = bin(fb, BinOp::BitwiseAnd, b, abs_mask)?; + let z = splat(fb, u, 0)?; + let neg = bin(fb, BinOp::INotEqual, sign, z)?; + let away = away(fb, dir, Some(neg))?; + // A normal half: the top 11 significand bits, one unit below them added first to round away. + let r = match away { + Away::Never => a, + Away::Always | Away::Where(_) => { + let unit = splat(fb, u, 0x1FFF)?; + let up = bin(fb, BinOp::IAdd, a, unit)?; + match away { + Away::Where(c) => fb.select(c, up, a)?, + _ => up, + } + } + }; + let keep = splat(fb, u, 0xFFFF_E000)?; + let r = bin(fb, BinOp::BitwiseAnd, r, keep)?; + // Past the largest half: its infinity rounding away, 65504 otherwise. + let largest = splat(fb, u, 0x477F_E000)?; + let over = bin(fb, BinOp::UGreaterThan, r, largest)?; + let inf = splat(fb, u, 0x7F80_0000)?; + let cap = match away { + Away::Never => largest, + Away::Always => inf, + Away::Where(c) => fb.select(c, inf, largest)?, + }; + let r = fb.select(over, cap, r)?; + // Below the smallest normal half: the significand shifted to units of 2^-24. + let k23 = splat(fb, u, 23)?; + let e = bin(fb, BinOp::ShiftRightLogical, a, k23)?; + let frac_mask = splat(fb, u, 0x007F_FFFF)?; + let frac = bin(fb, BinOp::BitwiseAnd, a, frac_mask)?; + let has_one = bin(fb, BinOp::INotEqual, e, z)?; + let one_bit = splat(fb, u, 0x0080_0000)?; + let lead = fb.select(has_one, one_bit, z)?; + let sig = bin(fb, BinOp::BitwiseOr, frac, lead)?; + let one = splat(fb, u, 1)?; + let e1 = fb.glsl(GlslOp::UMax, &[e, one])?; + let k126 = splat(fb, u, 126)?; + let sh = bin(fb, BinOp::ISub, k126, e1)?; + let k31 = splat(fb, u, 31)?; + let sh = fb.glsl(GlslOp::UMin, &[sh, k31])?; + let q = bin(fb, BinOp::ShiftRightLogical, sig, sh)?; + let q = bump(fb, u, (sig, q, sh), away)?; + let ft = ty(fb, f)?; + let qf = fb.convert(ConvOp::ConvertUToF, q, ft)?; + let unit = fsplat(fb, f, 2f64.powi(-24))?; + let small_value = bin(fb, BinOp::FMul, qf, unit)?; + let small_bits = fb.convert(ConvOp::Bitcast, small_value, ut)?; + let min_normal = splat(fb, u, 0x3880_0000)?; + let small = bin(fb, BinOp::ULessThan, a, min_normal)?; + let r = fb.select(small, small_bits, r)?; + // An infinity keeps its bits; a NaN is the canonical half NaN. + let is_inf = bin(fb, BinOp::IEqual, a, inf)?; + let r = fb.select(is_inf, a, r)?; + let r = bin(fb, BinOp::BitwiseOr, r, sign)?; + let is_nan = bin(fb, BinOp::UGreaterThan, a, inf)?; + let nan = splat(fb, u, HALF_NAN)?; + let r = fb.select(is_nan, nan, r)?; + fb.convert(ConvOp::Bitcast, r, ft) +} + +/// `x` (`double` lanes of form `from`) converted to `f32` rounded `dir`: the device's conversion, +/// which returns one of the two `f32`s around `x` (Vulkan's correctly rounded conversion in any +/// mode), stepped one `f32` to the other where it lies on the wrong side of `x` (the step from a +/// zero is to the smallest denormal of the direction's sign). A NaN is kept. +fn f64_to_f32(fb: &mut FunctionBuilder<'_>, from: Form, x: Value, dir: Directed) -> BResult { + let f32f = from.with(Held::Float(FloatFmt::F32)); + let u = u32_form(from); + let t32 = ty(fb, f32f)?; + let ut = ty(fb, u)?; + let t64 = ty(fb, from)?; + let r = fb.convert(ConvOp::FConvert, x, t32)?; + let back = fb.convert(ConvOp::FConvert, r, t64)?; + let rb = fb.convert(ConvOp::Bitcast, r, ut)?; + let one = splat(fb, u, 1)?; + let less = bin(fb, BinOp::ISub, rb, one)?; + let more = bin(fb, BinOp::IAdd, rb, one)?; + let rb = match dir { + Directed::Zero => { + let ab = fb.glsl(GlslOp::FAbs, &[back])?; + let ax = fb.glsl(GlslOp::FAbs, &[x])?; + let past = bin(fb, BinOp::FOrdGreaterThan, ab, ax)?; + fb.select(past, less, rb)? + } + Directed::Up | Directed::Down => { + let (wrong, from_zero, toward) = if dir == Directed::Up { + (BinOp::FOrdLessThan, 0x0000_0001, (less, more)) + } else { + (BinOp::FOrdGreaterThan, 0x8000_0001, (more, less)) + }; + let wrong = bin(fb, wrong, back, x)?; + let k31 = splat(fb, u, 31)?; + let sign = bin(fb, BinOp::ShiftRightLogical, rb, k31)?; + let z = splat(fb, u, 0)?; + let neg = bin(fb, BinOp::INotEqual, sign, z)?; + // Toward the direction's infinity: a negative's magnitude shrinks for `Up`. + let step = fb.select(neg, toward.0, toward.1)?; + let abs_mask = splat(fb, u, 0x7FFF_FFFF)?; + let mag = bin(fb, BinOp::BitwiseAnd, rb, abs_mask)?; + let zero = bin(fb, BinOp::IEqual, mag, z)?; + let from_zero = splat(fb, u, from_zero)?; + let step = fb.select(zero, from_zero, step)?; + fb.select(wrong, step, rb)? + } + }; + fb.convert(ConvOp::Bitcast, rb, t32) +} + +/// Float `x` of form `from` narrowed to float form `to`, rounded `dir`, exactly: `float` to `half` +/// ([`to_half`]), `double` to `float` ([`f64_to_f32`]), and `double` to `half` through an `f32` +/// rounded to odd (`narrow::f64_to_f32_odd`), whose one rounding after it to `half` is the exact +/// value's, as any rounding to a format of at most 22 significand bits is. +pub fn narrow_float( + fb: &mut FunctionBuilder<'_>, + from: Form, + to: Form, + x: Value, + dir: Directed, +) -> BResult { + match (from.float_fmt(), to.float_fmt()) { + (Some(FloatFmt::F32), Some(FloatFmt::Half)) => to_half(fb, from, x, dir), + (Some(FloatFmt::F64), Some(FloatFmt::F32)) => f64_to_f32(fb, from, x, dir), + (Some(FloatFmt::F64), Some(FloatFmt::Half)) => { + let o = narrow::f64_to_f32_odd(fb, from, x)?; + to_half(fb, from.with(Held::Float(FloatFmt::F32)), o, dir) + } + _ => Err(unconverted()), + } +} + +/// Whether [`narrow_float`] narrows from `from` to `to`. +pub fn narrows(from: FloatFmt, to: FloatFmt) -> bool { + matches!( + (from, to), + (FloatFmt::F32, FloatFmt::Half) + | (FloatFmt::F64, FloatFmt::F32) + | (FloatFmt::F64, FloatFmt::Half) + ) +} + +/// The range of an integer of `bits` bits, signed or not. +fn range(bits: u8, signed: bool) -> (i128, i128) { + let bits = u32::from(bits.clamp(1, 64)); + if signed { + (-(1i128 << (bits - 1)), (1i128 << (bits - 1)) - 1) + } else { + (0, (1i128 << bits) - 1) + } +} + +/// The bounds a saturating conversion from an integer of `from` bits (signed when `src_signed`) to +/// one of `to` bits (signed when `dst_signed`) clamps its operand to, each `None` where the +/// operand's own range already lies within it: the larger of the two lower bounds and the smaller +/// of the two upper ones. +pub fn sat_bounds( + (from, src_signed): (u8, bool), + (to, dst_signed): (u8, bool), +) -> (Option, Option) { + let (smin, smax) = range(from, src_signed); + let (dmin, dmax) = range(to, dst_signed); + let lo = smin.max(dmin); + let hi = smax.min(dmax); + ((lo > smin).then_some(lo), (hi < smax).then_some(hi)) +} + +/// Integer `x` of form `from` (read as signed when `src_signed`) clamped, in its own container, +/// to the range it shares with an integer of form `to` (signed when `dst_signed`) ([`sat_bounds`]): +/// `SClamp`, `SMax` or `SMin` of its sign extension, masked back to its width, or `UMin`. The +/// conversion to `to` that follows it is then exact. +pub fn saturate( + fb: &mut FunctionBuilder<'_>, + (src_signed, dst_signed): (bool, bool), + from: Form, + to: Form, + x: Value, +) -> BResult { + let (lo, hi) = sat_bounds((from.bits(), src_signed), (to.bits(), dst_signed)); + if lo.is_none() && hi.is_none() { + return Ok(x); + } + // Two's complement in the container (`splat` masks to it). + let k = |fb: &mut FunctionBuilder<'_>, v: i128| splat(fb, from, v as i64 as u64); + if !src_signed { + return match hi { + Some(h) => { + let h = k(fb, h)?; + fb.glsl(GlslOp::UMin, &[x, h]) + } + None => Ok(x), + }; + } + let s = sext(fb, from, x)?; + let r = match (lo, hi) { + (Some(l), Some(h)) => { + let (l, h) = (k(fb, l)?, k(fb, h)?); + fb.glsl(GlslOp::SClamp, &[s, l, h])? + } + (Some(l), None) => { + let l = k(fb, l)?; + fb.glsl(GlslOp::SMax, &[s, l])? + } + (None, Some(h)) => { + let h = k(fb, h)?; + fb.glsl(GlslOp::SMin, &[s, h])? + } + (None, None) => s, + }; + mask(fb, from, r) +} + +/// An upper estimate of the SPIR-V words of a directed conversion from `from` to `to` beyond the +/// conversion it replaces: [`to_half`]'s sixty instructions and twenty constants (a vector's +/// constants carry their lanes), a magnitude's rounding, or a `double`'s step. +pub fn directed_words(from: Form, to: Form) -> u32 { + let lanes = u32::from(to.lanes); + let half = 360 + 20 * lanes; + match to.float_fmt() { + Some(FloatFmt::Half) if from.float_fmt() == Some(FloatFmt::F64) => half + 96, + Some(FloatFmt::Half) => half + 32, + _ if from.is_float() => 160 + 8 * lanes, + _ => 200 + 10 * lanes, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn saturating_bounds_clamp_only_where_the_ranges_differ() { + // `int` to `uchar`: both bounds. + assert_eq!(sat_bounds((32, true), (8, false)), (Some(0), Some(255))); + // `uint` to `char`: the unsigned operand has no lower bound to clamp. + assert_eq!(sat_bounds((32, false), (8, true)), (None, Some(127))); + // `uchar` to `int`: nothing. + assert_eq!(sat_bounds((8, false), (32, true)), (None, None)); + // `char` to `uint`: the negatives to zero. + assert_eq!(sat_bounds((8, true), (32, false)), (Some(0), None)); + // `int` to `uint` and back. + assert_eq!(sat_bounds((32, true), (32, false)), (Some(0), None)); + assert_eq!( + sat_bounds((32, false), (32, true)), + (None, Some(i128::from(i32::MAX))) + ); + // `ulong` to `long`. + assert_eq!( + sat_bounds((64, false), (64, true)), + (None, Some(i128::from(i64::MAX))) + ); + // `long` to `int`. + assert_eq!( + sat_bounds((64, true), (32, true)), + (Some(i128::from(i32::MIN)), Some(i128::from(i32::MAX))) + ); + } + + #[test] + fn exact_conversions_are_the_ones_whose_magnitude_fits_the_significand() { + let int = |bits: u8| Form { + held: Held::Int { + bits, + container: if bits > 32 { 64 } else { 32 }, + }, + lanes: 1, + }; + assert!(converts_exactly(int(8), false, FloatFmt::Half)); + assert!(converts_exactly(int(8), true, FloatFmt::Half)); + assert!(!converts_exactly(int(16), true, FloatFmt::Half)); + assert!(converts_exactly(int(16), false, FloatFmt::F32)); + assert!(!converts_exactly(int(32), true, FloatFmt::F32)); + assert!(!converts_exactly(int(32), false, FloatFmt::F32)); + assert!(converts_exactly(int(32), false, FloatFmt::F64)); + assert!(!converts_exactly(int(64), true, FloatFmt::F64)); + } + + #[test] + fn directions_follow_the_conversion_rounding() { + assert_eq!( + Directed::of(Some(ConvertRounding::Rtz)), + Some(Directed::Zero) + ); + assert_eq!(Directed::of(Some(ConvertRounding::Rtp)), Some(Directed::Up)); + assert_eq!( + Directed::of(Some(ConvertRounding::Rtn)), + Some(Directed::Down) + ); + assert_eq!(Directed::of(Some(ConvertRounding::Rte)), None); + assert_eq!(Directed::of(None), None); + assert_eq!(Integral::of(None), Integral::Truncate); + assert_eq!(Integral::of(Some(ConvertRounding::Rtz)), Integral::Truncate); + assert_eq!(Integral::of(Some(ConvertRounding::Rte)), Integral::Even); + assert_eq!(Integral::of(Some(ConvertRounding::Rtp)), Integral::Up); + assert_eq!(Integral::of(Some(ConvertRounding::Rtn)), Integral::Down); + } +} diff --git a/crates/metal2vulkan/src/lower/arith/tests.rs b/crates/metal2vulkan/src/lower/arith/tests.rs index 4b406decb..77178dc06 100644 --- a/crates/metal2vulkan/src/lower/arith/tests.rs +++ b/crates/metal2vulkan/src/lower/arith/tests.rs @@ -3642,7 +3642,10 @@ fn saturating_float_to_integer_convert_matches_metal() { &|fb, x| { math::convert( fb, - ConvertKind::ToInt { signed }, + ConvertKind::ToInt { + signed, + integral: crate::lower::arith::rounding::Integral::Truncate, + }, ff, tf, x[0], @@ -3746,7 +3749,10 @@ fn saturating_converts_saturate_at_every_bound() { &|fb, x| { math::convert( fb, - ConvertKind::ToInt { signed }, + ConvertKind::ToInt { + signed, + integral: crate::lower::arith::rounding::Integral::Truncate, + }, ff, int(ibits), x[0], @@ -3771,7 +3777,10 @@ fn saturating_converts_saturate_at_every_bound() { &|fb, x| { math::convert( fb, - ConvertKind::ToInt { signed }, + ConvertKind::ToInt { + signed, + integral: crate::lower::arith::rounding::Integral::Truncate, + }, half, int(ibits), x[0], diff --git a/crates/metal2vulkan/src/lower/arith/tests/narrow.rs b/crates/metal2vulkan/src/lower/arith/tests/narrow.rs index ae47a63d1..b5585514d 100644 --- a/crates/metal2vulkan/src/lower/arith/tests/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/tests/narrow.rs @@ -1588,7 +1588,10 @@ fn narrow_conversions_round_once() { &|fb, x| { math::convert( fb, - ConvertKind::ToInt { signed }, + ConvertKind::ToInt { + signed, + integral: crate::lower::arith::rounding::Integral::Truncate, + }, half(), fi, x[0], diff --git a/crates/metal2vulkan/src/lower/arith/tests/wide.rs b/crates/metal2vulkan/src/lower/arith/tests/wide.rs index d57780d26..6b963b4a0 100644 --- a/crates/metal2vulkan/src/lower/arith/tests/wide.rs +++ b/crates/metal2vulkan/src/lower/arith/tests/wide.rs @@ -615,7 +615,18 @@ fn float_to_wide_truncates_and_saturates() { &format!("convert saturating f32 -> wide i{bits} signed={signed}"), &[float(32)], to, - &|fb, x| w::convert(fb, ConvertKind::ToInt { signed }, float(32), to, x[0]), + &|fb, x| { + w::convert( + fb, + ConvertKind::ToInt { + signed, + integral: crate::lower::arith::rounding::Integral::Truncate, + }, + float(32), + to, + x[0], + ) + }, &cases, &|c| { let x = f64::from(f32v(arg(c, 0))); diff --git a/crates/metal2vulkan/src/lower/arith/wide.rs b/crates/metal2vulkan/src/lower/arith/wide.rs index 20f0c437e..59b070b46 100644 --- a/crates/metal2vulkan/src/lower/arith/wide.rs +++ b/crates/metal2vulkan/src/lower/arith/wide.rs @@ -35,6 +35,7 @@ use crate::lower::arith::legal::{self, FloatFmt, Form, Held}; use crate::lower::arith::math::{self, ConvertKind, IntFn, Sign}; use crate::lower::arith::narrow; use crate::lower::arith::ops::{self, CastKind}; +use crate::lower::arith::rounding; use crate::lower::select::SelectCx; use crate::spirv::builder::{FunctionBuilder, ModuleBuilder}; use crate::spirv::error::{BResult, BuildError, OperandRule, TypeRule}; @@ -799,15 +800,17 @@ fn float_to_pair( /// Wide `x` (form `from`, read as signed when `signed`) converted to float form `to`, rounding /// once: to `double` as `hi * 2^32 + lo` (both exact, one rounding add); otherwise the magnitude /// shifted right by `s` (so it has at most 32 significant bits) with the bits shifted out folded -/// into a sticky lowest bit, converted (to odd for `bfloat`), and scaled back by `2^s` exactly. -/// A magnitude below `2^32` takes `s = 0`, so below `2^24` the `f32` is exact and a `half` -/// rounds it by integer arithmetic ([`narrow::round_integer`]). +/// into a sticky lowest bit, converted (to odd for `bfloat`, by `dir` for an `f32` rounded toward +/// zero or an infinity, [`rounding::magnitude_f32`], which the sticky bit keeps exact), and scaled +/// back by `2^s` exactly. A magnitude below `2^32` takes `s = 0`, so below `2^24` the `f32` is +/// exact and a `half` rounds it by integer arithmetic ([`narrow::round_integer`], or +/// [`rounding::to_half`] by `dir`). fn pair_to_float( fb: &mut FunctionBuilder<'_>, from: Form, to: Form, x: SValue, - signed: bool, + (signed, dir): (bool, Option), ) -> BResult { let n = from.lanes; let p = to_pair(fb, from, x, signed)?; @@ -819,6 +822,9 @@ fn pair_to_float( }; let t = ops::ty(fb, to)?; let r = if to.float_fmt() == Some(FloatFmt::F64) { + if dir.is_some() { + return Err(arity_error()); + } let fh = fb.convert(ConvOp::ConvertUToF, m.hi, t)?; let fl = fb.convert(ConvOp::ConvertUToF, m.lo, t)?; let up = ops::fsplat(fb, to, 2f64.powi(32))?; @@ -836,11 +842,15 @@ fn pair_to_float( let sticky = fb.select(lost, one, z)?; let m32 = bin(fb, BinOp::BitwiseOr, t32.lo, sticky)?; let f32f = to.with(Held::Float(FloatFmt::F32)); - let f = if to.float_fmt() == Some(FloatFmt::BFloat) { - narrow::int_to_f32_odd(fb, words_form(n), to, m32)? - } else { - let ft = ops::ty(fb, f32f)?; - fb.convert(ConvOp::ConvertUToF, m32, ft)? + let f = match (to.float_fmt(), dir) { + (Some(FloatFmt::BFloat), _) => narrow::int_to_f32_odd(fb, words_form(n), to, m32)?, + (Some(FloatFmt::F32), Some(d)) => { + rounding::magnitude_f32(fb, words_form(n), to, (m32, is_neg), d)? + } + _ => { + let ft = ops::ty(fb, f32f)?; + fb.convert(ConvOp::ConvertUToF, m32, ft)? + } }; fb.glsl(GlslOp::Ldexp, &[f, s])? }; @@ -851,7 +861,12 @@ fn pair_to_float( } None => r, }; - narrow::round_integer(fb, to, r) + match (to.float_fmt(), dir) { + (Some(FloatFmt::Half), Some(d)) => { + rounding::to_half(fb, to.with(Held::Float(FloatFmt::F32)), r, d) + } + _ => narrow::round_integer(fb, to, r), + } } /// A numeric cast with a wide side. @@ -872,7 +887,7 @@ pub fn cast( from_pair(fb, to, p) } CastKind::UiToFp | CastKind::SiToFp => { - pair_to_float(fb, from, to, x, kind == CastKind::SiToFp) + pair_to_float(fb, from, to, x, (kind == CastKind::SiToFp, None)) } CastKind::FpResize => Err(arity_error()), } @@ -892,8 +907,39 @@ pub fn convert( let p = to_pair(fb, from, x, signed)?; from_pair(fb, to, p) } - ConvertKind::ToFloat { signed } => pair_to_float(fb, from, to, x, signed), - ConvertKind::ToInt { signed } => { + ConvertKind::ToFloat { signed } => pair_to_float(fb, from, to, x, (signed, None)), + ConvertKind::ToFloatDirected { signed, dir } => { + pair_to_float(fb, from, to, x, (signed, Some(dir))) + } + // Clamped as pairs to the range both share, then truncated or extended. + ConvertKind::SatInt { + src_signed, + dst_signed, + } => { + use IntPredicate as P; + let n = from.lanes; + let mut p = to_pair(fb, from, x, src_signed)?; + let (lo, hi) = rounding::sat_bounds((from.bits(), src_signed), (to.bits(), dst_signed)); + let (lt, gt) = if src_signed { + (P::Slt, P::Sgt) + } else { + (P::Ult, P::Ugt) + }; + if let Some(l) = lo { + let lp = pk(fb, n, l as i64 as u64)?; + let below = compare(fb, lt, p, lp)?; + p = sel(fb, below, lp, p)?; + } + if let Some(h) = hi { + let hp = pk(fb, n, h as i64 as u64)?; + let above = compare(fb, gt, p, hp)?; + p = sel(fb, above, hp, p)?; + } + from_pair(fb, to, p) + } + ConvertKind::ToInt { signed, integral } => { + // Rounded to an integral value first; the NaN test reads the operand itself. + let v = rounding::integral(fb, integral, x)?; let nb = u32::from(to.bits()); let fw = from.container(); let kk = if signed { nb - 1 } else { nb }; @@ -911,15 +957,15 @@ pub fn convert( ops::fsplat(fb, from, 0.0)? }; let hi = ops::splat(fb, from, math::below_pow2_bits(fw, kk))?; - let c = fb.glsl(GlslOp::NClamp, &[x, lo, hi])?; + let c = fb.glsl(GlslOp::NClamp, &[v, lo, hi])?; let n = to.lanes; let mut p = float_to_pair(fb, from, to, c, signed)?; let limit = ops::splat(fb, from, math::pow2_bits(fw, kk))?; - let over = bin(fb, BinOp::FOrdGreaterThanEqual, x, limit)?; + let over = bin(fb, BinOp::FOrdGreaterThanEqual, v, limit)?; let max = pk(fb, n, ops::ones(kk as u8))?; p = sel(fb, over, max, p)?; if signed { - let under = bin(fb, BinOp::FOrdLessThan, x, lo)?; + let under = bin(fb, BinOp::FOrdLessThan, v, lo)?; let min = pk(fb, n, (!0u64) << (nb - 1))?; p = sel(fb, under, min, p)?; let nan = fb.unary(UnOp::IsNan, x)?; @@ -938,9 +984,16 @@ pub fn convert( ConvertKind::Fp8ToInt { fp8, signed } => { let f = from.with(Held::Float(FloatFmt::F32)); let v = narrow::fp8_to_f32(fb, fp8, narrow::u32_form(from), f, x)?; - convert(fb, ConvertKind::ToInt { signed }, f, to, v) + let k = ConvertKind::ToInt { + signed, + integral: rounding::Integral::Truncate, + }; + convert(fb, k, f, to, v) } - ConvertKind::Float | ConvertKind::FromFp8(_) | ConvertKind::ToBool => Err(arity_error()), + ConvertKind::Float + | ConvertKind::FloatDirected(_) + | ConvertKind::FromFp8(_) + | ConvertKind::ToBool => Err(arity_error()), } } @@ -1563,6 +1616,12 @@ impl WideOp { } WideOp::Bin { .. } => 192, WideOp::Compare { .. } => 96, + // A directed rounding's magnitude and `half` steps beyond the plain conversion. + WideOp::Convert { + kind: ConvertKind::ToFloatDirected { .. }, + from, + to, + } => 1024 + rounding::directed_words(*from, *to), WideOp::Cast { .. } | WideOp::Convert { .. } => { // The float paths: the collapse to 32 bits (two shifts, a compare) and rounding. 1024 @@ -2099,11 +2158,49 @@ fn select_convert( } let kind = match (c.dst_kind, c.src_kind) { (NumKind::Float, NumKind::Float) => return Err(not_lowered()), - (NumKind::Float, s) => ConvertKind::ToFloat { - signed: s == NumKind::Signed, - }, - (d, NumKind::Float) => ConvertKind::ToInt { - signed: d == NumKind::Signed, + (NumKind::Float, s) => { + let signed = s == NumKind::Signed; + let dst = to.float_fmt(); + match (rounding::Directed::of(c.rounding), dst) { + (_, Some(fmt)) if rounding::converts_exactly(from, signed, fmt) => { + ConvertKind::ToFloat { signed } + } + // Nearest even: the conversion's rounding where the entry declares + // `RoundingModeRTE` for its width (a `half` or `bfloat` rounds by integer + // arithmetic). + (None, Some(fmt @ (FloatFmt::F32 | FloatFmt::F64))) => { + if c.rounding.is_some() { + cx.families.arith.rte.insert(fmt.container()); + } + ConvertKind::ToFloat { signed } + } + (None, _) => ConvertKind::ToFloat { signed }, + (Some(dir), Some(FloatFmt::F32 | FloatFmt::Half)) => { + ConvertKind::ToFloatDirected { signed, dir } + } + // A `double` or `bfloat` by a directed rounding from a wide integer. + (Some(_), _) => return Err(not_lowered()), + } + } + (d, NumKind::Float) => { + let integral = rounding::Integral::of(c.rounding); + match integral { + rounding::Integral::Truncate => {} + rounding::Integral::Even => { + super::leave_denormals(cx, from, super::denormal::Covered::Deviation) + } + rounding::Integral::Up | rounding::Integral::Down => { + super::leave_denormals(cx, from, super::denormal::Covered::Unary) + } + } + ConvertKind::ToInt { + signed: d == NumKind::Signed, + integral, + } + } + (d, s) if c.saturate => ConvertKind::SatInt { + src_signed: s == NumKind::Signed, + dst_signed: d == NumKind::Signed, }, (_, s) => ConvertKind::Int { signed: s == NumKind::Signed, @@ -2111,10 +2208,15 @@ fn select_convert( }; let int_like = |f: Form| f.is_bool() || f.is_wide() || (f.is_int() && f.container() == 32); let ok = match kind { - ConvertKind::ToFloat { .. } => from.is_wide() && to.is_float(), + ConvertKind::ToFloat { .. } | ConvertKind::ToFloatDirected { .. } => { + from.is_wide() && to.is_float() + } ConvertKind::ToInt { .. } => from.is_float() && to.is_wide(), - ConvertKind::Int { .. } => int_like(from) && int_like(to) && !to.is_bool(), + ConvertKind::Int { .. } | ConvertKind::SatInt { .. } => { + int_like(from) && int_like(to) && !to.is_bool() + } ConvertKind::Float + | ConvertKind::FloatDirected(_) | ConvertKind::FromFp8(_) | ConvertKind::Fp8ToInt { .. } | ConvertKind::ToBool diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index f93a22212..208f244ec 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1960,6 +1960,18 @@ pub enum PrecisionNote { /// `fquantize2f16` flushes every one, SwiftShader's too); a NaN's encoding is the device's. /// Validation derives no tolerance from it. NativeHalfQuantize, + /// Native conversion rounding: an `air.convert` whose name rounds to nearest even (`.rte`, + /// Apple's OpenCL compiler's `convert_` and `convert__rte`) and that can round (a + /// 32- or 64-bit integer to `float`, a 64-bit integer to `double`, a `double` to `float`) is + /// the device's correctly rounded conversion, in an entry that cannot declare + /// `RoundingModeRTE` for the result's width: the device's float controls lack it, or its + /// rounding-mode independence ties it to a width that lacks it (MoltenVK, whose 64-bit + /// rounding mode it does not state). The Vulkan spec rounds such a conversion to either of + /// the two values around the exact one, the device's choice, where Metal rounds to nearest + /// even. Where the entry declares the mode, the conversion is Metal's and records nothing. The + /// recorded bound: one unit in the last place of the result, a value exactly between two the + /// other of them. Validation derives no tolerance from it. + NativeConversionRounding, /// Native device maths: a `float` or `half` operation's NaN result is the device's, its sign /// and payload unselected, where Metal answers a NaN the device is not measured to answer: /// every NaN Metal answers on a device whose arithmetic is not measured to answer Metal's diff --git a/crates/metal2vulkan/src/tests/fast_paths.rs b/crates/metal2vulkan/src/tests/fast_paths.rs index aa37a1f92..f5667cf5d 100644 --- a/crates/metal2vulkan/src/tests/fast_paths.rs +++ b/crates/metal2vulkan/src/tests/fast_paths.rs @@ -157,6 +157,7 @@ fn notes() -> Vec<(&'static str, Ledger)> { HalfOutputPrecision, NativeHalfConversion, NativeHalfQuantize, + NativeConversionRounding, F16FmaDoubleRounding, F16IntConvDoubleRounding, GradFromBias, diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index b541cbd26..e8b3e6868 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1165,6 +1165,7 @@ enum metal2vulkan-air::intrinsics::BarrierName #[derive(Clone, Copy, Debug, Part enum metal2vulkan-air::intrinsics::Bound #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Bound enum metal2vulkan-air::intrinsics::CommandStage #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum CommandStage enum metal2vulkan-air::intrinsics::Component #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Component +enum metal2vulkan-air::intrinsics::ConvertRounding #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum ConvertRounding enum metal2vulkan-air::intrinsics::DeclClass #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum DeclClass enum metal2vulkan-air::intrinsics::DerivativeOp #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum DerivativeOp enum metal2vulkan-air::intrinsics::Dropped #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Dropped @@ -1852,6 +1853,8 @@ field metal2vulkan-air::intrinsics::Coherent.ty pub ty: NumTy field metal2vulkan-air::intrinsics::Coherent.volatile pub volatile: bool field metal2vulkan-air::intrinsics::Convert.dst pub dst: NumTy field metal2vulkan-air::intrinsics::Convert.dst_kind pub dst_kind: NumKind +field metal2vulkan-air::intrinsics::Convert.rounding pub rounding: Option +field metal2vulkan-air::intrinsics::Convert.saturate pub saturate: bool field metal2vulkan-air::intrinsics::Convert.src pub src: NumTy field metal2vulkan-air::intrinsics::Convert.src_fp8 pub src_fp8: Option field metal2vulkan-air::intrinsics::Convert.src_kind pub src_kind: NumKind @@ -5499,6 +5502,7 @@ macro-member metal2vulkan-runtime::Degradation::MoltenVkArgumentBuffersUnread Mo macro-member metal2vulkan-runtime::Degradation::MultisamplePosition MultisamplePosition = "multisample-position"; macro-member metal2vulkan-runtime::Degradation::NanDepth NanDepth = "nan-depth", Precision, macro-member metal2vulkan-runtime::Degradation::NarrowSurfaceRows NarrowSurfaceRows = "narrow-surface-rows", Precision, +macro-member metal2vulkan-runtime::Degradation::NativeConversionRounding NativeConversionRounding = "native-conversion-rounding"; macro-member metal2vulkan-runtime::Degradation::NativeDivision NativeDivision = "native-division"; macro-member metal2vulkan-runtime::Degradation::NativeDot NativeDot = "native-dot"; macro-member metal2vulkan-runtime::Degradation::NativeFma NativeFma = "native-fma"; @@ -11006,6 +11010,10 @@ variant metal2vulkan-air::intrinsics::Component::W W variant metal2vulkan-air::intrinsics::Component::X X variant metal2vulkan-air::intrinsics::Component::Y Y variant metal2vulkan-air::intrinsics::Component::Z Z +variant metal2vulkan-air::intrinsics::ConvertRounding::Rte Rte +variant metal2vulkan-air::intrinsics::ConvertRounding::Rtn Rtn +variant metal2vulkan-air::intrinsics::ConvertRounding::Rtp Rtp +variant metal2vulkan-air::intrinsics::ConvertRounding::Rtz Rtz variant metal2vulkan-air::intrinsics::DeclClass::Air Air(AirIntrinsic) variant metal2vulkan-air::intrinsics::DeclClass::Drop Drop(Dropped) variant metal2vulkan-air::intrinsics::DeclClass::Llvm Llvm(LlvmIntrinsic) @@ -14279,6 +14287,7 @@ variant metal2vulkan::pipeline::PrecisionNote::HelperBranchDropsSimdgroupStores variant metal2vulkan::pipeline::PrecisionNote::LinkedSamplerNumbering LinkedSamplerNumbering variant metal2vulkan::pipeline::PrecisionNote::LoopedSampleOutputs LoopedSampleOutputs variant metal2vulkan::pipeline::PrecisionNote::MultisamplePosition MultisamplePosition +variant metal2vulkan::pipeline::PrecisionNote::NativeConversionRounding NativeConversionRounding variant metal2vulkan::pipeline::PrecisionNote::NativeDivision NativeDivision variant metal2vulkan::pipeline::PrecisionNote::NativeDot NativeDot variant metal2vulkan::pipeline::PrecisionNote::NativeFma NativeFma diff --git a/docs/API.md b/docs/API.md index b12545094..1b4423ecc 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4196,6 +4196,17 @@ bound. `OpQuantizeToF16` alone, on every device without `shaderFloat16`, its quantize measured or not: a value below the smallest normal `half` may round to a zero where Metal answers the half denormal (or `2^-14`, from just below it). An integer's `half` rounds exactly instead. +- `metal2vulkan-degrade-native-conversion-rounding` (precision). + `PrecisionNote::NativeConversionRounding`: Native device maths (a conversion): an `air.convert` + whose name rounds to nearest even (`.rte`: OpenCL's `convert_float` of an `int`, as Apple's + OpenCL compiler spells it) and that can round (a 32- or 64-bit integer to `float`, a 64-bit + integer to `double`, a `double` to `float`) is the device's correctly rounded conversion where + the entry cannot declare `RoundingModeRTE` for the result's width with the widths the device's + rounding-mode independence ties to it (MoltenVK, whose 64-bit rounding mode it does not state): + either value around the exact one, the device's choice, where Metal answers the nearer one, ties + to even. Where the entry declares the mode the conversion is Metal's and records nothing. The + conversions that round toward zero or an infinity (`.rtz`, `.rtp`, `.rtn`) are exact on every + device and record nothing. - `metal2vulkan-degrade-native-nan-encoding` (precision). `PrecisionNote::NativeNanEncoding`: Native device maths: a `float` or `half` operation's NaN result is the device's, its sign and payload unselected, where Metal answers a NaN the device is not measured to answer (every NaN but