authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-03-07 21:12:45+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-09 13:53:10-07:00
log684b81f366566353aeca18a7431f8df3304f439b
tree97199116a696b6a081482c8b146d16e7c85e549c
parent557f396f613a416e81116f85c080af8b976fe8cf

wasm: Implement fpext

This implements initial support for floating-point promotion for bitsizes <= 64

4 files changed, 39 insertions(+), 10 deletions(-)

src/arch/wasm/CodeGen.zig+36-9
......@@ -212,7 +212,8 @@ fn buildOpcode(args: OpcodeBuildArguments) wasm.Opcode {
212212 16 => switch (args.valtype1.?) {
213213 .i32 => if (args.signedness.? == .signed) return .i32_load16_s else return .i32_load16_u,
214214 .i64 => if (args.signedness.? == .signed) return .i64_load16_s else return .i64_load16_u,
215 .f32, .f64 => unreachable,
215 .f32 => return .f32_load,
216 .f64 => unreachable,
216217 },
217218 32 => switch (args.valtype1.?) {
218219 .i64 => if (args.signedness.? == .signed) return .i64_load32_s else return .i64_load32_u,
......@@ -242,7 +243,8 @@ fn buildOpcode(args: OpcodeBuildArguments) wasm.Opcode {
242243 16 => switch (args.valtype1.?) {
243244 .i32 => return .i32_store16,
244245 .i64 => return .i64_store16,
245 .f32, .f64 => unreachable,
246 .f32 => return .f32_store,
247 .f64 => unreachable,
246248 },
247249 32 => switch (args.valtype1.?) {
248250 .i64 => return .i64_store32,
......@@ -1064,7 +1066,7 @@ fn allocStackPtr(self: *Self, inst: Air.Inst.Index) !WValue {
10641066}
10651067
10661068/// From given zig bitsize, returns the wasm bitsize
1067fn toWasmIntBits(bits: u16) ?u16 {
1069fn toWasmBits(bits: u16) ?u16 {
10681070 return for ([_]u16{ 32, 64 }) |wasm_bits| {
10691071 if (bits <= wasm_bits) return wasm_bits;
10701072 } else null;
......@@ -1120,11 +1122,11 @@ fn isByRef(ty: Type, target: std.Target) bool {
11201122 .ErrorSet,
11211123 .Fn,
11221124 .Enum,
1123 .Vector,
11241125 .AnyFrame,
11251126 => return false,
11261127
11271128 .Array,
1129 .Vector,
11281130 .Struct,
11291131 .Frame,
11301132 .Union,
......@@ -1218,6 +1220,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
12181220 .cond_br => self.airCondBr(inst),
12191221 .dbg_stmt => WValue.none,
12201222 .intcast => self.airIntcast(inst),
1223 .fpext => self.airFpext(inst),
12211224 .float_to_int => self.airFloatToInt(inst),
12221225 .get_union_tag => self.airGetUnionTag(inst),
12231226
......@@ -1298,7 +1301,6 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
12981301 .is_err_ptr,
12991302 .is_non_err_ptr,
13001303 .fptrunc,
1301 .fpext,
13021304 .unwrap_errunion_payload_ptr,
13031305 .unwrap_errunion_err_ptr,
13041306
......@@ -1513,7 +1515,7 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
15131515
15141516 return self.memCopy(ty, lhs, rhs);
15151517 },
1516 .Struct, .Array, .Union => {
1518 .Struct, .Array, .Union, .Vector => {
15171519 return self.memCopy(ty, lhs, rhs);
15181520 },
15191521 .Pointer => {
......@@ -2408,9 +2410,9 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
24082410 const ref_info = ref_ty.intInfo(self.target);
24092411 const wanted_info = ty.intInfo(self.target);
24102412
2411 const op_bits = toWasmIntBits(ref_info.bits) orelse
2413 const op_bits = toWasmBits(ref_info.bits) orelse
24122414 return self.fail("TODO: Wasm intcast integer types of bitsize: {d}", .{ref_info.bits});
2413 const wanted_bits = toWasmIntBits(wanted_info.bits) orelse
2415 const wanted_bits = toWasmBits(wanted_info.bits) orelse
24142416 return self.fail("TODO: Wasm intcast integer types of bitsize: {d}", .{wanted_info.bits});
24152417
24162418 // hot path
......@@ -2651,7 +2653,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
26512653 const result = try self.allocLocal(self.air.getRefType(ty_op.ty));
26522654 const op_bits = op_ty.intInfo(self.target).bits;
26532655
2654 const wasm_bits = toWasmIntBits(wanted_bits) orelse
2656 const wasm_bits = toWasmBits(wanted_bits) orelse
26552657 return self.fail("TODO: Implement wasm integer truncation for integer bitsize: {d}", .{wanted_bits});
26562658
26572659 // Use wasm's instruction to wrap from 64bit to 32bit integer when possible
......@@ -3182,3 +3184,28 @@ fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
31823184 } else @as(u32, 0);
31833185 return self.load(operand, tag_ty, offset);
31843186}
3187
3188fn airFpext(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
3189 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
3190
3191 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
3192 const ty = self.air.typeOfIndex(inst);
3193 const wanted_bits = ty.floatBits(self.target);
3194 const have_bits = self.air.typeOf(ty_op.operand).floatBits(self.target);
3195 const operand = try self.resolveInst(ty_op.operand);
3196
3197 const have = toWasmBits(have_bits) orelse {
3198 return self.fail("TODO: Implement 'fpext' for floats with bitsize: {d}", .{have_bits});
3199 };
3200 const wanted = toWasmBits(wanted_bits) orelse {
3201 return self.fail("TODO: Implement 'fpext' for floats with bitsize: {d}", .{wanted_bits});
3202 };
3203 if (have == wanted) return operand;
3204
3205 assert(have < wanted);
3206 const result = try self.allocLocal(ty);
3207 try self.emitWValue(operand);
3208 try self.addTag(.f64_promote_f32);
3209 try self.addLabel(.local_set, result.local);
3210 return result;
3211}
src/arch/wasm/Emit.zig+1
......@@ -161,6 +161,7 @@ pub fn emitMir(emit: *Emit) InnerError!void {
161161 .i64_extend8_s => try emit.emitTag(tag),
162162 .i64_extend16_s => try emit.emitTag(tag),
163163 .i64_extend32_s => try emit.emitTag(tag),
164 .f64_promote_f32 => try emit.emitTag(tag),
164165 .i32_reinterpret_f32 => try emit.emitTag(tag),
165166 .i64_reinterpret_f64 => try emit.emitTag(tag),
166167 .f32_reinterpret_i32 => try emit.emitTag(tag),
src/arch/wasm/Mir.zig+2
......@@ -391,6 +391,8 @@ pub const Inst = struct {
391391 /// Uses `tag`
392392 i64_trunc_f64_u = 0xB1,
393393 /// Uses `tag`
394 f64_promote_f32 = 0xBB,
395 /// Uses `tag`
394396 i32_reinterpret_f32 = 0xBC,
395397 /// Uses `tag`
396398 i64_reinterpret_f64 = 0xBD,
test/behavior/switch.zig-1
......@@ -122,7 +122,6 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {
122122}
123123
124124test "switching on booleans" {
125 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
126125 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
127126 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
128127