From bc462404b39bf6847afa056f6e27457449eaca72 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 30 Mar 2020 22:01:14 +0200 Subject: [PATCH] [cranelift-frontend] Fix emit_zero for vectors (#1428) * Fix emit_zero for vectors * Add undef_vector_vars frontend test * Remove unused import --- cranelift/frontend/src/frontend.rs | 47 ++++++++++++++++++++++++++++++ cranelift/frontend/src/ssa.rs | 12 +++++--- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/cranelift/frontend/src/frontend.rs b/cranelift/frontend/src/frontend.rs index 6234649e9f..8213850948 100644 --- a/cranelift/frontend/src/frontend.rs +++ b/cranelift/frontend/src/frontend.rs @@ -1229,6 +1229,53 @@ block0: ); } + #[test] + fn undef_vector_vars() { + let mut sig = Signature::new(CallConv::SystemV); + sig.returns.push(AbiParam::new(I8X16)); + sig.returns.push(AbiParam::new(B8X16)); + sig.returns.push(AbiParam::new(F32X4)); + + let mut fn_ctx = FunctionBuilderContext::new(); + let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig); + { + let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx); + + let block0 = builder.create_block(); + let a = Variable::new(0); + let b = Variable::new(1); + let c = Variable::new(2); + builder.declare_var(a, I8X16); + builder.declare_var(b, B8X16); + builder.declare_var(c, F32X4); + builder.switch_to_block(block0); + + let a = builder.use_var(a); + let b = builder.use_var(b); + let c = builder.use_var(c); + builder.ins().return_(&[a, b, c]); + + builder.seal_all_blocks(); + builder.finalize(); + } + + assert_eq!( + func.display(None).to_string(), + "function %sample() -> i8x16, b8x16, f32x4 system_v { +block0: + v5 = f32const 0.0 + v6 = splat.f32x4 v5 + v2 -> v6 + v4 = vconst.b8x16 0x00 + v1 -> v4 + v3 = vconst.i8x16 0x00 + v0 -> v3 + return v0, v1, v2 +} +" + ); + } + #[test] fn test_greatest_divisible_power_of_two() { assert_eq!(64, greatest_divisible_power_of_two(64)); diff --git a/cranelift/frontend/src/ssa.rs b/cranelift/frontend/src/ssa.rs index 2ae03d3a57..cfb85a0287 100644 --- a/cranelift/frontend/src/ssa.rs +++ b/cranelift/frontend/src/ssa.rs @@ -9,6 +9,7 @@ use crate::Variable; use alloc::vec::Vec; +use core::convert::TryInto; use core::mem; use cranelift_codegen::cursor::{Cursor, FuncCursor}; use cranelift_codegen::entity::SecondaryMap; @@ -185,10 +186,13 @@ fn emit_zero(ty: Type, mut cur: FuncCursor) -> Value { cur.ins().null(ty) } else if ty.is_vector() { let scalar_ty = ty.lane_type(); - if scalar_ty.is_int() { - cur.ins().iconst(ty, 0) - } else if scalar_ty.is_bool() { - cur.ins().bconst(ty, false) + if scalar_ty.is_int() || scalar_ty.is_bool() { + let zero = cur.func.dfg.constants.insert( + core::iter::repeat(0) + .take(ty.bytes().try_into().unwrap()) + .collect(), + ); + cur.ins().vconst(ty, zero) } else if scalar_ty == F32 { let scalar = cur.ins().f32const(Ieee32::with_bits(0)); cur.ins().splat(ty, scalar)