authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-10 22:31:30+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-11 20:31:50+02:00
log78c44d63da14437fe0d64da92606a22d543b8169
tree0339a270a24cfcb600ed2a22efd1b1fff2b12881
parent0bae2caaf382dfb168ee404e3ffb717975f8289b
signaturelock-open Commit is signed but in an unrecognized format.

spirv: lower air unwrap_error_union_err


1 files changed, 28 insertions(+), 2 deletions(-)

src/codegen/spirv.zig+28-2
...@@ -402,7 +402,7 @@ pub const DeclGen = struct {...@@ -402,7 +402,7 @@ pub const DeclGen = struct {
402 return result_id;402 return result_id;
403 }403 }
404404
405 fn genUndef(self: *DeclGen, ty_ref: SpvType.Ref) Error!IdRef {405 fn constUndef(self: *DeclGen, ty_ref: SpvType.Ref) Error!IdRef {
406 const result_id = self.spv.allocId();406 const result_id = self.spv.allocId();
407 try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = self.typeId(ty_ref), .id_result = result_id });407 try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = self.typeId(ty_ref), .id_result = result_id });
408 return result_id;408 return result_id;
...@@ -1597,7 +1597,7 @@ pub const DeclGen = struct {...@@ -1597,7 +1597,7 @@ pub const DeclGen = struct {
1597 .block => try self.airBlock(inst),1597 .block => try self.airBlock(inst),
15981598
1599 .load => try self.airLoad(inst),1599 .load => try self.airLoad(inst),
1600 .store => return self.airStore(inst),1600 .store => return self.airStore(inst),
16011601
1602 .br => return self.airBr(inst),1602 .br => return self.airBr(inst),
1603 .breakpoint => return,1603 .breakpoint => return,
...@@ -1612,6 +1612,8 @@ pub const DeclGen = struct {...@@ -1612,6 +1612,8 @@ pub const DeclGen = struct {
1612 .switch_br => return self.airSwitchBr(inst),1612 .switch_br => return self.airSwitchBr(inst),
1613 .unreach => return self.airUnreach(),1613 .unreach => return self.airUnreach(),
16141614
1615 .unwrap_errunion_err => try self.airErrUnionErr(inst),
1616
1615 .assembly => try self.airAssembly(inst),1617 .assembly => try self.airAssembly(inst),
16161618
1617 .call => try self.airCall(inst, .auto),1619 .call => try self.airCall(inst, .auto),
...@@ -2535,6 +2537,30 @@ pub const DeclGen = struct {...@@ -2535,6 +2537,30 @@ pub const DeclGen = struct {
2535 return try self.extractField(payload_ty_ref, err_union_id, eu_layout.payloadFieldIndex());2537 return try self.extractField(payload_ty_ref, err_union_id, eu_layout.payloadFieldIndex());
2536 }2538 }
25372539
2540 fn airErrUnionErr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2541 if (self.liveness.isUnused(inst)) return null;
2542
2543 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2544 const operand_id = try self.resolve(ty_op.operand);
2545 const err_union_ty = self.air.typeOf(ty_op.operand);
2546 const err_ty_ref = try self.resolveType(Type.anyerror, .direct);
2547
2548 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
2549 // No error possible, so just return undefined.
2550 return try self.constUndef(err_ty_ref);
2551 }
2552
2553 const payload_ty = err_union_ty.errorUnionPayload();
2554 const eu_layout = self.errorUnionLayout(payload_ty);
2555
2556 if (!eu_layout.payload_has_bits) {
2557 // If no payload, error union is represented by error set.
2558 return operand_id;
2559 }
2560
2561 return try self.extractField(err_ty_ref, operand_id, eu_layout.errorFieldIndex());
2562 }
2563
2538 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {2564 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {
2539 const target = self.getTarget();2565 const target = self.getTarget();
2540 const pl_op = self.air.instructions.items(.data)[inst].pl_op;2566 const pl_op = self.air.instructions.items(.data)[inst].pl_op;