| ... | ... | @@ -40,17 +40,109 @@ const SpvTypeInfo = struct { |
| 40 | 40 | |
| 41 | 41 | const TypeMap = std.AutoHashMapUnmanaged(InternPool.Index, SpvTypeInfo); |
| 42 | 42 | |
| 43 | | const IncomingBlock = struct { |
| 44 | | src_label_id: IdRef, |
| 45 | | break_value_id: IdRef, |
| 46 | | }; |
| 43 | const ControlFlow = union(enum) { |
| 44 | const Structured = struct { |
| 45 | /// This type indicates the way that a block is terminated. The |
| 46 | /// state of a particular block is used to track how a jump from |
| 47 | /// inside the block must reach the outside. |
| 48 | const Block = union(enum) { |
| 49 | const Incoming = struct { |
| 50 | src_label: IdRef, |
| 51 | /// Instruction that returns an u32 value of the |
| 52 | /// `Air.Inst.Index` that control flow should jump to. |
| 53 | next_block: IdRef, |
| 54 | }; |
| 47 | 55 | |
| 48 | | const Block = struct { |
| 49 | | label_id: ?IdRef, |
| 50 | | incoming_blocks: std.ArrayListUnmanaged(IncomingBlock), |
| 51 | | }; |
| 56 | const SelectionMerge = struct { |
| 57 | /// Incoming block from the `then` label. |
| 58 | /// Note that hte incoming block from the `else` label is |
| 59 | /// either given by the next element in the stack. |
| 60 | incoming: Incoming, |
| 61 | /// The label id of the cond_br's merge block. |
| 62 | /// For the top-most element in the stack, this |
| 63 | /// value is undefined. |
| 64 | merge_block: IdRef, |
| 65 | }; |
| 66 | |
| 67 | /// For a `selection` type block, we cannot use early exits, and we |
| 68 | /// must generate a 'merge ladder' of OpSelection instructions. To that end, |
| 69 | /// we keep a stack of the merges that still must be closed at the end of |
| 70 | /// a block. |
| 71 | /// |
| 72 | /// This entire structure basically just resembles a tree like |
| 73 | /// a x |
| 74 | /// \ / |
| 75 | /// b o merge |
| 76 | /// \ / |
| 77 | /// c o merge |
| 78 | /// \ / |
| 79 | /// o merge |
| 80 | /// / |
| 81 | /// o jump to next block |
| 82 | selection: struct { |
| 83 | /// In order to know which merges we still need to do, we need to keep |
| 84 | /// a stack of those. |
| 85 | merge_stack: std.ArrayListUnmanaged(SelectionMerge) = .{}, |
| 86 | }, |
| 87 | /// For a `loop` type block, we can early-exit the block by |
| 88 | /// jumping to the loop exit node, and we don't need to generate |
| 89 | /// an entire stack of merges. |
| 90 | loop: struct { |
| 91 | /// The next block to jump to can be determined from any number |
| 92 | /// of conditions that jump to the loop exit. |
| 93 | merges: std.ArrayListUnmanaged(Incoming) = .{}, |
| 94 | /// The label id of the loop's merge block. |
| 95 | merge_block: IdRef, |
| 96 | }, |
| 97 | |
| 98 | fn deinit(self: *Structured.Block, a: Allocator) void { |
| 99 | switch (self.*) { |
| 100 | .selection => |*merge| merge.merge_stack.deinit(a), |
| 101 | .loop => |*merge| merge.merges.deinit(a), |
| 102 | } |
| 103 | self.* = undefined; |
| 104 | } |
| 105 | }; |
| 106 | /// The stack of (structured) blocks that we are currently in. This determines |
| 107 | /// how exits from the current block must be handled. |
| 108 | block_stack: std.ArrayListUnmanaged(*Structured.Block) = .{}, |
| 109 | /// Maps `block` inst indices to the variable that the block's result |
| 110 | /// value must be written to. |
| 111 | block_results: std.AutoHashMapUnmanaged(Air.Inst.Index, IdRef) = .{}, |
| 112 | }; |
| 52 | 113 | |
| 53 | | const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, *Block); |
| 114 | const Unstructured = struct { |
| 115 | const Incoming = struct { |
| 116 | src_label: IdRef, |
| 117 | break_value_id: IdRef, |
| 118 | }; |
| 119 | |
| 120 | const Block = struct { |
| 121 | label: ?IdRef = null, |
| 122 | incoming_blocks: std.ArrayListUnmanaged(Incoming) = .{}, |
| 123 | }; |
| 124 | |
| 125 | /// We need to keep track of result ids for block labels, as well as the 'incoming' |
| 126 | /// blocks for a block. |
| 127 | blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, *Block) = .{}, |
| 128 | }; |
| 129 | |
| 130 | structured: Structured, |
| 131 | unstructured: Unstructured, |
| 132 | |
| 133 | pub fn deinit(self: *ControlFlow, a: Allocator) void { |
| 134 | switch (self.*) { |
| 135 | .structured => |*cf| { |
| 136 | cf.block_stack.deinit(a); |
| 137 | cf.block_results.deinit(a); |
| 138 | }, |
| 139 | .unstructured => |*cf| { |
| 140 | cf.blocks.deinit(a); |
| 141 | }, |
| 142 | } |
| 143 | self.* = undefined; |
| 144 | } |
| 145 | }; |
| 54 | 146 | |
| 55 | 147 | /// This structure holds information that is relevant to the entire compilation, |
| 56 | 148 | /// in contrast to `DeclGen`, which only holds relevant information about a |
| ... | ... | @@ -99,6 +191,14 @@ pub const Object = struct { |
| 99 | 191 | air: Air, |
| 100 | 192 | liveness: Liveness, |
| 101 | 193 | ) !void { |
| 194 | const target = mod.getTarget(); |
| 195 | // We always want a structured control flow in shaders. This option is only relevant |
| 196 | // for OpenCL kernels. |
| 197 | const want_structured_cfg = switch (target.os.tag) { |
| 198 | .opencl => mod.comp.bin_file.options.want_structured_cfg orelse false, |
| 199 | else => true, |
| 200 | }; |
| 201 | |
| 102 | 202 | var decl_gen = DeclGen{ |
| 103 | 203 | .gpa = self.gpa, |
| 104 | 204 | .object = self, |
| ... | ... | @@ -108,7 +208,11 @@ pub const Object = struct { |
| 108 | 208 | .air = air, |
| 109 | 209 | .liveness = liveness, |
| 110 | 210 | .type_map = &self.type_map, |
| 111 | | .current_block_label_id = undefined, |
| 211 | .control_flow = switch (want_structured_cfg) { |
| 212 | true => .{ .structured = .{} }, |
| 213 | false => .{ .unstructured = .{} }, |
| 214 | }, |
| 215 | .current_block_label = undefined, |
| 112 | 216 | }; |
| 113 | 217 | defer decl_gen.deinit(); |
| 114 | 218 | |
| ... | ... | @@ -213,12 +317,11 @@ const DeclGen = struct { |
| 213 | 317 | /// is already in this map, its recursive. |
| 214 | 318 | wip_pointers: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, CacheRef) = .{}, |
| 215 | 319 | |
| 216 | | /// We need to keep track of result ids for block labels, as well as the 'incoming' |
| 217 | | /// blocks for a block. |
| 218 | | blocks: BlockMap = .{}, |
| 320 | /// This field keeps track of the current state wrt structured or unstructured control flow. |
| 321 | control_flow: ControlFlow, |
| 219 | 322 | |
| 220 | 323 | /// The label of the SPIR-V block we are currently generating. |
| 221 | | current_block_label_id: IdRef, |
| 324 | current_block_label: IdRef, |
| 222 | 325 | |
| 223 | 326 | /// The code (prologue and body) for the function we are currently generating code for. |
| 224 | 327 | func: SpvModule.Fn = .{}, |
| ... | ... | @@ -300,7 +403,7 @@ const DeclGen = struct { |
| 300 | 403 | self.args.deinit(self.gpa); |
| 301 | 404 | self.inst_results.deinit(self.gpa); |
| 302 | 405 | self.wip_pointers.deinit(self.gpa); |
| 303 | | self.blocks.deinit(self.gpa); |
| 406 | self.control_flow.deinit(self.gpa); |
| 304 | 407 | self.func.deinit(self.gpa); |
| 305 | 408 | self.base_line_stack.deinit(self.gpa); |
| 306 | 409 | } |
| ... | ... | @@ -384,10 +487,11 @@ const DeclGen = struct { |
| 384 | 487 | // TODO: This should probably be made a little more robust. |
| 385 | 488 | const func = self.func; |
| 386 | 489 | defer self.func = func; |
| 387 | | const block_label_id = self.current_block_label_id; |
| 388 | | defer self.current_block_label_id = block_label_id; |
| 490 | const block_label = self.current_block_label; |
| 491 | defer self.current_block_label = block_label; |
| 389 | 492 | |
| 390 | 493 | self.func = .{}; |
| 494 | defer self.func.deinit(self.gpa); |
| 391 | 495 | |
| 392 | 496 | // TODO: Merge this with genDecl? |
| 393 | 497 | const begin = self.spv.beginGlobal(); |
| ... | ... | @@ -409,7 +513,7 @@ const DeclGen = struct { |
| 409 | 513 | try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ |
| 410 | 514 | .id_result = root_block_id, |
| 411 | 515 | }); |
| 412 | | self.current_block_label_id = root_block_id; |
| 516 | self.current_block_label = root_block_id; |
| 413 | 517 | |
| 414 | 518 | const val_id = try self.constant(ty, val.toValue(), .indirect); |
| 415 | 519 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| ... | ... | @@ -432,9 +536,9 @@ const DeclGen = struct { |
| 432 | 536 | /// block we are currently generating. |
| 433 | 537 | /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to |
| 434 | 538 | /// keep track of the previous block. |
| 435 | | fn beginSpvBlock(self: *DeclGen, label_id: IdResult) !void { |
| 436 | | try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label_id }); |
| 437 | | self.current_block_label_id = label_id; |
| 539 | fn beginSpvBlock(self: *DeclGen, label: IdResult) !void { |
| 540 | try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label }); |
| 541 | self.current_block_label = label; |
| 438 | 542 | } |
| 439 | 543 | |
| 440 | 544 | /// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need |
| ... | ... | @@ -1783,13 +1887,22 @@ const DeclGen = struct { |
| 1783 | 1887 | try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ |
| 1784 | 1888 | .id_result = root_block_id, |
| 1785 | 1889 | }); |
| 1786 | | self.current_block_label_id = root_block_id; |
| 1890 | self.current_block_label = root_block_id; |
| 1787 | 1891 | |
| 1788 | 1892 | const main_body = self.air.getMainBody(); |
| 1789 | | try self.genBody(main_body); |
| 1790 | | |
| 1791 | | // Append the actual code into the functions section. |
| 1893 | switch (self.control_flow) { |
| 1894 | .structured => { |
| 1895 | _ = try self.genStructuredBody(.selection, main_body); |
| 1896 | // We always expect paths to here to end, but we still need the block |
| 1897 | // to act as a dummy merge block. |
| 1898 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 1899 | }, |
| 1900 | .unstructured => { |
| 1901 | try self.genBody(main_body); |
| 1902 | }, |
| 1903 | } |
| 1792 | 1904 | try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); |
| 1905 | // Append the actual code into the functions section. |
| 1793 | 1906 | try self.spv.addFunction(spv_decl_index, self.func); |
| 1794 | 1907 | |
| 1795 | 1908 | const fqn = ip.stringToSlice(try decl.getFullyQualifiedName(self.module)); |
| ... | ... | @@ -1847,7 +1960,7 @@ const DeclGen = struct { |
| 1847 | 1960 | try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ |
| 1848 | 1961 | .id_result = root_block_id, |
| 1849 | 1962 | }); |
| 1850 | | self.current_block_label_id = root_block_id; |
| 1963 | self.current_block_label = root_block_id; |
| 1851 | 1964 | |
| 1852 | 1965 | const val_id = try self.constant(decl.ty, init_val, .indirect); |
| 1853 | 1966 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| ... | ... | @@ -3646,6 +3759,154 @@ const DeclGen = struct { |
| 3646 | 3759 | return self.args.items[self.next_arg_index]; |
| 3647 | 3760 | } |
| 3648 | 3761 | |
| 3762 | /// Given a slice of incoming block connections, returns the block-id of the next |
| 3763 | /// block to jump to. This function emits instructions, so it should be emitted |
| 3764 | /// inside the merge block of the block. |
| 3765 | /// This function should only be called with structured control flow generation. |
| 3766 | fn structuredNextBlock(self: *DeclGen, incoming: []const ControlFlow.Structured.Block.Incoming) !IdRef { |
| 3767 | assert(self.control_flow == .structured); |
| 3768 | |
| 3769 | const result_id = self.spv.allocId(); |
| 3770 | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 3771 | try self.func.body.emitRaw(self.spv.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent... |
| 3772 | self.func.body.writeOperand(spec.IdResultType, self.typeId(block_id_ty_ref)); |
| 3773 | self.func.body.writeOperand(spec.IdRef, result_id); |
| 3774 | |
| 3775 | for (incoming) |incoming_block| { |
| 3776 | self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming_block.next_block, incoming_block.src_label }); |
| 3777 | } |
| 3778 | |
| 3779 | return result_id; |
| 3780 | } |
| 3781 | |
| 3782 | /// Jumps to the block with the target block-id. This function must only be called when |
| 3783 | /// terminating a body, there should be no instructions after it. |
| 3784 | /// This function should only be called with structured control flow generation. |
| 3785 | fn structuredBreak(self: *DeclGen, target_block: IdRef) !void { |
| 3786 | assert(self.control_flow == .structured); |
| 3787 | |
| 3788 | const sblock = self.control_flow.structured.block_stack.getLast(); |
| 3789 | const merge_block = switch (sblock.*) { |
| 3790 | .selection => |*merge| blk: { |
| 3791 | const merge_label = self.spv.allocId(); |
| 3792 | try merge.merge_stack.append(self.gpa, .{ |
| 3793 | .incoming = .{ |
| 3794 | .src_label = self.current_block_label, |
| 3795 | .next_block = target_block, |
| 3796 | }, |
| 3797 | .merge_block = merge_label, |
| 3798 | }); |
| 3799 | break :blk merge_label; |
| 3800 | }, |
| 3801 | // Loop blocks do not end in a break. Not through a direct break, |
| 3802 | // and also not through another instruction like cond_br or unreachable (these |
| 3803 | // situations are replaced by `cond_br` in sema, or there is a `block` instruction |
| 3804 | // placed around them). |
| 3805 | .loop => unreachable, |
| 3806 | }; |
| 3807 | |
| 3808 | try self.func.body.emitBranch(self.spv.gpa, merge_block); |
| 3809 | } |
| 3810 | |
| 3811 | /// Generate a body in a way that exits the body using only structured constructs. |
| 3812 | /// Returns the block-id of the next block to jump to. After this function, a jump |
| 3813 | /// should still be emitted to the block that should follow this structured body. |
| 3814 | /// This function should only be called with structured control flow generation. |
| 3815 | fn genStructuredBody( |
| 3816 | self: *DeclGen, |
| 3817 | /// This parameter defines the method that this structured body is exited with. |
| 3818 | block_merge_type: union(enum) { |
| 3819 | /// Using selection; early exits from this body are surrounded with |
| 3820 | /// if() statements. |
| 3821 | selection, |
| 3822 | /// Using loops; loops can be early exited by jumping to the merge block at |
| 3823 | /// any time. |
| 3824 | loop: struct { |
| 3825 | merge_label: IdRef, |
| 3826 | continue_label: IdRef, |
| 3827 | }, |
| 3828 | }, |
| 3829 | body: []const Air.Inst.Index, |
| 3830 | ) !IdRef { |
| 3831 | assert(self.control_flow == .structured); |
| 3832 | |
| 3833 | var sblock: ControlFlow.Structured.Block = switch (block_merge_type) { |
| 3834 | .loop => |merge| .{ .loop = .{ |
| 3835 | .merge_block = merge.merge_label, |
| 3836 | } }, |
| 3837 | .selection => .{ .selection = .{} }, |
| 3838 | }; |
| 3839 | defer sblock.deinit(self.gpa); |
| 3840 | |
| 3841 | { |
| 3842 | try self.control_flow.structured.block_stack.append(self.gpa, &sblock); |
| 3843 | defer _ = self.control_flow.structured.block_stack.pop(); |
| 3844 | |
| 3845 | try self.genBody(body); |
| 3846 | } |
| 3847 | |
| 3848 | switch (sblock) { |
| 3849 | .selection => |merge| { |
| 3850 | // Now generate the merge block for all merges that |
| 3851 | // still need to be performed. |
| 3852 | const merge_stack = merge.merge_stack.items; |
| 3853 | |
| 3854 | // If no merges on the stack, this block didn't generate any jumps (all paths |
| 3855 | // ended with a return or an unreachable). In that case, we don't need to do |
| 3856 | // any merging. |
| 3857 | if (merge_stack.len == 0) { |
| 3858 | // We still need to return a value of a next block to jump to. |
| 3859 | // For example, if we have code like |
| 3860 | // if (x) { |
| 3861 | // if (y) return else return; |
| 3862 | // } else {} |
| 3863 | // then we still need the outer to have an OpSelectionMerge and consequently |
| 3864 | // a phi node. In that case we can just return bogus, since we know that its |
| 3865 | // path will never be taken. |
| 3866 | |
| 3867 | // Make sure that we are still in a block when exiting the function. |
| 3868 | // TODO: Can we get rid of that? |
| 3869 | try self.beginSpvBlock(self.spv.allocId()); |
| 3870 | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 3871 | return try self.spv.constUndef(block_id_ty_ref); |
| 3872 | } |
| 3873 | |
| 3874 | // The top-most merge actually only has a single source, the |
| 3875 | // final jump of the block, or the merge block of a sub-block, cond_br, |
| 3876 | // or loop. Therefore we just need to generate a block with a jump to the |
| 3877 | // next merge block. |
| 3878 | try self.beginSpvBlock(merge_stack[merge_stack.len - 1].merge_block); |
| 3879 | |
| 3880 | // Now generate a merge ladder for the remaining merges in the stack. |
| 3881 | var incoming = ControlFlow.Structured.Block.Incoming{ |
| 3882 | .src_label = self.current_block_label, |
| 3883 | .next_block = merge_stack[merge_stack.len - 1].incoming.next_block, |
| 3884 | }; |
| 3885 | var i = merge_stack.len - 1; |
| 3886 | while (i > 0) { |
| 3887 | i -= 1; |
| 3888 | const step = merge_stack[i]; |
| 3889 | try self.func.body.emitBranch(self.spv.gpa, step.merge_block); |
| 3890 | try self.beginSpvBlock(step.merge_block); |
| 3891 | const next_block = try self.structuredNextBlock(&.{ incoming, step.incoming }); |
| 3892 | incoming = .{ |
| 3893 | .src_label = step.merge_block, |
| 3894 | .next_block = next_block, |
| 3895 | }; |
| 3896 | } |
| 3897 | |
| 3898 | return incoming.next_block; |
| 3899 | }, |
| 3900 | .loop => |merge| { |
| 3901 | // Close the loop by jumping to the continue label |
| 3902 | try self.func.body.emitBranch(self.spv.gpa, block_merge_type.loop.continue_label); |
| 3903 | // For blocks we must simple merge all the incoming blocks to get the next block. |
| 3904 | try self.beginSpvBlock(merge.merge_block); |
| 3905 | return try self.structuredNextBlock(merge.merges.items); |
| 3906 | }, |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3649 | 3910 | fn airBlock(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 3650 | 3911 | // In AIR, a block doesn't really define an entry point like a block, but |
| 3651 | 3912 | // more like a scope that breaks can jump out of and "return" a value from. |
| ... | ... | @@ -3661,62 +3922,170 @@ const DeclGen = struct { |
| 3661 | 3922 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; |
| 3662 | 3923 | const have_block_result = ty.isFnOrHasRuntimeBitsIgnoreComptime(mod); |
| 3663 | 3924 | |
| 3664 | | // 4 chosen as arbitrary initial capacity. |
| 3665 | | var block = Block{ |
| 3666 | | // Label id is lazily allocated if needed. |
| 3667 | | .label_id = null, |
| 3668 | | .incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.gpa, 4), |
| 3925 | const cf = switch (self.control_flow) { |
| 3926 | .structured => |*cf| cf, |
| 3927 | .unstructured => |*cf| { |
| 3928 | var block = ControlFlow.Unstructured.Block{}; |
| 3929 | defer block.incoming_blocks.deinit(self.gpa); |
| 3930 | |
| 3931 | // 4 chosen as arbitrary initial capacity. |
| 3932 | try block.incoming_blocks.ensureUnusedCapacity(self.gpa, 4); |
| 3933 | |
| 3934 | try cf.blocks.putNoClobber(self.gpa, inst, &block); |
| 3935 | defer assert(cf.blocks.remove(inst)); |
| 3936 | |
| 3937 | try self.genBody(body); |
| 3938 | |
| 3939 | // Only begin a new block if there were actually any breaks towards it. |
| 3940 | if (block.label) |label| { |
| 3941 | try self.beginSpvBlock(label); |
| 3942 | } |
| 3943 | |
| 3944 | if (!have_block_result) |
| 3945 | return null; |
| 3946 | |
| 3947 | assert(block.label != null); |
| 3948 | const result_id = self.spv.allocId(); |
| 3949 | const result_type_id = try self.resolveTypeId(ty); |
| 3950 | |
| 3951 | try self.func.body.emitRaw( |
| 3952 | self.spv.gpa, |
| 3953 | .OpPhi, |
| 3954 | // result type + result + variable/parent... |
| 3955 | 2 + @as(u16, @intCast(block.incoming_blocks.items.len * 2)), |
| 3956 | ); |
| 3957 | self.func.body.writeOperand(spec.IdResultType, result_type_id); |
| 3958 | self.func.body.writeOperand(spec.IdRef, result_id); |
| 3959 | |
| 3960 | for (block.incoming_blocks.items) |incoming| { |
| 3961 | self.func.body.writeOperand( |
| 3962 | spec.PairIdRefIdRef, |
| 3963 | .{ incoming.break_value_id, incoming.src_label }, |
| 3964 | ); |
| 3965 | } |
| 3966 | |
| 3967 | return result_id; |
| 3968 | }, |
| 3669 | 3969 | }; |
| 3670 | | defer block.incoming_blocks.deinit(self.gpa); |
| 3671 | 3970 | |
| 3672 | | try self.blocks.putNoClobber(self.gpa, inst, &block); |
| 3673 | | defer assert(self.blocks.remove(inst)); |
| 3971 | const maybe_block_result_var_id = if (have_block_result) blk: { |
| 3972 | const block_result_var_id = try self.alloc(ty, .{ .storage_class = .Function }); |
| 3973 | try cf.block_results.putNoClobber(self.gpa, inst, block_result_var_id); |
| 3974 | break :blk block_result_var_id; |
| 3975 | } else null; |
| 3976 | defer if (have_block_result) assert(cf.block_results.remove(inst)); |
| 3674 | 3977 | |
| 3675 | | try self.genBody(body); |
| 3978 | const next_block = try self.genStructuredBody(.selection, body); |
| 3676 | 3979 | |
| 3677 | | // Only begin a new block if there were actually any breaks towards it. |
| 3678 | | if (block.label_id) |label_id| { |
| 3679 | | try self.beginSpvBlock(label_id); |
| 3680 | | } |
| 3980 | // When encountering a block instruction, we are always at least in the function's scope, |
| 3981 | // so there always has to be another entry. |
| 3982 | assert(cf.block_stack.items.len > 0); |
| 3681 | 3983 | |
| 3682 | | if (!have_block_result) |
| 3683 | | return null; |
| 3984 | // Check if the target of the branch was this current block. |
| 3985 | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 3986 | const this_block = try self.constInt(block_id_ty_ref, inst); |
| 3987 | const jump_to_this_block_id = self.spv.allocId(); |
| 3988 | const bool_ty_ref = try self.resolveType(Type.bool, .direct); |
| 3989 | try self.func.body.emit(self.spv.gpa, .OpIEqual, .{ |
| 3990 | .id_result_type = self.typeId(bool_ty_ref), |
| 3991 | .id_result = jump_to_this_block_id, |
| 3992 | .operand_1 = next_block, |
| 3993 | .operand_2 = this_block, |
| 3994 | }); |
| 3684 | 3995 | |
| 3685 | | assert(block.label_id != null); |
| 3686 | | const result_id = self.spv.allocId(); |
| 3687 | | const result_type_id = try self.resolveTypeId(ty); |
| 3996 | const sblock = cf.block_stack.getLast(); |
| 3688 | 3997 | |
| 3689 | | try self.func.body.emitRaw(self.spv.gpa, .OpPhi, 2 + @as(u16, @intCast(block.incoming_blocks.items.len * 2))); // result type + result + variable/parent... |
| 3690 | | self.func.body.writeOperand(spec.IdResultType, result_type_id); |
| 3691 | | self.func.body.writeOperand(spec.IdRef, result_id); |
| 3998 | if (ty.isNoReturn(mod)) { |
| 3999 | // If this block is noreturn, this instruction is the last of a block, |
| 4000 | // and we must simply jump to the block's merge unconditionally. |
| 4001 | try self.structuredBreak(next_block); |
| 4002 | } else { |
| 4003 | switch (sblock.*) { |
| 4004 | .selection => |*merge| { |
| 4005 | // To jump out of a selection block, push a new entry onto its merge stack and |
| 4006 | // generate a conditional branch to there and to the instructions following this block. |
| 4007 | const merge_label = self.spv.allocId(); |
| 4008 | const then_label = self.spv.allocId(); |
| 4009 | try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ |
| 4010 | .merge_block = merge_label, |
| 4011 | .selection_control = .{}, |
| 4012 | }); |
| 4013 | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 4014 | .condition = jump_to_this_block_id, |
| 4015 | .true_label = then_label, |
| 4016 | .false_label = merge_label, |
| 4017 | }); |
| 4018 | try merge.merge_stack.append(self.gpa, .{ |
| 4019 | .incoming = .{ |
| 4020 | .src_label = self.current_block_label, |
| 4021 | .next_block = next_block, |
| 4022 | }, |
| 4023 | .merge_block = merge_label, |
| 4024 | }); |
| 3692 | 4025 | |
| 3693 | | for (block.incoming_blocks.items) |incoming| { |
| 3694 | | self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming.break_value_id, incoming.src_label_id }); |
| 4026 | try self.beginSpvBlock(then_label); |
| 4027 | }, |
| 4028 | .loop => |*merge| { |
| 4029 | // To jump out of a loop block, generate a conditional that exits the block |
| 4030 | // to the loop merge if the target ID is not the one of this block. |
| 4031 | const continue_label = self.spv.allocId(); |
| 4032 | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 4033 | .condition = jump_to_this_block_id, |
| 4034 | .true_label = continue_label, |
| 4035 | .false_label = merge.merge_block, |
| 4036 | }); |
| 4037 | try merge.merges.append(self.gpa, .{ |
| 4038 | .src_label = self.current_block_label, |
| 4039 | .next_block = next_block, |
| 4040 | }); |
| 4041 | try self.beginSpvBlock(continue_label); |
| 4042 | }, |
| 4043 | } |
| 3695 | 4044 | } |
| 3696 | 4045 | |
| 3697 | | return result_id; |
| 4046 | if (maybe_block_result_var_id) |block_result_var_id| { |
| 4047 | return try self.load(ty, block_result_var_id, .{}); |
| 4048 | } |
| 4049 | |
| 4050 | return null; |
| 3698 | 4051 | } |
| 3699 | 4052 | |
| 3700 | 4053 | fn airBr(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 4054 | const mod = self.module; |
| 3701 | 4055 | const br = self.air.instructions.items(.data)[inst].br; |
| 3702 | 4056 | const operand_ty = self.typeOf(br.operand); |
| 3703 | | const block = self.blocks.get(br.block_inst).?; |
| 3704 | 4057 | |
| 3705 | | const mod = self.module; |
| 3706 | | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 3707 | | const operand_id = try self.resolve(br.operand); |
| 3708 | | // current_block_label_id should not be undefined here, lest there is a br or br_void in the function's body. |
| 3709 | | try block.incoming_blocks.append(self.gpa, .{ |
| 3710 | | .src_label_id = self.current_block_label_id, |
| 3711 | | .break_value_id = operand_id, |
| 3712 | | }); |
| 3713 | | } |
| 4058 | switch (self.control_flow) { |
| 4059 | .structured => |*cf| { |
| 4060 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 4061 | const operand_id = try self.resolve(br.operand); |
| 4062 | const block_result_var_id = cf.block_results.get(br.block_inst).?; |
| 4063 | try self.store(operand_ty, block_result_var_id, operand_id, .{}); |
| 4064 | } |
| 3714 | 4065 | |
| 3715 | | if (block.label_id == null) { |
| 3716 | | block.label_id = self.spv.allocId(); |
| 3717 | | } |
| 4066 | const block_id_ty_ref = try self.intType(.unsigned, 32); |
| 4067 | const next_block = try self.constInt(block_id_ty_ref, br.block_inst); |
| 4068 | try self.structuredBreak(next_block); |
| 4069 | }, |
| 4070 | .unstructured => |cf| { |
| 4071 | const block = cf.blocks.get(br.block_inst).?; |
| 4072 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { |
| 4073 | const operand_id = try self.resolve(br.operand); |
| 4074 | // current_block_label should not be undefined here, lest there |
| 4075 | // is a br or br_void in the function's body. |
| 4076 | try block.incoming_blocks.append(self.gpa, .{ |
| 4077 | .src_label = self.current_block_label, |
| 4078 | .break_value_id = operand_id, |
| 4079 | }); |
| 4080 | } |
| 3718 | 4081 | |
| 3719 | | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = block.label_id.? }); |
| 4082 | if (block.label == null) { |
| 4083 | block.label = self.spv.allocId(); |
| 4084 | } |
| 4085 | |
| 4086 | try self.func.body.emitBranch(self.spv.gpa, block.label.?); |
| 4087 | }, |
| 4088 | } |
| 3720 | 4089 | } |
| 3721 | 4090 | |
| 3722 | 4091 | fn airCondBr(self: *DeclGen, inst: Air.Inst.Index) !void { |
| ... | ... | @@ -3726,23 +4095,104 @@ const DeclGen = struct { |
| 3726 | 4095 | const else_body = self.air.extra[cond_br.end + then_body.len ..][0..cond_br.data.else_body_len]; |
| 3727 | 4096 | const condition_id = try self.resolve(pl_op.operand); |
| 3728 | 4097 | |
| 3729 | | // These will always generate a new SPIR-V block, since they are ir.Body and not ir.Block. |
| 3730 | | const then_label_id = self.spv.allocId(); |
| 3731 | | const else_label_id = self.spv.allocId(); |
| 4098 | const then_label = self.spv.allocId(); |
| 4099 | const else_label = self.spv.allocId(); |
| 3732 | 4100 | |
| 3733 | | // TODO: We can generate OpSelectionMerge here if we know the target block that both of these will resolve to, |
| 3734 | | // but i don't know if those will always resolve to the same block. |
| 4101 | switch (self.control_flow) { |
| 4102 | .structured => { |
| 4103 | const merge_label = self.spv.allocId(); |
| 3735 | 4104 | |
| 3736 | | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 3737 | | .condition = condition_id, |
| 3738 | | .true_label = then_label_id, |
| 3739 | | .false_label = else_label_id, |
| 3740 | | }); |
| 4105 | try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ |
| 4106 | .merge_block = merge_label, |
| 4107 | .selection_control = .{}, |
| 4108 | }); |
| 4109 | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 4110 | .condition = condition_id, |
| 4111 | .true_label = then_label, |
| 4112 | .false_label = else_label, |
| 4113 | }); |
| 3741 | 4114 | |
| 3742 | | try self.beginSpvBlock(then_label_id); |
| 3743 | | try self.genBody(then_body); |
| 3744 | | try self.beginSpvBlock(else_label_id); |
| 3745 | | try self.genBody(else_body); |
| 4115 | try self.beginSpvBlock(then_label); |
| 4116 | const then_next = try self.genStructuredBody(.selection, then_body); |
| 4117 | const then_incoming = ControlFlow.Structured.Block.Incoming{ |
| 4118 | .src_label = self.current_block_label, |
| 4119 | .next_block = then_next, |
| 4120 | }; |
| 4121 | try self.func.body.emitBranch(self.spv.gpa, merge_label); |
| 4122 | |
| 4123 | try self.beginSpvBlock(else_label); |
| 4124 | const else_next = try self.genStructuredBody(.selection, else_body); |
| 4125 | const else_incoming = ControlFlow.Structured.Block.Incoming{ |
| 4126 | .src_label = self.current_block_label, |
| 4127 | .next_block = else_next, |
| 4128 | }; |
| 4129 | try self.func.body.emitBranch(self.spv.gpa, merge_label); |
| 4130 | |
| 4131 | try self.beginSpvBlock(merge_label); |
| 4132 | const next_block = try self.structuredNextBlock(&.{ then_incoming, else_incoming }); |
| 4133 | |
| 4134 | try self.structuredBreak(next_block); |
| 4135 | }, |
| 4136 | .unstructured => { |
| 4137 | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 4138 | .condition = condition_id, |
| 4139 | .true_label = then_label, |
| 4140 | .false_label = else_label, |
| 4141 | }); |
| 4142 | |
| 4143 | try self.beginSpvBlock(then_label); |
| 4144 | try self.genBody(then_body); |
| 4145 | try self.beginSpvBlock(else_label); |
| 4146 | try self.genBody(else_body); |
| 4147 | }, |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 4152 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 4153 | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 4154 | const body = self.air.extra[loop.end..][0..loop.data.body_len]; |
| 4155 | |
| 4156 | const body_label = self.spv.allocId(); |
| 4157 | |
| 4158 | switch (self.control_flow) { |
| 4159 | .structured => { |
| 4160 | const header_label = self.spv.allocId(); |
| 4161 | const merge_label = self.spv.allocId(); |
| 4162 | const continue_label = self.spv.allocId(); |
| 4163 | |
| 4164 | // The back-edge must point to the loop header, so generate a separate block for the |
| 4165 | // loop header so that we don't accidentally include some instructions from there |
| 4166 | // in the loop. |
| 4167 | try self.func.body.emitBranch(self.spv.gpa, header_label); |
| 4168 | try self.beginSpvBlock(header_label); |
| 4169 | |
| 4170 | // Emit loop header and jump to loop body |
| 4171 | try self.func.body.emit(self.spv.gpa, .OpLoopMerge, .{ |
| 4172 | .merge_block = merge_label, |
| 4173 | .continue_target = continue_label, |
| 4174 | .loop_control = .{}, |
| 4175 | }); |
| 4176 | try self.func.body.emitBranch(self.spv.gpa, body_label); |
| 4177 | |
| 4178 | try self.beginSpvBlock(body_label); |
| 4179 | |
| 4180 | const next_block = try self.genStructuredBody(.{ .loop = .{ |
| 4181 | .merge_label = merge_label, |
| 4182 | .continue_label = continue_label, |
| 4183 | } }, body); |
| 4184 | try self.structuredBreak(next_block); |
| 4185 | |
| 4186 | try self.beginSpvBlock(continue_label); |
| 4187 | try self.func.body.emitBranch(self.spv.gpa, header_label); |
| 4188 | }, |
| 4189 | .unstructured => { |
| 4190 | try self.func.body.emitBranch(self.spv.gpa, body_label); |
| 4191 | try self.beginSpvBlock(body_label); |
| 4192 | try self.genBody(body); |
| 4193 | try self.func.body.emitBranch(self.spv.gpa, body_label); |
| 4194 | }, |
| 4195 | } |
| 3746 | 4196 | } |
| 3747 | 4197 | |
| 3748 | 4198 | fn airLoad(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| ... | ... | @@ -3766,22 +4216,6 @@ const DeclGen = struct { |
| 3766 | 4216 | try self.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(self.module) }); |
| 3767 | 4217 | } |
| 3768 | 4218 | |
| 3769 | | fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 3770 | | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 3771 | | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 3772 | | const body = self.air.extra[loop.end..][0..loop.data.body_len]; |
| 3773 | | const loop_label_id = self.spv.allocId(); |
| 3774 | | |
| 3775 | | // Jump to the loop entry point |
| 3776 | | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id }); |
| 3777 | | |
| 3778 | | // TODO: Look into OpLoopMerge. |
| 3779 | | try self.beginSpvBlock(loop_label_id); |
| 3780 | | try self.genBody(body); |
| 3781 | | |
| 3782 | | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id }); |
| 3783 | | } |
| 3784 | | |
| 3785 | 4219 | fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 3786 | 4220 | const operand = self.air.instructions.items(.data)[inst].un_op; |
| 3787 | 4221 | const ret_ty = self.typeOf(operand); |
| ... | ... | @@ -3870,7 +4304,20 @@ const DeclGen = struct { |
| 3870 | 4304 | const err_block = self.spv.allocId(); |
| 3871 | 4305 | const ok_block = self.spv.allocId(); |
| 3872 | 4306 | |
| 3873 | | // TODO: Merge block |
| 4307 | switch (self.control_flow) { |
| 4308 | .structured => { |
| 4309 | // According to AIR documentation, this block is guaranteed |
| 4310 | // to not break and end in a return instruction. Thus, |
| 4311 | // for structured control flow, we can just naively use |
| 4312 | // the ok block as the merge block here. |
| 4313 | try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ |
| 4314 | .merge_block = ok_block, |
| 4315 | .selection_control = .{}, |
| 4316 | }); |
| 4317 | }, |
| 4318 | .unstructured => {}, |
| 4319 | } |
| 4320 | |
| 3874 | 4321 | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 3875 | 4322 | .condition = is_err_id, |
| 3876 | 4323 | .true_label = err_block, |
| ... | ... | @@ -3881,7 +4328,6 @@ const DeclGen = struct { |
| 3881 | 4328 | try self.genBody(body); |
| 3882 | 4329 | |
| 3883 | 4330 | try self.beginSpvBlock(ok_block); |
| 3884 | | // Now just extract the payload, if required. |
| 3885 | 4331 | } |
| 3886 | 4332 | if (self.liveness.isUnused(inst)) { |
| 3887 | 4333 | return null; |
| ... | ... | @@ -3890,6 +4336,7 @@ const DeclGen = struct { |
| 3890 | 4336 | return null; |
| 3891 | 4337 | } |
| 3892 | 4338 | |
| 4339 | // Now just extract the payload, if required. |
| 3893 | 4340 | return try self.extractField(payload_ty, err_union_id, eu_layout.payloadFieldIndex()); |
| 3894 | 4341 | } |
| 3895 | 4342 | |
| ... | ... | @@ -4155,9 +4602,8 @@ const DeclGen = struct { |
| 4155 | 4602 | // Zig switches are grouped by condition, so we need to loop through all of them |
| 4156 | 4603 | const num_conditions = blk: { |
| 4157 | 4604 | var extra_index: usize = switch_br.end; |
| 4158 | | var case_i: u32 = 0; |
| 4159 | 4605 | var num_conditions: u32 = 0; |
| 4160 | | while (case_i < num_cases) : (case_i += 1) { |
| 4606 | for (0..num_cases) |_| { |
| 4161 | 4607 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 4162 | 4608 | const case_body = self.air.extra[case.end + case.data.items_len ..][0..case.data.body_len]; |
| 4163 | 4609 | extra_index = case.end + case.data.items_len + case_body.len; |
| ... | ... | @@ -4171,6 +4617,18 @@ const DeclGen = struct { |
| 4171 | 4617 | // We always need the default case - if zig has none, we will generate unreachable there. |
| 4172 | 4618 | const default = self.spv.allocId(); |
| 4173 | 4619 | |
| 4620 | const merge_label = switch (self.control_flow) { |
| 4621 | .structured => self.spv.allocId(), |
| 4622 | .unstructured => null, |
| 4623 | }; |
| 4624 | |
| 4625 | if (self.control_flow == .structured) { |
| 4626 | try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ |
| 4627 | .merge_block = merge_label.?, |
| 4628 | .selection_control = .{}, |
| 4629 | }); |
| 4630 | } |
| 4631 | |
| 4174 | 4632 | // Emit the instruction before generating the blocks. |
| 4175 | 4633 | try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions); |
| 4176 | 4634 | self.func.body.writeOperand(IdRef, cond_indirect); |
| ... | ... | @@ -4179,20 +4637,17 @@ const DeclGen = struct { |
| 4179 | 4637 | // Emit each of the cases |
| 4180 | 4638 | { |
| 4181 | 4639 | var extra_index: usize = switch_br.end; |
| 4182 | | var case_i: u32 = 0; |
| 4183 | | while (case_i < num_cases) : (case_i += 1) { |
| 4640 | for (0..num_cases) |case_i| { |
| 4184 | 4641 | // SPIR-V needs a literal here, which' width depends on the case condition. |
| 4185 | 4642 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 4186 | 4643 | const items = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[case.end..][0..case.data.items_len])); |
| 4187 | 4644 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 4188 | 4645 | extra_index = case.end + case.data.items_len + case_body.len; |
| 4189 | 4646 | |
| 4190 | | const label = IdRef{ .id = first_case_label.id + case_i }; |
| 4647 | const label = IdRef{ .id = @intCast(first_case_label.id + case_i) }; |
| 4191 | 4648 | |
| 4192 | 4649 | for (items) |item| { |
| 4193 | | const value = (try self.air.value(item, mod)) orelse { |
| 4194 | | return self.todo("switch on runtime value???", .{}); |
| 4195 | | }; |
| 4650 | const value = (try self.air.value(item, mod)) orelse unreachable; |
| 4196 | 4651 | const int_val = switch (cond_ty.zigTypeTag(mod)) { |
| 4197 | 4652 | .Bool, .Int => if (cond_ty.isSignedInt(mod)) @as(u64, @bitCast(value.toSignedInt(mod))) else value.toUnsignedInt(mod), |
| 4198 | 4653 | .Enum => blk: { |
| ... | ... | @@ -4213,28 +4668,65 @@ const DeclGen = struct { |
| 4213 | 4668 | } |
| 4214 | 4669 | } |
| 4215 | 4670 | |
| 4671 | var incoming_structured_blocks = std.ArrayListUnmanaged(ControlFlow.Structured.Block.Incoming){}; |
| 4672 | defer incoming_structured_blocks.deinit(self.gpa); |
| 4673 | |
| 4674 | if (self.control_flow == .structured) { |
| 4675 | try incoming_structured_blocks.ensureUnusedCapacity(self.gpa, num_cases + 1); |
| 4676 | } |
| 4677 | |
| 4216 | 4678 | // Now, finally, we can start emitting each of the cases. |
| 4217 | 4679 | var extra_index: usize = switch_br.end; |
| 4218 | | var case_i: u32 = 0; |
| 4219 | | while (case_i < num_cases) : (case_i += 1) { |
| 4680 | for (0..num_cases) |case_i| { |
| 4220 | 4681 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 4221 | 4682 | const items = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[case.end..][0..case.data.items_len])); |
| 4222 | 4683 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 4223 | 4684 | extra_index = case.end + case.data.items_len + case_body.len; |
| 4224 | 4685 | |
| 4225 | | const label = IdResult{ .id = first_case_label.id + case_i }; |
| 4686 | const label = IdResult{ .id = @intCast(first_case_label.id + case_i) }; |
| 4226 | 4687 | |
| 4227 | 4688 | try self.beginSpvBlock(label); |
| 4228 | | try self.genBody(case_body); |
| 4689 | |
| 4690 | switch (self.control_flow) { |
| 4691 | .structured => { |
| 4692 | const next_block = try self.genStructuredBody(.selection, case_body); |
| 4693 | incoming_structured_blocks.appendAssumeCapacity(.{ |
| 4694 | .src_label = self.current_block_label, |
| 4695 | .next_block = next_block, |
| 4696 | }); |
| 4697 | try self.func.body.emitBranch(self.spv.gpa, merge_label.?); |
| 4698 | }, |
| 4699 | .unstructured => { |
| 4700 | try self.genBody(case_body); |
| 4701 | }, |
| 4702 | } |
| 4229 | 4703 | } |
| 4230 | 4704 | |
| 4231 | 4705 | const else_body = self.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 4232 | 4706 | try self.beginSpvBlock(default); |
| 4233 | 4707 | if (else_body.len != 0) { |
| 4234 | | try self.genBody(else_body); |
| 4708 | switch (self.control_flow) { |
| 4709 | .structured => { |
| 4710 | const next_block = try self.genStructuredBody(.selection, else_body); |
| 4711 | incoming_structured_blocks.appendAssumeCapacity(.{ |
| 4712 | .src_label = self.current_block_label, |
| 4713 | .next_block = next_block, |
| 4714 | }); |
| 4715 | try self.func.body.emitBranch(self.spv.gpa, merge_label.?); |
| 4716 | }, |
| 4717 | .unstructured => { |
| 4718 | try self.genBody(else_body); |
| 4719 | }, |
| 4720 | } |
| 4235 | 4721 | } else { |
| 4236 | 4722 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 4237 | 4723 | } |
| 4724 | |
| 4725 | if (self.control_flow == .structured) { |
| 4726 | try self.beginSpvBlock(merge_label.?); |
| 4727 | const next_block = try self.structuredNextBlock(incoming_structured_blocks.items); |
| 4728 | try self.structuredBreak(next_block); |
| 4729 | } |
| 4238 | 4730 | } |
| 4239 | 4731 | |
| 4240 | 4732 | fn airUnreach(self: *DeclGen) !void { |