authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-21 02:59:12+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-22 16:11:56+02:00
logcba97e47730ff42df1da23e7019350a2d9e1a312
treef49c6b9b6815cc34359d46b8d6d1768437966e7c
parent228f71fa0ce25a5c473496dd2b947ae05ab9bed8

SPIR-V: Make functions which always return a null result return void instead


2 files changed, 26 insertions(+), 38 deletions(-)

src/codegen/spirv.zig+26-37
......@@ -643,14 +643,12 @@ pub const DeclGen = struct {
643643
644644 fn genBody(self: *DeclGen, body: ir.Body) Error!void {
645645 for (body.instructions) |inst| {
646 const maybe_result_id = try self.genInst(inst);
647 if (maybe_result_id) |result_id|
648 try self.inst_results.putNoClobber(inst, result_id);
646 try self.genInst(inst);
649647 }
650648 }
651649
652 fn genInst(self: *DeclGen, inst: *Inst) !?ResultId {
653 return switch (inst.tag) {
650 fn genInst(self: *DeclGen, inst: *Inst) !void {
651 const result_id = switch (inst.tag) {
654652 .add, .addwrap => try self.genBinOp(inst.castTag(.add).?),
655653 .sub, .subwrap => try self.genBinOp(inst.castTag(.sub).?),
656654 .mul, .mulwrap => try self.genBinOp(inst.castTag(.mul).?),
......@@ -669,23 +667,25 @@ pub const DeclGen = struct {
669667 .not => try self.genUnOp(inst.castTag(.not).?),
670668 .alloc => try self.genAlloc(inst.castTag(.alloc).?),
671669 .arg => self.genArg(),
672 .block => try self.genBlock(inst.castTag(.block).?),
673 .br => try self.genBr(inst.castTag(.br).?),
674 .br_void => try self.genBrVoid(inst.castTag(.br_void).?),
670 .block => (try self.genBlock(inst.castTag(.block).?)) orelse return,
671 .br => return try self.genBr(inst.castTag(.br).?),
672 .br_void => return try self.genBrVoid(inst.castTag(.br_void).?),
675673 // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them
676674 // throughout the IR.
677 .breakpoint => null,
678 .condbr => try self.genCondBr(inst.castTag(.condbr).?),
675 .breakpoint => return,
676 .condbr => return try self.genCondBr(inst.castTag(.condbr).?),
679677 .constant => unreachable,
680 .dbg_stmt => try self.genDbgStmt(inst.castTag(.dbg_stmt).?),
678 .dbg_stmt => return try self.genDbgStmt(inst.castTag(.dbg_stmt).?),
681679 .load => try self.genLoad(inst.castTag(.load).?),
682 .loop => try self.genLoop(inst.castTag(.loop).?),
683 .ret => try self.genRet(inst.castTag(.ret).?),
684 .retvoid => try self.genRetVoid(),
685 .store => try self.genStore(inst.castTag(.store).?),
686 .unreach => try self.genUnreach(),
687 else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}),
680 .loop => return try self.genLoop(inst.castTag(.loop).?),
681 .ret => return try self.genRet(inst.castTag(.ret).?),
682 .retvoid => return try self.genRetVoid(),
683 .store => return try self.genStore(inst.castTag(.store).?),
684 .unreach => return try self.genUnreach(),
685 else => return self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}),
688686 };
687
688 try self.inst_results.putNoClobber(inst, result_id);
689689 }
690690
691691 fn genBinOp(self: *DeclGen, inst: *Inst.BinOp) !ResultId {
......@@ -876,7 +876,7 @@ pub const DeclGen = struct {
876876 return result_id;
877877 }
878878
879 fn genBr(self: *DeclGen, inst: *Inst.Br) !?ResultId {
879 fn genBr(self: *DeclGen, inst: *Inst.Br) !void {
880880 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
881881 const target = self.blocks.get(inst.block).?;
882882
......@@ -891,19 +891,16 @@ pub const DeclGen = struct {
891891 }
892892
893893 try writeInstruction(&self.code, .OpBranch, &[_]Word{target.label_id});
894
895 return null;
896894 }
897895
898 fn genBrVoid(self: *DeclGen, inst: *Inst.BrVoid) !?ResultId {
896 fn genBrVoid(self: *DeclGen, inst: *Inst.BrVoid) !void {
899897 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
900898 const target = self.blocks.get(inst.block).?;
901899 // Don't need to add this to the incoming block list, as there is no value to insert in the phi node anyway.
902900 try writeInstruction(&self.code, .OpBranch, &[_]Word{target.label_id});
903 return null;
904901 }
905902
906 fn genCondBr(self: *DeclGen, inst: *Inst.CondBr) !?ResultId {
903 fn genCondBr(self: *DeclGen, inst: *Inst.CondBr) !void {
907904 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
908905 const condition_id = try self.resolve(inst.condition);
909906
......@@ -924,14 +921,11 @@ pub const DeclGen = struct {
924921 try self.genBody(inst.then_body);
925922 try self.beginSPIRVBlock(else_label_id);
926923 try self.genBody(inst.else_body);
927
928 return null;
929924 }
930925
931 fn genDbgStmt(self: *DeclGen, inst: *Inst.DbgStmt) !?ResultId {
926 fn genDbgStmt(self: *DeclGen, inst: *Inst.DbgStmt) !void {
932927 const src_fname_id = try self.spv.resolveSourceFileName(self.decl);
933928 try writeInstruction(&self.code, .OpLine, &[_]Word{ src_fname_id, inst.line, inst.column });
934 return null;
935929 }
936930
937931 fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId {
......@@ -950,7 +944,7 @@ pub const DeclGen = struct {
950944 return result_id;
951945 }
952946
953 fn genLoop(self: *DeclGen, inst: *Inst.Loop) !?ResultId {
947 fn genLoop(self: *DeclGen, inst: *Inst.Loop) !void {
954948 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
955949 const loop_label_id = self.spv.allocResultId();
956950
......@@ -963,23 +957,20 @@ pub const DeclGen = struct {
963957 try self.genBody(inst.body);
964958
965959 try writeInstruction(&self.code, .OpBranch, &[_]Word{ loop_label_id });
966 return null;
967960 }
968961
969 fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId {
962 fn genRet(self: *DeclGen, inst: *Inst.UnOp) !void {
970963 const operand_id = try self.resolve(inst.operand);
971964 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
972965 try writeInstruction(&self.code, .OpReturnValue, &[_]Word{operand_id});
973 return null;
974966 }
975967
976 fn genRetVoid(self: *DeclGen) !?ResultId {
968 fn genRetVoid(self: *DeclGen) !void {
977969 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
978970 try writeInstruction(&self.code, .OpReturn, &[_]Word{});
979 return null;
980971 }
981972
982 fn genStore(self: *DeclGen, inst: *Inst.BinOp) !?ResultId {
973 fn genStore(self: *DeclGen, inst: *Inst.BinOp) !void {
983974 const dst_ptr_id = try self.resolve(inst.lhs);
984975 const src_val_id = try self.resolve(inst.rhs);
985976
......@@ -989,12 +980,10 @@ pub const DeclGen = struct {
989980 &[_]Word{ dst_ptr_id, src_val_id };
990981
991982 try writeInstruction(&self.code, .OpStore, operands);
992 return null;
993983 }
994984
995 fn genUnreach(self: *DeclGen) !?ResultId {
985 fn genUnreach(self: *DeclGen) !void {
996986 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
997987 try writeInstruction(&self.code, .OpUnreachable, &[_]Word{});
998 return null;
999988 }
1000989};
src/link/SpirV.zig-1
......@@ -146,7 +146,6 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void {
146146 if (!decl.has_tv) continue;
147147
148148 decl.fn_link.spirv.id = spv.allocResultId();
149 log.debug("Allocating id {} to '{s}'", .{ decl.fn_link.spirv.id, std.mem.spanZ(decl.name) });
150149 }
151150 }
152151