diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a3dc27a4801..79534aca66a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -553,11 +553,6 @@ set(ZIG_STAGE2_SOURCES src/codegen/c/Type.zig src/codegen/llvm.zig src/codegen/llvm/bindings.zig - src/codegen/spirv.zig - src/codegen/spirv/Assembler.zig - src/codegen/spirv/Module.zig - src/codegen/spirv/Section.zig - src/codegen/spirv/spec.zig src/crash_report.zig src/dev.zig src/libs/freebsd.zig @@ -620,11 +615,6 @@ set(ZIG_STAGE2_SOURCES src/link/Plan9.zig src/link/Plan9/aout.zig src/link/Queue.zig - src/link/SpirV.zig - src/link/SpirV/BinaryModule.zig - src/link/SpirV/deduplicate.zig - src/link/SpirV/lower_invocation_globals.zig - src/link/SpirV/prune_unused.zig src/link/StringTable.zig src/link/Wasm.zig src/link/Wasm/Archive.zig diff --git a/lib/std/Build/Watch.zig b/lib/std/Build/Watch.zig index cc8bd60c9a78..06c07d929b26 100644 --- a/lib/std/Build/Watch.zig +++ b/lib/std/Build/Watch.zig @@ -171,7 +171,13 @@ const Os = switch (builtin.os.tag) { const gop = try w.dir_table.getOrPut(gpa, path); if (!gop.found_existing) { var mount_id: MountId = undefined; - const dir_handle = try Os.getDirHandle(gpa, path, &mount_id); + const dir_handle = Os.getDirHandle(gpa, path, &mount_id) catch |err| switch (err) { + error.FileNotFound => { + std.debug.assert(w.dir_table.swapRemove(path)); + continue; + }, + else => return err, + }; const fan_fd = blk: { const fd_gop = try w.os.poll_fds.getOrPut(gpa, mount_id); if (!fd_gop.found_existing) { diff --git a/src/Zcu.zig b/src/Zcu.zig index c13f7aaac9be..228144bc9cd7 100644 --- a/src/Zcu.zig +++ b/src/Zcu.zig @@ -3646,9 +3646,8 @@ pub fn errorSetBits(zcu: *const Zcu) u16 { if (zcu.error_limit == 0) return 0; if (target.cpu.arch.isSpirV()) { - if (!target.cpu.has(.spirv, .storage_push_constant16)) { - return 32; - } + // As expected by https://github.com/Snektron/zig-spirv-test-executor + if (zcu.comp.config.is_test) return 32; } return @as(u16, std.math.log2_int(ErrorInt, zcu.error_limit)) + 1; diff --git a/src/Zcu/PerThread.zig b/src/Zcu/PerThread.zig index 119b742a89ea..fac09f2fc0c8 100644 --- a/src/Zcu/PerThread.zig +++ b/src/Zcu/PerThread.zig @@ -4398,13 +4398,10 @@ fn runCodegenInner(pt: Zcu.PerThread, func_index: InternPool.Index, air: *Air) e const lf = comp.bin_file orelse return error.NoLinkFile; - // TODO: self-hosted codegen should always have a type of MIR; codegen should produce that MIR, - // and the linker should consume it. However, our SPIR-V backend is currently tightly coupled - // with our SPIR-V linker, so needs to work more like the LLVM backend. This should be fixed to - // unblock threaded codegen for SPIR-V. + // Just like LLVM, the SPIR-V backend can't multi-threaded due to SPIR-V design limitations. if (lf.cast(.spirv)) |spirv_file| { assert(pt.tid == .main); // SPIR-V has a lot of shared state - spirv_file.object.updateFunc(pt, func_index, air, &liveness) catch |err| { + spirv_file.updateFunc(pt, func_index, air, &liveness) catch |err| { switch (err) { error.OutOfMemory => comp.link_diags.setAllocFailure(), } diff --git a/src/codegen.zig b/src/codegen.zig index 17e3c8504a7d..2e06bfb67660 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -57,7 +57,7 @@ fn importBackend(comptime backend: std.builtin.CompilerBackend) type { .stage2_powerpc => unreachable, .stage2_riscv64 => @import("arch/riscv64/CodeGen.zig"), .stage2_sparc64 => @import("arch/sparc64/CodeGen.zig"), - .stage2_spirv => @import("codegen/spirv.zig"), + .stage2_spirv => @import("codegen/spirv/CodeGen.zig"), .stage2_wasm => @import("arch/wasm/CodeGen.zig"), .stage2_x86, .stage2_x86_64 => @import("arch/x86_64/CodeGen.zig"), _ => unreachable, diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig deleted file mode 100644 index 17fa62d18f02..000000000000 --- a/src/codegen/spirv.zig +++ /dev/null @@ -1,6658 +0,0 @@ -const std = @import("std"); -const Allocator = std.mem.Allocator; -const Target = std.Target; -const log = std.log.scoped(.codegen); -const assert = std.debug.assert; -const Signedness = std.builtin.Signedness; - -const Zcu = @import("../Zcu.zig"); -const Decl = Zcu.Decl; -const Type = @import("../Type.zig"); -const Value = @import("../Value.zig"); -const Air = @import("../Air.zig"); -const InternPool = @import("../InternPool.zig"); - -const spec = @import("spirv/spec.zig"); -const Opcode = spec.Opcode; -const Word = spec.Word; -const Id = spec.Id; -const StorageClass = spec.StorageClass; - -const SpvModule = @import("spirv/Module.zig"); -const IdRange = SpvModule.IdRange; - -const SpvSection = @import("spirv/Section.zig"); -const SpvAssembler = @import("spirv/Assembler.zig"); - -const InstMap = std.AutoHashMapUnmanaged(Air.Inst.Index, Id); - -pub fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features { - return comptime &.initMany(&.{ - .expand_intcast_safe, - .expand_int_from_float_safe, - .expand_int_from_float_optimized_safe, - .expand_add_safe, - .expand_sub_safe, - .expand_mul_safe, - }); -} - -pub const zig_call_abi_ver = 3; -pub const big_int_bits = 32; - -const InternMap = std.AutoHashMapUnmanaged(struct { InternPool.Index, NavGen.Repr }, Id); -const PtrTypeMap = std.AutoHashMapUnmanaged( - struct { InternPool.Index, StorageClass, NavGen.Repr }, - struct { ty_id: Id, fwd_emitted: bool }, -); - -const ControlFlow = union(enum) { - const Structured = struct { - /// This type indicates the way that a block is terminated. The - /// state of a particular block is used to track how a jump from - /// inside the block must reach the outside. - const Block = union(enum) { - const Incoming = struct { - src_label: Id, - /// Instruction that returns an u32 value of the - /// `Air.Inst.Index` that control flow should jump to. - next_block: Id, - }; - - const SelectionMerge = struct { - /// Incoming block from the `then` label. - /// Note that hte incoming block from the `else` label is - /// either given by the next element in the stack. - incoming: Incoming, - /// The label id of the cond_br's merge block. - /// For the top-most element in the stack, this - /// value is undefined. - merge_block: Id, - }; - - /// For a `selection` type block, we cannot use early exits, and we - /// must generate a 'merge ladder' of OpSelection instructions. To that end, - /// we keep a stack of the merges that still must be closed at the end of - /// a block. - /// - /// This entire structure basically just resembles a tree like - /// a x - /// \ / - /// b o merge - /// \ / - /// c o merge - /// \ / - /// o merge - /// / - /// o jump to next block - selection: struct { - /// In order to know which merges we still need to do, we need to keep - /// a stack of those. - merge_stack: std.ArrayListUnmanaged(SelectionMerge) = .empty, - }, - /// For a `loop` type block, we can early-exit the block by - /// jumping to the loop exit node, and we don't need to generate - /// an entire stack of merges. - loop: struct { - /// The next block to jump to can be determined from any number - /// of conditions that jump to the loop exit. - merges: std.ArrayListUnmanaged(Incoming) = .empty, - /// The label id of the loop's merge block. - merge_block: Id, - }, - - fn deinit(self: *Structured.Block, a: Allocator) void { - switch (self.*) { - .selection => |*merge| merge.merge_stack.deinit(a), - .loop => |*merge| merge.merges.deinit(a), - } - self.* = undefined; - } - }; - /// The stack of (structured) blocks that we are currently in. This determines - /// how exits from the current block must be handled. - block_stack: std.ArrayListUnmanaged(*Structured.Block) = .empty, - /// Maps `block` inst indices to the variable that the block's result - /// value must be written to. - block_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, - }; - - const Unstructured = struct { - const Incoming = struct { - src_label: Id, - break_value_id: Id, - }; - - const Block = struct { - label: ?Id = null, - incoming_blocks: std.ArrayListUnmanaged(Incoming) = .empty, - }; - - /// We need to keep track of result ids for block labels, as well as the 'incoming' - /// blocks for a block. - blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, *Block) = .empty, - }; - - structured: Structured, - unstructured: Unstructured, - - pub fn deinit(self: *ControlFlow, a: Allocator) void { - switch (self.*) { - .structured => |*cf| { - cf.block_stack.deinit(a); - cf.block_results.deinit(a); - }, - .unstructured => |*cf| { - cf.blocks.deinit(a); - }, - } - self.* = undefined; - } -}; - -/// This structure holds information that is relevant to the entire compilation, -/// in contrast to `NavGen`, which only holds relevant information about a -/// single decl. -pub const Object = struct { - /// A general-purpose allocator that can be used for any allocation for this Object. - gpa: Allocator, - - /// the SPIR-V module that represents the final binary. - spv: SpvModule, - - /// The Zig module that this object file is generated for. - /// A map of Zig decl indices to SPIR-V decl indices. - nav_link: std.AutoHashMapUnmanaged(InternPool.Nav.Index, SpvModule.Decl.Index) = .empty, - - /// A map of Zig InternPool indices for anonymous decls to SPIR-V decl indices. - uav_link: std.AutoHashMapUnmanaged(struct { InternPool.Index, StorageClass }, SpvModule.Decl.Index) = .empty, - - /// A map that maps AIR intern pool indices to SPIR-V result-ids. - intern_map: InternMap = .empty, - - /// This map serves a dual purpose: - /// - It keeps track of pointers that are currently being emitted, so that we can tell - /// if they are recursive and need an OpTypeForwardPointer. - /// - It caches pointers by child-type. This is required because sometimes we rely on - /// ID-equality for pointers, and pointers constructed via `ptrType()` aren't interned - /// via the usual `intern_map` mechanism. - ptr_types: PtrTypeMap = .{}, - - /// For test declarations for Vulkan, we have to add a buffer. - /// We only need to generate this once, this holds the link information - /// related to that. - error_buffer: ?SpvModule.Decl.Index = null, - - pub fn init(gpa: Allocator, target: *const std.Target) Object { - return .{ - .gpa = gpa, - .spv = SpvModule.init(gpa, target), - }; - } - - pub fn deinit(self: *Object) void { - self.spv.deinit(); - self.nav_link.deinit(self.gpa); - self.uav_link.deinit(self.gpa); - self.intern_map.deinit(self.gpa); - self.ptr_types.deinit(self.gpa); - } - - fn genNav( - self: *Object, - pt: Zcu.PerThread, - nav_index: InternPool.Nav.Index, - air: Air, - liveness: Air.Liveness, - do_codegen: bool, - ) !void { - const zcu = pt.zcu; - const gpa = zcu.gpa; - const structured_cfg = zcu.navFileScope(nav_index).mod.?.structured_cfg; - - var nav_gen = NavGen{ - .gpa = gpa, - .object = self, - .pt = pt, - .spv = &self.spv, - .owner_nav = nav_index, - .air = air, - .liveness = liveness, - .intern_map = &self.intern_map, - .ptr_types = &self.ptr_types, - .control_flow = switch (structured_cfg) { - true => .{ .structured = .{} }, - false => .{ .unstructured = .{} }, - }, - .current_block_label = undefined, - .base_line = zcu.navSrcLine(nav_index), - }; - defer nav_gen.deinit(); - - nav_gen.genNav(do_codegen) catch |err| switch (err) { - error.CodegenFail => switch (zcu.codegenFailMsg(nav_index, nav_gen.error_msg.?)) { - error.CodegenFail => {}, - error.OutOfMemory => |e| return e, - }, - else => |other| { - // There might be an error that happened *after* self.error_msg - // was already allocated, so be sure to free it. - if (nav_gen.error_msg) |error_msg| { - error_msg.deinit(gpa); - } - - return other; - }, - }; - } - - pub fn updateFunc( - self: *Object, - pt: Zcu.PerThread, - func_index: InternPool.Index, - air: *const Air, - liveness: *const ?Air.Liveness, - ) !void { - const nav = pt.zcu.funcInfo(func_index).owner_nav; - // TODO: Separate types for generating decls and functions? - try self.genNav(pt, nav, air.*, liveness.*.?, true); - } - - pub fn updateNav( - self: *Object, - pt: Zcu.PerThread, - nav: InternPool.Nav.Index, - ) !void { - try self.genNav(pt, nav, undefined, undefined, false); - } - - /// Fetch or allocate a result id for nav index. This function also marks the nav as alive. - /// Note: Function does not actually generate the nav, it just allocates an index. - pub fn resolveNav(self: *Object, zcu: *Zcu, nav_index: InternPool.Nav.Index) !SpvModule.Decl.Index { - const ip = &zcu.intern_pool; - const entry = try self.nav_link.getOrPut(self.gpa, nav_index); - if (!entry.found_existing) { - const nav = ip.getNav(nav_index); - // TODO: Extern fn? - const kind: SpvModule.Decl.Kind = if (ip.isFunctionType(nav.typeOf(ip))) - .func - else switch (nav.getAddrspace()) { - .generic => .invocation_global, - else => .global, - }; - - entry.value_ptr.* = try self.spv.allocDecl(kind); - } - - return entry.value_ptr.*; - } -}; - -/// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. -const NavGen = struct { - /// A general-purpose allocator that can be used for any allocations for this NavGen. - gpa: Allocator, - - /// The object that this decl is generated into. - object: *Object, - - /// The Zig module that we are generating decls for. - pt: Zcu.PerThread, - - /// The SPIR-V module that instructions should be emitted into. - /// This is the same as `self.object.spv`, repeated here for brevity. - spv: *SpvModule, - - /// The decl we are currently generating code for. - owner_nav: InternPool.Nav.Index, - - /// The intermediate code of the declaration we are currently generating. Note: If - /// the declaration is not a function, this value will be undefined! - air: Air, - - /// The liveness analysis of the intermediate code for the declaration we are currently generating. - /// Note: If the declaration is not a function, this value will be undefined! - liveness: Air.Liveness, - - /// An array of function argument result-ids. Each index corresponds with the - /// function argument of the same index. - args: std.ArrayListUnmanaged(Id) = .empty, - - /// A counter to keep track of how many `arg` instructions we've seen yet. - next_arg_index: u32 = 0, - - /// A map keeping track of which instruction generated which result-id. - inst_results: InstMap = .empty, - - /// A map that maps AIR intern pool indices to SPIR-V result-ids. - /// See `Object.intern_map`. - intern_map: *InternMap, - - /// Module's pointer types, see `Object.ptr_types`. - ptr_types: *PtrTypeMap, - - /// This field keeps track of the current state wrt structured or unstructured control flow. - control_flow: ControlFlow, - - /// The label of the SPIR-V block we are currently generating. - current_block_label: Id, - - /// The code (prologue and body) for the function we are currently generating code for. - func: SpvModule.Fn = .{}, - - /// The base offset of the current decl, which is what `dbg_stmt` is relative to. - base_line: u32, - - /// If `gen` returned `Error.CodegenFail`, this contains an explanatory message. - /// Memory is owned by `module.gpa`. - error_msg: ?*Zcu.ErrorMsg = null, - - /// Possible errors the `genDecl` function may return. - const Error = error{ CodegenFail, OutOfMemory }; - - /// This structure is used to return information about a type typically used for - /// arithmetic operations. These types may either be integers, floats, or a vector - /// of these. If the type is a scalar, 'inner type' refers to the - /// scalar type. Otherwise, if its a vector, it refers to the vector's element type. - const ArithmeticTypeInfo = struct { - /// A classification of the inner type. - const Class = enum { - /// A boolean. - bool, - - /// A regular, **native**, integer. - /// This is only returned when the backend supports this int as a native type (when - /// the relevant capability is enabled). - integer, - - /// A regular float. These are all required to be natively supported. Floating points - /// for which the relevant capability is not enabled are not emulated. - float, - - /// An integer of a 'strange' size (which' bit size is not the same as its backing - /// type. **Note**: this may **also** include power-of-2 integers for which the - /// relevant capability is not enabled), but still within the limits of the largest - /// natively supported integer type. - strange_integer, - - /// An integer with more bits than the largest natively supported integer type. - composite_integer, - }; - - /// The number of bits in the inner type. - /// This is the actual number of bits of the type, not the size of the backing integer. - bits: u16, - - /// The number of bits required to store the type. - /// For `integer` and `float`, this is equal to `bits`. - /// For `strange_integer` and `bool` this is the size of the backing integer. - /// For `composite_integer` this is the elements count. - backing_bits: u16, - - /// Null if this type is a scalar, or the length - /// of the vector otherwise. - vector_len: ?u32, - - /// Whether the inner type is signed. Only relevant for integers. - signedness: std.builtin.Signedness, - - /// A classification of the inner type. These scenarios - /// will all have to be handled slightly different. - class: Class, - }; - - /// Data can be lowered into in two basic representations: indirect, which is when - /// a type is stored in memory, and direct, which is how a type is stored when its - /// a direct SPIR-V value. - const Repr = enum { - /// A SPIR-V value as it would be used in operations. - direct, - /// A SPIR-V value as it is stored in memory. - indirect, - }; - - /// Free resources owned by the NavGen. - pub fn deinit(self: *NavGen) void { - self.args.deinit(self.gpa); - self.inst_results.deinit(self.gpa); - self.control_flow.deinit(self.gpa); - self.func.deinit(self.gpa); - } - - pub fn fail(self: *NavGen, comptime format: []const u8, args: anytype) Error { - @branchHint(.cold); - const zcu = self.pt.zcu; - const src_loc = zcu.navSrcLoc(self.owner_nav); - assert(self.error_msg == null); - self.error_msg = try Zcu.ErrorMsg.create(zcu.gpa, src_loc, format, args); - return error.CodegenFail; - } - - pub fn todo(self: *NavGen, comptime format: []const u8, args: anytype) Error { - return self.fail("TODO (SPIR-V): " ++ format, args); - } - - /// This imports the "default" extended instruction set for the target - /// For OpenCL, OpenCL.std.100. For Vulkan and OpenGL, GLSL.std.450. - fn importExtendedSet(self: *NavGen) !Id { - const target = self.spv.target; - return switch (target.os.tag) { - .opencl, .amdhsa => try self.spv.importInstructionSet(.open_cl_std), - .vulkan, .opengl => try self.spv.importInstructionSet(.glsl_std_450), - else => unreachable, - }; - } - - /// Fetch the result-id for a previously generated instruction or constant. - fn resolve(self: *NavGen, inst: Air.Inst.Ref) !Id { - const pt = self.pt; - const zcu = pt.zcu; - if (try self.air.value(inst, pt)) |val| { - const ty = self.typeOf(inst); - if (ty.zigTypeTag(zcu) == .@"fn") { - const fn_nav = switch (zcu.intern_pool.indexToKey(val.ip_index)) { - .@"extern" => |@"extern"| @"extern".owner_nav, - .func => |func| func.owner_nav, - else => unreachable, - }; - const spv_decl_index = try self.object.resolveNav(zcu, fn_nav); - try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {}); - return self.spv.declPtr(spv_decl_index).result_id; - } - - return try self.constant(ty, val, .direct); - } - const index = inst.toIndex().?; - return self.inst_results.get(index).?; // Assertion means instruction does not dominate usage. - } - - fn resolveUav(self: *NavGen, val: InternPool.Index) !Id { - // TODO: This cannot be a function at this point, but it should probably be handled anyway. - - const zcu = self.pt.zcu; - const ty = Type.fromInterned(zcu.intern_pool.typeOf(val)); - const decl_ptr_ty_id = try self.ptrType(ty, self.spvStorageClass(.generic), .indirect); - - const spv_decl_index = blk: { - const entry = try self.object.uav_link.getOrPut(self.object.gpa, .{ val, .function }); - if (entry.found_existing) { - try self.addFunctionDep(entry.value_ptr.*, .function); - - const result_id = self.spv.declPtr(entry.value_ptr.*).result_id; - return try self.castToGeneric(decl_ptr_ty_id, result_id); - } - - const spv_decl_index = try self.spv.allocDecl(.invocation_global); - try self.addFunctionDep(spv_decl_index, .function); - entry.value_ptr.* = spv_decl_index; - break :blk spv_decl_index; - }; - - // TODO: At some point we will be able to generate this all constant here, but then all of - // constant() will need to be implemented such that it doesn't generate any at-runtime code. - // NOTE: Because this is a global, we really only want to initialize it once. Therefore the - // constant lowering of this value will need to be deferred to an initializer similar to - // other globals. - - const result_id = self.spv.declPtr(spv_decl_index).result_id; - - { - // Save the current state so that we can temporarily generate into a different function. - // TODO: This should probably be made a little more robust. - const func = self.func; - defer self.func = func; - const block_label = self.current_block_label; - defer self.current_block_label = block_label; - - self.func = .{}; - defer self.func.deinit(self.gpa); - - const initializer_proto_ty_id = try self.functionType(Type.void, &.{}); - - const initializer_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ - .id_result_type = try self.resolveType(Type.void, .direct), - .id_result = initializer_id, - .function_control = .{}, - .function_type = initializer_proto_ty_id, - }); - const root_block_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ - .id_result = root_block_id, - }); - self.current_block_label = root_block_id; - - const val_id = try self.constant(ty, Value.fromInterned(val), .indirect); - try self.func.body.emit(self.spv.gpa, .OpStore, .{ - .pointer = result_id, - .object = val_id, - }); - - try self.func.body.emit(self.spv.gpa, .OpReturn, {}); - try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); - try self.spv.addFunction(spv_decl_index, self.func); - - try self.spv.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)}); - - const fn_decl_ptr_ty_id = try self.ptrType(ty, .function, .indirect); - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = fn_decl_ptr_ty_id, - .id_result = result_id, - .set = try self.spv.importInstructionSet(.zig), - .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... - .id_ref_4 = &.{initializer_id}, - }); - } - - return try self.castToGeneric(decl_ptr_ty_id, result_id); - } - - fn addFunctionDep(self: *NavGen, decl_index: SpvModule.Decl.Index, storage_class: StorageClass) !void { - if (self.spv.version.minor < 4) { - // Before version 1.4, the interface’s storage classes are limited to the Input and Output - if (storage_class == .input or storage_class == .output) { - try self.func.decl_deps.put(self.spv.gpa, decl_index, {}); - } - } else { - try self.func.decl_deps.put(self.spv.gpa, decl_index, {}); - } - } - - fn castToGeneric(self: *NavGen, type_id: Id, ptr_id: Id) !Id { - if (self.spv.hasFeature(.generic_pointer)) { - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{ - .id_result_type = type_id, - .id_result = result_id, - .pointer = ptr_id, - }); - return result_id; - } - - return ptr_id; - } - - /// Start a new SPIR-V block, Emits the label of the new block, and stores which - /// block we are currently generating. - /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to - /// keep track of the previous block. - fn beginSpvBlock(self: *NavGen, label: Id) !void { - try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label }); - self.current_block_label = label; - } - - /// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need - /// to emulate them in other instructions/types. This function returns, given an integer bit width (signed or unsigned, sign - /// included), the width of the underlying type which represents it, given the enabled features for the current target. - /// If the result is `null`, the largest type the target platform supports natively is not able to perform computations using - /// that size. In this case, multiple elements of the largest type should be used. - /// The backing type will be chosen as the smallest supported integer larger or equal to it in number of bits. - /// The result is valid to be used with OpTypeInt. - /// TODO: Should the result of this function be cached? - fn backingIntBits(self: *NavGen, bits: u16) struct { u16, bool } { - // The backend will never be asked to compiler a 0-bit integer, so we won't have to handle those in this function. - assert(bits != 0); - - if (self.spv.hasFeature(.arbitrary_precision_integers) and bits <= 32) { - return .{ bits, false }; - } - - // We require Int8 and Int16 capabilities and benefit Int64 when available. - // 32-bit integers are always supported (see spec, 2.16.1, Data rules). - const ints = [_]struct { bits: u16, enabled: bool }{ - .{ .bits = 8, .enabled = true }, - .{ .bits = 16, .enabled = true }, - .{ .bits = 32, .enabled = true }, - .{ - .bits = 64, - .enabled = self.spv.hasFeature(.int64) or self.spv.target.cpu.arch == .spirv64, - }, - }; - - for (ints) |int| { - if (bits <= int.bits and int.enabled) return .{ int.bits, false }; - } - - // Big int - return .{ std.mem.alignForward(u16, bits, big_int_bits), true }; - } - - /// Return the amount of bits in the largest supported integer type. This is either 32 (always supported), or 64 (if - /// the Int64 capability is enabled). - /// Note: The extension SPV_INTEL_arbitrary_precision_integers allows any integer size (at least up to 32 bits). - /// In theory that could also be used, but since the spec says that it only guarantees support up to 32-bit ints there - /// is no way of knowing whether those are actually supported. - /// TODO: Maybe this should be cached? - fn largestSupportedIntBits(self: *NavGen) u16 { - if (self.spv.hasFeature(.int64) or self.spv.target.cpu.arch == .spirv64) { - return 64; - } - return 32; - } - - fn arithmeticTypeInfo(self: *NavGen, ty: Type) ArithmeticTypeInfo { - const zcu = self.pt.zcu; - const target = self.spv.target; - var scalar_ty = ty.scalarType(zcu); - if (scalar_ty.zigTypeTag(zcu) == .@"enum") { - scalar_ty = scalar_ty.intTagType(zcu); - } - const vector_len = if (ty.isVector(zcu)) ty.vectorLen(zcu) else null; - return switch (scalar_ty.zigTypeTag(zcu)) { - .bool => .{ - .bits = 1, // Doesn't matter for this class. - .backing_bits = self.backingIntBits(1).@"0", - .vector_len = vector_len, - .signedness = .unsigned, // Technically, but doesn't matter for this class. - .class = .bool, - }, - .float => .{ - .bits = scalar_ty.floatBits(target), - .backing_bits = scalar_ty.floatBits(target), // TODO: F80? - .vector_len = vector_len, - .signedness = .signed, // Technically, but doesn't matter for this class. - .class = .float, - }, - .int => blk: { - const int_info = scalar_ty.intInfo(zcu); - // TODO: Maybe it's useful to also return this value. - const backing_bits, const big_int = self.backingIntBits(int_info.bits); - break :blk .{ - .bits = int_info.bits, - .backing_bits = backing_bits, - .vector_len = vector_len, - .signedness = int_info.signedness, - .class = class: { - if (big_int) break :class .composite_integer; - break :class if (backing_bits == int_info.bits) .integer else .strange_integer; - }, - }; - }, - .@"enum" => unreachable, - .vector => unreachable, - else => unreachable, // Unhandled arithmetic type - }; - } - - /// Checks whether the type can be directly translated to SPIR-V vectors - fn isSpvVector(self: *NavGen, ty: Type) bool { - const zcu = self.pt.zcu; - if (ty.zigTypeTag(zcu) != .vector) return false; - - // TODO: This check must be expanded for types that can be represented - // as integers (enums / packed structs?) and types that are represented - // by multiple SPIR-V values. - const scalar_ty = ty.scalarType(zcu); - switch (scalar_ty.zigTypeTag(zcu)) { - .bool, - .int, - .float, - => {}, - else => return false, - } - - const elem_ty = ty.childType(zcu); - const len = ty.vectorLen(zcu); - - if (elem_ty.isNumeric(zcu) or elem_ty.toIntern() == .bool_type) { - if (len > 1 and len <= 4) return true; - if (self.spv.hasFeature(.vector16)) return (len == 8 or len == 16); - } - - return false; - } - - /// Emits a bool constant in a particular representation. - fn constBool(self: *NavGen, value: bool, repr: Repr) !Id { - return switch (repr) { - .indirect => self.constInt(Type.u1, @intFromBool(value)), - .direct => self.spv.constBool(value), - }; - } - - /// Emits an integer constant. - /// This function, unlike SpvModule.constInt, takes care to bitcast - /// the value to an unsigned int first for Kernels. - fn constInt(self: *NavGen, ty: Type, value: anytype) !Id { - const zcu = self.pt.zcu; - const scalar_ty = ty.scalarType(zcu); - const int_info = scalar_ty.intInfo(zcu); - // Use backing bits so that negatives are sign extended - const backing_bits, const big_int = self.backingIntBits(int_info.bits); - assert(backing_bits != 0); // u0 is comptime - - const result_ty_id = try self.resolveType(scalar_ty, .indirect); - const signedness: Signedness = switch (@typeInfo(@TypeOf(value))) { - .int => |int| int.signedness, - .comptime_int => if (value < 0) .signed else .unsigned, - else => unreachable, - }; - if (@sizeOf(@TypeOf(value)) >= 4 and big_int) { - const value64: u64 = switch (signedness) { - .signed => @bitCast(@as(i64, @intCast(value))), - .unsigned => @as(u64, @intCast(value)), - }; - assert(backing_bits == 64); - return self.constructComposite(result_ty_id, &.{ - try self.constInt(.u32, @as(u32, @truncate(value64))), - try self.constInt(.u32, @as(u32, @truncate(value64 << 32))), - }); - } - - const final_value: spec.LiteralContextDependentNumber = switch (self.spv.target.os.tag) { - .opencl, .amdhsa => blk: { - const value64: u64 = switch (signedness) { - .signed => @bitCast(@as(i64, @intCast(value))), - .unsigned => @as(u64, @intCast(value)), - }; - - // Manually truncate the value to the right amount of bits. - const truncated_value = if (backing_bits == 64) - value64 - else - value64 & (@as(u64, 1) << @intCast(backing_bits)) - 1; - - break :blk switch (backing_bits) { - 1...32 => .{ .uint32 = @truncate(truncated_value) }, - 33...64 => .{ .uint64 = truncated_value }, - else => unreachable, - }; - }, - else => switch (backing_bits) { - 1...32 => if (signedness == .signed) .{ .int32 = @intCast(value) } else .{ .uint32 = @intCast(value) }, - 33...64 => if (signedness == .signed) .{ .int64 = value } else .{ .uint64 = value }, - else => unreachable, - }, - }; - - const result_id = try self.spv.constant(result_ty_id, final_value); - - if (!ty.isVector(zcu)) return result_id; - return self.constructCompositeSplat(ty, result_id); - } - - pub fn constructComposite(self: *NavGen, result_ty_id: Id, constituents: []const Id) !Id { - const result_id = self.spv.allocId(); - try self.func.body.emit(self.gpa, .OpCompositeConstruct, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .constituents = constituents, - }); - return result_id; - } - - /// Construct a composite at runtime with all lanes set to the same value. - /// ty must be an aggregate type. - fn constructCompositeSplat(self: *NavGen, ty: Type, constituent: Id) !Id { - const zcu = self.pt.zcu; - const n: usize = @intCast(ty.arrayLen(zcu)); - - const constituents = try self.gpa.alloc(Id, n); - defer self.gpa.free(constituents); - @memset(constituents, constituent); - - const result_ty_id = try self.resolveType(ty, .direct); - return self.constructComposite(result_ty_id, constituents); - } - - /// This function generates a load for a constant in direct (ie, non-memory) representation. - /// When the constant is simple, it can be generated directly using OpConstant instructions. - /// When the constant is more complicated however, it needs to be constructed using multiple values. This - /// is done by emitting a sequence of instructions that initialize the value. - // - /// This function should only be called during function code generation. - fn constant(self: *NavGen, ty: Type, val: Value, repr: Repr) !Id { - // Note: Using intern_map can only be used with constants that DO NOT generate any runtime code!! - // Ideally that should be all constants in the future, or it should be cleaned up somehow. For - // now, only use the intern_map on case-by-case basis by breaking to :cache. - if (self.intern_map.get(.{ val.toIntern(), repr })) |id| { - return id; - } - - const pt = self.pt; - const zcu = pt.zcu; - const target = self.spv.target; - const result_ty_id = try self.resolveType(ty, repr); - const ip = &zcu.intern_pool; - - log.debug("lowering constant: ty = {f}, val = {f}, key = {s}", .{ ty.fmt(pt), val.fmtValue(pt), @tagName(ip.indexToKey(val.toIntern())) }); - if (val.isUndefDeep(zcu)) { - return self.spv.constUndef(result_ty_id); - } - - const cacheable_id = cache: { - switch (ip.indexToKey(val.toIntern())) { - .int_type, - .ptr_type, - .array_type, - .vector_type, - .opt_type, - .anyframe_type, - .error_union_type, - .simple_type, - .struct_type, - .tuple_type, - .union_type, - .opaque_type, - .enum_type, - .func_type, - .error_set_type, - .inferred_error_set_type, - => unreachable, // types, not values - - .undef => unreachable, // handled above - - .variable, - .@"extern", - .func, - .enum_literal, - .empty_enum_value, - => unreachable, // non-runtime values - - .simple_value => |simple_value| switch (simple_value) { - .undefined, - .void, - .null, - .empty_tuple, - .@"unreachable", - => unreachable, // non-runtime values - - .false, .true => break :cache try self.constBool(val.toBool(), repr), - }, - .int => { - if (ty.isSignedInt(zcu)) { - break :cache try self.constInt(ty, val.toSignedInt(zcu)); - } else { - break :cache try self.constInt(ty, val.toUnsignedInt(zcu)); - } - }, - .float => { - const lit: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { - 16 => .{ .uint32 = @as(u16, @bitCast(val.toFloat(f16, zcu))) }, - 32 => .{ .float32 = val.toFloat(f32, zcu) }, - 64 => .{ .float64 = val.toFloat(f64, zcu) }, - 80, 128 => unreachable, // TODO - else => unreachable, - }; - break :cache try self.spv.constant(result_ty_id, lit); - }, - .err => |err| { - const value = try pt.getErrorValue(err.name); - break :cache try self.constInt(ty, value); - }, - .error_union => |error_union| { - // TODO: Error unions may be constructed with constant instructions if the payload type - // allows it. For now, just generate it here regardless. - const err_int_ty = try pt.errorIntType(); - const err_ty = switch (error_union.val) { - .err_name => ty.errorUnionSet(zcu), - .payload => err_int_ty, - }; - const err_val = switch (error_union.val) { - .err_name => |err_name| Value.fromInterned(try pt.intern(.{ .err = .{ - .ty = ty.errorUnionSet(zcu).toIntern(), - .name = err_name, - } })), - .payload => try pt.intValue(err_int_ty, 0), - }; - const payload_ty = ty.errorUnionPayload(zcu); - const eu_layout = self.errorUnionLayout(payload_ty); - if (!eu_layout.payload_has_bits) { - // We use the error type directly as the type. - break :cache try self.constant(err_ty, err_val, .indirect); - } - - const payload_val = Value.fromInterned(switch (error_union.val) { - .err_name => try pt.intern(.{ .undef = payload_ty.toIntern() }), - .payload => |payload| payload, - }); - - var constituents: [2]Id = undefined; - var types: [2]Type = undefined; - if (eu_layout.error_first) { - constituents[0] = try self.constant(err_ty, err_val, .indirect); - constituents[1] = try self.constant(payload_ty, payload_val, .indirect); - types = .{ err_ty, payload_ty }; - } else { - constituents[0] = try self.constant(payload_ty, payload_val, .indirect); - constituents[1] = try self.constant(err_ty, err_val, .indirect); - types = .{ payload_ty, err_ty }; - } - - const comp_ty_id = try self.resolveType(ty, .direct); - return try self.constructComposite(comp_ty_id, &constituents); - }, - .enum_tag => { - const int_val = try val.intFromEnum(ty, pt); - const int_ty = ty.intTagType(zcu); - break :cache try self.constant(int_ty, int_val, repr); - }, - .ptr => return self.constantPtr(val), - .slice => |slice| { - const ptr_id = try self.constantPtr(Value.fromInterned(slice.ptr)); - const len_id = try self.constant(Type.usize, Value.fromInterned(slice.len), .indirect); - const comp_ty_id = try self.resolveType(ty, .direct); - return try self.constructComposite(comp_ty_id, &.{ ptr_id, len_id }); - }, - .opt => { - const payload_ty = ty.optionalChild(zcu); - const maybe_payload_val = val.optionalValue(zcu); - - if (!payload_ty.hasRuntimeBits(zcu)) { - break :cache try self.constBool(maybe_payload_val != null, .indirect); - } else if (ty.optionalReprIsPayload(zcu)) { - // Optional representation is a nullable pointer or slice. - if (maybe_payload_val) |payload_val| { - return try self.constant(payload_ty, payload_val, .indirect); - } else { - break :cache try self.spv.constNull(result_ty_id); - } - } - - // Optional representation is a structure. - // { Payload, Bool } - - const has_pl_id = try self.constBool(maybe_payload_val != null, .indirect); - const payload_id = if (maybe_payload_val) |payload_val| - try self.constant(payload_ty, payload_val, .indirect) - else - try self.spv.constUndef(try self.resolveType(payload_ty, .indirect)); - - const comp_ty_id = try self.resolveType(ty, .direct); - return try self.constructComposite(comp_ty_id, &.{ payload_id, has_pl_id }); - }, - .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) { - inline .array_type, .vector_type => |array_type, tag| { - const elem_ty = Type.fromInterned(array_type.child); - - const constituents = try self.gpa.alloc(Id, @intCast(ty.arrayLenIncludingSentinel(zcu))); - defer self.gpa.free(constituents); - - const child_repr: Repr = switch (tag) { - .array_type => .indirect, - .vector_type => .direct, - else => unreachable, - }; - - switch (aggregate.storage) { - .bytes => |bytes| { - // TODO: This is really space inefficient, perhaps there is a better - // way to do it? - for (constituents, bytes.toSlice(constituents.len, ip)) |*constituent, byte| { - constituent.* = try self.constInt(elem_ty, byte); - } - }, - .elems => |elems| { - for (constituents, elems) |*constituent, elem| { - constituent.* = try self.constant(elem_ty, Value.fromInterned(elem), child_repr); - } - }, - .repeated_elem => |elem| { - @memset(constituents, try self.constant(elem_ty, Value.fromInterned(elem), child_repr)); - }, - } - - const comp_ty_id = try self.resolveType(ty, .direct); - return self.constructComposite(comp_ty_id, constituents); - }, - .struct_type => { - const struct_type = zcu.typeToStruct(ty).?; - - if (struct_type.layout == .@"packed") { - // TODO: composite int - // TODO: endianness - const bits: u16 = @intCast(ty.bitSize(zcu)); - const bytes = std.mem.alignForward(u16, self.backingIntBits(bits).@"0", 8) / 8; - var limbs: [8]u8 = undefined; - @memset(&limbs, 0); - val.writeToPackedMemory(ty, pt, limbs[0..bytes], 0) catch unreachable; - const backing_ty = Type.fromInterned(struct_type.backingIntTypeUnordered(ip)); - return try self.constInt(backing_ty, @as(u64, @bitCast(limbs))); - } - - var types = std.ArrayList(Type).init(self.gpa); - defer types.deinit(); - - var constituents = std.ArrayList(Id).init(self.gpa); - defer constituents.deinit(); - - var it = struct_type.iterateRuntimeOrder(ip); - while (it.next()) |field_index| { - const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); - if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - // This is a zero-bit field - we only needed it for the alignment. - continue; - } - - // TODO: Padding? - const field_val = try val.fieldValue(pt, field_index); - const field_id = try self.constant(field_ty, field_val, .indirect); - - try types.append(field_ty); - try constituents.append(field_id); - } - - const comp_ty_id = try self.resolveType(ty, .direct); - return try self.constructComposite(comp_ty_id, constituents.items); - }, - .tuple_type => return self.todo("implement tuple types", .{}), - else => unreachable, - }, - .un => |un| { - if (un.tag == .none) { - assert(ty.containerLayout(zcu) == .@"packed"); // TODO - const int_ty = try pt.intType(.unsigned, @intCast(ty.bitSize(zcu))); - return try self.constant(int_ty, Value.fromInterned(un.val), .direct); - } - const active_field = ty.unionTagFieldIndex(Value.fromInterned(un.tag), zcu).?; - const union_obj = zcu.typeToUnion(ty).?; - const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[active_field]); - const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(zcu)) - try self.constant(field_ty, Value.fromInterned(un.val), .direct) - else - null; - return try self.unionInit(ty, active_field, payload); - }, - .memoized_call => unreachable, - } - }; - - try self.intern_map.putNoClobber(self.gpa, .{ val.toIntern(), repr }, cacheable_id); - - return cacheable_id; - } - - fn constantPtr(self: *NavGen, ptr_val: Value) Error!Id { - const pt = self.pt; - - if (ptr_val.isUndef(pt.zcu)) { - const result_ty = ptr_val.typeOf(pt.zcu); - const result_ty_id = try self.resolveType(result_ty, .direct); - return self.spv.constUndef(result_ty_id); - } - - var arena = std.heap.ArenaAllocator.init(self.gpa); - defer arena.deinit(); - - const derivation = try ptr_val.pointerDerivation(arena.allocator(), pt); - return self.derivePtr(derivation); - } - - fn derivePtr(self: *NavGen, derivation: Value.PointerDeriveStep) Error!Id { - const pt = self.pt; - const zcu = pt.zcu; - switch (derivation) { - .comptime_alloc_ptr, .comptime_field_ptr => unreachable, - .int => |int| { - const result_ty_id = try self.resolveType(int.ptr_ty, .direct); - // TODO: This can probably be an OpSpecConstantOp Bitcast, but - // that is not implemented by Mesa yet. Therefore, just generate it - // as a runtime operation. - const result_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ - .id_result_type = result_ty_id, - .id_result = result_ptr_id, - .integer_value = try self.constant(Type.usize, try pt.intValue(Type.usize, int.addr), .direct), - }); - return result_ptr_id; - }, - .nav_ptr => |nav| { - const result_ptr_ty = try pt.navPtrType(nav); - return self.constantNavRef(result_ptr_ty, nav); - }, - .uav_ptr => |uav| { - const result_ptr_ty = Type.fromInterned(uav.orig_ty); - return self.constantUavRef(result_ptr_ty, uav); - }, - .eu_payload_ptr => @panic("TODO"), - .opt_payload_ptr => @panic("TODO"), - .field_ptr => |field| { - const parent_ptr_id = try self.derivePtr(field.parent.*); - const parent_ptr_ty = try field.parent.ptrType(pt); - return self.structFieldPtr(field.result_ptr_ty, parent_ptr_ty, parent_ptr_id, field.field_idx); - }, - .elem_ptr => |elem| { - const parent_ptr_id = try self.derivePtr(elem.parent.*); - const parent_ptr_ty = try elem.parent.ptrType(pt); - const index_id = try self.constInt(Type.usize, elem.elem_idx); - return self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id); - }, - .offset_and_cast => |oac| { - const parent_ptr_id = try self.derivePtr(oac.parent.*); - const parent_ptr_ty = try oac.parent.ptrType(pt); - const result_ty_id = try self.resolveType(oac.new_ptr_ty, .direct); - const child_size = oac.new_ptr_ty.childType(zcu).abiSize(zcu); - - if (parent_ptr_ty.childType(zcu).isVector(zcu) and oac.byte_offset % child_size == 0) { - // Vector element ptr accesses are derived as offset_and_cast. - // We can just use OpAccessChain. - return self.accessChain( - result_ty_id, - parent_ptr_id, - &.{@intCast(@divExact(oac.byte_offset, child_size))}, - ); - } - - if (oac.byte_offset == 0) { - // Allow changing the pointer type child only to restructure arrays. - // e.g. [3][2]T to T is fine, as is [2]T -> [2][1]T. - const result_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = result_ty_id, - .id_result = result_ptr_id, - .operand = parent_ptr_id, - }); - return result_ptr_id; - } - - return self.fail("cannot perform pointer cast: '{f}' to '{f}'", .{ - parent_ptr_ty.fmt(pt), - oac.new_ptr_ty.fmt(pt), - }); - }, - } - } - - fn constantUavRef( - self: *NavGen, - ty: Type, - uav: InternPool.Key.Ptr.BaseAddr.Uav, - ) !Id { - // TODO: Merge this function with constantDeclRef. - - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const ty_id = try self.resolveType(ty, .direct); - const uav_ty = Type.fromInterned(ip.typeOf(uav.val)); - - switch (ip.indexToKey(uav.val)) { - .func => unreachable, // TODO - .@"extern" => assert(!ip.isFunctionType(uav_ty.toIntern())), - else => {}, - } - - // const is_fn_body = decl_ty.zigTypeTag(zcu) == .@"fn"; - if (!uav_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { - // Pointer to nothing - return undefined - return self.spv.constUndef(ty_id); - } - - // Uav refs are always generic. - assert(ty.ptrAddressSpace(zcu) == .generic); - const decl_ptr_ty_id = try self.ptrType(uav_ty, .generic, .indirect); - const ptr_id = try self.resolveUav(uav.val); - - if (decl_ptr_ty_id != ty_id) { - // Differing pointer types, insert a cast. - const casted_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = ty_id, - .id_result = casted_ptr_id, - .operand = ptr_id, - }); - return casted_ptr_id; - } else { - return ptr_id; - } - } - - fn constantNavRef(self: *NavGen, ty: Type, nav_index: InternPool.Nav.Index) !Id { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const ty_id = try self.resolveType(ty, .direct); - const nav = ip.getNav(nav_index); - const nav_ty: Type = .fromInterned(nav.typeOf(ip)); - - switch (nav.status) { - .unresolved => unreachable, - .type_resolved => {}, // this is not a function or extern - .fully_resolved => |r| switch (ip.indexToKey(r.val)) { - .func => { - // TODO: Properly lower function pointers. For now we are going to hack around it and - // just generate an empty pointer. Function pointers are represented by a pointer to usize. - return try self.spv.constUndef(ty_id); - }, - .@"extern" => if (ip.isFunctionType(nav_ty.toIntern())) @panic("TODO"), - else => {}, - }, - } - - if (!nav_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { - // Pointer to nothing - return undefined. - return self.spv.constUndef(ty_id); - } - - const spv_decl_index = try self.object.resolveNav(zcu, nav_index); - const spv_decl = self.spv.declPtr(spv_decl_index); - - const decl_id = switch (spv_decl.kind) { - .func => unreachable, // TODO: Is this possible? - .global, .invocation_global => spv_decl.result_id, - }; - - const storage_class = self.spvStorageClass(nav.getAddrspace()); - try self.addFunctionDep(spv_decl_index, storage_class); - - const decl_ptr_ty_id = try self.ptrType(nav_ty, storage_class, .indirect); - - const ptr_id = switch (storage_class) { - .generic => try self.castToGeneric(decl_ptr_ty_id, decl_id), - else => decl_id, - }; - - if (decl_ptr_ty_id != ty_id) { - // Differing pointer types, insert a cast. - const casted_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = ty_id, - .id_result = casted_ptr_id, - .operand = ptr_id, - }); - return casted_ptr_id; - } else { - return ptr_id; - } - } - - // Turn a Zig type's name into a cache reference. - fn resolveTypeName(self: *NavGen, ty: Type) ![]const u8 { - var aw: std.io.Writer.Allocating = .init(self.gpa); - defer aw.deinit(); - ty.print(&aw.writer, self.pt) catch |err| switch (err) { - error.WriteFailed => return error.OutOfMemory, - }; - return try aw.toOwnedSlice(); - } - - /// Create an integer type suitable for storing at least 'bits' bits. - /// The integer type that is returned by this function is the type that is used to perform - /// actual operations (as well as store) a Zig type of a particular number of bits. To create - /// a type with an exact size, use SpvModule.intType. - fn intType(self: *NavGen, signedness: std.builtin.Signedness, bits: u16) !Id { - const backing_bits, const big_int = self.backingIntBits(bits); - if (big_int) { - if (backing_bits > 64) { - return self.fail("composite integers larger than 64bit aren't supported", .{}); - } - const int_ty = try self.resolveType(.u32, .direct); - return self.arrayType(backing_bits / big_int_bits, int_ty); - } - - return switch (self.spv.target.os.tag) { - // Kernel only supports unsigned ints. - .opencl, .amdhsa => return self.spv.intType(.unsigned, backing_bits), - else => self.spv.intType(signedness, backing_bits), - }; - } - - fn arrayType(self: *NavGen, len: u32, child_ty: Id) !Id { - const len_id = try self.constInt(Type.u32, len); - return self.spv.arrayType(len_id, child_ty); - } - - fn ptrType(self: *NavGen, child_ty: Type, storage_class: StorageClass, child_repr: Repr) !Id { - const zcu = self.pt.zcu; - const ip = &zcu.intern_pool; - const key = .{ child_ty.toIntern(), storage_class, child_repr }; - const entry = try self.ptr_types.getOrPut(self.gpa, key); - if (entry.found_existing) { - const fwd_id = entry.value_ptr.ty_id; - if (!entry.value_ptr.fwd_emitted) { - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypeForwardPointer, .{ - .pointer_type = fwd_id, - .storage_class = storage_class, - }); - entry.value_ptr.fwd_emitted = true; - } - return fwd_id; - } - - const result_id = self.spv.allocId(); - entry.value_ptr.* = .{ - .ty_id = result_id, - .fwd_emitted = false, - }; - - const child_ty_id = try self.resolveType(child_ty, child_repr); - - switch (self.spv.target.os.tag) { - .vulkan, .opengl => { - if (child_ty.zigTypeTag(zcu) == .@"struct") { - switch (storage_class) { - .uniform, .push_constant => try self.spv.decorate(child_ty_id, .block), - else => {}, - } - } - - switch (ip.indexToKey(child_ty.toIntern())) { - .func_type, .opaque_type => {}, - else => { - try self.spv.decorate(result_id, .{ .array_stride = .{ .array_stride = @intCast(child_ty.abiSize(zcu)) } }); - }, - } - }, - else => {}, - } - - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{ - .id_result = result_id, - .storage_class = storage_class, - .type = child_ty_id, - }); - - self.ptr_types.getPtr(key).?.fwd_emitted = true; - - return result_id; - } - - fn functionType(self: *NavGen, return_ty: Type, param_types: []const Type) !Id { - const return_ty_id = try self.resolveFnReturnType(return_ty); - const param_ids = try self.gpa.alloc(Id, param_types.len); - defer self.gpa.free(param_ids); - - for (param_types, param_ids) |param_ty, *param_id| { - param_id.* = try self.resolveType(param_ty, .direct); - } - - return self.spv.functionType(return_ty_id, param_ids); - } - - /// Generate a union type. Union types are always generated with the - /// most aligned field active. If the tag alignment is greater - /// than that of the payload, a regular union (non-packed, with both tag and - /// payload), will be generated as follows: - /// struct { - /// tag: TagType, - /// payload: MostAlignedFieldType, - /// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8, - /// padding: [padding_size]u8, - /// } - /// If the payload alignment is greater than that of the tag: - /// struct { - /// payload: MostAlignedFieldType, - /// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8, - /// tag: TagType, - /// padding: [padding_size]u8, - /// } - /// If any of the fields' size is 0, it will be omitted. - fn resolveUnionType(self: *NavGen, ty: Type) !Id { - const zcu = self.pt.zcu; - const ip = &zcu.intern_pool; - const union_obj = zcu.typeToUnion(ty).?; - - if (union_obj.flagsUnordered(ip).layout == .@"packed") { - return try self.intType(.unsigned, @intCast(ty.bitSize(zcu))); - } - - const layout = self.unionLayout(ty); - if (!layout.has_payload) { - // No payload, so represent this as just the tag type. - return try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect); - } - - var member_types: [4]Id = undefined; - var member_names: [4][]const u8 = undefined; - - const u8_ty_id = try self.resolveType(Type.u8, .direct); - - if (layout.tag_size != 0) { - const tag_ty_id = try self.resolveType(Type.fromInterned(union_obj.enum_tag_ty), .indirect); - member_types[layout.tag_index] = tag_ty_id; - member_names[layout.tag_index] = "(tag)"; - } - - if (layout.payload_size != 0) { - const payload_ty_id = try self.resolveType(layout.payload_ty, .indirect); - member_types[layout.payload_index] = payload_ty_id; - member_names[layout.payload_index] = "(payload)"; - } - - if (layout.payload_padding_size != 0) { - const payload_padding_ty_id = try self.arrayType(@intCast(layout.payload_padding_size), u8_ty_id); - member_types[layout.payload_padding_index] = payload_padding_ty_id; - member_names[layout.payload_padding_index] = "(payload padding)"; - } - - if (layout.padding_size != 0) { - const padding_ty_id = try self.arrayType(@intCast(layout.padding_size), u8_ty_id); - member_types[layout.padding_index] = padding_ty_id; - member_names[layout.padding_index] = "(padding)"; - } - - const result_id = self.spv.allocId(); - try self.spv.structType(result_id, member_types[0..layout.total_fields], member_names[0..layout.total_fields]); - - const type_name = try self.resolveTypeName(ty); - defer self.gpa.free(type_name); - try self.spv.debugName(result_id, type_name); - - return result_id; - } - - fn resolveFnReturnType(self: *NavGen, ret_ty: Type) !Id { - const zcu = self.pt.zcu; - if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - // If the return type is an error set or an error union, then we make this - // anyerror return type instead, so that it can be coerced into a function - // pointer type which has anyerror as the return type. - if (ret_ty.isError(zcu)) { - return self.resolveType(Type.anyerror, .direct); - } else { - return self.resolveType(Type.void, .direct); - } - } - - return try self.resolveType(ret_ty, .direct); - } - - /// Turn a Zig type into a SPIR-V Type, and return a reference to it. - fn resolveType(self: *NavGen, ty: Type, repr: Repr) Error!Id { - if (self.intern_map.get(.{ ty.toIntern(), repr })) |id| { - return id; - } - - const id = try self.resolveTypeInner(ty, repr); - try self.intern_map.put(self.gpa, .{ ty.toIntern(), repr }, id); - return id; - } - - fn resolveTypeInner(self: *NavGen, ty: Type, repr: Repr) Error!Id { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - log.debug("resolveType: ty = {f}", .{ty.fmt(pt)}); - const target = self.spv.target; - - const section = &self.spv.sections.types_globals_constants; - - switch (ty.zigTypeTag(zcu)) { - .noreturn => { - assert(repr == .direct); - return try self.spv.voidType(); - }, - .void => switch (repr) { - .direct => { - return try self.spv.voidType(); - }, - // Pointers to void - .indirect => { - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeOpaque, .{ - .id_result = result_id, - .literal_string = "void", - }); - return result_id; - }, - }, - .bool => switch (repr) { - .direct => return try self.spv.boolType(), - .indirect => return try self.resolveType(Type.u1, .indirect), - }, - .int => { - const int_info = ty.intInfo(zcu); - if (int_info.bits == 0) { - // Some times, the backend will be asked to generate a pointer to i0. OpTypeInt - // with 0 bits is invalid, so return an opaque type in this case. - assert(repr == .indirect); - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeOpaque, .{ - .id_result = result_id, - .literal_string = "u0", - }); - return result_id; - } - return try self.intType(int_info.signedness, int_info.bits); - }, - .@"enum" => { - const tag_ty = ty.intTagType(zcu); - return try self.resolveType(tag_ty, repr); - }, - .float => { - // We can (and want) not really emulate floating points with other floating point types like with the integer types, - // so if the float is not supported, just return an error. - const bits = ty.floatBits(target); - const supported = switch (bits) { - 16 => self.spv.hasFeature(.float16), - // 32-bit floats are always supported (see spec, 2.16.1, Data rules). - 32 => true, - 64 => self.spv.hasFeature(.float64), - else => false, - }; - - if (!supported) { - return self.fail("Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); - } - - return try self.spv.floatType(bits); - }, - .array => { - const elem_ty = ty.childType(zcu); - const elem_ty_id = try self.resolveType(elem_ty, .indirect); - const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel(zcu)) orelse { - return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(zcu)}); - }; - - if (!elem_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - // The size of the array would be 0, but that is not allowed in SPIR-V. - // This path can be reached when the backend is asked to generate a pointer to - // an array of some zero-bit type. This should always be an indirect path. - assert(repr == .indirect); - - // We cannot use the child type here, so just use an opaque type. - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeOpaque, .{ - .id_result = result_id, - .literal_string = "zero-sized array", - }); - return result_id; - } else if (total_len == 0) { - // The size of the array would be 0, but that is not allowed in SPIR-V. - // This path can be reached for example when there is a slicing of a pointer - // that produces a zero-length array. In all cases where this type can be generated, - // this should be an indirect path. - assert(repr == .indirect); - - // In this case, we have an array of a non-zero sized type. In this case, - // generate an array of 1 element instead, so that ptr_elem_ptr instructions - // can be lowered to ptrAccessChain instead of manually performing the math. - return try self.arrayType(1, elem_ty_id); - } else { - const result_id = try self.arrayType(total_len, elem_ty_id); - switch (self.spv.target.os.tag) { - .vulkan, .opengl => { - try self.spv.decorate(result_id, .{ .array_stride = .{ - .array_stride = @intCast(elem_ty.abiSize(zcu)), - } }); - }, - else => {}, - } - return result_id; - } - }, - .vector => { - const elem_ty = ty.childType(zcu); - const elem_ty_id = try self.resolveType(elem_ty, repr); - const len = ty.vectorLen(zcu); - - if (self.isSpvVector(ty)) { - return try self.spv.vectorType(len, elem_ty_id); - } else { - return try self.arrayType(len, elem_ty_id); - } - }, - .@"fn" => switch (repr) { - .direct => { - const fn_info = zcu.typeToFunc(ty).?; - - comptime assert(zig_call_abi_ver == 3); - switch (fn_info.cc) { - .auto, - .spirv_kernel, - .spirv_fragment, - .spirv_vertex, - .spirv_device, - => {}, - else => unreachable, - } - - // Guaranteed by callConvSupportsVarArgs, there are no SPIR-V CCs which support - // varargs. - assert(!fn_info.is_var_args); - - // Note: Logic is different from functionType(). - const param_ty_ids = try self.gpa.alloc(Id, fn_info.param_types.len); - defer self.gpa.free(param_ty_ids); - var param_index: usize = 0; - for (fn_info.param_types.get(ip)) |param_ty_index| { - const param_ty = Type.fromInterned(param_ty_index); - if (!param_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; - - param_ty_ids[param_index] = try self.resolveType(param_ty, .direct); - param_index += 1; - } - - const return_ty_id = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type)); - - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeFunction, .{ - .id_result = result_id, - .return_type = return_ty_id, - .id_ref_2 = param_ty_ids[0..param_index], - }); - - return result_id; - }, - .indirect => { - // TODO: Represent function pointers properly. - // For now, just use an usize type. - return try self.resolveType(Type.usize, .indirect); - }, - }, - .pointer => { - const ptr_info = ty.ptrInfo(zcu); - - const child_ty = Type.fromInterned(ptr_info.child); - const storage_class = self.spvStorageClass(ptr_info.flags.address_space); - const ptr_ty_id = try self.ptrType(child_ty, storage_class, .indirect); - - if (ptr_info.flags.size != .slice) { - return ptr_ty_id; - } - - const size_ty_id = try self.resolveType(Type.usize, .direct); - const result_id = self.spv.allocId(); - try self.spv.structType( - result_id, - &.{ ptr_ty_id, size_ty_id }, - &.{ "ptr", "len" }, - ); - return result_id; - }, - .@"struct" => { - const struct_type = switch (ip.indexToKey(ty.toIntern())) { - .tuple_type => |tuple| { - const member_types = try self.gpa.alloc(Id, tuple.values.len); - defer self.gpa.free(member_types); - - var member_index: usize = 0; - for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, field_val| { - if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(zcu)) continue; - - member_types[member_index] = try self.resolveType(Type.fromInterned(field_ty), .indirect); - member_index += 1; - } - - const result_id = self.spv.allocId(); - try self.spv.structType(result_id, member_types[0..member_index], null); - - const type_name = try self.resolveTypeName(ty); - defer self.gpa.free(type_name); - try self.spv.debugName(result_id, type_name); - - return result_id; - }, - .struct_type => ip.loadStructType(ty.toIntern()), - else => unreachable, - }; - - if (struct_type.layout == .@"packed") { - return try self.resolveType(Type.fromInterned(struct_type.backingIntTypeUnordered(ip)), .direct); - } - - var member_types = std.ArrayList(Id).init(self.gpa); - defer member_types.deinit(); - - var member_names = std.ArrayList([]const u8).init(self.gpa); - defer member_names.deinit(); - - var index: u32 = 0; - var it = struct_type.iterateRuntimeOrder(ip); - const result_id = self.spv.allocId(); - while (it.next()) |field_index| { - const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); - if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - // This is a zero-bit field - we only needed it for the alignment. - continue; - } - - switch (self.spv.target.os.tag) { - .vulkan, .opengl => { - try self.spv.decorateMember(result_id, index, .{ .offset = .{ - .byte_offset = @intCast(ty.structFieldOffset(field_index, zcu)), - } }); - }, - else => {}, - } - - const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse - try ip.getOrPutStringFmt(zcu.gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls); - try member_types.append(try self.resolveType(field_ty, .indirect)); - try member_names.append(field_name.toSlice(ip)); - - index += 1; - } - - try self.spv.structType(result_id, member_types.items, member_names.items); - - const type_name = try self.resolveTypeName(ty); - defer self.gpa.free(type_name); - try self.spv.debugName(result_id, type_name); - - return result_id; - }, - .optional => { - const payload_ty = ty.optionalChild(zcu); - if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - // Just use a bool. - // Note: Always generate the bool with indirect format, to save on some sanity - // Perform the conversion to a direct bool when the field is extracted. - return try self.resolveType(Type.bool, .indirect); - } - - const payload_ty_id = try self.resolveType(payload_ty, .indirect); - if (ty.optionalReprIsPayload(zcu)) { - // Optional is actually a pointer or a slice. - return payload_ty_id; - } - - const bool_ty_id = try self.resolveType(Type.bool, .indirect); - - const result_id = self.spv.allocId(); - try self.spv.structType( - result_id, - &.{ payload_ty_id, bool_ty_id }, - &.{ "payload", "valid" }, - ); - return result_id; - }, - .@"union" => return try self.resolveUnionType(ty), - .error_set => { - const err_int_ty = try pt.errorIntType(); - return try self.resolveType(err_int_ty, repr); - }, - .error_union => { - const payload_ty = ty.errorUnionPayload(zcu); - const error_ty_id = try self.resolveType(Type.anyerror, .indirect); - - const eu_layout = self.errorUnionLayout(payload_ty); - if (!eu_layout.payload_has_bits) { - return error_ty_id; - } - - const payload_ty_id = try self.resolveType(payload_ty, .indirect); - - var member_types: [2]Id = undefined; - var member_names: [2][]const u8 = undefined; - if (eu_layout.error_first) { - // Put the error first - member_types = .{ error_ty_id, payload_ty_id }; - member_names = .{ "error", "payload" }; - // TODO: ABI padding? - } else { - // Put the payload first. - member_types = .{ payload_ty_id, error_ty_id }; - member_names = .{ "payload", "error" }; - // TODO: ABI padding? - } - - const result_id = self.spv.allocId(); - try self.spv.structType(result_id, &member_types, &member_names); - return result_id; - }, - .@"opaque" => { - const type_name = try self.resolveTypeName(ty); - defer self.gpa.free(type_name); - - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeOpaque, .{ - .id_result = result_id, - .literal_string = type_name, - }); - return result_id; - }, - - .null, - .undefined, - .enum_literal, - .comptime_float, - .comptime_int, - .type, - => unreachable, // Must be comptime. - - .frame, .@"anyframe" => unreachable, // TODO - } - } - - fn spvStorageClass(self: *NavGen, as: std.builtin.AddressSpace) StorageClass { - return switch (as) { - .generic => if (self.spv.hasFeature(.generic_pointer)) .generic else .function, - .global => switch (self.spv.target.os.tag) { - .opencl, .amdhsa => .cross_workgroup, - else => .storage_buffer, - }, - .push_constant => { - return .push_constant; - }, - .output => { - return .output; - }, - .uniform => { - return .uniform; - }, - .storage_buffer => { - return .storage_buffer; - }, - .physical_storage_buffer => { - return .physical_storage_buffer; - }, - .constant => .uniform_constant, - .shared => .workgroup, - .local => .function, - .input => .input, - .gs, - .fs, - .ss, - .param, - .flash, - .flash1, - .flash2, - .flash3, - .flash4, - .flash5, - .cog, - .lut, - .hub, - => unreachable, - }; - } - - const ErrorUnionLayout = struct { - payload_has_bits: bool, - error_first: bool, - - fn errorFieldIndex(self: @This()) u32 { - assert(self.payload_has_bits); - return if (self.error_first) 0 else 1; - } - - fn payloadFieldIndex(self: @This()) u32 { - assert(self.payload_has_bits); - return if (self.error_first) 1 else 0; - } - }; - - fn errorUnionLayout(self: *NavGen, payload_ty: Type) ErrorUnionLayout { - const pt = self.pt; - const zcu = pt.zcu; - - const error_align = Type.anyerror.abiAlignment(zcu); - const payload_align = payload_ty.abiAlignment(zcu); - - const error_first = error_align.compare(.gt, payload_align); - return .{ - .payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu), - .error_first = error_first, - }; - } - - const UnionLayout = struct { - /// If false, this union is represented - /// by only an integer of the tag type. - has_payload: bool, - tag_size: u32, - tag_index: u32, - /// Note: This is the size of the payload type itself, NOT the size of the ENTIRE payload. - /// Use `has_payload` instead!! - payload_ty: Type, - payload_size: u32, - payload_index: u32, - payload_padding_size: u32, - payload_padding_index: u32, - padding_size: u32, - padding_index: u32, - total_fields: u32, - }; - - fn unionLayout(self: *NavGen, ty: Type) UnionLayout { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const layout = ty.unionGetLayout(zcu); - const union_obj = zcu.typeToUnion(ty).?; - - var union_layout = UnionLayout{ - .has_payload = layout.payload_size != 0, - .tag_size = @intCast(layout.tag_size), - .tag_index = undefined, - .payload_ty = undefined, - .payload_size = undefined, - .payload_index = undefined, - .payload_padding_size = undefined, - .payload_padding_index = undefined, - .padding_size = @intCast(layout.padding), - .padding_index = undefined, - .total_fields = undefined, - }; - - if (union_layout.has_payload) { - const most_aligned_field = layout.most_aligned_field; - const most_aligned_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[most_aligned_field]); - union_layout.payload_ty = most_aligned_field_ty; - union_layout.payload_size = @intCast(most_aligned_field_ty.abiSize(zcu)); - } else { - union_layout.payload_size = 0; - } - - union_layout.payload_padding_size = @intCast(layout.payload_size - union_layout.payload_size); - - const tag_first = layout.tag_align.compare(.gte, layout.payload_align); - var field_index: u32 = 0; - - if (union_layout.tag_size != 0 and tag_first) { - union_layout.tag_index = field_index; - field_index += 1; - } - - if (union_layout.payload_size != 0) { - union_layout.payload_index = field_index; - field_index += 1; - } - - if (union_layout.payload_padding_size != 0) { - union_layout.payload_padding_index = field_index; - field_index += 1; - } - - if (union_layout.tag_size != 0 and !tag_first) { - union_layout.tag_index = field_index; - field_index += 1; - } - - if (union_layout.padding_size != 0) { - union_layout.padding_index = field_index; - field_index += 1; - } - - union_layout.total_fields = field_index; - - return union_layout; - } - - /// This structure represents a "temporary" value: Something we are currently - /// operating on. It typically lives no longer than the function that - /// implements a particular AIR operation. These are used to easier - /// implement vectorizable operations (see Vectorization and the build* - /// functions), and typically are only used for vectors of primitive types. - const Temporary = struct { - /// The type of the temporary. This is here mainly - /// for easier bookkeeping. Because we will never really - /// store Temporaries, they only cause extra stack space, - /// therefore no real storage is wasted. - ty: Type, - /// The value that this temporary holds. This is not necessarily - /// a value that is actually usable, or a single value: It is virtual - /// until materialize() is called, at which point is turned into - /// the usual SPIR-V representation of `self.ty`. - value: Temporary.Value, - - const Value = union(enum) { - singleton: Id, - exploded_vector: IdRange, - }; - - fn init(ty: Type, singleton: Id) Temporary { - return .{ .ty = ty, .value = .{ .singleton = singleton } }; - } - - fn materialize(self: Temporary, ng: *NavGen) !Id { - const zcu = ng.pt.zcu; - switch (self.value) { - .singleton => |id| return id, - .exploded_vector => |range| { - assert(self.ty.isVector(zcu)); - assert(self.ty.vectorLen(zcu) == range.len); - const constituents = try ng.gpa.alloc(Id, range.len); - defer ng.gpa.free(constituents); - for (constituents, 0..range.len) |*id, i| { - id.* = range.at(i); - } - const result_ty_id = try ng.resolveType(self.ty, .direct); - return ng.constructComposite(result_ty_id, constituents); - }, - } - } - - fn vectorization(self: Temporary, ng: *NavGen) Vectorization { - return Vectorization.fromType(self.ty, ng); - } - - fn pun(self: Temporary, new_ty: Type) Temporary { - return .{ - .ty = new_ty, - .value = self.value, - }; - } - - /// 'Explode' a temporary into separate elements. This turns a vector - /// into a bag of elements. - fn explode(self: Temporary, ng: *NavGen) !IdRange { - const zcu = ng.pt.zcu; - - // If the value is a scalar, then this is a no-op. - if (!self.ty.isVector(zcu)) { - return switch (self.value) { - .singleton => |id| .{ .base = @intFromEnum(id), .len = 1 }, - .exploded_vector => |range| range, - }; - } - - const ty_id = try ng.resolveType(self.ty.scalarType(zcu), .direct); - const n = self.ty.vectorLen(zcu); - const results = ng.spv.allocIds(n); - - const id = switch (self.value) { - .singleton => |id| id, - .exploded_vector => |range| return range, - }; - - for (0..n) |i| { - const indexes = [_]u32{@intCast(i)}; - try ng.func.body.emit(ng.spv.gpa, .OpCompositeExtract, .{ - .id_result_type = ty_id, - .id_result = results.at(i), - .composite = id, - .indexes = &indexes, - }); - } - - return results; - } - }; - - /// Initialize a `Temporary` from an AIR value. - fn temporary(self: *NavGen, inst: Air.Inst.Ref) !Temporary { - return .{ - .ty = self.typeOf(inst), - .value = .{ .singleton = try self.resolve(inst) }, - }; - } - - /// This union describes how a particular operation should be vectorized. - /// That depends on the operation and number of components of the inputs. - const Vectorization = union(enum) { - /// This is an operation between scalars. - scalar, - /// This operation is unrolled into separate operations. - /// Inputs may still be SPIR-V vectors, for example, - /// when the operation can't be vectorized in SPIR-V. - /// Value is number of components. - unrolled: u32, - - /// Derive a vectorization from a particular type - fn fromType(ty: Type, ng: *NavGen) Vectorization { - const zcu = ng.pt.zcu; - if (!ty.isVector(zcu)) return .scalar; - return .{ .unrolled = ty.vectorLen(zcu) }; - } - - /// Given two vectorization methods, compute a "unification": a fallback - /// that works for both, according to the following rules: - /// - Scalars may broadcast - /// - SPIR-V vectorized operations will unroll - /// - Prefer scalar > unrolled - fn unify(a: Vectorization, b: Vectorization) Vectorization { - if (a == .scalar and b == .scalar) return .scalar; - if (a == .unrolled or b == .unrolled) { - if (a == .unrolled and b == .unrolled) assert(a.components() == b.components()); - if (a == .unrolled) return .{ .unrolled = a.components() }; - return .{ .unrolled = b.components() }; - } - unreachable; - } - - /// Query the number of components that inputs of this operation have. - /// Note: for broadcasting scalars, this returns the number of elements - /// that the broadcasted vector would have. - fn components(self: Vectorization) u32 { - return switch (self) { - .scalar => 1, - .unrolled => |n| n, - }; - } - - /// Turns `ty` into the result-type of the entire operation. - /// `ty` may be a scalar or vector, it doesn't matter. - fn resultType(self: Vectorization, ng: *NavGen, ty: Type) !Type { - const pt = ng.pt; - const scalar_ty = ty.scalarType(pt.zcu); - return switch (self) { - .scalar => scalar_ty, - .unrolled => |n| try pt.vectorType(.{ .len = n, .child = scalar_ty.toIntern() }), - }; - } - - /// Before a temporary can be used, some setup may need to be one. This function implements - /// this setup, and returns a new type that holds the relevant information on how to access - /// elements of the input. - fn prepare(self: Vectorization, ng: *NavGen, tmp: Temporary) !PreparedOperand { - const pt = ng.pt; - const is_vector = tmp.ty.isVector(pt.zcu); - const value: PreparedOperand.Value = switch (tmp.value) { - .singleton => |id| switch (self) { - .scalar => blk: { - assert(!is_vector); - break :blk .{ .scalar = id }; - }, - .unrolled => blk: { - if (is_vector) break :blk .{ .vector_exploded = try tmp.explode(ng) }; - break :blk .{ .scalar_broadcast = id }; - }, - }, - .exploded_vector => |range| switch (self) { - .scalar => unreachable, - .unrolled => |n| blk: { - assert(range.len == n); - break :blk .{ .vector_exploded = range }; - }, - }, - }; - - return .{ - .ty = tmp.ty, - .value = value, - }; - } - - /// Finalize the results of an operation back into a temporary. `results` is - /// a list of result-ids of the operation. - fn finalize(self: Vectorization, ty: Type, results: IdRange) Temporary { - assert(self.components() == results.len); - return .{ - .ty = ty, - .value = switch (self) { - .scalar => .{ .singleton = results.at(0) }, - .unrolled => .{ .exploded_vector = results }, - }, - }; - } - - /// This struct represents an operand that has gone through some setup, and is - /// ready to be used as part of an operation. - const PreparedOperand = struct { - ty: Type, - value: PreparedOperand.Value, - - /// The types of value that a prepared operand can hold internally. Depends - /// on the operation and input value. - const Value = union(enum) { - /// A single scalar value that is used by a scalar operation. - scalar: Id, - /// A single scalar that is broadcasted in an unrolled operation. - scalar_broadcast: Id, - /// A vector represented by a consecutive list of IDs that is used in an unrolled operation. - vector_exploded: IdRange, - }; - - /// Query the value at a particular index of the operation. Note that - /// the index is *not* the component/lane, but the index of the *operation*. - fn at(self: PreparedOperand, i: usize) Id { - switch (self.value) { - .scalar => |id| { - assert(i == 0); - return id; - }, - .scalar_broadcast => |id| return id, - .vector_exploded => |range| return range.at(i), - } - } - }; - }; - - /// A utility function to compute the vectorization style of - /// a list of values. These values may be any of the following: - /// - A `Vectorization` instance - /// - A Type, in which case the vectorization is computed via `Vectorization.fromType`. - /// - A Temporary, in which case the vectorization is computed via `Temporary.vectorization`. - fn vectorization(self: *NavGen, args: anytype) Vectorization { - var v: Vectorization = undefined; - assert(args.len >= 1); - inline for (args, 0..) |arg, i| { - const iv: Vectorization = switch (@TypeOf(arg)) { - Vectorization => arg, - Type => Vectorization.fromType(arg, self), - Temporary => arg.vectorization(self), - else => @compileError("invalid type"), - }; - if (i == 0) { - v = iv; - } else { - v = v.unify(iv); - } - } - return v; - } - - /// This function builds an OpSConvert of OpUConvert depending on the - /// signedness of the types. - fn buildConvert(self: *NavGen, dst_ty: Type, src: Temporary) !Temporary { - const zcu = self.pt.zcu; - - const dst_ty_id = try self.resolveType(dst_ty.scalarType(zcu), .direct); - const src_ty_id = try self.resolveType(src.ty.scalarType(zcu), .direct); - - const v = self.vectorization(.{ dst_ty, src }); - const result_ty = try v.resultType(self, dst_ty); - - // We can directly compare integers, because those type-IDs are cached. - if (dst_ty_id == src_ty_id) { - // Nothing to do, type-pun to the right value. - // Note, Caller guarantees that the types fit (or caller will normalize after), - // so we don't have to normalize here. - // Note, dst_ty may be a scalar type even if we expect a vector, so we have to - // convert to the right type here. - return src.pun(result_ty); - } - - const ops = v.components(); - const results = self.spv.allocIds(ops); - - const op_result_ty = dst_ty.scalarType(zcu); - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - - const opcode: Opcode = blk: { - if (dst_ty.scalarType(zcu).isAnyFloat()) break :blk .OpFConvert; - if (dst_ty.scalarType(zcu).isSignedInt(zcu)) break :blk .OpSConvert; - break :blk .OpUConvert; - }; - - const op_src = try v.prepare(self, src); - - for (0..ops) |i| { - try self.func.body.emitRaw(self.spv.gpa, opcode, 3); - self.func.body.writeOperand(spec.Id, op_result_ty_id); - self.func.body.writeOperand(Id, results.at(i)); - self.func.body.writeOperand(Id, op_src.at(i)); - } - - return v.finalize(result_ty, results); - } - - fn buildFma(self: *NavGen, a: Temporary, b: Temporary, c: Temporary) !Temporary { - const zcu = self.pt.zcu; - const target = self.spv.target; - - const v = self.vectorization(.{ a, b, c }); - const ops = v.components(); - const results = self.spv.allocIds(ops); - - const op_result_ty = a.ty.scalarType(zcu); - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - const result_ty = try v.resultType(self, a.ty); - - const op_a = try v.prepare(self, a); - const op_b = try v.prepare(self, b); - const op_c = try v.prepare(self, c); - - const set = try self.importExtendedSet(); - - // TODO: Put these numbers in some definition - const instruction: u32 = switch (target.os.tag) { - .opencl => 26, // fma - // NOTE: Vulkan's FMA instruction does *NOT* produce the right values! - // its precision guarantees do NOT match zigs and it does NOT match OpenCLs! - // it needs to be emulated! - .vulkan, .opengl => return self.todo("implement fma operation for {s} os", .{@tagName(target.os.tag)}), - else => unreachable, - }; - - for (0..ops) |i| { - try self.func.body.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = op_result_ty_id, - .id_result = results.at(i), - .set = set, - .instruction = .{ .inst = instruction }, - .id_ref_4 = &.{ op_a.at(i), op_b.at(i), op_c.at(i) }, - }); - } - - return v.finalize(result_ty, results); - } - - fn buildSelect(self: *NavGen, condition: Temporary, lhs: Temporary, rhs: Temporary) !Temporary { - const zcu = self.pt.zcu; - - const v = self.vectorization(.{ condition, lhs, rhs }); - const ops = v.components(); - const results = self.spv.allocIds(ops); - - const op_result_ty = lhs.ty.scalarType(zcu); - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - const result_ty = try v.resultType(self, lhs.ty); - - assert(condition.ty.scalarType(zcu).zigTypeTag(zcu) == .bool); - - const cond = try v.prepare(self, condition); - const object_1 = try v.prepare(self, lhs); - const object_2 = try v.prepare(self, rhs); - - for (0..ops) |i| { - try self.func.body.emit(self.spv.gpa, .OpSelect, .{ - .id_result_type = op_result_ty_id, - .id_result = results.at(i), - .condition = cond.at(i), - .object_1 = object_1.at(i), - .object_2 = object_2.at(i), - }); - } - - return v.finalize(result_ty, results); - } - - const CmpPredicate = enum { - l_eq, - l_ne, - i_ne, - i_eq, - s_lt, - s_gt, - s_le, - s_ge, - u_lt, - u_gt, - u_le, - u_ge, - f_oeq, - f_une, - f_olt, - f_ole, - f_ogt, - f_oge, - }; - - fn buildCmp(self: *NavGen, pred: CmpPredicate, lhs: Temporary, rhs: Temporary) !Temporary { - const v = self.vectorization(.{ lhs, rhs }); - const ops = v.components(); - const results = self.spv.allocIds(ops); - - const op_result_ty: Type = .bool; - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - const result_ty = try v.resultType(self, Type.bool); - - const op_lhs = try v.prepare(self, lhs); - const op_rhs = try v.prepare(self, rhs); - - const opcode: Opcode = switch (pred) { - .l_eq => .OpLogicalEqual, - .l_ne => .OpLogicalNotEqual, - .i_eq => .OpIEqual, - .i_ne => .OpINotEqual, - .s_lt => .OpSLessThan, - .s_gt => .OpSGreaterThan, - .s_le => .OpSLessThanEqual, - .s_ge => .OpSGreaterThanEqual, - .u_lt => .OpULessThan, - .u_gt => .OpUGreaterThan, - .u_le => .OpULessThanEqual, - .u_ge => .OpUGreaterThanEqual, - .f_oeq => .OpFOrdEqual, - .f_une => .OpFUnordNotEqual, - .f_olt => .OpFOrdLessThan, - .f_ole => .OpFOrdLessThanEqual, - .f_ogt => .OpFOrdGreaterThan, - .f_oge => .OpFOrdGreaterThanEqual, - }; - - for (0..ops) |i| { - try self.func.body.emitRaw(self.spv.gpa, opcode, 4); - self.func.body.writeOperand(spec.Id, op_result_ty_id); - self.func.body.writeOperand(Id, results.at(i)); - self.func.body.writeOperand(Id, op_lhs.at(i)); - self.func.body.writeOperand(Id, op_rhs.at(i)); - } - - return v.finalize(result_ty, results); - } - - const UnaryOp = enum { - l_not, - bit_not, - i_neg, - f_neg, - i_abs, - f_abs, - clz, - ctz, - floor, - ceil, - trunc, - round, - sqrt, - sin, - cos, - tan, - exp, - exp2, - log, - log2, - log10, - }; - - fn buildUnary(self: *NavGen, op: UnaryOp, operand: Temporary) !Temporary { - const zcu = self.pt.zcu; - const target = self.spv.target; - const v = self.vectorization(.{operand}); - const ops = v.components(); - const results = self.spv.allocIds(ops); - const op_result_ty = operand.ty.scalarType(zcu); - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - const result_ty = try v.resultType(self, operand.ty); - - const op_operand = try v.prepare(self, operand); - - if (switch (op) { - .l_not => .OpLogicalNot, - .bit_not => .OpNot, - .i_neg => .OpSNegate, - .f_neg => .OpFNegate, - else => @as(?Opcode, null), - }) |opcode| { - for (0..ops) |i| { - try self.func.body.emitRaw(self.spv.gpa, opcode, 3); - self.func.body.writeOperand(spec.Id, op_result_ty_id); - self.func.body.writeOperand(Id, results.at(i)); - self.func.body.writeOperand(Id, op_operand.at(i)); - } - } else { - const set = try self.importExtendedSet(); - const extinst: u32 = switch (target.os.tag) { - .opencl => switch (op) { - .i_abs => 141, // s_abs - .f_abs => 23, // fabs - .clz => 151, // clz - .ctz => 152, // ctz - .floor => 25, // floor - .ceil => 12, // ceil - .trunc => 66, // trunc - .round => 55, // round - .sqrt => 61, // sqrt - .sin => 57, // sin - .cos => 14, // cos - .tan => 62, // tan - .exp => 19, // exp - .exp2 => 20, // exp2 - .log => 37, // log - .log2 => 38, // log2 - .log10 => 39, // log10 - else => unreachable, - }, - // Note: We'll need to check these for floating point accuracy - // Vulkan does not put tight requirements on these, for correction - // we might want to emulate them at some point. - .vulkan, .opengl => switch (op) { - .i_abs => 5, // SAbs - .f_abs => 4, // FAbs - .floor => 8, // Floor - .ceil => 9, // Ceil - .trunc => 3, // Trunc - .round => 1, // Round - .clz, - .ctz, - .sqrt, - .sin, - .cos, - .tan, - .exp, - .exp2, - .log, - .log2, - .log10, - => return self.todo("implement unary operation '{s}' for {s} os", .{ @tagName(op), @tagName(target.os.tag) }), - else => unreachable, - }, - else => unreachable, - }; - - for (0..ops) |i| { - try self.func.body.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = op_result_ty_id, - .id_result = results.at(i), - .set = set, - .instruction = .{ .inst = extinst }, - .id_ref_4 = &.{op_operand.at(i)}, - }); - } - } - - return v.finalize(result_ty, results); - } - - const BinaryOp = enum { - i_add, - f_add, - i_sub, - f_sub, - i_mul, - f_mul, - s_div, - u_div, - f_div, - s_rem, - f_rem, - s_mod, - u_mod, - f_mod, - srl, - sra, - sll, - bit_and, - bit_or, - bit_xor, - f_max, - s_max, - u_max, - f_min, - s_min, - u_min, - l_and, - l_or, - }; - - fn buildBinary(self: *NavGen, op: BinaryOp, lhs: Temporary, rhs: Temporary) !Temporary { - const zcu = self.pt.zcu; - const target = self.spv.target; - - const v = self.vectorization(.{ lhs, rhs }); - const ops = v.components(); - const results = self.spv.allocIds(ops); - - const op_result_ty = lhs.ty.scalarType(zcu); - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - const result_ty = try v.resultType(self, lhs.ty); - - const op_lhs = try v.prepare(self, lhs); - const op_rhs = try v.prepare(self, rhs); - - if (switch (op) { - .i_add => .OpIAdd, - .f_add => .OpFAdd, - .i_sub => .OpISub, - .f_sub => .OpFSub, - .i_mul => .OpIMul, - .f_mul => .OpFMul, - .s_div => .OpSDiv, - .u_div => .OpUDiv, - .f_div => .OpFDiv, - .s_rem => .OpSRem, - .f_rem => .OpFRem, - .s_mod => .OpSMod, - .u_mod => .OpUMod, - .f_mod => .OpFMod, - .srl => .OpShiftRightLogical, - .sra => .OpShiftRightArithmetic, - .sll => .OpShiftLeftLogical, - .bit_and => .OpBitwiseAnd, - .bit_or => .OpBitwiseOr, - .bit_xor => .OpBitwiseXor, - .l_and => .OpLogicalAnd, - .l_or => .OpLogicalOr, - else => @as(?Opcode, null), - }) |opcode| { - for (0..ops) |i| { - try self.func.body.emitRaw(self.spv.gpa, opcode, 4); - self.func.body.writeOperand(spec.Id, op_result_ty_id); - self.func.body.writeOperand(Id, results.at(i)); - self.func.body.writeOperand(Id, op_lhs.at(i)); - self.func.body.writeOperand(Id, op_rhs.at(i)); - } - } else { - const set = try self.importExtendedSet(); - - // TODO: Put these numbers in some definition - const extinst: u32 = switch (target.os.tag) { - .opencl => switch (op) { - .f_max => 27, // fmax - .s_max => 156, // s_max - .u_max => 157, // u_max - .f_min => 28, // fmin - .s_min => 158, // s_min - .u_min => 159, // u_min - else => unreachable, - }, - .vulkan, .opengl => switch (op) { - .f_max => 40, // FMax - .s_max => 42, // SMax - .u_max => 41, // UMax - .f_min => 37, // FMin - .s_min => 39, // SMin - .u_min => 38, // UMin - else => unreachable, - }, - else => unreachable, - }; - - for (0..ops) |i| { - try self.func.body.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = op_result_ty_id, - .id_result = results.at(i), - .set = set, - .instruction = .{ .inst = extinst }, - .id_ref_4 = &.{ op_lhs.at(i), op_rhs.at(i) }, - }); - } - } - - return v.finalize(result_ty, results); - } - - /// This function builds an extended multiplication, either OpSMulExtended or OpUMulExtended on Vulkan, - /// or OpIMul and s_mul_hi or u_mul_hi on OpenCL. - fn buildWideMul( - self: *NavGen, - op: enum { - s_mul_extended, - u_mul_extended, - }, - lhs: Temporary, - rhs: Temporary, - ) !struct { Temporary, Temporary } { - const pt = self.pt; - const zcu = pt.zcu; - const target = self.spv.target; - const ip = &zcu.intern_pool; - - const v = lhs.vectorization(self).unify(rhs.vectorization(self)); - const ops = v.components(); - - const arith_op_ty = lhs.ty.scalarType(zcu); - const arith_op_ty_id = try self.resolveType(arith_op_ty, .direct); - - const lhs_op = try v.prepare(self, lhs); - const rhs_op = try v.prepare(self, rhs); - - const value_results = self.spv.allocIds(ops); - const overflow_results = self.spv.allocIds(ops); - - switch (target.os.tag) { - .opencl => { - // Currently, SPIRV-LLVM-Translator based backends cannot deal with OpSMulExtended and - // OpUMulExtended. For these we will use the OpenCL s_mul_hi to compute the high-order bits - // instead. - const set = try self.importExtendedSet(); - const overflow_inst: u32 = switch (op) { - .s_mul_extended => 160, // s_mul_hi - .u_mul_extended => 203, // u_mul_hi - }; - - for (0..ops) |i| { - try self.func.body.emit(self.spv.gpa, .OpIMul, .{ - .id_result_type = arith_op_ty_id, - .id_result = value_results.at(i), - .operand_1 = lhs_op.at(i), - .operand_2 = rhs_op.at(i), - }); - - try self.func.body.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = arith_op_ty_id, - .id_result = overflow_results.at(i), - .set = set, - .instruction = .{ .inst = overflow_inst }, - .id_ref_4 = &.{ lhs_op.at(i), rhs_op.at(i) }, - }); - } - }, - .vulkan, .opengl => { - // Operations return a struct{T, T} - // where T is maybe vectorized. - const op_result_ty: Type = .fromInterned(try ip.getTupleType(zcu.gpa, pt.tid, .{ - .types = &.{ arith_op_ty.toIntern(), arith_op_ty.toIntern() }, - .values = &.{ .none, .none }, - })); - const op_result_ty_id = try self.resolveType(op_result_ty, .direct); - - const opcode: Opcode = switch (op) { - .s_mul_extended => .OpSMulExtended, - .u_mul_extended => .OpUMulExtended, - }; - - for (0..ops) |i| { - const op_result = self.spv.allocId(); - - try self.func.body.emitRaw(self.spv.gpa, opcode, 4); - self.func.body.writeOperand(spec.Id, op_result_ty_id); - self.func.body.writeOperand(Id, op_result); - self.func.body.writeOperand(Id, lhs_op.at(i)); - self.func.body.writeOperand(Id, rhs_op.at(i)); - - // The above operation returns a struct. We might want to expand - // Temporary to deal with the fact that these are structs eventually, - // but for now, take the struct apart and return two separate vectors. - - try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ - .id_result_type = arith_op_ty_id, - .id_result = value_results.at(i), - .composite = op_result, - .indexes = &.{0}, - }); - - try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ - .id_result_type = arith_op_ty_id, - .id_result = overflow_results.at(i), - .composite = op_result, - .indexes = &.{1}, - }); - } - }, - else => unreachable, - } - - const result_ty = try v.resultType(self, lhs.ty); - return .{ - v.finalize(result_ty, value_results), - v.finalize(result_ty, overflow_results), - }; - } - - /// The SPIR-V backend is not yet advanced enough to support the std testing infrastructure. - /// In order to be able to run tests, we "temporarily" lower test kernels into separate entry- - /// points. The test executor will then be able to invoke these to run the tests. - /// Note that tests are lowered according to std.builtin.TestFn, which is `fn () anyerror!void`. - /// (anyerror!void has the same layout as anyerror). - /// Each test declaration generates a function like. - /// %anyerror = OpTypeInt 0 16 - /// %p_invocation_globals_struct_ty = ... - /// %p_anyerror = OpTypePointer CrossWorkgroup %anyerror - /// %K = OpTypeFunction %void %p_invocation_globals_struct_ty %p_anyerror - /// - /// %test = OpFunction %void %K - /// %p_invocation_globals = OpFunctionParameter p_invocation_globals_struct_ty - /// %p_err = OpFunctionParameter %p_anyerror - /// %lbl = OpLabel - /// %result = OpFunctionCall %anyerror %func %p_invocation_globals - /// OpStore %p_err %result - /// OpFunctionEnd - /// TODO is to also write out the error as a function call parameter, and to somehow fetch - /// the name of an error in the text executor. - fn generateTestEntryPoint(self: *NavGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void { - const zcu = self.pt.zcu; - const target = self.spv.target; - - const anyerror_ty_id = try self.resolveType(Type.anyerror, .direct); - const ptr_anyerror_ty = try self.pt.ptrType(.{ - .child = Type.anyerror.toIntern(), - .flags = .{ .address_space = .global }, - }); - const ptr_anyerror_ty_id = try self.resolveType(ptr_anyerror_ty, .direct); - - const spv_decl_index = try self.spv.allocDecl(.func); - const kernel_id = self.spv.declPtr(spv_decl_index).result_id; - - var decl_deps = std.ArrayList(SpvModule.Decl.Index).init(self.gpa); - defer decl_deps.deinit(); - try decl_deps.append(spv_test_decl_index); - - const section = &self.spv.sections.functions; - - const p_error_id = self.spv.allocId(); - switch (target.os.tag) { - .opencl, .amdhsa => { - const kernel_proto_ty_id = try self.functionType(Type.void, &.{ptr_anyerror_ty}); - - try section.emit(self.spv.gpa, .OpFunction, .{ - .id_result_type = try self.resolveType(Type.void, .direct), - .id_result = kernel_id, - .function_control = .{}, - .function_type = kernel_proto_ty_id, - }); - - try section.emit(self.spv.gpa, .OpFunctionParameter, .{ - .id_result_type = ptr_anyerror_ty_id, - .id_result = p_error_id, - }); - - try section.emit(self.spv.gpa, .OpLabel, .{ - .id_result = self.spv.allocId(), - }); - }, - .vulkan, .opengl => { - if (self.object.error_buffer == null) { - const spv_err_decl_index = try self.spv.allocDecl(.global); - try self.spv.declareDeclDeps(spv_err_decl_index, &.{}); - - const buffer_struct_ty_id = self.spv.allocId(); - try self.spv.structType(buffer_struct_ty_id, &.{anyerror_ty_id}, &.{"error_out"}); - try self.spv.decorate(buffer_struct_ty_id, .block); - try self.spv.decorateMember(buffer_struct_ty_id, 0, .{ .offset = .{ .byte_offset = 0 } }); - - const ptr_buffer_struct_ty_id = self.spv.allocId(); - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpTypePointer, .{ - .id_result = ptr_buffer_struct_ty_id, - .storage_class = self.spvStorageClass(.global), - .type = buffer_struct_ty_id, - }); - - const buffer_struct_id = self.spv.declPtr(spv_err_decl_index).result_id; - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{ - .id_result_type = ptr_buffer_struct_ty_id, - .id_result = buffer_struct_id, - .storage_class = self.spvStorageClass(.global), - }); - try self.spv.decorate(buffer_struct_id, .{ .descriptor_set = .{ .descriptor_set = 0 } }); - try self.spv.decorate(buffer_struct_id, .{ .binding = .{ .binding_point = 0 } }); - - self.object.error_buffer = spv_err_decl_index; - } - - try self.spv.sections.execution_modes.emit(self.spv.gpa, .OpExecutionMode, .{ - .entry_point = kernel_id, - .mode = .{ .local_size = .{ - .x_size = 1, - .y_size = 1, - .z_size = 1, - } }, - }); - - const kernel_proto_ty_id = try self.functionType(Type.void, &.{}); - try section.emit(self.spv.gpa, .OpFunction, .{ - .id_result_type = try self.resolveType(Type.void, .direct), - .id_result = kernel_id, - .function_control = .{}, - .function_type = kernel_proto_ty_id, - }); - try section.emit(self.spv.gpa, .OpLabel, .{ - .id_result = self.spv.allocId(), - }); - - const spv_err_decl_index = self.object.error_buffer.?; - const buffer_id = self.spv.declPtr(spv_err_decl_index).result_id; - try decl_deps.append(spv_err_decl_index); - - const zero_id = try self.constInt(Type.u32, 0); - try section.emit(self.spv.gpa, .OpInBoundsAccessChain, .{ - .id_result_type = ptr_anyerror_ty_id, - .id_result = p_error_id, - .base = buffer_id, - .indexes = &.{zero_id}, - }); - }, - else => unreachable, - } - - const test_id = self.spv.declPtr(spv_test_decl_index).result_id; - const error_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpFunctionCall, .{ - .id_result_type = anyerror_ty_id, - .id_result = error_id, - .function = test_id, - }); - // Note: Convert to direct not required. - try section.emit(self.spv.gpa, .OpStore, .{ - .pointer = p_error_id, - .object = error_id, - .memory_access = .{ - .aligned = .{ .literal_integer = @intCast(Type.abiAlignment(.anyerror, zcu).toByteUnits().?) }, - }, - }); - try section.emit(self.spv.gpa, .OpReturn, {}); - try section.emit(self.spv.gpa, .OpFunctionEnd, {}); - - // Just generate a quick other name because the intel runtime crashes when the entry- - // point name is the same as a different OpName. - const test_name = try std.fmt.allocPrint(self.gpa, "test {s}", .{name}); - defer self.gpa.free(test_name); - - const execution_mode: spec.ExecutionModel = switch (target.os.tag) { - .vulkan, .opengl => .gl_compute, - .opencl, .amdhsa => .kernel, - else => unreachable, - }; - - try self.spv.declareDeclDeps(spv_decl_index, decl_deps.items); - try self.spv.declareEntryPoint(spv_decl_index, test_name, execution_mode, null); - } - - fn genNav(self: *NavGen, do_codegen: bool) !void { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - - const nav = ip.getNav(self.owner_nav); - const val = zcu.navValue(self.owner_nav); - const ty = val.typeOf(zcu); - - if (!do_codegen and !ty.hasRuntimeBits(zcu)) { - return; - } - - const spv_decl_index = try self.object.resolveNav(zcu, self.owner_nav); - const result_id = self.spv.declPtr(spv_decl_index).result_id; - - switch (self.spv.declPtr(spv_decl_index).kind) { - .func => { - const fn_info = zcu.typeToFunc(ty).?; - const return_ty_id = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type)); - - const prototype_ty_id = try self.resolveType(ty, .direct); - try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ - .id_result_type = return_ty_id, - .id_result = result_id, - .function_type = prototype_ty_id, - // Note: the backend will never be asked to generate an inline function - // (this is handled in sema), so we don't need to set function_control here. - .function_control = .{}, - }); - - comptime assert(zig_call_abi_ver == 3); - try self.args.ensureUnusedCapacity(self.gpa, fn_info.param_types.len); - for (fn_info.param_types.get(ip)) |param_ty_index| { - const param_ty = Type.fromInterned(param_ty_index); - if (!param_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; - - const param_type_id = try self.resolveType(param_ty, .direct); - const arg_result_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpFunctionParameter, .{ - .id_result_type = param_type_id, - .id_result = arg_result_id, - }); - self.args.appendAssumeCapacity(arg_result_id); - } - - // TODO: This could probably be done in a better way... - const root_block_id = self.spv.allocId(); - - // The root block of a function declaration should appear before OpVariable instructions, - // so it is generated into the function's prologue. - try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ - .id_result = root_block_id, - }); - self.current_block_label = root_block_id; - - const main_body = self.air.getMainBody(); - switch (self.control_flow) { - .structured => { - _ = try self.genStructuredBody(.selection, main_body); - // We always expect paths to here to end, but we still need the block - // to act as a dummy merge block. - try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); - }, - .unstructured => { - try self.genBody(main_body); - }, - } - try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); - // Append the actual code into the functions section. - try self.spv.addFunction(spv_decl_index, self.func); - - try self.spv.debugName(result_id, nav.fqn.toSlice(ip)); - - // Temporarily generate a test kernel declaration if this is a test function. - if (self.pt.zcu.test_functions.contains(self.owner_nav)) { - try self.generateTestEntryPoint(nav.fqn.toSlice(ip), spv_decl_index); - } - }, - .global => { - const maybe_init_val: ?Value = switch (ip.indexToKey(val.toIntern())) { - .func => unreachable, - .variable => |variable| Value.fromInterned(variable.init), - .@"extern" => null, - else => val, - }; - assert(maybe_init_val == null); // TODO - - const storage_class = self.spvStorageClass(nav.getAddrspace()); - assert(storage_class != .generic); // These should be instance globals - - const ptr_ty_id = try self.ptrType(ty, storage_class, .indirect); - - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{ - .id_result_type = ptr_ty_id, - .id_result = result_id, - .storage_class = storage_class, - }); - - if (std.meta.stringToEnum(spec.BuiltIn, nav.fqn.toSlice(ip))) |builtin| { - try self.spv.decorate(result_id, .{ .built_in = .{ .built_in = builtin } }); - } - - try self.spv.debugName(result_id, nav.fqn.toSlice(ip)); - try self.spv.declareDeclDeps(spv_decl_index, &.{}); - }, - .invocation_global => { - const maybe_init_val: ?Value = switch (ip.indexToKey(val.toIntern())) { - .func => unreachable, - .variable => |variable| Value.fromInterned(variable.init), - .@"extern" => null, - else => val, - }; - - try self.spv.declareDeclDeps(spv_decl_index, &.{}); - - const ptr_ty_id = try self.ptrType(ty, .function, .indirect); - - if (maybe_init_val) |init_val| { - // TODO: Combine with resolveAnonDecl? - const initializer_proto_ty_id = try self.functionType(Type.void, &.{}); - - const initializer_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ - .id_result_type = try self.resolveType(Type.void, .direct), - .id_result = initializer_id, - .function_control = .{}, - .function_type = initializer_proto_ty_id, - }); - - const root_block_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ - .id_result = root_block_id, - }); - self.current_block_label = root_block_id; - - const val_id = try self.constant(ty, init_val, .indirect); - try self.func.body.emit(self.spv.gpa, .OpStore, .{ - .pointer = result_id, - .object = val_id, - }); - - try self.func.body.emit(self.spv.gpa, .OpReturn, {}); - try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); - try self.spv.addFunction(spv_decl_index, self.func); - - try self.spv.debugNameFmt(initializer_id, "initializer of {f}", .{nav.fqn.fmt(ip)}); - - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = ptr_ty_id, - .id_result = result_id, - .set = try self.spv.importInstructionSet(.zig), - .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... - .id_ref_4 = &.{initializer_id}, - }); - } else { - try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ - .id_result_type = ptr_ty_id, - .id_result = result_id, - .set = try self.spv.importInstructionSet(.zig), - .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... - .id_ref_4 = &.{}, - }); - } - }, - } - } - - fn intFromBool(self: *NavGen, value: Temporary) !Temporary { - return try self.intFromBool2(value, Type.u1); - } - - fn intFromBool2(self: *NavGen, value: Temporary, result_ty: Type) !Temporary { - const zero_id = try self.constInt(result_ty, 0); - const one_id = try self.constInt(result_ty, 1); - - return try self.buildSelect( - value, - Temporary.init(result_ty, one_id), - Temporary.init(result_ty, zero_id), - ); - } - - /// Convert representation from indirect (in memory) to direct (in 'register') - /// This converts the argument type from resolveType(ty, .indirect) to resolveType(ty, .direct). - fn convertToDirect(self: *NavGen, ty: Type, operand_id: Id) !Id { - const pt = self.pt; - const zcu = pt.zcu; - switch (ty.scalarType(zcu).zigTypeTag(zcu)) { - .bool => { - const false_id = try self.constBool(false, .indirect); - const operand_ty = blk: { - if (!ty.isVector(pt.zcu)) break :blk Type.u1; - break :blk try pt.vectorType(.{ - .len = ty.vectorLen(pt.zcu), - .child = Type.u1.toIntern(), - }); - }; - - const result = try self.buildCmp( - .i_ne, - Temporary.init(operand_ty, operand_id), - Temporary.init(Type.u1, false_id), - ); - return try result.materialize(self); - }, - else => return operand_id, - } - } - - /// Convert representation from direct (in 'register) to direct (in memory) - /// This converts the argument type from resolveType(ty, .direct) to resolveType(ty, .indirect). - fn convertToIndirect(self: *NavGen, ty: Type, operand_id: Id) !Id { - const zcu = self.pt.zcu; - switch (ty.scalarType(zcu).zigTypeTag(zcu)) { - .bool => { - const result = try self.intFromBool(Temporary.init(ty, operand_id)); - return try result.materialize(self); - }, - else => return operand_id, - } - } - - fn extractField(self: *NavGen, result_ty: Type, object: Id, field: u32) !Id { - const result_ty_id = try self.resolveType(result_ty, .indirect); - const result_id = self.spv.allocId(); - const indexes = [_]u32{field}; - try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .composite = object, - .indexes = &indexes, - }); - // Convert bools; direct structs have their field types as indirect values. - return try self.convertToDirect(result_ty, result_id); - } - - fn extractVectorComponent(self: *NavGen, result_ty: Type, vector_id: Id, field: u32) !Id { - const result_ty_id = try self.resolveType(result_ty, .direct); - const result_id = self.spv.allocId(); - const indexes = [_]u32{field}; - try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .composite = vector_id, - .indexes = &indexes, - }); - // Vector components are already stored in direct representation. - return result_id; - } - - const MemoryOptions = struct { - is_volatile: bool = false, - }; - - fn load(self: *NavGen, value_ty: Type, ptr_id: Id, options: MemoryOptions) !Id { - const zcu = self.pt.zcu; - const alignment: u32 = @intCast(value_ty.abiAlignment(zcu).toByteUnits().?); - const indirect_value_ty_id = try self.resolveType(value_ty, .indirect); - const result_id = self.spv.allocId(); - const access: spec.MemoryAccess.Extended = .{ - .@"volatile" = options.is_volatile, - .aligned = .{ .literal_integer = alignment }, - }; - try self.func.body.emit(self.spv.gpa, .OpLoad, .{ - .id_result_type = indirect_value_ty_id, - .id_result = result_id, - .pointer = ptr_id, - .memory_access = access, - }); - return try self.convertToDirect(value_ty, result_id); - } - - fn store(self: *NavGen, value_ty: Type, ptr_id: Id, value_id: Id, options: MemoryOptions) !void { - const indirect_value_id = try self.convertToIndirect(value_ty, value_id); - const access: spec.MemoryAccess.Extended = .{ .@"volatile" = options.is_volatile }; - try self.func.body.emit(self.spv.gpa, .OpStore, .{ - .pointer = ptr_id, - .object = indirect_value_id, - .memory_access = access, - }); - } - - fn genBody(self: *NavGen, body: []const Air.Inst.Index) Error!void { - for (body) |inst| { - try self.genInst(inst); - } - } - - fn genInst(self: *NavGen, inst: Air.Inst.Index) !void { - const zcu = self.pt.zcu; - const ip = &zcu.intern_pool; - if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) - return; - - const air_tags = self.air.instructions.items(.tag); - const maybe_result_id: ?Id = switch (air_tags[@intFromEnum(inst)]) { - // zig fmt: off - .add, .add_wrap, .add_optimized => try self.airArithOp(inst, .f_add, .i_add, .i_add), - .sub, .sub_wrap, .sub_optimized => try self.airArithOp(inst, .f_sub, .i_sub, .i_sub), - .mul, .mul_wrap, .mul_optimized => try self.airArithOp(inst, .f_mul, .i_mul, .i_mul), - - .sqrt => try self.airUnOpSimple(inst, .sqrt), - .sin => try self.airUnOpSimple(inst, .sin), - .cos => try self.airUnOpSimple(inst, .cos), - .tan => try self.airUnOpSimple(inst, .tan), - .exp => try self.airUnOpSimple(inst, .exp), - .exp2 => try self.airUnOpSimple(inst, .exp2), - .log => try self.airUnOpSimple(inst, .log), - .log2 => try self.airUnOpSimple(inst, .log2), - .log10 => try self.airUnOpSimple(inst, .log10), - .abs => try self.airAbs(inst), - .floor => try self.airUnOpSimple(inst, .floor), - .ceil => try self.airUnOpSimple(inst, .ceil), - .round => try self.airUnOpSimple(inst, .round), - .trunc_float => try self.airUnOpSimple(inst, .trunc), - .neg, .neg_optimized => try self.airUnOpSimple(inst, .f_neg), - - .div_float, .div_float_optimized => try self.airArithOp(inst, .f_div, .s_div, .u_div), - .div_floor, .div_floor_optimized => try self.airDivFloor(inst), - .div_trunc, .div_trunc_optimized => try self.airDivTrunc(inst), - - .rem, .rem_optimized => try self.airArithOp(inst, .f_rem, .s_rem, .u_mod), - .mod, .mod_optimized => try self.airArithOp(inst, .f_mod, .s_mod, .u_mod), - - .add_with_overflow => try self.airAddSubOverflow(inst, .i_add, .u_lt, .s_lt), - .sub_with_overflow => try self.airAddSubOverflow(inst, .i_sub, .u_gt, .s_gt), - .mul_with_overflow => try self.airMulOverflow(inst), - .shl_with_overflow => try self.airShlOverflow(inst), - - .mul_add => try self.airMulAdd(inst), - - .ctz => try self.airClzCtz(inst, .ctz), - .clz => try self.airClzCtz(inst, .clz), - - .select => try self.airSelect(inst), - - .splat => try self.airSplat(inst), - .reduce, .reduce_optimized => try self.airReduce(inst), - .shuffle_one => try self.airShuffleOne(inst), - .shuffle_two => try self.airShuffleTwo(inst), - - .ptr_add => try self.airPtrAdd(inst), - .ptr_sub => try self.airPtrSub(inst), - - .bit_and => try self.airBinOpSimple(inst, .bit_and), - .bit_or => try self.airBinOpSimple(inst, .bit_or), - .xor => try self.airBinOpSimple(inst, .bit_xor), - .bool_and => try self.airBinOpSimple(inst, .l_and), - .bool_or => try self.airBinOpSimple(inst, .l_or), - - .shl, .shl_exact => try self.airShift(inst, .sll, .sll), - .shr, .shr_exact => try self.airShift(inst, .srl, .sra), - - .min => try self.airMinMax(inst, .min), - .max => try self.airMinMax(inst, .max), - - .bitcast => try self.airBitCast(inst), - .intcast, .trunc => try self.airIntCast(inst), - .float_from_int => try self.airFloatFromInt(inst), - .int_from_float => try self.airIntFromFloat(inst), - .fpext, .fptrunc => try self.airFloatCast(inst), - .not => try self.airNot(inst), - - .array_to_slice => try self.airArrayToSlice(inst), - .slice => try self.airSlice(inst), - .aggregate_init => try self.airAggregateInit(inst), - .memcpy => return self.airMemcpy(inst), - .memmove => return self.airMemmove(inst), - - .slice_ptr => try self.airSliceField(inst, 0), - .slice_len => try self.airSliceField(inst, 1), - .slice_elem_ptr => try self.airSliceElemPtr(inst), - .slice_elem_val => try self.airSliceElemVal(inst), - .ptr_elem_ptr => try self.airPtrElemPtr(inst), - .ptr_elem_val => try self.airPtrElemVal(inst), - .array_elem_val => try self.airArrayElemVal(inst), - - .vector_store_elem => return self.airVectorStoreElem(inst), - - .set_union_tag => return self.airSetUnionTag(inst), - .get_union_tag => try self.airGetUnionTag(inst), - .union_init => try self.airUnionInit(inst), - - .struct_field_val => try self.airStructFieldVal(inst), - .field_parent_ptr => try self.airFieldParentPtr(inst), - - .struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0), - .struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1), - .struct_field_ptr_index_2 => try self.airStructFieldPtrIndex(inst, 2), - .struct_field_ptr_index_3 => try self.airStructFieldPtrIndex(inst, 3), - - .cmp_eq => try self.airCmp(inst, .eq), - .cmp_neq => try self.airCmp(inst, .neq), - .cmp_gt => try self.airCmp(inst, .gt), - .cmp_gte => try self.airCmp(inst, .gte), - .cmp_lt => try self.airCmp(inst, .lt), - .cmp_lte => try self.airCmp(inst, .lte), - .cmp_vector => try self.airVectorCmp(inst), - - .arg => self.airArg(), - .alloc => try self.airAlloc(inst), - // TODO: We probably need to have a special implementation of this for the C abi. - .ret_ptr => try self.airAlloc(inst), - .block => try self.airBlock(inst), - - .load => try self.airLoad(inst), - .store, .store_safe => return self.airStore(inst), - - .br => return self.airBr(inst), - // For now just ignore this instruction. This effectively falls back on the old implementation, - // this doesn't change anything for us. - .repeat => return, - .breakpoint => return, - .cond_br => return self.airCondBr(inst), - .loop => return self.airLoop(inst), - .ret => return self.airRet(inst), - .ret_safe => return self.airRet(inst), // TODO - .ret_load => return self.airRetLoad(inst), - .@"try" => try self.airTry(inst), - .switch_br => return self.airSwitchBr(inst), - .unreach, .trap => return self.airUnreach(), - - .dbg_empty_stmt => return, - .dbg_stmt => return self.airDbgStmt(inst), - .dbg_inline_block => try self.airDbgInlineBlock(inst), - .dbg_var_ptr, .dbg_var_val, .dbg_arg_inline => return self.airDbgVar(inst), - - .unwrap_errunion_err => try self.airErrUnionErr(inst), - .unwrap_errunion_payload => try self.airErrUnionPayload(inst), - .wrap_errunion_err => try self.airWrapErrUnionErr(inst), - .wrap_errunion_payload => try self.airWrapErrUnionPayload(inst), - - .is_null => try self.airIsNull(inst, false, .is_null), - .is_non_null => try self.airIsNull(inst, false, .is_non_null), - .is_null_ptr => try self.airIsNull(inst, true, .is_null), - .is_non_null_ptr => try self.airIsNull(inst, true, .is_non_null), - .is_err => try self.airIsErr(inst, .is_err), - .is_non_err => try self.airIsErr(inst, .is_non_err), - - .optional_payload => try self.airUnwrapOptional(inst), - .optional_payload_ptr => try self.airUnwrapOptionalPtr(inst), - .wrap_optional => try self.airWrapOptional(inst), - - .assembly => try self.airAssembly(inst), - - .call => try self.airCall(inst, .auto), - .call_always_tail => try self.airCall(inst, .always_tail), - .call_never_tail => try self.airCall(inst, .never_tail), - .call_never_inline => try self.airCall(inst, .never_inline), - - .work_item_id => try self.airWorkItemId(inst), - .work_group_size => try self.airWorkGroupSize(inst), - .work_group_id => try self.airWorkGroupId(inst), - - // zig fmt: on - - else => |tag| return self.todo("implement AIR tag {s}", .{@tagName(tag)}), - }; - - const result_id = maybe_result_id orelse return; - try self.inst_results.putNoClobber(self.gpa, inst, result_id); - } - - fn airBinOpSimple(self: *NavGen, inst: Air.Inst.Index, op: BinaryOp) !?Id { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const lhs = try self.temporary(bin_op.lhs); - const rhs = try self.temporary(bin_op.rhs); - - const result = try self.buildBinary(op, lhs, rhs); - return try result.materialize(self); - } - - fn airShift(self: *NavGen, inst: Air.Inst.Index, unsigned: BinaryOp, signed: BinaryOp) !?Id { - const zcu = self.pt.zcu; - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - - if (self.typeOf(bin_op.lhs).isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu)) { - return self.fail("vector shift with scalar rhs", .{}); - } - - const base = try self.temporary(bin_op.lhs); - const shift = try self.temporary(bin_op.rhs); - - const result_ty = self.typeOfIndex(inst); - - const info = self.arithmeticTypeInfo(result_ty); - switch (info.class) { - .composite_integer => return self.todo("shift ops for composite integers", .{}), - .integer, .strange_integer => {}, - .float, .bool => unreachable, - } - - // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that, - // so just manually upcast it if required. - - // Note: The sign may differ here between the shift and the base type, in case - // of an arithmetic right shift. SPIR-V still expects the same type, - // so in that case we have to cast convert to signed. - const casted_shift = try self.buildConvert(base.ty.scalarType(zcu), shift); - - const shifted = switch (info.signedness) { - .unsigned => try self.buildBinary(unsigned, base, casted_shift), - .signed => try self.buildBinary(signed, base, casted_shift), - }; - - const result = try self.normalize(shifted, info); - return try result.materialize(self); - } - - const MinMax = enum { min, max }; - - fn airMinMax(self: *NavGen, inst: Air.Inst.Index, op: MinMax) !?Id { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - - const lhs = try self.temporary(bin_op.lhs); - const rhs = try self.temporary(bin_op.rhs); - - const result = try self.minMax(lhs, rhs, op); - return try result.materialize(self); - } - - fn minMax(self: *NavGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { - const info = self.arithmeticTypeInfo(lhs.ty); - - const binop: BinaryOp = switch (info.class) { - .float => switch (op) { - .min => .f_min, - .max => .f_max, - }, - .integer, .strange_integer => switch (info.signedness) { - .signed => switch (op) { - .min => .s_min, - .max => .s_max, - }, - .unsigned => switch (op) { - .min => .u_min, - .max => .u_max, - }, - }, - .composite_integer => unreachable, // TODO - .bool => unreachable, - }; - - return try self.buildBinary(binop, lhs, rhs); - } - - /// This function normalizes values to a canonical representation - /// after some arithmetic operation. This mostly consists of wrapping - /// behavior for strange integers: - /// - Unsigned integers are bitwise masked with a mask that only passes - /// the valid bits through. - /// - Signed integers are also sign extended if they are negative. - /// All other values are returned unmodified (this makes strange integer - /// wrapping easier to use in generic operations). - fn normalize(self: *NavGen, value: Temporary, info: ArithmeticTypeInfo) !Temporary { - const zcu = self.pt.zcu; - const ty = value.ty; - switch (info.class) { - .composite_integer, .integer, .bool, .float => return value, - .strange_integer => switch (info.signedness) { - .unsigned => { - const mask_value = if (info.bits == 64) 0xFFFF_FFFF_FFFF_FFFF else (@as(u64, 1) << @as(u6, @intCast(info.bits))) - 1; - const mask_id = try self.constInt(ty.scalarType(zcu), mask_value); - return try self.buildBinary(.bit_and, value, Temporary.init(ty.scalarType(zcu), mask_id)); - }, - .signed => { - // Shift left and right so that we can copy the sight bit that way. - const shift_amt_id = try self.constInt(ty.scalarType(zcu), info.backing_bits - info.bits); - const shift_amt = Temporary.init(ty.scalarType(zcu), shift_amt_id); - const left = try self.buildBinary(.sll, value, shift_amt); - return try self.buildBinary(.sra, left, shift_amt); - }, - }, - } - } - - fn airDivFloor(self: *NavGen, inst: Air.Inst.Index) !?Id { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - - const lhs = try self.temporary(bin_op.lhs); - const rhs = try self.temporary(bin_op.rhs); - - const info = self.arithmeticTypeInfo(lhs.ty); - switch (info.class) { - .composite_integer => unreachable, // TODO - .integer, .strange_integer => { - switch (info.signedness) { - .unsigned => { - const result = try self.buildBinary(.u_div, lhs, rhs); - return try result.materialize(self); - }, - .signed => {}, - } - - // For signed integers: - // (a / b) - (a % b != 0 && a < 0 != b < 0); - // There shouldn't be any overflow issues. - - const div = try self.buildBinary(.s_div, lhs, rhs); - const rem = try self.buildBinary(.s_rem, lhs, rhs); - - const zero = Temporary.init(lhs.ty, try self.constInt(lhs.ty, 0)); - - const rem_is_not_zero = try self.buildCmp(.i_ne, rem, zero); - - const result_negative = try self.buildCmp( - .l_ne, - try self.buildCmp(.s_lt, lhs, zero), - try self.buildCmp(.s_lt, rhs, zero), - ); - const rem_is_not_zero_and_result_is_negative = try self.buildBinary( - .l_and, - rem_is_not_zero, - result_negative, - ); - - const result = try self.buildBinary( - .i_sub, - div, - try self.intFromBool2(rem_is_not_zero_and_result_is_negative, div.ty), - ); - - return try result.materialize(self); - }, - .float => { - const div = try self.buildBinary(.f_div, lhs, rhs); - const result = try self.buildUnary(.floor, div); - return try result.materialize(self); - }, - .bool => unreachable, - } - } - - fn airDivTrunc(self: *NavGen, inst: Air.Inst.Index) !?Id { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - - const lhs = try self.temporary(bin_op.lhs); - const rhs = try self.temporary(bin_op.rhs); - - const info = self.arithmeticTypeInfo(lhs.ty); - switch (info.class) { - .composite_integer => unreachable, // TODO - .integer, .strange_integer => switch (info.signedness) { - .unsigned => { - const result = try self.buildBinary(.u_div, lhs, rhs); - return try result.materialize(self); - }, - .signed => { - const result = try self.buildBinary(.s_div, lhs, rhs); - return try result.materialize(self); - }, - }, - .float => { - const div = try self.buildBinary(.f_div, lhs, rhs); - const result = try self.buildUnary(.trunc, div); - return try result.materialize(self); - }, - .bool => unreachable, - } - } - - fn airUnOpSimple(self: *NavGen, inst: Air.Inst.Index, op: UnaryOp) !?Id { - const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; - const operand = try self.temporary(un_op); - const result = try self.buildUnary(op, operand); - return try result.materialize(self); - } - - fn airArithOp( - self: *NavGen, - inst: Air.Inst.Index, - comptime fop: BinaryOp, - comptime sop: BinaryOp, - comptime uop: BinaryOp, - ) !?Id { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - - const lhs = try self.temporary(bin_op.lhs); - const rhs = try self.temporary(bin_op.rhs); - - const info = self.arithmeticTypeInfo(lhs.ty); - - const result = switch (info.class) { - .composite_integer => unreachable, // TODO - .integer, .strange_integer => switch (info.signedness) { - .signed => try self.buildBinary(sop, lhs, rhs), - .unsigned => try self.buildBinary(uop, lhs, rhs), - }, - .float => try self.buildBinary(fop, lhs, rhs), - .bool => unreachable, - }; - - return try result.materialize(self); - } - - fn airAbs(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand = try self.temporary(ty_op.operand); - // Note: operand_ty may be signed, while ty is always unsigned! - const result_ty = self.typeOfIndex(inst); - const result = try self.abs(result_ty, operand); - return try result.materialize(self); - } - - fn abs(self: *NavGen, result_ty: Type, value: Temporary) !Temporary { - const zcu = self.pt.zcu; - const operand_info = self.arithmeticTypeInfo(value.ty); - - switch (operand_info.class) { - .float => return try self.buildUnary(.f_abs, value), - .integer, .strange_integer => { - const abs_value = try self.buildUnary(.i_abs, value); - - switch (self.spv.target.os.tag) { - .vulkan, .opengl => { - if (value.ty.intInfo(zcu).signedness == .signed) { - return self.todo("perform bitcast after @abs", .{}); - } - }, - else => {}, - } - - return try self.normalize(abs_value, self.arithmeticTypeInfo(result_ty)); - }, - .composite_integer => unreachable, // TODO - .bool => unreachable, - } - } - - fn airAddSubOverflow( - self: *NavGen, - inst: Air.Inst.Index, - comptime add: BinaryOp, - comptime ucmp: CmpPredicate, - comptime scmp: CmpPredicate, - ) !?Id { - _ = scmp; - // Note: OpIAddCarry and OpISubBorrow are not really useful here: For unsigned numbers, - // there is in both cases only one extra operation required. For signed operations, - // the overflow bit is set then going from 0x80.. to 0x00.., but this doesn't actually - // normally set a carry bit. So the SPIR-V overflow operations are not particularly - // useful here. - - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; - - const lhs = try self.temporary(extra.lhs); - const rhs = try self.temporary(extra.rhs); - - const result_ty = self.typeOfIndex(inst); - - const info = self.arithmeticTypeInfo(lhs.ty); - switch (info.class) { - .composite_integer => unreachable, // TODO - .strange_integer, .integer => {}, - .float, .bool => unreachable, - } - - const sum = try self.buildBinary(add, lhs, rhs); - const result = try self.normalize(sum, info); - - const overflowed = switch (info.signedness) { - // Overflow happened if the result is smaller than either of the operands. It doesn't matter which. - // For subtraction the conditions need to be swapped. - .unsigned => try self.buildCmp(ucmp, result, lhs), - // For signed operations, we check the signs of the operands and the result. - .signed => blk: { - // Signed overflow detection using the sign bits of the operands and the result. - // For addition (a + b), overflow occurs if the operands have the same sign - // and the result's sign is different from the operands' sign. - // (sign(a) == sign(b)) && (sign(a) != sign(result)) - // For subtraction (a - b), overflow occurs if the operands have different signs - // and the result's sign is different from the minuend's (a's) sign. - // (sign(a) != sign(b)) && (sign(a) != sign(result)) - const zero = Temporary.init(rhs.ty, try self.constInt(rhs.ty, 0)); - - const lhs_is_neg = try self.buildCmp(.s_lt, lhs, zero); - const rhs_is_neg = try self.buildCmp(.s_lt, rhs, zero); - const result_is_neg = try self.buildCmp(.s_lt, result, zero); - - const signs_match = try self.buildCmp(.l_eq, lhs_is_neg, rhs_is_neg); - const result_sign_differs = try self.buildCmp(.l_ne, lhs_is_neg, result_is_neg); - - const overflow_condition = if (add == .i_add) - signs_match - else // .i_sub - try self.buildUnary(.l_not, signs_match); - - break :blk try self.buildBinary(.l_and, overflow_condition, result_sign_differs); - }, - }; - - const ov = try self.intFromBool(overflowed); - - const result_ty_id = try self.resolveType(result_ty, .direct); - return try self.constructComposite(result_ty_id, &.{ try result.materialize(self), try ov.materialize(self) }); - } - - fn airMulOverflow(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; - - const lhs = try self.temporary(extra.lhs); - const rhs = try self.temporary(extra.rhs); - - const result_ty = self.typeOfIndex(inst); - - const info = self.arithmeticTypeInfo(lhs.ty); - switch (info.class) { - .composite_integer => unreachable, // TODO - .strange_integer, .integer => {}, - .float, .bool => unreachable, - } - - // There are 3 cases which we have to deal with: - // - If info.bits < 32 / 2, we will upcast to 32 and check the higher bits - // - If info.bits > 32 / 2, we have to use extended multiplication - // - Additionally, if info.bits != 32, we'll have to check the high bits - // of the result too. - - const largest_int_bits = self.largestSupportedIntBits(); - // If non-null, the number of bits that the multiplication should be performed in. If - // null, we have to use wide multiplication. - const maybe_op_ty_bits: ?u16 = switch (info.bits) { - 0 => unreachable, - 1...16 => 32, - 17...32 => if (largest_int_bits > 32) 64 else null, // Upcast if we can. - 33...64 => null, // Always use wide multiplication. - else => unreachable, // TODO: Composite integers - }; - - const result, const overflowed = switch (info.signedness) { - .unsigned => blk: { - if (maybe_op_ty_bits) |op_ty_bits| { - const op_ty = try pt.intType(.unsigned, op_ty_bits); - const casted_lhs = try self.buildConvert(op_ty, lhs); - const casted_rhs = try self.buildConvert(op_ty, rhs); - - const full_result = try self.buildBinary(.i_mul, casted_lhs, casted_rhs); - - const low_bits = try self.buildConvert(lhs.ty, full_result); - const result = try self.normalize(low_bits, info); - - // Shift the result bits away to get the overflow bits. - const shift = Temporary.init(full_result.ty, try self.constInt(full_result.ty, info.bits)); - const overflow = try self.buildBinary(.srl, full_result, shift); - - // Directly check if its zero in the op_ty without converting first. - const zero = Temporary.init(full_result.ty, try self.constInt(full_result.ty, 0)); - const overflowed = try self.buildCmp(.i_ne, zero, overflow); - - break :blk .{ result, overflowed }; - } - - const low_bits, const high_bits = try self.buildWideMul(.u_mul_extended, lhs, rhs); - - // Truncate the result, if required. - const result = try self.normalize(low_bits, info); - - // Overflow happened if the high-bits of the result are non-zero OR if the - // high bits of the low word of the result (those outside the range of the - // int) are nonzero. - const zero = Temporary.init(lhs.ty, try self.constInt(lhs.ty, 0)); - const high_overflowed = try self.buildCmp(.i_ne, zero, high_bits); - - // If no overflow bits in low_bits, no extra work needs to be done. - if (info.backing_bits == info.bits) break :blk .{ result, high_overflowed }; - - // Shift the result bits away to get the overflow bits. - const shift = Temporary.init(lhs.ty, try self.constInt(lhs.ty, info.bits)); - const low_overflow = try self.buildBinary(.srl, low_bits, shift); - const low_overflowed = try self.buildCmp(.i_ne, zero, low_overflow); - - const overflowed = try self.buildBinary(.l_or, low_overflowed, high_overflowed); - - break :blk .{ result, overflowed }; - }, - .signed => blk: { - // - lhs >= 0, rhxs >= 0: expect positive; overflow should be 0 - // - lhs == 0 : expect positive; overflow should be 0 - // - rhs == 0: expect positive; overflow should be 0 - // - lhs > 0, rhs < 0: expect negative; overflow should be -1 - // - lhs < 0, rhs > 0: expect negative; overflow should be -1 - // - lhs <= 0, rhs <= 0: expect positive; overflow should be 0 - // ------ - // overflow should be -1 when - // (lhs > 0 && rhs < 0) || (lhs < 0 && rhs > 0) - - const zero = Temporary.init(lhs.ty, try self.constInt(lhs.ty, 0)); - const lhs_negative = try self.buildCmp(.s_lt, lhs, zero); - const rhs_negative = try self.buildCmp(.s_lt, rhs, zero); - const lhs_positive = try self.buildCmp(.s_gt, lhs, zero); - const rhs_positive = try self.buildCmp(.s_gt, rhs, zero); - - // Set to `true` if we expect -1. - const expected_overflow_bit = try self.buildBinary( - .l_or, - try self.buildBinary(.l_and, lhs_positive, rhs_negative), - try self.buildBinary(.l_and, lhs_negative, rhs_positive), - ); - - if (maybe_op_ty_bits) |op_ty_bits| { - const op_ty = try pt.intType(.signed, op_ty_bits); - // Assume normalized; sign bit is set. We want a sign extend. - const casted_lhs = try self.buildConvert(op_ty, lhs); - const casted_rhs = try self.buildConvert(op_ty, rhs); - - const full_result = try self.buildBinary(.i_mul, casted_lhs, casted_rhs); - - // Truncate to the result type. - const low_bits = try self.buildConvert(lhs.ty, full_result); - const result = try self.normalize(low_bits, info); - - // Now, we need to check the overflow bits AND the sign - // bit for the expected overflow bits. - // To do that, shift out everything bit the sign bit and - // then check what remains. - const shift = Temporary.init(full_result.ty, try self.constInt(full_result.ty, info.bits - 1)); - // Use SRA so that any sign bits are duplicated. Now we can just check if ALL bits are set - // for negative cases. - const overflow = try self.buildBinary(.sra, full_result, shift); - - const long_all_set = Temporary.init(full_result.ty, try self.constInt(full_result.ty, -1)); - const long_zero = Temporary.init(full_result.ty, try self.constInt(full_result.ty, 0)); - const mask = try self.buildSelect(expected_overflow_bit, long_all_set, long_zero); - - const overflowed = try self.buildCmp(.i_ne, mask, overflow); - - break :blk .{ result, overflowed }; - } - - const low_bits, const high_bits = try self.buildWideMul(.s_mul_extended, lhs, rhs); - - // Truncate result if required. - const result = try self.normalize(low_bits, info); - - const all_set = Temporary.init(lhs.ty, try self.constInt(lhs.ty, -1)); - const mask = try self.buildSelect(expected_overflow_bit, all_set, zero); - - // Like with unsigned, overflow happened if high_bits are not the ones we expect, - // and we also need to check some ones from the low bits. - - const high_overflowed = try self.buildCmp(.i_ne, mask, high_bits); - - // If no overflow bits in low_bits, no extra work needs to be done. - // Careful, we still have to check the sign bit, so this branch - // only goes for i33 and such. - if (info.backing_bits == info.bits + 1) break :blk .{ result, high_overflowed }; - - // Shift the result bits away to get the overflow bits. - const shift = Temporary.init(lhs.ty, try self.constInt(lhs.ty, info.bits - 1)); - // Use SRA so that any sign bits are duplicated. Now we can just check if ALL bits are set - // for negative cases. - const low_overflow = try self.buildBinary(.sra, low_bits, shift); - const low_overflowed = try self.buildCmp(.i_ne, mask, low_overflow); - - const overflowed = try self.buildBinary(.l_or, low_overflowed, high_overflowed); - - break :blk .{ result, overflowed }; - }, - }; - - const ov = try self.intFromBool(overflowed); - - const result_ty_id = try self.resolveType(result_ty, .direct); - return try self.constructComposite(result_ty_id, &.{ try result.materialize(self), try ov.materialize(self) }); - } - - fn airShlOverflow(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; - - if (self.typeOf(extra.lhs).isVector(zcu) and !self.typeOf(extra.rhs).isVector(zcu)) { - return self.fail("vector shift with scalar rhs", .{}); - } - - const base = try self.temporary(extra.lhs); - const shift = try self.temporary(extra.rhs); - - const result_ty = self.typeOfIndex(inst); - - const info = self.arithmeticTypeInfo(base.ty); - switch (info.class) { - .composite_integer => unreachable, // TODO - .integer, .strange_integer => {}, - .float, .bool => unreachable, - } - - // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that, - // so just manually upcast it if required. - const casted_shift = try self.buildConvert(base.ty.scalarType(zcu), shift); - - const left = try self.buildBinary(.sll, base, casted_shift); - const result = try self.normalize(left, info); - - const right = switch (info.signedness) { - .unsigned => try self.buildBinary(.srl, result, casted_shift), - .signed => try self.buildBinary(.sra, result, casted_shift), - }; - - const overflowed = try self.buildCmp(.i_ne, base, right); - const ov = try self.intFromBool(overflowed); - - const result_ty_id = try self.resolveType(result_ty, .direct); - return try self.constructComposite(result_ty_id, &.{ try result.materialize(self), try ov.materialize(self) }); - } - - fn airMulAdd(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const extra = self.air.extraData(Air.Bin, pl_op.payload).data; - - const a = try self.temporary(extra.lhs); - const b = try self.temporary(extra.rhs); - const c = try self.temporary(pl_op.operand); - - const result_ty = self.typeOfIndex(inst); - const info = self.arithmeticTypeInfo(result_ty); - assert(info.class == .float); // .mul_add is only emitted for floats - - const result = try self.buildFma(a, b, c); - return try result.materialize(self); - } - - fn airClzCtz(self: *NavGen, inst: Air.Inst.Index, op: UnaryOp) !?Id { - if (self.liveness.isUnused(inst)) return null; - - const zcu = self.pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand = try self.temporary(ty_op.operand); - - const scalar_result_ty = self.typeOfIndex(inst).scalarType(zcu); - - const info = self.arithmeticTypeInfo(operand.ty); - switch (info.class) { - .composite_integer => unreachable, // TODO - .integer, .strange_integer => {}, - .float, .bool => unreachable, - } - - const count = try self.buildUnary(op, operand); - - // Result of OpenCL ctz/clz returns operand.ty, and we want result_ty. - // result_ty is always large enough to hold the result, so we might have to down - // cast it. - const result = try self.buildConvert(scalar_result_ty, count); - return try result.materialize(self); - } - - fn airSelect(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const extra = self.air.extraData(Air.Bin, pl_op.payload).data; - const pred = try self.temporary(pl_op.operand); - const a = try self.temporary(extra.lhs); - const b = try self.temporary(extra.rhs); - - const result = try self.buildSelect(pred, a, b); - return try result.materialize(self); - } - - fn airSplat(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - - const operand_id = try self.resolve(ty_op.operand); - const result_ty = self.typeOfIndex(inst); - - return try self.constructCompositeSplat(result_ty, operand_id); - } - - fn airReduce(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce; - const operand = try self.resolve(reduce.operand); - const operand_ty = self.typeOf(reduce.operand); - const scalar_ty = operand_ty.scalarType(zcu); - const scalar_ty_id = try self.resolveType(scalar_ty, .direct); - const info = self.arithmeticTypeInfo(operand_ty); - const len = operand_ty.vectorLen(zcu); - const first = try self.extractVectorComponent(scalar_ty, operand, 0); - - switch (reduce.operation) { - .Min, .Max => |op| { - var result = Temporary.init(scalar_ty, first); - const cmp_op: MinMax = switch (op) { - .Max => .max, - .Min => .min, - else => unreachable, - }; - for (1..len) |i| { - const lhs = result; - const rhs_id = try self.extractVectorComponent(scalar_ty, operand, @intCast(i)); - const rhs = Temporary.init(scalar_ty, rhs_id); - - result = try self.minMax(lhs, rhs, cmp_op); - } - - return try result.materialize(self); - }, - else => {}, - } - - var result_id = first; - - const opcode: Opcode = switch (info.class) { - .bool => switch (reduce.operation) { - .And => .OpLogicalAnd, - .Or => .OpLogicalOr, - .Xor => .OpLogicalNotEqual, - else => unreachable, - }, - .strange_integer, .integer => switch (reduce.operation) { - .And => .OpBitwiseAnd, - .Or => .OpBitwiseOr, - .Xor => .OpBitwiseXor, - .Add => .OpIAdd, - .Mul => .OpIMul, - else => unreachable, - }, - .float => switch (reduce.operation) { - .Add => .OpFAdd, - .Mul => .OpFMul, - else => unreachable, - }, - .composite_integer => unreachable, // TODO - }; - - for (1..len) |i| { - const lhs = result_id; - const rhs = try self.extractVectorComponent(scalar_ty, operand, @intCast(i)); - result_id = self.spv.allocId(); - - try self.func.body.emitRaw(self.spv.gpa, opcode, 4); - self.func.body.writeOperand(spec.Id, scalar_ty_id); - self.func.body.writeOperand(spec.Id, result_id); - self.func.body.writeOperand(spec.Id, lhs); - self.func.body.writeOperand(spec.Id, rhs); - } - - return result_id; - } - - fn airShuffleOne(ng: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = ng.pt; - const zcu = pt.zcu; - const gpa = zcu.gpa; - - const unwrapped = ng.air.unwrapShuffleOne(zcu, inst); - const mask = unwrapped.mask; - const result_ty = unwrapped.result_ty; - const elem_ty = result_ty.childType(zcu); - const operand = try ng.resolve(unwrapped.operand); - - const constituents = try gpa.alloc(Id, mask.len); - defer gpa.free(constituents); - - for (constituents, mask) |*id, mask_elem| { - id.* = switch (mask_elem.unwrap()) { - .elem => |idx| try ng.extractVectorComponent(elem_ty, operand, idx), - .value => |val| try ng.constant(elem_ty, .fromInterned(val), .direct), - }; - } - - const result_ty_id = try ng.resolveType(result_ty, .direct); - return try ng.constructComposite(result_ty_id, constituents); - } - - fn airShuffleTwo(ng: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = ng.pt; - const zcu = pt.zcu; - const gpa = zcu.gpa; - - const unwrapped = ng.air.unwrapShuffleTwo(zcu, inst); - const mask = unwrapped.mask; - const result_ty = unwrapped.result_ty; - const elem_ty = result_ty.childType(zcu); - const elem_ty_id = try ng.resolveType(elem_ty, .direct); - const operand_a = try ng.resolve(unwrapped.operand_a); - const operand_b = try ng.resolve(unwrapped.operand_b); - - const constituents = try gpa.alloc(Id, mask.len); - defer gpa.free(constituents); - - for (constituents, mask) |*id, mask_elem| { - id.* = switch (mask_elem.unwrap()) { - .a_elem => |idx| try ng.extractVectorComponent(elem_ty, operand_a, idx), - .b_elem => |idx| try ng.extractVectorComponent(elem_ty, operand_b, idx), - .undef => try ng.spv.constUndef(elem_ty_id), - }; - } - - const result_ty_id = try ng.resolveType(result_ty, .direct); - return try ng.constructComposite(result_ty_id, constituents); - } - - fn indicesToIds(self: *NavGen, indices: []const u32) ![]Id { - const ids = try self.gpa.alloc(Id, indices.len); - errdefer self.gpa.free(ids); - for (indices, ids) |index, *id| { - id.* = try self.constInt(Type.u32, index); - } - - return ids; - } - - fn accessChainId( - self: *NavGen, - result_ty_id: Id, - base: Id, - indices: []const Id, - ) !Id { - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpInBoundsAccessChain, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .base = base, - .indexes = indices, - }); - return result_id; - } - - /// AccessChain is essentially PtrAccessChain with 0 as initial argument. The effective - /// difference lies in whether the resulting type of the first dereference will be the - /// same as that of the base pointer, or that of a dereferenced base pointer. AccessChain - /// is the latter and PtrAccessChain is the former. - fn accessChain( - self: *NavGen, - result_ty_id: Id, - base: Id, - indices: []const u32, - ) !Id { - const ids = try self.indicesToIds(indices); - defer self.gpa.free(ids); - return try self.accessChainId(result_ty_id, base, ids); - } - - fn ptrAccessChain( - self: *NavGen, - result_ty_id: Id, - base: Id, - element: Id, - indices: []const u32, - ) !Id { - const ids = try self.indicesToIds(indices); - defer self.gpa.free(ids); - - const result_id = self.spv.allocId(); - switch (self.spv.target.os.tag) { - .opencl, .amdhsa => { - try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .base = base, - .element = element, - .indexes = ids, - }); - }, - else => { - try self.func.body.emit(self.spv.gpa, .OpPtrAccessChain, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .base = base, - .element = element, - .indexes = ids, - }); - }, - } - return result_id; - } - - fn ptrAdd(self: *NavGen, result_ty: Type, ptr_ty: Type, ptr_id: Id, offset_id: Id) !Id { - const zcu = self.pt.zcu; - const result_ty_id = try self.resolveType(result_ty, .direct); - - switch (ptr_ty.ptrSize(zcu)) { - .one => { - // Pointer to array - // TODO: Is this correct? - return try self.accessChainId(result_ty_id, ptr_id, &.{offset_id}); - }, - .c, .many => { - return try self.ptrAccessChain(result_ty_id, ptr_id, offset_id, &.{}); - }, - .slice => { - // TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does. - const slice_ptr_id = try self.extractField(result_ty, ptr_id, 0); - return try self.ptrAccessChain(result_ty_id, slice_ptr_id, offset_id, &.{}); - }, - } - } - - fn airPtrAdd(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; - const ptr_id = try self.resolve(bin_op.lhs); - const offset_id = try self.resolve(bin_op.rhs); - const ptr_ty = self.typeOf(bin_op.lhs); - const result_ty = self.typeOfIndex(inst); - - return try self.ptrAdd(result_ty, ptr_ty, ptr_id, offset_id); - } - - fn airPtrSub(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; - const ptr_id = try self.resolve(bin_op.lhs); - const ptr_ty = self.typeOf(bin_op.lhs); - const offset_id = try self.resolve(bin_op.rhs); - const offset_ty = self.typeOf(bin_op.rhs); - const offset_ty_id = try self.resolveType(offset_ty, .direct); - const result_ty = self.typeOfIndex(inst); - - const negative_offset_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpSNegate, .{ - .id_result_type = offset_ty_id, - .id_result = negative_offset_id, - .operand = offset_id, - }); - return try self.ptrAdd(result_ty, ptr_ty, ptr_id, negative_offset_id); - } - - fn cmp( - self: *NavGen, - op: std.math.CompareOperator, - lhs: Temporary, - rhs: Temporary, - ) !Temporary { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const scalar_ty = lhs.ty.scalarType(zcu); - const is_vector = lhs.ty.isVector(zcu); - - switch (scalar_ty.zigTypeTag(zcu)) { - .int, .bool, .float => {}, - .@"enum" => { - assert(!is_vector); - const ty = lhs.ty.intTagType(zcu); - return try self.cmp(op, lhs.pun(ty), rhs.pun(ty)); - }, - .@"struct" => { - const struct_ty = zcu.typeToPackedStruct(scalar_ty).?; - const ty = Type.fromInterned(struct_ty.backingIntTypeUnordered(ip)); - return try self.cmp(op, lhs.pun(ty), rhs.pun(ty)); - }, - .error_set => { - assert(!is_vector); - const err_int_ty = try pt.errorIntType(); - return try self.cmp(op, lhs.pun(err_int_ty), rhs.pun(err_int_ty)); - }, - .pointer => { - assert(!is_vector); - // Note that while SPIR-V offers OpPtrEqual and OpPtrNotEqual, they are - // currently not implemented in the SPIR-V LLVM translator. Thus, we emit these using - // OpConvertPtrToU... - - const usize_ty_id = try self.resolveType(Type.usize, .direct); - - const lhs_int_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{ - .id_result_type = usize_ty_id, - .id_result = lhs_int_id, - .pointer = try lhs.materialize(self), - }); - - const rhs_int_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{ - .id_result_type = usize_ty_id, - .id_result = rhs_int_id, - .pointer = try rhs.materialize(self), - }); - - const lhs_int = Temporary.init(Type.usize, lhs_int_id); - const rhs_int = Temporary.init(Type.usize, rhs_int_id); - return try self.cmp(op, lhs_int, rhs_int); - }, - .optional => { - assert(!is_vector); - - const ty = lhs.ty; - - const payload_ty = ty.optionalChild(zcu); - if (ty.optionalReprIsPayload(zcu)) { - assert(payload_ty.hasRuntimeBitsIgnoreComptime(zcu)); - assert(!payload_ty.isSlice(zcu)); - - return try self.cmp(op, lhs.pun(payload_ty), rhs.pun(payload_ty)); - } - - const lhs_id = try lhs.materialize(self); - const rhs_id = try rhs.materialize(self); - - const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) - try self.extractField(Type.bool, lhs_id, 1) - else - try self.convertToDirect(Type.bool, lhs_id); - - const rhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) - try self.extractField(Type.bool, rhs_id, 1) - else - try self.convertToDirect(Type.bool, rhs_id); - - const lhs_valid = Temporary.init(Type.bool, lhs_valid_id); - const rhs_valid = Temporary.init(Type.bool, rhs_valid_id); - - if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - return try self.cmp(op, lhs_valid, rhs_valid); - } - - // a = lhs_valid - // b = rhs_valid - // c = lhs_pl == rhs_pl - // - // For op == .eq we have: - // a == b && a -> c - // = a == b && (!a || c) - // - // For op == .neq we have - // a == b && a -> c - // = !(a == b && a -> c) - // = a != b || !(a -> c - // = a != b || !(!a || c) - // = a != b || a && !c - - const lhs_pl_id = try self.extractField(payload_ty, lhs_id, 0); - const rhs_pl_id = try self.extractField(payload_ty, rhs_id, 0); - - const lhs_pl = Temporary.init(payload_ty, lhs_pl_id); - const rhs_pl = Temporary.init(payload_ty, rhs_pl_id); - - return switch (op) { - .eq => try self.buildBinary( - .l_and, - try self.cmp(.eq, lhs_valid, rhs_valid), - try self.buildBinary( - .l_or, - try self.buildUnary(.l_not, lhs_valid), - try self.cmp(.eq, lhs_pl, rhs_pl), - ), - ), - .neq => try self.buildBinary( - .l_or, - try self.cmp(.neq, lhs_valid, rhs_valid), - try self.buildBinary( - .l_and, - lhs_valid, - try self.cmp(.neq, lhs_pl, rhs_pl), - ), - ), - else => unreachable, - }; - }, - else => |ty| return self.todo("implement cmp operation for '{s}' type", .{@tagName(ty)}), - } - - const info = self.arithmeticTypeInfo(scalar_ty); - const pred: CmpPredicate = switch (info.class) { - .composite_integer => unreachable, // TODO - .float => switch (op) { - .eq => .f_oeq, - .neq => .f_une, - .lt => .f_olt, - .lte => .f_ole, - .gt => .f_ogt, - .gte => .f_oge, - }, - .bool => switch (op) { - .eq => .l_eq, - .neq => .l_ne, - else => unreachable, - }, - .integer, .strange_integer => switch (info.signedness) { - .signed => switch (op) { - .eq => .i_eq, - .neq => .i_ne, - .lt => .s_lt, - .lte => .s_le, - .gt => .s_gt, - .gte => .s_ge, - }, - .unsigned => switch (op) { - .eq => .i_eq, - .neq => .i_ne, - .lt => .u_lt, - .lte => .u_le, - .gt => .u_gt, - .gte => .u_ge, - }, - }, - }; - - return try self.buildCmp(pred, lhs, rhs); - } - - fn airCmp( - self: *NavGen, - inst: Air.Inst.Index, - comptime op: std.math.CompareOperator, - ) !?Id { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const lhs = try self.temporary(bin_op.lhs); - const rhs = try self.temporary(bin_op.rhs); - - const result = try self.cmp(op, lhs, rhs); - return try result.materialize(self); - } - - fn airVectorCmp(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const vec_cmp = self.air.extraData(Air.VectorCmp, ty_pl.payload).data; - const lhs = try self.temporary(vec_cmp.lhs); - const rhs = try self.temporary(vec_cmp.rhs); - const op = vec_cmp.compareOperator(); - - const result = try self.cmp(op, lhs, rhs); - return try result.materialize(self); - } - - /// Bitcast one type to another. Note: both types, input, output are expected in **direct** representation. - fn bitCast( - self: *NavGen, - dst_ty: Type, - src_ty: Type, - src_id: Id, - ) !Id { - const zcu = self.pt.zcu; - const src_ty_id = try self.resolveType(src_ty, .direct); - const dst_ty_id = try self.resolveType(dst_ty, .direct); - - const result_id = blk: { - if (src_ty_id == dst_ty_id) break :blk src_id; - - // TODO: Some more cases are missing here - // See fn bitCast in llvm.zig - - if (src_ty.zigTypeTag(zcu) == .int and dst_ty.isPtrAtRuntime(zcu)) { - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ - .id_result_type = dst_ty_id, - .id_result = result_id, - .integer_value = src_id, - }); - break :blk result_id; - } - - // We can only use OpBitcast for specific conversions: between numerical types, and - // between pointers. If the resolved spir-v types fall into this category then emit OpBitcast, - // otherwise use a temporary and perform a pointer cast. - const can_bitcast = (src_ty.isNumeric(zcu) and dst_ty.isNumeric(zcu)) or (src_ty.isPtrAtRuntime(zcu) and dst_ty.isPtrAtRuntime(zcu)); - if (can_bitcast) { - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = dst_ty_id, - .id_result = result_id, - .operand = src_id, - }); - - break :blk result_id; - } - - const dst_ptr_ty_id = try self.ptrType(dst_ty, .function, .indirect); - - const tmp_id = try self.alloc(src_ty, .{ .storage_class = .function }); - try self.store(src_ty, tmp_id, src_id, .{}); - const casted_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = dst_ptr_ty_id, - .id_result = casted_ptr_id, - .operand = tmp_id, - }); - break :blk try self.load(dst_ty, casted_ptr_id, .{}); - }; - - // Because strange integers use sign-extended representation, we may need to normalize - // the result here. - // TODO: This detail could cause stuff like @as(*const i1, @ptrCast(&@as(u1, 1))) to break - // should we change the representation of strange integers? - if (dst_ty.zigTypeTag(zcu) == .int) { - const info = self.arithmeticTypeInfo(dst_ty); - const result = try self.normalize(Temporary.init(dst_ty, result_id), info); - return try result.materialize(self); - } - - return result_id; - } - - fn airBitCast(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_ty = self.typeOf(ty_op.operand); - const result_ty = self.typeOfIndex(inst); - if (operand_ty.toIntern() == .bool_type) { - const operand = try self.temporary(ty_op.operand); - const result = try self.intFromBool(operand); - return try result.materialize(self); - } - const operand_id = try self.resolve(ty_op.operand); - return try self.bitCast(result_ty, operand_ty, operand_id); - } - - fn airIntCast(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const src = try self.temporary(ty_op.operand); - const dst_ty = self.typeOfIndex(inst); - - const src_info = self.arithmeticTypeInfo(src.ty); - const dst_info = self.arithmeticTypeInfo(dst_ty); - - if (src_info.backing_bits == dst_info.backing_bits) { - return try src.materialize(self); - } - - const converted = try self.buildConvert(dst_ty, src); - - // Make sure to normalize the result if shrinking. - // Because strange ints are sign extended in their backing - // type, we don't need to normalize when growing the type. The - // representation is already the same. - const result = if (dst_info.bits < src_info.bits) - try self.normalize(converted, dst_info) - else - converted; - - return try result.materialize(self); - } - - fn intFromPtr(self: *NavGen, operand_id: Id) !Id { - const result_type_id = try self.resolveType(Type.usize, .direct); - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{ - .id_result_type = result_type_id, - .id_result = result_id, - .pointer = operand_id, - }); - return result_id; - } - - fn airFloatFromInt(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_ty = self.typeOf(ty_op.operand); - const operand_id = try self.resolve(ty_op.operand); - const result_ty = self.typeOfIndex(inst); - return try self.floatFromInt(result_ty, operand_ty, operand_id); - } - - fn floatFromInt(self: *NavGen, result_ty: Type, operand_ty: Type, operand_id: Id) !Id { - const operand_info = self.arithmeticTypeInfo(operand_ty); - const result_id = self.spv.allocId(); - const result_ty_id = try self.resolveType(result_ty, .direct); - switch (operand_info.signedness) { - .signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .signed_value = operand_id, - }), - .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .unsigned_value = operand_id, - }), - } - return result_id; - } - - fn airIntFromFloat(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_id = try self.resolve(ty_op.operand); - const result_ty = self.typeOfIndex(inst); - return try self.intFromFloat(result_ty, operand_id); - } - - fn intFromFloat(self: *NavGen, result_ty: Type, operand_id: Id) !Id { - const result_info = self.arithmeticTypeInfo(result_ty); - const result_ty_id = try self.resolveType(result_ty, .direct); - const result_id = self.spv.allocId(); - switch (result_info.signedness) { - .signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .float_value = operand_id, - }), - .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{ - .id_result_type = result_ty_id, - .id_result = result_id, - .float_value = operand_id, - }), - } - return result_id; - } - - fn airFloatCast(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand = try self.temporary(ty_op.operand); - const dest_ty = self.typeOfIndex(inst); - const result = try self.buildConvert(dest_ty, operand); - return try result.materialize(self); - } - - fn airNot(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand = try self.temporary(ty_op.operand); - const result_ty = self.typeOfIndex(inst); - const info = self.arithmeticTypeInfo(result_ty); - - const result = switch (info.class) { - .bool => try self.buildUnary(.l_not, operand), - .float => unreachable, - .composite_integer => unreachable, // TODO - .strange_integer, .integer => blk: { - const complement = try self.buildUnary(.bit_not, operand); - break :blk try self.normalize(complement, info); - }, - }; - - return try result.materialize(self); - } - - fn airArrayToSlice(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const array_ptr_ty = self.typeOf(ty_op.operand); - const array_ty = array_ptr_ty.childType(zcu); - const slice_ty = self.typeOfIndex(inst); - const elem_ptr_ty = slice_ty.slicePtrFieldType(zcu); - - const elem_ptr_ty_id = try self.resolveType(elem_ptr_ty, .direct); - - const array_ptr_id = try self.resolve(ty_op.operand); - const len_id = try self.constInt(Type.usize, array_ty.arrayLen(zcu)); - - const elem_ptr_id = if (!array_ty.hasRuntimeBitsIgnoreComptime(zcu)) - // Note: The pointer is something like *opaque{}, so we need to bitcast it to the element type. - try self.bitCast(elem_ptr_ty, array_ptr_ty, array_ptr_id) - else - // Convert the pointer-to-array to a pointer to the first element. - try self.accessChain(elem_ptr_ty_id, array_ptr_id, &.{0}); - - const slice_ty_id = try self.resolveType(slice_ty, .direct); - return try self.constructComposite(slice_ty_id, &.{ elem_ptr_id, len_id }); - } - - fn airSlice(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; - const ptr_id = try self.resolve(bin_op.lhs); - const len_id = try self.resolve(bin_op.rhs); - const slice_ty = self.typeOfIndex(inst); - const slice_ty_id = try self.resolveType(slice_ty, .direct); - return try self.constructComposite(slice_ty_id, &.{ ptr_id, len_id }); - } - - fn airAggregateInit(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const result_ty = self.typeOfIndex(inst); - const len: usize = @intCast(result_ty.arrayLen(zcu)); - const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra.items[ty_pl.payload..][0..len]); - - switch (result_ty.zigTypeTag(zcu)) { - .@"struct" => { - if (zcu.typeToPackedStruct(result_ty)) |struct_type| { - comptime assert(Type.packed_struct_layout_version == 2); - const backing_int_ty = Type.fromInterned(struct_type.backingIntTypeUnordered(ip)); - var running_int_id = try self.constInt(backing_int_ty, 0); - var running_bits: u16 = 0; - for (struct_type.field_types.get(ip), elements) |field_ty_ip, element| { - const field_ty = Type.fromInterned(field_ty_ip); - if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; - const field_id = try self.resolve(element); - const ty_bit_size: u16 = @intCast(field_ty.bitSize(zcu)); - const field_int_ty = try self.pt.intType(.unsigned, ty_bit_size); - const field_int_id = blk: { - if (field_ty.isPtrAtRuntime(zcu)) { - assert(self.spv.target.cpu.arch == .spirv64 and - field_ty.ptrAddressSpace(zcu) == .storage_buffer); - break :blk try self.intFromPtr(field_id); - } - break :blk try self.bitCast(field_int_ty, field_ty, field_id); - }; - const shift_rhs = try self.constInt(backing_int_ty, running_bits); - const extended_int_conv = try self.buildConvert(backing_int_ty, .{ - .ty = field_int_ty, - .value = .{ .singleton = field_int_id }, - }); - const shifted = try self.buildBinary(.sll, extended_int_conv, .{ - .ty = backing_int_ty, - .value = .{ .singleton = shift_rhs }, - }); - const running_int_tmp = try self.buildBinary( - .bit_or, - .{ .ty = backing_int_ty, .value = .{ .singleton = running_int_id } }, - shifted, - ); - running_int_id = try running_int_tmp.materialize(self); - running_bits += ty_bit_size; - } - return running_int_id; - } - - const types = try self.gpa.alloc(Type, elements.len); - defer self.gpa.free(types); - const constituents = try self.gpa.alloc(Id, elements.len); - defer self.gpa.free(constituents); - var index: usize = 0; - - switch (ip.indexToKey(result_ty.toIntern())) { - .tuple_type => |tuple| { - for (tuple.types.get(ip), elements, 0..) |field_ty, element, i| { - if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; - assert(Type.fromInterned(field_ty).hasRuntimeBits(zcu)); - - const id = try self.resolve(element); - types[index] = Type.fromInterned(field_ty); - constituents[index] = try self.convertToIndirect(Type.fromInterned(field_ty), id); - index += 1; - } - }, - .struct_type => { - const struct_type = ip.loadStructType(result_ty.toIntern()); - var it = struct_type.iterateRuntimeOrder(ip); - for (elements, 0..) |element, i| { - const field_index = it.next().?; - if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; - const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); - assert(field_ty.hasRuntimeBitsIgnoreComptime(zcu)); - - const id = try self.resolve(element); - types[index] = field_ty; - constituents[index] = try self.convertToIndirect(field_ty, id); - index += 1; - } - }, - else => unreachable, - } - - const result_ty_id = try self.resolveType(result_ty, .direct); - return try self.constructComposite(result_ty_id, constituents[0..index]); - }, - .vector => { - const n_elems = result_ty.vectorLen(zcu); - const elem_ids = try self.gpa.alloc(Id, n_elems); - defer self.gpa.free(elem_ids); - - for (elements, 0..) |element, i| { - elem_ids[i] = try self.resolve(element); - } - - const result_ty_id = try self.resolveType(result_ty, .direct); - return try self.constructComposite(result_ty_id, elem_ids); - }, - .array => { - const array_info = result_ty.arrayInfo(zcu); - const n_elems: usize = @intCast(result_ty.arrayLenIncludingSentinel(zcu)); - const elem_ids = try self.gpa.alloc(Id, n_elems); - defer self.gpa.free(elem_ids); - - for (elements, 0..) |element, i| { - const id = try self.resolve(element); - elem_ids[i] = try self.convertToIndirect(array_info.elem_type, id); - } - - if (array_info.sentinel) |sentinel_val| { - elem_ids[n_elems - 1] = try self.constant(array_info.elem_type, sentinel_val, .indirect); - } - - const result_ty_id = try self.resolveType(result_ty, .direct); - return try self.constructComposite(result_ty_id, elem_ids); - }, - else => unreachable, - } - } - - fn sliceOrArrayLen(self: *NavGen, operand_id: Id, ty: Type) !Id { - const pt = self.pt; - const zcu = pt.zcu; - switch (ty.ptrSize(zcu)) { - .slice => return self.extractField(Type.usize, operand_id, 1), - .one => { - const array_ty = ty.childType(zcu); - const elem_ty = array_ty.childType(zcu); - const abi_size = elem_ty.abiSize(zcu); - const size = array_ty.arrayLenIncludingSentinel(zcu) * abi_size; - return try self.constInt(Type.usize, size); - }, - .many, .c => unreachable, - } - } - - fn sliceOrArrayPtr(self: *NavGen, operand_id: Id, ty: Type) !Id { - const zcu = self.pt.zcu; - if (ty.isSlice(zcu)) { - const ptr_ty = ty.slicePtrFieldType(zcu); - return self.extractField(ptr_ty, operand_id, 0); - } - return operand_id; - } - - fn airMemcpy(self: *NavGen, inst: Air.Inst.Index) !void { - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const dest_slice = try self.resolve(bin_op.lhs); - const src_slice = try self.resolve(bin_op.rhs); - const dest_ty = self.typeOf(bin_op.lhs); - const src_ty = self.typeOf(bin_op.rhs); - const dest_ptr = try self.sliceOrArrayPtr(dest_slice, dest_ty); - const src_ptr = try self.sliceOrArrayPtr(src_slice, src_ty); - const len = try self.sliceOrArrayLen(dest_slice, dest_ty); - try self.func.body.emit(self.spv.gpa, .OpCopyMemorySized, .{ - .target = dest_ptr, - .source = src_ptr, - .size = len, - }); - } - - fn airMemmove(self: *NavGen, inst: Air.Inst.Index) !void { - _ = inst; - return self.fail("TODO implement airMemcpy for spirv", .{}); - } - - fn airSliceField(self: *NavGen, inst: Air.Inst.Index, field: u32) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const field_ty = self.typeOfIndex(inst); - const operand_id = try self.resolve(ty_op.operand); - return try self.extractField(field_ty, operand_id, field); - } - - fn airSliceElemPtr(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; - const slice_ty = self.typeOf(bin_op.lhs); - if (!slice_ty.isVolatilePtr(zcu) and self.liveness.isUnused(inst)) return null; - - const slice_id = try self.resolve(bin_op.lhs); - const index_id = try self.resolve(bin_op.rhs); - - const ptr_ty = self.typeOfIndex(inst); - const ptr_ty_id = try self.resolveType(ptr_ty, .direct); - - const slice_ptr = try self.extractField(ptr_ty, slice_id, 0); - return try self.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{}); - } - - fn airSliceElemVal(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const slice_ty = self.typeOf(bin_op.lhs); - if (!slice_ty.isVolatilePtr(zcu) and self.liveness.isUnused(inst)) return null; - - const slice_id = try self.resolve(bin_op.lhs); - const index_id = try self.resolve(bin_op.rhs); - - const ptr_ty = slice_ty.slicePtrFieldType(zcu); - const ptr_ty_id = try self.resolveType(ptr_ty, .direct); - - const slice_ptr = try self.extractField(ptr_ty, slice_id, 0); - const elem_ptr = try self.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{}); - return try self.load(slice_ty.childType(zcu), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(zcu) }); - } - - fn ptrElemPtr(self: *NavGen, ptr_ty: Type, ptr_id: Id, index_id: Id) !Id { - const zcu = self.pt.zcu; - // Construct new pointer type for the resulting pointer - const elem_ty = ptr_ty.elemType2(zcu); // use elemType() so that we get T for *[N]T. - const elem_ptr_ty_id = try self.ptrType(elem_ty, self.spvStorageClass(ptr_ty.ptrAddressSpace(zcu)), .indirect); - if (ptr_ty.isSinglePointer(zcu)) { - // Pointer-to-array. In this case, the resulting pointer is not of the same type - // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain. - return try self.accessChainId(elem_ptr_ty_id, ptr_id, &.{index_id}); - } else { - // Resulting pointer type is the same as the ptr_ty, so use ptrAccessChain - return try self.ptrAccessChain(elem_ptr_ty_id, ptr_id, index_id, &.{}); - } - } - - fn airPtrElemPtr(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; - const src_ptr_ty = self.typeOf(bin_op.lhs); - const elem_ty = src_ptr_ty.childType(zcu); - const ptr_id = try self.resolve(bin_op.lhs); - - if (!elem_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - const dst_ptr_ty = self.typeOfIndex(inst); - return try self.bitCast(dst_ptr_ty, src_ptr_ty, ptr_id); - } - - const index_id = try self.resolve(bin_op.rhs); - return try self.ptrElemPtr(src_ptr_ty, ptr_id, index_id); - } - - fn airArrayElemVal(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const array_ty = self.typeOf(bin_op.lhs); - const elem_ty = array_ty.childType(zcu); - const array_id = try self.resolve(bin_op.lhs); - const index_id = try self.resolve(bin_op.rhs); - - // SPIR-V doesn't have an array indexing function for some damn reason. - // For now, just generate a temporary and use that. - // TODO: This backend probably also should use isByRef from llvm... - - const is_vector = array_ty.isVector(zcu); - - const elem_repr: Repr = if (is_vector) .direct else .indirect; - const ptr_array_ty_id = try self.ptrType(array_ty, .function, .direct); - const ptr_elem_ty_id = try self.ptrType(elem_ty, .function, elem_repr); - - const tmp_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{ - .id_result_type = ptr_array_ty_id, - .id_result = tmp_id, - .storage_class = .function, - }); - - try self.func.body.emit(self.spv.gpa, .OpStore, .{ - .pointer = tmp_id, - .object = array_id, - }); - - const elem_ptr_id = try self.accessChainId(ptr_elem_ty_id, tmp_id, &.{index_id}); - - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpLoad, .{ - .id_result_type = try self.resolveType(elem_ty, elem_repr), - .id_result = result_id, - .pointer = elem_ptr_id, - }); - - if (is_vector) { - // Result is already in direct representation - return result_id; - } - - // This is an array type; the elements are stored in indirect representation. - // We have to convert the type to direct. - - return try self.convertToDirect(elem_ty, result_id); - } - - fn airPtrElemVal(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const ptr_ty = self.typeOf(bin_op.lhs); - const elem_ty = self.typeOfIndex(inst); - const ptr_id = try self.resolve(bin_op.lhs); - const index_id = try self.resolve(bin_op.rhs); - const elem_ptr_id = try self.ptrElemPtr(ptr_ty, ptr_id, index_id); - return try self.load(elem_ty, elem_ptr_id, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); - } - - fn airVectorStoreElem(self: *NavGen, inst: Air.Inst.Index) !void { - const zcu = self.pt.zcu; - const data = self.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; - const extra = self.air.extraData(Air.Bin, data.payload).data; - - const vector_ptr_ty = self.typeOf(data.vector_ptr); - const vector_ty = vector_ptr_ty.childType(zcu); - const scalar_ty = vector_ty.scalarType(zcu); - - const storage_class = self.spvStorageClass(vector_ptr_ty.ptrAddressSpace(zcu)); - const scalar_ptr_ty_id = try self.ptrType(scalar_ty, storage_class, .indirect); - - const vector_ptr = try self.resolve(data.vector_ptr); - const index = try self.resolve(extra.lhs); - const operand = try self.resolve(extra.rhs); - - const elem_ptr_id = try self.accessChainId(scalar_ptr_ty_id, vector_ptr, &.{index}); - try self.store(scalar_ty, elem_ptr_id, operand, .{ - .is_volatile = vector_ptr_ty.isVolatilePtr(zcu), - }); - } - - fn airSetUnionTag(self: *NavGen, inst: Air.Inst.Index) !void { - const zcu = self.pt.zcu; - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const un_ptr_ty = self.typeOf(bin_op.lhs); - const un_ty = un_ptr_ty.childType(zcu); - const layout = self.unionLayout(un_ty); - - if (layout.tag_size == 0) return; - - const tag_ty = un_ty.unionTagTypeSafety(zcu).?; - const tag_ptr_ty_id = try self.ptrType(tag_ty, self.spvStorageClass(un_ptr_ty.ptrAddressSpace(zcu)), .indirect); - - const union_ptr_id = try self.resolve(bin_op.lhs); - const new_tag_id = try self.resolve(bin_op.rhs); - - if (!layout.has_payload) { - try self.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(zcu) }); - } else { - const ptr_id = try self.accessChain(tag_ptr_ty_id, union_ptr_id, &.{layout.tag_index}); - try self.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(zcu) }); - } - } - - fn airGetUnionTag(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const un_ty = self.typeOf(ty_op.operand); - - const zcu = self.pt.zcu; - const layout = self.unionLayout(un_ty); - if (layout.tag_size == 0) return null; - - const union_handle = try self.resolve(ty_op.operand); - if (!layout.has_payload) return union_handle; - - const tag_ty = un_ty.unionTagTypeSafety(zcu).?; - return try self.extractField(tag_ty, union_handle, layout.tag_index); - } - - fn unionInit( - self: *NavGen, - ty: Type, - active_field: u32, - payload: ?Id, - ) !Id { - // To initialize a union, generate a temporary variable with the - // union type, then get the field pointer and pointer-cast it to the - // right type to store it. Finally load the entire union. - - // Note: The result here is not cached, because it generates runtime code. - - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const union_ty = zcu.typeToUnion(ty).?; - const tag_ty = Type.fromInterned(union_ty.enum_tag_ty); - - const layout = self.unionLayout(ty); - const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]); - - if (union_ty.flagsUnordered(ip).layout == .@"packed") { - if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - const int_ty = try pt.intType(.unsigned, @intCast(ty.bitSize(zcu))); - return self.constInt(int_ty, 0); - } - - assert(payload != null); - if (payload_ty.isInt(zcu)) { - if (ty.bitSize(zcu) == payload_ty.bitSize(zcu)) { - return self.bitCast(ty, payload_ty, payload.?); - } - - const trunc = try self.buildConvert(ty, .{ .ty = payload_ty, .value = .{ .singleton = payload.? } }); - return try trunc.materialize(self); - } - - const payload_int_ty = try pt.intType(.unsigned, @intCast(payload_ty.bitSize(zcu))); - const payload_int = if (payload_ty.ip_index == .bool_type) - try self.convertToIndirect(payload_ty, payload.?) - else - try self.bitCast(payload_int_ty, payload_ty, payload.?); - const trunc = try self.buildConvert(ty, .{ .ty = payload_int_ty, .value = .{ .singleton = payload_int } }); - return try trunc.materialize(self); - } - - const tag_int = if (layout.tag_size != 0) blk: { - const tag_val = try pt.enumValueFieldIndex(tag_ty, active_field); - const tag_int_val = try tag_val.intFromEnum(tag_ty, pt); - break :blk tag_int_val.toUnsignedInt(zcu); - } else 0; - - if (!layout.has_payload) { - return try self.constInt(tag_ty, tag_int); - } - - const tmp_id = try self.alloc(ty, .{ .storage_class = .function }); - - if (layout.tag_size != 0) { - const tag_ptr_ty_id = try self.ptrType(tag_ty, .function, .indirect); - const ptr_id = try self.accessChain(tag_ptr_ty_id, tmp_id, &.{@as(u32, @intCast(layout.tag_index))}); - const tag_id = try self.constInt(tag_ty, tag_int); - try self.store(tag_ty, ptr_id, tag_id, .{}); - } - - if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, .function, .indirect); - const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); - const active_pl_ptr_id = if (!layout.payload_ty.eql(payload_ty, zcu)) blk: { - const active_pl_ptr_ty_id = try self.ptrType(payload_ty, .function, .indirect); - const active_pl_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = active_pl_ptr_ty_id, - .id_result = active_pl_ptr_id, - .operand = pl_ptr_id, - }); - break :blk active_pl_ptr_id; - } else pl_ptr_id; - - try self.store(payload_ty, active_pl_ptr_id, payload.?, .{}); - } else { - assert(payload == null); - } - - // Just leave the padding fields uninitialized... - // TODO: Or should we initialize them with undef explicitly? - - return try self.load(ty, tmp_id, .{}); - } - - fn airUnionInit(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ip = &zcu.intern_pool; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; - const ty = self.typeOfIndex(inst); - - const union_obj = zcu.typeToUnion(ty).?; - const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]); - const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(zcu)) - try self.resolve(extra.init) - else - null; - return try self.unionInit(ty, extra.field_index, payload); - } - - fn airStructFieldVal(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; - - const object_ty = self.typeOf(struct_field.struct_operand); - const object_id = try self.resolve(struct_field.struct_operand); - const field_index = struct_field.field_index; - const field_ty = object_ty.fieldType(field_index, zcu); - - if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) return null; - - switch (object_ty.zigTypeTag(zcu)) { - .@"struct" => switch (object_ty.containerLayout(zcu)) { - .@"packed" => { - const struct_ty = zcu.typeToPackedStruct(object_ty).?; - const bit_offset = zcu.structPackedFieldBitOffset(struct_ty, field_index); - const bit_offset_id = try self.constInt(.u16, bit_offset); - const signedness = if (field_ty.isInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned; - const field_bit_size: u16 = @intCast(field_ty.bitSize(zcu)); - const field_int_ty = try pt.intType(signedness, field_bit_size); - const shift_lhs: Temporary = .{ .ty = object_ty, .value = .{ .singleton = object_id } }; - const shift = try self.buildBinary(.srl, shift_lhs, .{ .ty = .u16, .value = .{ .singleton = bit_offset_id } }); - const mask_id = try self.constInt(object_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1); - const masked = try self.buildBinary(.bit_and, shift, .{ .ty = object_ty, .value = .{ .singleton = mask_id } }); - const result_id = blk: { - if (self.backingIntBits(field_bit_size).@"0" == self.backingIntBits(@intCast(object_ty.bitSize(zcu))).@"0") - break :blk try self.bitCast(field_int_ty, object_ty, try masked.materialize(self)); - const trunc = try self.buildConvert(field_int_ty, masked); - break :blk try trunc.materialize(self); - }; - if (field_ty.ip_index == .bool_type) return try self.convertToDirect(.bool, result_id); - if (field_ty.isInt(zcu)) return result_id; - return try self.bitCast(field_ty, field_int_ty, result_id); - }, - else => return try self.extractField(field_ty, object_id, field_index), - }, - .@"union" => switch (object_ty.containerLayout(zcu)) { - .@"packed" => { - const backing_int_ty = try pt.intType(.unsigned, @intCast(object_ty.bitSize(zcu))); - const signedness = if (field_ty.isInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned; - const field_bit_size: u16 = @intCast(field_ty.bitSize(zcu)); - const int_ty = try pt.intType(signedness, field_bit_size); - const mask_id = try self.constInt(backing_int_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1); - const masked = try self.buildBinary( - .bit_and, - .{ .ty = backing_int_ty, .value = .{ .singleton = object_id } }, - .{ .ty = backing_int_ty, .value = .{ .singleton = mask_id } }, - ); - const result_id = blk: { - if (self.backingIntBits(field_bit_size).@"0" == self.backingIntBits(@intCast(backing_int_ty.bitSize(zcu))).@"0") - break :blk try self.bitCast(int_ty, backing_int_ty, try masked.materialize(self)); - const trunc = try self.buildConvert(int_ty, masked); - break :blk try trunc.materialize(self); - }; - if (field_ty.ip_index == .bool_type) return try self.convertToDirect(.bool, result_id); - if (field_ty.isInt(zcu)) return result_id; - return try self.bitCast(field_ty, int_ty, result_id); - }, - else => { - // Store, ptr-elem-ptr, pointer-cast, load - const layout = self.unionLayout(object_ty); - assert(layout.has_payload); - - const tmp_id = try self.alloc(object_ty, .{ .storage_class = .function }); - try self.store(object_ty, tmp_id, object_id, .{}); - - const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, .function, .indirect); - const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); - - const active_pl_ptr_ty_id = try self.ptrType(field_ty, .function, .indirect); - const active_pl_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = active_pl_ptr_ty_id, - .id_result = active_pl_ptr_id, - .operand = pl_ptr_id, - }); - return try self.load(field_ty, active_pl_ptr_id, .{}); - }, - }, - else => unreachable, - } - } - - fn airFieldParentPtr(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; - - const parent_ty = ty_pl.ty.toType().childType(zcu); - const result_ty_id = try self.resolveType(ty_pl.ty.toType(), .indirect); - - const field_ptr = try self.resolve(extra.field_ptr); - const field_ptr_int = try self.intFromPtr(field_ptr); - const field_offset = parent_ty.structFieldOffset(extra.field_index, zcu); - - const base_ptr_int = base_ptr_int: { - if (field_offset == 0) break :base_ptr_int field_ptr_int; - - const field_offset_id = try self.constInt(Type.usize, field_offset); - const field_ptr_tmp = Temporary.init(Type.usize, field_ptr_int); - const field_offset_tmp = Temporary.init(Type.usize, field_offset_id); - const result = try self.buildBinary(.i_sub, field_ptr_tmp, field_offset_tmp); - break :base_ptr_int try result.materialize(self); - }; - - const base_ptr = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ - .id_result_type = result_ty_id, - .id_result = base_ptr, - .integer_value = base_ptr_int, - }); - - return base_ptr; - } - - fn structFieldPtr( - self: *NavGen, - result_ptr_ty: Type, - object_ptr_ty: Type, - object_ptr: Id, - field_index: u32, - ) !Id { - const result_ty_id = try self.resolveType(result_ptr_ty, .direct); - - const zcu = self.pt.zcu; - const object_ty = object_ptr_ty.childType(zcu); - switch (object_ty.zigTypeTag(zcu)) { - .pointer => { - assert(object_ty.isSlice(zcu)); - return self.accessChain(result_ty_id, object_ptr, &.{field_index}); - }, - .@"struct" => switch (object_ty.containerLayout(zcu)) { - .@"packed" => return self.todo("implement field access for packed structs", .{}), - else => { - return try self.accessChain(result_ty_id, object_ptr, &.{field_index}); - }, - }, - .@"union" => { - const layout = self.unionLayout(object_ty); - if (!layout.has_payload) { - // Asked to get a pointer to a zero-sized field. Just lower this - // to undefined, there is no reason to make it be a valid pointer. - return try self.spv.constUndef(result_ty_id); - } - - const storage_class = self.spvStorageClass(object_ptr_ty.ptrAddressSpace(zcu)); - const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, storage_class, .indirect); - const pl_ptr_id = blk: { - if (object_ty.containerLayout(zcu) == .@"packed") break :blk object_ptr; - break :blk try self.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index}); - }; - - const active_pl_ptr_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ - .id_result_type = result_ty_id, - .id_result = active_pl_ptr_id, - .operand = pl_ptr_id, - }); - return active_pl_ptr_id; - }, - else => unreachable, - } - } - - fn airStructFieldPtrIndex(self: *NavGen, inst: Air.Inst.Index, field_index: u32) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const struct_ptr = try self.resolve(ty_op.operand); - const struct_ptr_ty = self.typeOf(ty_op.operand); - const result_ptr_ty = self.typeOfIndex(inst); - return try self.structFieldPtr(result_ptr_ty, struct_ptr_ty, struct_ptr, field_index); - } - - const AllocOptions = struct { - initializer: ?Id = null, - /// The final storage class of the pointer. This may be either `.Generic` or `.Function`. - /// In either case, the local is allocated in the `.Function` storage class, and optionally - /// cast back to `.Generic`. - storage_class: StorageClass, - }; - - // Allocate a function-local variable, with possible initializer. - // This function returns a pointer to a variable of type `ty`, - // which is in the Generic address space. The variable is actually - // placed in the Function address space. - fn alloc( - self: *NavGen, - ty: Type, - options: AllocOptions, - ) !Id { - const ptr_fn_ty_id = try self.ptrType(ty, .function, .indirect); - - // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to - // directly generate them into func.prologue instead of the body. - const var_id = self.spv.allocId(); - try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{ - .id_result_type = ptr_fn_ty_id, - .id_result = var_id, - .storage_class = .function, - .initializer = options.initializer, - }); - - switch (self.spv.target.os.tag) { - .vulkan, .opengl => return var_id, - else => {}, - } - - switch (options.storage_class) { - .generic => { - const ptr_gn_ty_id = try self.ptrType(ty, .generic, .indirect); - // Convert to a generic pointer - return self.castToGeneric(ptr_gn_ty_id, var_id); - }, - .function => return var_id, - else => unreachable, - } - } - - fn airAlloc(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const ptr_ty = self.typeOfIndex(inst); - const child_ty = ptr_ty.childType(zcu); - return try self.alloc(child_ty, .{ - .storage_class = self.spvStorageClass(ptr_ty.ptrAddressSpace(zcu)), - }); - } - - fn airArg(self: *NavGen) Id { - defer self.next_arg_index += 1; - return self.args.items[self.next_arg_index]; - } - - /// Given a slice of incoming block connections, returns the block-id of the next - /// block to jump to. This function emits instructions, so it should be emitted - /// inside the merge block of the block. - /// This function should only be called with structured control flow generation. - fn structuredNextBlock(self: *NavGen, incoming: []const ControlFlow.Structured.Block.Incoming) !Id { - assert(self.control_flow == .structured); - - const result_id = self.spv.allocId(); - const block_id_ty_id = try self.resolveType(Type.u32, .direct); - try self.func.body.emitRaw(self.spv.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent... - self.func.body.writeOperand(spec.Id, block_id_ty_id); - self.func.body.writeOperand(spec.Id, result_id); - - for (incoming) |incoming_block| { - self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming_block.next_block, incoming_block.src_label }); - } - - return result_id; - } - - /// Jumps to the block with the target block-id. This function must only be called when - /// terminating a body, there should be no instructions after it. - /// This function should only be called with structured control flow generation. - fn structuredBreak(self: *NavGen, target_block: Id) !void { - assert(self.control_flow == .structured); - - const sblock = self.control_flow.structured.block_stack.getLast(); - const merge_block = switch (sblock.*) { - .selection => |*merge| blk: { - const merge_label = self.spv.allocId(); - try merge.merge_stack.append(self.gpa, .{ - .incoming = .{ - .src_label = self.current_block_label, - .next_block = target_block, - }, - .merge_block = merge_label, - }); - break :blk merge_label; - }, - // Loop blocks do not end in a break. Not through a direct break, - // and also not through another instruction like cond_br or unreachable (these - // situations are replaced by `cond_br` in sema, or there is a `block` instruction - // placed around them). - .loop => unreachable, - }; - - try self.func.body.emitBranch(self.spv.gpa, merge_block); - } - - /// Generate a body in a way that exits the body using only structured constructs. - /// Returns the block-id of the next block to jump to. After this function, a jump - /// should still be emitted to the block that should follow this structured body. - /// This function should only be called with structured control flow generation. - fn genStructuredBody( - self: *NavGen, - /// This parameter defines the method that this structured body is exited with. - block_merge_type: union(enum) { - /// Using selection; early exits from this body are surrounded with - /// if() statements. - selection, - /// Using loops; loops can be early exited by jumping to the merge block at - /// any time. - loop: struct { - merge_label: Id, - continue_label: Id, - }, - }, - body: []const Air.Inst.Index, - ) !Id { - assert(self.control_flow == .structured); - - var sblock: ControlFlow.Structured.Block = switch (block_merge_type) { - .loop => |merge| .{ .loop = .{ - .merge_block = merge.merge_label, - } }, - .selection => .{ .selection = .{} }, - }; - defer sblock.deinit(self.gpa); - - { - try self.control_flow.structured.block_stack.append(self.gpa, &sblock); - defer _ = self.control_flow.structured.block_stack.pop(); - - try self.genBody(body); - } - - switch (sblock) { - .selection => |merge| { - // Now generate the merge block for all merges that - // still need to be performed. - const merge_stack = merge.merge_stack.items; - - // If no merges on the stack, this block didn't generate any jumps (all paths - // ended with a return or an unreachable). In that case, we don't need to do - // any merging. - if (merge_stack.len == 0) { - // We still need to return a value of a next block to jump to. - // For example, if we have code like - // if (x) { - // if (y) return else return; - // } else {} - // then we still need the outer to have an OpSelectionMerge and consequently - // a phi node. In that case we can just return bogus, since we know that its - // path will never be taken. - - // Make sure that we are still in a block when exiting the function. - // TODO: Can we get rid of that? - try self.beginSpvBlock(self.spv.allocId()); - const block_id_ty_id = try self.resolveType(Type.u32, .direct); - return try self.spv.constUndef(block_id_ty_id); - } - - // The top-most merge actually only has a single source, the - // final jump of the block, or the merge block of a sub-block, cond_br, - // or loop. Therefore we just need to generate a block with a jump to the - // next merge block. - try self.beginSpvBlock(merge_stack[merge_stack.len - 1].merge_block); - - // Now generate a merge ladder for the remaining merges in the stack. - var incoming = ControlFlow.Structured.Block.Incoming{ - .src_label = self.current_block_label, - .next_block = merge_stack[merge_stack.len - 1].incoming.next_block, - }; - var i = merge_stack.len - 1; - while (i > 0) { - i -= 1; - const step = merge_stack[i]; - try self.func.body.emitBranch(self.spv.gpa, step.merge_block); - try self.beginSpvBlock(step.merge_block); - const next_block = try self.structuredNextBlock(&.{ incoming, step.incoming }); - incoming = .{ - .src_label = step.merge_block, - .next_block = next_block, - }; - } - - return incoming.next_block; - }, - .loop => |merge| { - // Close the loop by jumping to the continue label - try self.func.body.emitBranch(self.spv.gpa, block_merge_type.loop.continue_label); - // For blocks we must simple merge all the incoming blocks to get the next block. - try self.beginSpvBlock(merge.merge_block); - return try self.structuredNextBlock(merge.merges.items); - }, - } - } - - fn airBlock(self: *NavGen, inst: Air.Inst.Index) !?Id { - const inst_datas = self.air.instructions.items(.data); - const extra = self.air.extraData(Air.Block, inst_datas[@intFromEnum(inst)].ty_pl.payload); - return self.lowerBlock(inst, @ptrCast(self.air.extra.items[extra.end..][0..extra.data.body_len])); - } - - fn lowerBlock(self: *NavGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) !?Id { - // In AIR, a block doesn't really define an entry point like a block, but - // more like a scope that breaks can jump out of and "return" a value from. - // This cannot be directly modelled in SPIR-V, so in a block instruction, - // we're going to split up the current block by first generating the code - // of the block, then a label, and then generate the rest of the current - // ir.Block in a different SPIR-V block. - - const pt = self.pt; - const zcu = pt.zcu; - const ty = self.typeOfIndex(inst); - const have_block_result = ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu); - - const cf = switch (self.control_flow) { - .structured => |*cf| cf, - .unstructured => |*cf| { - var block = ControlFlow.Unstructured.Block{}; - defer block.incoming_blocks.deinit(self.gpa); - - // 4 chosen as arbitrary initial capacity. - try block.incoming_blocks.ensureUnusedCapacity(self.gpa, 4); - - try cf.blocks.putNoClobber(self.gpa, inst, &block); - defer assert(cf.blocks.remove(inst)); - - try self.genBody(body); - - // Only begin a new block if there were actually any breaks towards it. - if (block.label) |label| { - try self.beginSpvBlock(label); - } - - if (!have_block_result) - return null; - - assert(block.label != null); - const result_id = self.spv.allocId(); - const result_type_id = try self.resolveType(ty, .direct); - - try self.func.body.emitRaw( - self.spv.gpa, - .OpPhi, - // result type + result + variable/parent... - 2 + @as(u16, @intCast(block.incoming_blocks.items.len * 2)), - ); - self.func.body.writeOperand(spec.Id, result_type_id); - self.func.body.writeOperand(spec.Id, result_id); - - for (block.incoming_blocks.items) |incoming| { - self.func.body.writeOperand( - spec.PairIdRefIdRef, - .{ incoming.break_value_id, incoming.src_label }, - ); - } - - return result_id; - }, - }; - - const maybe_block_result_var_id = if (have_block_result) blk: { - const block_result_var_id = try self.alloc(ty, .{ .storage_class = .function }); - try cf.block_results.putNoClobber(self.gpa, inst, block_result_var_id); - break :blk block_result_var_id; - } else null; - defer if (have_block_result) assert(cf.block_results.remove(inst)); - - const next_block = try self.genStructuredBody(.selection, body); - - // When encountering a block instruction, we are always at least in the function's scope, - // so there always has to be another entry. - assert(cf.block_stack.items.len > 0); - - // Check if the target of the branch was this current block. - const this_block = try self.constInt(Type.u32, @intFromEnum(inst)); - const jump_to_this_block_id = self.spv.allocId(); - const bool_ty_id = try self.resolveType(Type.bool, .direct); - try self.func.body.emit(self.spv.gpa, .OpIEqual, .{ - .id_result_type = bool_ty_id, - .id_result = jump_to_this_block_id, - .operand_1 = next_block, - .operand_2 = this_block, - }); - - const sblock = cf.block_stack.getLast(); - - if (ty.isNoReturn(zcu)) { - // If this block is noreturn, this instruction is the last of a block, - // and we must simply jump to the block's merge unconditionally. - try self.structuredBreak(next_block); - } else { - switch (sblock.*) { - .selection => |*merge| { - // To jump out of a selection block, push a new entry onto its merge stack and - // generate a conditional branch to there and to the instructions following this block. - const merge_label = self.spv.allocId(); - const then_label = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ - .merge_block = merge_label, - .selection_control = .{}, - }); - try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ - .condition = jump_to_this_block_id, - .true_label = then_label, - .false_label = merge_label, - }); - try merge.merge_stack.append(self.gpa, .{ - .incoming = .{ - .src_label = self.current_block_label, - .next_block = next_block, - }, - .merge_block = merge_label, - }); - - try self.beginSpvBlock(then_label); - }, - .loop => |*merge| { - // To jump out of a loop block, generate a conditional that exits the block - // to the loop merge if the target ID is not the one of this block. - const continue_label = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ - .condition = jump_to_this_block_id, - .true_label = continue_label, - .false_label = merge.merge_block, - }); - try merge.merges.append(self.gpa, .{ - .src_label = self.current_block_label, - .next_block = next_block, - }); - try self.beginSpvBlock(continue_label); - }, - } - } - - if (maybe_block_result_var_id) |block_result_var_id| { - return try self.load(ty, block_result_var_id, .{}); - } - - return null; - } - - fn airBr(self: *NavGen, inst: Air.Inst.Index) !void { - const zcu = self.pt.zcu; - const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br; - const operand_ty = self.typeOf(br.operand); - - switch (self.control_flow) { - .structured => |*cf| { - if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { - const operand_id = try self.resolve(br.operand); - const block_result_var_id = cf.block_results.get(br.block_inst).?; - try self.store(operand_ty, block_result_var_id, operand_id, .{}); - } - - const next_block = try self.constInt(Type.u32, @intFromEnum(br.block_inst)); - try self.structuredBreak(next_block); - }, - .unstructured => |cf| { - const block = cf.blocks.get(br.block_inst).?; - if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { - const operand_id = try self.resolve(br.operand); - // current_block_label should not be undefined here, lest there - // is a br or br_void in the function's body. - try block.incoming_blocks.append(self.gpa, .{ - .src_label = self.current_block_label, - .break_value_id = operand_id, - }); - } - - if (block.label == null) { - block.label = self.spv.allocId(); - } - - try self.func.body.emitBranch(self.spv.gpa, block.label.?); - }, - } - } - - fn airCondBr(self: *NavGen, inst: Air.Inst.Index) !void { - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const cond_br = self.air.extraData(Air.CondBr, pl_op.payload); - const then_body: []const Air.Inst.Index = @ptrCast(self.air.extra.items[cond_br.end..][0..cond_br.data.then_body_len]); - const else_body: []const Air.Inst.Index = @ptrCast(self.air.extra.items[cond_br.end + then_body.len ..][0..cond_br.data.else_body_len]); - const condition_id = try self.resolve(pl_op.operand); - - const then_label = self.spv.allocId(); - const else_label = self.spv.allocId(); - - switch (self.control_flow) { - .structured => { - const merge_label = self.spv.allocId(); - - try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ - .merge_block = merge_label, - .selection_control = .{}, - }); - try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ - .condition = condition_id, - .true_label = then_label, - .false_label = else_label, - }); - - try self.beginSpvBlock(then_label); - const then_next = try self.genStructuredBody(.selection, then_body); - const then_incoming = ControlFlow.Structured.Block.Incoming{ - .src_label = self.current_block_label, - .next_block = then_next, - }; - try self.func.body.emitBranch(self.spv.gpa, merge_label); - - try self.beginSpvBlock(else_label); - const else_next = try self.genStructuredBody(.selection, else_body); - const else_incoming = ControlFlow.Structured.Block.Incoming{ - .src_label = self.current_block_label, - .next_block = else_next, - }; - try self.func.body.emitBranch(self.spv.gpa, merge_label); - - try self.beginSpvBlock(merge_label); - const next_block = try self.structuredNextBlock(&.{ then_incoming, else_incoming }); - - try self.structuredBreak(next_block); - }, - .unstructured => { - try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ - .condition = condition_id, - .true_label = then_label, - .false_label = else_label, - }); - - try self.beginSpvBlock(then_label); - try self.genBody(then_body); - try self.beginSpvBlock(else_label); - try self.genBody(else_body); - }, - } - } - - fn airLoop(self: *NavGen, inst: Air.Inst.Index) !void { - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const loop = self.air.extraData(Air.Block, ty_pl.payload); - const body: []const Air.Inst.Index = @ptrCast(self.air.extra.items[loop.end..][0..loop.data.body_len]); - - const body_label = self.spv.allocId(); - - switch (self.control_flow) { - .structured => { - const header_label = self.spv.allocId(); - const merge_label = self.spv.allocId(); - const continue_label = self.spv.allocId(); - - // The back-edge must point to the loop header, so generate a separate block for the - // loop header so that we don't accidentally include some instructions from there - // in the loop. - try self.func.body.emitBranch(self.spv.gpa, header_label); - try self.beginSpvBlock(header_label); - - // Emit loop header and jump to loop body - try self.func.body.emit(self.spv.gpa, .OpLoopMerge, .{ - .merge_block = merge_label, - .continue_target = continue_label, - .loop_control = .{}, - }); - try self.func.body.emitBranch(self.spv.gpa, body_label); - - try self.beginSpvBlock(body_label); - - const next_block = try self.genStructuredBody(.{ .loop = .{ - .merge_label = merge_label, - .continue_label = continue_label, - } }, body); - try self.structuredBreak(next_block); - - try self.beginSpvBlock(continue_label); - try self.func.body.emitBranch(self.spv.gpa, header_label); - }, - .unstructured => { - try self.func.body.emitBranch(self.spv.gpa, body_label); - try self.beginSpvBlock(body_label); - try self.genBody(body); - try self.func.body.emitBranch(self.spv.gpa, body_label); - }, - } - } - - fn airLoad(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const ptr_ty = self.typeOf(ty_op.operand); - const elem_ty = self.typeOfIndex(inst); - const operand = try self.resolve(ty_op.operand); - if (!ptr_ty.isVolatilePtr(zcu) and self.liveness.isUnused(inst)) return null; - - return try self.load(elem_ty, operand, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); - } - - fn airStore(self: *NavGen, inst: Air.Inst.Index) !void { - const zcu = self.pt.zcu; - const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; - const ptr_ty = self.typeOf(bin_op.lhs); - const elem_ty = ptr_ty.childType(zcu); - const ptr = try self.resolve(bin_op.lhs); - const value = try self.resolve(bin_op.rhs); - - try self.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); - } - - fn airRet(self: *NavGen, inst: Air.Inst.Index) !void { - const pt = self.pt; - const zcu = pt.zcu; - const operand = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; - const ret_ty = self.typeOf(operand); - if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - const fn_info = zcu.typeToFunc(zcu.navValue(self.owner_nav).typeOf(zcu)).?; - if (Type.fromInterned(fn_info.return_type).isError(zcu)) { - // Functions with an empty error set are emitted with an error code - // return type and return zero so they can be function pointers coerced - // to functions that return anyerror. - const no_err_id = try self.constInt(Type.anyerror, 0); - return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id }); - } else { - return try self.func.body.emit(self.spv.gpa, .OpReturn, {}); - } - } - - const operand_id = try self.resolve(operand); - try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = operand_id }); - } - - fn airRetLoad(self: *NavGen, inst: Air.Inst.Index) !void { - const pt = self.pt; - const zcu = pt.zcu; - const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; - const ptr_ty = self.typeOf(un_op); - const ret_ty = ptr_ty.childType(zcu); - - if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - const fn_info = zcu.typeToFunc(zcu.navValue(self.owner_nav).typeOf(zcu)).?; - if (Type.fromInterned(fn_info.return_type).isError(zcu)) { - // Functions with an empty error set are emitted with an error code - // return type and return zero so they can be function pointers coerced - // to functions that return anyerror. - const no_err_id = try self.constInt(Type.anyerror, 0); - return try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ .value = no_err_id }); - } else { - return try self.func.body.emit(self.spv.gpa, .OpReturn, {}); - } - } - - const ptr = try self.resolve(un_op); - const value = try self.load(ret_ty, ptr, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); - try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ - .value = value, - }); - } - - fn airTry(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const err_union_id = try self.resolve(pl_op.operand); - const extra = self.air.extraData(Air.Try, pl_op.payload); - const body: []const Air.Inst.Index = @ptrCast(self.air.extra.items[extra.end..][0..extra.data.body_len]); - - const err_union_ty = self.typeOf(pl_op.operand); - const payload_ty = self.typeOfIndex(inst); - - const bool_ty_id = try self.resolveType(Type.bool, .direct); - - const eu_layout = self.errorUnionLayout(payload_ty); - - if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { - const err_id = if (eu_layout.payload_has_bits) - try self.extractField(Type.anyerror, err_union_id, eu_layout.errorFieldIndex()) - else - err_union_id; - - const zero_id = try self.constInt(Type.anyerror, 0); - const is_err_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{ - .id_result_type = bool_ty_id, - .id_result = is_err_id, - .operand_1 = err_id, - .operand_2 = zero_id, - }); - - // When there is an error, we must evaluate `body`. Otherwise we must continue - // with the current body. - // Just generate a new block here, then generate a new block inline for the remainder of the body. - - const err_block = self.spv.allocId(); - const ok_block = self.spv.allocId(); - - switch (self.control_flow) { - .structured => { - // According to AIR documentation, this block is guaranteed - // to not break and end in a return instruction. Thus, - // for structured control flow, we can just naively use - // the ok block as the merge block here. - try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ - .merge_block = ok_block, - .selection_control = .{}, - }); - }, - .unstructured => {}, - } - - try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ - .condition = is_err_id, - .true_label = err_block, - .false_label = ok_block, - }); - - try self.beginSpvBlock(err_block); - try self.genBody(body); - - try self.beginSpvBlock(ok_block); - } - - if (!eu_layout.payload_has_bits) { - return null; - } - - // Now just extract the payload, if required. - return try self.extractField(payload_ty, err_union_id, eu_layout.payloadFieldIndex()); - } - - fn airErrUnionErr(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_id = try self.resolve(ty_op.operand); - const err_union_ty = self.typeOf(ty_op.operand); - const err_ty_id = try self.resolveType(Type.anyerror, .direct); - - if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { - // No error possible, so just return undefined. - return try self.spv.constUndef(err_ty_id); - } - - const payload_ty = err_union_ty.errorUnionPayload(zcu); - const eu_layout = self.errorUnionLayout(payload_ty); - - if (!eu_layout.payload_has_bits) { - // If no payload, error union is represented by error set. - return operand_id; - } - - return try self.extractField(Type.anyerror, operand_id, eu_layout.errorFieldIndex()); - } - - fn airErrUnionPayload(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_id = try self.resolve(ty_op.operand); - const payload_ty = self.typeOfIndex(inst); - const eu_layout = self.errorUnionLayout(payload_ty); - - if (!eu_layout.payload_has_bits) { - return null; // No error possible. - } - - return try self.extractField(payload_ty, operand_id, eu_layout.payloadFieldIndex()); - } - - fn airWrapErrUnionErr(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const err_union_ty = self.typeOfIndex(inst); - const payload_ty = err_union_ty.errorUnionPayload(zcu); - const operand_id = try self.resolve(ty_op.operand); - const eu_layout = self.errorUnionLayout(payload_ty); - - if (!eu_layout.payload_has_bits) { - return operand_id; - } - - const payload_ty_id = try self.resolveType(payload_ty, .indirect); - - var members: [2]Id = undefined; - members[eu_layout.errorFieldIndex()] = operand_id; - members[eu_layout.payloadFieldIndex()] = try self.spv.constUndef(payload_ty_id); - - var types: [2]Type = undefined; - types[eu_layout.errorFieldIndex()] = Type.anyerror; - types[eu_layout.payloadFieldIndex()] = payload_ty; - - const err_union_ty_id = try self.resolveType(err_union_ty, .direct); - return try self.constructComposite(err_union_ty_id, &members); - } - - fn airWrapErrUnionPayload(self: *NavGen, inst: Air.Inst.Index) !?Id { - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const err_union_ty = self.typeOfIndex(inst); - const operand_id = try self.resolve(ty_op.operand); - const payload_ty = self.typeOf(ty_op.operand); - const eu_layout = self.errorUnionLayout(payload_ty); - - if (!eu_layout.payload_has_bits) { - return try self.constInt(Type.anyerror, 0); - } - - var members: [2]Id = undefined; - members[eu_layout.errorFieldIndex()] = try self.constInt(Type.anyerror, 0); - members[eu_layout.payloadFieldIndex()] = try self.convertToIndirect(payload_ty, operand_id); - - var types: [2]Type = undefined; - types[eu_layout.errorFieldIndex()] = Type.anyerror; - types[eu_layout.payloadFieldIndex()] = payload_ty; - - const err_union_ty_id = try self.resolveType(err_union_ty, .direct); - return try self.constructComposite(err_union_ty_id, &members); - } - - fn airIsNull(self: *NavGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { is_null, is_non_null }) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; - const operand_id = try self.resolve(un_op); - const operand_ty = self.typeOf(un_op); - const optional_ty = if (is_pointer) operand_ty.childType(zcu) else operand_ty; - const payload_ty = optional_ty.optionalChild(zcu); - - const bool_ty_id = try self.resolveType(Type.bool, .direct); - - if (optional_ty.optionalReprIsPayload(zcu)) { - // Pointer payload represents nullability: pointer or slice. - const loaded_id = if (is_pointer) - try self.load(optional_ty, operand_id, .{}) - else - operand_id; - - const ptr_ty = if (payload_ty.isSlice(zcu)) - payload_ty.slicePtrFieldType(zcu) - else - payload_ty; - - const ptr_id = if (payload_ty.isSlice(zcu)) - try self.extractField(ptr_ty, loaded_id, 0) - else - loaded_id; - - const ptr_ty_id = try self.resolveType(ptr_ty, .direct); - const null_id = try self.spv.constNull(ptr_ty_id); - const null_tmp = Temporary.init(ptr_ty, null_id); - const ptr = Temporary.init(ptr_ty, ptr_id); - - const op: std.math.CompareOperator = switch (pred) { - .is_null => .eq, - .is_non_null => .neq, - }; - const result = try self.cmp(op, ptr, null_tmp); - return try result.materialize(self); - } - - const is_non_null_id = blk: { - if (is_pointer) { - if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - const storage_class = self.spvStorageClass(operand_ty.ptrAddressSpace(zcu)); - const bool_ptr_ty_id = try self.ptrType(Type.bool, storage_class, .indirect); - const tag_ptr_id = try self.accessChain(bool_ptr_ty_id, operand_id, &.{1}); - break :blk try self.load(Type.bool, tag_ptr_id, .{}); - } - - break :blk try self.load(Type.bool, operand_id, .{}); - } - - break :blk if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) - try self.extractField(Type.bool, operand_id, 1) - else - // Optional representation is bool indicating whether the optional is set - // Optionals with no payload are represented as an (indirect) bool, so convert - // it back to the direct bool here. - try self.convertToDirect(Type.bool, operand_id); - }; - - return switch (pred) { - .is_null => blk: { - // Invert condition - const result_id = self.spv.allocId(); - try self.func.body.emit(self.spv.gpa, .OpLogicalNot, .{ - .id_result_type = bool_ty_id, - .id_result = result_id, - .operand = is_non_null_id, - }); - break :blk result_id; - }, - .is_non_null => is_non_null_id, - }; - } - - fn airIsErr(self: *NavGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err }) !?Id { - const zcu = self.pt.zcu; - const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; - const operand_id = try self.resolve(un_op); - const err_union_ty = self.typeOf(un_op); - - if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { - return try self.constBool(pred == .is_non_err, .direct); - } - - const payload_ty = err_union_ty.errorUnionPayload(zcu); - const eu_layout = self.errorUnionLayout(payload_ty); - const bool_ty_id = try self.resolveType(Type.bool, .direct); - - const error_id = if (!eu_layout.payload_has_bits) - operand_id - else - try self.extractField(Type.anyerror, operand_id, eu_layout.errorFieldIndex()); - - const result_id = self.spv.allocId(); - switch (pred) { - inline else => |pred_ct| try self.func.body.emit( - self.spv.gpa, - switch (pred_ct) { - .is_err => .OpINotEqual, - .is_non_err => .OpIEqual, - }, - .{ - .id_result_type = bool_ty_id, - .id_result = result_id, - .operand_1 = error_id, - .operand_2 = try self.constInt(Type.anyerror, 0), - }, - ), - } - return result_id; - } - - fn airUnwrapOptional(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_id = try self.resolve(ty_op.operand); - const optional_ty = self.typeOf(ty_op.operand); - const payload_ty = self.typeOfIndex(inst); - - if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) return null; - - if (optional_ty.optionalReprIsPayload(zcu)) { - return operand_id; - } - - return try self.extractField(payload_ty, operand_id, 0); - } - - fn airUnwrapOptionalPtr(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const operand_id = try self.resolve(ty_op.operand); - const operand_ty = self.typeOf(ty_op.operand); - const optional_ty = operand_ty.childType(zcu); - const payload_ty = optional_ty.optionalChild(zcu); - const result_ty = self.typeOfIndex(inst); - const result_ty_id = try self.resolveType(result_ty, .direct); - - if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - // There is no payload, but we still need to return a valid pointer. - // We can just return anything here, so just return a pointer to the operand. - return try self.bitCast(result_ty, operand_ty, operand_id); - } - - if (optional_ty.optionalReprIsPayload(zcu)) { - // They are the same value. - return try self.bitCast(result_ty, operand_ty, operand_id); - } - - return try self.accessChain(result_ty_id, operand_id, &.{0}); - } - - fn airWrapOptional(self: *NavGen, inst: Air.Inst.Index) !?Id { - const pt = self.pt; - const zcu = pt.zcu; - const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; - const payload_ty = self.typeOf(ty_op.operand); - - if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { - return try self.constBool(true, .indirect); - } - - const operand_id = try self.resolve(ty_op.operand); - - const optional_ty = self.typeOfIndex(inst); - if (optional_ty.optionalReprIsPayload(zcu)) { - return operand_id; - } - - const payload_id = try self.convertToIndirect(payload_ty, operand_id); - const members = [_]Id{ payload_id, try self.constBool(true, .indirect) }; - const optional_ty_id = try self.resolveType(optional_ty, .direct); - return try self.constructComposite(optional_ty_id, &members); - } - - fn airSwitchBr(self: *NavGen, inst: Air.Inst.Index) !void { - const pt = self.pt; - const zcu = pt.zcu; - const target = self.spv.target; - const switch_br = self.air.unwrapSwitch(inst); - const cond_ty = self.typeOf(switch_br.operand); - const cond = try self.resolve(switch_br.operand); - var cond_indirect = try self.convertToIndirect(cond_ty, cond); - - const cond_words: u32 = switch (cond_ty.zigTypeTag(zcu)) { - .bool, .error_set => 1, - .int => blk: { - const bits = cond_ty.intInfo(zcu).bits; - const backing_bits, const big_int = self.backingIntBits(bits); - if (big_int) return self.todo("implement composite int switch", .{}); - break :blk if (backing_bits <= 32) 1 else 2; - }, - .@"enum" => blk: { - const int_ty = cond_ty.intTagType(zcu); - const int_info = int_ty.intInfo(zcu); - const backing_bits, const big_int = self.backingIntBits(int_info.bits); - if (big_int) return self.todo("implement composite int switch", .{}); - break :blk if (backing_bits <= 32) 1 else 2; - }, - .pointer => blk: { - cond_indirect = try self.intFromPtr(cond_indirect); - break :blk target.ptrBitWidth() / 32; - }, - // TODO: Figure out which types apply here, and work around them as we can only do integers. - else => return self.todo("implement switch for type {s}", .{@tagName(cond_ty.zigTypeTag(zcu))}), - }; - - const num_cases = switch_br.cases_len; - - // Compute the total number of arms that we need. - // Zig switches are grouped by condition, so we need to loop through all of them - const num_conditions = blk: { - var num_conditions: u32 = 0; - var it = switch_br.iterateCases(); - while (it.next()) |case| { - if (case.ranges.len > 0) return self.todo("switch with ranges", .{}); - num_conditions += @intCast(case.items.len); - } - break :blk num_conditions; - }; - - // First, pre-allocate the labels for the cases. - const case_labels = self.spv.allocIds(num_cases); - // We always need the default case - if zig has none, we will generate unreachable there. - const default = self.spv.allocId(); - - const merge_label = switch (self.control_flow) { - .structured => self.spv.allocId(), - .unstructured => null, - }; - - if (self.control_flow == .structured) { - try self.func.body.emit(self.spv.gpa, .OpSelectionMerge, .{ - .merge_block = merge_label.?, - .selection_control = .{}, - }); - } - - // Emit the instruction before generating the blocks. - try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions); - self.func.body.writeOperand(Id, cond_indirect); - self.func.body.writeOperand(Id, default); - - // Emit each of the cases - { - var it = switch_br.iterateCases(); - while (it.next()) |case| { - // SPIR-V needs a literal here, which' width depends on the case condition. - const label = case_labels.at(case.idx); - - for (case.items) |item| { - const value = (try self.air.value(item, pt)) orelse unreachable; - const int_val: u64 = switch (cond_ty.zigTypeTag(zcu)) { - .bool, .int => if (cond_ty.isSignedInt(zcu)) @bitCast(value.toSignedInt(zcu)) else value.toUnsignedInt(zcu), - .@"enum" => blk: { - // TODO: figure out of cond_ty is correct (something with enum literals) - break :blk (try value.intFromEnum(cond_ty, pt)).toUnsignedInt(zcu); // TODO: composite integer constants - }, - .error_set => value.getErrorInt(zcu), - .pointer => value.toUnsignedInt(zcu), - else => unreachable, - }; - const int_lit: spec.LiteralContextDependentNumber = switch (cond_words) { - 1 => .{ .uint32 = @intCast(int_val) }, - 2 => .{ .uint64 = int_val }, - else => unreachable, - }; - self.func.body.writeOperand(spec.LiteralContextDependentNumber, int_lit); - self.func.body.writeOperand(Id, label); - } - } - } - - var incoming_structured_blocks: std.ArrayListUnmanaged(ControlFlow.Structured.Block.Incoming) = .empty; - defer incoming_structured_blocks.deinit(self.gpa); - - if (self.control_flow == .structured) { - try incoming_structured_blocks.ensureUnusedCapacity(self.gpa, num_cases + 1); - } - - // Now, finally, we can start emitting each of the cases. - var it = switch_br.iterateCases(); - while (it.next()) |case| { - const label = case_labels.at(case.idx); - - try self.beginSpvBlock(label); - - switch (self.control_flow) { - .structured => { - const next_block = try self.genStructuredBody(.selection, case.body); - incoming_structured_blocks.appendAssumeCapacity(.{ - .src_label = self.current_block_label, - .next_block = next_block, - }); - try self.func.body.emitBranch(self.spv.gpa, merge_label.?); - }, - .unstructured => { - try self.genBody(case.body); - }, - } - } - - const else_body = it.elseBody(); - try self.beginSpvBlock(default); - if (else_body.len != 0) { - switch (self.control_flow) { - .structured => { - const next_block = try self.genStructuredBody(.selection, else_body); - incoming_structured_blocks.appendAssumeCapacity(.{ - .src_label = self.current_block_label, - .next_block = next_block, - }); - try self.func.body.emitBranch(self.spv.gpa, merge_label.?); - }, - .unstructured => { - try self.genBody(else_body); - }, - } - } else { - try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); - } - - if (self.control_flow == .structured) { - try self.beginSpvBlock(merge_label.?); - const next_block = try self.structuredNextBlock(incoming_structured_blocks.items); - try self.structuredBreak(next_block); - } - } - - fn airUnreach(self: *NavGen) !void { - try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); - } - - fn airDbgStmt(self: *NavGen, inst: Air.Inst.Index) !void { - const pt = self.pt; - const zcu = pt.zcu; - const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; - const path = zcu.navFileScope(self.owner_nav).sub_file_path; - try self.func.body.emit(self.spv.gpa, .OpLine, .{ - .file = try self.spv.resolveString(path), - .line = self.base_line + dbg_stmt.line + 1, - .column = dbg_stmt.column + 1, - }); - } - - fn airDbgInlineBlock(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const inst_datas = self.air.instructions.items(.data); - const extra = self.air.extraData(Air.DbgInlineBlock, inst_datas[@intFromEnum(inst)].ty_pl.payload); - const old_base_line = self.base_line; - defer self.base_line = old_base_line; - self.base_line = zcu.navSrcLine(zcu.funcInfo(extra.data.func).owner_nav); - return self.lowerBlock(inst, @ptrCast(self.air.extra.items[extra.end..][0..extra.data.body_len])); - } - - fn airDbgVar(self: *NavGen, inst: Air.Inst.Index) !void { - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const target_id = try self.resolve(pl_op.operand); - const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); - try self.spv.debugName(target_id, name.toSlice(self.air)); - } - - fn airAssembly(self: *NavGen, inst: Air.Inst.Index) !?Id { - const zcu = self.pt.zcu; - const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; - const extra = self.air.extraData(Air.Asm, ty_pl.payload); - - const is_volatile = extra.data.flags.is_volatile; - const outputs_len = extra.data.flags.outputs_len; - - if (!is_volatile and self.liveness.isUnused(inst)) return null; - - var extra_i: usize = extra.end; - const outputs: []const Air.Inst.Ref = @ptrCast(self.air.extra.items[extra_i..][0..outputs_len]); - extra_i += outputs.len; - const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra.items[extra_i..][0..extra.data.inputs_len]); - extra_i += inputs.len; - - if (outputs.len > 1) { - return self.todo("implement inline asm with more than 1 output", .{}); - } - - var as: SpvAssembler = .{ - .gpa = self.gpa, - .spv = self.spv, - .func = &self.func, - }; - defer as.deinit(); - - var output_extra_i = extra_i; - for (outputs) |output| { - if (output != .none) { - return self.todo("implement inline asm with non-returned output", .{}); - } - const extra_bytes = std.mem.sliceAsBytes(self.air.extra.items[extra_i..]); - const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra.items[extra_i..]), 0); - const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); - extra_i += (constraint.len + name.len + (2 + 3)) / 4; - // TODO: Record output and use it somewhere. - } - - for (inputs) |input| { - const extra_bytes = std.mem.sliceAsBytes(self.air.extra.items[extra_i..]); - const constraint = std.mem.sliceTo(extra_bytes, 0); - const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); - // This equation accounts for the fact that even if we have exactly 4 bytes - // for the string, we still use the next u32 for the null terminator. - extra_i += (constraint.len + name.len + (2 + 3)) / 4; - - const input_ty = self.typeOf(input); - - if (std.mem.eql(u8, constraint, "c")) { - // constant - const val = (try self.air.value(input, self.pt)) orelse { - return self.fail("assembly inputs with 'c' constraint have to be compile-time known", .{}); - }; - - // TODO: This entire function should be handled a bit better... - const ip = &zcu.intern_pool; - switch (ip.indexToKey(val.toIntern())) { - .int_type, - .ptr_type, - .array_type, - .vector_type, - .opt_type, - .anyframe_type, - .error_union_type, - .simple_type, - .struct_type, - .union_type, - .opaque_type, - .enum_type, - .func_type, - .error_set_type, - .inferred_error_set_type, - => unreachable, // types, not values - - .undef => return self.fail("assembly input with 'c' constraint cannot be undefined", .{}), - - .int => try as.value_map.put(as.gpa, name, .{ .constant = @intCast(val.toUnsignedInt(zcu)) }), - .enum_literal => |str| try as.value_map.put(as.gpa, name, .{ .string = str.toSlice(ip) }), - - else => unreachable, // TODO - } - } else if (std.mem.eql(u8, constraint, "t")) { - // type - if (input_ty.zigTypeTag(zcu) == .type) { - // This assembly input is a type instead of a value. - // That's fine for now, just make sure to resolve it as such. - const val = (try self.air.value(input, self.pt)).?; - const ty_id = try self.resolveType(val.toType(), .direct); - try as.value_map.put(as.gpa, name, .{ .ty = ty_id }); - } else { - const ty_id = try self.resolveType(input_ty, .direct); - try as.value_map.put(as.gpa, name, .{ .ty = ty_id }); - } - } else { - if (input_ty.zigTypeTag(zcu) == .type) { - return self.fail("use the 't' constraint to supply types to SPIR-V inline assembly", .{}); - } - - const val_id = try self.resolve(input); - try as.value_map.put(as.gpa, name, .{ .value = val_id }); - } - } - - // TODO: do something with clobbers - _ = extra.data.clobbers; - - const asm_source = std.mem.sliceAsBytes(self.air.extra.items[extra_i..])[0..extra.data.source_len]; - - as.assemble(asm_source) catch |err| switch (err) { - error.AssembleFail => { - // TODO: For now the compiler only supports a single error message per decl, - // so to translate the possible multiple errors from the assembler, emit - // them as notes here. - // TODO: Translate proper error locations. - assert(as.errors.items.len != 0); - assert(self.error_msg == null); - const src_loc = zcu.navSrcLoc(self.owner_nav); - self.error_msg = try Zcu.ErrorMsg.create(zcu.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{}); - const notes = try zcu.gpa.alloc(Zcu.ErrorMsg, as.errors.items.len); - - // Sub-scope to prevent `return error.CodegenFail` from running the errdefers. - { - errdefer zcu.gpa.free(notes); - var i: usize = 0; - errdefer for (notes[0..i]) |*note| { - note.deinit(zcu.gpa); - }; - - while (i < as.errors.items.len) : (i += 1) { - notes[i] = try Zcu.ErrorMsg.init(zcu.gpa, src_loc, "{s}", .{as.errors.items[i].msg}); - } - } - self.error_msg.?.notes = notes; - return error.CodegenFail; - }, - else => |others| return others, - }; - - for (outputs) |output| { - _ = output; - const extra_bytes = std.mem.sliceAsBytes(self.air.extra.items[output_extra_i..]); - const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(self.air.extra.items[output_extra_i..]), 0); - const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); - output_extra_i += (constraint.len + name.len + (2 + 3)) / 4; - - const result = as.value_map.get(name) orelse return { - return self.fail("invalid asm output '{s}'", .{name}); - }; - - switch (result) { - .just_declared, .unresolved_forward_reference => unreachable, - .ty => return self.fail("cannot return spir-v type as value from assembly", .{}), - .value => |ref| return ref, - .constant, .string => return self.fail("cannot return constant from assembly", .{}), - } - - // TODO: Multiple results - // TODO: Check that the output type from assembly is the same as the type actually expected by Zig. - } - - return null; - } - - fn airCall(self: *NavGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !?Id { - _ = modifier; - - const pt = self.pt; - const zcu = pt.zcu; - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const extra = self.air.extraData(Air.Call, pl_op.payload); - const args: []const Air.Inst.Ref = @ptrCast(self.air.extra.items[extra.end..][0..extra.data.args_len]); - const callee_ty = self.typeOf(pl_op.operand); - const zig_fn_ty = switch (callee_ty.zigTypeTag(zcu)) { - .@"fn" => callee_ty, - .pointer => return self.fail("cannot call function pointers", .{}), - else => unreachable, - }; - const fn_info = zcu.typeToFunc(zig_fn_ty).?; - const return_type = fn_info.return_type; - - const result_type_id = try self.resolveFnReturnType(Type.fromInterned(return_type)); - const result_id = self.spv.allocId(); - const callee_id = try self.resolve(pl_op.operand); - - comptime assert(zig_call_abi_ver == 3); - const params = try self.gpa.alloc(spec.Id, args.len); - defer self.gpa.free(params); - var n_params: usize = 0; - for (args) |arg| { - // Note: resolve() might emit instructions, so we need to call it - // before starting to emit OpFunctionCall instructions. Hence the - // temporary params buffer. - const arg_ty = self.typeOf(arg); - if (!arg_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; - const arg_id = try self.resolve(arg); - - params[n_params] = arg_id; - n_params += 1; - } - - try self.func.body.emit(self.spv.gpa, .OpFunctionCall, .{ - .id_result_type = result_type_id, - .id_result = result_id, - .function = callee_id, - .id_ref_3 = params[0..n_params], - }); - - if (self.liveness.isUnused(inst) or !Type.fromInterned(return_type).hasRuntimeBitsIgnoreComptime(zcu)) { - return null; - } - - return result_id; - } - - fn builtin3D(self: *NavGen, result_ty: Type, builtin: spec.BuiltIn, dimension: u32, out_of_range_value: anytype) !Id { - if (dimension >= 3) { - return try self.constInt(result_ty, out_of_range_value); - } - const vec_ty = try self.pt.vectorType(.{ - .len = 3, - .child = result_ty.toIntern(), - }); - const ptr_ty_id = try self.ptrType(vec_ty, .input, .indirect); - const spv_decl_index = try self.spv.builtin(ptr_ty_id, builtin); - try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {}); - const ptr = self.spv.declPtr(spv_decl_index).result_id; - const vec = try self.load(vec_ty, ptr, .{}); - return try self.extractVectorComponent(result_ty, vec, dimension); - } - - fn airWorkItemId(self: *NavGen, inst: Air.Inst.Index) !?Id { - if (self.liveness.isUnused(inst)) return null; - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const dimension = pl_op.payload; - // TODO: Should we make these builtins return usize? - const result_id = try self.builtin3D(Type.u64, .local_invocation_id, dimension, 0); - const tmp = Temporary.init(Type.u64, result_id); - const result = try self.buildConvert(Type.u32, tmp); - return try result.materialize(self); - } - - fn airWorkGroupSize(self: *NavGen, inst: Air.Inst.Index) !?Id { - if (self.liveness.isUnused(inst)) return null; - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const dimension = pl_op.payload; - // TODO: Should we make these builtins return usize? - const result_id = try self.builtin3D(Type.u64, .workgroup_size, dimension, 0); - const tmp = Temporary.init(Type.u64, result_id); - const result = try self.buildConvert(Type.u32, tmp); - return try result.materialize(self); - } - - fn airWorkGroupId(self: *NavGen, inst: Air.Inst.Index) !?Id { - if (self.liveness.isUnused(inst)) return null; - const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; - const dimension = pl_op.payload; - // TODO: Should we make these builtins return usize? - const result_id = try self.builtin3D(Type.u64, .workgroup_id, dimension, 0); - const tmp = Temporary.init(Type.u64, result_id); - const result = try self.buildConvert(Type.u32, tmp); - return try result.materialize(self); - } - - fn typeOf(self: *NavGen, inst: Air.Inst.Ref) Type { - const zcu = self.pt.zcu; - return self.air.typeOf(inst, &zcu.intern_pool); - } - - fn typeOfIndex(self: *NavGen, inst: Air.Inst.Index) Type { - const zcu = self.pt.zcu; - return self.air.typeOfIndex(inst, &zcu.intern_pool); - } -}; diff --git a/src/codegen/spirv/Assembler.zig b/src/codegen/spirv/Assembler.zig index 190c8c2e927c..a47cd49644f7 100644 --- a/src/codegen/spirv/Assembler.zig +++ b/src/codegen/spirv/Assembler.zig @@ -1,147 +1,146 @@ -const Assembler = @This(); - const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; +const CodeGen = @import("CodeGen.zig"); +const Decl = @import("Module.zig").Decl; + const spec = @import("spec.zig"); const Opcode = spec.Opcode; const Word = spec.Word; const Id = spec.Id; const StorageClass = spec.StorageClass; -const SpvModule = @import("Module.zig"); - -/// Represents a token in the assembly template. -const Token = struct { - tag: Tag, - start: u32, - end: u32, +const Assembler = @This(); - const Tag = enum { - /// Returned when there was no more input to match. - eof, - /// %identifier - result_id, - /// %identifier when appearing on the LHS of an equals sign. - /// While not technically a token, its relatively easy to resolve - /// this during lexical analysis and relieves a bunch of headaches - /// during parsing. - result_id_assign, - /// Mask, int, or float. These are grouped together as some - /// SPIR-V enumerants look a bit like integers as well (for example - /// "3D"), and so it is easier to just interpret them as the expected - /// type when resolving an instruction's operands. - value, - /// An enumerant that looks like an opcode, that is, OpXxxx. - /// Not necessarily a *valid* opcode. - opcode, - /// String literals. - /// Note, this token is also returned for unterminated - /// strings. In this case the closing " is not present. - string, - /// |. - pipe, - /// =. - equals, - /// $identifier. This is used (for now) for constant values, like integers. - /// These can be used in place of a normal `value`. - placeholder, +cg: *CodeGen, +errors: std.ArrayListUnmanaged(ErrorMsg) = .empty, +src: []const u8 = undefined, +/// `ass.src` tokenized. +tokens: std.ArrayListUnmanaged(Token) = .empty, +current_token: u32 = 0, +/// The instruction that is currently being parsed or has just been parsed. +inst: struct { + opcode: Opcode = undefined, + operands: std.ArrayListUnmanaged(Operand) = .empty, + string_bytes: std.ArrayListUnmanaged(u8) = .empty, - fn name(self: Tag) []const u8 { - return switch (self) { - .eof => "", - .result_id => "", - .result_id_assign => "", - .value => "", - .opcode => "", - .string => "", - .pipe => "'|'", - .equals => "'='", - .placeholder => "", - }; + fn result(ass: @This()) ?AsmValue.Ref { + for (ass.operands.items[0..@min(ass.operands.items.len, 2)]) |op| { + switch (op) { + .result_id => |index| return index, + else => {}, + } } - }; -}; + return null; + } +} = .{}, +value_map: std.StringArrayHashMapUnmanaged(AsmValue) = .{}, +inst_map: std.StringArrayHashMapUnmanaged(void) = .empty, -/// This union represents utility information for a decoded operand. -/// Note that this union only needs to maintain a minimal amount of -/// bookkeeping: these values are enough to either decode the operands -/// into a spec type, or emit it directly into its binary form. const Operand = union(enum) { /// Any 'simple' 32-bit value. This could be a mask or /// enumerant, etc, depending on the operands. value: u32, - - /// An int- or float literal encoded as 1 word. This may be - /// a 32-bit literal or smaller, already in the proper format: - /// the opper bits are 0 for floats and unsigned ints, and sign-extended - /// for signed ints. + /// An int- or float literal encoded as 1 word. literal32: u32, - - /// An int- or float literal encoded as 2 words. This may be a 33-bit - /// to 64 bit literal, already in the proper format: - /// the opper bits are 0 for floats and unsigned ints, and sign-extended - /// for signed ints. + /// An int- or float literal encoded as 2 words. literal64: u64, - - /// A result-id which is assigned to in this instruction. If present, - /// this is the first operand of the instruction. + /// A result-id which is assigned to in this instruction. + /// If present, this is the first operand of the instruction. result_id: AsmValue.Ref, - /// A result-id which referred to (not assigned to) in this instruction. ref_id: AsmValue.Ref, - /// Offset into `inst.string_bytes`. The string ends at the next zero-terminator. string: u32, }; -/// A structure representing an error message that the assembler may return, when -/// the assembly source is not syntactically or semantically correct. +pub fn deinit(ass: *Assembler) void { + const gpa = ass.cg.module.gpa; + for (ass.errors.items) |err| gpa.free(err.msg); + ass.tokens.deinit(gpa); + ass.errors.deinit(gpa); + ass.inst.operands.deinit(gpa); + ass.inst.string_bytes.deinit(gpa); + ass.value_map.deinit(gpa); + ass.inst_map.deinit(gpa); +} + +const Error = error{ AssembleFail, OutOfMemory }; + +pub fn assemble(ass: *Assembler, src: []const u8) Error!void { + const gpa = ass.cg.module.gpa; + + ass.src = src; + ass.errors.clearRetainingCapacity(); + + // Populate the opcode map if it isn't already + if (ass.inst_map.count() == 0) { + const instructions = spec.InstructionSet.core.instructions(); + try ass.inst_map.ensureUnusedCapacity(gpa, @intCast(instructions.len)); + for (spec.InstructionSet.core.instructions(), 0..) |inst, i| { + const entry = try ass.inst_map.getOrPut(gpa, inst.name); + assert(entry.index == i); + } + } + + try ass.tokenize(); + while (!ass.testToken(.eof)) { + try ass.parseInstruction(); + try ass.processInstruction(); + } + + if (ass.errors.items.len > 0) return error.AssembleFail; +} + const ErrorMsg = struct { /// The offset in bytes from the start of `src` that this error occured. byte_offset: u32, - /// An explanatory error message. - /// Memory is owned by `self.gpa`. TODO: Maybe allocate this with an arena - /// allocator if it is needed elsewhere? msg: []const u8, }; -/// Possible errors the `assemble` function may return. -const Error = error{ AssembleFail, OutOfMemory }; +fn addError(ass: *Assembler, offset: u32, comptime fmt: []const u8, args: anytype) !void { + const gpa = ass.cg.module.gpa; + const msg = try std.fmt.allocPrint(gpa, fmt, args); + errdefer gpa.free(msg); + try ass.errors.append(gpa, .{ + .byte_offset = offset, + .msg = msg, + }); +} + +fn fail(ass: *Assembler, offset: u32, comptime fmt: []const u8, args: anytype) Error { + try ass.addError(offset, fmt, args); + return error.AssembleFail; +} + +fn todo(ass: *Assembler, comptime fmt: []const u8, args: anytype) Error { + return ass.fail(0, "todo: " ++ fmt, args); +} -/// This union is used to keep track of results of spir-v instructions. This can either be just a plain -/// result-id, in the case of most instructions, or for example a type that is constructed from -/// an OpTypeXxx instruction. const AsmValue = union(enum) { - /// The results are stored in an array hash map, and can be referred to either by name (without the %), - /// or by values of this index type. + /// The results are stored in an array hash map, and can be referred + /// to either by name (without the %), or by values of this index type. pub const Ref = u32; - /// This result-value is the RHS of the current instruction. + /// The RHS of the current instruction. just_declared, - - /// This is used as placeholder for ref-ids of which the result-id is not yet known. + /// A placeholder for ref-ids of which the result-id is not yet known. /// It will be further resolved at a later stage to a more concrete forward reference. unresolved_forward_reference, - - /// This result-value is a normal result produced by a different instruction. + /// A normal result produced by a different instruction. value: Id, - - /// This result-value represents a type registered into the module's type system. + /// A type registered into the module's type system. ty: Id, - - /// This is a pre-supplied constant integer value. + /// A pre-supplied constant integer value. constant: u32, - - /// This is a pre-supplied constant string value. string: []const u8, /// Retrieve the result-id of this AsmValue. Asserts that this AsmValue /// is of a variant that allows the result to be obtained (not an unresolved /// forward declaration, not in the process of being declared, etc). - pub fn resultId(self: AsmValue) Id { - return switch (self) { + pub fn resultId(value: AsmValue) Id { + return switch (value) { .just_declared, .unresolved_forward_reference, // TODO: Lower this value as constant? @@ -154,226 +153,101 @@ const AsmValue = union(enum) { } }; -/// This map type maps results to values. Results can be addressed either by name (without the %), or by -/// AsmValue.Ref in AsmValueMap.keys/.values. -const AsmValueMap = std.StringArrayHashMapUnmanaged(AsmValue); - -/// An allocator used for common allocations. -gpa: Allocator, - -/// A list of errors that occured during processing the assembly. -errors: std.ArrayListUnmanaged(ErrorMsg) = .empty, - -/// The source code that is being assembled. -/// This is set when calling `assemble()`. -src: []const u8 = undefined, - -/// The module that this assembly is associated to. -/// Instructions like OpType*, OpDecorate, etc are emitted into this module. -spv: *SpvModule, - -/// The function that the function-specific instructions should be emitted to. -func: *SpvModule.Fn, - -/// `self.src` tokenized. -tokens: std.ArrayListUnmanaged(Token) = .empty, - -/// The token that is next during parsing. -current_token: u32 = 0, - -/// This field groups the properties of the instruction that is currently -/// being parsed or has just been parsed. -inst: struct { - /// The opcode of the current instruction. - opcode: Opcode = undefined, - /// Operands of the current instruction. - operands: std.ArrayListUnmanaged(Operand) = .empty, - /// This is where string data resides. Strings are zero-terminated. - string_bytes: std.ArrayListUnmanaged(u8) = .empty, - - /// Return a reference to the result of this instruction, if any. - fn result(self: @This()) ?AsmValue.Ref { - // The result, if present, is either the first or second - // operand of an instruction. - for (self.operands.items[0..@min(self.operands.items.len, 2)]) |op| { - switch (op) { - .result_id => |index| return index, - else => {}, - } - } - return null; - } -} = .{}, - -/// This map maps results to their tracked values. -value_map: AsmValueMap = .{}, - -/// This set is used to quickly transform from an opcode name to the -/// index in its instruction set. The index of the key is the -/// index in `spec.InstructionSet.core.instructions()`. -instruction_map: std.StringArrayHashMapUnmanaged(void) = .empty, - -/// Free the resources owned by this assembler. -pub fn deinit(self: *Assembler) void { - for (self.errors.items) |err| { - self.gpa.free(err.msg); - } - self.tokens.deinit(self.gpa); - self.errors.deinit(self.gpa); - self.inst.operands.deinit(self.gpa); - self.inst.string_bytes.deinit(self.gpa); - self.value_map.deinit(self.gpa); - self.instruction_map.deinit(self.gpa); -} - -pub fn assemble(self: *Assembler, src: []const u8) Error!void { - self.src = src; - self.errors.clearRetainingCapacity(); - - // Populate the opcode map if it isn't already - if (self.instruction_map.count() == 0) { - const instructions = spec.InstructionSet.core.instructions(); - try self.instruction_map.ensureUnusedCapacity(self.gpa, @intCast(instructions.len)); - for (spec.InstructionSet.core.instructions(), 0..) |inst, i| { - const entry = try self.instruction_map.getOrPut(self.gpa, inst.name); - assert(entry.index == i); - } - } - - try self.tokenize(); - while (!self.testToken(.eof)) { - try self.parseInstruction(); - try self.processInstruction(); - } - if (self.errors.items.len > 0) - return error.AssembleFail; -} - -fn addError(self: *Assembler, offset: u32, comptime fmt: []const u8, args: anytype) !void { - const msg = try std.fmt.allocPrint(self.gpa, fmt, args); - errdefer self.gpa.free(msg); - try self.errors.append(self.gpa, .{ - .byte_offset = offset, - .msg = msg, - }); -} - -fn fail(self: *Assembler, offset: u32, comptime fmt: []const u8, args: anytype) Error { - try self.addError(offset, fmt, args); - return error.AssembleFail; -} - -fn todo(self: *Assembler, comptime fmt: []const u8, args: anytype) Error { - return self.fail(0, "todo: " ++ fmt, args); -} - -/// Attempt to process the instruction currently in `self.inst`. +/// Attempt to process the instruction currently in `ass.inst`. /// This for example emits the instruction in the module or function, or /// records type definitions. /// If this function returns `error.AssembleFail`, an explanatory -/// error message has already been emitted into `self.errors`. -fn processInstruction(self: *Assembler) !void { - const result: AsmValue = switch (self.inst.opcode) { +/// error message has already been emitted into `ass.errors`. +fn processInstruction(ass: *Assembler) !void { + const module = ass.cg.module; + const result: AsmValue = switch (ass.inst.opcode) { .OpEntryPoint => { - return self.fail(0, "cannot export entry points via OpEntryPoint, export the kernel using callconv(.kernel)", .{}); + return ass.fail(ass.currentToken().start, "cannot export entry points in assembly", .{}); + }, + .OpExecutionMode, .OpExecutionModeId => { + return ass.fail(ass.currentToken().start, "cannot set execution mode in assembly", .{}); }, .OpCapability => { - try self.spv.addCapability(@enumFromInt(self.inst.operands.items[0].value)); + try module.addCapability(@enumFromInt(ass.inst.operands.items[0].value)); return; }, .OpExtension => { - const ext_name_offset = self.inst.operands.items[0].string; - const ext_name = std.mem.sliceTo(self.inst.string_bytes.items[ext_name_offset..], 0); - try self.spv.addExtension(ext_name); + const ext_name_offset = ass.inst.operands.items[0].string; + const ext_name = std.mem.sliceTo(ass.inst.string_bytes.items[ext_name_offset..], 0); + try module.addExtension(ext_name); return; }, .OpExtInstImport => blk: { - const set_name_offset = self.inst.operands.items[1].string; - const set_name = std.mem.sliceTo(self.inst.string_bytes.items[set_name_offset..], 0); + const set_name_offset = ass.inst.operands.items[1].string; + const set_name = std.mem.sliceTo(ass.inst.string_bytes.items[set_name_offset..], 0); const set_tag = std.meta.stringToEnum(spec.InstructionSet, set_name) orelse { - return self.fail(set_name_offset, "unknown instruction set: {s}", .{set_name}); + return ass.fail(set_name_offset, "unknown instruction set: {s}", .{set_name}); }; - break :blk .{ .value = try self.spv.importInstructionSet(set_tag) }; + break :blk .{ .value = try module.importInstructionSet(set_tag) }; }, - .OpExecutionMode, .OpExecutionModeId => { - assert(try self.processGenericInstruction() == null); - const entry_point_id = try self.resolveRefId(self.inst.operands.items[0].ref_id); - const exec_mode: spec.ExecutionMode = @enumFromInt(self.inst.operands.items[1].value); - const gop = try self.spv.entry_points.getOrPut(self.gpa, entry_point_id); - if (!gop.found_existing) { - gop.value_ptr.* = .{}; - } else if (gop.value_ptr.exec_mode != null) { - return self.fail( - self.currentToken().start, - "cannot set execution mode more than once to any entry point", - .{}, - ); - } - gop.value_ptr.exec_mode = exec_mode; - return; - }, - else => switch (self.inst.opcode.class()) { - .type_declaration => try self.processTypeInstruction(), - else => (try self.processGenericInstruction()) orelse return, + else => switch (ass.inst.opcode.class()) { + .type_declaration => try ass.processTypeInstruction(), + else => (try ass.processGenericInstruction()) orelse return, }, }; - const result_ref = self.inst.result().?; - switch (self.value_map.values()[result_ref]) { - .just_declared => self.value_map.values()[result_ref] = result, + const result_ref = ass.inst.result().?; + switch (ass.value_map.values()[result_ref]) { + .just_declared => ass.value_map.values()[result_ref] = result, else => { // TODO: Improve source location. - const name = self.value_map.keys()[result_ref]; - return self.fail(0, "duplicate definition of %{s}", .{name}); + const name = ass.value_map.keys()[result_ref]; + return ass.fail(0, "duplicate definition of %{s}", .{name}); }, } } -/// Record `self.inst` into the module's type system, and return the AsmValue that -/// refers to the result. -fn processTypeInstruction(self: *Assembler) !AsmValue { - const operands = self.inst.operands.items; - const section = &self.spv.sections.types_globals_constants; - const id = switch (self.inst.opcode) { - .OpTypeVoid => try self.spv.voidType(), - .OpTypeBool => try self.spv.boolType(), +fn processTypeInstruction(ass: *Assembler) !AsmValue { + const cg = ass.cg; + const gpa = cg.module.gpa; + const module = cg.module; + const operands = ass.inst.operands.items; + const section = &module.sections.globals; + const id = switch (ass.inst.opcode) { + .OpTypeVoid => try module.voidType(), + .OpTypeBool => try module.boolType(), .OpTypeInt => blk: { const signedness: std.builtin.Signedness = switch (operands[2].literal32) { 0 => .unsigned, 1 => .signed, else => { // TODO: Improve source location. - return self.fail(0, "{} is not a valid signedness (expected 0 or 1)", .{operands[2].literal32}); + return ass.fail(0, "{} is not a valid signedness (expected 0 or 1)", .{operands[2].literal32}); }, }; const width = std.math.cast(u16, operands[1].literal32) orelse { - return self.fail(0, "int type of {} bits is too large", .{operands[1].literal32}); + return ass.fail(0, "int type of {} bits is too large", .{operands[1].literal32}); }; - break :blk try self.spv.intType(signedness, width); + break :blk try module.intType(signedness, width); }, .OpTypeFloat => blk: { const bits = operands[1].literal32; switch (bits) { 16, 32, 64 => {}, else => { - return self.fail(0, "{} is not a valid bit count for floats (expected 16, 32 or 64)", .{bits}); + return ass.fail(0, "{} is not a valid bit count for floats (expected 16, 32 or 64)", .{bits}); }, } - break :blk try self.spv.floatType(@intCast(bits)); + break :blk try module.floatType(@intCast(bits)); }, .OpTypeVector => blk: { - const child_type = try self.resolveRefId(operands[1].ref_id); - break :blk try self.spv.vectorType(operands[2].literal32, child_type); + const child_type = try ass.resolveRefId(operands[1].ref_id); + break :blk try module.vectorType(operands[2].literal32, child_type); }, .OpTypeArray => { // TODO: The length of an OpTypeArray is determined by a constant (which may be a spec constant), // and so some consideration must be taken when entering this in the type system. - return self.todo("process OpTypeArray", .{}); + return ass.todo("process OpTypeArray", .{}); }, .OpTypeRuntimeArray => blk: { - const element_type = try self.resolveRefId(operands[1].ref_id); - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeRuntimeArray, .{ + const element_type = try ass.resolveRefId(operands[1].ref_id); + const result_id = module.allocId(); + try section.emit(module.gpa, .OpTypeRuntimeArray, .{ .id_result = result_id, .element_type = element_type, }); @@ -381,9 +255,9 @@ fn processTypeInstruction(self: *Assembler) !AsmValue { }, .OpTypePointer => blk: { const storage_class: StorageClass = @enumFromInt(operands[1].value); - const child_type = try self.resolveRefId(operands[2].ref_id); - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypePointer, .{ + const child_type = try ass.resolveRefId(operands[2].ref_id); + const result_id = module.allocId(); + try section.emit(module.gpa, .OpTypePointer, .{ .id_result = result_id, .storage_class = storage_class, .type = child_type, @@ -391,17 +265,16 @@ fn processTypeInstruction(self: *Assembler) !AsmValue { break :blk result_id; }, .OpTypeStruct => blk: { - const ids = try self.gpa.alloc(Id, operands[1..].len); - defer self.gpa.free(ids); - for (operands[1..], ids) |op, *id| id.* = try self.resolveRefId(op.ref_id); - const result_id = self.spv.allocId(); - try self.spv.structType(result_id, ids, null); - break :blk result_id; + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const ids = try cg.id_scratch.addManyAsSlice(gpa, operands[1..].len); + for (operands[1..], ids) |op, *id| id.* = try ass.resolveRefId(op.ref_id); + break :blk try module.structType(ids, null, null, .none); }, .OpTypeImage => blk: { - const sampled_type = try self.resolveRefId(operands[1].ref_id); - const result_id = self.spv.allocId(); - try section.emit(self.gpa, .OpTypeImage, .{ + const sampled_type = try ass.resolveRefId(operands[1].ref_id); + const result_id = module.allocId(); + try section.emit(gpa, .OpTypeImage, .{ .id_result = result_id, .sampled_type = sampled_type, .dim = @enumFromInt(operands[2].value), @@ -414,187 +287,178 @@ fn processTypeInstruction(self: *Assembler) !AsmValue { break :blk result_id; }, .OpTypeSampler => blk: { - const result_id = self.spv.allocId(); - try section.emit(self.gpa, .OpTypeSampler, .{ .id_result = result_id }); + const result_id = module.allocId(); + try section.emit(gpa, .OpTypeSampler, .{ .id_result = result_id }); break :blk result_id; }, .OpTypeSampledImage => blk: { - const image_type = try self.resolveRefId(operands[1].ref_id); - const result_id = self.spv.allocId(); - try section.emit(self.gpa, .OpTypeSampledImage, .{ .id_result = result_id, .image_type = image_type }); + const image_type = try ass.resolveRefId(operands[1].ref_id); + const result_id = module.allocId(); + try section.emit(gpa, .OpTypeSampledImage, .{ .id_result = result_id, .image_type = image_type }); break :blk result_id; }, .OpTypeFunction => blk: { const param_operands = operands[2..]; - const return_type = try self.resolveRefId(operands[1].ref_id); + const return_type = try ass.resolveRefId(operands[1].ref_id); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const param_types = try cg.id_scratch.addManyAsSlice(gpa, param_operands.len); - const param_types = try self.spv.gpa.alloc(Id, param_operands.len); - defer self.spv.gpa.free(param_types); for (param_types, param_operands) |*param, operand| { - param.* = try self.resolveRefId(operand.ref_id); + param.* = try ass.resolveRefId(operand.ref_id); } - const result_id = self.spv.allocId(); - try section.emit(self.spv.gpa, .OpTypeFunction, .{ + const result_id = module.allocId(); + try section.emit(module.gpa, .OpTypeFunction, .{ .id_result = result_id, .return_type = return_type, .id_ref_2 = param_types, }); break :blk result_id; }, - else => return self.todo("process type instruction {s}", .{@tagName(self.inst.opcode)}), + else => return ass.todo("process type instruction {s}", .{@tagName(ass.inst.opcode)}), }; - return AsmValue{ .ty = id }; + return .{ .ty = id }; } -/// Emit `self.inst` into `self.spv` and `self.func`, and return the AsmValue -/// that this produces (if any). This function processes common instructions: /// - No forward references are allowed in operands. /// - Target section is determined from instruction type. -/// - Function-local instructions are emitted in `self.func`. -fn processGenericInstruction(self: *Assembler) !?AsmValue { - const operands = self.inst.operands.items; - var maybe_spv_decl_index: ?SpvModule.Decl.Index = null; - const section = switch (self.inst.opcode.class()) { - .constant_creation => &self.spv.sections.types_globals_constants, - .annotation => &self.spv.sections.annotations, +fn processGenericInstruction(ass: *Assembler) !?AsmValue { + const module = ass.cg.module; + const target = module.zcu.getTarget(); + const operands = ass.inst.operands.items; + var maybe_spv_decl_index: ?Decl.Index = null; + const section = switch (ass.inst.opcode.class()) { + .constant_creation => &module.sections.globals, + .annotation => &module.sections.annotations, .type_declaration => unreachable, // Handled elsewhere. - else => switch (self.inst.opcode) { + else => switch (ass.inst.opcode) { .OpEntryPoint => unreachable, - .OpExecutionMode, .OpExecutionModeId => &self.spv.sections.execution_modes, + .OpExecutionMode, .OpExecutionModeId => &module.sections.execution_modes, .OpVariable => section: { const storage_class: spec.StorageClass = @enumFromInt(operands[2].value); - if (storage_class == .function) break :section &self.func.prologue; - maybe_spv_decl_index = try self.spv.allocDecl(.global); - if (self.spv.version.minor < 4 and storage_class != .input and storage_class != .output) { + if (storage_class == .function) break :section &ass.cg.prologue; + maybe_spv_decl_index = try module.allocDecl(.global); + if (!target.cpu.has(.spirv, .v1_4) and storage_class != .input and storage_class != .output) { // Before version 1.4, the interface’s storage classes are limited to the Input and Output - break :section &self.spv.sections.types_globals_constants; + break :section &module.sections.globals; } - try self.func.decl_deps.put(self.spv.gpa, maybe_spv_decl_index.?, {}); - // TODO: In theory this can be non-empty if there is an initializer which depends on another global... - try self.spv.declareDeclDeps(maybe_spv_decl_index.?, &.{}); - break :section &self.spv.sections.types_globals_constants; + try ass.cg.module.decl_deps.append(module.gpa, maybe_spv_decl_index.?); + break :section &module.sections.globals; }, - // Default case - to be worked out further. - else => &self.func.body, + else => &ass.cg.body, }, }; var maybe_result_id: ?Id = null; const first_word = section.instructions.items.len; - // At this point we're not quite sure how many operands this instruction is going to have, - // so insert 0 and patch up the actual opcode word later. - try section.ensureUnusedCapacity(self.spv.gpa, 1); + // At this point we're not quite sure how many operands this instruction is + // going to have, so insert 0 and patch up the actual opcode word later. + try section.ensureUnusedCapacity(module.gpa, 1); section.writeWord(0); for (operands) |operand| { switch (operand) { .value, .literal32 => |word| { - try section.ensureUnusedCapacity(self.spv.gpa, 1); + try section.ensureUnusedCapacity(module.gpa, 1); section.writeWord(word); }, .literal64 => |dword| { - try section.ensureUnusedCapacity(self.spv.gpa, 2); + try section.ensureUnusedCapacity(module.gpa, 2); section.writeDoubleWord(dword); }, .result_id => { maybe_result_id = if (maybe_spv_decl_index) |spv_decl_index| - self.spv.declPtr(spv_decl_index).result_id + module.declPtr(spv_decl_index).result_id else - self.spv.allocId(); - try section.ensureUnusedCapacity(self.spv.gpa, 1); + module.allocId(); + try section.ensureUnusedCapacity(module.gpa, 1); section.writeOperand(Id, maybe_result_id.?); }, .ref_id => |index| { - const result = try self.resolveRef(index); - try section.ensureUnusedCapacity(self.spv.gpa, 1); + const result = try ass.resolveRef(index); + try section.ensureUnusedCapacity(module.gpa, 1); section.writeOperand(spec.Id, result.resultId()); }, .string => |offset| { - const text = std.mem.sliceTo(self.inst.string_bytes.items[offset..], 0); + const text = std.mem.sliceTo(ass.inst.string_bytes.items[offset..], 0); const size = std.math.divCeil(usize, text.len + 1, @sizeOf(Word)) catch unreachable; - try section.ensureUnusedCapacity(self.spv.gpa, size); + try section.ensureUnusedCapacity(module.gpa, size); section.writeOperand(spec.LiteralString, text); }, } } const actual_word_count = section.instructions.items.len - first_word; - section.instructions.items[first_word] |= @as(u32, @as(u16, @intCast(actual_word_count))) << 16 | @intFromEnum(self.inst.opcode); + section.instructions.items[first_word] |= @as(u32, @as(u16, @intCast(actual_word_count))) << 16 | @intFromEnum(ass.inst.opcode); - if (maybe_result_id) |result| { - return AsmValue{ .value = result }; - } + if (maybe_result_id) |result| return .{ .value = result }; return null; } -/// Resolve a value reference. This function makes sure that the reference is -/// not self-referential, but it does allow the result to be forward declared. -fn resolveMaybeForwardRef(self: *Assembler, ref: AsmValue.Ref) !AsmValue { - const value = self.value_map.values()[ref]; +fn resolveMaybeForwardRef(ass: *Assembler, ref: AsmValue.Ref) !AsmValue { + const value = ass.value_map.values()[ref]; switch (value) { .just_declared => { - const name = self.value_map.keys()[ref]; + const name = ass.value_map.keys()[ref]; // TODO: Improve source location. - return self.fail(0, "self-referential parameter %{s}", .{name}); + return ass.fail(0, "ass-referential parameter %{s}", .{name}); }, else => return value, } } -/// Resolve a value reference. This function -/// makes sure that the result is not self-referential, nor that it is forward declared. -fn resolveRef(self: *Assembler, ref: AsmValue.Ref) !AsmValue { - const value = try self.resolveMaybeForwardRef(ref); +fn resolveRef(ass: *Assembler, ref: AsmValue.Ref) !AsmValue { + const value = try ass.resolveMaybeForwardRef(ref); switch (value) { .just_declared => unreachable, .unresolved_forward_reference => { - const name = self.value_map.keys()[ref]; + const name = ass.value_map.keys()[ref]; // TODO: Improve source location. - return self.fail(0, "reference to undeclared result-id %{s}", .{name}); + return ass.fail(0, "reference to undeclared result-id %{s}", .{name}); }, else => return value, } } -fn resolveRefId(self: *Assembler, ref: AsmValue.Ref) !Id { - const value = try self.resolveRef(ref); +fn resolveRefId(ass: *Assembler, ref: AsmValue.Ref) !Id { + const value = try ass.resolveRef(ref); return value.resultId(); } -/// Attempt to parse an instruction into `self.inst`. -/// If this function returns `error.AssembleFail`, an explanatory -/// error message has been emitted into `self.errors`. -fn parseInstruction(self: *Assembler) !void { - self.inst.opcode = undefined; - self.inst.operands.clearRetainingCapacity(); - self.inst.string_bytes.clearRetainingCapacity(); - - const lhs_result_tok = self.currentToken(); - const maybe_lhs_result: ?AsmValue.Ref = if (self.eatToken(.result_id_assign)) blk: { - const name = self.tokenText(lhs_result_tok)[1..]; - const entry = try self.value_map.getOrPut(self.gpa, name); - try self.expectToken(.equals); +fn parseInstruction(ass: *Assembler) !void { + const gpa = ass.cg.module.gpa; + + ass.inst.opcode = undefined; + ass.inst.operands.clearRetainingCapacity(); + ass.inst.string_bytes.clearRetainingCapacity(); + + const lhs_result_tok = ass.currentToken(); + const maybe_lhs_result: ?AsmValue.Ref = if (ass.eatToken(.result_id_assign)) blk: { + const name = ass.tokenText(lhs_result_tok)[1..]; + const entry = try ass.value_map.getOrPut(gpa, name); + try ass.expectToken(.equals); if (!entry.found_existing) { entry.value_ptr.* = .just_declared; } break :blk @intCast(entry.index); } else null; - const opcode_tok = self.currentToken(); + const opcode_tok = ass.currentToken(); if (maybe_lhs_result != null) { - try self.expectToken(.opcode); - } else if (!self.eatToken(.opcode)) { - return self.fail(opcode_tok.start, "expected start of instruction, found {s}", .{opcode_tok.tag.name()}); + try ass.expectToken(.opcode); + } else if (!ass.eatToken(.opcode)) { + return ass.fail(opcode_tok.start, "expected start of instruction, found {s}", .{opcode_tok.tag.name()}); } - const opcode_text = self.tokenText(opcode_tok); - const index = self.instruction_map.getIndex(opcode_text) orelse { - return self.fail(opcode_tok.start, "invalid opcode '{s}'", .{opcode_text}); + const opcode_text = ass.tokenText(opcode_tok); + const index = ass.inst_map.getIndex(opcode_text) orelse { + return ass.fail(opcode_tok.start, "invalid opcode '{s}'", .{opcode_text}); }; const inst = spec.InstructionSet.core.instructions()[index]; - self.inst.opcode = @enumFromInt(inst.opcode); + ass.inst.opcode = @enumFromInt(inst.opcode); const expected_operands = inst.operands; // This is a loop because the result-id is not always the first operand. @@ -603,66 +467,67 @@ fn parseInstruction(self: *Assembler) !void { } else false; if (requires_lhs_result and maybe_lhs_result == null) { - return self.fail(opcode_tok.start, "opcode '{s}' expects result on left-hand side", .{@tagName(self.inst.opcode)}); + return ass.fail(opcode_tok.start, "opcode '{s}' expects result on left-hand side", .{@tagName(ass.inst.opcode)}); } else if (!requires_lhs_result and maybe_lhs_result != null) { - return self.fail( + return ass.fail( lhs_result_tok.start, "opcode '{s}' does not expect a result-id on the left-hand side", - .{@tagName(self.inst.opcode)}, + .{@tagName(ass.inst.opcode)}, ); } for (expected_operands) |operand| { if (operand.kind == .id_result) { - try self.inst.operands.append(self.gpa, .{ .result_id = maybe_lhs_result.? }); + try ass.inst.operands.append(gpa, .{ .result_id = maybe_lhs_result.? }); continue; } switch (operand.quantifier) { - .required => if (self.isAtInstructionBoundary()) { - return self.fail( - self.currentToken().start, + .required => if (ass.isAtInstructionBoundary()) { + return ass.fail( + ass.currentToken().start, "missing required operand", // TODO: Operand name? .{}, ); } else { - try self.parseOperand(operand.kind); + try ass.parseOperand(operand.kind); }, - .optional => if (!self.isAtInstructionBoundary()) { - try self.parseOperand(operand.kind); + .optional => if (!ass.isAtInstructionBoundary()) { + try ass.parseOperand(operand.kind); }, - .variadic => while (!self.isAtInstructionBoundary()) { - try self.parseOperand(operand.kind); + .variadic => while (!ass.isAtInstructionBoundary()) { + try ass.parseOperand(operand.kind); }, } } } -/// Parse a single operand of a particular type. -fn parseOperand(self: *Assembler, kind: spec.OperandKind) Error!void { +fn parseOperand(ass: *Assembler, kind: spec.OperandKind) Error!void { switch (kind.category()) { - .bit_enum => try self.parseBitEnum(kind), - .value_enum => try self.parseValueEnum(kind), - .id => try self.parseRefId(), + .bit_enum => try ass.parseBitEnum(kind), + .value_enum => try ass.parseValueEnum(kind), + .id => try ass.parseRefId(), else => switch (kind) { - .literal_integer => try self.parseLiteralInteger(), - .literal_string => try self.parseString(), - .literal_context_dependent_number => try self.parseContextDependentNumber(), - .literal_ext_inst_integer => try self.parseLiteralExtInstInteger(), - .pair_id_ref_id_ref => try self.parsePhiSource(), - else => return self.todo("parse operand of type {s}", .{@tagName(kind)}), + .literal_integer => try ass.parseLiteralInteger(), + .literal_string => try ass.parseString(), + .literal_context_dependent_number => try ass.parseContextDependentNumber(), + .literal_ext_inst_integer => try ass.parseLiteralExtInstInteger(), + .pair_id_ref_id_ref => try ass.parsePhiSource(), + else => return ass.todo("parse operand of type {s}", .{@tagName(kind)}), }, } } /// Also handles parsing any required extra operands. -fn parseBitEnum(self: *Assembler, kind: spec.OperandKind) !void { - var tok = self.currentToken(); - try self.expectToken(.value); +fn parseBitEnum(ass: *Assembler, kind: spec.OperandKind) !void { + const gpa = ass.cg.module.gpa; + + var tok = ass.currentToken(); + try ass.expectToken(.value); - var text = self.tokenText(tok); + var text = ass.tokenText(tok); if (std.mem.eql(u8, text, "None")) { - try self.inst.operands.append(self.gpa, .{ .value = 0 }); + try ass.inst.operands.append(gpa, .{ .value = 0 }); return; } @@ -673,18 +538,18 @@ fn parseBitEnum(self: *Assembler, kind: spec.OperandKind) !void { if (std.mem.eql(u8, enumerant.name, text)) break enumerant; } else { - return self.fail(tok.start, "'{s}' is not a valid flag for bitmask {s}", .{ text, @tagName(kind) }); + return ass.fail(tok.start, "'{s}' is not a valid flag for bitmask {s}", .{ text, @tagName(kind) }); }; mask |= enumerant.value; - if (!self.eatToken(.pipe)) + if (!ass.eatToken(.pipe)) break; - tok = self.currentToken(); - try self.expectToken(.value); - text = self.tokenText(tok); + tok = ass.currentToken(); + try ass.expectToken(.value); + text = ass.tokenText(tok); } - try self.inst.operands.append(self.gpa, .{ .value = mask }); + try ass.inst.operands.append(gpa, .{ .value = mask }); // Assume values are sorted. // TODO: ensure in generator. @@ -693,43 +558,45 @@ fn parseBitEnum(self: *Assembler, kind: spec.OperandKind) !void { continue; for (enumerant.parameters) |param_kind| { - if (self.isAtInstructionBoundary()) { - return self.fail(self.currentToken().start, "missing required parameter for bit flag '{s}'", .{enumerant.name}); + if (ass.isAtInstructionBoundary()) { + return ass.fail(ass.currentToken().start, "missing required parameter for bit flag '{s}'", .{enumerant.name}); } - try self.parseOperand(param_kind); + try ass.parseOperand(param_kind); } } } /// Also handles parsing any required extra operands. -fn parseValueEnum(self: *Assembler, kind: spec.OperandKind) !void { - const tok = self.currentToken(); - if (self.eatToken(.placeholder)) { - const name = self.tokenText(tok)[1..]; - const value = self.value_map.get(name) orelse { - return self.fail(tok.start, "invalid placeholder '${s}'", .{name}); +fn parseValueEnum(ass: *Assembler, kind: spec.OperandKind) !void { + const gpa = ass.cg.module.gpa; + + const tok = ass.currentToken(); + if (ass.eatToken(.placeholder)) { + const name = ass.tokenText(tok)[1..]; + const value = ass.value_map.get(name) orelse { + return ass.fail(tok.start, "invalid placeholder '${s}'", .{name}); }; switch (value) { .constant => |literal32| { - try self.inst.operands.append(self.gpa, .{ .value = literal32 }); + try ass.inst.operands.append(gpa, .{ .value = literal32 }); }, .string => |str| { const enumerant = for (kind.enumerants()) |enumerant| { if (std.mem.eql(u8, enumerant.name, str)) break enumerant; } else { - return self.fail(tok.start, "'{s}' is not a valid value for enumeration {s}", .{ str, @tagName(kind) }); + return ass.fail(tok.start, "'{s}' is not a valid value for enumeration {s}", .{ str, @tagName(kind) }); }; - try self.inst.operands.append(self.gpa, .{ .value = enumerant.value }); + try ass.inst.operands.append(gpa, .{ .value = enumerant.value }); }, - else => return self.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}), + else => return ass.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}), } return; } - try self.expectToken(.value); + try ass.expectToken(.value); - const text = self.tokenText(tok); + const text = ass.tokenText(tok); const int_value = std.fmt.parseInt(u32, text, 0) catch null; const enumerant = for (kind.enumerants()) |enumerant| { if (int_value) |v| { @@ -738,182 +605,194 @@ fn parseValueEnum(self: *Assembler, kind: spec.OperandKind) !void { if (std.mem.eql(u8, enumerant.name, text)) break enumerant; } } else { - return self.fail(tok.start, "'{s}' is not a valid value for enumeration {s}", .{ text, @tagName(kind) }); + return ass.fail(tok.start, "'{s}' is not a valid value for enumeration {s}", .{ text, @tagName(kind) }); }; - try self.inst.operands.append(self.gpa, .{ .value = enumerant.value }); + try ass.inst.operands.append(gpa, .{ .value = enumerant.value }); for (enumerant.parameters) |param_kind| { - if (self.isAtInstructionBoundary()) { - return self.fail(self.currentToken().start, "missing required parameter for enum variant '{s}'", .{enumerant.name}); + if (ass.isAtInstructionBoundary()) { + return ass.fail(ass.currentToken().start, "missing required parameter for enum variant '{s}'", .{enumerant.name}); } - try self.parseOperand(param_kind); + try ass.parseOperand(param_kind); } } -fn parseRefId(self: *Assembler) !void { - const tok = self.currentToken(); - try self.expectToken(.result_id); +fn parseRefId(ass: *Assembler) !void { + const gpa = ass.cg.module.gpa; + + const tok = ass.currentToken(); + try ass.expectToken(.result_id); - const name = self.tokenText(tok)[1..]; - const entry = try self.value_map.getOrPut(self.gpa, name); + const name = ass.tokenText(tok)[1..]; + const entry = try ass.value_map.getOrPut(gpa, name); if (!entry.found_existing) { entry.value_ptr.* = .unresolved_forward_reference; } const index: AsmValue.Ref = @intCast(entry.index); - try self.inst.operands.append(self.gpa, .{ .ref_id = index }); + try ass.inst.operands.append(gpa, .{ .ref_id = index }); } -fn parseLiteralInteger(self: *Assembler) !void { - const tok = self.currentToken(); - if (self.eatToken(.placeholder)) { - const name = self.tokenText(tok)[1..]; - const value = self.value_map.get(name) orelse { - return self.fail(tok.start, "invalid placeholder '${s}'", .{name}); +fn parseLiteralInteger(ass: *Assembler) !void { + const gpa = ass.cg.module.gpa; + + const tok = ass.currentToken(); + if (ass.eatToken(.placeholder)) { + const name = ass.tokenText(tok)[1..]; + const value = ass.value_map.get(name) orelse { + return ass.fail(tok.start, "invalid placeholder '${s}'", .{name}); }; switch (value) { .constant => |literal32| { - try self.inst.operands.append(self.gpa, .{ .literal32 = literal32 }); + try ass.inst.operands.append(gpa, .{ .literal32 = literal32 }); }, else => { - return self.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}); + return ass.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}); }, } return; } - try self.expectToken(.value); + try ass.expectToken(.value); // According to the SPIR-V machine readable grammar, a LiteralInteger // may consist of one or more words. From the SPIR-V docs it seems like there // only one instruction where multiple words are allowed, the literals that make up the // switch cases of OpSwitch. This case is handled separately, and so we just assume // everything is a 32-bit integer in this function. - const text = self.tokenText(tok); + const text = ass.tokenText(tok); const value = std.fmt.parseInt(u32, text, 0) catch { - return self.fail(tok.start, "'{s}' is not a valid 32-bit integer literal", .{text}); + return ass.fail(tok.start, "'{s}' is not a valid 32-bit integer literal", .{text}); }; - try self.inst.operands.append(self.gpa, .{ .literal32 = value }); + try ass.inst.operands.append(gpa, .{ .literal32 = value }); } -fn parseLiteralExtInstInteger(self: *Assembler) !void { - const tok = self.currentToken(); - if (self.eatToken(.placeholder)) { - const name = self.tokenText(tok)[1..]; - const value = self.value_map.get(name) orelse { - return self.fail(tok.start, "invalid placeholder '${s}'", .{name}); +fn parseLiteralExtInstInteger(ass: *Assembler) !void { + const gpa = ass.cg.module.gpa; + + const tok = ass.currentToken(); + if (ass.eatToken(.placeholder)) { + const name = ass.tokenText(tok)[1..]; + const value = ass.value_map.get(name) orelse { + return ass.fail(tok.start, "invalid placeholder '${s}'", .{name}); }; switch (value) { .constant => |literal32| { - try self.inst.operands.append(self.gpa, .{ .literal32 = literal32 }); + try ass.inst.operands.append(gpa, .{ .literal32 = literal32 }); }, else => { - return self.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}); + return ass.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}); }, } return; } - try self.expectToken(.value); - const text = self.tokenText(tok); + try ass.expectToken(.value); + const text = ass.tokenText(tok); const value = std.fmt.parseInt(u32, text, 0) catch { - return self.fail(tok.start, "'{s}' is not a valid 32-bit integer literal", .{text}); + return ass.fail(tok.start, "'{s}' is not a valid 32-bit integer literal", .{text}); }; - try self.inst.operands.append(self.gpa, .{ .literal32 = value }); + try ass.inst.operands.append(gpa, .{ .literal32 = value }); } -fn parseString(self: *Assembler) !void { - const tok = self.currentToken(); - try self.expectToken(.string); +fn parseString(ass: *Assembler) !void { + const gpa = ass.cg.module.gpa; + + const tok = ass.currentToken(); + try ass.expectToken(.string); // Note, the string might not have a closing quote. In this case, // an error is already emitted but we are trying to continue processing // anyway, so in this function we have to deal with that situation. - const text = self.tokenText(tok); + const text = ass.tokenText(tok); assert(text.len > 0 and text[0] == '"'); const literal = if (text.len != 1 and text[text.len - 1] == '"') text[1 .. text.len - 1] else text[1..]; - const string_offset: u32 = @intCast(self.inst.string_bytes.items.len); - try self.inst.string_bytes.ensureUnusedCapacity(self.gpa, literal.len + 1); - self.inst.string_bytes.appendSliceAssumeCapacity(literal); - self.inst.string_bytes.appendAssumeCapacity(0); + const string_offset: u32 = @intCast(ass.inst.string_bytes.items.len); + try ass.inst.string_bytes.ensureUnusedCapacity(gpa, literal.len + 1); + ass.inst.string_bytes.appendSliceAssumeCapacity(literal); + ass.inst.string_bytes.appendAssumeCapacity(0); - try self.inst.operands.append(self.gpa, .{ .string = string_offset }); + try ass.inst.operands.append(gpa, .{ .string = string_offset }); } -fn parseContextDependentNumber(self: *Assembler) !void { +fn parseContextDependentNumber(ass: *Assembler) !void { + const module = ass.cg.module; + // For context dependent numbers, the actual type to parse is determined by the instruction. // Currently, this operand appears in OpConstant and OpSpecConstant, where the too-be-parsed type // is determined by the result type. That means that in this instructions we have to resolve the // operand type early and look at the result to see how we need to proceed. - assert(self.inst.opcode == .OpConstant or self.inst.opcode == .OpSpecConstant); + assert(ass.inst.opcode == .OpConstant or ass.inst.opcode == .OpSpecConstant); - const tok = self.currentToken(); - const result = try self.resolveRef(self.inst.operands.items[0].ref_id); + const tok = ass.currentToken(); + const result = try ass.resolveRef(ass.inst.operands.items[0].ref_id); const result_id = result.resultId(); // We are going to cheat a little bit: The types we are interested in, int and float, - // are added to the module and cached via self.spv.intType and self.spv.floatType. Therefore, + // are added to the module and cached via module.intType and module.floatType. Therefore, // we can determine the width of these types by directly checking the cache. // This only works if the Assembler and codegen both use spv.intType and spv.floatType though. // We don't expect there to be many of these types, so just look it up every time. // TODO: Count be improved to be a little bit more efficent. { - var it = self.spv.cache.int_types.iterator(); + var it = module.cache.int_types.iterator(); while (it.next()) |entry| { const id = entry.value_ptr.*; if (id != result_id) continue; const info = entry.key_ptr.*; - return try self.parseContextDependentInt(info.signedness, info.bits); + return try ass.parseContextDependentInt(info.signedness, info.bits); } } { - var it = self.spv.cache.float_types.iterator(); + var it = module.cache.float_types.iterator(); while (it.next()) |entry| { const id = entry.value_ptr.*; if (id != result_id) continue; const info = entry.key_ptr.*; switch (info.bits) { - 16 => try self.parseContextDependentFloat(16), - 32 => try self.parseContextDependentFloat(32), - 64 => try self.parseContextDependentFloat(64), - else => return self.fail(tok.start, "cannot parse {}-bit info literal", .{info.bits}), + 16 => try ass.parseContextDependentFloat(16), + 32 => try ass.parseContextDependentFloat(32), + 64 => try ass.parseContextDependentFloat(64), + else => return ass.fail(tok.start, "cannot parse {}-bit info literal", .{info.bits}), } } } - return self.fail(tok.start, "cannot parse literal constant", .{}); + return ass.fail(tok.start, "cannot parse literal constant", .{}); } -fn parseContextDependentInt(self: *Assembler, signedness: std.builtin.Signedness, width: u32) !void { - const tok = self.currentToken(); - if (self.eatToken(.placeholder)) { - const name = self.tokenText(tok)[1..]; - const value = self.value_map.get(name) orelse { - return self.fail(tok.start, "invalid placeholder '${s}'", .{name}); +fn parseContextDependentInt(ass: *Assembler, signedness: std.builtin.Signedness, width: u32) !void { + const gpa = ass.cg.module.gpa; + + const tok = ass.currentToken(); + if (ass.eatToken(.placeholder)) { + const name = ass.tokenText(tok)[1..]; + const value = ass.value_map.get(name) orelse { + return ass.fail(tok.start, "invalid placeholder '${s}'", .{name}); }; switch (value) { .constant => |literal32| { - try self.inst.operands.append(self.gpa, .{ .literal32 = literal32 }); + try ass.inst.operands.append(gpa, .{ .literal32 = literal32 }); }, else => { - return self.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}); + return ass.fail(tok.start, "value '{s}' cannot be used as placeholder", .{name}); }, } return; } - try self.expectToken(.value); + try ass.expectToken(.value); if (width == 0 or width > 2 * @bitSizeOf(spec.Word)) { - return self.fail(tok.start, "cannot parse {}-bit integer literal", .{width}); + return ass.fail(tok.start, "cannot parse {}-bit integer literal", .{width}); } - const text = self.tokenText(tok); + const text = ass.tokenText(tok); invalid: { // Just parse the integer as the next larger integer type, and check if it overflows afterwards. const int = std.fmt.parseInt(i128, text, 0) catch break :invalid; @@ -928,112 +807,166 @@ fn parseContextDependentInt(self: *Assembler, signedness: std.builtin.Signedness // Note, we store the sign-extended version here. if (width <= @bitSizeOf(spec.Word)) { - try self.inst.operands.append(self.gpa, .{ .literal32 = @truncate(@as(u128, @bitCast(int))) }); + try ass.inst.operands.append(gpa, .{ .literal32 = @truncate(@as(u128, @bitCast(int))) }); } else { - try self.inst.operands.append(self.gpa, .{ .literal64 = @truncate(@as(u128, @bitCast(int))) }); + try ass.inst.operands.append(gpa, .{ .literal64 = @truncate(@as(u128, @bitCast(int))) }); } return; } - return self.fail(tok.start, "'{s}' is not a valid {s} {}-bit int literal", .{ text, @tagName(signedness), width }); + return ass.fail(tok.start, "'{s}' is not a valid {s} {}-bit int literal", .{ text, @tagName(signedness), width }); } -fn parseContextDependentFloat(self: *Assembler, comptime width: u16) !void { +fn parseContextDependentFloat(ass: *Assembler, comptime width: u16) !void { + const gpa = ass.cg.module.gpa; + const Float = std.meta.Float(width); const Int = std.meta.Int(.unsigned, width); - const tok = self.currentToken(); - try self.expectToken(.value); + const tok = ass.currentToken(); + try ass.expectToken(.value); - const text = self.tokenText(tok); + const text = ass.tokenText(tok); const value = std.fmt.parseFloat(Float, text) catch { - return self.fail(tok.start, "'{s}' is not a valid {}-bit float literal", .{ text, width }); + return ass.fail(tok.start, "'{s}' is not a valid {}-bit float literal", .{ text, width }); }; const float_bits: Int = @bitCast(value); if (width <= @bitSizeOf(spec.Word)) { - try self.inst.operands.append(self.gpa, .{ .literal32 = float_bits }); + try ass.inst.operands.append(gpa, .{ .literal32 = float_bits }); } else { assert(width <= 2 * @bitSizeOf(spec.Word)); - try self.inst.operands.append(self.gpa, .{ .literal64 = float_bits }); + try ass.inst.operands.append(gpa, .{ .literal64 = float_bits }); } } -fn parsePhiSource(self: *Assembler) !void { - try self.parseRefId(); - if (self.isAtInstructionBoundary()) { - return self.fail(self.currentToken().start, "missing phi block parent", .{}); +fn parsePhiSource(ass: *Assembler) !void { + try ass.parseRefId(); + if (ass.isAtInstructionBoundary()) { + return ass.fail(ass.currentToken().start, "missing phi block parent", .{}); } - try self.parseRefId(); + try ass.parseRefId(); } -/// Returns whether the `current_token` cursor is currently pointing -/// at the start of a new instruction. -fn isAtInstructionBoundary(self: Assembler) bool { - return switch (self.currentToken().tag) { +/// Returns whether the `current_token` cursor +/// is currently pointing at the start of a new instruction. +fn isAtInstructionBoundary(ass: Assembler) bool { + return switch (ass.currentToken().tag) { .opcode, .result_id_assign, .eof => true, else => false, }; } -fn expectToken(self: *Assembler, tag: Token.Tag) !void { - if (self.eatToken(tag)) +fn expectToken(ass: *Assembler, tag: Token.Tag) !void { + if (ass.eatToken(tag)) return; - return self.fail(self.currentToken().start, "unexpected {s}, expected {s}", .{ - self.currentToken().tag.name(), + return ass.fail(ass.currentToken().start, "unexpected {s}, expected {s}", .{ + ass.currentToken().tag.name(), tag.name(), }); } -fn eatToken(self: *Assembler, tag: Token.Tag) bool { - if (self.testToken(tag)) { - self.current_token += 1; +fn eatToken(ass: *Assembler, tag: Token.Tag) bool { + if (ass.testToken(tag)) { + ass.current_token += 1; return true; } return false; } -fn testToken(self: Assembler, tag: Token.Tag) bool { - return self.currentToken().tag == tag; +fn testToken(ass: Assembler, tag: Token.Tag) bool { + return ass.currentToken().tag == tag; } -fn currentToken(self: Assembler) Token { - return self.tokens.items[self.current_token]; +fn currentToken(ass: Assembler) Token { + return ass.tokens.items[ass.current_token]; } -fn tokenText(self: Assembler, tok: Token) []const u8 { - return self.src[tok.start..tok.end]; +fn tokenText(ass: Assembler, tok: Token) []const u8 { + return ass.src[tok.start..tok.end]; } -/// Tokenize `self.src` and put the tokens in `self.tokens`. -/// Any errors encountered are appended to `self.errors`. -fn tokenize(self: *Assembler) !void { - self.tokens.clearRetainingCapacity(); +/// Tokenize `ass.src` and put the tokens in `ass.tokens`. +/// Any errors encountered are appended to `ass.errors`. +fn tokenize(ass: *Assembler) !void { + const gpa = ass.cg.module.gpa; + + ass.tokens.clearRetainingCapacity(); var offset: u32 = 0; while (true) { - const tok = try self.nextToken(offset); + const tok = try ass.nextToken(offset); // Resolve result-id assignment now. - // Note: If the previous token wasn't a result-id, just ignore it, + // NOTE: If the previous token wasn't a result-id, just ignore it, // we will catch it while parsing. - if (tok.tag == .equals and self.tokens.items[self.tokens.items.len - 1].tag == .result_id) { - self.tokens.items[self.tokens.items.len - 1].tag = .result_id_assign; + if (tok.tag == .equals and ass.tokens.items[ass.tokens.items.len - 1].tag == .result_id) { + ass.tokens.items[ass.tokens.items.len - 1].tag = .result_id_assign; } - try self.tokens.append(self.gpa, tok); + try ass.tokens.append(gpa, tok); if (tok.tag == .eof) break; offset = tok.end; } } +const Token = struct { + tag: Tag, + start: u32, + end: u32, + + const Tag = enum { + /// Returned when there was no more input to match. + eof, + /// %identifier + result_id, + /// %identifier when appearing on the LHS of an equals sign. + /// While not technically a token, its relatively easy to resolve + /// this during lexical analysis and relieves a bunch of headaches + /// during parsing. + result_id_assign, + /// Mask, int, or float. These are grouped together as some + /// SPIR-V enumerants look a bit like integers as well (for example + /// "3D"), and so it is easier to just interpret them as the expected + /// type when resolving an instruction's operands. + value, + /// An enumerant that looks like an opcode, that is, OpXxxx. + /// Not necessarily a *valid* opcode. + opcode, + /// String literals. + /// Note, this token is also returned for unterminated + /// strings. In this case the closing " is not present. + string, + /// |. + pipe, + /// =. + equals, + /// $identifier. This is used (for now) for constant values, like integers. + /// These can be used in place of a normal `value`. + placeholder, + + fn name(tag: Tag) []const u8 { + return switch (tag) { + .eof => "", + .result_id => "", + .result_id_assign => "", + .value => "", + .opcode => "", + .string => "", + .pipe => "'|'", + .equals => "'='", + .placeholder => "", + }; + } + }; +}; + /// Retrieve the next token from the input. This function will assert /// that the token is surrounded by whitespace if required, but will not /// interpret the token yet. -/// Note: This function doesn't handle .result_id_assign - this is handled in -/// tokenize(). -fn nextToken(self: *Assembler, start_offset: u32) !Token { +/// NOTE: This function doesn't handle .result_id_assign - this is handled in tokenize(). +fn nextToken(ass: *Assembler, start_offset: u32) !Token { // We generally separate the input into the following types: // - Whitespace. Generally ignored, but also used as delimiter for some // tokens. @@ -1059,8 +992,8 @@ fn nextToken(self: *Assembler, start_offset: u32) !Token { var token_start = start_offset; var offset = start_offset; var tag = Token.Tag.eof; - while (offset < self.src.len) : (offset += 1) { - const c = self.src[offset]; + while (offset < ass.src.len) : (offset += 1) { + const c = ass.src[offset]; switch (state) { .start => switch (c) { ' ', '\t', '\r', '\n' => token_start = offset + 1, @@ -1093,7 +1026,7 @@ fn nextToken(self: *Assembler, start_offset: u32) !Token { }, .value => switch (c) { '"' => { - try self.addError(offset, "unexpected string literal", .{}); + try ass.addError(offset, "unexpected string literal", .{}); // The user most likely just forgot a delimiter here - keep // the tag as value. break; @@ -1105,7 +1038,7 @@ fn nextToken(self: *Assembler, start_offset: u32) !Token { '_', 'a'...'z', 'A'...'Z', '0'...'9' => {}, ' ', '\t', '\r', '\n', '=', '|' => break, else => { - try self.addError(offset, "illegal character in result-id or placeholder", .{}); + try ass.addError(offset, "illegal character in result-id or placeholder", .{}); // Again, probably a forgotten delimiter here. break; }, @@ -1118,7 +1051,7 @@ fn nextToken(self: *Assembler, start_offset: u32) !Token { .string_end => switch (c) { ' ', '\t', '\r', '\n', '=', '|' => break, else => { - try self.addError(offset, "unexpected character after string literal", .{}); + try ass.addError(offset, "unexpected character after string literal", .{}); // The token is still unmistakibly a string. break; }, @@ -1128,7 +1061,7 @@ fn nextToken(self: *Assembler, start_offset: u32) !Token { } } - var tok = Token{ + var tok: Token = .{ .tag = tag, .start = token_start, .end = offset, @@ -1136,13 +1069,13 @@ fn nextToken(self: *Assembler, start_offset: u32) !Token { switch (state) { .string, .escape => { - try self.addError(token_start, "unterminated string", .{}); + try ass.addError(token_start, "unterminated string", .{}); }, .result_id => if (offset - token_start == 1) { - try self.addError(token_start, "result-id must have at least one name character", .{}); + try ass.addError(token_start, "result-id must have at least one name character", .{}); }, .value => { - const text = self.tokenText(tok); + const text = ass.tokenText(tok); const prefix = "Op"; const looks_like_opcode = text.len > prefix.len and std.mem.startsWith(u8, text, prefix) and diff --git a/src/codegen/spirv/CodeGen.zig b/src/codegen/spirv/CodeGen.zig new file mode 100644 index 000000000000..fa52c226bdcb --- /dev/null +++ b/src/codegen/spirv/CodeGen.zig @@ -0,0 +1,6188 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const Target = std.Target; +const Signedness = std.builtin.Signedness; +const assert = std.debug.assert; +const log = std.log.scoped(.codegen); + +const Zcu = @import("../../Zcu.zig"); +const Type = @import("../../Type.zig"); +const Value = @import("../../Value.zig"); +const Air = @import("../../Air.zig"); +const InternPool = @import("../../InternPool.zig"); +const Section = @import("Section.zig"); +const Assembler = @import("Assembler.zig"); + +const spec = @import("spec.zig"); +const Opcode = spec.Opcode; +const Word = spec.Word; +const Id = spec.Id; +const IdRange = spec.IdRange; +const StorageClass = spec.StorageClass; + +const Module = @import("Module.zig"); +const Decl = Module.Decl; +const Repr = Module.Repr; +const InternMap = Module.InternMap; +const PtrTypeMap = Module.PtrTypeMap; + +const CodeGen = @This(); + +pub fn legalizeFeatures(_: *const std.Target) *const Air.Legalize.Features { + return comptime &.initMany(&.{ + .expand_intcast_safe, + .expand_int_from_float_safe, + .expand_int_from_float_optimized_safe, + .expand_add_safe, + .expand_sub_safe, + .expand_mul_safe, + }); +} + +pub const zig_call_abi_ver = 3; + +const ControlFlow = union(enum) { + const Structured = struct { + /// This type indicates the way that a block is terminated. The + /// state of a particular block is used to track how a jump from + /// inside the block must reach the outside. + const Block = union(enum) { + const Incoming = struct { + src_label: Id, + /// Instruction that returns an u32 value of the + /// `Air.Inst.Index` that control flow should jump to. + next_block: Id, + }; + + const SelectionMerge = struct { + /// Incoming block from the `then` label. + /// Note that hte incoming block from the `else` label is + /// either given by the next element in the stack. + incoming: Incoming, + /// The label id of the cond_br's merge block. + /// For the top-most element in the stack, this + /// value is undefined. + merge_block: Id, + }; + + /// For a `selection` type block, we cannot use early exits, and we + /// must generate a 'merge ladder' of OpSelection instructions. To that end, + /// we keep a stack of the merges that still must be closed at the end of + /// a block. + /// + /// This entire structure basically just resembles a tree like + /// a x + /// \ / + /// b o merge + /// \ / + /// c o merge + /// \ / + /// o merge + /// / + /// o jump to next block + selection: struct { + /// In order to know which merges we still need to do, we need to keep + /// a stack of those. + merge_stack: std.ArrayListUnmanaged(SelectionMerge) = .empty, + }, + /// For a `loop` type block, we can early-exit the block by + /// jumping to the loop exit node, and we don't need to generate + /// an entire stack of merges. + loop: struct { + /// The next block to jump to can be determined from any number + /// of conditions that jump to the loop exit. + merges: std.ArrayListUnmanaged(Incoming) = .empty, + /// The label id of the loop's merge block. + merge_block: Id, + }, + + fn deinit(block: *Structured.Block, gpa: Allocator) void { + switch (block.*) { + .selection => |*merge| merge.merge_stack.deinit(gpa), + .loop => |*merge| merge.merges.deinit(gpa), + } + block.* = undefined; + } + }; + /// This determines how exits from the current block must be handled. + block_stack: std.ArrayListUnmanaged(*Structured.Block) = .empty, + block_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, + }; + + const Unstructured = struct { + const Incoming = struct { + src_label: Id, + break_value_id: Id, + }; + + const Block = struct { + label: ?Id = null, + incoming_blocks: std.ArrayListUnmanaged(Incoming) = .empty, + }; + + /// We need to keep track of result ids for block labels, as well as the 'incoming' + /// blocks for a block. + blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, *Block) = .empty, + }; + + structured: Structured, + unstructured: Unstructured, + + pub fn deinit(cg: *ControlFlow, gpa: Allocator) void { + switch (cg.*) { + .structured => |*cf| { + cf.block_stack.deinit(gpa); + cf.block_results.deinit(gpa); + }, + .unstructured => |*cf| { + cf.blocks.deinit(gpa); + }, + } + cg.* = undefined; + } +}; + +pt: Zcu.PerThread, +air: Air, +liveness: Air.Liveness, +owner_nav: InternPool.Nav.Index, +module: *Module, +control_flow: ControlFlow, +base_line: u32, +block_label: Id = .none, +next_arg_index: u32 = 0, +args: std.ArrayListUnmanaged(Id) = .empty, +inst_results: std.AutoHashMapUnmanaged(Air.Inst.Index, Id) = .empty, +id_scratch: std.ArrayListUnmanaged(Id) = .empty, +prologue: Section = .{}, +body: Section = .{}, +error_msg: ?*Zcu.ErrorMsg = null, + +pub fn deinit(cg: *CodeGen) void { + const gpa = cg.module.gpa; + cg.control_flow.deinit(gpa); + cg.args.deinit(gpa); + cg.inst_results.deinit(gpa); + cg.id_scratch.deinit(gpa); + cg.prologue.deinit(gpa); + cg.body.deinit(gpa); +} + +const Error = error{ CodegenFail, OutOfMemory }; + +pub fn genNav(cg: *CodeGen, do_codegen: bool) Error!void { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const target = zcu.getTarget(); + + const nav = ip.getNav(cg.owner_nav); + const val = zcu.navValue(cg.owner_nav); + const ty = val.typeOf(zcu); + + if (!do_codegen and !ty.hasRuntimeBits(zcu)) return; + + const spv_decl_index = try cg.module.resolveNav(ip, cg.owner_nav); + const decl = cg.module.declPtr(spv_decl_index); + const result_id = decl.result_id; + decl.begin_dep = cg.module.decl_deps.items.len; + + switch (decl.kind) { + .func => { + const fn_info = zcu.typeToFunc(ty).?; + const return_ty_id = try cg.resolveFnReturnType(.fromInterned(fn_info.return_type)); + const is_test = zcu.test_functions.contains(cg.owner_nav); + + const func_result_id = if (is_test) cg.module.allocId() else result_id; + const prototype_ty_id = try cg.resolveType(ty, .direct); + try cg.prologue.emit(gpa, .OpFunction, .{ + .id_result_type = return_ty_id, + .id_result = func_result_id, + .function_type = prototype_ty_id, + // Note: the backend will never be asked to generate an inline function + // (this is handled in sema), so we don't need to set function_control here. + .function_control = .{}, + }); + + comptime assert(zig_call_abi_ver == 3); + try cg.args.ensureUnusedCapacity(gpa, fn_info.param_types.len); + for (fn_info.param_types.get(ip)) |param_ty_index| { + const param_ty: Type = .fromInterned(param_ty_index); + if (!param_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; + + const param_type_id = try cg.resolveType(param_ty, .direct); + const arg_result_id = cg.module.allocId(); + try cg.prologue.emit(gpa, .OpFunctionParameter, .{ + .id_result_type = param_type_id, + .id_result = arg_result_id, + }); + cg.args.appendAssumeCapacity(arg_result_id); + } + + // TODO: This could probably be done in a better way... + const root_block_id = cg.module.allocId(); + + // The root block of a function declaration should appear before OpVariable instructions, + // so it is generated into the function's prologue. + try cg.prologue.emit(gpa, .OpLabel, .{ + .id_result = root_block_id, + }); + cg.block_label = root_block_id; + + const main_body = cg.air.getMainBody(); + switch (cg.control_flow) { + .structured => { + _ = try cg.genStructuredBody(.selection, main_body); + // We always expect paths to here to end, but we still need the block + // to act as a dummy merge block. + try cg.body.emit(gpa, .OpUnreachable, {}); + }, + .unstructured => { + try cg.genBody(main_body); + }, + } + try cg.body.emit(gpa, .OpFunctionEnd, {}); + // Append the actual code into the functions section. + try cg.module.sections.functions.append(gpa, cg.prologue); + try cg.module.sections.functions.append(gpa, cg.body); + + // Temporarily generate a test kernel declaration if this is a test function. + if (is_test) { + try cg.generateTestEntryPoint(nav.fqn.toSlice(ip), spv_decl_index, func_result_id); + } + + try cg.module.debugName(func_result_id, nav.fqn.toSlice(ip)); + }, + .global => { + const maybe_init_val: ?Value = switch (ip.indexToKey(val.toIntern())) { + .func => unreachable, + .variable => |variable| .fromInterned(variable.init), + .@"extern" => null, + else => val, + }; + assert(maybe_init_val == null); // TODO + + const storage_class = cg.module.storageClass(nav.getAddrspace()); + assert(storage_class != .generic); // These should be instance globals + + const ty_id = try cg.resolveType(ty, .indirect); + const ptr_ty_id = try cg.module.ptrType(ty_id, storage_class); + + try cg.module.sections.globals.emit(gpa, .OpVariable, .{ + .id_result_type = ptr_ty_id, + .id_result = result_id, + .storage_class = storage_class, + }); + + switch (target.os.tag) { + .vulkan, .opengl => { + if (ty.zigTypeTag(zcu) == .@"struct") { + switch (storage_class) { + .uniform, .push_constant => try cg.module.decorate(ty_id, .block), + else => {}, + } + } + + switch (ip.indexToKey(ty.toIntern())) { + .func_type, .opaque_type => {}, + else => { + try cg.module.decorate(ptr_ty_id, .{ + .array_stride = .{ .array_stride = @intCast(ty.abiSize(zcu)) }, + }); + }, + } + }, + else => {}, + } + + if (std.meta.stringToEnum(spec.BuiltIn, nav.fqn.toSlice(ip))) |builtin| { + try cg.module.decorate(result_id, .{ .built_in = .{ .built_in = builtin } }); + } + + try cg.module.debugName(result_id, nav.fqn.toSlice(ip)); + }, + .invocation_global => { + const maybe_init_val: ?Value = switch (ip.indexToKey(val.toIntern())) { + .func => unreachable, + .variable => |variable| .fromInterned(variable.init), + .@"extern" => null, + else => val, + }; + + const ty_id = try cg.resolveType(ty, .indirect); + const ptr_ty_id = try cg.module.ptrType(ty_id, .function); + + if (maybe_init_val) |init_val| { + // TODO: Combine with resolveAnonDecl? + const void_ty_id = try cg.resolveType(.void, .direct); + const initializer_proto_ty_id = try cg.module.functionType(void_ty_id, &.{}); + + const initializer_id = cg.module.allocId(); + try cg.prologue.emit(gpa, .OpFunction, .{ + .id_result_type = try cg.resolveType(.void, .direct), + .id_result = initializer_id, + .function_control = .{}, + .function_type = initializer_proto_ty_id, + }); + + const root_block_id = cg.module.allocId(); + try cg.prologue.emit(gpa, .OpLabel, .{ + .id_result = root_block_id, + }); + cg.block_label = root_block_id; + + const val_id = try cg.constant(ty, init_val, .indirect); + try cg.body.emit(gpa, .OpStore, .{ + .pointer = result_id, + .object = val_id, + }); + + try cg.body.emit(gpa, .OpReturn, {}); + try cg.body.emit(gpa, .OpFunctionEnd, {}); + try cg.module.sections.functions.append(gpa, cg.prologue); + try cg.module.sections.functions.append(gpa, cg.body); + + try cg.module.debugNameFmt(initializer_id, "initializer of {f}", .{nav.fqn.fmt(ip)}); + + try cg.module.sections.globals.emit(gpa, .OpExtInst, .{ + .id_result_type = ptr_ty_id, + .id_result = result_id, + .set = try cg.module.importInstructionSet(.zig), + .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... + .id_ref_4 = &.{initializer_id}, + }); + } else { + try cg.module.sections.globals.emit(gpa, .OpExtInst, .{ + .id_result_type = ptr_ty_id, + .id_result = result_id, + .set = try cg.module.importInstructionSet(.zig), + .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... + .id_ref_4 = &.{}, + }); + } + }, + } + + cg.module.declPtr(spv_decl_index).end_dep = cg.module.decl_deps.items.len; +} + +pub fn fail(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { + @branchHint(.cold); + const zcu = cg.module.zcu; + const src_loc = zcu.navSrcLoc(cg.owner_nav); + assert(cg.error_msg == null); + cg.error_msg = try Zcu.ErrorMsg.create(zcu.gpa, src_loc, format, args); + return error.CodegenFail; +} + +pub fn todo(cg: *CodeGen, comptime format: []const u8, args: anytype) Error { + return cg.fail("TODO (SPIR-V): " ++ format, args); +} + +/// This imports the "default" extended instruction set for the target +/// For OpenCL, OpenCL.std.100. For Vulkan and OpenGL, GLSL.std.450. +fn importExtendedSet(cg: *CodeGen) !Id { + const target = cg.module.zcu.getTarget(); + return switch (target.os.tag) { + .opencl, .amdhsa => try cg.module.importInstructionSet(.@"OpenCL.std"), + .vulkan, .opengl => try cg.module.importInstructionSet(.@"GLSL.std.450"), + else => unreachable, + }; +} + +/// Fetch the result-id for a previously generated instruction or constant. +fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id { + const pt = cg.pt; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + if (try cg.air.value(inst, pt)) |val| { + const ty = cg.typeOf(inst); + if (ty.zigTypeTag(zcu) == .@"fn") { + const fn_nav = switch (zcu.intern_pool.indexToKey(val.ip_index)) { + .@"extern" => |@"extern"| @"extern".owner_nav, + .func => |func| func.owner_nav, + else => unreachable, + }; + const spv_decl_index = try cg.module.resolveNav(ip, fn_nav); + try cg.module.decl_deps.append(cg.module.gpa, spv_decl_index); + return cg.module.declPtr(spv_decl_index).result_id; + } + + return try cg.constant(ty, val, .direct); + } + const index = inst.toIndex().?; + return cg.inst_results.get(index).?; // Assertion means instruction does not dominate usage. +} + +fn resolveUav(cg: *CodeGen, val: InternPool.Index) !Id { + const gpa = cg.module.gpa; + + // TODO: This cannot be a function at this point, but it should probably be handled anyway. + + const zcu = cg.module.zcu; + const ty: Type = .fromInterned(zcu.intern_pool.typeOf(val)); + const ty_id = try cg.resolveType(ty, .indirect); + + const spv_decl_index = blk: { + const entry = try cg.module.uav_link.getOrPut(gpa, .{ val, .function }); + if (entry.found_existing) { + try cg.addFunctionDep(entry.value_ptr.*, .function); + return cg.module.declPtr(entry.value_ptr.*).result_id; + } + + const spv_decl_index = try cg.module.allocDecl(.invocation_global); + try cg.addFunctionDep(spv_decl_index, .function); + entry.value_ptr.* = spv_decl_index; + break :blk spv_decl_index; + }; + + // TODO: At some point we will be able to generate this all constant here, but then all of + // constant() will need to be implemented such that it doesn't generate any at-runtime code. + // NOTE: Because this is a global, we really only want to initialize it once. Therefore the + // constant lowering of this value will need to be deferred to an initializer similar to + // other globals. + + const result_id = cg.module.declPtr(spv_decl_index).result_id; + + { + // Save the current state so that we can temporarily generate into a different function. + // TODO: This should probably be made a little more robust. + const func_prologue = cg.prologue; + const func_body = cg.body; + const block_label = cg.block_label; + defer { + cg.prologue = func_prologue; + cg.body = func_body; + cg.block_label = block_label; + } + + cg.prologue = .{}; + cg.body = .{}; + defer { + cg.prologue.deinit(gpa); + cg.body.deinit(gpa); + } + + const void_ty_id = try cg.resolveType(.void, .direct); + const initializer_proto_ty_id = try cg.module.functionType(void_ty_id, &.{}); + + const initializer_id = cg.module.allocId(); + try cg.prologue.emit(gpa, .OpFunction, .{ + .id_result_type = try cg.resolveType(.void, .direct), + .id_result = initializer_id, + .function_control = .{}, + .function_type = initializer_proto_ty_id, + }); + const root_block_id = cg.module.allocId(); + try cg.prologue.emit(gpa, .OpLabel, .{ + .id_result = root_block_id, + }); + cg.block_label = root_block_id; + + const val_id = try cg.constant(ty, .fromInterned(val), .indirect); + try cg.body.emit(gpa, .OpStore, .{ + .pointer = result_id, + .object = val_id, + }); + + try cg.body.emit(gpa, .OpReturn, {}); + try cg.body.emit(gpa, .OpFunctionEnd, {}); + + try cg.module.sections.functions.append(gpa, cg.prologue); + try cg.module.sections.functions.append(gpa, cg.body); + + try cg.module.debugNameFmt(initializer_id, "initializer of __anon_{d}", .{@intFromEnum(val)}); + + const fn_decl_ptr_ty_id = try cg.module.ptrType(ty_id, .function); + try cg.module.sections.globals.emit(gpa, .OpExtInst, .{ + .id_result_type = fn_decl_ptr_ty_id, + .id_result = result_id, + .set = try cg.module.importInstructionSet(.zig), + .instruction = .{ .inst = 0 }, // TODO: Put this definition somewhere... + .id_ref_4 = &.{initializer_id}, + }); + } + + return result_id; +} + +fn addFunctionDep(cg: *CodeGen, decl_index: Module.Decl.Index, storage_class: StorageClass) !void { + const gpa = cg.module.gpa; + const target = cg.module.zcu.getTarget(); + if (target.cpu.has(.spirv, .v1_4)) { + try cg.module.decl_deps.append(gpa, decl_index); + } else { + // Before version 1.4, the interface’s storage classes are limited to the Input and Output + if (storage_class == .input or storage_class == .output) { + try cg.module.decl_deps.append(gpa, decl_index); + } + } +} + +/// Start a new SPIR-V block, Emits the label of the new block, and stores which +/// block we are currently generating. +/// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to +/// keep track of the previous block. +fn beginSpvBlock(cg: *CodeGen, label: Id) !void { + try cg.body.emit(cg.module.gpa, .OpLabel, .{ .id_result = label }); + cg.block_label = label; +} + +/// Return the amount of bits in the largest supported integer type. This is either 32 (always supported), or 64 (if +/// the Int64 capability is enabled). +/// Note: The extension SPV_INTEL_arbitrary_precision_integers allows any integer size (at least up to 32 bits). +/// In theory that could also be used, but since the spec says that it only guarantees support up to 32-bit ints there +/// is no way of knowing whether those are actually supported. +/// TODO: Maybe this should be cached? +fn largestSupportedIntBits(cg: *CodeGen) u16 { + const target = cg.module.zcu.getTarget(); + if (target.cpu.has(.spirv, .int64) or target.cpu.arch == .spirv64) { + return 64; + } + return 32; +} + +const ArithmeticTypeInfo = struct { + const Class = enum { + bool, + /// A regular, **native**, integer. + /// This is only returned when the backend supports this int as a native type (when + /// the relevant capability is enabled). + integer, + /// A regular float. These are all required to be natively supported. Floating points + /// for which the relevant capability is not enabled are not emulated. + float, + /// An integer of a 'strange' size (which' bit size is not the same as its backing + /// type. **Note**: this may **also** include power-of-2 integers for which the + /// relevant capability is not enabled), but still within the limits of the largest + /// natively supported integer type. + strange_integer, + /// An integer with more bits than the largest natively supported integer type. + composite_integer, + }; + + /// A classification of the inner type. + /// These scenarios will all have to be handled slightly different. + class: Class, + /// The number of bits in the inner type. + /// This is the actual number of bits of the type, not the size of the backing integer. + bits: u16, + /// The number of bits required to store the type. + /// For `integer` and `float`, this is equal to `bits`. + /// For `strange_integer` and `bool` this is the size of the backing integer. + /// For `composite_integer` this is the elements count. + backing_bits: u16, + /// Null if this type is a scalar, or the length of the vector otherwise. + vector_len: ?u32, + /// Whether the inner type is signed. Only relevant for integers. + signedness: std.builtin.Signedness, +}; + +fn arithmeticTypeInfo(cg: *CodeGen, ty: Type) ArithmeticTypeInfo { + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + var scalar_ty = ty.scalarType(zcu); + if (scalar_ty.zigTypeTag(zcu) == .@"enum") { + scalar_ty = scalar_ty.intTagType(zcu); + } + const vector_len = if (ty.isVector(zcu)) ty.vectorLen(zcu) else null; + return switch (scalar_ty.zigTypeTag(zcu)) { + .bool => .{ + .bits = 1, // Doesn't matter for this class. + .backing_bits = cg.module.backingIntBits(1).@"0", + .vector_len = vector_len, + .signedness = .unsigned, // Technically, but doesn't matter for this class. + .class = .bool, + }, + .float => .{ + .bits = scalar_ty.floatBits(target), + .backing_bits = scalar_ty.floatBits(target), // TODO: F80? + .vector_len = vector_len, + .signedness = .signed, // Technically, but doesn't matter for this class. + .class = .float, + }, + .int => blk: { + const int_info = scalar_ty.intInfo(zcu); + // TODO: Maybe it's useful to also return this value. + const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); + break :blk .{ + .bits = int_info.bits, + .backing_bits = backing_bits, + .vector_len = vector_len, + .signedness = int_info.signedness, + .class = class: { + if (big_int) break :class .composite_integer; + break :class if (backing_bits == int_info.bits) .integer else .strange_integer; + }, + }; + }, + .@"enum" => unreachable, + .vector => unreachable, + else => unreachable, // Unhandled arithmetic type + }; +} + +/// Checks whether the type can be directly translated to SPIR-V vectors +fn isSpvVector(cg: *CodeGen, ty: Type) bool { + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + if (ty.zigTypeTag(zcu) != .vector) return false; + + // TODO: This check must be expanded for types that can be represented + // as integers (enums / packed structs?) and types that are represented + // by multiple SPIR-V values. + const scalar_ty = ty.scalarType(zcu); + switch (scalar_ty.zigTypeTag(zcu)) { + .bool, + .int, + .float, + => {}, + else => return false, + } + + const elem_ty = ty.childType(zcu); + const len = ty.vectorLen(zcu); + + if (elem_ty.isNumeric(zcu) or elem_ty.toIntern() == .bool_type) { + if (len > 1 and len <= 4) return true; + if (target.cpu.has(.spirv, .vector16)) return (len == 8 or len == 16); + } + + return false; +} + +/// Emits a bool constant in a particular representation. +fn constBool(cg: *CodeGen, value: bool, repr: Repr) !Id { + return switch (repr) { + .indirect => cg.constInt(.u1, @intFromBool(value)), + .direct => cg.module.constBool(value), + }; +} + +/// Emits an integer constant. +/// This function, unlike Module.constInt, takes care to bitcast +/// the value to an unsigned int first for Kernels. +fn constInt(cg: *CodeGen, ty: Type, value: anytype) !Id { + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + const scalar_ty = ty.scalarType(zcu); + const int_info = scalar_ty.intInfo(zcu); + // Use backing bits so that negatives are sign extended + const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); + assert(backing_bits != 0); // u0 is comptime + + const result_ty_id = try cg.resolveType(scalar_ty, .indirect); + const signedness: Signedness = switch (@typeInfo(@TypeOf(value))) { + .int => |int| int.signedness, + .comptime_int => if (value < 0) .signed else .unsigned, + else => unreachable, + }; + if (@sizeOf(@TypeOf(value)) >= 4 and big_int) { + const value64: u64 = switch (signedness) { + .signed => @bitCast(@as(i64, @intCast(value))), + .unsigned => @as(u64, @intCast(value)), + }; + assert(backing_bits == 64); + return cg.constructComposite(result_ty_id, &.{ + try cg.constInt(.u32, @as(u32, @truncate(value64))), + try cg.constInt(.u32, @as(u32, @truncate(value64 << 32))), + }); + } + + const final_value: spec.LiteralContextDependentNumber = switch (target.os.tag) { + .opencl, .amdhsa => blk: { + const value64: u64 = switch (signedness) { + .signed => @bitCast(@as(i64, @intCast(value))), + .unsigned => @as(u64, @intCast(value)), + }; + + // Manually truncate the value to the right amount of bits. + const truncated_value = if (backing_bits == 64) + value64 + else + value64 & (@as(u64, 1) << @intCast(backing_bits)) - 1; + + break :blk switch (backing_bits) { + 1...32 => .{ .uint32 = @truncate(truncated_value) }, + 33...64 => .{ .uint64 = truncated_value }, + else => unreachable, + }; + }, + else => switch (backing_bits) { + 1...32 => if (signedness == .signed) .{ .int32 = @intCast(value) } else .{ .uint32 = @intCast(value) }, + 33...64 => if (signedness == .signed) .{ .int64 = value } else .{ .uint64 = value }, + else => unreachable, + }, + }; + + const result_id = try cg.module.constant(result_ty_id, final_value); + + if (!ty.isVector(zcu)) return result_id; + return cg.constructCompositeSplat(ty, result_id); +} + +pub fn constructComposite(cg: *CodeGen, result_ty_id: Id, constituents: []const Id) !Id { + const gpa = cg.module.gpa; + const result_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpCompositeConstruct, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .constituents = constituents, + }); + return result_id; +} + +/// Construct a composite at runtime with all lanes set to the same value. +/// ty must be an aggregate type. +fn constructCompositeSplat(cg: *CodeGen, ty: Type, constituent: Id) !Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const n: usize = @intCast(ty.arrayLen(zcu)); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + + const constituents = try cg.id_scratch.addManyAsSlice(gpa, n); + @memset(constituents, constituent); + + const result_ty_id = try cg.resolveType(ty, .direct); + return cg.constructComposite(result_ty_id, constituents); +} + +/// This function generates a load for a constant in direct (ie, non-memory) representation. +/// When the constant is simple, it can be generated directly using OpConstant instructions. +/// When the constant is more complicated however, it needs to be constructed using multiple values. This +/// is done by emitting a sequence of instructions that initialize the value. +// +/// This function should only be called during function code generation. +fn constant(cg: *CodeGen, ty: Type, val: Value, repr: Repr) Error!Id { + const gpa = cg.module.gpa; + + // Note: Using intern_map can only be used with constants that DO NOT generate any runtime code!! + // Ideally that should be all constants in the future, or it should be cleaned up somehow. For + // now, only use the intern_map on case-by-case basis by breaking to :cache. + if (cg.module.intern_map.get(.{ val.toIntern(), repr })) |id| { + return id; + } + + const pt = cg.pt; + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + const result_ty_id = try cg.resolveType(ty, repr); + const ip = &zcu.intern_pool; + + log.debug("lowering constant: ty = {f}, val = {f}, key = {s}", .{ ty.fmt(pt), val.fmtValue(pt), @tagName(ip.indexToKey(val.toIntern())) }); + if (val.isUndefDeep(zcu)) { + return cg.module.constUndef(result_ty_id); + } + + const cacheable_id = cache: { + switch (ip.indexToKey(val.toIntern())) { + .int_type, + .ptr_type, + .array_type, + .vector_type, + .opt_type, + .anyframe_type, + .error_union_type, + .simple_type, + .struct_type, + .tuple_type, + .union_type, + .opaque_type, + .enum_type, + .func_type, + .error_set_type, + .inferred_error_set_type, + => unreachable, // types, not values + + .undef => unreachable, // handled above + + .variable, + .@"extern", + .func, + .enum_literal, + .empty_enum_value, + => unreachable, // non-runtime values + + .simple_value => |simple_value| switch (simple_value) { + .undefined, + .void, + .null, + .empty_tuple, + .@"unreachable", + => unreachable, // non-runtime values + + .false, .true => break :cache try cg.constBool(val.toBool(), repr), + }, + .int => { + if (ty.isSignedInt(zcu)) { + break :cache try cg.constInt(ty, val.toSignedInt(zcu)); + } else { + break :cache try cg.constInt(ty, val.toUnsignedInt(zcu)); + } + }, + .float => { + const lit: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { + 16 => .{ .uint32 = @as(u16, @bitCast(val.toFloat(f16, zcu))) }, + 32 => .{ .float32 = val.toFloat(f32, zcu) }, + 64 => .{ .float64 = val.toFloat(f64, zcu) }, + 80, 128 => unreachable, // TODO + else => unreachable, + }; + break :cache try cg.module.constant(result_ty_id, lit); + }, + .err => |err| { + const value = try pt.getErrorValue(err.name); + break :cache try cg.constInt(ty, value); + }, + .error_union => |error_union| { + // TODO: Error unions may be constructed with constant instructions if the payload type + // allows it. For now, just generate it here regardless. + const err_ty = ty.errorUnionSet(zcu); + const payload_ty = ty.errorUnionPayload(zcu); + const err_val_id = switch (error_union.val) { + .err_name => |err_name| try cg.constInt( + err_ty, + try pt.getErrorValue(err_name), + ), + .payload => try cg.constInt(err_ty, 0), + }; + const eu_layout = cg.errorUnionLayout(payload_ty); + if (!eu_layout.payload_has_bits) { + // We use the error type directly as the type. + break :cache err_val_id; + } + + const payload_val_id = switch (error_union.val) { + .err_name => try cg.constant(payload_ty, .undef, .indirect), + .payload => |p| try cg.constant(payload_ty, .fromInterned(p), .indirect), + }; + + var constituents: [2]Id = undefined; + var types: [2]Type = undefined; + if (eu_layout.error_first) { + constituents[0] = err_val_id; + constituents[1] = payload_val_id; + types = .{ err_ty, payload_ty }; + } else { + constituents[0] = payload_val_id; + constituents[1] = err_val_id; + types = .{ payload_ty, err_ty }; + } + + const comp_ty_id = try cg.resolveType(ty, .direct); + return try cg.constructComposite(comp_ty_id, &constituents); + }, + .enum_tag => { + const int_val = try val.intFromEnum(ty, pt); + const int_ty = ty.intTagType(zcu); + break :cache try cg.constant(int_ty, int_val, repr); + }, + .ptr => return cg.constantPtr(val), + .slice => |slice| { + const ptr_id = try cg.constantPtr(.fromInterned(slice.ptr)); + const len_id = try cg.constant(.usize, .fromInterned(slice.len), .indirect); + const comp_ty_id = try cg.resolveType(ty, .direct); + return try cg.constructComposite(comp_ty_id, &.{ ptr_id, len_id }); + }, + .opt => { + const payload_ty = ty.optionalChild(zcu); + const maybe_payload_val = val.optionalValue(zcu); + + if (!payload_ty.hasRuntimeBits(zcu)) { + break :cache try cg.constBool(maybe_payload_val != null, .indirect); + } else if (ty.optionalReprIsPayload(zcu)) { + // Optional representation is a nullable pointer or slice. + if (maybe_payload_val) |payload_val| { + return try cg.constant(payload_ty, payload_val, .indirect); + } else { + break :cache try cg.module.constNull(result_ty_id); + } + } + + // Optional representation is a structure. + // { Payload, Bool } + + const has_pl_id = try cg.constBool(maybe_payload_val != null, .indirect); + const payload_id = if (maybe_payload_val) |payload_val| + try cg.constant(payload_ty, payload_val, .indirect) + else + try cg.module.constUndef(try cg.resolveType(payload_ty, .indirect)); + + const comp_ty_id = try cg.resolveType(ty, .direct); + return try cg.constructComposite(comp_ty_id, &.{ payload_id, has_pl_id }); + }, + .aggregate => |aggregate| switch (ip.indexToKey(ty.ip_index)) { + inline .array_type, .vector_type => |array_type, tag| { + const elem_ty: Type = .fromInterned(array_type.child); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const constituents = try cg.id_scratch.addManyAsSlice(gpa, @intCast(ty.arrayLenIncludingSentinel(zcu))); + + const child_repr: Repr = switch (tag) { + .array_type => .indirect, + .vector_type => .direct, + else => unreachable, + }; + + switch (aggregate.storage) { + .bytes => |bytes| { + // TODO: This is really space inefficient, perhaps there is a better + // way to do it? + for (constituents, bytes.toSlice(constituents.len, ip)) |*constituent, byte| { + constituent.* = try cg.constInt(elem_ty, byte); + } + }, + .elems => |elems| { + for (constituents, elems) |*constituent, elem| { + constituent.* = try cg.constant(elem_ty, .fromInterned(elem), child_repr); + } + }, + .repeated_elem => |elem| { + @memset(constituents, try cg.constant(elem_ty, .fromInterned(elem), child_repr)); + }, + } + + const comp_ty_id = try cg.resolveType(ty, .direct); + return cg.constructComposite(comp_ty_id, constituents); + }, + .struct_type => { + const struct_type = zcu.typeToStruct(ty).?; + + if (struct_type.layout == .@"packed") { + // TODO: composite int + // TODO: endianness + const bits: u16 = @intCast(ty.bitSize(zcu)); + const bytes = std.mem.alignForward(u16, cg.module.backingIntBits(bits).@"0", 8) / 8; + var limbs: [8]u8 = undefined; + @memset(&limbs, 0); + val.writeToPackedMemory(ty, pt, limbs[0..bytes], 0) catch unreachable; + const backing_ty: Type = .fromInterned(struct_type.backingIntTypeUnordered(ip)); + return try cg.constInt(backing_ty, @as(u64, @bitCast(limbs))); + } + + var types = std.ArrayList(Type).init(gpa); + defer types.deinit(); + + var constituents = std.ArrayList(Id).init(gpa); + defer constituents.deinit(); + + var it = struct_type.iterateRuntimeOrder(ip); + while (it.next()) |field_index| { + const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]); + if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + // This is a zero-bit field - we only needed it for the alignment. + continue; + } + + // TODO: Padding? + const field_val = try val.fieldValue(pt, field_index); + const field_id = try cg.constant(field_ty, field_val, .indirect); + + try types.append(field_ty); + try constituents.append(field_id); + } + + const comp_ty_id = try cg.resolveType(ty, .direct); + return try cg.constructComposite(comp_ty_id, constituents.items); + }, + .tuple_type => return cg.todo("implement tuple types", .{}), + else => unreachable, + }, + .un => |un| { + if (un.tag == .none) { + assert(ty.containerLayout(zcu) == .@"packed"); // TODO + const int_ty = try pt.intType(.unsigned, @intCast(ty.bitSize(zcu))); + return try cg.constInt(int_ty, Value.toUnsignedInt(.fromInterned(un.val), zcu)); + } + const active_field = ty.unionTagFieldIndex(.fromInterned(un.tag), zcu).?; + const union_obj = zcu.typeToUnion(ty).?; + const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[active_field]); + const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(zcu)) + try cg.constant(field_ty, .fromInterned(un.val), .direct) + else + null; + return try cg.unionInit(ty, active_field, payload); + }, + .memoized_call => unreachable, + } + }; + + try cg.module.intern_map.putNoClobber(gpa, .{ val.toIntern(), repr }, cacheable_id); + + return cacheable_id; +} + +fn constantPtr(cg: *CodeGen, ptr_val: Value) !Id { + const pt = cg.pt; + const zcu = cg.module.zcu; + const gpa = cg.module.gpa; + + if (ptr_val.isUndef(zcu)) { + const result_ty = ptr_val.typeOf(zcu); + const result_ty_id = try cg.resolveType(result_ty, .direct); + return cg.module.constUndef(result_ty_id); + } + + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + + const derivation = try ptr_val.pointerDerivation(arena.allocator(), pt); + return cg.derivePtr(derivation); +} + +fn derivePtr(cg: *CodeGen, derivation: Value.PointerDeriveStep) !Id { + const gpa = cg.module.gpa; + const pt = cg.pt; + const zcu = cg.module.zcu; + switch (derivation) { + .comptime_alloc_ptr, .comptime_field_ptr => unreachable, + .int => |int| { + const result_ty_id = try cg.resolveType(int.ptr_ty, .direct); + // TODO: This can probably be an OpSpecConstantOp Bitcast, but + // that is not implemented by Mesa yet. Therefore, just generate it + // as a runtime operation. + const result_ptr_id = cg.module.allocId(); + const value_id = try cg.constInt(.usize, int.addr); + try cg.body.emit(gpa, .OpConvertUToPtr, .{ + .id_result_type = result_ty_id, + .id_result = result_ptr_id, + .integer_value = value_id, + }); + return result_ptr_id; + }, + .nav_ptr => |nav| { + const result_ptr_ty = try pt.navPtrType(nav); + return cg.constantNavRef(result_ptr_ty, nav); + }, + .uav_ptr => |uav| { + const result_ptr_ty: Type = .fromInterned(uav.orig_ty); + return cg.constantUavRef(result_ptr_ty, uav); + }, + .eu_payload_ptr => @panic("TODO"), + .opt_payload_ptr => @panic("TODO"), + .field_ptr => |field| { + const parent_ptr_id = try cg.derivePtr(field.parent.*); + const parent_ptr_ty = try field.parent.ptrType(pt); + return cg.structFieldPtr(field.result_ptr_ty, parent_ptr_ty, parent_ptr_id, field.field_idx); + }, + .elem_ptr => |elem| { + const parent_ptr_id = try cg.derivePtr(elem.parent.*); + const parent_ptr_ty = try elem.parent.ptrType(pt); + const index_id = try cg.constInt(.usize, elem.elem_idx); + return cg.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id); + }, + .offset_and_cast => |oac| { + const parent_ptr_id = try cg.derivePtr(oac.parent.*); + const parent_ptr_ty = try oac.parent.ptrType(pt); + const result_ty_id = try cg.resolveType(oac.new_ptr_ty, .direct); + const child_size = oac.new_ptr_ty.childType(zcu).abiSize(zcu); + + if (parent_ptr_ty.childType(zcu).isVector(zcu) and oac.byte_offset % child_size == 0) { + // Vector element ptr accesses are derived as offset_and_cast. + // We can just use OpAccessChain. + return cg.accessChain( + result_ty_id, + parent_ptr_id, + &.{@intCast(@divExact(oac.byte_offset, child_size))}, + ); + } + + if (oac.byte_offset == 0) { + // Allow changing the pointer type child only to restructure arrays. + // e.g. [3][2]T to T is fine, as is [2]T -> [2][1]T. + const result_ptr_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpBitcast, .{ + .id_result_type = result_ty_id, + .id_result = result_ptr_id, + .operand = parent_ptr_id, + }); + return result_ptr_id; + } + + return cg.fail("cannot perform pointer cast: '{f}' to '{f}'", .{ + parent_ptr_ty.fmt(pt), + oac.new_ptr_ty.fmt(pt), + }); + }, + } +} + +fn constantUavRef( + cg: *CodeGen, + ty: Type, + uav: InternPool.Key.Ptr.BaseAddr.Uav, +) !Id { + // TODO: Merge this function with constantDeclRef. + + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const ty_id = try cg.resolveType(ty, .direct); + const uav_ty: Type = .fromInterned(ip.typeOf(uav.val)); + + switch (ip.indexToKey(uav.val)) { + .func => unreachable, // TODO + .@"extern" => assert(!ip.isFunctionType(uav_ty.toIntern())), + else => {}, + } + + // const is_fn_body = decl_ty.zigTypeTag(zcu) == .@"fn"; + if (!uav_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { + // Pointer to nothing - return undefined + return cg.module.constUndef(ty_id); + } + + // Uav refs are always generic. + assert(ty.ptrAddressSpace(zcu) == .generic); + const uav_ty_id = try cg.resolveType(uav_ty, .indirect); + const decl_ptr_ty_id = try cg.module.ptrType(uav_ty_id, .generic); + const ptr_id = try cg.resolveUav(uav.val); + + if (decl_ptr_ty_id != ty_id) { + // Differing pointer types, insert a cast. + const casted_ptr_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpBitcast, .{ + .id_result_type = ty_id, + .id_result = casted_ptr_id, + .operand = ptr_id, + }); + return casted_ptr_id; + } else { + return ptr_id; + } +} + +fn constantNavRef(cg: *CodeGen, ty: Type, nav_index: InternPool.Nav.Index) !Id { + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const ty_id = try cg.resolveType(ty, .direct); + const nav = ip.getNav(nav_index); + const nav_ty: Type = .fromInterned(nav.typeOf(ip)); + + switch (nav.status) { + .unresolved => unreachable, + .type_resolved => {}, // this is not a function or extern + .fully_resolved => |r| switch (ip.indexToKey(r.val)) { + .func => { + // TODO: Properly lower function pointers. For now we are going to hack around it and + // just generate an empty pointer. Function pointers are represented by a pointer to usize. + return try cg.module.constUndef(ty_id); + }, + .@"extern" => if (ip.isFunctionType(nav_ty.toIntern())) @panic("TODO"), + else => {}, + }, + } + + if (!nav_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { + // Pointer to nothing - return undefined. + return cg.module.constUndef(ty_id); + } + + const spv_decl_index = try cg.module.resolveNav(ip, nav_index); + const spv_decl = cg.module.declPtr(spv_decl_index); + const spv_decl_result_id = spv_decl.result_id; + assert(spv_decl.kind != .func); + + const storage_class = cg.module.storageClass(nav.getAddrspace()); + try cg.addFunctionDep(spv_decl_index, storage_class); + + const nav_ty_id = try cg.resolveType(nav_ty, .indirect); + const decl_ptr_ty_id = try cg.module.ptrType(nav_ty_id, storage_class); + + if (decl_ptr_ty_id != ty_id) { + // Differing pointer types, insert a cast. + const casted_ptr_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpBitcast, .{ + .id_result_type = ty_id, + .id_result = casted_ptr_id, + .operand = spv_decl_result_id, + }); + return casted_ptr_id; + } + + return spv_decl_result_id; +} + +// Turn a Zig type's name into a cache reference. +fn resolveTypeName(cg: *CodeGen, ty: Type) ![]const u8 { + const gpa = cg.module.gpa; + var aw: std.io.Writer.Allocating = .init(gpa); + defer aw.deinit(); + ty.print(&aw.writer, cg.pt) catch |err| switch (err) { + error.WriteFailed => return error.OutOfMemory, + }; + return try aw.toOwnedSlice(); +} + +/// Generate a union type. Union types are always generated with the +/// most aligned field active. If the tag alignment is greater +/// than that of the payload, a regular union (non-packed, with both tag and +/// payload), will be generated as follows: +/// struct { +/// tag: TagType, +/// payload: MostAlignedFieldType, +/// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8, +/// padding: [padding_size]u8, +/// } +/// If the payload alignment is greater than that of the tag: +/// struct { +/// payload: MostAlignedFieldType, +/// payload_padding: [payload_size - @sizeOf(MostAlignedFieldType)]u8, +/// tag: TagType, +/// padding: [padding_size]u8, +/// } +/// If any of the fields' size is 0, it will be omitted. +fn resolveUnionType(cg: *CodeGen, ty: Type) !Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const union_obj = zcu.typeToUnion(ty).?; + + if (union_obj.flagsUnordered(ip).layout == .@"packed") { + return try cg.module.intType(.unsigned, @intCast(ty.bitSize(zcu))); + } + + const layout = cg.unionLayout(ty); + if (!layout.has_payload) { + // No payload, so represent this as just the tag type. + return try cg.resolveType(.fromInterned(union_obj.enum_tag_ty), .indirect); + } + + var member_types: [4]Id = undefined; + var member_names: [4][]const u8 = undefined; + + const u8_ty_id = try cg.resolveType(.u8, .direct); + + if (layout.tag_size != 0) { + const tag_ty_id = try cg.resolveType(.fromInterned(union_obj.enum_tag_ty), .indirect); + member_types[layout.tag_index] = tag_ty_id; + member_names[layout.tag_index] = "(tag)"; + } + + if (layout.payload_size != 0) { + const payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); + member_types[layout.payload_index] = payload_ty_id; + member_names[layout.payload_index] = "(payload)"; + } + + if (layout.payload_padding_size != 0) { + const len_id = try cg.constInt(.u32, layout.payload_padding_size); + const payload_padding_ty_id = try cg.module.arrayType(len_id, u8_ty_id); + member_types[layout.payload_padding_index] = payload_padding_ty_id; + member_names[layout.payload_padding_index] = "(payload padding)"; + } + + if (layout.padding_size != 0) { + const len_id = try cg.constInt(.u32, layout.padding_size); + const padding_ty_id = try cg.module.arrayType(len_id, u8_ty_id); + member_types[layout.padding_index] = padding_ty_id; + member_names[layout.padding_index] = "(padding)"; + } + + const result_id = try cg.module.structType( + member_types[0..layout.total_fields], + member_names[0..layout.total_fields], + null, + .none, + ); + + const type_name = try cg.resolveTypeName(ty); + defer gpa.free(type_name); + try cg.module.debugName(result_id, type_name); + + return result_id; +} + +fn resolveFnReturnType(cg: *CodeGen, ret_ty: Type) !Id { + const zcu = cg.module.zcu; + if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + // If the return type is an error set or an error union, then we make this + // anyerror return type instead, so that it can be coerced into a function + // pointer type which has anyerror as the return type. + if (ret_ty.isError(zcu)) { + return cg.resolveType(.anyerror, .direct); + } else { + return cg.resolveType(.void, .direct); + } + } + + return try cg.resolveType(ret_ty, .direct); +} + +fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id { + const gpa = cg.module.gpa; + const pt = cg.pt; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const target = cg.module.zcu.getTarget(); + + log.debug("resolveType: ty = {f}", .{ty.fmt(pt)}); + + switch (ty.zigTypeTag(zcu)) { + .noreturn => { + assert(repr == .direct); + return try cg.module.voidType(); + }, + .void => switch (repr) { + .direct => return try cg.module.voidType(), + .indirect => return try cg.module.opaqueType("void"), + }, + .bool => switch (repr) { + .direct => return try cg.module.boolType(), + .indirect => return try cg.resolveType(.u1, .indirect), + }, + .int => { + const int_info = ty.intInfo(zcu); + if (int_info.bits == 0) { + assert(repr == .indirect); + return try cg.module.opaqueType("u0"); + } + return try cg.module.intType(int_info.signedness, int_info.bits); + }, + .@"enum" => return try cg.resolveType(ty.intTagType(zcu), repr), + .float => { + const bits = ty.floatBits(target); + const supported = switch (bits) { + 16 => target.cpu.has(.spirv, .float16), + 32 => true, + 64 => target.cpu.has(.spirv, .float64), + else => false, + }; + + if (!supported) { + return cg.fail( + "floating point width of {} bits is not supported for the current SPIR-V feature set", + .{bits}, + ); + } + + return try cg.module.floatType(bits); + }, + .array => { + const elem_ty = ty.childType(zcu); + const elem_ty_id = try cg.resolveType(elem_ty, .indirect); + const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel(zcu)) orelse { + return cg.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(zcu)}); + }; + + if (!elem_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + assert(repr == .indirect); + return try cg.module.opaqueType("zero-sized-array"); + } else if (total_len == 0) { + // The size of the array would be 0, but that is not allowed in SPIR-V. + // This path can be reached for example when there is a slicing of a pointer + // that produces a zero-length array. In all cases where this type can be generated, + // this should be an indirect path. + assert(repr == .indirect); + // In this case, we have an array of a non-zero sized type. In this case, + // generate an array of 1 element instead, so that ptr_elem_ptr instructions + // can be lowered to ptrAccessChain instead of manually performing the math. + const len_id = try cg.constInt(.u32, 1); + return try cg.module.arrayType(len_id, elem_ty_id); + } else { + const total_len_id = try cg.constInt(.u32, total_len); + const result_id = try cg.module.arrayType(total_len_id, elem_ty_id); + switch (target.os.tag) { + .vulkan, .opengl => { + try cg.module.decorate(result_id, .{ + .array_stride = .{ + .array_stride = @intCast(elem_ty.abiSize(zcu)), + }, + }); + }, + else => {}, + } + return result_id; + } + }, + .vector => { + const elem_ty = ty.childType(zcu); + const elem_ty_id = try cg.resolveType(elem_ty, repr); + const len = ty.vectorLen(zcu); + if (cg.isSpvVector(ty)) return try cg.module.vectorType(len, elem_ty_id); + const len_id = try cg.constInt(.u32, len); + return try cg.module.arrayType(len_id, elem_ty_id); + }, + .@"fn" => switch (repr) { + .direct => { + const fn_info = zcu.typeToFunc(ty).?; + + comptime assert(zig_call_abi_ver == 3); + assert(!fn_info.is_var_args); + switch (fn_info.cc) { + .auto, + .spirv_kernel, + .spirv_fragment, + .spirv_vertex, + .spirv_device, + => {}, + else => unreachable, + } + + const return_ty_id = try cg.resolveFnReturnType(.fromInterned(fn_info.return_type)); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const param_ty_ids = try cg.id_scratch.addManyAsSlice(gpa, fn_info.param_types.len); + + var param_index: usize = 0; + for (fn_info.param_types.get(ip)) |param_ty_index| { + const param_ty: Type = .fromInterned(param_ty_index); + if (!param_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; + + param_ty_ids[param_index] = try cg.resolveType(param_ty, .direct); + param_index += 1; + } + + return try cg.module.functionType(return_ty_id, param_ty_ids[0..param_index]); + }, + .indirect => { + // TODO: Represent function pointers properly. + // For now, just use an usize type. + return try cg.resolveType(.usize, .indirect); + }, + }, + .pointer => { + const ptr_info = ty.ptrInfo(zcu); + + const child_ty: Type = .fromInterned(ptr_info.child); + const child_ty_id = try cg.resolveType(child_ty, .indirect); + const storage_class = cg.module.storageClass(ptr_info.flags.address_space); + const ptr_ty_id = try cg.module.ptrType(child_ty_id, storage_class); + + if (ptr_info.flags.size != .slice) { + return ptr_ty_id; + } + + const size_ty_id = try cg.resolveType(.usize, .direct); + return try cg.module.structType( + &.{ ptr_ty_id, size_ty_id }, + &.{ "ptr", "len" }, + null, + .none, + ); + }, + .@"struct" => { + const struct_type = switch (ip.indexToKey(ty.toIntern())) { + .tuple_type => |tuple| { + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const member_types = try cg.id_scratch.addManyAsSlice(gpa, tuple.values.len); + + var member_index: usize = 0; + for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, field_val| { + if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(zcu)) continue; + + member_types[member_index] = try cg.resolveType(.fromInterned(field_ty), .indirect); + member_index += 1; + } + + const result_id = try cg.module.structType( + member_types[0..member_index], + null, + null, + .none, + ); + const type_name = try cg.resolveTypeName(ty); + defer gpa.free(type_name); + try cg.module.debugName(result_id, type_name); + return result_id; + }, + .struct_type => ip.loadStructType(ty.toIntern()), + else => unreachable, + }; + + if (struct_type.layout == .@"packed") { + return try cg.resolveType(.fromInterned(struct_type.backingIntTypeUnordered(ip)), .direct); + } + + var member_types = std.ArrayList(Id).init(gpa); + defer member_types.deinit(); + + var member_names = std.ArrayList([]const u8).init(gpa); + defer member_names.deinit(); + + var member_offsets = std.ArrayList(u32).init(gpa); + defer member_offsets.deinit(); + + var it = struct_type.iterateRuntimeOrder(ip); + while (it.next()) |field_index| { + const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]); + if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; + + const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse + try ip.getOrPutStringFmt(zcu.gpa, pt.tid, "{d}", .{field_index}, .no_embedded_nulls); + try member_types.append(try cg.resolveType(field_ty, .indirect)); + try member_names.append(field_name.toSlice(ip)); + try member_offsets.append(@intCast(ty.structFieldOffset(field_index, zcu))); + } + + const result_id = try cg.module.structType( + member_types.items, + member_names.items, + member_offsets.items, + ty.toIntern(), + ); + + const type_name = try cg.resolveTypeName(ty); + defer gpa.free(type_name); + try cg.module.debugName(result_id, type_name); + + return result_id; + }, + .optional => { + const payload_ty = ty.optionalChild(zcu); + if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + // Just use a bool. + // Note: Always generate the bool with indirect format, to save on some sanity + // Perform the conversion to a direct bool when the field is extracted. + return try cg.resolveType(.bool, .indirect); + } + + const payload_ty_id = try cg.resolveType(payload_ty, .indirect); + if (ty.optionalReprIsPayload(zcu)) { + // Optional is actually a pointer or a slice. + return payload_ty_id; + } + + const bool_ty_id = try cg.resolveType(.bool, .indirect); + + return try cg.module.structType( + &.{ payload_ty_id, bool_ty_id }, + &.{ "payload", "valid" }, + null, + .none, + ); + }, + .@"union" => return try cg.resolveUnionType(ty), + .error_set => { + const err_int_ty = try pt.errorIntType(); + return try cg.resolveType(err_int_ty, repr); + }, + .error_union => { + const payload_ty = ty.errorUnionPayload(zcu); + const err_ty = ty.errorUnionSet(zcu); + const error_ty_id = try cg.resolveType(err_ty, .indirect); + + const eu_layout = cg.errorUnionLayout(payload_ty); + if (!eu_layout.payload_has_bits) { + return error_ty_id; + } + + const payload_ty_id = try cg.resolveType(payload_ty, .indirect); + + var member_types: [2]Id = undefined; + var member_names: [2][]const u8 = undefined; + if (eu_layout.error_first) { + // Put the error first + member_types = .{ error_ty_id, payload_ty_id }; + member_names = .{ "error", "payload" }; + // TODO: ABI padding? + } else { + // Put the payload first. + member_types = .{ payload_ty_id, error_ty_id }; + member_names = .{ "payload", "error" }; + // TODO: ABI padding? + } + + return try cg.module.structType(&member_types, &member_names, null, .none); + }, + .@"opaque" => { + const type_name = try cg.resolveTypeName(ty); + defer gpa.free(type_name); + return try cg.module.opaqueType(type_name); + }, + + .null, + .undefined, + .enum_literal, + .comptime_float, + .comptime_int, + .type, + => unreachable, // Must be comptime. + + .frame, .@"anyframe" => unreachable, // TODO + } +} + +const ErrorUnionLayout = struct { + payload_has_bits: bool, + error_first: bool, + + fn errorFieldIndex(cg: @This()) u32 { + assert(cg.payload_has_bits); + return if (cg.error_first) 0 else 1; + } + + fn payloadFieldIndex(cg: @This()) u32 { + assert(cg.payload_has_bits); + return if (cg.error_first) 1 else 0; + } +}; + +fn errorUnionLayout(cg: *CodeGen, payload_ty: Type) ErrorUnionLayout { + const zcu = cg.module.zcu; + + const error_align = Type.abiAlignment(.anyerror, zcu); + const payload_align = payload_ty.abiAlignment(zcu); + + const error_first = error_align.compare(.gt, payload_align); + return .{ + .payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu), + .error_first = error_first, + }; +} + +const UnionLayout = struct { + /// If false, this union is represented + /// by only an integer of the tag type. + has_payload: bool, + tag_size: u32, + tag_index: u32, + /// Note: This is the size of the payload type itcg, NOT the size of the ENTIRE payload. + /// Use `has_payload` instead!! + payload_ty: Type, + payload_size: u32, + payload_index: u32, + payload_padding_size: u32, + payload_padding_index: u32, + padding_size: u32, + padding_index: u32, + total_fields: u32, +}; + +fn unionLayout(cg: *CodeGen, ty: Type) UnionLayout { + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const layout = ty.unionGetLayout(zcu); + const union_obj = zcu.typeToUnion(ty).?; + + var union_layout: UnionLayout = .{ + .has_payload = layout.payload_size != 0, + .tag_size = @intCast(layout.tag_size), + .tag_index = undefined, + .payload_ty = undefined, + .payload_size = undefined, + .payload_index = undefined, + .payload_padding_size = undefined, + .payload_padding_index = undefined, + .padding_size = @intCast(layout.padding), + .padding_index = undefined, + .total_fields = undefined, + }; + + if (union_layout.has_payload) { + const most_aligned_field = layout.most_aligned_field; + const most_aligned_field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[most_aligned_field]); + union_layout.payload_ty = most_aligned_field_ty; + union_layout.payload_size = @intCast(most_aligned_field_ty.abiSize(zcu)); + } else { + union_layout.payload_size = 0; + } + + union_layout.payload_padding_size = @intCast(layout.payload_size - union_layout.payload_size); + + const tag_first = layout.tag_align.compare(.gte, layout.payload_align); + var field_index: u32 = 0; + + if (union_layout.tag_size != 0 and tag_first) { + union_layout.tag_index = field_index; + field_index += 1; + } + + if (union_layout.payload_size != 0) { + union_layout.payload_index = field_index; + field_index += 1; + } + + if (union_layout.payload_padding_size != 0) { + union_layout.payload_padding_index = field_index; + field_index += 1; + } + + if (union_layout.tag_size != 0 and !tag_first) { + union_layout.tag_index = field_index; + field_index += 1; + } + + if (union_layout.padding_size != 0) { + union_layout.padding_index = field_index; + field_index += 1; + } + + union_layout.total_fields = field_index; + + return union_layout; +} + +/// This structure represents a "temporary" value: Something we are currently +/// operating on. It typically lives no longer than the function that +/// implements a particular AIR operation. These are used to easier +/// implement vectorizable operations (see Vectorization and the build* +/// functions), and typically are only used for vectors of primitive types. +const Temporary = struct { + /// The type of the temporary. This is here mainly + /// for easier bookkeeping. Because we will never really + /// store Temporaries, they only cause extra stack space, + /// therefore no real storage is wasted. + ty: Type, + /// The value that this temporary holds. This is not necessarily + /// a value that is actually usable, or a single value: It is virtual + /// until materialize() is called, at which point is turned into + /// the usual SPIR-V representation of `cg.ty`. + value: Temporary.Value, + + const Value = union(enum) { + singleton: Id, + exploded_vector: IdRange, + }; + + fn init(ty: Type, singleton: Id) Temporary { + return .{ .ty = ty, .value = .{ .singleton = singleton } }; + } + + fn materialize(temp: Temporary, cg: *CodeGen) !Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + switch (temp.value) { + .singleton => |id| return id, + .exploded_vector => |range| { + assert(temp.ty.isVector(zcu)); + assert(temp.ty.vectorLen(zcu) == range.len); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const constituents = try cg.id_scratch.addManyAsSlice(gpa, range.len); + for (constituents, 0..range.len) |*id, i| { + id.* = range.at(i); + } + + const result_ty_id = try cg.resolveType(temp.ty, .direct); + return cg.constructComposite(result_ty_id, constituents); + }, + } + } + + fn vectorization(temp: Temporary, cg: *CodeGen) Vectorization { + return .fromType(temp.ty, cg); + } + + fn pun(temp: Temporary, new_ty: Type) Temporary { + return .{ + .ty = new_ty, + .value = temp.value, + }; + } + + /// 'Explode' a temporary into separate elements. This turns a vector + /// into a bag of elements. + fn explode(temp: Temporary, cg: *CodeGen) !IdRange { + const zcu = cg.module.zcu; + + // If the value is a scalar, then this is a no-op. + if (!temp.ty.isVector(zcu)) { + return switch (temp.value) { + .singleton => |id| .{ .base = @intFromEnum(id), .len = 1 }, + .exploded_vector => |range| range, + }; + } + + const ty_id = try cg.resolveType(temp.ty.scalarType(zcu), .direct); + const n = temp.ty.vectorLen(zcu); + const results = cg.module.allocIds(n); + + const id = switch (temp.value) { + .singleton => |id| id, + .exploded_vector => |range| return range, + }; + + for (0..n) |i| { + const indexes = [_]u32{@intCast(i)}; + try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ + .id_result_type = ty_id, + .id_result = results.at(i), + .composite = id, + .indexes = &indexes, + }); + } + + return results; + } +}; + +/// Initialize a `Temporary` from an AIR value. +fn temporary(cg: *CodeGen, inst: Air.Inst.Ref) !Temporary { + return .{ + .ty = cg.typeOf(inst), + .value = .{ .singleton = try cg.resolve(inst) }, + }; +} + +/// This union describes how a particular operation should be vectorized. +/// That depends on the operation and number of components of the inputs. +const Vectorization = union(enum) { + /// This is an operation between scalars. + scalar, + /// This operation is unrolled into separate operations. + /// Inputs may still be SPIR-V vectors, for example, + /// when the operation can't be vectorized in SPIR-V. + /// Value is number of components. + unrolled: u32, + + /// Derive a vectorization from a particular type + fn fromType(ty: Type, cg: *CodeGen) Vectorization { + const zcu = cg.module.zcu; + if (!ty.isVector(zcu)) return .scalar; + return .{ .unrolled = ty.vectorLen(zcu) }; + } + + /// Given two vectorization methods, compute a "unification": a fallback + /// that works for both, according to the following rules: + /// - Scalars may broadcast + /// - SPIR-V vectorized operations will unroll + /// - Prefer scalar > unrolled + fn unify(a: Vectorization, b: Vectorization) Vectorization { + if (a == .scalar and b == .scalar) return .scalar; + if (a == .unrolled or b == .unrolled) { + if (a == .unrolled and b == .unrolled) assert(a.components() == b.components()); + if (a == .unrolled) return .{ .unrolled = a.components() }; + return .{ .unrolled = b.components() }; + } + unreachable; + } + + /// Query the number of components that inputs of this operation have. + /// Note: for broadcasting scalars, this returns the number of elements + /// that the broadcasted vector would have. + fn components(vec: Vectorization) u32 { + return switch (vec) { + .scalar => 1, + .unrolled => |n| n, + }; + } + + /// Turns `ty` into the result-type of the entire operation. + /// `ty` may be a scalar or vector, it doesn't matter. + fn resultType(vec: Vectorization, cg: *CodeGen, ty: Type) !Type { + const pt = cg.pt; + const zcu = cg.module.zcu; + const scalar_ty = ty.scalarType(zcu); + return switch (vec) { + .scalar => scalar_ty, + .unrolled => |n| try pt.vectorType(.{ .len = n, .child = scalar_ty.toIntern() }), + }; + } + + /// Before a temporary can be used, some setup may need to be one. This function implements + /// this setup, and returns a new type that holds the relevant information on how to access + /// elements of the input. + fn prepare(vec: Vectorization, cg: *CodeGen, tmp: Temporary) !PreparedOperand { + const zcu = cg.module.zcu; + const is_vector = tmp.ty.isVector(zcu); + const value: PreparedOperand.Value = switch (tmp.value) { + .singleton => |id| switch (vec) { + .scalar => blk: { + assert(!is_vector); + break :blk .{ .scalar = id }; + }, + .unrolled => blk: { + if (is_vector) break :blk .{ .vector_exploded = try tmp.explode(cg) }; + break :blk .{ .scalar_broadcast = id }; + }, + }, + .exploded_vector => |range| switch (vec) { + .scalar => unreachable, + .unrolled => |n| blk: { + assert(range.len == n); + break :blk .{ .vector_exploded = range }; + }, + }, + }; + + return .{ + .ty = tmp.ty, + .value = value, + }; + } + + /// Finalize the results of an operation back into a temporary. `results` is + /// a list of result-ids of the operation. + fn finalize(vec: Vectorization, ty: Type, results: IdRange) Temporary { + assert(vec.components() == results.len); + return .{ + .ty = ty, + .value = switch (vec) { + .scalar => .{ .singleton = results.at(0) }, + .unrolled => .{ .exploded_vector = results }, + }, + }; + } + + /// This struct represents an operand that has gone through some setup, and is + /// ready to be used as part of an operation. + const PreparedOperand = struct { + ty: Type, + value: PreparedOperand.Value, + + /// The types of value that a prepared operand can hold internally. Depends + /// on the operation and input value. + const Value = union(enum) { + /// A single scalar value that is used by a scalar operation. + scalar: Id, + /// A single scalar that is broadcasted in an unrolled operation. + scalar_broadcast: Id, + /// A vector represented by a consecutive list of IDs that is used in an unrolled operation. + vector_exploded: IdRange, + }; + + /// Query the value at a particular index of the operation. Note that + /// the index is *not* the component/lane, but the index of the *operation*. + fn at(op: PreparedOperand, i: usize) Id { + switch (op.value) { + .scalar => |id| { + assert(i == 0); + return id; + }, + .scalar_broadcast => |id| return id, + .vector_exploded => |range| return range.at(i), + } + } + }; +}; + +/// A utility function to compute the vectorization style of +/// a list of values. These values may be any of the following: +/// - A `Vectorization` instance +/// - A Type, in which case the vectorization is computed via `Vectorization.fromType`. +/// - A Temporary, in which case the vectorization is computed via `Temporary.vectorization`. +fn vectorization(cg: *CodeGen, args: anytype) Vectorization { + var v: Vectorization = undefined; + assert(args.len >= 1); + inline for (args, 0..) |arg, i| { + const iv: Vectorization = switch (@TypeOf(arg)) { + Vectorization => arg, + Type => Vectorization.fromType(arg, cg), + Temporary => arg.vectorization(cg), + else => @compileError("invalid type"), + }; + if (i == 0) { + v = iv; + } else { + v = v.unify(iv); + } + } + return v; +} + +/// This function builds an OpSConvert of OpUConvert depending on the +/// signedness of the types. +fn buildConvert(cg: *CodeGen, dst_ty: Type, src: Temporary) !Temporary { + const zcu = cg.module.zcu; + + const dst_ty_id = try cg.resolveType(dst_ty.scalarType(zcu), .direct); + const src_ty_id = try cg.resolveType(src.ty.scalarType(zcu), .direct); + + const v = cg.vectorization(.{ dst_ty, src }); + const result_ty = try v.resultType(cg, dst_ty); + + // We can directly compare integers, because those type-IDs are cached. + if (dst_ty_id == src_ty_id) { + // Nothing to do, type-pun to the right value. + // Note, Caller guarantees that the types fit (or caller will normalize after), + // so we don't have to normalize here. + // Note, dst_ty may be a scalar type even if we expect a vector, so we have to + // convert to the right type here. + return src.pun(result_ty); + } + + const ops = v.components(); + const results = cg.module.allocIds(ops); + + const op_result_ty = dst_ty.scalarType(zcu); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + + const opcode: Opcode = blk: { + if (dst_ty.scalarType(zcu).isAnyFloat()) break :blk .OpFConvert; + if (dst_ty.scalarType(zcu).isSignedInt(zcu)) break :blk .OpSConvert; + break :blk .OpUConvert; + }; + + const op_src = try v.prepare(cg, src); + + for (0..ops) |i| { + try cg.body.emitRaw(cg.module.gpa, opcode, 3); + cg.body.writeOperand(Id, op_result_ty_id); + cg.body.writeOperand(Id, results.at(i)); + cg.body.writeOperand(Id, op_src.at(i)); + } + + return v.finalize(result_ty, results); +} + +fn buildFma(cg: *CodeGen, a: Temporary, b: Temporary, c: Temporary) !Temporary { + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + + const v = cg.vectorization(.{ a, b, c }); + const ops = v.components(); + const results = cg.module.allocIds(ops); + + const op_result_ty = a.ty.scalarType(zcu); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + const result_ty = try v.resultType(cg, a.ty); + + const op_a = try v.prepare(cg, a); + const op_b = try v.prepare(cg, b); + const op_c = try v.prepare(cg, c); + + const set = try cg.importExtendedSet(); + const opcode: u32 = switch (target.os.tag) { + .opencl => @intFromEnum(spec.OpenClOpcode.fma), + // NOTE: Vulkan's FMA instruction does *NOT* produce the right values! + // its precision guarantees do NOT match zigs and it does NOT match OpenCLs! + // it needs to be emulated! + .vulkan, .opengl => @intFromEnum(spec.GlslOpcode.Fma), + else => unreachable, + }; + + for (0..ops) |i| { + try cg.body.emit(cg.module.gpa, .OpExtInst, .{ + .id_result_type = op_result_ty_id, + .id_result = results.at(i), + .set = set, + .instruction = .{ .inst = opcode }, + .id_ref_4 = &.{ op_a.at(i), op_b.at(i), op_c.at(i) }, + }); + } + + return v.finalize(result_ty, results); +} + +fn buildSelect(cg: *CodeGen, condition: Temporary, lhs: Temporary, rhs: Temporary) !Temporary { + const zcu = cg.module.zcu; + + const v = cg.vectorization(.{ condition, lhs, rhs }); + const ops = v.components(); + const results = cg.module.allocIds(ops); + + const op_result_ty = lhs.ty.scalarType(zcu); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + const result_ty = try v.resultType(cg, lhs.ty); + + assert(condition.ty.scalarType(zcu).zigTypeTag(zcu) == .bool); + + const cond = try v.prepare(cg, condition); + const object_1 = try v.prepare(cg, lhs); + const object_2 = try v.prepare(cg, rhs); + + for (0..ops) |i| { + try cg.body.emit(cg.module.gpa, .OpSelect, .{ + .id_result_type = op_result_ty_id, + .id_result = results.at(i), + .condition = cond.at(i), + .object_1 = object_1.at(i), + .object_2 = object_2.at(i), + }); + } + + return v.finalize(result_ty, results); +} + +fn buildCmp(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Temporary { + const v = cg.vectorization(.{ lhs, rhs }); + const ops = v.components(); + const results = cg.module.allocIds(ops); + + const op_result_ty: Type = .bool; + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + const result_ty = try v.resultType(cg, Type.bool); + + const op_lhs = try v.prepare(cg, lhs); + const op_rhs = try v.prepare(cg, rhs); + + for (0..ops) |i| { + try cg.body.emitRaw(cg.module.gpa, opcode, 4); + cg.body.writeOperand(Id, op_result_ty_id); + cg.body.writeOperand(Id, results.at(i)); + cg.body.writeOperand(Id, op_lhs.at(i)); + cg.body.writeOperand(Id, op_rhs.at(i)); + } + + return v.finalize(result_ty, results); +} + +const UnaryOp = enum { + l_not, + bit_not, + i_neg, + f_neg, + i_abs, + f_abs, + clz, + ctz, + floor, + ceil, + trunc, + round, + sqrt, + sin, + cos, + tan, + exp, + exp2, + log, + log2, + log10, + + pub fn extInstOpcode(op: UnaryOp, target: *const std.Target) ?u32 { + return switch (target.os.tag) { + .opencl => @intFromEnum(@as(spec.OpenClOpcode, switch (op) { + .i_abs => .s_abs, + .f_abs => .fabs, + .clz => .clz, + .ctz => .ctz, + .floor => .floor, + .ceil => .ceil, + .trunc => .trunc, + .round => .round, + .sqrt => .sqrt, + .sin => .sin, + .cos => .cos, + .tan => .tan, + .exp => .exp, + .exp2 => .exp2, + .log => .log, + .log2 => .log2, + .log10 => .log10, + else => return null, + })), + // Note: We'll need to check these for floating point accuracy + // Vulkan does not put tight requirements on these, for correction + // we might want to emulate them at some point. + .vulkan, .opengl => @intFromEnum(@as(spec.GlslOpcode, switch (op) { + .i_abs => .SAbs, + .f_abs => .FAbs, + .floor => .Floor, + .ceil => .Ceil, + .trunc => .Trunc, + .round => .Round, + else => return null, + })), + else => unreachable, + }; + } +}; + +fn buildUnary(cg: *CodeGen, op: UnaryOp, operand: Temporary) !Temporary { + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + const v = cg.vectorization(.{operand}); + const ops = v.components(); + const results = cg.module.allocIds(ops); + const op_result_ty = operand.ty.scalarType(zcu); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + const result_ty = try v.resultType(cg, operand.ty); + const op_operand = try v.prepare(cg, operand); + + if (op.extInstOpcode(target)) |opcode| { + const set = try cg.importExtendedSet(); + for (0..ops) |i| { + try cg.body.emit(cg.module.gpa, .OpExtInst, .{ + .id_result_type = op_result_ty_id, + .id_result = results.at(i), + .set = set, + .instruction = .{ .inst = opcode }, + .id_ref_4 = &.{op_operand.at(i)}, + }); + } + } else { + const opcode: Opcode = switch (op) { + .l_not => .OpLogicalNot, + .bit_not => .OpNot, + .i_neg => .OpSNegate, + .f_neg => .OpFNegate, + else => return cg.todo( + "implement unary operation '{s}' for {s} os", + .{ @tagName(op), @tagName(target.os.tag) }, + ), + }; + for (0..ops) |i| { + try cg.body.emitRaw(cg.module.gpa, opcode, 3); + cg.body.writeOperand(Id, op_result_ty_id); + cg.body.writeOperand(Id, results.at(i)); + cg.body.writeOperand(Id, op_operand.at(i)); + } + } + + return v.finalize(result_ty, results); +} + +fn buildBinary(cg: *CodeGen, opcode: Opcode, lhs: Temporary, rhs: Temporary) !Temporary { + const zcu = cg.module.zcu; + + const v = cg.vectorization(.{ lhs, rhs }); + const ops = v.components(); + const results = cg.module.allocIds(ops); + + const op_result_ty = lhs.ty.scalarType(zcu); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + const result_ty = try v.resultType(cg, lhs.ty); + + const op_lhs = try v.prepare(cg, lhs); + const op_rhs = try v.prepare(cg, rhs); + + for (0..ops) |i| { + try cg.body.emitRaw(cg.module.gpa, opcode, 4); + cg.body.writeOperand(Id, op_result_ty_id); + cg.body.writeOperand(Id, results.at(i)); + cg.body.writeOperand(Id, op_lhs.at(i)); + cg.body.writeOperand(Id, op_rhs.at(i)); + } + + return v.finalize(result_ty, results); +} + +/// This function builds an extended multiplication, either OpSMulExtended or OpUMulExtended on Vulkan, +/// or OpIMul and s_mul_hi or u_mul_hi on OpenCL. +fn buildWideMul( + cg: *CodeGen, + signedness: std.builtin.Signedness, + lhs: Temporary, + rhs: Temporary, +) !struct { Temporary, Temporary } { + const pt = cg.pt; + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + const ip = &zcu.intern_pool; + + const v = lhs.vectorization(cg).unify(rhs.vectorization(cg)); + const ops = v.components(); + + const arith_op_ty = lhs.ty.scalarType(zcu); + const arith_op_ty_id = try cg.resolveType(arith_op_ty, .direct); + + const lhs_op = try v.prepare(cg, lhs); + const rhs_op = try v.prepare(cg, rhs); + + const value_results = cg.module.allocIds(ops); + const overflow_results = cg.module.allocIds(ops); + + switch (target.os.tag) { + .opencl => { + // Currently, SPIRV-LLVM-Translator based backends cannot deal with OpSMulExtended and + // OpUMulExtended. For these we will use the OpenCL s_mul_hi to compute the high-order bits + // instead. + const set = try cg.importExtendedSet(); + const overflow_inst: spec.OpenClOpcode = switch (signedness) { + .signed => .s_mul_hi, + .unsigned => .u_mul_hi, + }; + + for (0..ops) |i| { + try cg.body.emit(cg.module.gpa, .OpIMul, .{ + .id_result_type = arith_op_ty_id, + .id_result = value_results.at(i), + .operand_1 = lhs_op.at(i), + .operand_2 = rhs_op.at(i), + }); + + try cg.body.emit(cg.module.gpa, .OpExtInst, .{ + .id_result_type = arith_op_ty_id, + .id_result = overflow_results.at(i), + .set = set, + .instruction = .{ .inst = @intFromEnum(overflow_inst) }, + .id_ref_4 = &.{ lhs_op.at(i), rhs_op.at(i) }, + }); + } + }, + .vulkan, .opengl => { + // Operations return a struct{T, T} + // where T is maybe vectorized. + const op_result_ty: Type = .fromInterned(try ip.getTupleType(zcu.gpa, pt.tid, .{ + .types = &.{ arith_op_ty.toIntern(), arith_op_ty.toIntern() }, + .values = &.{ .none, .none }, + })); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + + const opcode: Opcode = switch (signedness) { + .signed => .OpSMulExtended, + .unsigned => .OpUMulExtended, + }; + + for (0..ops) |i| { + const op_result = cg.module.allocId(); + + try cg.body.emitRaw(cg.module.gpa, opcode, 4); + cg.body.writeOperand(Id, op_result_ty_id); + cg.body.writeOperand(Id, op_result); + cg.body.writeOperand(Id, lhs_op.at(i)); + cg.body.writeOperand(Id, rhs_op.at(i)); + + // The above operation returns a struct. We might want to expand + // Temporary to deal with the fact that these are structs eventually, + // but for now, take the struct apart and return two separate vectors. + + try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ + .id_result_type = arith_op_ty_id, + .id_result = value_results.at(i), + .composite = op_result, + .indexes = &.{0}, + }); + + try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ + .id_result_type = arith_op_ty_id, + .id_result = overflow_results.at(i), + .composite = op_result, + .indexes = &.{1}, + }); + } + }, + else => unreachable, + } + + const result_ty = try v.resultType(cg, lhs.ty); + return .{ + v.finalize(result_ty, value_results), + v.finalize(result_ty, overflow_results), + }; +} + +/// The SPIR-V backend is not yet advanced enough to support the std testing infrastructure. +/// In order to be able to run tests, we "temporarily" lower test kernels into separate entry- +/// points. The test executor will then be able to invoke these to run the tests. +/// Note that tests are lowered according to std.builtin.TestFn, which is `fn () anyerror!void`. +/// (anyerror!void has the same layout as anyerror). +/// Each test declaration generates a function like. +/// %anyerror = OpTypeInt 0 16 +/// %p_invocation_globals_struct_ty = ... +/// %p_anyerror = OpTypePointer CrossWorkgroup %anyerror +/// %K = OpTypeFunction %void %p_invocation_globals_struct_ty %p_anyerror +/// +/// %test = OpFunction %void %K +/// %p_invocation_globals = OpFunctionParameter p_invocation_globals_struct_ty +/// %p_err = OpFunctionParameter %p_anyerror +/// %lbl = OpLabel +/// %result = OpFunctionCall %anyerror %func %p_invocation_globals +/// OpStore %p_err %result +/// OpFunctionEnd +/// TODO is to also write out the error as a function call parameter, and to somehow fetch +/// the name of an error in the text executor. +fn generateTestEntryPoint( + cg: *CodeGen, + name: []const u8, + spv_decl_index: Module.Decl.Index, + test_id: Id, +) !void { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + + const anyerror_ty_id = try cg.resolveType(.anyerror, .direct); + const ptr_anyerror_ty = try cg.pt.ptrType(.{ + .child = .anyerror_type, + .flags = .{ .address_space = .global }, + }); + const ptr_anyerror_ty_id = try cg.resolveType(ptr_anyerror_ty, .direct); + + const kernel_id = cg.module.declPtr(spv_decl_index).result_id; + + const section = &cg.module.sections.functions; + + const p_error_id = cg.module.allocId(); + switch (target.os.tag) { + .opencl, .amdhsa => { + const void_ty_id = try cg.resolveType(.void, .direct); + const kernel_proto_ty_id = try cg.module.functionType(void_ty_id, &.{ptr_anyerror_ty_id}); + + try section.emit(gpa, .OpFunction, .{ + .id_result_type = try cg.resolveType(.void, .direct), + .id_result = kernel_id, + .function_control = .{}, + .function_type = kernel_proto_ty_id, + }); + + try section.emit(gpa, .OpFunctionParameter, .{ + .id_result_type = ptr_anyerror_ty_id, + .id_result = p_error_id, + }); + + try section.emit(gpa, .OpLabel, .{ + .id_result = cg.module.allocId(), + }); + }, + .vulkan, .opengl => { + if (cg.module.error_buffer == null) { + const spv_err_decl_index = try cg.module.allocDecl(.global); + const err_buf_result_id = cg.module.declPtr(spv_err_decl_index).result_id; + + const buffer_struct_ty_id = try cg.module.structType( + &.{anyerror_ty_id}, + &.{"error_out"}, + null, + .none, + ); + try cg.module.decorate(buffer_struct_ty_id, .block); + try cg.module.decorateMember(buffer_struct_ty_id, 0, .{ .offset = .{ .byte_offset = 0 } }); + + const ptr_buffer_struct_ty_id = cg.module.allocId(); + try cg.module.sections.globals.emit(gpa, .OpTypePointer, .{ + .id_result = ptr_buffer_struct_ty_id, + .storage_class = cg.module.storageClass(.global), + .type = buffer_struct_ty_id, + }); + + try cg.module.sections.globals.emit(gpa, .OpVariable, .{ + .id_result_type = ptr_buffer_struct_ty_id, + .id_result = err_buf_result_id, + .storage_class = cg.module.storageClass(.global), + }); + try cg.module.decorate(err_buf_result_id, .{ .descriptor_set = .{ .descriptor_set = 0 } }); + try cg.module.decorate(err_buf_result_id, .{ .binding = .{ .binding_point = 0 } }); + + cg.module.error_buffer = spv_err_decl_index; + } + + try cg.module.sections.execution_modes.emit(gpa, .OpExecutionMode, .{ + .entry_point = kernel_id, + .mode = .{ .local_size = .{ + .x_size = 1, + .y_size = 1, + .z_size = 1, + } }, + }); + + const void_ty_id = try cg.resolveType(.void, .direct); + const kernel_proto_ty_id = try cg.module.functionType(void_ty_id, &.{}); + try section.emit(gpa, .OpFunction, .{ + .id_result_type = try cg.resolveType(.void, .direct), + .id_result = kernel_id, + .function_control = .{}, + .function_type = kernel_proto_ty_id, + }); + try section.emit(gpa, .OpLabel, .{ + .id_result = cg.module.allocId(), + }); + + const spv_err_decl_index = cg.module.error_buffer.?; + const buffer_id = cg.module.declPtr(spv_err_decl_index).result_id; + try cg.module.decl_deps.append(gpa, spv_err_decl_index); + + const zero_id = try cg.constInt(.u32, 0); + try section.emit(gpa, .OpInBoundsAccessChain, .{ + .id_result_type = ptr_anyerror_ty_id, + .id_result = p_error_id, + .base = buffer_id, + .indexes = &.{zero_id}, + }); + }, + else => unreachable, + } + + const error_id = cg.module.allocId(); + try section.emit(gpa, .OpFunctionCall, .{ + .id_result_type = anyerror_ty_id, + .id_result = error_id, + .function = test_id, + }); + // Note: Convert to direct not required. + try section.emit(gpa, .OpStore, .{ + .pointer = p_error_id, + .object = error_id, + .memory_access = .{ + .aligned = .{ .literal_integer = @intCast(Type.abiAlignment(.anyerror, zcu).toByteUnits().?) }, + }, + }); + try section.emit(gpa, .OpReturn, {}); + try section.emit(gpa, .OpFunctionEnd, {}); + + // Just generate a quick other name because the intel runtime crashes when the entry- + // point name is the same as a different OpName. + const test_name = try std.fmt.allocPrint(cg.module.arena, "test {s}", .{name}); + + const execution_mode: spec.ExecutionModel = switch (target.os.tag) { + .vulkan, .opengl => .gl_compute, + .opencl, .amdhsa => .kernel, + else => unreachable, + }; + + try cg.module.declareEntryPoint(spv_decl_index, test_name, execution_mode, null); +} + +fn intFromBool(cg: *CodeGen, value: Temporary) !Temporary { + return try cg.intFromBool2(value, Type.u1); +} + +fn intFromBool2(cg: *CodeGen, value: Temporary, result_ty: Type) !Temporary { + const zero_id = try cg.constInt(result_ty, 0); + const one_id = try cg.constInt(result_ty, 1); + + return try cg.buildSelect( + value, + Temporary.init(result_ty, one_id), + Temporary.init(result_ty, zero_id), + ); +} + +/// Convert representation from indirect (in memory) to direct (in 'register') +/// This converts the argument type from resolveType(ty, .indirect) to resolveType(ty, .direct). +fn convertToDirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { + const pt = cg.pt; + const zcu = cg.module.zcu; + switch (ty.scalarType(zcu).zigTypeTag(zcu)) { + .bool => { + const false_id = try cg.constBool(false, .indirect); + const operand_ty = blk: { + if (!ty.isVector(zcu)) break :blk Type.u1; + break :blk try pt.vectorType(.{ + .len = ty.vectorLen(zcu), + .child = .u1_type, + }); + }; + + const result = try cg.buildCmp( + .OpINotEqual, + Temporary.init(operand_ty, operand_id), + Temporary.init(.u1, false_id), + ); + return try result.materialize(cg); + }, + else => return operand_id, + } +} + +/// Convert representation from direct (in 'register) to direct (in memory) +/// This converts the argument type from resolveType(ty, .direct) to resolveType(ty, .indirect). +fn convertToIndirect(cg: *CodeGen, ty: Type, operand_id: Id) !Id { + const zcu = cg.module.zcu; + switch (ty.scalarType(zcu).zigTypeTag(zcu)) { + .bool => { + const result = try cg.intFromBool(Temporary.init(ty, operand_id)); + return try result.materialize(cg); + }, + else => return operand_id, + } +} + +fn extractField(cg: *CodeGen, result_ty: Type, object: Id, field: u32) !Id { + const result_ty_id = try cg.resolveType(result_ty, .indirect); + const result_id = cg.module.allocId(); + const indexes = [_]u32{field}; + try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .composite = object, + .indexes = &indexes, + }); + // Convert bools; direct structs have their field types as indirect values. + return try cg.convertToDirect(result_ty, result_id); +} + +fn extractVectorComponent(cg: *CodeGen, result_ty: Type, vector_id: Id, field: u32) !Id { + const result_ty_id = try cg.resolveType(result_ty, .direct); + const result_id = cg.module.allocId(); + const indexes = [_]u32{field}; + try cg.body.emit(cg.module.gpa, .OpCompositeExtract, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .composite = vector_id, + .indexes = &indexes, + }); + // Vector components are already stored in direct representation. + return result_id; +} + +const MemoryOptions = struct { + is_volatile: bool = false, +}; + +fn load(cg: *CodeGen, value_ty: Type, ptr_id: Id, options: MemoryOptions) !Id { + const zcu = cg.module.zcu; + const alignment: u32 = @intCast(value_ty.abiAlignment(zcu).toByteUnits().?); + const indirect_value_ty_id = try cg.resolveType(value_ty, .indirect); + const result_id = cg.module.allocId(); + const access: spec.MemoryAccess.Extended = .{ + .@"volatile" = options.is_volatile, + .aligned = .{ .literal_integer = alignment }, + }; + try cg.body.emit(cg.module.gpa, .OpLoad, .{ + .id_result_type = indirect_value_ty_id, + .id_result = result_id, + .pointer = ptr_id, + .memory_access = access, + }); + return try cg.convertToDirect(value_ty, result_id); +} + +fn store(cg: *CodeGen, value_ty: Type, ptr_id: Id, value_id: Id, options: MemoryOptions) !void { + const indirect_value_id = try cg.convertToIndirect(value_ty, value_id); + const access: spec.MemoryAccess.Extended = .{ .@"volatile" = options.is_volatile }; + try cg.body.emit(cg.module.gpa, .OpStore, .{ + .pointer = ptr_id, + .object = indirect_value_id, + .memory_access = access, + }); +} + +fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) !void { + for (body) |inst| { + try cg.genInst(inst); + } +} + +fn genInst(cg: *CodeGen, inst: Air.Inst.Index) Error!void { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + if (cg.liveness.isUnused(inst) and !cg.air.mustLower(inst, ip)) + return; + + const air_tags = cg.air.instructions.items(.tag); + const maybe_result_id: ?Id = switch (air_tags[@intFromEnum(inst)]) { + // zig fmt: off + .add, .add_wrap, .add_optimized => try cg.airArithOp(inst, .OpFAdd, .OpIAdd, .OpIAdd), + .sub, .sub_wrap, .sub_optimized => try cg.airArithOp(inst, .OpFSub, .OpISub, .OpISub), + .mul, .mul_wrap, .mul_optimized => try cg.airArithOp(inst, .OpFMul, .OpIMul, .OpIMul), + + .sqrt => try cg.airUnOpSimple(inst, .sqrt), + .sin => try cg.airUnOpSimple(inst, .sin), + .cos => try cg.airUnOpSimple(inst, .cos), + .tan => try cg.airUnOpSimple(inst, .tan), + .exp => try cg.airUnOpSimple(inst, .exp), + .exp2 => try cg.airUnOpSimple(inst, .exp2), + .log => try cg.airUnOpSimple(inst, .log), + .log2 => try cg.airUnOpSimple(inst, .log2), + .log10 => try cg.airUnOpSimple(inst, .log10), + .abs => try cg.airAbs(inst), + .floor => try cg.airUnOpSimple(inst, .floor), + .ceil => try cg.airUnOpSimple(inst, .ceil), + .round => try cg.airUnOpSimple(inst, .round), + .trunc_float => try cg.airUnOpSimple(inst, .trunc), + .neg, .neg_optimized => try cg.airUnOpSimple(inst, .f_neg), + + .div_float, .div_float_optimized => try cg.airArithOp(inst, .OpFDiv, .OpSDiv, .OpUDiv), + .div_floor, .div_floor_optimized => try cg.airDivFloor(inst), + .div_trunc, .div_trunc_optimized => try cg.airDivTrunc(inst), + + .rem, .rem_optimized => try cg.airArithOp(inst, .OpFRem, .OpSRem, .OpUMod), + .mod, .mod_optimized => try cg.airArithOp(inst, .OpFMod, .OpSMod, .OpUMod), + + .add_with_overflow => try cg.airAddSubOverflow(inst, .OpIAdd, .OpULessThan, .OpSLessThan), + .sub_with_overflow => try cg.airAddSubOverflow(inst, .OpISub, .OpUGreaterThan, .OpSGreaterThan), + .mul_with_overflow => try cg.airMulOverflow(inst), + .shl_with_overflow => try cg.airShlOverflow(inst), + + .mul_add => try cg.airMulAdd(inst), + + .ctz => try cg.airClzCtz(inst, .ctz), + .clz => try cg.airClzCtz(inst, .clz), + + .select => try cg.airSelect(inst), + + .splat => try cg.airSplat(inst), + .reduce, .reduce_optimized => try cg.airReduce(inst), + .shuffle_one => try cg.airShuffleOne(inst), + .shuffle_two => try cg.airShuffleTwo(inst), + + .ptr_add => try cg.airPtrAdd(inst), + .ptr_sub => try cg.airPtrSub(inst), + + .bit_and => try cg.airBinOpSimple(inst, .OpBitwiseAnd), + .bit_or => try cg.airBinOpSimple(inst, .OpBitwiseOr), + .xor => try cg.airBinOpSimple(inst, .OpBitwiseXor), + .bool_and => try cg.airBinOpSimple(inst, .OpLogicalAnd), + .bool_or => try cg.airBinOpSimple(inst, .OpLogicalOr), + + .shl, .shl_exact => try cg.airShift(inst, .OpShiftLeftLogical, .OpShiftLeftLogical), + .shr, .shr_exact => try cg.airShift(inst, .OpShiftRightLogical, .OpShiftRightArithmetic), + + .min => try cg.airMinMax(inst, .min), + .max => try cg.airMinMax(inst, .max), + + .bitcast => try cg.airBitCast(inst), + .intcast, .trunc => try cg.airIntCast(inst), + .float_from_int => try cg.airFloatFromInt(inst), + .int_from_float => try cg.airIntFromFloat(inst), + .fpext, .fptrunc => try cg.airFloatCast(inst), + .not => try cg.airNot(inst), + + .array_to_slice => try cg.airArrayToSlice(inst), + .slice => try cg.airSlice(inst), + .aggregate_init => try cg.airAggregateInit(inst), + .memcpy => return cg.airMemcpy(inst), + .memmove => return cg.airMemmove(inst), + + .slice_ptr => try cg.airSliceField(inst, 0), + .slice_len => try cg.airSliceField(inst, 1), + .slice_elem_ptr => try cg.airSliceElemPtr(inst), + .slice_elem_val => try cg.airSliceElemVal(inst), + .ptr_elem_ptr => try cg.airPtrElemPtr(inst), + .ptr_elem_val => try cg.airPtrElemVal(inst), + .array_elem_val => try cg.airArrayElemVal(inst), + + .vector_store_elem => return cg.airVectorStoreElem(inst), + + .set_union_tag => return cg.airSetUnionTag(inst), + .get_union_tag => try cg.airGetUnionTag(inst), + .union_init => try cg.airUnionInit(inst), + + .struct_field_val => try cg.airStructFieldVal(inst), + .field_parent_ptr => try cg.airFieldParentPtr(inst), + + .struct_field_ptr_index_0 => try cg.airStructFieldPtrIndex(inst, 0), + .struct_field_ptr_index_1 => try cg.airStructFieldPtrIndex(inst, 1), + .struct_field_ptr_index_2 => try cg.airStructFieldPtrIndex(inst, 2), + .struct_field_ptr_index_3 => try cg.airStructFieldPtrIndex(inst, 3), + + .cmp_eq => try cg.airCmp(inst, .eq), + .cmp_neq => try cg.airCmp(inst, .neq), + .cmp_gt => try cg.airCmp(inst, .gt), + .cmp_gte => try cg.airCmp(inst, .gte), + .cmp_lt => try cg.airCmp(inst, .lt), + .cmp_lte => try cg.airCmp(inst, .lte), + .cmp_vector => try cg.airVectorCmp(inst), + + .arg => cg.airArg(), + .alloc => try cg.airAlloc(inst), + // TODO: We probably need to have a special implementation of this for the C abi. + .ret_ptr => try cg.airAlloc(inst), + .block => try cg.airBlock(inst), + + .load => try cg.airLoad(inst), + .store, .store_safe => return cg.airStore(inst), + + .br => return cg.airBr(inst), + // For now just ignore this instruction. This effectively falls back on the old implementation, + // this doesn't change anything for us. + .repeat => return, + .breakpoint => return, + .cond_br => return cg.airCondBr(inst), + .loop => return cg.airLoop(inst), + .ret => return cg.airRet(inst), + .ret_safe => return cg.airRet(inst), // TODO + .ret_load => return cg.airRetLoad(inst), + .@"try" => try cg.airTry(inst), + .switch_br => return cg.airSwitchBr(inst), + .unreach, .trap => return cg.airUnreach(), + + .dbg_empty_stmt => return, + .dbg_stmt => return cg.airDbgStmt(inst), + .dbg_inline_block => try cg.airDbgInlineBlock(inst), + .dbg_var_ptr, .dbg_var_val, .dbg_arg_inline => return cg.airDbgVar(inst), + + .unwrap_errunion_err => try cg.airErrUnionErr(inst), + .unwrap_errunion_payload => try cg.airErrUnionPayload(inst), + .wrap_errunion_err => try cg.airWrapErrUnionErr(inst), + .wrap_errunion_payload => try cg.airWrapErrUnionPayload(inst), + + .is_null => try cg.airIsNull(inst, false, .is_null), + .is_non_null => try cg.airIsNull(inst, false, .is_non_null), + .is_null_ptr => try cg.airIsNull(inst, true, .is_null), + .is_non_null_ptr => try cg.airIsNull(inst, true, .is_non_null), + .is_err => try cg.airIsErr(inst, .is_err), + .is_non_err => try cg.airIsErr(inst, .is_non_err), + + .optional_payload => try cg.airUnwrapOptional(inst), + .optional_payload_ptr => try cg.airUnwrapOptionalPtr(inst), + .wrap_optional => try cg.airWrapOptional(inst), + + .assembly => try cg.airAssembly(inst), + + .call => try cg.airCall(inst, .auto), + .call_always_tail => try cg.airCall(inst, .always_tail), + .call_never_tail => try cg.airCall(inst, .never_tail), + .call_never_inline => try cg.airCall(inst, .never_inline), + + .work_item_id => try cg.airWorkItemId(inst), + .work_group_size => try cg.airWorkGroupSize(inst), + .work_group_id => try cg.airWorkGroupId(inst), + + // zig fmt: on + + else => |tag| return cg.todo("implement AIR tag {s}", .{@tagName(tag)}), + }; + + const result_id = maybe_result_id orelse return; + try cg.inst_results.putNoClobber(gpa, inst, result_id); +} + +fn airBinOpSimple(cg: *CodeGen, inst: Air.Inst.Index, op: Opcode) !?Id { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const lhs = try cg.temporary(bin_op.lhs); + const rhs = try cg.temporary(bin_op.rhs); + + const result = try cg.buildBinary(op, lhs, rhs); + return try result.materialize(cg); +} + +fn airShift(cg: *CodeGen, inst: Air.Inst.Index, unsigned: Opcode, signed: Opcode) !?Id { + const zcu = cg.module.zcu; + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + + if (cg.typeOf(bin_op.lhs).isVector(zcu) and !cg.typeOf(bin_op.rhs).isVector(zcu)) { + return cg.fail("vector shift with scalar rhs", .{}); + } + + const base = try cg.temporary(bin_op.lhs); + const shift = try cg.temporary(bin_op.rhs); + + const result_ty = cg.typeOfIndex(inst); + + const info = cg.arithmeticTypeInfo(result_ty); + switch (info.class) { + .composite_integer => return cg.todo("shift ops for composite integers", .{}), + .integer, .strange_integer => {}, + .float, .bool => unreachable, + } + + // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that, + // so just manually upcast it if required. + + // Note: The sign may differ here between the shift and the base type, in case + // of an arithmetic right shift. SPIR-V still expects the same type, + // so in that case we have to cast convert to signed. + const casted_shift = try cg.buildConvert(base.ty.scalarType(zcu), shift); + + const shifted = switch (info.signedness) { + .unsigned => try cg.buildBinary(unsigned, base, casted_shift), + .signed => try cg.buildBinary(signed, base, casted_shift), + }; + + const result = try cg.normalize(shifted, info); + return try result.materialize(cg); +} + +const MinMax = enum { + min, + max, + + pub fn extInstOpcode( + op: MinMax, + target: *const std.Target, + info: ArithmeticTypeInfo, + ) u32 { + return switch (target.os.tag) { + .opencl => @intFromEnum(@as(spec.OpenClOpcode, switch (info.class) { + .float => switch (op) { + .min => .fmin, + .max => .fmax, + }, + .integer, .strange_integer, .composite_integer => switch (info.signedness) { + .signed => switch (op) { + .min => .s_min, + .max => .s_max, + }, + .unsigned => switch (op) { + .min => .u_min, + .max => .u_max, + }, + }, + .bool => unreachable, + })), + .vulkan, .opengl => @intFromEnum(@as(spec.GlslOpcode, switch (info.class) { + .float => switch (op) { + .min => .FMin, + .max => .FMax, + }, + .integer, .strange_integer, .composite_integer => switch (info.signedness) { + .signed => switch (op) { + .min => .SMin, + .max => .SMax, + }, + .unsigned => switch (op) { + .min => .UMin, + .max => .UMax, + }, + }, + .bool => unreachable, + })), + else => unreachable, + }; + } +}; + +fn airMinMax(cg: *CodeGen, inst: Air.Inst.Index, op: MinMax) !?Id { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + + const lhs = try cg.temporary(bin_op.lhs); + const rhs = try cg.temporary(bin_op.rhs); + + const result = try cg.minMax(lhs, rhs, op); + return try result.materialize(cg); +} + +fn minMax(cg: *CodeGen, lhs: Temporary, rhs: Temporary, op: MinMax) !Temporary { + const zcu = cg.module.zcu; + const target = zcu.getTarget(); + const info = cg.arithmeticTypeInfo(lhs.ty); + + const v = cg.vectorization(.{ lhs, rhs }); + const ops = v.components(); + const results = cg.module.allocIds(ops); + + const op_result_ty = lhs.ty.scalarType(zcu); + const op_result_ty_id = try cg.resolveType(op_result_ty, .direct); + const result_ty = try v.resultType(cg, lhs.ty); + + const op_lhs = try v.prepare(cg, lhs); + const op_rhs = try v.prepare(cg, rhs); + + const set = try cg.importExtendedSet(); + const opcode = op.extInstOpcode(target, info); + for (0..ops) |i| { + try cg.body.emit(cg.module.gpa, .OpExtInst, .{ + .id_result_type = op_result_ty_id, + .id_result = results.at(i), + .set = set, + .instruction = .{ .inst = opcode }, + .id_ref_4 = &.{ op_lhs.at(i), op_rhs.at(i) }, + }); + } + + return v.finalize(result_ty, results); +} + +/// This function normalizes values to a canonical representation +/// after some arithmetic operation. This mostly consists of wrapping +/// behavior for strange integers: +/// - Unsigned integers are bitwise masked with a mask that only passes +/// the valid bits through. +/// - Signed integers are also sign extended if they are negative. +/// All other values are returned unmodified (this makes strange integer +/// wrapping easier to use in generic operations). +fn normalize(cg: *CodeGen, value: Temporary, info: ArithmeticTypeInfo) !Temporary { + const zcu = cg.module.zcu; + const ty = value.ty; + switch (info.class) { + .composite_integer, .integer, .bool, .float => return value, + .strange_integer => switch (info.signedness) { + .unsigned => { + const mask_value = if (info.bits == 64) 0xFFFF_FFFF_FFFF_FFFF else (@as(u64, 1) << @as(u6, @intCast(info.bits))) - 1; + const mask_id = try cg.constInt(ty.scalarType(zcu), mask_value); + return try cg.buildBinary(.OpBitwiseAnd, value, Temporary.init(ty.scalarType(zcu), mask_id)); + }, + .signed => { + // Shift left and right so that we can copy the sight bit that way. + const shift_amt_id = try cg.constInt(ty.scalarType(zcu), info.backing_bits - info.bits); + const shift_amt: Temporary = .init(ty.scalarType(zcu), shift_amt_id); + const left = try cg.buildBinary(.OpShiftLeftLogical, value, shift_amt); + return try cg.buildBinary(.OpShiftRightArithmetic, left, shift_amt); + }, + }, + } +} + +fn airDivFloor(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + + const lhs = try cg.temporary(bin_op.lhs); + const rhs = try cg.temporary(bin_op.rhs); + + const info = cg.arithmeticTypeInfo(lhs.ty); + switch (info.class) { + .composite_integer => unreachable, // TODO + .integer, .strange_integer => { + switch (info.signedness) { + .unsigned => { + const result = try cg.buildBinary(.OpUDiv, lhs, rhs); + return try result.materialize(cg); + }, + .signed => {}, + } + + // For signed integers: + // (a / b) - (a % b != 0 && a < 0 != b < 0); + // There shouldn't be any overflow issues. + + const div = try cg.buildBinary(.OpSDiv, lhs, rhs); + const rem = try cg.buildBinary(.OpSRem, lhs, rhs); + + const zero: Temporary = .init(lhs.ty, try cg.constInt(lhs.ty, 0)); + + const rem_is_not_zero = try cg.buildCmp(.OpINotEqual, rem, zero); + + const result_negative = try cg.buildCmp( + .OpLogicalNotEqual, + try cg.buildCmp(.OpSLessThan, lhs, zero), + try cg.buildCmp(.OpSLessThan, rhs, zero), + ); + const rem_is_not_zero_and_result_is_negative = try cg.buildBinary( + .OpLogicalAnd, + rem_is_not_zero, + result_negative, + ); + + const result = try cg.buildBinary( + .OpISub, + div, + try cg.intFromBool2(rem_is_not_zero_and_result_is_negative, div.ty), + ); + + return try result.materialize(cg); + }, + .float => { + const div = try cg.buildBinary(.OpFDiv, lhs, rhs); + const result = try cg.buildUnary(.floor, div); + return try result.materialize(cg); + }, + .bool => unreachable, + } +} + +fn airDivTrunc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + + const lhs = try cg.temporary(bin_op.lhs); + const rhs = try cg.temporary(bin_op.rhs); + + const info = cg.arithmeticTypeInfo(lhs.ty); + switch (info.class) { + .composite_integer => unreachable, // TODO + .integer, .strange_integer => switch (info.signedness) { + .unsigned => { + const result = try cg.buildBinary(.OpUDiv, lhs, rhs); + return try result.materialize(cg); + }, + .signed => { + const result = try cg.buildBinary(.OpSDiv, lhs, rhs); + return try result.materialize(cg); + }, + }, + .float => { + const div = try cg.buildBinary(.OpFDiv, lhs, rhs); + const result = try cg.buildUnary(.trunc, div); + return try result.materialize(cg); + }, + .bool => unreachable, + } +} + +fn airUnOpSimple(cg: *CodeGen, inst: Air.Inst.Index, op: UnaryOp) !?Id { + const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; + const operand = try cg.temporary(un_op); + const result = try cg.buildUnary(op, operand); + return try result.materialize(cg); +} + +fn airArithOp( + cg: *CodeGen, + inst: Air.Inst.Index, + comptime fop: Opcode, + comptime sop: Opcode, + comptime uop: Opcode, +) !?Id { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + + const lhs = try cg.temporary(bin_op.lhs); + const rhs = try cg.temporary(bin_op.rhs); + + const info = cg.arithmeticTypeInfo(lhs.ty); + + const result = switch (info.class) { + .composite_integer => unreachable, // TODO + .integer, .strange_integer => switch (info.signedness) { + .signed => try cg.buildBinary(sop, lhs, rhs), + .unsigned => try cg.buildBinary(uop, lhs, rhs), + }, + .float => try cg.buildBinary(fop, lhs, rhs), + .bool => unreachable, + }; + + return try result.materialize(cg); +} + +fn airAbs(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand = try cg.temporary(ty_op.operand); + // Note: operand_ty may be signed, while ty is always unsigned! + const result_ty = cg.typeOfIndex(inst); + const result = try cg.abs(result_ty, operand); + return try result.materialize(cg); +} + +fn abs(cg: *CodeGen, result_ty: Type, value: Temporary) !Temporary { + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + const operand_info = cg.arithmeticTypeInfo(value.ty); + + switch (operand_info.class) { + .float => return try cg.buildUnary(.f_abs, value), + .integer, .strange_integer => { + const abs_value = try cg.buildUnary(.i_abs, value); + + switch (target.os.tag) { + .vulkan, .opengl => { + if (value.ty.intInfo(zcu).signedness == .signed) { + return cg.todo("perform bitcast after @abs", .{}); + } + }, + else => {}, + } + + return try cg.normalize(abs_value, cg.arithmeticTypeInfo(result_ty)); + }, + .composite_integer => unreachable, // TODO + .bool => unreachable, + } +} + +fn airAddSubOverflow( + cg: *CodeGen, + inst: Air.Inst.Index, + comptime add: Opcode, + u_opcode: Opcode, + s_opcode: Opcode, +) !?Id { + _ = s_opcode; + // Note: OpIAddCarry and OpISubBorrow are not really useful here: For unsigned numbers, + // there is in both cases only one extra operation required. For signed operations, + // the overflow bit is set then going from 0x80.. to 0x00.., but this doesn't actually + // normally set a carry bit. So the SPIR-V overflow operations are not particularly + // useful here. + + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; + + const lhs = try cg.temporary(extra.lhs); + const rhs = try cg.temporary(extra.rhs); + + const result_ty = cg.typeOfIndex(inst); + + const info = cg.arithmeticTypeInfo(lhs.ty); + switch (info.class) { + .composite_integer => unreachable, // TODO + .strange_integer, .integer => {}, + .float, .bool => unreachable, + } + + const sum = try cg.buildBinary(add, lhs, rhs); + const result = try cg.normalize(sum, info); + + const overflowed = switch (info.signedness) { + // Overflow happened if the result is smaller than either of the operands. It doesn't matter which. + // For subtraction the conditions need to be swapped. + .unsigned => try cg.buildCmp(u_opcode, result, lhs), + // For signed operations, we check the signs of the operands and the result. + .signed => blk: { + // Signed overflow detection using the sign bits of the operands and the result. + // For addition (a + b), overflow occurs if the operands have the same sign + // and the result's sign is different from the operands' sign. + // (sign(a) == sign(b)) && (sign(a) != sign(result)) + // For subtraction (a - b), overflow occurs if the operands have different signs + // and the result's sign is different from the minuend's (a's) sign. + // (sign(a) != sign(b)) && (sign(a) != sign(result)) + const zero: Temporary = .init(rhs.ty, try cg.constInt(rhs.ty, 0)); + + const lhs_is_neg = try cg.buildCmp(.OpSLessThan, lhs, zero); + const rhs_is_neg = try cg.buildCmp(.OpSLessThan, rhs, zero); + const result_is_neg = try cg.buildCmp(.OpSLessThan, result, zero); + + const signs_match = try cg.buildCmp(.OpLogicalEqual, lhs_is_neg, rhs_is_neg); + const result_sign_differs = try cg.buildCmp(.OpLogicalNotEqual, lhs_is_neg, result_is_neg); + + const overflow_condition = if (add == .OpIAdd) + signs_match + else // .OpISub + try cg.buildUnary(.l_not, signs_match); + + break :blk try cg.buildCmp(.OpLogicalAnd, overflow_condition, result_sign_differs); + }, + }; + + const ov = try cg.intFromBool(overflowed); + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, &.{ try result.materialize(cg), try ov.materialize(cg) }); +} + +fn airMulOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const pt = cg.pt; + + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; + + const lhs = try cg.temporary(extra.lhs); + const rhs = try cg.temporary(extra.rhs); + + const result_ty = cg.typeOfIndex(inst); + + const info = cg.arithmeticTypeInfo(lhs.ty); + switch (info.class) { + .composite_integer => unreachable, // TODO + .strange_integer, .integer => {}, + .float, .bool => unreachable, + } + + // There are 3 cases which we have to deal with: + // - If info.bits < 32 / 2, we will upcast to 32 and check the higher bits + // - If info.bits > 32 / 2, we have to use extended multiplication + // - Additionally, if info.bits != 32, we'll have to check the high bits + // of the result too. + + const largest_int_bits = cg.largestSupportedIntBits(); + // If non-null, the number of bits that the multiplication should be performed in. If + // null, we have to use wide multiplication. + const maybe_op_ty_bits: ?u16 = switch (info.bits) { + 0 => unreachable, + 1...16 => 32, + 17...32 => if (largest_int_bits > 32) 64 else null, // Upcast if we can. + 33...64 => null, // Always use wide multiplication. + else => unreachable, // TODO: Composite integers + }; + + const result, const overflowed = switch (info.signedness) { + .unsigned => blk: { + if (maybe_op_ty_bits) |op_ty_bits| { + const op_ty = try pt.intType(.unsigned, op_ty_bits); + const casted_lhs = try cg.buildConvert(op_ty, lhs); + const casted_rhs = try cg.buildConvert(op_ty, rhs); + + const full_result = try cg.buildBinary(.OpIMul, casted_lhs, casted_rhs); + + const low_bits = try cg.buildConvert(lhs.ty, full_result); + const result = try cg.normalize(low_bits, info); + + // Shift the result bits away to get the overflow bits. + const shift: Temporary = .init(full_result.ty, try cg.constInt(full_result.ty, info.bits)); + const overflow = try cg.buildBinary(.OpShiftRightLogical, full_result, shift); + + // Directly check if its zero in the op_ty without converting first. + const zero: Temporary = .init(full_result.ty, try cg.constInt(full_result.ty, 0)); + const overflowed = try cg.buildCmp(.OpINotEqual, zero, overflow); + + break :blk .{ result, overflowed }; + } + + const low_bits, const high_bits = try cg.buildWideMul(.unsigned, lhs, rhs); + + // Truncate the result, if required. + const result = try cg.normalize(low_bits, info); + + // Overflow happened if the high-bits of the result are non-zero OR if the + // high bits of the low word of the result (those outside the range of the + // int) are nonzero. + const zero: Temporary = .init(lhs.ty, try cg.constInt(lhs.ty, 0)); + const high_overflowed = try cg.buildCmp(.OpINotEqual, zero, high_bits); + + // If no overflow bits in low_bits, no extra work needs to be done. + if (info.backing_bits == info.bits) break :blk .{ result, high_overflowed }; + + // Shift the result bits away to get the overflow bits. + const shift: Temporary = .init(lhs.ty, try cg.constInt(lhs.ty, info.bits)); + const low_overflow = try cg.buildBinary(.OpShiftRightLogical, low_bits, shift); + const low_overflowed = try cg.buildCmp(.OpINotEqual, zero, low_overflow); + + const overflowed = try cg.buildCmp(.OpLogicalOr, low_overflowed, high_overflowed); + + break :blk .{ result, overflowed }; + }, + .signed => blk: { + // - lhs >= 0, rhxs >= 0: expect positive; overflow should be 0 + // - lhs == 0 : expect positive; overflow should be 0 + // - rhs == 0: expect positive; overflow should be 0 + // - lhs > 0, rhs < 0: expect negative; overflow should be -1 + // - lhs < 0, rhs > 0: expect negative; overflow should be -1 + // - lhs <= 0, rhs <= 0: expect positive; overflow should be 0 + // ------ + // overflow should be -1 when + // (lhs > 0 && rhs < 0) || (lhs < 0 && rhs > 0) + + const zero: Temporary = .init(lhs.ty, try cg.constInt(lhs.ty, 0)); + const lhs_negative = try cg.buildCmp(.OpSLessThan, lhs, zero); + const rhs_negative = try cg.buildCmp(.OpSLessThan, rhs, zero); + const lhs_positive = try cg.buildCmp(.OpSGreaterThan, lhs, zero); + const rhs_positive = try cg.buildCmp(.OpSGreaterThan, rhs, zero); + + // Set to `true` if we expect -1. + const expected_overflow_bit = try cg.buildBinary( + .OpLogicalOr, + try cg.buildCmp(.OpLogicalAnd, lhs_positive, rhs_negative), + try cg.buildCmp(.OpLogicalAnd, lhs_negative, rhs_positive), + ); + + if (maybe_op_ty_bits) |op_ty_bits| { + const op_ty = try pt.intType(.signed, op_ty_bits); + // Assume normalized; sign bit is set. We want a sign extend. + const casted_lhs = try cg.buildConvert(op_ty, lhs); + const casted_rhs = try cg.buildConvert(op_ty, rhs); + + const full_result = try cg.buildBinary(.OpIMul, casted_lhs, casted_rhs); + + // Truncate to the result type. + const low_bits = try cg.buildConvert(lhs.ty, full_result); + const result = try cg.normalize(low_bits, info); + + // Now, we need to check the overflow bits AND the sign + // bit for the expected overflow bits. + // To do that, shift out everything bit the sign bit and + // then check what remains. + const shift: Temporary = .init(full_result.ty, try cg.constInt(full_result.ty, info.bits - 1)); + // Use SRA so that any sign bits are duplicated. Now we can just check if ALL bits are set + // for negative cases. + const overflow = try cg.buildBinary(.OpShiftRightArithmetic, full_result, shift); + + const long_all_set: Temporary = .init(full_result.ty, try cg.constInt(full_result.ty, -1)); + const long_zero: Temporary = .init(full_result.ty, try cg.constInt(full_result.ty, 0)); + const mask = try cg.buildSelect(expected_overflow_bit, long_all_set, long_zero); + + const overflowed = try cg.buildCmp(.OpINotEqual, mask, overflow); + + break :blk .{ result, overflowed }; + } + + const low_bits, const high_bits = try cg.buildWideMul(.signed, lhs, rhs); + + // Truncate result if required. + const result = try cg.normalize(low_bits, info); + + const all_set: Temporary = .init(lhs.ty, try cg.constInt(lhs.ty, -1)); + const mask = try cg.buildSelect(expected_overflow_bit, all_set, zero); + + // Like with unsigned, overflow happened if high_bits are not the ones we expect, + // and we also need to check some ones from the low bits. + + const high_overflowed = try cg.buildCmp(.OpINotEqual, mask, high_bits); + + // If no overflow bits in low_bits, no extra work needs to be done. + // Careful, we still have to check the sign bit, so this branch + // only goes for i33 and such. + if (info.backing_bits == info.bits + 1) break :blk .{ result, high_overflowed }; + + // Shift the result bits away to get the overflow bits. + const shift: Temporary = .init(lhs.ty, try cg.constInt(lhs.ty, info.bits - 1)); + // Use SRA so that any sign bits are duplicated. Now we can just check if ALL bits are set + // for negative cases. + const low_overflow = try cg.buildBinary(.OpShiftRightArithmetic, low_bits, shift); + const low_overflowed = try cg.buildCmp(.OpINotEqual, mask, low_overflow); + + const overflowed = try cg.buildCmp(.OpLogicalOr, low_overflowed, high_overflowed); + + break :blk .{ result, overflowed }; + }, + }; + + const ov = try cg.intFromBool(overflowed); + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, &.{ try result.materialize(cg), try ov.materialize(cg) }); +} + +fn airShlOverflow(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const extra = cg.air.extraData(Air.Bin, ty_pl.payload).data; + + if (cg.typeOf(extra.lhs).isVector(zcu) and !cg.typeOf(extra.rhs).isVector(zcu)) { + return cg.fail("vector shift with scalar rhs", .{}); + } + + const base = try cg.temporary(extra.lhs); + const shift = try cg.temporary(extra.rhs); + + const result_ty = cg.typeOfIndex(inst); + + const info = cg.arithmeticTypeInfo(base.ty); + switch (info.class) { + .composite_integer => unreachable, // TODO + .integer, .strange_integer => {}, + .float, .bool => unreachable, + } + + // Sometimes Zig doesn't make both of the arguments the same types here. SPIR-V expects that, + // so just manually upcast it if required. + const casted_shift = try cg.buildConvert(base.ty.scalarType(zcu), shift); + + const left = try cg.buildBinary(.OpShiftLeftLogical, base, casted_shift); + const result = try cg.normalize(left, info); + + const right = switch (info.signedness) { + .unsigned => try cg.buildBinary(.OpShiftRightLogical, result, casted_shift), + .signed => try cg.buildBinary(.OpShiftRightArithmetic, result, casted_shift), + }; + + const overflowed = try cg.buildCmp(.OpINotEqual, base, right); + const ov = try cg.intFromBool(overflowed); + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, &.{ try result.materialize(cg), try ov.materialize(cg) }); +} + +fn airMulAdd(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const extra = cg.air.extraData(Air.Bin, pl_op.payload).data; + + const a = try cg.temporary(extra.lhs); + const b = try cg.temporary(extra.rhs); + const c = try cg.temporary(pl_op.operand); + + const result_ty = cg.typeOfIndex(inst); + const info = cg.arithmeticTypeInfo(result_ty); + assert(info.class == .float); // .mul_add is only emitted for floats + + const result = try cg.buildFma(a, b, c); + return try result.materialize(cg); +} + +fn airClzCtz(cg: *CodeGen, inst: Air.Inst.Index, op: UnaryOp) !?Id { + if (cg.liveness.isUnused(inst)) return null; + + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand = try cg.temporary(ty_op.operand); + + const scalar_result_ty = cg.typeOfIndex(inst).scalarType(zcu); + + const info = cg.arithmeticTypeInfo(operand.ty); + switch (info.class) { + .composite_integer => unreachable, // TODO + .integer, .strange_integer => {}, + .float, .bool => unreachable, + } + + const count = try cg.buildUnary(op, operand); + + // Result of OpenCL ctz/clz returns operand.ty, and we want result_ty. + // result_ty is always large enough to hold the result, so we might have to down + // cast it. + const result = try cg.buildConvert(scalar_result_ty, count); + return try result.materialize(cg); +} + +fn airSelect(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const extra = cg.air.extraData(Air.Bin, pl_op.payload).data; + const pred = try cg.temporary(pl_op.operand); + const a = try cg.temporary(extra.lhs); + const b = try cg.temporary(extra.rhs); + + const result = try cg.buildSelect(pred, a, b); + return try result.materialize(cg); +} + +fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + + const operand_id = try cg.resolve(ty_op.operand); + const result_ty = cg.typeOfIndex(inst); + + return try cg.constructCompositeSplat(result_ty, operand_id); +} + +fn airReduce(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const reduce = cg.air.instructions.items(.data)[@intFromEnum(inst)].reduce; + const operand = try cg.resolve(reduce.operand); + const operand_ty = cg.typeOf(reduce.operand); + const scalar_ty = operand_ty.scalarType(zcu); + const scalar_ty_id = try cg.resolveType(scalar_ty, .direct); + const info = cg.arithmeticTypeInfo(operand_ty); + const len = operand_ty.vectorLen(zcu); + const first = try cg.extractVectorComponent(scalar_ty, operand, 0); + + switch (reduce.operation) { + .Min, .Max => |op| { + var result: Temporary = .init(scalar_ty, first); + const cmp_op: MinMax = switch (op) { + .Max => .max, + .Min => .min, + else => unreachable, + }; + for (1..len) |i| { + const lhs = result; + const rhs_id = try cg.extractVectorComponent(scalar_ty, operand, @intCast(i)); + const rhs: Temporary = .init(scalar_ty, rhs_id); + + result = try cg.minMax(lhs, rhs, cmp_op); + } + + return try result.materialize(cg); + }, + else => {}, + } + + var result_id = first; + + const opcode: Opcode = switch (info.class) { + .bool => switch (reduce.operation) { + .And => .OpLogicalAnd, + .Or => .OpLogicalOr, + .Xor => .OpLogicalNotEqual, + else => unreachable, + }, + .strange_integer, .integer => switch (reduce.operation) { + .And => .OpBitwiseAnd, + .Or => .OpBitwiseOr, + .Xor => .OpBitwiseXor, + .Add => .OpIAdd, + .Mul => .OpIMul, + else => unreachable, + }, + .float => switch (reduce.operation) { + .Add => .OpFAdd, + .Mul => .OpFMul, + else => unreachable, + }, + .composite_integer => unreachable, // TODO + }; + + for (1..len) |i| { + const lhs = result_id; + const rhs = try cg.extractVectorComponent(scalar_ty, operand, @intCast(i)); + result_id = cg.module.allocId(); + + try cg.body.emitRaw(cg.module.gpa, opcode, 4); + cg.body.writeOperand(Id, scalar_ty_id); + cg.body.writeOperand(Id, result_id); + cg.body.writeOperand(Id, lhs); + cg.body.writeOperand(Id, rhs); + } + + return result_id; +} + +fn airShuffleOne(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const gpa = zcu.gpa; + + const unwrapped = cg.air.unwrapShuffleOne(zcu, inst); + const mask = unwrapped.mask; + const result_ty = unwrapped.result_ty; + const elem_ty = result_ty.childType(zcu); + const operand = try cg.resolve(unwrapped.operand); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const constituents = try cg.id_scratch.addManyAsSlice(gpa, mask.len); + + for (constituents, mask) |*id, mask_elem| { + id.* = switch (mask_elem.unwrap()) { + .elem => |idx| try cg.extractVectorComponent(elem_ty, operand, idx), + .value => |val| try cg.constant(elem_ty, .fromInterned(val), .direct), + }; + } + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, constituents); +} + +fn airShuffleTwo(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const gpa = zcu.gpa; + + const unwrapped = cg.air.unwrapShuffleTwo(zcu, inst); + const mask = unwrapped.mask; + const result_ty = unwrapped.result_ty; + const elem_ty = result_ty.childType(zcu); + const elem_ty_id = try cg.resolveType(elem_ty, .direct); + const operand_a = try cg.resolve(unwrapped.operand_a); + const operand_b = try cg.resolve(unwrapped.operand_b); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const constituents = try cg.id_scratch.addManyAsSlice(gpa, mask.len); + + for (constituents, mask) |*id, mask_elem| { + id.* = switch (mask_elem.unwrap()) { + .a_elem => |idx| try cg.extractVectorComponent(elem_ty, operand_a, idx), + .b_elem => |idx| try cg.extractVectorComponent(elem_ty, operand_b, idx), + .undef => try cg.module.constUndef(elem_ty_id), + }; + } + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, constituents); +} + +fn accessChainId( + cg: *CodeGen, + result_ty_id: Id, + base: Id, + indices: []const Id, +) !Id { + const result_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpInBoundsAccessChain, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .base = base, + .indexes = indices, + }); + return result_id; +} + +/// AccessChain is essentially PtrAccessChain with 0 as initial argument. The effective +/// difference lies in whether the resulting type of the first dereference will be the +/// same as that of the base pointer, or that of a dereferenced base pointer. AccessChain +/// is the latter and PtrAccessChain is the former. +fn accessChain( + cg: *CodeGen, + result_ty_id: Id, + base: Id, + indices: []const u32, +) !Id { + const gpa = cg.module.gpa; + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const ids = try cg.id_scratch.addManyAsSlice(gpa, indices.len); + for (indices, ids) |index, *id| { + id.* = try cg.constInt(.u32, index); + } + return try cg.accessChainId(result_ty_id, base, ids); +} + +fn ptrAccessChain( + cg: *CodeGen, + result_ty_id: Id, + base: Id, + element: Id, + indices: []const u32, +) !Id { + const gpa = cg.module.gpa; + const target = cg.module.zcu.getTarget(); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const ids = try cg.id_scratch.addManyAsSlice(gpa, indices.len); + for (indices, ids) |index, *id| { + id.* = try cg.constInt(.u32, index); + } + + const result_id = cg.module.allocId(); + switch (target.os.tag) { + .opencl, .amdhsa => { + try cg.body.emit(gpa, .OpInBoundsPtrAccessChain, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .base = base, + .element = element, + .indexes = ids, + }); + }, + .vulkan, .opengl => { + try cg.body.emit(gpa, .OpPtrAccessChain, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .base = base, + .element = element, + .indexes = ids, + }); + }, + else => unreachable, + } + return result_id; +} + +fn ptrAdd(cg: *CodeGen, result_ty: Type, ptr_ty: Type, ptr_id: Id, offset_id: Id) !Id { + const zcu = cg.module.zcu; + const result_ty_id = try cg.resolveType(result_ty, .direct); + + switch (ptr_ty.ptrSize(zcu)) { + .one => { + // Pointer to array + // TODO: Is this correct? + return try cg.accessChainId(result_ty_id, ptr_id, &.{offset_id}); + }, + .c, .many => { + return try cg.ptrAccessChain(result_ty_id, ptr_id, offset_id, &.{}); + }, + .slice => { + // TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does. + const slice_ptr_id = try cg.extractField(result_ty, ptr_id, 0); + return try cg.ptrAccessChain(result_ty_id, slice_ptr_id, offset_id, &.{}); + }, + } +} + +fn airPtrAdd(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; + const ptr_id = try cg.resolve(bin_op.lhs); + const offset_id = try cg.resolve(bin_op.rhs); + const ptr_ty = cg.typeOf(bin_op.lhs); + const result_ty = cg.typeOfIndex(inst); + + return try cg.ptrAdd(result_ty, ptr_ty, ptr_id, offset_id); +} + +fn airPtrSub(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; + const ptr_id = try cg.resolve(bin_op.lhs); + const ptr_ty = cg.typeOf(bin_op.lhs); + const offset_id = try cg.resolve(bin_op.rhs); + const offset_ty = cg.typeOf(bin_op.rhs); + const offset_ty_id = try cg.resolveType(offset_ty, .direct); + const result_ty = cg.typeOfIndex(inst); + + const negative_offset_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpSNegate, .{ + .id_result_type = offset_ty_id, + .id_result = negative_offset_id, + .operand = offset_id, + }); + return try cg.ptrAdd(result_ty, ptr_ty, ptr_id, negative_offset_id); +} + +fn cmp( + cg: *CodeGen, + op: std.math.CompareOperator, + lhs: Temporary, + rhs: Temporary, +) !Temporary { + const gpa = cg.module.gpa; + const pt = cg.pt; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const scalar_ty = lhs.ty.scalarType(zcu); + const is_vector = lhs.ty.isVector(zcu); + + switch (scalar_ty.zigTypeTag(zcu)) { + .int, .bool, .float => {}, + .@"enum" => { + assert(!is_vector); + const ty = lhs.ty.intTagType(zcu); + return try cg.cmp(op, lhs.pun(ty), rhs.pun(ty)); + }, + .@"struct" => { + const struct_ty = zcu.typeToPackedStruct(scalar_ty).?; + const ty: Type = .fromInterned(struct_ty.backingIntTypeUnordered(ip)); + return try cg.cmp(op, lhs.pun(ty), rhs.pun(ty)); + }, + .error_set => { + assert(!is_vector); + const err_int_ty = try pt.errorIntType(); + return try cg.cmp(op, lhs.pun(err_int_ty), rhs.pun(err_int_ty)); + }, + .pointer => { + assert(!is_vector); + // Note that while SPIR-V offers OpPtrEqual and OpPtrNotEqual, they are + // currently not implemented in the SPIR-V LLVM translator. Thus, we emit these using + // OpConvertPtrToU... + + const usize_ty_id = try cg.resolveType(.usize, .direct); + + const lhs_int_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpConvertPtrToU, .{ + .id_result_type = usize_ty_id, + .id_result = lhs_int_id, + .pointer = try lhs.materialize(cg), + }); + + const rhs_int_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpConvertPtrToU, .{ + .id_result_type = usize_ty_id, + .id_result = rhs_int_id, + .pointer = try rhs.materialize(cg), + }); + + const lhs_int: Temporary = .init(.usize, lhs_int_id); + const rhs_int: Temporary = .init(.usize, rhs_int_id); + return try cg.cmp(op, lhs_int, rhs_int); + }, + .optional => { + assert(!is_vector); + + const ty = lhs.ty; + + const payload_ty = ty.optionalChild(zcu); + if (ty.optionalReprIsPayload(zcu)) { + assert(payload_ty.hasRuntimeBitsIgnoreComptime(zcu)); + assert(!payload_ty.isSlice(zcu)); + + return try cg.cmp(op, lhs.pun(payload_ty), rhs.pun(payload_ty)); + } + + const lhs_id = try lhs.materialize(cg); + const rhs_id = try rhs.materialize(cg); + + const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) + try cg.extractField(.bool, lhs_id, 1) + else + try cg.convertToDirect(.bool, lhs_id); + + const rhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) + try cg.extractField(.bool, rhs_id, 1) + else + try cg.convertToDirect(.bool, rhs_id); + + const lhs_valid: Temporary = .init(.bool, lhs_valid_id); + const rhs_valid: Temporary = .init(.bool, rhs_valid_id); + + if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + return try cg.cmp(op, lhs_valid, rhs_valid); + } + + // a = lhs_valid + // b = rhs_valid + // c = lhs_pl == rhs_pl + // + // For op == .eq we have: + // a == b && a -> c + // = a == b && (!a || c) + // + // For op == .neq we have + // a == b && a -> c + // = !(a == b && a -> c) + // = a != b || !(a -> c + // = a != b || !(!a || c) + // = a != b || a && !c + + const lhs_pl_id = try cg.extractField(payload_ty, lhs_id, 0); + const rhs_pl_id = try cg.extractField(payload_ty, rhs_id, 0); + + const lhs_pl: Temporary = .init(payload_ty, lhs_pl_id); + const rhs_pl: Temporary = .init(payload_ty, rhs_pl_id); + + return switch (op) { + .eq => try cg.buildBinary( + .OpLogicalAnd, + try cg.cmp(.eq, lhs_valid, rhs_valid), + try cg.buildBinary( + .OpLogicalOr, + try cg.buildUnary(.l_not, lhs_valid), + try cg.cmp(.eq, lhs_pl, rhs_pl), + ), + ), + .neq => try cg.buildBinary( + .OpLogicalOr, + try cg.cmp(.neq, lhs_valid, rhs_valid), + try cg.buildBinary( + .OpLogicalAnd, + lhs_valid, + try cg.cmp(.neq, lhs_pl, rhs_pl), + ), + ), + else => unreachable, + }; + }, + else => |ty| return cg.todo("implement cmp operation for '{s}' type", .{@tagName(ty)}), + } + + const info = cg.arithmeticTypeInfo(scalar_ty); + const pred: Opcode = switch (info.class) { + .composite_integer => unreachable, // TODO + .float => switch (op) { + .eq => .OpFOrdEqual, + .neq => .OpFUnordNotEqual, + .lt => .OpFOrdLessThan, + .lte => .OpFOrdLessThanEqual, + .gt => .OpFOrdGreaterThan, + .gte => .OpFOrdGreaterThanEqual, + }, + .bool => switch (op) { + .eq => .OpLogicalEqual, + .neq => .OpLogicalNotEqual, + else => unreachable, + }, + .integer, .strange_integer => switch (info.signedness) { + .signed => switch (op) { + .eq => .OpIEqual, + .neq => .OpINotEqual, + .lt => .OpSLessThan, + .lte => .OpSLessThanEqual, + .gt => .OpSGreaterThan, + .gte => .OpSGreaterThanEqual, + }, + .unsigned => switch (op) { + .eq => .OpIEqual, + .neq => .OpINotEqual, + .lt => .OpULessThan, + .lte => .OpULessThanEqual, + .gt => .OpUGreaterThan, + .gte => .OpUGreaterThanEqual, + }, + }, + }; + + return try cg.buildCmp(pred, lhs, rhs); +} + +fn airCmp( + cg: *CodeGen, + inst: Air.Inst.Index, + comptime op: std.math.CompareOperator, +) !?Id { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const lhs = try cg.temporary(bin_op.lhs); + const rhs = try cg.temporary(bin_op.rhs); + + const result = try cg.cmp(op, lhs, rhs); + return try result.materialize(cg); +} + +fn airVectorCmp(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const vec_cmp = cg.air.extraData(Air.VectorCmp, ty_pl.payload).data; + const lhs = try cg.temporary(vec_cmp.lhs); + const rhs = try cg.temporary(vec_cmp.rhs); + const op = vec_cmp.compareOperator(); + + const result = try cg.cmp(op, lhs, rhs); + return try result.materialize(cg); +} + +/// Bitcast one type to another. Note: both types, input, output are expected in **direct** representation. +fn bitCast( + cg: *CodeGen, + dst_ty: Type, + src_ty: Type, + src_id: Id, +) !Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const src_ty_id = try cg.resolveType(src_ty, .direct); + const dst_ty_id = try cg.resolveType(dst_ty, .direct); + + const result_id = blk: { + if (src_ty_id == dst_ty_id) break :blk src_id; + + // TODO: Some more cases are missing here + // See fn bitCast in llvm.zig + + if (src_ty.zigTypeTag(zcu) == .int and dst_ty.isPtrAtRuntime(zcu)) { + const result_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpConvertUToPtr, .{ + .id_result_type = dst_ty_id, + .id_result = result_id, + .integer_value = src_id, + }); + break :blk result_id; + } + + // We can only use OpBitcast for specific conversions: between numerical types, and + // between pointers. If the resolved spir-v types fall into this category then emit OpBitcast, + // otherwise use a temporary and perform a pointer cast. + const can_bitcast = (src_ty.isNumeric(zcu) and dst_ty.isNumeric(zcu)) or (src_ty.isPtrAtRuntime(zcu) and dst_ty.isPtrAtRuntime(zcu)); + if (can_bitcast) { + const result_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpBitcast, .{ + .id_result_type = dst_ty_id, + .id_result = result_id, + .operand = src_id, + }); + + break :blk result_id; + } + + const dst_ptr_ty_id = try cg.module.ptrType(dst_ty_id, .function); + + const tmp_id = try cg.alloc(src_ty, .{ .storage_class = .function }); + try cg.store(src_ty, tmp_id, src_id, .{}); + const casted_ptr_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpBitcast, .{ + .id_result_type = dst_ptr_ty_id, + .id_result = casted_ptr_id, + .operand = tmp_id, + }); + break :blk try cg.load(dst_ty, casted_ptr_id, .{}); + }; + + // Because strange integers use sign-extended representation, we may need to normalize + // the result here. + // TODO: This detail could cause stuff like @as(*const i1, @ptrCast(&@as(u1, 1))) to break + // should we change the representation of strange integers? + if (dst_ty.zigTypeTag(zcu) == .int) { + const info = cg.arithmeticTypeInfo(dst_ty); + const result = try cg.normalize(Temporary.init(dst_ty, result_id), info); + return try result.materialize(cg); + } + + return result_id; +} + +fn airBitCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_ty = cg.typeOf(ty_op.operand); + const result_ty = cg.typeOfIndex(inst); + if (operand_ty.toIntern() == .bool_type) { + const operand = try cg.temporary(ty_op.operand); + const result = try cg.intFromBool(operand); + return try result.materialize(cg); + } + const operand_id = try cg.resolve(ty_op.operand); + return try cg.bitCast(result_ty, operand_ty, operand_id); +} + +fn airIntCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const src = try cg.temporary(ty_op.operand); + const dst_ty = cg.typeOfIndex(inst); + + const src_info = cg.arithmeticTypeInfo(src.ty); + const dst_info = cg.arithmeticTypeInfo(dst_ty); + + if (src_info.backing_bits == dst_info.backing_bits) { + return try src.materialize(cg); + } + + const converted = try cg.buildConvert(dst_ty, src); + + // Make sure to normalize the result if shrinking. + // Because strange ints are sign extended in their backing + // type, we don't need to normalize when growing the type. The + // representation is already the same. + const result = if (dst_info.bits < src_info.bits) + try cg.normalize(converted, dst_info) + else + converted; + + return try result.materialize(cg); +} + +fn intFromPtr(cg: *CodeGen, operand_id: Id) !Id { + const result_type_id = try cg.resolveType(.usize, .direct); + const result_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpConvertPtrToU, .{ + .id_result_type = result_type_id, + .id_result = result_id, + .pointer = operand_id, + }); + return result_id; +} + +fn airFloatFromInt(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_ty = cg.typeOf(ty_op.operand); + const operand_id = try cg.resolve(ty_op.operand); + const result_ty = cg.typeOfIndex(inst); + return try cg.floatFromInt(result_ty, operand_ty, operand_id); +} + +fn floatFromInt(cg: *CodeGen, result_ty: Type, operand_ty: Type, operand_id: Id) !Id { + const gpa = cg.module.gpa; + const operand_info = cg.arithmeticTypeInfo(operand_ty); + const result_id = cg.module.allocId(); + const result_ty_id = try cg.resolveType(result_ty, .direct); + switch (operand_info.signedness) { + .signed => try cg.body.emit(gpa, .OpConvertSToF, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .signed_value = operand_id, + }), + .unsigned => try cg.body.emit(gpa, .OpConvertUToF, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .unsigned_value = operand_id, + }), + } + return result_id; +} + +fn airIntFromFloat(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_id = try cg.resolve(ty_op.operand); + const result_ty = cg.typeOfIndex(inst); + return try cg.intFromFloat(result_ty, operand_id); +} + +fn intFromFloat(cg: *CodeGen, result_ty: Type, operand_id: Id) !Id { + const gpa = cg.module.gpa; + const result_info = cg.arithmeticTypeInfo(result_ty); + const result_ty_id = try cg.resolveType(result_ty, .direct); + const result_id = cg.module.allocId(); + switch (result_info.signedness) { + .signed => try cg.body.emit(gpa, .OpConvertFToS, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .float_value = operand_id, + }), + .unsigned => try cg.body.emit(gpa, .OpConvertFToU, .{ + .id_result_type = result_ty_id, + .id_result = result_id, + .float_value = operand_id, + }), + } + return result_id; +} + +fn airFloatCast(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand = try cg.temporary(ty_op.operand); + const dest_ty = cg.typeOfIndex(inst); + const result = try cg.buildConvert(dest_ty, operand); + return try result.materialize(cg); +} + +fn airNot(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand = try cg.temporary(ty_op.operand); + const result_ty = cg.typeOfIndex(inst); + const info = cg.arithmeticTypeInfo(result_ty); + + const result = switch (info.class) { + .bool => try cg.buildUnary(.l_not, operand), + .float => unreachable, + .composite_integer => unreachable, // TODO + .strange_integer, .integer => blk: { + const complement = try cg.buildUnary(.bit_not, operand); + break :blk try cg.normalize(complement, info); + }, + }; + + return try result.materialize(cg); +} + +fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const array_ptr_ty = cg.typeOf(ty_op.operand); + const array_ty = array_ptr_ty.childType(zcu); + const slice_ty = cg.typeOfIndex(inst); + const elem_ptr_ty = slice_ty.slicePtrFieldType(zcu); + + const elem_ptr_ty_id = try cg.resolveType(elem_ptr_ty, .direct); + + const array_ptr_id = try cg.resolve(ty_op.operand); + const len_id = try cg.constInt(.usize, array_ty.arrayLen(zcu)); + + const elem_ptr_id = if (!array_ty.hasRuntimeBitsIgnoreComptime(zcu)) + // Note: The pointer is something like *opaque{}, so we need to bitcast it to the element type. + try cg.bitCast(elem_ptr_ty, array_ptr_ty, array_ptr_id) + else + // Convert the pointer-to-array to a pointer to the first element. + try cg.accessChain(elem_ptr_ty_id, array_ptr_id, &.{0}); + + const slice_ty_id = try cg.resolveType(slice_ty, .direct); + return try cg.constructComposite(slice_ty_id, &.{ elem_ptr_id, len_id }); +} + +fn airSlice(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; + const ptr_id = try cg.resolve(bin_op.lhs); + const len_id = try cg.resolve(bin_op.rhs); + const slice_ty = cg.typeOfIndex(inst); + const slice_ty_id = try cg.resolveType(slice_ty, .direct); + return try cg.constructComposite(slice_ty_id, &.{ ptr_id, len_id }); +} + +fn airAggregateInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const gpa = cg.module.gpa; + const pt = cg.pt; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const target = cg.module.zcu.getTarget(); + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const result_ty = cg.typeOfIndex(inst); + const len: usize = @intCast(result_ty.arrayLen(zcu)); + const elements: []const Air.Inst.Ref = @ptrCast(cg.air.extra.items[ty_pl.payload..][0..len]); + + switch (result_ty.zigTypeTag(zcu)) { + .@"struct" => { + if (zcu.typeToPackedStruct(result_ty)) |struct_type| { + comptime assert(Type.packed_struct_layout_version == 2); + const backing_int_ty: Type = .fromInterned(struct_type.backingIntTypeUnordered(ip)); + var running_int_id = try cg.constInt(backing_int_ty, 0); + var running_bits: u16 = 0; + for (struct_type.field_types.get(ip), elements) |field_ty_ip, element| { + const field_ty: Type = .fromInterned(field_ty_ip); + if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; + const field_id = try cg.resolve(element); + const ty_bit_size: u16 = @intCast(field_ty.bitSize(zcu)); + const field_int_ty = try cg.pt.intType(.unsigned, ty_bit_size); + const field_int_id = blk: { + if (field_ty.isPtrAtRuntime(zcu)) { + assert(target.cpu.arch == .spirv64 and + field_ty.ptrAddressSpace(zcu) == .storage_buffer); + break :blk try cg.intFromPtr(field_id); + } + break :blk try cg.bitCast(field_int_ty, field_ty, field_id); + }; + const shift_rhs = try cg.constInt(backing_int_ty, running_bits); + const extended_int_conv = try cg.buildConvert(backing_int_ty, .{ + .ty = field_int_ty, + .value = .{ .singleton = field_int_id }, + }); + const shifted = try cg.buildBinary(.OpShiftLeftLogical, extended_int_conv, .{ + .ty = backing_int_ty, + .value = .{ .singleton = shift_rhs }, + }); + const running_int_tmp = try cg.buildBinary( + .OpBitwiseOr, + .{ .ty = backing_int_ty, .value = .{ .singleton = running_int_id } }, + shifted, + ); + running_int_id = try running_int_tmp.materialize(cg); + running_bits += ty_bit_size; + } + return running_int_id; + } + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const constituents = try cg.id_scratch.addManyAsSlice(gpa, elements.len); + + const types = try gpa.alloc(Type, elements.len); + defer gpa.free(types); + + var index: usize = 0; + + switch (ip.indexToKey(result_ty.toIntern())) { + .tuple_type => |tuple| { + for (tuple.types.get(ip), elements, 0..) |field_ty, element, i| { + if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; + assert(Type.fromInterned(field_ty).hasRuntimeBits(zcu)); + + const id = try cg.resolve(element); + types[index] = .fromInterned(field_ty); + constituents[index] = try cg.convertToIndirect(.fromInterned(field_ty), id); + index += 1; + } + }, + .struct_type => { + const struct_type = ip.loadStructType(result_ty.toIntern()); + var it = struct_type.iterateRuntimeOrder(ip); + for (elements, 0..) |element, i| { + const field_index = it.next().?; + if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; + const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]); + assert(field_ty.hasRuntimeBitsIgnoreComptime(zcu)); + + const id = try cg.resolve(element); + types[index] = field_ty; + constituents[index] = try cg.convertToIndirect(field_ty, id); + index += 1; + } + }, + else => unreachable, + } + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, constituents[0..index]); + }, + .vector => { + const n_elems = result_ty.vectorLen(zcu); + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const elem_ids = try cg.id_scratch.addManyAsSlice(gpa, n_elems); + + for (elements, 0..) |element, i| { + elem_ids[i] = try cg.resolve(element); + } + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, elem_ids); + }, + .array => { + const array_info = result_ty.arrayInfo(zcu); + const n_elems: usize = @intCast(result_ty.arrayLenIncludingSentinel(zcu)); + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const elem_ids = try cg.id_scratch.addManyAsSlice(gpa, n_elems); + + for (elements, 0..) |element, i| { + const id = try cg.resolve(element); + elem_ids[i] = try cg.convertToIndirect(array_info.elem_type, id); + } + + if (array_info.sentinel) |sentinel_val| { + elem_ids[n_elems - 1] = try cg.constant(array_info.elem_type, sentinel_val, .indirect); + } + + const result_ty_id = try cg.resolveType(result_ty, .direct); + return try cg.constructComposite(result_ty_id, elem_ids); + }, + else => unreachable, + } +} + +fn sliceOrArrayLen(cg: *CodeGen, operand_id: Id, ty: Type) !Id { + const zcu = cg.module.zcu; + switch (ty.ptrSize(zcu)) { + .slice => return cg.extractField(.usize, operand_id, 1), + .one => { + const array_ty = ty.childType(zcu); + const elem_ty = array_ty.childType(zcu); + const abi_size = elem_ty.abiSize(zcu); + const size = array_ty.arrayLenIncludingSentinel(zcu) * abi_size; + return try cg.constInt(.usize, size); + }, + .many, .c => unreachable, + } +} + +fn sliceOrArrayPtr(cg: *CodeGen, operand_id: Id, ty: Type) !Id { + const zcu = cg.module.zcu; + if (ty.isSlice(zcu)) { + const ptr_ty = ty.slicePtrFieldType(zcu); + return cg.extractField(ptr_ty, operand_id, 0); + } + return operand_id; +} + +fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) !void { + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const dest_slice = try cg.resolve(bin_op.lhs); + const src_slice = try cg.resolve(bin_op.rhs); + const dest_ty = cg.typeOf(bin_op.lhs); + const src_ty = cg.typeOf(bin_op.rhs); + const dest_ptr = try cg.sliceOrArrayPtr(dest_slice, dest_ty); + const src_ptr = try cg.sliceOrArrayPtr(src_slice, src_ty); + const len = try cg.sliceOrArrayLen(dest_slice, dest_ty); + try cg.body.emit(cg.module.gpa, .OpCopyMemorySized, .{ + .target = dest_ptr, + .source = src_ptr, + .size = len, + }); +} + +fn airMemmove(cg: *CodeGen, inst: Air.Inst.Index) !void { + _ = inst; + return cg.fail("TODO implement airMemcpy for spirv", .{}); +} + +fn airSliceField(cg: *CodeGen, inst: Air.Inst.Index, field: u32) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const field_ty = cg.typeOfIndex(inst); + const operand_id = try cg.resolve(ty_op.operand); + return try cg.extractField(field_ty, operand_id, field); +} + +fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; + const slice_ty = cg.typeOf(bin_op.lhs); + if (!slice_ty.isVolatilePtr(zcu) and cg.liveness.isUnused(inst)) return null; + + const slice_id = try cg.resolve(bin_op.lhs); + const index_id = try cg.resolve(bin_op.rhs); + + const ptr_ty = cg.typeOfIndex(inst); + const ptr_ty_id = try cg.resolveType(ptr_ty, .direct); + + const slice_ptr = try cg.extractField(ptr_ty, slice_id, 0); + return try cg.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{}); +} + +fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const slice_ty = cg.typeOf(bin_op.lhs); + if (!slice_ty.isVolatilePtr(zcu) and cg.liveness.isUnused(inst)) return null; + + const slice_id = try cg.resolve(bin_op.lhs); + const index_id = try cg.resolve(bin_op.rhs); + + const ptr_ty = slice_ty.slicePtrFieldType(zcu); + const ptr_ty_id = try cg.resolveType(ptr_ty, .direct); + + const slice_ptr = try cg.extractField(ptr_ty, slice_id, 0); + const elem_ptr = try cg.ptrAccessChain(ptr_ty_id, slice_ptr, index_id, &.{}); + return try cg.load(slice_ty.childType(zcu), elem_ptr, .{ .is_volatile = slice_ty.isVolatilePtr(zcu) }); +} + +fn ptrElemPtr(cg: *CodeGen, ptr_ty: Type, ptr_id: Id, index_id: Id) !Id { + const zcu = cg.module.zcu; + // Construct new pointer type for the resulting pointer + const elem_ty = ptr_ty.elemType2(zcu); // use elemType() so that we get T for *[N]T. + const elem_ty_id = try cg.resolveType(elem_ty, .indirect); + const elem_ptr_ty_id = try cg.module.ptrType(elem_ty_id, cg.module.storageClass(ptr_ty.ptrAddressSpace(zcu))); + if (ptr_ty.isSinglePointer(zcu)) { + // Pointer-to-array. In this case, the resulting pointer is not of the same type + // as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain. + return try cg.accessChainId(elem_ptr_ty_id, ptr_id, &.{index_id}); + } else { + // Resulting pointer type is the same as the ptr_ty, so use ptrAccessChain + return try cg.ptrAccessChain(elem_ptr_ty_id, ptr_id, index_id, &.{}); + } +} + +fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data; + const src_ptr_ty = cg.typeOf(bin_op.lhs); + const elem_ty = src_ptr_ty.childType(zcu); + const ptr_id = try cg.resolve(bin_op.lhs); + + if (!elem_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + const dst_ptr_ty = cg.typeOfIndex(inst); + return try cg.bitCast(dst_ptr_ty, src_ptr_ty, ptr_id); + } + + const index_id = try cg.resolve(bin_op.rhs); + return try cg.ptrElemPtr(src_ptr_ty, ptr_id, index_id); +} + +fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const array_ty = cg.typeOf(bin_op.lhs); + const elem_ty = array_ty.childType(zcu); + const array_id = try cg.resolve(bin_op.lhs); + const index_id = try cg.resolve(bin_op.rhs); + + // SPIR-V doesn't have an array indexing function for some damn reason. + // For now, just generate a temporary and use that. + // TODO: This backend probably also should use isByRef from llvm... + + const is_vector = array_ty.isVector(zcu); + + const elem_repr: Repr = if (is_vector) .direct else .indirect; + const array_ty_id = try cg.resolveType(array_ty, .direct); + const elem_ty_id = try cg.resolveType(elem_ty, elem_repr); + const ptr_array_ty_id = try cg.module.ptrType(array_ty_id, .function); + const ptr_elem_ty_id = try cg.module.ptrType(elem_ty_id, .function); + + const tmp_id = cg.module.allocId(); + try cg.prologue.emit(gpa, .OpVariable, .{ + .id_result_type = ptr_array_ty_id, + .id_result = tmp_id, + .storage_class = .function, + }); + + try cg.body.emit(gpa, .OpStore, .{ + .pointer = tmp_id, + .object = array_id, + }); + + const elem_ptr_id = try cg.accessChainId(ptr_elem_ty_id, tmp_id, &.{index_id}); + + const result_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpLoad, .{ + .id_result_type = try cg.resolveType(elem_ty, elem_repr), + .id_result = result_id, + .pointer = elem_ptr_id, + }); + + if (is_vector) { + // Result is already in direct representation + return result_id; + } + + // This is an array type; the elements are stored in indirect representation. + // We have to convert the type to direct. + + return try cg.convertToDirect(elem_ty, result_id); +} + +fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const ptr_ty = cg.typeOf(bin_op.lhs); + const elem_ty = cg.typeOfIndex(inst); + const ptr_id = try cg.resolve(bin_op.lhs); + const index_id = try cg.resolve(bin_op.rhs); + const elem_ptr_id = try cg.ptrElemPtr(ptr_ty, ptr_id, index_id); + return try cg.load(elem_ty, elem_ptr_id, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); +} + +fn airVectorStoreElem(cg: *CodeGen, inst: Air.Inst.Index) !void { + const zcu = cg.module.zcu; + const data = cg.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; + const extra = cg.air.extraData(Air.Bin, data.payload).data; + + const vector_ptr_ty = cg.typeOf(data.vector_ptr); + const vector_ty = vector_ptr_ty.childType(zcu); + const scalar_ty = vector_ty.scalarType(zcu); + + const scalar_ty_id = try cg.resolveType(scalar_ty, .indirect); + const storage_class = cg.module.storageClass(vector_ptr_ty.ptrAddressSpace(zcu)); + const scalar_ptr_ty_id = try cg.module.ptrType(scalar_ty_id, storage_class); + + const vector_ptr = try cg.resolve(data.vector_ptr); + const index = try cg.resolve(extra.lhs); + const operand = try cg.resolve(extra.rhs); + + const elem_ptr_id = try cg.accessChainId(scalar_ptr_ty_id, vector_ptr, &.{index}); + try cg.store(scalar_ty, elem_ptr_id, operand, .{ + .is_volatile = vector_ptr_ty.isVolatilePtr(zcu), + }); +} + +fn airSetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !void { + const zcu = cg.module.zcu; + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const un_ptr_ty = cg.typeOf(bin_op.lhs); + const un_ty = un_ptr_ty.childType(zcu); + const layout = cg.unionLayout(un_ty); + + if (layout.tag_size == 0) return; + + const tag_ty = un_ty.unionTagTypeSafety(zcu).?; + const tag_ty_id = try cg.resolveType(tag_ty, .indirect); + const tag_ptr_ty_id = try cg.module.ptrType(tag_ty_id, cg.module.storageClass(un_ptr_ty.ptrAddressSpace(zcu))); + + const union_ptr_id = try cg.resolve(bin_op.lhs); + const new_tag_id = try cg.resolve(bin_op.rhs); + + if (!layout.has_payload) { + try cg.store(tag_ty, union_ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(zcu) }); + } else { + const ptr_id = try cg.accessChain(tag_ptr_ty_id, union_ptr_id, &.{layout.tag_index}); + try cg.store(tag_ty, ptr_id, new_tag_id, .{ .is_volatile = un_ptr_ty.isVolatilePtr(zcu) }); + } +} + +fn airGetUnionTag(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const un_ty = cg.typeOf(ty_op.operand); + + const zcu = cg.module.zcu; + const layout = cg.unionLayout(un_ty); + if (layout.tag_size == 0) return null; + + const union_handle = try cg.resolve(ty_op.operand); + if (!layout.has_payload) return union_handle; + + const tag_ty = un_ty.unionTagTypeSafety(zcu).?; + return try cg.extractField(tag_ty, union_handle, layout.tag_index); +} + +fn unionInit( + cg: *CodeGen, + ty: Type, + active_field: u32, + payload: ?Id, +) !Id { + // To initialize a union, generate a temporary variable with the + // union type, then get the field pointer and pointer-cast it to the + // right type to store it. Finally load the entire union. + + // Note: The result here is not cached, because it generates runtime code. + + const pt = cg.pt; + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const union_ty = zcu.typeToUnion(ty).?; + const tag_ty: Type = .fromInterned(union_ty.enum_tag_ty); + + const layout = cg.unionLayout(ty); + const payload_ty: Type = .fromInterned(union_ty.field_types.get(ip)[active_field]); + + if (union_ty.flagsUnordered(ip).layout == .@"packed") { + if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + const int_ty = try pt.intType(.unsigned, @intCast(ty.bitSize(zcu))); + return cg.constInt(int_ty, 0); + } + + assert(payload != null); + if (payload_ty.isInt(zcu)) { + if (ty.bitSize(zcu) == payload_ty.bitSize(zcu)) { + return cg.bitCast(ty, payload_ty, payload.?); + } + + const trunc = try cg.buildConvert(ty, .{ .ty = payload_ty, .value = .{ .singleton = payload.? } }); + return try trunc.materialize(cg); + } + + const payload_int_ty = try pt.intType(.unsigned, @intCast(payload_ty.bitSize(zcu))); + const payload_int = if (payload_ty.ip_index == .bool_type) + try cg.convertToIndirect(payload_ty, payload.?) + else + try cg.bitCast(payload_int_ty, payload_ty, payload.?); + const trunc = try cg.buildConvert(ty, .{ .ty = payload_int_ty, .value = .{ .singleton = payload_int } }); + return try trunc.materialize(cg); + } + + const tag_int = if (layout.tag_size != 0) blk: { + const tag_val = try pt.enumValueFieldIndex(tag_ty, active_field); + const tag_int_val = try tag_val.intFromEnum(tag_ty, pt); + break :blk tag_int_val.toUnsignedInt(zcu); + } else 0; + + if (!layout.has_payload) { + return try cg.constInt(tag_ty, tag_int); + } + + const tmp_id = try cg.alloc(ty, .{ .storage_class = .function }); + + if (layout.tag_size != 0) { + const tag_ty_id = try cg.resolveType(tag_ty, .indirect); + const tag_ptr_ty_id = try cg.module.ptrType(tag_ty_id, .function); + const ptr_id = try cg.accessChain(tag_ptr_ty_id, tmp_id, &.{@as(u32, @intCast(layout.tag_index))}); + const tag_id = try cg.constInt(tag_ty, tag_int); + try cg.store(tag_ty, ptr_id, tag_id, .{}); + } + + if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); + const pl_ptr_ty_id = try cg.module.ptrType(layout_payload_ty_id, .function); + const pl_ptr_id = try cg.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); + const active_pl_ptr_id = if (!layout.payload_ty.eql(payload_ty, zcu)) blk: { + const payload_ty_id = try cg.resolveType(payload_ty, .indirect); + const active_pl_ptr_ty_id = try cg.module.ptrType(payload_ty_id, .function); + const active_pl_ptr_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpBitcast, .{ + .id_result_type = active_pl_ptr_ty_id, + .id_result = active_pl_ptr_id, + .operand = pl_ptr_id, + }); + break :blk active_pl_ptr_id; + } else pl_ptr_id; + + try cg.store(payload_ty, active_pl_ptr_id, payload.?, .{}); + } else { + assert(payload == null); + } + + // Just leave the padding fields uninitialized... + // TODO: Or should we initialize them with undef explicitly? + + return try cg.load(ty, tmp_id, .{}); +} + +fn airUnionInit(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ip = &zcu.intern_pool; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const extra = cg.air.extraData(Air.UnionInit, ty_pl.payload).data; + const ty = cg.typeOfIndex(inst); + + const union_obj = zcu.typeToUnion(ty).?; + const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[extra.field_index]); + const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(zcu)) + try cg.resolve(extra.init) + else + null; + return try cg.unionInit(ty, extra.field_index, payload); +} + +fn airStructFieldVal(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const pt = cg.pt; + const zcu = cg.module.zcu; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const struct_field = cg.air.extraData(Air.StructField, ty_pl.payload).data; + + const object_ty = cg.typeOf(struct_field.struct_operand); + const object_id = try cg.resolve(struct_field.struct_operand); + const field_index = struct_field.field_index; + const field_ty = object_ty.fieldType(field_index, zcu); + + if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) return null; + + switch (object_ty.zigTypeTag(zcu)) { + .@"struct" => switch (object_ty.containerLayout(zcu)) { + .@"packed" => { + const struct_ty = zcu.typeToPackedStruct(object_ty).?; + const struct_backing_int_bits = cg.module.backingIntBits(@intCast(object_ty.bitSize(zcu))).@"0"; + const bit_offset = zcu.structPackedFieldBitOffset(struct_ty, field_index); + // We use the same int type the packed struct is backed by, because even though it would + // be valid SPIR-V to use an smaller type like u16, some implementations like PoCL will complain. + const bit_offset_id = try cg.constInt(object_ty, bit_offset); + const signedness = if (field_ty.isInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned; + const field_bit_size: u16 = @intCast(field_ty.bitSize(zcu)); + const field_int_ty = try pt.intType(signedness, field_bit_size); + const shift_lhs: Temporary = .{ .ty = object_ty, .value = .{ .singleton = object_id } }; + const shift = try cg.buildBinary(.OpShiftRightLogical, shift_lhs, .{ .ty = object_ty, .value = .{ .singleton = bit_offset_id } }); + const mask_id = try cg.constInt(object_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1); + const masked = try cg.buildBinary(.OpBitwiseAnd, shift, .{ .ty = object_ty, .value = .{ .singleton = mask_id } }); + const result_id = blk: { + if (cg.module.backingIntBits(field_bit_size).@"0" == struct_backing_int_bits) + break :blk try cg.bitCast(field_int_ty, object_ty, try masked.materialize(cg)); + const trunc = try cg.buildConvert(field_int_ty, masked); + break :blk try trunc.materialize(cg); + }; + if (field_ty.ip_index == .bool_type) return try cg.convertToDirect(.bool, result_id); + if (field_ty.isInt(zcu)) return result_id; + return try cg.bitCast(field_ty, field_int_ty, result_id); + }, + else => return try cg.extractField(field_ty, object_id, field_index), + }, + .@"union" => switch (object_ty.containerLayout(zcu)) { + .@"packed" => { + const backing_int_ty = try pt.intType(.unsigned, @intCast(object_ty.bitSize(zcu))); + const signedness = if (field_ty.isInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned; + const field_bit_size: u16 = @intCast(field_ty.bitSize(zcu)); + const int_ty = try pt.intType(signedness, field_bit_size); + const mask_id = try cg.constInt(backing_int_ty, (@as(u64, 1) << @as(u6, @intCast(field_bit_size))) - 1); + const masked = try cg.buildBinary( + .OpBitwiseAnd, + .{ .ty = backing_int_ty, .value = .{ .singleton = object_id } }, + .{ .ty = backing_int_ty, .value = .{ .singleton = mask_id } }, + ); + const result_id = blk: { + if (cg.module.backingIntBits(field_bit_size).@"0" == cg.module.backingIntBits(@intCast(backing_int_ty.bitSize(zcu))).@"0") + break :blk try cg.bitCast(int_ty, backing_int_ty, try masked.materialize(cg)); + const trunc = try cg.buildConvert(int_ty, masked); + break :blk try trunc.materialize(cg); + }; + if (field_ty.ip_index == .bool_type) return try cg.convertToDirect(.bool, result_id); + if (field_ty.isInt(zcu)) return result_id; + return try cg.bitCast(field_ty, int_ty, result_id); + }, + else => { + // Store, ptr-elem-ptr, pointer-cast, load + const layout = cg.unionLayout(object_ty); + assert(layout.has_payload); + + const tmp_id = try cg.alloc(object_ty, .{ .storage_class = .function }); + try cg.store(object_ty, tmp_id, object_id, .{}); + + const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); + const pl_ptr_ty_id = try cg.module.ptrType(layout_payload_ty_id, .function); + const pl_ptr_id = try cg.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); + + const field_ty_id = try cg.resolveType(field_ty, .indirect); + const active_pl_ptr_ty_id = try cg.module.ptrType(field_ty_id, .function); + const active_pl_ptr_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpBitcast, .{ + .id_result_type = active_pl_ptr_ty_id, + .id_result = active_pl_ptr_id, + .operand = pl_ptr_id, + }); + return try cg.load(field_ty, active_pl_ptr_id, .{}); + }, + }, + else => unreachable, + } +} + +fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const extra = cg.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; + + const parent_ty = ty_pl.ty.toType().childType(zcu); + const result_ty_id = try cg.resolveType(ty_pl.ty.toType(), .indirect); + + const field_ptr = try cg.resolve(extra.field_ptr); + const field_ptr_int = try cg.intFromPtr(field_ptr); + const field_offset = parent_ty.structFieldOffset(extra.field_index, zcu); + + const base_ptr_int = base_ptr_int: { + if (field_offset == 0) break :base_ptr_int field_ptr_int; + + const field_offset_id = try cg.constInt(.usize, field_offset); + const field_ptr_tmp: Temporary = .init(.usize, field_ptr_int); + const field_offset_tmp: Temporary = .init(.usize, field_offset_id); + const result = try cg.buildBinary(.OpISub, field_ptr_tmp, field_offset_tmp); + break :base_ptr_int try result.materialize(cg); + }; + + const base_ptr = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpConvertUToPtr, .{ + .id_result_type = result_ty_id, + .id_result = base_ptr, + .integer_value = base_ptr_int, + }); + + return base_ptr; +} + +fn structFieldPtr( + cg: *CodeGen, + result_ptr_ty: Type, + object_ptr_ty: Type, + object_ptr: Id, + field_index: u32, +) !Id { + const result_ty_id = try cg.resolveType(result_ptr_ty, .direct); + + const zcu = cg.module.zcu; + const object_ty = object_ptr_ty.childType(zcu); + switch (object_ty.zigTypeTag(zcu)) { + .pointer => { + assert(object_ty.isSlice(zcu)); + return cg.accessChain(result_ty_id, object_ptr, &.{field_index}); + }, + .@"struct" => switch (object_ty.containerLayout(zcu)) { + .@"packed" => return cg.todo("implement field access for packed structs", .{}), + else => { + return try cg.accessChain(result_ty_id, object_ptr, &.{field_index}); + }, + }, + .@"union" => { + const layout = cg.unionLayout(object_ty); + if (!layout.has_payload) { + // Asked to get a pointer to a zero-sized field. Just lower this + // to undefined, there is no reason to make it be a valid pointer. + return try cg.module.constUndef(result_ty_id); + } + + const storage_class = cg.module.storageClass(object_ptr_ty.ptrAddressSpace(zcu)); + const layout_payload_ty_id = try cg.resolveType(layout.payload_ty, .indirect); + const pl_ptr_ty_id = try cg.module.ptrType(layout_payload_ty_id, storage_class); + const pl_ptr_id = blk: { + if (object_ty.containerLayout(zcu) == .@"packed") break :blk object_ptr; + break :blk try cg.accessChain(pl_ptr_ty_id, object_ptr, &.{layout.payload_index}); + }; + + const active_pl_ptr_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpBitcast, .{ + .id_result_type = result_ty_id, + .id_result = active_pl_ptr_id, + .operand = pl_ptr_id, + }); + return active_pl_ptr_id; + }, + else => unreachable, + } +} + +fn airStructFieldPtrIndex(cg: *CodeGen, inst: Air.Inst.Index, field_index: u32) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const struct_ptr = try cg.resolve(ty_op.operand); + const struct_ptr_ty = cg.typeOf(ty_op.operand); + const result_ptr_ty = cg.typeOfIndex(inst); + return try cg.structFieldPtr(result_ptr_ty, struct_ptr_ty, struct_ptr, field_index); +} + +const AllocOptions = struct { + initializer: ?Id = null, + /// The final storage class of the pointer. This may be either `.Generic` or `.Function`. + /// In either case, the local is allocated in the `.Function` storage class, and optionally + /// cast back to `.Generic`. + storage_class: StorageClass, +}; + +// Allocate a function-local variable, with possible initializer. +// This function returns a pointer to a variable of type `ty`, +// which is in the Generic address space. The variable is actually +// placed in the Function address space. +fn alloc( + cg: *CodeGen, + ty: Type, + options: AllocOptions, +) !Id { + const ty_id = try cg.resolveType(ty, .indirect); + const ptr_fn_ty_id = try cg.module.ptrType(ty_id, .function); + + // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to + // directly generate them into func.prologue instead of the body. + const var_id = cg.module.allocId(); + try cg.prologue.emit(cg.module.gpa, .OpVariable, .{ + .id_result_type = ptr_fn_ty_id, + .id_result = var_id, + .storage_class = .function, + .initializer = options.initializer, + }); + + return var_id; +} + +fn airAlloc(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ptr_ty = cg.typeOfIndex(inst); + const child_ty = ptr_ty.childType(zcu); + return try cg.alloc(child_ty, .{ + .storage_class = cg.module.storageClass(ptr_ty.ptrAddressSpace(zcu)), + }); +} + +fn airArg(cg: *CodeGen) Id { + defer cg.next_arg_index += 1; + return cg.args.items[cg.next_arg_index]; +} + +/// Given a slice of incoming block connections, returns the block-id of the next +/// block to jump to. This function emits instructions, so it should be emitted +/// inside the merge block of the block. +/// This function should only be called with structured control flow generation. +fn structuredNextBlock(cg: *CodeGen, incoming: []const ControlFlow.Structured.Block.Incoming) !Id { + assert(cg.control_flow == .structured); + + const result_id = cg.module.allocId(); + const block_id_ty_id = try cg.resolveType(.u32, .direct); + try cg.body.emitRaw(cg.module.gpa, .OpPhi, @intCast(2 + incoming.len * 2)); // result type + result + variable/parent... + cg.body.writeOperand(Id, block_id_ty_id); + cg.body.writeOperand(Id, result_id); + + for (incoming) |incoming_block| { + cg.body.writeOperand(spec.PairIdRefIdRef, .{ incoming_block.next_block, incoming_block.src_label }); + } + + return result_id; +} + +/// Jumps to the block with the target block-id. This function must only be called when +/// terminating a body, there should be no instructions after it. +/// This function should only be called with structured control flow generation. +fn structuredBreak(cg: *CodeGen, target_block: Id) !void { + assert(cg.control_flow == .structured); + + const gpa = cg.module.gpa; + const sblock = cg.control_flow.structured.block_stack.getLast(); + const merge_block = switch (sblock.*) { + .selection => |*merge| blk: { + const merge_label = cg.module.allocId(); + try merge.merge_stack.append(gpa, .{ + .incoming = .{ + .src_label = cg.block_label, + .next_block = target_block, + }, + .merge_block = merge_label, + }); + break :blk merge_label; + }, + // Loop blocks do not end in a break. Not through a direct break, + // and also not through another instruction like cond_br or unreachable (these + // situations are replaced by `cond_br` in sema, or there is a `block` instruction + // placed around them). + .loop => unreachable, + }; + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = merge_block }); +} + +/// Generate a body in a way that exits the body using only structured constructs. +/// Returns the block-id of the next block to jump to. After this function, a jump +/// should still be emitted to the block that should follow this structured body. +/// This function should only be called with structured control flow generation. +fn genStructuredBody( + cg: *CodeGen, + /// This parameter defines the method that this structured body is exited with. + block_merge_type: union(enum) { + /// Using selection; early exits from this body are surrounded with + /// if() statements. + selection, + /// Using loops; loops can be early exited by jumping to the merge block at + /// any time. + loop: struct { + merge_label: Id, + continue_label: Id, + }, + }, + body: []const Air.Inst.Index, +) !Id { + assert(cg.control_flow == .structured); + + const gpa = cg.module.gpa; + + var sblock: ControlFlow.Structured.Block = switch (block_merge_type) { + .loop => |merge| .{ .loop = .{ + .merge_block = merge.merge_label, + } }, + .selection => .{ .selection = .{} }, + }; + defer sblock.deinit(gpa); + + { + try cg.control_flow.structured.block_stack.append(gpa, &sblock); + defer _ = cg.control_flow.structured.block_stack.pop(); + + try cg.genBody(body); + } + + switch (sblock) { + .selection => |merge| { + // Now generate the merge block for all merges that + // still need to be performed. + const merge_stack = merge.merge_stack.items; + + // If no merges on the stack, this block didn't generate any jumps (all paths + // ended with a return or an unreachable). In that case, we don't need to do + // any merging. + if (merge_stack.len == 0) { + // We still need to return a value of a next block to jump to. + // For example, if we have code like + // if (x) { + // if (y) return else return; + // } else {} + // then we still need the outer to have an OpSelectionMerge and consequently + // a phi node. In that case we can just return bogus, since we know that its + // path will never be taken. + + // Make sure that we are still in a block when exiting the function. + // TODO: Can we get rid of that? + try cg.beginSpvBlock(cg.module.allocId()); + const block_id_ty_id = try cg.resolveType(.u32, .direct); + return try cg.module.constUndef(block_id_ty_id); + } + + // The top-most merge actually only has a single source, the + // final jump of the block, or the merge block of a sub-block, cond_br, + // or loop. Therefore we just need to generate a block with a jump to the + // next merge block. + try cg.beginSpvBlock(merge_stack[merge_stack.len - 1].merge_block); + + // Now generate a merge ladder for the remaining merges in the stack. + var incoming: ControlFlow.Structured.Block.Incoming = .{ + .src_label = cg.block_label, + .next_block = merge_stack[merge_stack.len - 1].incoming.next_block, + }; + var i = merge_stack.len - 1; + while (i > 0) { + i -= 1; + const step = merge_stack[i]; + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = step.merge_block }); + try cg.beginSpvBlock(step.merge_block); + const next_block = try cg.structuredNextBlock(&.{ incoming, step.incoming }); + incoming = .{ + .src_label = step.merge_block, + .next_block = next_block, + }; + } + + return incoming.next_block; + }, + .loop => |merge| { + // Close the loop by jumping to the continue label + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = block_merge_type.loop.continue_label }); + // For blocks we must simple merge all the incoming blocks to get the next block. + try cg.beginSpvBlock(merge.merge_block); + return try cg.structuredNextBlock(merge.merges.items); + }, + } +} + +fn airBlock(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const inst_datas = cg.air.instructions.items(.data); + const extra = cg.air.extraData(Air.Block, inst_datas[@intFromEnum(inst)].ty_pl.payload); + return cg.lowerBlock(inst, @ptrCast(cg.air.extra.items[extra.end..][0..extra.data.body_len])); +} + +fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, body: []const Air.Inst.Index) !?Id { + // In AIR, a block doesn't really define an entry point like a block, but + // more like a scope that breaks can jump out of and "return" a value from. + // This cannot be directly modelled in SPIR-V, so in a block instruction, + // we're going to split up the current block by first generating the code + // of the block, then a label, and then generate the rest of the current + // ir.Block in a different SPIR-V block. + + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const ty = cg.typeOfIndex(inst); + const have_block_result = ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu); + + const cf = switch (cg.control_flow) { + .structured => |*cf| cf, + .unstructured => |*cf| { + var block: ControlFlow.Unstructured.Block = .{}; + defer block.incoming_blocks.deinit(gpa); + + // 4 chosen as arbitrary initial capacity. + try block.incoming_blocks.ensureUnusedCapacity(gpa, 4); + + try cf.blocks.putNoClobber(gpa, inst, &block); + defer assert(cf.blocks.remove(inst)); + + try cg.genBody(body); + + // Only begin a new block if there were actually any breaks towards it. + if (block.label) |label| { + try cg.beginSpvBlock(label); + } + + if (!have_block_result) + return null; + + assert(block.label != null); + const result_id = cg.module.allocId(); + const result_type_id = try cg.resolveType(ty, .direct); + + try cg.body.emitRaw( + gpa, + .OpPhi, + // result type + result + variable/parent... + 2 + @as(u16, @intCast(block.incoming_blocks.items.len * 2)), + ); + cg.body.writeOperand(Id, result_type_id); + cg.body.writeOperand(Id, result_id); + + for (block.incoming_blocks.items) |incoming| { + cg.body.writeOperand( + spec.PairIdRefIdRef, + .{ incoming.break_value_id, incoming.src_label }, + ); + } + + return result_id; + }, + }; + + const maybe_block_result_var_id = if (have_block_result) blk: { + const block_result_var_id = try cg.alloc(ty, .{ .storage_class = .function }); + try cf.block_results.putNoClobber(gpa, inst, block_result_var_id); + break :blk block_result_var_id; + } else null; + defer if (have_block_result) assert(cf.block_results.remove(inst)); + + const next_block = try cg.genStructuredBody(.selection, body); + + // When encountering a block instruction, we are always at least in the function's scope, + // so there always has to be another entry. + assert(cf.block_stack.items.len > 0); + + // Check if the target of the branch was this current block. + const this_block = try cg.constInt(.u32, @intFromEnum(inst)); + const jump_to_this_block_id = cg.module.allocId(); + const bool_ty_id = try cg.resolveType(.bool, .direct); + try cg.body.emit(gpa, .OpIEqual, .{ + .id_result_type = bool_ty_id, + .id_result = jump_to_this_block_id, + .operand_1 = next_block, + .operand_2 = this_block, + }); + + const sblock = cf.block_stack.getLast(); + + if (ty.isNoReturn(zcu)) { + // If this block is noreturn, this instruction is the last of a block, + // and we must simply jump to the block's merge unconditionally. + try cg.structuredBreak(next_block); + } else { + switch (sblock.*) { + .selection => |*merge| { + // To jump out of a selection block, push a new entry onto its merge stack and + // generate a conditional branch to there and to the instructions following this block. + const merge_label = cg.module.allocId(); + const then_label = cg.module.allocId(); + try cg.body.emit(gpa, .OpSelectionMerge, .{ + .merge_block = merge_label, + .selection_control = .{}, + }); + try cg.body.emit(gpa, .OpBranchConditional, .{ + .condition = jump_to_this_block_id, + .true_label = then_label, + .false_label = merge_label, + }); + try merge.merge_stack.append(gpa, .{ + .incoming = .{ + .src_label = cg.block_label, + .next_block = next_block, + }, + .merge_block = merge_label, + }); + + try cg.beginSpvBlock(then_label); + }, + .loop => |*merge| { + // To jump out of a loop block, generate a conditional that exits the block + // to the loop merge if the target ID is not the one of this block. + const continue_label = cg.module.allocId(); + try cg.body.emit(gpa, .OpBranchConditional, .{ + .condition = jump_to_this_block_id, + .true_label = continue_label, + .false_label = merge.merge_block, + }); + try merge.merges.append(gpa, .{ + .src_label = cg.block_label, + .next_block = next_block, + }); + try cg.beginSpvBlock(continue_label); + }, + } + } + + if (maybe_block_result_var_id) |block_result_var_id| { + return try cg.load(ty, block_result_var_id, .{}); + } + + return null; +} + +fn airBr(cg: *CodeGen, inst: Air.Inst.Index) !void { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const br = cg.air.instructions.items(.data)[@intFromEnum(inst)].br; + const operand_ty = cg.typeOf(br.operand); + + switch (cg.control_flow) { + .structured => |*cf| { + if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { + const operand_id = try cg.resolve(br.operand); + const block_result_var_id = cf.block_results.get(br.block_inst).?; + try cg.store(operand_ty, block_result_var_id, operand_id, .{}); + } + + const next_block = try cg.constInt(.u32, @intFromEnum(br.block_inst)); + try cg.structuredBreak(next_block); + }, + .unstructured => |cf| { + const block = cf.blocks.get(br.block_inst).?; + if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { + const operand_id = try cg.resolve(br.operand); + // block_label should not be undefined here, lest there + // is a br or br_void in the function's body. + try block.incoming_blocks.append(gpa, .{ + .src_label = cg.block_label, + .break_value_id = operand_id, + }); + } + + if (block.label == null) { + block.label = cg.module.allocId(); + } + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = block.label.? }); + }, + } +} + +fn airCondBr(cg: *CodeGen, inst: Air.Inst.Index) !void { + const gpa = cg.module.gpa; + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const cond_br = cg.air.extraData(Air.CondBr, pl_op.payload); + const then_body: []const Air.Inst.Index = @ptrCast(cg.air.extra.items[cond_br.end..][0..cond_br.data.then_body_len]); + const else_body: []const Air.Inst.Index = @ptrCast(cg.air.extra.items[cond_br.end + then_body.len ..][0..cond_br.data.else_body_len]); + const condition_id = try cg.resolve(pl_op.operand); + + const then_label = cg.module.allocId(); + const else_label = cg.module.allocId(); + + switch (cg.control_flow) { + .structured => { + const merge_label = cg.module.allocId(); + + try cg.body.emit(gpa, .OpSelectionMerge, .{ + .merge_block = merge_label, + .selection_control = .{}, + }); + try cg.body.emit(gpa, .OpBranchConditional, .{ + .condition = condition_id, + .true_label = then_label, + .false_label = else_label, + }); + + try cg.beginSpvBlock(then_label); + const then_next = try cg.genStructuredBody(.selection, then_body); + const then_incoming: ControlFlow.Structured.Block.Incoming = .{ + .src_label = cg.block_label, + .next_block = then_next, + }; + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = merge_label }); + + try cg.beginSpvBlock(else_label); + const else_next = try cg.genStructuredBody(.selection, else_body); + const else_incoming: ControlFlow.Structured.Block.Incoming = .{ + .src_label = cg.block_label, + .next_block = else_next, + }; + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = merge_label }); + + try cg.beginSpvBlock(merge_label); + const next_block = try cg.structuredNextBlock(&.{ then_incoming, else_incoming }); + + try cg.structuredBreak(next_block); + }, + .unstructured => { + try cg.body.emit(gpa, .OpBranchConditional, .{ + .condition = condition_id, + .true_label = then_label, + .false_label = else_label, + }); + + try cg.beginSpvBlock(then_label); + try cg.genBody(then_body); + try cg.beginSpvBlock(else_label); + try cg.genBody(else_body); + }, + } +} + +fn airLoop(cg: *CodeGen, inst: Air.Inst.Index) !void { + const gpa = cg.module.gpa; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const loop = cg.air.extraData(Air.Block, ty_pl.payload); + const body: []const Air.Inst.Index = @ptrCast(cg.air.extra.items[loop.end..][0..loop.data.body_len]); + + const body_label = cg.module.allocId(); + + switch (cg.control_flow) { + .structured => { + const header_label = cg.module.allocId(); + const merge_label = cg.module.allocId(); + const continue_label = cg.module.allocId(); + + // The back-edge must point to the loop header, so generate a separate block for the + // loop header so that we don't accidentally include some instructions from there + // in the loop. + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = header_label }); + try cg.beginSpvBlock(header_label); + + // Emit loop header and jump to loop body + try cg.body.emit(gpa, .OpLoopMerge, .{ + .merge_block = merge_label, + .continue_target = continue_label, + .loop_control = .{}, + }); + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = body_label }); + + try cg.beginSpvBlock(body_label); + + const next_block = try cg.genStructuredBody(.{ .loop = .{ + .merge_label = merge_label, + .continue_label = continue_label, + } }, body); + try cg.structuredBreak(next_block); + + try cg.beginSpvBlock(continue_label); + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = header_label }); + }, + .unstructured => { + try cg.body.emit(gpa, .OpBranch, .{ .target_label = body_label }); + try cg.beginSpvBlock(body_label); + try cg.genBody(body); + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = body_label }); + }, + } +} + +fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const ptr_ty = cg.typeOf(ty_op.operand); + const elem_ty = cg.typeOfIndex(inst); + const operand = try cg.resolve(ty_op.operand); + if (!ptr_ty.isVolatilePtr(zcu) and cg.liveness.isUnused(inst)) return null; + + return try cg.load(elem_ty, operand, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); +} + +fn airStore(cg: *CodeGen, inst: Air.Inst.Index) !void { + const zcu = cg.module.zcu; + const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + const ptr_ty = cg.typeOf(bin_op.lhs); + const elem_ty = ptr_ty.childType(zcu); + const ptr = try cg.resolve(bin_op.lhs); + const value = try cg.resolve(bin_op.rhs); + + try cg.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); +} + +fn airRet(cg: *CodeGen, inst: Air.Inst.Index) !void { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const operand = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; + const ret_ty = cg.typeOf(operand); + if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + const fn_info = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?; + if (Type.fromInterned(fn_info.return_type).isError(zcu)) { + // Functions with an empty error set are emitted with an error code + // return type and return zero so they can be function pointers coerced + // to functions that return anyerror. + const no_err_id = try cg.constInt(.anyerror, 0); + return try cg.body.emit(gpa, .OpReturnValue, .{ .value = no_err_id }); + } else { + return try cg.body.emit(gpa, .OpReturn, {}); + } + } + + const operand_id = try cg.resolve(operand); + try cg.body.emit(gpa, .OpReturnValue, .{ .value = operand_id }); +} + +fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) !void { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; + const ptr_ty = cg.typeOf(un_op); + const ret_ty = ptr_ty.childType(zcu); + + if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + const fn_info = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?; + if (Type.fromInterned(fn_info.return_type).isError(zcu)) { + // Functions with an empty error set are emitted with an error code + // return type and return zero so they can be function pointers coerced + // to functions that return anyerror. + const no_err_id = try cg.constInt(.anyerror, 0); + return try cg.body.emit(gpa, .OpReturnValue, .{ .value = no_err_id }); + } else { + return try cg.body.emit(gpa, .OpReturn, {}); + } + } + + const ptr = try cg.resolve(un_op); + const value = try cg.load(ret_ty, ptr, .{ .is_volatile = ptr_ty.isVolatilePtr(zcu) }); + try cg.body.emit(gpa, .OpReturnValue, .{ + .value = value, + }); +} + +fn airTry(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const err_union_id = try cg.resolve(pl_op.operand); + const extra = cg.air.extraData(Air.Try, pl_op.payload); + const body: []const Air.Inst.Index = @ptrCast(cg.air.extra.items[extra.end..][0..extra.data.body_len]); + + const err_union_ty = cg.typeOf(pl_op.operand); + const payload_ty = cg.typeOfIndex(inst); + + const bool_ty_id = try cg.resolveType(.bool, .direct); + + const eu_layout = cg.errorUnionLayout(payload_ty); + + if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { + const err_id = if (eu_layout.payload_has_bits) + try cg.extractField(.anyerror, err_union_id, eu_layout.errorFieldIndex()) + else + err_union_id; + + const zero_id = try cg.constInt(.anyerror, 0); + const is_err_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpINotEqual, .{ + .id_result_type = bool_ty_id, + .id_result = is_err_id, + .operand_1 = err_id, + .operand_2 = zero_id, + }); + + // When there is an error, we must evaluate `body`. Otherwise we must continue + // with the current body. + // Just generate a new block here, then generate a new block inline for the remainder of the body. + + const err_block = cg.module.allocId(); + const ok_block = cg.module.allocId(); + + switch (cg.control_flow) { + .structured => { + // According to AIR documentation, this block is guaranteed + // to not break and end in a return instruction. Thus, + // for structured control flow, we can just naively use + // the ok block as the merge block here. + try cg.body.emit(gpa, .OpSelectionMerge, .{ + .merge_block = ok_block, + .selection_control = .{}, + }); + }, + .unstructured => {}, + } + + try cg.body.emit(gpa, .OpBranchConditional, .{ + .condition = is_err_id, + .true_label = err_block, + .false_label = ok_block, + }); + + try cg.beginSpvBlock(err_block); + try cg.genBody(body); + + try cg.beginSpvBlock(ok_block); + } + + if (!eu_layout.payload_has_bits) { + return null; + } + + // Now just extract the payload, if required. + return try cg.extractField(payload_ty, err_union_id, eu_layout.payloadFieldIndex()); +} + +fn airErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_id = try cg.resolve(ty_op.operand); + const err_union_ty = cg.typeOf(ty_op.operand); + const err_ty_id = try cg.resolveType(.anyerror, .direct); + + if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { + // No error possible, so just return undefined. + return try cg.module.constUndef(err_ty_id); + } + + const payload_ty = err_union_ty.errorUnionPayload(zcu); + const eu_layout = cg.errorUnionLayout(payload_ty); + + if (!eu_layout.payload_has_bits) { + // If no payload, error union is represented by error set. + return operand_id; + } + + return try cg.extractField(.anyerror, operand_id, eu_layout.errorFieldIndex()); +} + +fn airErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_id = try cg.resolve(ty_op.operand); + const payload_ty = cg.typeOfIndex(inst); + const eu_layout = cg.errorUnionLayout(payload_ty); + + if (!eu_layout.payload_has_bits) { + return null; // No error possible. + } + + return try cg.extractField(payload_ty, operand_id, eu_layout.payloadFieldIndex()); +} + +fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const err_union_ty = cg.typeOfIndex(inst); + const payload_ty = err_union_ty.errorUnionPayload(zcu); + const operand_id = try cg.resolve(ty_op.operand); + const eu_layout = cg.errorUnionLayout(payload_ty); + + if (!eu_layout.payload_has_bits) { + return operand_id; + } + + const payload_ty_id = try cg.resolveType(payload_ty, .indirect); + + var members: [2]Id = undefined; + members[eu_layout.errorFieldIndex()] = operand_id; + members[eu_layout.payloadFieldIndex()] = try cg.module.constUndef(payload_ty_id); + + var types: [2]Type = undefined; + types[eu_layout.errorFieldIndex()] = .anyerror; + types[eu_layout.payloadFieldIndex()] = payload_ty; + + const err_union_ty_id = try cg.resolveType(err_union_ty, .direct); + return try cg.constructComposite(err_union_ty_id, &members); +} + +fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const err_union_ty = cg.typeOfIndex(inst); + const operand_id = try cg.resolve(ty_op.operand); + const payload_ty = cg.typeOf(ty_op.operand); + const eu_layout = cg.errorUnionLayout(payload_ty); + + if (!eu_layout.payload_has_bits) { + return try cg.constInt(.anyerror, 0); + } + + var members: [2]Id = undefined; + members[eu_layout.errorFieldIndex()] = try cg.constInt(.anyerror, 0); + members[eu_layout.payloadFieldIndex()] = try cg.convertToIndirect(payload_ty, operand_id); + + var types: [2]Type = undefined; + types[eu_layout.errorFieldIndex()] = .anyerror; + types[eu_layout.payloadFieldIndex()] = payload_ty; + + const err_union_ty_id = try cg.resolveType(err_union_ty, .direct); + return try cg.constructComposite(err_union_ty_id, &members); +} + +fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { is_null, is_non_null }) !?Id { + const zcu = cg.module.zcu; + const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; + const operand_id = try cg.resolve(un_op); + const operand_ty = cg.typeOf(un_op); + const optional_ty = if (is_pointer) operand_ty.childType(zcu) else operand_ty; + const payload_ty = optional_ty.optionalChild(zcu); + + const bool_ty_id = try cg.resolveType(.bool, .direct); + + if (optional_ty.optionalReprIsPayload(zcu)) { + // Pointer payload represents nullability: pointer or slice. + const loaded_id = if (is_pointer) + try cg.load(optional_ty, operand_id, .{}) + else + operand_id; + + const ptr_ty = if (payload_ty.isSlice(zcu)) + payload_ty.slicePtrFieldType(zcu) + else + payload_ty; + + const ptr_id = if (payload_ty.isSlice(zcu)) + try cg.extractField(ptr_ty, loaded_id, 0) + else + loaded_id; + + const ptr_ty_id = try cg.resolveType(ptr_ty, .direct); + const null_id = try cg.module.constNull(ptr_ty_id); + const null_tmp: Temporary = .init(ptr_ty, null_id); + const ptr: Temporary = .init(ptr_ty, ptr_id); + + const op: std.math.CompareOperator = switch (pred) { + .is_null => .eq, + .is_non_null => .neq, + }; + const result = try cg.cmp(op, ptr, null_tmp); + return try result.materialize(cg); + } + + const is_non_null_id = blk: { + if (is_pointer) { + if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + const storage_class = cg.module.storageClass(operand_ty.ptrAddressSpace(zcu)); + const bool_indirect_ty_id = try cg.resolveType(.bool, .indirect); + const bool_ptr_ty_id = try cg.module.ptrType(bool_indirect_ty_id, storage_class); + const tag_ptr_id = try cg.accessChain(bool_ptr_ty_id, operand_id, &.{1}); + break :blk try cg.load(.bool, tag_ptr_id, .{}); + } + + break :blk try cg.load(.bool, operand_id, .{}); + } + + break :blk if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) + try cg.extractField(.bool, operand_id, 1) + else + // Optional representation is bool indicating whether the optional is set + // Optionals with no payload are represented as an (indirect) bool, so convert + // it back to the direct bool here. + try cg.convertToDirect(.bool, operand_id); + }; + + return switch (pred) { + .is_null => blk: { + // Invert condition + const result_id = cg.module.allocId(); + try cg.body.emit(cg.module.gpa, .OpLogicalNot, .{ + .id_result_type = bool_ty_id, + .id_result = result_id, + .operand = is_non_null_id, + }); + break :blk result_id; + }, + .is_non_null => is_non_null_id, + }; +} + +fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err }) !?Id { + const zcu = cg.module.zcu; + const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op; + const operand_id = try cg.resolve(un_op); + const err_union_ty = cg.typeOf(un_op); + + if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { + return try cg.constBool(pred == .is_non_err, .direct); + } + + const payload_ty = err_union_ty.errorUnionPayload(zcu); + const eu_layout = cg.errorUnionLayout(payload_ty); + const bool_ty_id = try cg.resolveType(.bool, .direct); + + const error_id = if (!eu_layout.payload_has_bits) + operand_id + else + try cg.extractField(.anyerror, operand_id, eu_layout.errorFieldIndex()); + + const result_id = cg.module.allocId(); + switch (pred) { + inline else => |pred_ct| try cg.body.emit( + cg.module.gpa, + switch (pred_ct) { + .is_err => .OpINotEqual, + .is_non_err => .OpIEqual, + }, + .{ + .id_result_type = bool_ty_id, + .id_result = result_id, + .operand_1 = error_id, + .operand_2 = try cg.constInt(.anyerror, 0), + }, + ), + } + return result_id; +} + +fn airUnwrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_id = try cg.resolve(ty_op.operand); + const optional_ty = cg.typeOf(ty_op.operand); + const payload_ty = cg.typeOfIndex(inst); + + if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) return null; + + if (optional_ty.optionalReprIsPayload(zcu)) { + return operand_id; + } + + return try cg.extractField(payload_ty, operand_id, 0); +} + +fn airUnwrapOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const operand_id = try cg.resolve(ty_op.operand); + const operand_ty = cg.typeOf(ty_op.operand); + const optional_ty = operand_ty.childType(zcu); + const payload_ty = optional_ty.optionalChild(zcu); + const result_ty = cg.typeOfIndex(inst); + const result_ty_id = try cg.resolveType(result_ty, .direct); + + if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + // There is no payload, but we still need to return a valid pointer. + // We can just return anything here, so just return a pointer to the operand. + return try cg.bitCast(result_ty, operand_ty, operand_id); + } + + if (optional_ty.optionalReprIsPayload(zcu)) { + // They are the same value. + return try cg.bitCast(result_ty, operand_ty, operand_id); + } + + return try cg.accessChain(result_ty_id, operand_id, &.{0}); +} + +fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; + const payload_ty = cg.typeOf(ty_op.operand); + + if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { + return try cg.constBool(true, .indirect); + } + + const operand_id = try cg.resolve(ty_op.operand); + + const optional_ty = cg.typeOfIndex(inst); + if (optional_ty.optionalReprIsPayload(zcu)) { + return operand_id; + } + + const payload_id = try cg.convertToIndirect(payload_ty, operand_id); + const members = [_]Id{ payload_id, try cg.constBool(true, .indirect) }; + const optional_ty_id = try cg.resolveType(optional_ty, .direct); + return try cg.constructComposite(optional_ty_id, &members); +} + +fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void { + const gpa = cg.module.gpa; + const pt = cg.pt; + const zcu = cg.module.zcu; + const target = cg.module.zcu.getTarget(); + const switch_br = cg.air.unwrapSwitch(inst); + const cond_ty = cg.typeOf(switch_br.operand); + const cond = try cg.resolve(switch_br.operand); + var cond_indirect = try cg.convertToIndirect(cond_ty, cond); + + const cond_words: u32 = switch (cond_ty.zigTypeTag(zcu)) { + .bool, .error_set => 1, + .int => blk: { + const bits = cond_ty.intInfo(zcu).bits; + const backing_bits, const big_int = cg.module.backingIntBits(bits); + if (big_int) return cg.todo("implement composite int switch", .{}); + break :blk if (backing_bits <= 32) 1 else 2; + }, + .@"enum" => blk: { + const int_ty = cond_ty.intTagType(zcu); + const int_info = int_ty.intInfo(zcu); + const backing_bits, const big_int = cg.module.backingIntBits(int_info.bits); + if (big_int) return cg.todo("implement composite int switch", .{}); + break :blk if (backing_bits <= 32) 1 else 2; + }, + .pointer => blk: { + cond_indirect = try cg.intFromPtr(cond_indirect); + break :blk target.ptrBitWidth() / 32; + }, + // TODO: Figure out which types apply here, and work around them as we can only do integers. + else => return cg.todo("implement switch for type {s}", .{@tagName(cond_ty.zigTypeTag(zcu))}), + }; + + const num_cases = switch_br.cases_len; + + // Compute the total number of arms that we need. + // Zig switches are grouped by condition, so we need to loop through all of them + const num_conditions = blk: { + var num_conditions: u32 = 0; + var it = switch_br.iterateCases(); + while (it.next()) |case| { + if (case.ranges.len > 0) return cg.todo("switch with ranges", .{}); + num_conditions += @intCast(case.items.len); + } + break :blk num_conditions; + }; + + // First, pre-allocate the labels for the cases. + const case_labels = cg.module.allocIds(num_cases); + // We always need the default case - if zig has none, we will generate unreachable there. + const default = cg.module.allocId(); + + const merge_label = switch (cg.control_flow) { + .structured => cg.module.allocId(), + .unstructured => null, + }; + + if (cg.control_flow == .structured) { + try cg.body.emit(gpa, .OpSelectionMerge, .{ + .merge_block = merge_label.?, + .selection_control = .{}, + }); + } + + // Emit the instruction before generating the blocks. + try cg.body.emitRaw(gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions); + cg.body.writeOperand(Id, cond_indirect); + cg.body.writeOperand(Id, default); + + // Emit each of the cases + { + var it = switch_br.iterateCases(); + while (it.next()) |case| { + // SPIR-V needs a literal here, which' width depends on the case condition. + const label = case_labels.at(case.idx); + + for (case.items) |item| { + const value = (try cg.air.value(item, pt)) orelse unreachable; + const int_val: u64 = switch (cond_ty.zigTypeTag(zcu)) { + .bool, .int => if (cond_ty.isSignedInt(zcu)) @bitCast(value.toSignedInt(zcu)) else value.toUnsignedInt(zcu), + .@"enum" => blk: { + // TODO: figure out of cond_ty is correct (something with enum literals) + break :blk (try value.intFromEnum(cond_ty, pt)).toUnsignedInt(zcu); // TODO: composite integer constants + }, + .error_set => value.getErrorInt(zcu), + .pointer => value.toUnsignedInt(zcu), + else => unreachable, + }; + const int_lit: spec.LiteralContextDependentNumber = switch (cond_words) { + 1 => .{ .uint32 = @intCast(int_val) }, + 2 => .{ .uint64 = int_val }, + else => unreachable, + }; + cg.body.writeOperand(spec.LiteralContextDependentNumber, int_lit); + cg.body.writeOperand(Id, label); + } + } + } + + var incoming_structured_blocks: std.ArrayListUnmanaged(ControlFlow.Structured.Block.Incoming) = .empty; + defer incoming_structured_blocks.deinit(gpa); + + if (cg.control_flow == .structured) { + try incoming_structured_blocks.ensureUnusedCapacity(gpa, num_cases + 1); + } + + // Now, finally, we can start emitting each of the cases. + var it = switch_br.iterateCases(); + while (it.next()) |case| { + const label = case_labels.at(case.idx); + + try cg.beginSpvBlock(label); + + switch (cg.control_flow) { + .structured => { + const next_block = try cg.genStructuredBody(.selection, case.body); + incoming_structured_blocks.appendAssumeCapacity(.{ + .src_label = cg.block_label, + .next_block = next_block, + }); + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = merge_label.? }); + }, + .unstructured => { + try cg.genBody(case.body); + }, + } + } + + const else_body = it.elseBody(); + try cg.beginSpvBlock(default); + if (else_body.len != 0) { + switch (cg.control_flow) { + .structured => { + const next_block = try cg.genStructuredBody(.selection, else_body); + incoming_structured_blocks.appendAssumeCapacity(.{ + .src_label = cg.block_label, + .next_block = next_block, + }); + + try cg.body.emit(gpa, .OpBranch, .{ .target_label = merge_label.? }); + }, + .unstructured => { + try cg.genBody(else_body); + }, + } + } else { + try cg.body.emit(gpa, .OpUnreachable, {}); + } + + if (cg.control_flow == .structured) { + try cg.beginSpvBlock(merge_label.?); + const next_block = try cg.structuredNextBlock(incoming_structured_blocks.items); + try cg.structuredBreak(next_block); + } +} + +fn airUnreach(cg: *CodeGen) !void { + try cg.body.emit(cg.module.gpa, .OpUnreachable, {}); +} + +fn airDbgStmt(cg: *CodeGen, inst: Air.Inst.Index) !void { + const zcu = cg.module.zcu; + const dbg_stmt = cg.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; + const path = zcu.navFileScope(cg.owner_nav).sub_file_path; + + if (zcu.comp.config.root_strip) return; + + try cg.body.emit(cg.module.gpa, .OpLine, .{ + .file = try cg.module.debugString(path), + .line = cg.base_line + dbg_stmt.line + 1, + .column = dbg_stmt.column + 1, + }); +} + +fn airDbgInlineBlock(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const zcu = cg.module.zcu; + const inst_datas = cg.air.instructions.items(.data); + const extra = cg.air.extraData(Air.DbgInlineBlock, inst_datas[@intFromEnum(inst)].ty_pl.payload); + const old_base_line = cg.base_line; + defer cg.base_line = old_base_line; + cg.base_line = zcu.navSrcLine(zcu.funcInfo(extra.data.func).owner_nav); + return cg.lowerBlock(inst, @ptrCast(cg.air.extra.items[extra.end..][0..extra.data.body_len])); +} + +fn airDbgVar(cg: *CodeGen, inst: Air.Inst.Index) !void { + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const target_id = try cg.resolve(pl_op.operand); + const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); + try cg.module.debugName(target_id, name.toSlice(cg.air)); +} + +fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; + const extra = cg.air.extraData(Air.Asm, ty_pl.payload); + + const is_volatile = extra.data.flags.is_volatile; + const outputs_len = extra.data.flags.outputs_len; + + if (!is_volatile and cg.liveness.isUnused(inst)) return null; + + var extra_i: usize = extra.end; + const outputs: []const Air.Inst.Ref = @ptrCast(cg.air.extra.items[extra_i..][0..outputs_len]); + extra_i += outputs.len; + const inputs: []const Air.Inst.Ref = @ptrCast(cg.air.extra.items[extra_i..][0..extra.data.inputs_len]); + extra_i += inputs.len; + + if (outputs.len > 1) { + return cg.todo("implement inline asm with more than 1 output", .{}); + } + + var ass: Assembler = .{ .cg = cg }; + defer ass.deinit(); + + var output_extra_i = extra_i; + for (outputs) |output| { + if (output != .none) { + return cg.todo("implement inline asm with non-returned output", .{}); + } + const extra_bytes = std.mem.sliceAsBytes(cg.air.extra.items[extra_i..]); + const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(cg.air.extra.items[extra_i..]), 0); + const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); + extra_i += (constraint.len + name.len + (2 + 3)) / 4; + // TODO: Record output and use it somewhere. + } + + for (inputs) |input| { + const extra_bytes = std.mem.sliceAsBytes(cg.air.extra.items[extra_i..]); + const constraint = std.mem.sliceTo(extra_bytes, 0); + const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); + // This equation accounts for the fact that even if we have exactly 4 bytes + // for the string, we still use the next u32 for the null terminator. + extra_i += (constraint.len + name.len + (2 + 3)) / 4; + + const input_ty = cg.typeOf(input); + + if (std.mem.eql(u8, constraint, "c")) { + // constant + const val = (try cg.air.value(input, cg.pt)) orelse { + return cg.fail("assembly inputs with 'c' constraint have to be compile-time known", .{}); + }; + + // TODO: This entire function should be handled a bit better... + const ip = &zcu.intern_pool; + switch (ip.indexToKey(val.toIntern())) { + .int_type, + .ptr_type, + .array_type, + .vector_type, + .opt_type, + .anyframe_type, + .error_union_type, + .simple_type, + .struct_type, + .union_type, + .opaque_type, + .enum_type, + .func_type, + .error_set_type, + .inferred_error_set_type, + => unreachable, // types, not values + + .undef => return cg.fail("assembly input with 'c' constraint cannot be undefined", .{}), + + .int => try ass.value_map.put(gpa, name, .{ .constant = @intCast(val.toUnsignedInt(zcu)) }), + .enum_literal => |str| try ass.value_map.put(gpa, name, .{ .string = str.toSlice(ip) }), + + else => unreachable, // TODO + } + } else if (std.mem.eql(u8, constraint, "t")) { + // type + if (input_ty.zigTypeTag(zcu) == .type) { + // This assembly input is a type instead of a value. + // That's fine for now, just make sure to resolve it as such. + const val = (try cg.air.value(input, cg.pt)).?; + const ty_id = try cg.resolveType(val.toType(), .direct); + try ass.value_map.put(gpa, name, .{ .ty = ty_id }); + } else { + const ty_id = try cg.resolveType(input_ty, .direct); + try ass.value_map.put(gpa, name, .{ .ty = ty_id }); + } + } else { + if (input_ty.zigTypeTag(zcu) == .type) { + return cg.fail("use the 't' constraint to supply types to SPIR-V inline assembly", .{}); + } + + const val_id = try cg.resolve(input); + try ass.value_map.put(gpa, name, .{ .value = val_id }); + } + } + + // TODO: do something with clobbers + _ = extra.data.clobbers; + + const asm_source = std.mem.sliceAsBytes(cg.air.extra.items[extra_i..])[0..extra.data.source_len]; + + ass.assemble(asm_source) catch |err| switch (err) { + error.AssembleFail => { + // TODO: For now the compiler only supports a single error message per decl, + // so to translate the possible multiple errors from the assembler, emit + // them as notes here. + // TODO: Translate proper error locations. + assert(ass.errors.items.len != 0); + assert(cg.error_msg == null); + const src_loc = zcu.navSrcLoc(cg.owner_nav); + cg.error_msg = try Zcu.ErrorMsg.create(zcu.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{}); + const notes = try zcu.gpa.alloc(Zcu.ErrorMsg, ass.errors.items.len); + + // Sub-scope to prevent `return error.CodegenFail` from running the errdefers. + { + errdefer zcu.gpa.free(notes); + var i: usize = 0; + errdefer for (notes[0..i]) |*note| { + note.deinit(zcu.gpa); + }; + + while (i < ass.errors.items.len) : (i += 1) { + notes[i] = try Zcu.ErrorMsg.init(zcu.gpa, src_loc, "{s}", .{ass.errors.items[i].msg}); + } + } + cg.error_msg.?.notes = notes; + return error.CodegenFail; + }, + else => |others| return others, + }; + + for (outputs) |output| { + _ = output; + const extra_bytes = std.mem.sliceAsBytes(cg.air.extra.items[output_extra_i..]); + const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(cg.air.extra.items[output_extra_i..]), 0); + const name = std.mem.sliceTo(extra_bytes[constraint.len + 1 ..], 0); + output_extra_i += (constraint.len + name.len + (2 + 3)) / 4; + + const result = ass.value_map.get(name) orelse return { + return cg.fail("invalid asm output '{s}'", .{name}); + }; + + switch (result) { + .just_declared, .unresolved_forward_reference => unreachable, + .ty => return cg.fail("cannot return spir-v type as value from assembly", .{}), + .value => |ref| return ref, + .constant, .string => return cg.fail("cannot return constant from assembly", .{}), + } + + // TODO: Multiple results + // TODO: Check that the output type from assembly is the same as the type actually expected by Zig. + } + + return null; +} + +fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !?Id { + _ = modifier; + + const gpa = cg.module.gpa; + const zcu = cg.module.zcu; + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const extra = cg.air.extraData(Air.Call, pl_op.payload); + const args: []const Air.Inst.Ref = @ptrCast(cg.air.extra.items[extra.end..][0..extra.data.args_len]); + const callee_ty = cg.typeOf(pl_op.operand); + const zig_fn_ty = switch (callee_ty.zigTypeTag(zcu)) { + .@"fn" => callee_ty, + .pointer => return cg.fail("cannot call function pointers", .{}), + else => unreachable, + }; + const fn_info = zcu.typeToFunc(zig_fn_ty).?; + const return_type = fn_info.return_type; + + const result_type_id = try cg.resolveFnReturnType(.fromInterned(return_type)); + const result_id = cg.module.allocId(); + const callee_id = try cg.resolve(pl_op.operand); + + comptime assert(zig_call_abi_ver == 3); + + const scratch_top = cg.id_scratch.items.len; + defer cg.id_scratch.shrinkRetainingCapacity(scratch_top); + const params = try cg.id_scratch.addManyAsSlice(gpa, args.len); + + var n_params: usize = 0; + for (args) |arg| { + // Note: resolve() might emit instructions, so we need to call it + // before starting to emit OpFunctionCall instructions. Hence the + // temporary params buffer. + const arg_ty = cg.typeOf(arg); + if (!arg_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; + const arg_id = try cg.resolve(arg); + + params[n_params] = arg_id; + n_params += 1; + } + + try cg.body.emit(gpa, .OpFunctionCall, .{ + .id_result_type = result_type_id, + .id_result = result_id, + .function = callee_id, + .id_ref_3 = params[0..n_params], + }); + + if (cg.liveness.isUnused(inst) or !Type.fromInterned(return_type).hasRuntimeBitsIgnoreComptime(zcu)) { + return null; + } + + return result_id; +} + +fn builtin3D( + cg: *CodeGen, + result_ty: Type, + builtin: spec.BuiltIn, + dimension: u32, + out_of_range_value: anytype, +) !Id { + const gpa = cg.module.gpa; + if (dimension >= 3) return try cg.constInt(result_ty, out_of_range_value); + const u32_ty_id = try cg.module.intType(.unsigned, 32); + const vec_ty_id = try cg.module.vectorType(3, u32_ty_id); + const ptr_ty_id = try cg.module.ptrType(vec_ty_id, .input); + const spv_decl_index = try cg.module.builtin(ptr_ty_id, builtin, .input); + try cg.module.decl_deps.append(gpa, spv_decl_index); + const ptr_id = cg.module.declPtr(spv_decl_index).result_id; + const vec_id = cg.module.allocId(); + try cg.body.emit(gpa, .OpLoad, .{ + .id_result_type = vec_ty_id, + .id_result = vec_id, + .pointer = ptr_id, + }); + return try cg.extractVectorComponent(result_ty, vec_id, dimension); +} + +fn airWorkItemId(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + if (cg.liveness.isUnused(inst)) return null; + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const dimension = pl_op.payload; + return try cg.builtin3D(.u32, .local_invocation_id, dimension, 0); +} + +// TODO: this must be an OpConstant/OpSpec but even then the driver crashes. +fn airWorkGroupSize(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + if (cg.liveness.isUnused(inst)) return null; + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const dimension = pl_op.payload; + return try cg.builtin3D(.u32, .workgroup_id, dimension, 0); +} + +fn airWorkGroupId(cg: *CodeGen, inst: Air.Inst.Index) !?Id { + if (cg.liveness.isUnused(inst)) return null; + const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; + const dimension = pl_op.payload; + return try cg.builtin3D(.u32, .workgroup_id, dimension, 0); +} + +fn typeOf(cg: *CodeGen, inst: Air.Inst.Ref) Type { + const zcu = cg.module.zcu; + return cg.air.typeOf(inst, &zcu.intern_pool); +} + +fn typeOfIndex(cg: *CodeGen, inst: Air.Inst.Index) Type { + const zcu = cg.module.zcu; + return cg.air.typeOfIndex(inst, &zcu.intern_pool); +} diff --git a/src/codegen/spirv/Module.zig b/src/codegen/spirv/Module.zig index ffcff0eab444..8daac06222c2 100644 --- a/src/codegen/spirv/Module.zig +++ b/src/codegen/spirv/Module.zig @@ -1,57 +1,100 @@ -//! This structure represents a SPIR-V (sections) module being compiled, and keeps track of all relevant information. -//! That includes the actual instructions, the current result-id bound, and data structures for querying result-id's -//! of data which needs to be persistent over different calls to Decl code generation. +//! This structure represents a SPIR-V (sections) module being compiled, and keeps +//! track of all relevant information. That includes the actual instructions, the +//! current result-id bound, and data structures for querying result-id's of data +//! which needs to be persistent over different calls to Decl code generation. //! -//! A SPIR-V binary module supports both little- and big endian layout. The layout is detected by the magic word in the -//! header. Therefore, we can ignore any byte order throughout the implementation, and just use the host byte order, -//! and make this a problem for the consumer. -const Module = @This(); - +//! A SPIR-V binary module supports both little- and big endian layout. The layout +//! is detected by the magic word in the header. Therefore, we can ignore any byte +//! order throughout the implementation, and just use the host byte order, and make +//! this a problem for the consumer. const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; -const autoHashStrat = std.hash.autoHashStrat; -const Wyhash = std.hash.Wyhash; +const Zcu = @import("../../Zcu.zig"); +const InternPool = @import("../../InternPool.zig"); +const Section = @import("Section.zig"); const spec = @import("spec.zig"); const Word = spec.Word; const Id = spec.Id; -const Section = @import("Section.zig"); +const Module = @This(); -/// This structure represents a function that isc in-progress of being emitted. -/// Commonly, the contents of this structure will be merged with the appropriate -/// sections of the module and re-used. Note that the SPIR-V module system makes -/// no attempt of compacting result-id's, so any Fn instance should ultimately -/// be merged into the module it's result-id's are allocated from. -pub const Fn = struct { - /// The prologue of this function; this section contains the function's - /// OpFunction, OpFunctionParameter, OpLabel and OpVariable instructions, and - /// is separated from the actual function contents as OpVariable instructions - /// must appear in the first block of a function definition. - prologue: Section = .{}, - /// The code of the body of this function. - /// This section should also contain the OpFunctionEnd instruction marking - /// the end of this function definition. - body: Section = .{}, - /// The decl dependencies that this function depends on. - decl_deps: std.AutoArrayHashMapUnmanaged(Decl.Index, void) = .empty, - - /// Reset this function without deallocating resources, so that - /// it may be used to emit code for another function. - pub fn reset(self: *Fn) void { - self.prologue.reset(); - self.body.reset(); - self.decl_deps.clearRetainingCapacity(); - } +gpa: Allocator, +arena: Allocator, +zcu: *Zcu, +nav_link: std.AutoHashMapUnmanaged(InternPool.Nav.Index, Decl.Index) = .empty, +uav_link: std.AutoHashMapUnmanaged(struct { InternPool.Index, spec.StorageClass }, Decl.Index) = .empty, +intern_map: std.AutoHashMapUnmanaged(struct { InternPool.Index, Repr }, Id) = .empty, +decls: std.ArrayListUnmanaged(Decl) = .empty, +decl_deps: std.ArrayListUnmanaged(Decl.Index) = .empty, +entry_points: std.AutoArrayHashMapUnmanaged(Id, EntryPoint) = .empty, +/// This map serves a dual purpose: +/// - It keeps track of pointers that are currently being emitted, so that we can tell +/// if they are recursive and need an OpTypeForwardPointer. +/// - It caches pointers by child-type. This is required because sometimes we rely on +/// ID-equality for pointers, and pointers constructed via `ptrType()` aren't interned +/// via the usual `intern_map` mechanism. +ptr_types: std.AutoHashMapUnmanaged(struct { Id, spec.StorageClass }, Id) = .{}, +/// For test declarations compiled for Vulkan target, we have to add a buffer. +/// We only need to generate this once, this holds the link information related to that. +error_buffer: ?Decl.Index = null, +/// SPIR-V instructions return result-ids. +/// This variable holds the module-wide counter for these. +next_result_id: Word = 1, +/// Some types shouldn't be emitted more than one time, but cannot be caught by +/// the `intern_map` during codegen. Sometimes, IDs are compared to check if +/// types are the same, so we can't delay until the dedup pass. Therefore, +/// this is an ad-hoc structure to cache types where required. +/// According to the SPIR-V specification, section 2.8, this includes all non-aggregate +/// non-pointer types. +/// Additionally, this is used for other values which can be cached, for example, +/// built-in variables. +cache: struct { + bool_type: ?Id = null, + void_type: ?Id = null, + opaque_types: std.StringHashMapUnmanaged(Id) = .empty, + int_types: std.AutoHashMapUnmanaged(std.builtin.Type.Int, Id) = .empty, + float_types: std.AutoHashMapUnmanaged(std.builtin.Type.Float, Id) = .empty, + vector_types: std.AutoHashMapUnmanaged(struct { Id, u32 }, Id) = .empty, + array_types: std.AutoHashMapUnmanaged(struct { Id, Id }, Id) = .empty, + struct_types: std.ArrayHashMapUnmanaged(StructType, Id, StructType.HashContext, true) = .empty, + fn_types: std.ArrayHashMapUnmanaged(FnType, Id, FnType.HashContext, true) = .empty, - /// Free the resources owned by this function. - pub fn deinit(self: *Fn, a: Allocator) void { - self.prologue.deinit(a); - self.body.deinit(a); - self.decl_deps.deinit(a); - self.* = undefined; - } + capabilities: std.AutoHashMapUnmanaged(spec.Capability, void) = .empty, + extensions: std.StringHashMapUnmanaged(void) = .empty, + extended_instruction_set: std.AutoHashMapUnmanaged(spec.InstructionSet, Id) = .empty, + decorations: std.AutoHashMapUnmanaged(struct { Id, spec.Decoration }, void) = .empty, + builtins: std.AutoHashMapUnmanaged(struct { spec.BuiltIn, spec.StorageClass }, Decl.Index) = .empty, + strings: std.StringArrayHashMapUnmanaged(Id) = .empty, + + bool_const: [2]?Id = .{ null, null }, + constants: std.ArrayHashMapUnmanaged(Constant, Id, Constant.HashContext, true) = .empty, +} = .{}, +/// Module layout, according to SPIR-V Spec section 2.4, "Logical Layout of a Module". +sections: struct { + capabilities: Section = .{}, + extensions: Section = .{}, + extended_instruction_set: Section = .{}, + memory_model: Section = .{}, + execution_modes: Section = .{}, + debug_strings: Section = .{}, + debug_names: Section = .{}, + annotations: Section = .{}, + globals: Section = .{}, + functions: Section = .{}, +} = .{}, + +pub const big_int_bits = 32; + +/// Data can be lowered into in two basic representations: indirect, which is when +/// a type is stored in memory, and direct, which is how a type is stored when its +/// a direct SPIR-V value. +pub const Repr = enum { + /// A SPIR-V value as it would be used in operations. + direct, + /// A SPIR-V value as it is stored in memory. + indirect, }; /// Declarations, both functions and globals, can have dependencies. These are used for 2 things: @@ -82,201 +125,166 @@ pub const Decl = struct { /// - For `invocation_global`, this is the result-id of the associated InvocationGlobal instruction. result_id: Id, /// The offset of the first dependency of this decl in the `decl_deps` array. - begin_dep: u32, + begin_dep: usize = 0, /// The past-end offset of the dependencies of this decl in the `decl_deps` array. - end_dep: u32, + end_dep: usize = 0, }; /// This models a kernel entry point. pub const EntryPoint = struct { /// The declaration that should be exported. - decl_index: ?Decl.Index = null, + decl_index: Decl.Index, /// The name of the kernel to be exported. - name: ?[]const u8 = null, + name: []const u8, /// Calling Convention - exec_model: ?spec.ExecutionModel = null, + exec_model: spec.ExecutionModel, exec_mode: ?spec.ExecutionMode = null, }; -/// A general-purpose allocator which may be used to allocate resources for this module -gpa: Allocator, - -/// Arena for things that need to live for the length of this program. -arena: std.heap.ArenaAllocator, - -/// Target info -target: *const std.Target, +const StructType = struct { + fields: []const Id, + ip_index: InternPool.Index, -/// The target SPIR-V version -version: spec.Version, - -/// Module layout, according to SPIR-V Spec section 2.4, "Logical Layout of a Module". -sections: struct { - /// Capability instructions - capabilities: Section = .{}, - /// OpExtension instructions - extensions: Section = .{}, - /// OpExtInstImport - extended_instruction_set: Section = .{}, - /// memory model defined by target - memory_model: Section = .{}, - /// OpEntryPoint instructions - Handled by `self.entry_points`. - /// OpExecutionMode and OpExecutionModeId instructions. - execution_modes: Section = .{}, - /// OpString, OpSourcExtension, OpSource, OpSourceContinued. - debug_strings: Section = .{}, - // OpName, OpMemberName. - debug_names: Section = .{}, - // OpModuleProcessed - skip for now. - /// Annotation instructions (OpDecorate etc). - annotations: Section = .{}, - /// Type declarations, constants, global variables - /// From this section, OpLine and OpNoLine is allowed. - /// According to the SPIR-V documentation, this section normally - /// also holds type and constant instructions. These are managed - /// via the cache instead, which is the sole structure that - /// manages that section. These will be inserted between this and - /// the previous section when emitting the final binary. - /// TODO: Do we need this section? Globals are also managed with another mechanism. - types_globals_constants: Section = .{}, - // Functions without a body - skip for now. - /// Regular function definitions. - functions: Section = .{}, -} = .{}, - -/// SPIR-V instructions return result-ids. This variable holds the module-wide counter for these. -next_result_id: Word, - -/// Cache for results of OpString instructions. -strings: std.StringArrayHashMapUnmanaged(Id) = .empty, - -/// Some types shouldn't be emitted more than one time, but cannot be caught by -/// the `intern_map` during codegen. Sometimes, IDs are compared to check if -/// types are the same, so we can't delay until the dedup pass. Therefore, -/// this is an ad-hoc structure to cache types where required. -/// According to the SPIR-V specification, section 2.8, this includes all non-aggregate -/// non-pointer types. -/// Additionally, this is used for other values which can be cached, for example, -/// built-in variables. -cache: struct { - bool_type: ?Id = null, - void_type: ?Id = null, - int_types: std.AutoHashMapUnmanaged(std.builtin.Type.Int, Id) = .empty, - float_types: std.AutoHashMapUnmanaged(std.builtin.Type.Float, Id) = .empty, - vector_types: std.AutoHashMapUnmanaged(struct { Id, u32 }, Id) = .empty, - array_types: std.AutoHashMapUnmanaged(struct { Id, Id }, Id) = .empty, - - capabilities: std.AutoHashMapUnmanaged(spec.Capability, void) = .empty, - extensions: std.StringHashMapUnmanaged(void) = .empty, - extended_instruction_set: std.AutoHashMapUnmanaged(spec.InstructionSet, Id) = .empty, - decorations: std.AutoHashMapUnmanaged(struct { Id, spec.Decoration }, void) = .empty, - builtins: std.AutoHashMapUnmanaged(struct { Id, spec.BuiltIn }, Decl.Index) = .empty, - - bool_const: [2]?Id = .{ null, null }, -} = .{}, - -/// Set of Decls, referred to by Decl.Index. -decls: std.ArrayListUnmanaged(Decl) = .empty, - -/// List of dependencies, per decl. This list holds all the dependencies, sliced by the -/// begin_dep and end_dep in `self.decls`. -decl_deps: std.ArrayListUnmanaged(Decl.Index) = .empty, - -/// The list of entry points that should be exported from this module. -entry_points: std.AutoArrayHashMapUnmanaged(Id, EntryPoint) = .empty, - -pub fn init(gpa: Allocator, target: *const std.Target) Module { - const version_minor: u8 = blk: { - // Prefer higher versions - if (target.cpu.has(.spirv, .v1_6)) break :blk 6; - if (target.cpu.has(.spirv, .v1_5)) break :blk 5; - if (target.cpu.has(.spirv, .v1_4)) break :blk 4; - if (target.cpu.has(.spirv, .v1_3)) break :blk 3; - if (target.cpu.has(.spirv, .v1_2)) break :blk 2; - if (target.cpu.has(.spirv, .v1_1)) break :blk 1; - break :blk 0; - }; + const HashContext = struct { + pub fn hash(_: @This(), ty: StructType) u32 { + var hasher = std.hash.Wyhash.init(0); + hasher.update(std.mem.sliceAsBytes(ty.fields)); + hasher.update(std.mem.asBytes(&ty.ip_index)); + return @truncate(hasher.final()); + } - return .{ - .gpa = gpa, - .arena = std.heap.ArenaAllocator.init(gpa), - .target = target, - .version = .{ .major = 1, .minor = version_minor }, - .next_result_id = 1, // 0 is an invalid SPIR-V result id, so start counting at 1. + pub fn eql(_: @This(), a: StructType, b: StructType, _: usize) bool { + return a.ip_index == b.ip_index and std.mem.eql(Id, a.fields, b.fields); + } }; -} - -pub fn deinit(self: *Module) void { - self.sections.capabilities.deinit(self.gpa); - self.sections.extensions.deinit(self.gpa); - self.sections.extended_instruction_set.deinit(self.gpa); - self.sections.memory_model.deinit(self.gpa); - self.sections.execution_modes.deinit(self.gpa); - self.sections.debug_strings.deinit(self.gpa); - self.sections.debug_names.deinit(self.gpa); - self.sections.annotations.deinit(self.gpa); - self.sections.types_globals_constants.deinit(self.gpa); - self.sections.functions.deinit(self.gpa); - - self.strings.deinit(self.gpa); +}; - self.cache.int_types.deinit(self.gpa); - self.cache.float_types.deinit(self.gpa); - self.cache.vector_types.deinit(self.gpa); - self.cache.array_types.deinit(self.gpa); - self.cache.capabilities.deinit(self.gpa); - self.cache.extensions.deinit(self.gpa); - self.cache.extended_instruction_set.deinit(self.gpa); - self.cache.decorations.deinit(self.gpa); - self.cache.builtins.deinit(self.gpa); +const FnType = struct { + return_ty: Id, + params: []const Id, - self.decls.deinit(self.gpa); - self.decl_deps.deinit(self.gpa); - self.entry_points.deinit(self.gpa); + const HashContext = struct { + pub fn hash(_: @This(), ty: FnType) u32 { + var hasher = std.hash.Wyhash.init(0); + hasher.update(std.mem.asBytes(&ty.return_ty)); + hasher.update(std.mem.sliceAsBytes(ty.params)); + return @truncate(hasher.final()); + } - self.arena.deinit(); + pub fn eql(_: @This(), a: FnType, b: FnType, _: usize) bool { + return a.return_ty == b.return_ty and + std.mem.eql(Id, a.params, b.params); + } + }; +}; - self.* = undefined; -} +const Constant = struct { + ty: Id, + value: spec.LiteralContextDependentNumber, + + const HashContext = struct { + pub fn hash(_: @This(), value: Constant) u32 { + const Tag = @typeInfo(spec.LiteralContextDependentNumber).@"union".tag_type.?; + var hasher = std.hash.Wyhash.init(0); + hasher.update(std.mem.asBytes(&value.ty)); + hasher.update(std.mem.asBytes(&@as(Tag, value.value))); + switch (value.value) { + inline else => |v| hasher.update(std.mem.asBytes(&v)), + } + return @truncate(hasher.final()); + } -pub const IdRange = struct { - base: u32, - len: u32, + pub fn eql(_: @This(), a: Constant, b: Constant, _: usize) bool { + if (a.ty != b.ty) return false; + const Tag = @typeInfo(spec.LiteralContextDependentNumber).@"union".tag_type.?; + if (@as(Tag, a.value) != @as(Tag, b.value)) return false; + return switch (a.value) { + inline else => |v, tag| v == @field(b.value, @tagName(tag)), + }; + } + }; +}; - pub fn at(range: IdRange, i: usize) Id { - assert(i < range.len); - return @enumFromInt(range.base + i); +pub fn deinit(module: *Module) void { + module.nav_link.deinit(module.gpa); + module.uav_link.deinit(module.gpa); + module.intern_map.deinit(module.gpa); + module.ptr_types.deinit(module.gpa); + + module.sections.capabilities.deinit(module.gpa); + module.sections.extensions.deinit(module.gpa); + module.sections.extended_instruction_set.deinit(module.gpa); + module.sections.memory_model.deinit(module.gpa); + module.sections.execution_modes.deinit(module.gpa); + module.sections.debug_strings.deinit(module.gpa); + module.sections.debug_names.deinit(module.gpa); + module.sections.annotations.deinit(module.gpa); + module.sections.globals.deinit(module.gpa); + module.sections.functions.deinit(module.gpa); + + module.cache.opaque_types.deinit(module.gpa); + module.cache.int_types.deinit(module.gpa); + module.cache.float_types.deinit(module.gpa); + module.cache.vector_types.deinit(module.gpa); + module.cache.array_types.deinit(module.gpa); + module.cache.struct_types.deinit(module.gpa); + module.cache.fn_types.deinit(module.gpa); + module.cache.capabilities.deinit(module.gpa); + module.cache.extensions.deinit(module.gpa); + module.cache.extended_instruction_set.deinit(module.gpa); + module.cache.decorations.deinit(module.gpa); + module.cache.builtins.deinit(module.gpa); + module.cache.strings.deinit(module.gpa); + + module.cache.constants.deinit(module.gpa); + + module.decls.deinit(module.gpa); + module.decl_deps.deinit(module.gpa); + module.entry_points.deinit(module.gpa); + + module.* = undefined; +} + +/// Fetch or allocate a result id for nav index. This function also marks the nav as alive. +/// Note: Function does not actually generate the nav, it just allocates an index. +pub fn resolveNav(module: *Module, ip: *InternPool, nav_index: InternPool.Nav.Index) !Decl.Index { + const entry = try module.nav_link.getOrPut(module.gpa, nav_index); + if (!entry.found_existing) { + const nav = ip.getNav(nav_index); + // TODO: Extern fn? + const kind: Decl.Kind = if (ip.isFunctionType(nav.typeOf(ip))) + .func + else switch (nav.getAddrspace()) { + .generic => .invocation_global, + else => .global, + }; + entry.value_ptr.* = try module.allocDecl(kind); } -}; -pub fn allocIds(self: *Module, n: u32) IdRange { - defer self.next_result_id += n; - return .{ - .base = self.next_result_id, - .len = n, - }; + return entry.value_ptr.*; } -pub fn allocId(self: *Module) Id { - return self.allocIds(1).at(0); +pub fn allocIds(module: *Module, n: u32) spec.IdRange { + defer module.next_result_id += n; + return .{ .base = module.next_result_id, .len = n }; } -pub fn idBound(self: Module) Word { - return self.next_result_id; +pub fn allocId(module: *Module) Id { + return module.allocIds(1).at(0); } -pub fn hasFeature(self: *Module, feature: std.Target.spirv.Feature) bool { - return self.target.cpu.has(.spirv, feature); +pub fn idBound(module: Module) Word { + return module.next_result_id; } -fn addEntryPointDeps( - self: *Module, +pub fn addEntryPointDeps( + module: *Module, decl_index: Decl.Index, seen: *std.DynamicBitSetUnmanaged, interface: *std.ArrayList(Id), ) !void { - const decl = self.declPtr(decl_index); - const deps = self.decl_deps.items[decl.begin_dep..decl.end_dep]; + const decl = module.declPtr(decl_index); + const deps = module.decl_deps.items[decl.begin_dep..decl.end_dep]; if (seen.isSet(@intFromEnum(decl_index))) { return; @@ -289,36 +297,38 @@ fn addEntryPointDeps( } for (deps) |dep| { - try self.addEntryPointDeps(dep, seen, interface); + try module.addEntryPointDeps(dep, seen, interface); } } -fn entryPoints(self: *Module) !Section { +fn entryPoints(module: *Module) !Section { + const target = module.zcu.getTarget(); + var entry_points = Section{}; - errdefer entry_points.deinit(self.gpa); + errdefer entry_points.deinit(module.gpa); - var interface = std.ArrayList(Id).init(self.gpa); + var interface = std.ArrayList(Id).init(module.gpa); defer interface.deinit(); - var seen = try std.DynamicBitSetUnmanaged.initEmpty(self.gpa, self.decls.items.len); - defer seen.deinit(self.gpa); + var seen = try std.DynamicBitSetUnmanaged.initEmpty(module.gpa, module.decls.items.len); + defer seen.deinit(module.gpa); - for (self.entry_points.keys(), self.entry_points.values()) |entry_point_id, entry_point| { + for (module.entry_points.keys(), module.entry_points.values()) |entry_point_id, entry_point| { interface.items.len = 0; - seen.setRangeValue(.{ .start = 0, .end = self.decls.items.len }, false); + seen.setRangeValue(.{ .start = 0, .end = module.decls.items.len }, false); - try self.addEntryPointDeps(entry_point.decl_index.?, &seen, &interface); - try entry_points.emit(self.gpa, .OpEntryPoint, .{ - .execution_model = entry_point.exec_model.?, + try module.addEntryPointDeps(entry_point.decl_index, &seen, &interface); + try entry_points.emit(module.gpa, .OpEntryPoint, .{ + .execution_model = entry_point.exec_model, .entry_point = entry_point_id, - .name = entry_point.name.?, + .name = entry_point.name, .interface = interface.items, }); if (entry_point.exec_mode == null and entry_point.exec_model == .fragment) { - switch (self.target.os.tag) { + switch (target.os.tag) { .vulkan, .opengl => |tag| { - try self.sections.execution_modes.emit(self.gpa, .OpExecutionMode, .{ + try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{ .entry_point = entry_point_id, .mode = if (tag == .vulkan) .origin_upper_left else .origin_lower_left, }); @@ -332,83 +342,100 @@ fn entryPoints(self: *Module) !Section { return entry_points; } -pub fn finalize(self: *Module, a: Allocator) ![]Word { +pub fn finalize(module: *Module, gpa: Allocator) ![]Word { + const target = module.zcu.getTarget(); + // Emit capabilities and extensions - switch (self.target.os.tag) { + switch (target.os.tag) { .opengl => { - try self.addCapability(.shader); - try self.addCapability(.matrix); + try module.addCapability(.shader); + try module.addCapability(.matrix); }, .vulkan => { - try self.addCapability(.shader); - try self.addCapability(.matrix); - if (self.target.cpu.arch == .spirv64) { - try self.addExtension("SPV_KHR_physical_storage_buffer"); - try self.addCapability(.physical_storage_buffer_addresses); + try module.addCapability(.shader); + try module.addCapability(.matrix); + if (target.cpu.arch == .spirv64) { + try module.addExtension("SPV_KHR_physical_storage_buffer"); + try module.addCapability(.physical_storage_buffer_addresses); } }, .opencl, .amdhsa => { - try self.addCapability(.kernel); - try self.addCapability(.addresses); + try module.addCapability(.kernel); + try module.addCapability(.addresses); }, else => unreachable, } - if (self.target.cpu.arch == .spirv64) try self.addCapability(.int64); - if (self.target.cpu.has(.spirv, .int64)) try self.addCapability(.int64); - if (self.target.cpu.has(.spirv, .float16)) try self.addCapability(.float16); - if (self.target.cpu.has(.spirv, .float64)) try self.addCapability(.float64); - if (self.target.cpu.has(.spirv, .generic_pointer)) try self.addCapability(.generic_pointer); - if (self.target.cpu.has(.spirv, .vector16)) try self.addCapability(.vector16); - if (self.target.cpu.has(.spirv, .storage_push_constant16)) { - try self.addExtension("SPV_KHR_16bit_storage"); - try self.addCapability(.storage_push_constant16); + if (target.cpu.arch == .spirv64) try module.addCapability(.int64); + if (target.cpu.has(.spirv, .int64)) try module.addCapability(.int64); + if (target.cpu.has(.spirv, .float16)) { + if (target.os.tag == .opencl) try module.addExtension("cl_khr_fp16"); + try module.addCapability(.float16); } - if (self.target.cpu.has(.spirv, .arbitrary_precision_integers)) { - try self.addExtension("SPV_INTEL_arbitrary_precision_integers"); - try self.addCapability(.arbitrary_precision_integers_intel); + if (target.cpu.has(.spirv, .float64)) try module.addCapability(.float64); + if (target.cpu.has(.spirv, .generic_pointer)) try module.addCapability(.generic_pointer); + if (target.cpu.has(.spirv, .vector16)) try module.addCapability(.vector16); + if (target.cpu.has(.spirv, .storage_push_constant16)) { + try module.addExtension("SPV_KHR_16bit_storage"); + try module.addCapability(.storage_push_constant16); } - if (self.target.cpu.has(.spirv, .variable_pointers)) { - try self.addExtension("SPV_KHR_variable_pointers"); - try self.addCapability(.variable_pointers_storage_buffer); - try self.addCapability(.variable_pointers); + if (target.cpu.has(.spirv, .arbitrary_precision_integers)) { + try module.addExtension("SPV_INTEL_arbitrary_precision_integers"); + try module.addCapability(.arbitrary_precision_integers_intel); + } + if (target.cpu.has(.spirv, .variable_pointers)) { + try module.addExtension("SPV_KHR_variable_pointers"); + try module.addCapability(.variable_pointers_storage_buffer); + try module.addCapability(.variable_pointers); } // These are well supported - try self.addCapability(.int8); - try self.addCapability(.int16); + try module.addCapability(.int8); + try module.addCapability(.int16); // Emit memory model - const addressing_model: spec.AddressingModel = switch (self.target.os.tag) { + const addressing_model: spec.AddressingModel = switch (target.os.tag) { .opengl => .logical, - .vulkan => if (self.target.cpu.arch == .spirv32) .logical else .physical_storage_buffer64, - .opencl => if (self.target.cpu.arch == .spirv32) .physical32 else .physical64, + .vulkan => if (target.cpu.arch == .spirv32) .logical else .physical_storage_buffer64, + .opencl => if (target.cpu.arch == .spirv32) .physical32 else .physical64, .amdhsa => .physical64, else => unreachable, }; - try self.sections.memory_model.emit(self.gpa, .OpMemoryModel, .{ + try module.sections.memory_model.emit(module.gpa, .OpMemoryModel, .{ .addressing_model = addressing_model, - .memory_model = switch (self.target.os.tag) { + .memory_model = switch (target.os.tag) { .opencl => .open_cl, .vulkan, .opengl => .glsl450, else => unreachable, }, }); - // See SPIR-V Spec section 2.3, "Physical Layout of a SPIR-V Module and Instruction" - // TODO: Audit calls to allocId() in this function to make it idempotent. - var entry_points = try self.entryPoints(); - defer entry_points.deinit(self.gpa); + var entry_points = try module.entryPoints(); + defer entry_points.deinit(module.gpa); + + const version: spec.Version = .{ + .major = 1, + .minor = blk: { + // Prefer higher versions + if (target.cpu.has(.spirv, .v1_6)) break :blk 6; + if (target.cpu.has(.spirv, .v1_5)) break :blk 5; + if (target.cpu.has(.spirv, .v1_4)) break :blk 4; + if (target.cpu.has(.spirv, .v1_3)) break :blk 3; + if (target.cpu.has(.spirv, .v1_2)) break :blk 2; + if (target.cpu.has(.spirv, .v1_1)) break :blk 1; + break :blk 0; + }, + }; const header = [_]Word{ spec.magic_number, - self.version.toWord(), + version.toWord(), spec.zig_generator_id, - self.idBound(), + module.idBound(), 0, // Schema (currently reserved for future use) }; var source = Section{}; - defer source.deinit(self.gpa); - try self.sections.debug_strings.emit(self.gpa, .OpSource, .{ + defer source.deinit(module.gpa); + try module.sections.debug_strings.emit(module.gpa, .OpSource, .{ .source_language = .zig, .version = 0, // We cannot emit these because the Khronos translator does not parse this instruction @@ -421,26 +448,26 @@ pub fn finalize(self: *Module, a: Allocator) ![]Word { // Note: needs to be kept in order according to section 2.3! const buffers = &[_][]const Word{ &header, - self.sections.capabilities.toWords(), - self.sections.extensions.toWords(), - self.sections.extended_instruction_set.toWords(), - self.sections.memory_model.toWords(), + module.sections.capabilities.toWords(), + module.sections.extensions.toWords(), + module.sections.extended_instruction_set.toWords(), + module.sections.memory_model.toWords(), entry_points.toWords(), - self.sections.execution_modes.toWords(), + module.sections.execution_modes.toWords(), source.toWords(), - self.sections.debug_strings.toWords(), - self.sections.debug_names.toWords(), - self.sections.annotations.toWords(), - self.sections.types_globals_constants.toWords(), - self.sections.functions.toWords(), + module.sections.debug_strings.toWords(), + module.sections.debug_names.toWords(), + module.sections.annotations.toWords(), + module.sections.globals.toWords(), + module.sections.functions.toWords(), }; var total_result_size: usize = 0; for (buffers) |buffer| { total_result_size += buffer.len; } - const result = try a.alloc(Word, total_result_size); - errdefer a.free(result); + const result = try gpa.alloc(Word, total_result_size); + errdefer comptime unreachable; var offset: usize = 0; for (buffers) |buffer| { @@ -451,34 +478,27 @@ pub fn finalize(self: *Module, a: Allocator) ![]Word { return result; } -/// Merge the sections making up a function declaration into this module. -pub fn addFunction(self: *Module, decl_index: Decl.Index, func: Fn) !void { - try self.sections.functions.append(self.gpa, func.prologue); - try self.sections.functions.append(self.gpa, func.body); - try self.declareDeclDeps(decl_index, func.decl_deps.keys()); -} - -pub fn addCapability(self: *Module, cap: spec.Capability) !void { - const entry = try self.cache.capabilities.getOrPut(self.gpa, cap); +pub fn addCapability(module: *Module, cap: spec.Capability) !void { + const entry = try module.cache.capabilities.getOrPut(module.gpa, cap); if (entry.found_existing) return; - try self.sections.capabilities.emit(self.gpa, .OpCapability, .{ .capability = cap }); + try module.sections.capabilities.emit(module.gpa, .OpCapability, .{ .capability = cap }); } -pub fn addExtension(self: *Module, ext: []const u8) !void { - const entry = try self.cache.extensions.getOrPut(self.gpa, ext); +pub fn addExtension(module: *Module, ext: []const u8) !void { + const entry = try module.cache.extensions.getOrPut(module.gpa, ext); if (entry.found_existing) return; - try self.sections.extensions.emit(self.gpa, .OpExtension, .{ .name = ext }); + try module.sections.extensions.emit(module.gpa, .OpExtension, .{ .name = ext }); } /// Imports or returns the existing id of an extended instruction set -pub fn importInstructionSet(self: *Module, set: spec.InstructionSet) !Id { +pub fn importInstructionSet(module: *Module, set: spec.InstructionSet) !Id { assert(set != .core); - const gop = try self.cache.extended_instruction_set.getOrPut(self.gpa, set); + const gop = try module.cache.extended_instruction_set.getOrPut(module.gpa, set); if (gop.found_existing) return gop.value_ptr.*; - const result_id = self.allocId(); - try self.sections.extended_instruction_set.emit(self.gpa, .OpExtInstImport, .{ + const result_id = module.allocId(); + try module.sections.extended_instruction_set.emit(module.gpa, .OpExtInstImport, .{ .id_result = result_id, .name = @tagName(set), }); @@ -487,104 +507,130 @@ pub fn importInstructionSet(self: *Module, set: spec.InstructionSet) !Id { return result_id; } -/// Fetch the result-id of an instruction corresponding to a string. -pub fn resolveString(self: *Module, string: []const u8) !Id { - if (self.strings.get(string)) |id| { - return id; - } - - const id = self.allocId(); - try self.strings.put(self.gpa, try self.arena.allocator().dupe(u8, string), id); - - try self.sections.debug_strings.emit(self.gpa, .OpString, .{ - .id_result = id, - .string = string, - }); - - return id; -} +pub fn boolType(module: *Module) !Id { + if (module.cache.bool_type) |id| return id; -pub fn structType(self: *Module, result_id: Id, types: []const Id, maybe_names: ?[]const []const u8) !void { - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeStruct, .{ + const result_id = module.allocId(); + try module.sections.globals.emit(module.gpa, .OpTypeBool, .{ .id_result = result_id, - .id_ref = types, }); - - if (maybe_names) |names| { - assert(names.len == types.len); - for (names, 0..) |name, i| { - try self.memberDebugName(result_id, @intCast(i), name); - } - } + module.cache.bool_type = result_id; + return result_id; } -pub fn boolType(self: *Module) !Id { - if (self.cache.bool_type) |id| return id; +pub fn voidType(module: *Module) !Id { + if (module.cache.void_type) |id| return id; - const result_id = self.allocId(); - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeBool, .{ + const result_id = module.allocId(); + try module.sections.globals.emit(module.gpa, .OpTypeVoid, .{ .id_result = result_id, }); - self.cache.bool_type = result_id; + module.cache.void_type = result_id; + try module.debugName(result_id, "void"); return result_id; } -pub fn voidType(self: *Module) !Id { - if (self.cache.void_type) |id| return id; - - const result_id = self.allocId(); - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVoid, .{ +pub fn opaqueType(module: *Module, name: []const u8) !Id { + if (module.cache.opaque_types.get(name)) |id| return id; + const result_id = module.allocId(); + const name_dup = try module.arena.dupe(u8, name); + try module.sections.globals.emit(module.gpa, .OpTypeOpaque, .{ .id_result = result_id, + .literal_string = name_dup, }); - self.cache.void_type = result_id; - try self.debugName(result_id, "void"); + try module.debugName(result_id, name_dup); + try module.cache.opaque_types.put(module.gpa, name_dup, result_id); return result_id; } -pub fn intType(self: *Module, signedness: std.builtin.Signedness, bits: u16) !Id { +pub fn backingIntBits(module: *Module, bits: u16) struct { u16, bool } { + assert(bits != 0); + const target = module.zcu.getTarget(); + + if (target.cpu.has(.spirv, .arbitrary_precision_integers) and bits <= 32) { + return .{ bits, false }; + } + + // We require Int8 and Int16 capabilities and benefit Int64 when available. + // 32-bit integers are always supported (see spec, 2.16.1, Data rules). + const ints = [_]struct { bits: u16, enabled: bool }{ + .{ .bits = 8, .enabled = true }, + .{ .bits = 16, .enabled = true }, + .{ .bits = 32, .enabled = true }, + .{ + .bits = 64, + .enabled = target.cpu.has(.spirv, .int64) or target.cpu.arch == .spirv64, + }, + }; + + for (ints) |int| { + if (bits <= int.bits and int.enabled) return .{ int.bits, false }; + } + + // Big int + return .{ std.mem.alignForward(u16, bits, big_int_bits), true }; +} + +pub fn intType(module: *Module, signedness: std.builtin.Signedness, bits: u16) !Id { assert(bits > 0); - const entry = try self.cache.int_types.getOrPut(self.gpa, .{ .signedness = signedness, .bits = bits }); + + const target = module.zcu.getTarget(); + const actual_signedness = switch (target.os.tag) { + // Kernel only supports unsigned ints. + .opencl, .amdhsa => .unsigned, + else => signedness, + }; + const backing_bits, const big_int = module.backingIntBits(bits); + if (big_int) { + // TODO: support composite integers larger than 64 bit + assert(backing_bits <= 64); + const u32_ty = try module.intType(.unsigned, 32); + const len_id = try module.constant(u32_ty, .{ .uint32 = backing_bits / big_int_bits }); + return module.arrayType(len_id, u32_ty); + } + + const entry = try module.cache.int_types.getOrPut(module.gpa, .{ .signedness = actual_signedness, .bits = backing_bits }); if (!entry.found_existing) { - const result_id = self.allocId(); + const result_id = module.allocId(); entry.value_ptr.* = result_id; - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeInt, .{ + try module.sections.globals.emit(module.gpa, .OpTypeInt, .{ .id_result = result_id, - .width = bits, - .signedness = switch (signedness) { + .width = backing_bits, + .signedness = switch (actual_signedness) { .signed => 1, .unsigned => 0, }, }); - switch (signedness) { - .signed => try self.debugNameFmt(result_id, "i{}", .{bits}), - .unsigned => try self.debugNameFmt(result_id, "u{}", .{bits}), + switch (actual_signedness) { + .signed => try module.debugNameFmt(result_id, "i{}", .{backing_bits}), + .unsigned => try module.debugNameFmt(result_id, "u{}", .{backing_bits}), } } return entry.value_ptr.*; } -pub fn floatType(self: *Module, bits: u16) !Id { +pub fn floatType(module: *Module, bits: u16) !Id { assert(bits > 0); - const entry = try self.cache.float_types.getOrPut(self.gpa, .{ .bits = bits }); + const entry = try module.cache.float_types.getOrPut(module.gpa, .{ .bits = bits }); if (!entry.found_existing) { - const result_id = self.allocId(); + const result_id = module.allocId(); entry.value_ptr.* = result_id; - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeFloat, .{ + try module.sections.globals.emit(module.gpa, .OpTypeFloat, .{ .id_result = result_id, .width = bits, }); - try self.debugNameFmt(result_id, "f{}", .{bits}); + try module.debugNameFmt(result_id, "f{}", .{bits}); } return entry.value_ptr.*; } -pub fn vectorType(self: *Module, len: u32, child_ty_id: Id) !Id { - const entry = try self.cache.vector_types.getOrPut(self.gpa, .{ child_ty_id, len }); +pub fn vectorType(module: *Module, len: u32, child_ty_id: Id) !Id { + const entry = try module.cache.vector_types.getOrPut(module.gpa, .{ child_ty_id, len }); if (!entry.found_existing) { - const result_id = self.allocId(); + const result_id = module.allocId(); entry.value_ptr.* = result_id; - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVector, .{ + try module.sections.globals.emit(module.gpa, .OpTypeVector, .{ .id_result = result_id, .component_type = child_ty_id, .component_count = len, @@ -593,12 +639,12 @@ pub fn vectorType(self: *Module, len: u32, child_ty_id: Id) !Id { return entry.value_ptr.*; } -pub fn arrayType(self: *Module, len_id: Id, child_ty_id: Id) !Id { - const entry = try self.cache.array_types.getOrPut(self.gpa, .{ child_ty_id, len_id }); +pub fn arrayType(module: *Module, len_id: Id, child_ty_id: Id) !Id { + const entry = try module.cache.array_types.getOrPut(module.gpa, .{ child_ty_id, len_id }); if (!entry.found_existing) { - const result_id = self.allocId(); + const result_id = module.allocId(); entry.value_ptr.* = result_id; - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeArray, .{ + try module.sections.globals.emit(module.gpa, .OpTypeArray, .{ .id_result = result_id, .element_type = child_ty_id, .length = len_id, @@ -607,37 +653,114 @@ pub fn arrayType(self: *Module, len_id: Id, child_ty_id: Id) !Id { return entry.value_ptr.*; } -pub fn functionType(self: *Module, return_ty_id: Id, param_type_ids: []const Id) !Id { - const result_id = self.allocId(); - try self.sections.types_globals_constants.emit(self.gpa, .OpTypeFunction, .{ +pub fn ptrType(module: *Module, child_ty_id: Id, storage_class: spec.StorageClass) !Id { + const key = .{ child_ty_id, storage_class }; + const gop = try module.ptr_types.getOrPut(module.gpa, key); + if (!gop.found_existing) { + gop.value_ptr.* = module.allocId(); + try module.sections.globals.emit(module.gpa, .OpTypePointer, .{ + .id_result = gop.value_ptr.*, + .storage_class = storage_class, + .type = child_ty_id, + }); + return gop.value_ptr.*; + } + return gop.value_ptr.*; +} + +pub fn structType( + module: *Module, + types: []const Id, + maybe_names: ?[]const []const u8, + maybe_offsets: ?[]const u32, + ip_index: InternPool.Index, +) !Id { + const target = module.zcu.getTarget(); + + if (module.cache.struct_types.get(.{ .fields = types, .ip_index = ip_index })) |id| return id; + const result_id = module.allocId(); + const types_dup = try module.arena.dupe(Id, types); + try module.sections.globals.emit(module.gpa, .OpTypeStruct, .{ .id_result = result_id, - .return_type = return_ty_id, - .id_ref_2 = param_type_ids, + .id_ref = types_dup, }); + + if (maybe_names) |names| { + assert(names.len == types.len); + for (names, 0..) |name, i| { + try module.memberDebugName(result_id, @intCast(i), name); + } + } + + switch (target.os.tag) { + .vulkan, .opengl => { + if (maybe_offsets) |offsets| { + assert(offsets.len == types.len); + for (offsets, 0..) |offset, i| { + try module.decorateMember( + result_id, + @intCast(i), + .{ .offset = .{ .byte_offset = offset } }, + ); + } + } + }, + else => {}, + } + + try module.cache.struct_types.put( + module.gpa, + .{ + .fields = types_dup, + .ip_index = if (module.zcu.comp.config.root_strip) .none else ip_index, + }, + result_id, + ); return result_id; } -pub fn constant(self: *Module, result_ty_id: Id, value: spec.LiteralContextDependentNumber) !Id { - const result_id = self.allocId(); - const section = &self.sections.types_globals_constants; - try section.emit(self.gpa, .OpConstant, .{ - .id_result_type = result_ty_id, +pub fn functionType(module: *Module, return_ty_id: Id, param_type_ids: []const Id) !Id { + if (module.cache.fn_types.get(.{ + .return_ty = return_ty_id, + .params = param_type_ids, + })) |id| return id; + const result_id = module.allocId(); + const params_dup = try module.arena.dupe(Id, param_type_ids); + try module.sections.globals.emit(module.gpa, .OpTypeFunction, .{ .id_result = result_id, - .value = value, + .return_type = return_ty_id, + .id_ref_2 = params_dup, }); + try module.cache.fn_types.put(module.gpa, .{ + .return_ty = return_ty_id, + .params = params_dup, + }, result_id); return result_id; } -pub fn constBool(self: *Module, value: bool) !Id { - if (self.cache.bool_const[@intFromBool(value)]) |b| return b; +pub fn constant(module: *Module, ty_id: Id, value: spec.LiteralContextDependentNumber) !Id { + const gop = try module.cache.constants.getOrPut(module.gpa, .{ .ty = ty_id, .value = value }); + if (!gop.found_existing) { + gop.value_ptr.* = module.allocId(); + try module.sections.globals.emit(module.gpa, .OpConstant, .{ + .id_result_type = ty_id, + .id_result = gop.value_ptr.*, + .value = value, + }); + } + return gop.value_ptr.*; +} + +pub fn constBool(module: *Module, value: bool) !Id { + if (module.cache.bool_const[@intFromBool(value)]) |b| return b; - const result_ty_id = try self.boolType(); - const result_id = self.allocId(); - self.cache.bool_const[@intFromBool(value)] = result_id; + const result_ty_id = try module.boolType(); + const result_id = module.allocId(); + module.cache.bool_const[@intFromBool(value)] = result_id; switch (value) { - inline else => |value_ct| try self.sections.types_globals_constants.emit( - self.gpa, + inline else => |value_ct| try module.sections.globals.emit( + module.gpa, if (value_ct) .OpConstantTrue else .OpConstantFalse, .{ .id_result_type = result_ty_id, @@ -649,37 +772,40 @@ pub fn constBool(self: *Module, value: bool) !Id { return result_id; } -/// Return a pointer to a builtin variable. `result_ty_id` must be a **pointer** -/// with storage class `.Input`. -pub fn builtin(self: *Module, result_ty_id: Id, spirv_builtin: spec.BuiltIn) !Decl.Index { - const entry = try self.cache.builtins.getOrPut(self.gpa, .{ result_ty_id, spirv_builtin }); - if (!entry.found_existing) { - const decl_index = try self.allocDecl(.global); - const result_id = self.declPtr(decl_index).result_id; - entry.value_ptr.* = decl_index; - try self.sections.types_globals_constants.emit(self.gpa, .OpVariable, .{ +pub fn builtin( + module: *Module, + result_ty_id: Id, + spirv_builtin: spec.BuiltIn, + storage_class: spec.StorageClass, +) !Decl.Index { + const gop = try module.cache.builtins.getOrPut(module.gpa, .{ spirv_builtin, storage_class }); + if (!gop.found_existing) { + const decl_index = try module.allocDecl(.global); + const decl = module.declPtr(decl_index); + + gop.value_ptr.* = decl_index; + try module.sections.globals.emit(module.gpa, .OpVariable, .{ .id_result_type = result_ty_id, - .id_result = result_id, - .storage_class = .input, + .id_result = decl.result_id, + .storage_class = storage_class, }); - try self.decorate(result_id, .{ .built_in = .{ .built_in = spirv_builtin } }); - try self.declareDeclDeps(decl_index, &.{}); + try module.decorate(decl.result_id, .{ .built_in = .{ .built_in = spirv_builtin } }); } - return entry.value_ptr.*; + return gop.value_ptr.*; } -pub fn constUndef(self: *Module, ty_id: Id) !Id { - const result_id = self.allocId(); - try self.sections.types_globals_constants.emit(self.gpa, .OpUndef, .{ +pub fn constUndef(module: *Module, ty_id: Id) !Id { + const result_id = module.allocId(); + try module.sections.globals.emit(module.gpa, .OpUndef, .{ .id_result_type = ty_id, .id_result = result_id, }); return result_id; } -pub fn constNull(self: *Module, ty_id: Id) !Id { - const result_id = self.allocId(); - try self.sections.types_globals_constants.emit(self.gpa, .OpConstantNull, .{ +pub fn constNull(module: *Module, ty_id: Id) !Id { + const result_id = module.allocId(); + try module.sections.globals.emit(module.gpa, .OpConstantNull, .{ .id_result_type = ty_id, .id_result = result_id, }); @@ -688,13 +814,13 @@ pub fn constNull(self: *Module, ty_id: Id) !Id { /// Decorate a result-id. pub fn decorate( - self: *Module, + module: *Module, target: Id, decoration: spec.Decoration.Extended, ) !void { - const entry = try self.cache.decorations.getOrPut(self.gpa, .{ target, decoration }); - if (!entry.found_existing) { - try self.sections.annotations.emit(self.gpa, .OpDecorate, .{ + const gop = try module.cache.decorations.getOrPut(module.gpa, .{ target, decoration }); + if (!gop.found_existing) { + try module.sections.annotations.emit(module.gpa, .OpDecorate, .{ .target = target, .decoration = decoration, }); @@ -704,79 +830,112 @@ pub fn decorate( /// Decorate a result-id which is a member of some struct. /// We really don't have to and shouldn't need to cache this. pub fn decorateMember( - self: *Module, + module: *Module, structure_type: Id, member: u32, decoration: spec.Decoration.Extended, ) !void { - try self.sections.annotations.emit(self.gpa, .OpMemberDecorate, .{ + try module.sections.annotations.emit(module.gpa, .OpMemberDecorate, .{ .structure_type = structure_type, .member = member, .decoration = decoration, }); } -pub fn allocDecl(self: *Module, kind: Decl.Kind) !Decl.Index { - try self.decls.append(self.gpa, .{ +pub fn allocDecl(module: *Module, kind: Decl.Kind) !Decl.Index { + try module.decls.append(module.gpa, .{ .kind = kind, - .result_id = self.allocId(), - .begin_dep = undefined, - .end_dep = undefined, + .result_id = module.allocId(), }); - return @as(Decl.Index, @enumFromInt(@as(u32, @intCast(self.decls.items.len - 1)))); -} - -pub fn declPtr(self: *Module, index: Decl.Index) *Decl { - return &self.decls.items[@intFromEnum(index)]; + return @as(Decl.Index, @enumFromInt(@as(u32, @intCast(module.decls.items.len - 1)))); } -/// Declare ALL dependencies for a decl. -pub fn declareDeclDeps(self: *Module, decl_index: Decl.Index, deps: []const Decl.Index) !void { - const begin_dep: u32 = @intCast(self.decl_deps.items.len); - try self.decl_deps.appendSlice(self.gpa, deps); - const end_dep: u32 = @intCast(self.decl_deps.items.len); - - const decl = self.declPtr(decl_index); - decl.begin_dep = begin_dep; - decl.end_dep = end_dep; +pub fn declPtr(module: *Module, index: Decl.Index) *Decl { + return &module.decls.items[@intFromEnum(index)]; } /// Declare a SPIR-V function as an entry point. This causes an extra wrapper /// function to be generated, which is then exported as the real entry point. The purpose of this /// wrapper is to allocate and initialize the structure holding the instance globals. pub fn declareEntryPoint( - self: *Module, + module: *Module, decl_index: Decl.Index, name: []const u8, exec_model: spec.ExecutionModel, exec_mode: ?spec.ExecutionMode, ) !void { - const gop = try self.entry_points.getOrPut(self.gpa, self.declPtr(decl_index).result_id); + const gop = try module.entry_points.getOrPut(module.gpa, module.declPtr(decl_index).result_id); gop.value_ptr.decl_index = decl_index; - gop.value_ptr.name = try self.arena.allocator().dupe(u8, name); + gop.value_ptr.name = name; gop.value_ptr.exec_model = exec_model; // Might've been set by assembler if (!gop.found_existing) gop.value_ptr.exec_mode = exec_mode; } -pub fn debugName(self: *Module, target: Id, name: []const u8) !void { - try self.sections.debug_names.emit(self.gpa, .OpName, .{ +pub fn debugName(module: *Module, target: Id, name: []const u8) !void { + try module.sections.debug_names.emit(module.gpa, .OpName, .{ .target = target, .name = name, }); } -pub fn debugNameFmt(self: *Module, target: Id, comptime fmt: []const u8, args: anytype) !void { - const name = try std.fmt.allocPrint(self.gpa, fmt, args); - defer self.gpa.free(name); - try self.debugName(target, name); +pub fn debugNameFmt(module: *Module, target: Id, comptime fmt: []const u8, args: anytype) !void { + const name = try std.fmt.allocPrint(module.gpa, fmt, args); + defer module.gpa.free(name); + try module.debugName(target, name); } -pub fn memberDebugName(self: *Module, target: Id, member: u32, name: []const u8) !void { - try self.sections.debug_names.emit(self.gpa, .OpMemberName, .{ +pub fn memberDebugName(module: *Module, target: Id, member: u32, name: []const u8) !void { + try module.sections.debug_names.emit(module.gpa, .OpMemberName, .{ .type = target, .member = member, .name = name, }); } + +pub fn debugString(module: *Module, string: []const u8) !Id { + const entry = try module.cache.strings.getOrPut(module.gpa, string); + if (!entry.found_existing) { + entry.value_ptr.* = module.allocId(); + try module.sections.debug_strings.emit(module.gpa, .OpString, .{ + .id_result = entry.value_ptr.*, + .string = string, + }); + } + return entry.value_ptr.*; +} + +pub fn storageClass(module: *Module, as: std.builtin.AddressSpace) spec.StorageClass { + const target = module.zcu.getTarget(); + return switch (as) { + .generic => .function, + .global => switch (target.os.tag) { + .opencl, .amdhsa => .cross_workgroup, + else => .storage_buffer, + }, + .push_constant => .push_constant, + .output => .output, + .uniform => .uniform, + .storage_buffer => .storage_buffer, + .physical_storage_buffer => .physical_storage_buffer, + .constant => .uniform_constant, + .shared => .workgroup, + .local => .function, + .input => .input, + .gs, + .fs, + .ss, + .param, + .flash, + .flash1, + .flash2, + .flash3, + .flash4, + .flash5, + .cog, + .lut, + .hub, + => unreachable, + }; +} diff --git a/src/codegen/spirv/Section.zig b/src/codegen/spirv/Section.zig index 53fbe66764c0..d5748afa39fe 100644 --- a/src/codegen/spirv/Section.zig +++ b/src/codegen/spirv/Section.zig @@ -13,8 +13,6 @@ const Log2Word = std.math.Log2Int(Word); const Opcode = spec.Opcode; -/// The instructions in this section. Memory is owned by the Module -/// externally associated to this Section. instructions: std.ArrayListUnmanaged(Word) = .empty, pub fn deinit(section: *Section, allocator: Allocator) void { @@ -22,9 +20,8 @@ pub fn deinit(section: *Section, allocator: Allocator) void { section.* = undefined; } -/// Clear the instructions in this section pub fn reset(section: *Section) void { - section.instructions.items.len = 0; + section.instructions.clearRetainingCapacity(); } pub fn toWords(section: Section) []Word { @@ -36,9 +33,12 @@ pub fn append(section: *Section, allocator: Allocator, other_section: Section) ! try section.instructions.appendSlice(allocator, other_section.instructions.items); } -/// Ensure capacity of at least `capacity` more words in this section. -pub fn ensureUnusedCapacity(section: *Section, allocator: Allocator, capacity: usize) !void { - try section.instructions.ensureUnusedCapacity(allocator, capacity); +pub fn ensureUnusedCapacity( + section: *Section, + allocator: Allocator, + words: usize, +) !void { + try section.instructions.ensureUnusedCapacity(allocator, words); } /// Write an instruction and size, operands are to be inserted manually. @@ -46,7 +46,7 @@ pub fn emitRaw( section: *Section, allocator: Allocator, opcode: Opcode, - operand_words: usize, // opcode itself not included + operand_words: usize, ) !void { const word_count = 1 + operand_words; try section.instructions.ensureUnusedCapacity(allocator, word_count); @@ -64,45 +64,26 @@ pub fn emitRawInstruction( section.writeWords(operands); } -pub fn emit( +pub fn emitAssumeCapacity( section: *Section, - allocator: Allocator, comptime opcode: spec.Opcode, operands: opcode.Operands(), ) !void { const word_count = instructionSize(opcode, operands); - try section.instructions.ensureUnusedCapacity(allocator, word_count); section.writeWord(@as(Word, @intCast(word_count << 16)) | @intFromEnum(opcode)); section.writeOperands(opcode.Operands(), operands); } -pub fn emitBranch( - section: *Section, - allocator: Allocator, - target_label: spec.Id, -) !void { - try section.emit(allocator, .OpBranch, .{ - .target_label = target_label, - }); -} - -pub fn emitSpecConstantOp( +pub fn emit( section: *Section, allocator: Allocator, comptime opcode: spec.Opcode, operands: opcode.Operands(), ) !void { - const word_count = operandsSize(opcode.Operands(), operands); - try section.emitRaw(allocator, .OpSpecConstantOp, 1 + word_count); - section.writeOperand(spec.Id, operands.id_result_type); - section.writeOperand(spec.Id, operands.id_result); - section.writeOperand(Opcode, opcode); - - const fields = @typeInfo(opcode.Operands()).@"struct".fields; - // First 2 fields are always id_result_type and id_result. - inline for (fields[2..]) |field| { - section.writeOperand(field.type, @field(operands, field.name)); - } + const word_count = instructionSize(opcode, operands); + try section.instructions.ensureUnusedCapacity(allocator, word_count); + section.writeWord(@as(Word, @intCast(word_count << 16)) | @intFromEnum(opcode)); + section.writeOperands(opcode.Operands(), operands); } pub fn writeWord(section: *Section, word: Word) void { @@ -126,7 +107,6 @@ fn writeOperands(section: *Section, comptime Operands: type, operands: Operands) .void => return, else => unreachable, }; - inline for (fields) |field| { section.writeOperand(field.type, @field(operands, field.name)); } @@ -134,30 +114,18 @@ fn writeOperands(section: *Section, comptime Operands: type, operands: Operands) pub fn writeOperand(section: *Section, comptime Operand: type, operand: Operand) void { switch (Operand) { + spec.LiteralSpecConstantOpInteger => unreachable, spec.Id => section.writeWord(@intFromEnum(operand)), - spec.LiteralInteger => section.writeWord(operand), - spec.LiteralString => section.writeString(operand), - spec.LiteralContextDependentNumber => section.writeContextDependentNumber(operand), - spec.LiteralExtInstInteger => section.writeWord(operand.inst), - - // TODO: Where this type is used (OpSpecConstantOp) is currently not correct in the spec json, - // so it most likely needs to be altered into something that can actually describe the entire - // instruction in which it is used. - spec.LiteralSpecConstantOpInteger => section.writeWord(@intFromEnum(operand.opcode)), - spec.PairLiteralIntegerIdRef => section.writeWords(&.{ operand.value, @enumFromInt(operand.label) }), spec.PairIdRefLiteralInteger => section.writeWords(&.{ @intFromEnum(operand.target), operand.member }), spec.PairIdRefIdRef => section.writeWords(&.{ @intFromEnum(operand[0]), @intFromEnum(operand[1]) }), - else => switch (@typeInfo(Operand)) { .@"enum" => section.writeWord(@intFromEnum(operand)), - .optional => |info| if (operand) |child| { - section.writeOperand(info.child, child); - }, + .optional => |info| if (operand) |child| section.writeOperand(info.child, child), .pointer => |info| { std.debug.assert(info.size == .slice); // Should be no other pointer types in the spec. for (operand) |item| { @@ -178,18 +146,14 @@ pub fn writeOperand(section: *Section, comptime Operand: type, operand: Operand) } fn writeString(section: *Section, str: []const u8) void { - // TODO: Not actually sure whether this is correct for big-endian. - // See https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#Literal const zero_terminated_len = str.len + 1; var i: usize = 0; while (i < zero_terminated_len) : (i += @sizeOf(Word)) { var word: Word = 0; - var j: usize = 0; while (j < @sizeOf(Word) and i + j < str.len) : (j += 1) { word |= @as(Word, str[i + j]) << @as(Log2Word, @intCast(j * @bitSizeOf(u8))); } - section.instructions.appendAssumeCapacity(word); } } @@ -233,20 +197,19 @@ fn writeExtendedMask(section: *Section, comptime Operand: type, operand: Operand } fn writeExtendedUnion(section: *Section, comptime Operand: type, operand: Operand) void { - const tag = std.meta.activeTag(operand); - section.writeWord(@intFromEnum(tag)); - - inline for (@typeInfo(Operand).@"union".fields) |field| { - if (@field(Operand, field.name) == tag) { - section.writeOperands(field.type, @field(operand, field.name)); - return; - } - } - unreachable; + return switch (operand) { + inline else => |op, tag| { + section.writeWord(@intFromEnum(tag)); + section.writeOperands( + @FieldType(Operand, @tagName(tag)), + op, + ); + }, + }; } fn instructionSize(comptime opcode: spec.Opcode, operands: opcode.Operands()) usize { - return 1 + operandsSize(opcode.Operands(), operands); + return operandsSize(opcode.Operands(), operands) + 1; } fn operandsSize(comptime Operands: type, operands: Operands) usize { @@ -266,28 +229,14 @@ fn operandsSize(comptime Operands: type, operands: Operands) usize { fn operandSize(comptime Operand: type, operand: Operand) usize { return switch (Operand) { - spec.Id, - spec.LiteralInteger, - spec.LiteralExtInstInteger, - => 1, - - spec.LiteralString => std.math.divCeil(usize, operand.len + 1, @sizeOf(Word)) catch unreachable, // Add one for zero-terminator - + spec.LiteralSpecConstantOpInteger => unreachable, + spec.Id, spec.LiteralInteger, spec.LiteralExtInstInteger => 1, + spec.LiteralString => std.math.divCeil(usize, operand.len + 1, @sizeOf(Word)) catch unreachable, spec.LiteralContextDependentNumber => switch (operand) { .int32, .uint32, .float32 => 1, .int64, .uint64, .float64 => 2, }, - - // TODO: Where this type is used (OpSpecConstantOp) is currently not correct in the spec - // json, so it most likely needs to be altered into something that can actually - // describe the entire insturction in which it is used. - spec.LiteralSpecConstantOpInteger => 1, - - spec.PairLiteralIntegerIdRef, - spec.PairIdRefLiteralInteger, - spec.PairIdRefIdRef, - => 2, - + spec.PairLiteralIntegerIdRef, spec.PairIdRefLiteralInteger, spec.PairIdRefIdRef => 2, else => switch (@typeInfo(Operand)) { .@"enum" => 1, .optional => |info| if (operand) |child| operandSize(info.child, child) else 0, @@ -299,133 +248,25 @@ fn operandSize(comptime Operand: type, operand: Operand) usize { } break :blk total; }, - .@"struct" => |info| if (info.layout == .@"packed") 1 else extendedMaskSize(Operand, operand), - .@"union" => extendedUnionSize(Operand, operand), - else => unreachable, - }, - }; -} + .@"struct" => |struct_info| { + if (struct_info.layout == .@"packed") return 1; -fn extendedMaskSize(comptime Operand: type, operand: Operand) usize { - var total: usize = 0; - var any_set = false; - inline for (@typeInfo(Operand).@"struct".fields) |field| { - switch (@typeInfo(field.type)) { - .optional => |info| if (@field(operand, field.name)) |child| { - total += operandsSize(info.child, child); - any_set = true; + var total: usize = 0; + inline for (@typeInfo(Operand).@"struct".fields) |field| { + switch (@typeInfo(field.type)) { + .optional => |info| if (@field(operand, field.name)) |child| { + total += operandsSize(info.child, child); + }, + .bool => {}, + else => unreachable, + } + } + return total + 1; // Add one for the mask itself. }, - .bool => if (@field(operand, field.name)) { - any_set = true; + .@"union" => switch (operand) { + inline else => |op, tag| operandsSize(@FieldType(Operand, @tagName(tag)), op) + 1, }, else => unreachable, - } - } - return total + 1; // Add one for the mask itself. -} - -fn extendedUnionSize(comptime Operand: type, operand: Operand) usize { - const tag = std.meta.activeTag(operand); - inline for (@typeInfo(Operand).@"union".fields) |field| { - if (@field(Operand, field.name) == tag) { - // Add one for the tag itself. - return 1 + operandsSize(field.type, @field(operand, field.name)); - } - } - unreachable; -} - -test "SPIR-V Section emit() - no operands" { - var section = Section{}; - defer section.deinit(std.testing.allocator); - - try section.emit(std.testing.allocator, .OpNop, {}); - - try testing.expect(section.instructions.items[0] == (@as(Word, 1) << 16) | @intFromEnum(Opcode.OpNop)); -} - -test "SPIR-V Section emit() - simple" { - var section = Section{}; - defer section.deinit(std.testing.allocator); - - try section.emit(std.testing.allocator, .OpUndef, .{ - .id_result_type = @enumFromInt(0), - .id_result = @enumFromInt(1), - }); - - try testing.expectEqualSlices(Word, &.{ - (@as(Word, 3) << 16) | @intFromEnum(Opcode.OpUndef), - 0, - 1, - }, section.instructions.items); -} - -test "SPIR-V Section emit() - string" { - var section = Section{}; - defer section.deinit(std.testing.allocator); - - try section.emit(std.testing.allocator, .OpSource, .{ - .source_language = .Unknown, - .version = 123, - .file = @enumFromInt(256), - .source = "pub fn main() void {}", - }); - - try testing.expectEqualSlices(Word, &.{ - (@as(Word, 10) << 16) | @intFromEnum(Opcode.OpSource), - @intFromEnum(spec.SourceLanguage.Unknown), - 123, - 456, - std.mem.bytesToValue(Word, "pub "), - std.mem.bytesToValue(Word, "fn m"), - std.mem.bytesToValue(Word, "ain("), - std.mem.bytesToValue(Word, ") vo"), - std.mem.bytesToValue(Word, "id {"), - std.mem.bytesToValue(Word, "}\x00\x00\x00"), - }, section.instructions.items); -} - -test "SPIR-V Section emit() - extended mask" { - var section = Section{}; - defer section.deinit(std.testing.allocator); - - try section.emit(std.testing.allocator, .OpLoopMerge, .{ - .merge_block = @enumFromInt(10), - .continue_target = @enumFromInt(20), - .loop_control = .{ - .Unroll = true, - .DependencyLength = .{ - .literal_integer = 2, - }, - }, - }); - - try testing.expectEqualSlices(Word, &.{ - (@as(Word, 5) << 16) | @intFromEnum(Opcode.OpLoopMerge), - 10, - 20, - @as(Word, @bitCast(spec.LoopControl{ .Unroll = true, .DependencyLength = true })), - 2, - }, section.instructions.items); -} - -test "SPIR-V Section emit() - extended union" { - var section = Section{}; - defer section.deinit(std.testing.allocator); - - try section.emit(std.testing.allocator, .OpExecutionMode, .{ - .entry_point = @enumFromInt(888), - .mode = .{ - .LocalSize = .{ .x_size = 4, .y_size = 8, .z_size = 16 }, }, - }); - - try testing.expectEqualSlices(Word, &.{ - (@as(Word, 6) << 16) | @intFromEnum(Opcode.OpExecutionMode), - 888, - @intFromEnum(spec.ExecutionMode.LocalSize), - 4, - 8, - 16, - }, section.instructions.items); + }; } diff --git a/src/codegen/spirv/spec.zig b/src/codegen/spirv/spec.zig index a2b18d4db080..9f91320fdca3 100644 --- a/src/codegen/spirv/spec.zig +++ b/src/codegen/spirv/spec.zig @@ -26,6 +26,16 @@ pub const Id = enum(Word) { } }; +pub const IdRange = struct { + base: u32, + len: u32, + + pub fn at(range: IdRange, i: usize) Id { + std.debug.assert(i < range.len); + return @enumFromInt(range.base + i); + } +}; + pub const LiteralInteger = Word; pub const LiteralFloat = Word; pub const LiteralString = []const u8; @@ -181,25 +191,6 @@ pub const OperandKind = enum { pair_id_ref_literal_integer, pair_id_ref_id_ref, tensor_operands, - debug_info_debug_info_flags, - debug_info_debug_base_type_attribute_encoding, - debug_info_debug_composite_type, - debug_info_debug_type_qualifier, - debug_info_debug_operation, - open_cl_debug_info_100_debug_info_flags, - open_cl_debug_info_100_debug_base_type_attribute_encoding, - open_cl_debug_info_100_debug_composite_type, - open_cl_debug_info_100_debug_type_qualifier, - open_cl_debug_info_100_debug_operation, - open_cl_debug_info_100_debug_imported_entity, - non_semantic_clspv_reflection_6_kernel_property_flags, - non_semantic_shader_debug_info_100_debug_info_flags, - non_semantic_shader_debug_info_100_build_identifier_flags, - non_semantic_shader_debug_info_100_debug_base_type_attribute_encoding, - non_semantic_shader_debug_info_100_debug_composite_type, - non_semantic_shader_debug_info_100_debug_type_qualifier, - non_semantic_shader_debug_info_100_debug_operation, - non_semantic_shader_debug_info_100_debug_imported_entity, pub fn category(self: OperandKind) OperandCategory { return switch (self) { @@ -275,25 +266,6 @@ pub const OperandKind = enum { .pair_id_ref_literal_integer => .composite, .pair_id_ref_id_ref => .composite, .tensor_operands => .bit_enum, - .debug_info_debug_info_flags => .bit_enum, - .debug_info_debug_base_type_attribute_encoding => .value_enum, - .debug_info_debug_composite_type => .value_enum, - .debug_info_debug_type_qualifier => .value_enum, - .debug_info_debug_operation => .value_enum, - .open_cl_debug_info_100_debug_info_flags => .bit_enum, - .open_cl_debug_info_100_debug_base_type_attribute_encoding => .value_enum, - .open_cl_debug_info_100_debug_composite_type => .value_enum, - .open_cl_debug_info_100_debug_type_qualifier => .value_enum, - .open_cl_debug_info_100_debug_operation => .value_enum, - .open_cl_debug_info_100_debug_imported_entity => .value_enum, - .non_semantic_clspv_reflection_6_kernel_property_flags => .bit_enum, - .non_semantic_shader_debug_info_100_debug_info_flags => .bit_enum, - .non_semantic_shader_debug_info_100_build_identifier_flags => .bit_enum, - .non_semantic_shader_debug_info_100_debug_base_type_attribute_encoding => .value_enum, - .non_semantic_shader_debug_info_100_debug_composite_type => .value_enum, - .non_semantic_shader_debug_info_100_debug_type_qualifier => .value_enum, - .non_semantic_shader_debug_info_100_debug_operation => .value_enum, - .non_semantic_shader_debug_info_100_debug_imported_entity => .value_enum, }; } pub fn enumerants(self: OperandKind) []const Enumerant { @@ -1465,178 +1437,10 @@ pub const OperandKind = enum { .{ .name = "MakeElementVisibleARM", .value = 0x0008, .parameters = &.{.id_ref} }, .{ .name = "NonPrivateElementARM", .value = 0x0010, .parameters = &.{} }, }, - .debug_info_debug_info_flags => &.{ - .{ .name = "FlagIsProtected", .value = 0x01, .parameters = &.{} }, - .{ .name = "FlagIsPrivate", .value = 0x02, .parameters = &.{} }, - .{ .name = "FlagIsPublic", .value = 0x03, .parameters = &.{} }, - .{ .name = "FlagIsLocal", .value = 0x04, .parameters = &.{} }, - .{ .name = "FlagIsDefinition", .value = 0x08, .parameters = &.{} }, - .{ .name = "FlagFwdDecl", .value = 0x10, .parameters = &.{} }, - .{ .name = "FlagArtificial", .value = 0x20, .parameters = &.{} }, - .{ .name = "FlagExplicit", .value = 0x40, .parameters = &.{} }, - .{ .name = "FlagPrototyped", .value = 0x80, .parameters = &.{} }, - .{ .name = "FlagObjectPointer", .value = 0x100, .parameters = &.{} }, - .{ .name = "FlagStaticMember", .value = 0x200, .parameters = &.{} }, - .{ .name = "FlagIndirectVariable", .value = 0x400, .parameters = &.{} }, - .{ .name = "FlagLValueReference", .value = 0x800, .parameters = &.{} }, - .{ .name = "FlagRValueReference", .value = 0x1000, .parameters = &.{} }, - .{ .name = "FlagIsOptimized", .value = 0x2000, .parameters = &.{} }, - }, - .debug_info_debug_base_type_attribute_encoding => &.{ - .{ .name = "Unspecified", .value = 0, .parameters = &.{} }, - .{ .name = "Address", .value = 1, .parameters = &.{} }, - .{ .name = "Boolean", .value = 2, .parameters = &.{} }, - .{ .name = "Float", .value = 4, .parameters = &.{} }, - .{ .name = "Signed", .value = 5, .parameters = &.{} }, - .{ .name = "SignedChar", .value = 6, .parameters = &.{} }, - .{ .name = "Unsigned", .value = 7, .parameters = &.{} }, - .{ .name = "UnsignedChar", .value = 8, .parameters = &.{} }, - }, - .debug_info_debug_composite_type => &.{ - .{ .name = "Class", .value = 0, .parameters = &.{} }, - .{ .name = "Structure", .value = 1, .parameters = &.{} }, - .{ .name = "Union", .value = 2, .parameters = &.{} }, - }, - .debug_info_debug_type_qualifier => &.{ - .{ .name = "ConstType", .value = 0, .parameters = &.{} }, - .{ .name = "VolatileType", .value = 1, .parameters = &.{} }, - .{ .name = "RestrictType", .value = 2, .parameters = &.{} }, - }, - .debug_info_debug_operation => &.{ - .{ .name = "Deref", .value = 0, .parameters = &.{} }, - .{ .name = "Plus", .value = 1, .parameters = &.{} }, - .{ .name = "Minus", .value = 2, .parameters = &.{} }, - .{ .name = "PlusUconst", .value = 3, .parameters = &.{.literal_integer} }, - .{ .name = "BitPiece", .value = 4, .parameters = &.{ .literal_integer, .literal_integer } }, - .{ .name = "Swap", .value = 5, .parameters = &.{} }, - .{ .name = "Xderef", .value = 6, .parameters = &.{} }, - .{ .name = "StackValue", .value = 7, .parameters = &.{} }, - .{ .name = "Constu", .value = 8, .parameters = &.{.literal_integer} }, - }, - .open_cl_debug_info_100_debug_info_flags => &.{ - .{ .name = "FlagIsProtected", .value = 0x01, .parameters = &.{} }, - .{ .name = "FlagIsPrivate", .value = 0x02, .parameters = &.{} }, - .{ .name = "FlagIsPublic", .value = 0x03, .parameters = &.{} }, - .{ .name = "FlagIsLocal", .value = 0x04, .parameters = &.{} }, - .{ .name = "FlagIsDefinition", .value = 0x08, .parameters = &.{} }, - .{ .name = "FlagFwdDecl", .value = 0x10, .parameters = &.{} }, - .{ .name = "FlagArtificial", .value = 0x20, .parameters = &.{} }, - .{ .name = "FlagExplicit", .value = 0x40, .parameters = &.{} }, - .{ .name = "FlagPrototyped", .value = 0x80, .parameters = &.{} }, - .{ .name = "FlagObjectPointer", .value = 0x100, .parameters = &.{} }, - .{ .name = "FlagStaticMember", .value = 0x200, .parameters = &.{} }, - .{ .name = "FlagIndirectVariable", .value = 0x400, .parameters = &.{} }, - .{ .name = "FlagLValueReference", .value = 0x800, .parameters = &.{} }, - .{ .name = "FlagRValueReference", .value = 0x1000, .parameters = &.{} }, - .{ .name = "FlagIsOptimized", .value = 0x2000, .parameters = &.{} }, - .{ .name = "FlagIsEnumClass", .value = 0x4000, .parameters = &.{} }, - .{ .name = "FlagTypePassByValue", .value = 0x8000, .parameters = &.{} }, - .{ .name = "FlagTypePassByReference", .value = 0x10000, .parameters = &.{} }, - }, - .open_cl_debug_info_100_debug_base_type_attribute_encoding => &.{ - .{ .name = "Unspecified", .value = 0, .parameters = &.{} }, - .{ .name = "Address", .value = 1, .parameters = &.{} }, - .{ .name = "Boolean", .value = 2, .parameters = &.{} }, - .{ .name = "Float", .value = 3, .parameters = &.{} }, - .{ .name = "Signed", .value = 4, .parameters = &.{} }, - .{ .name = "SignedChar", .value = 5, .parameters = &.{} }, - .{ .name = "Unsigned", .value = 6, .parameters = &.{} }, - .{ .name = "UnsignedChar", .value = 7, .parameters = &.{} }, - }, - .open_cl_debug_info_100_debug_composite_type => &.{ - .{ .name = "Class", .value = 0, .parameters = &.{} }, - .{ .name = "Structure", .value = 1, .parameters = &.{} }, - .{ .name = "Union", .value = 2, .parameters = &.{} }, - }, - .open_cl_debug_info_100_debug_type_qualifier => &.{ - .{ .name = "ConstType", .value = 0, .parameters = &.{} }, - .{ .name = "VolatileType", .value = 1, .parameters = &.{} }, - .{ .name = "RestrictType", .value = 2, .parameters = &.{} }, - .{ .name = "AtomicType", .value = 3, .parameters = &.{} }, - }, - .open_cl_debug_info_100_debug_operation => &.{ - .{ .name = "Deref", .value = 0, .parameters = &.{} }, - .{ .name = "Plus", .value = 1, .parameters = &.{} }, - .{ .name = "Minus", .value = 2, .parameters = &.{} }, - .{ .name = "PlusUconst", .value = 3, .parameters = &.{.literal_integer} }, - .{ .name = "BitPiece", .value = 4, .parameters = &.{ .literal_integer, .literal_integer } }, - .{ .name = "Swap", .value = 5, .parameters = &.{} }, - .{ .name = "Xderef", .value = 6, .parameters = &.{} }, - .{ .name = "StackValue", .value = 7, .parameters = &.{} }, - .{ .name = "Constu", .value = 8, .parameters = &.{.literal_integer} }, - .{ .name = "Fragment", .value = 9, .parameters = &.{ .literal_integer, .literal_integer } }, - }, - .open_cl_debug_info_100_debug_imported_entity => &.{ - .{ .name = "ImportedModule", .value = 0, .parameters = &.{} }, - .{ .name = "ImportedDeclaration", .value = 1, .parameters = &.{} }, - }, - .non_semantic_clspv_reflection_6_kernel_property_flags => &.{ - .{ .name = "MayUsePrintf", .value = 0x1, .parameters = &.{} }, - }, - .non_semantic_shader_debug_info_100_debug_info_flags => &.{ - .{ .name = "FlagIsProtected", .value = 0x01, .parameters = &.{} }, - .{ .name = "FlagIsPrivate", .value = 0x02, .parameters = &.{} }, - .{ .name = "FlagIsPublic", .value = 0x03, .parameters = &.{} }, - .{ .name = "FlagIsLocal", .value = 0x04, .parameters = &.{} }, - .{ .name = "FlagIsDefinition", .value = 0x08, .parameters = &.{} }, - .{ .name = "FlagFwdDecl", .value = 0x10, .parameters = &.{} }, - .{ .name = "FlagArtificial", .value = 0x20, .parameters = &.{} }, - .{ .name = "FlagExplicit", .value = 0x40, .parameters = &.{} }, - .{ .name = "FlagPrototyped", .value = 0x80, .parameters = &.{} }, - .{ .name = "FlagObjectPointer", .value = 0x100, .parameters = &.{} }, - .{ .name = "FlagStaticMember", .value = 0x200, .parameters = &.{} }, - .{ .name = "FlagIndirectVariable", .value = 0x400, .parameters = &.{} }, - .{ .name = "FlagLValueReference", .value = 0x800, .parameters = &.{} }, - .{ .name = "FlagRValueReference", .value = 0x1000, .parameters = &.{} }, - .{ .name = "FlagIsOptimized", .value = 0x2000, .parameters = &.{} }, - .{ .name = "FlagIsEnumClass", .value = 0x4000, .parameters = &.{} }, - .{ .name = "FlagTypePassByValue", .value = 0x8000, .parameters = &.{} }, - .{ .name = "FlagTypePassByReference", .value = 0x10000, .parameters = &.{} }, - .{ .name = "FlagUnknownPhysicalLayout", .value = 0x20000, .parameters = &.{} }, - }, - .non_semantic_shader_debug_info_100_build_identifier_flags => &.{ - .{ .name = "IdentifierPossibleDuplicates", .value = 0x01, .parameters = &.{} }, - }, - .non_semantic_shader_debug_info_100_debug_base_type_attribute_encoding => &.{ - .{ .name = "Unspecified", .value = 0, .parameters = &.{} }, - .{ .name = "Address", .value = 1, .parameters = &.{} }, - .{ .name = "Boolean", .value = 2, .parameters = &.{} }, - .{ .name = "Float", .value = 3, .parameters = &.{} }, - .{ .name = "Signed", .value = 4, .parameters = &.{} }, - .{ .name = "SignedChar", .value = 5, .parameters = &.{} }, - .{ .name = "Unsigned", .value = 6, .parameters = &.{} }, - .{ .name = "UnsignedChar", .value = 7, .parameters = &.{} }, - }, - .non_semantic_shader_debug_info_100_debug_composite_type => &.{ - .{ .name = "Class", .value = 0, .parameters = &.{} }, - .{ .name = "Structure", .value = 1, .parameters = &.{} }, - .{ .name = "Union", .value = 2, .parameters = &.{} }, - }, - .non_semantic_shader_debug_info_100_debug_type_qualifier => &.{ - .{ .name = "ConstType", .value = 0, .parameters = &.{} }, - .{ .name = "VolatileType", .value = 1, .parameters = &.{} }, - .{ .name = "RestrictType", .value = 2, .parameters = &.{} }, - .{ .name = "AtomicType", .value = 3, .parameters = &.{} }, - }, - .non_semantic_shader_debug_info_100_debug_operation => &.{ - .{ .name = "Deref", .value = 0, .parameters = &.{} }, - .{ .name = "Plus", .value = 1, .parameters = &.{} }, - .{ .name = "Minus", .value = 2, .parameters = &.{} }, - .{ .name = "PlusUconst", .value = 3, .parameters = &.{.id_ref} }, - .{ .name = "BitPiece", .value = 4, .parameters = &.{ .id_ref, .id_ref } }, - .{ .name = "Swap", .value = 5, .parameters = &.{} }, - .{ .name = "Xderef", .value = 6, .parameters = &.{} }, - .{ .name = "StackValue", .value = 7, .parameters = &.{} }, - .{ .name = "Constu", .value = 8, .parameters = &.{.id_ref} }, - .{ .name = "Fragment", .value = 9, .parameters = &.{ .id_ref, .id_ref } }, - }, - .non_semantic_shader_debug_info_100_debug_imported_entity => &.{ - .{ .name = "ImportedModule", .value = 0, .parameters = &.{} }, - .{ .name = "ImportedDeclaration", .value = 1, .parameters = &.{} }, - }, }; } }; + pub const Opcode = enum(u16) { OpNop = 0, OpUndef = 1, @@ -3523,6 +3327,259 @@ pub const Opcode = enum(u16) { }; } }; + +pub const GlslOpcode = enum(u16) { + Round = 1, + RoundEven = 2, + Trunc = 3, + FAbs = 4, + SAbs = 5, + FSign = 6, + SSign = 7, + Floor = 8, + Ceil = 9, + Fract = 10, + Radians = 11, + Degrees = 12, + Sin = 13, + Cos = 14, + Tan = 15, + Asin = 16, + Acos = 17, + Atan = 18, + Sinh = 19, + Cosh = 20, + Tanh = 21, + Asinh = 22, + Acosh = 23, + Atanh = 24, + Atan2 = 25, + Pow = 26, + Exp = 27, + Log = 28, + Exp2 = 29, + Log2 = 30, + Sqrt = 31, + InverseSqrt = 32, + Determinant = 33, + MatrixInverse = 34, + Modf = 35, + ModfStruct = 36, + FMin = 37, + UMin = 38, + SMin = 39, + FMax = 40, + UMax = 41, + SMax = 42, + FClamp = 43, + UClamp = 44, + SClamp = 45, + FMix = 46, + IMix = 47, + Step = 48, + SmoothStep = 49, + Fma = 50, + Frexp = 51, + FrexpStruct = 52, + Ldexp = 53, + PackSnorm4x8 = 54, + PackUnorm4x8 = 55, + PackSnorm2x16 = 56, + PackUnorm2x16 = 57, + PackHalf2x16 = 58, + PackDouble2x32 = 59, + UnpackSnorm2x16 = 60, + UnpackUnorm2x16 = 61, + UnpackHalf2x16 = 62, + UnpackSnorm4x8 = 63, + UnpackUnorm4x8 = 64, + UnpackDouble2x32 = 65, + Length = 66, + Distance = 67, + Cross = 68, + Normalize = 69, + FaceForward = 70, + Reflect = 71, + Refract = 72, + FindILsb = 73, + FindSMsb = 74, + FindUMsb = 75, + InterpolateAtCentroid = 76, + InterpolateAtSample = 77, + InterpolateAtOffset = 78, + NMin = 79, + NMax = 80, + NClamp = 81, +}; + +pub const OpenClOpcode = enum(u16) { + acos = 0, + acosh = 1, + acospi = 2, + asin = 3, + asinh = 4, + asinpi = 5, + atan = 6, + atan2 = 7, + atanh = 8, + atanpi = 9, + atan2pi = 10, + cbrt = 11, + ceil = 12, + copysign = 13, + cos = 14, + cosh = 15, + cospi = 16, + erfc = 17, + erf = 18, + exp = 19, + exp2 = 20, + exp10 = 21, + expm1 = 22, + fabs = 23, + fdim = 24, + floor = 25, + fma = 26, + fmax = 27, + fmin = 28, + fmod = 29, + fract = 30, + frexp = 31, + hypot = 32, + ilogb = 33, + ldexp = 34, + lgamma = 35, + lgamma_r = 36, + log = 37, + log2 = 38, + log10 = 39, + log1p = 40, + logb = 41, + mad = 42, + maxmag = 43, + minmag = 44, + modf = 45, + nan = 46, + nextafter = 47, + pow = 48, + pown = 49, + powr = 50, + remainder = 51, + remquo = 52, + rint = 53, + rootn = 54, + round = 55, + rsqrt = 56, + sin = 57, + sincos = 58, + sinh = 59, + sinpi = 60, + sqrt = 61, + tan = 62, + tanh = 63, + tanpi = 64, + tgamma = 65, + trunc = 66, + half_cos = 67, + half_divide = 68, + half_exp = 69, + half_exp2 = 70, + half_exp10 = 71, + half_log = 72, + half_log2 = 73, + half_log10 = 74, + half_powr = 75, + half_recip = 76, + half_rsqrt = 77, + half_sin = 78, + half_sqrt = 79, + half_tan = 80, + native_cos = 81, + native_divide = 82, + native_exp = 83, + native_exp2 = 84, + native_exp10 = 85, + native_log = 86, + native_log2 = 87, + native_log10 = 88, + native_powr = 89, + native_recip = 90, + native_rsqrt = 91, + native_sin = 92, + native_sqrt = 93, + native_tan = 94, + fclamp = 95, + degrees = 96, + fmax_common = 97, + fmin_common = 98, + mix = 99, + radians = 100, + step = 101, + smoothstep = 102, + sign = 103, + cross = 104, + distance = 105, + length = 106, + normalize = 107, + fast_distance = 108, + fast_length = 109, + fast_normalize = 110, + s_abs = 141, + s_abs_diff = 142, + s_add_sat = 143, + u_add_sat = 144, + s_hadd = 145, + u_hadd = 146, + s_rhadd = 147, + u_rhadd = 148, + s_clamp = 149, + u_clamp = 150, + clz = 151, + ctz = 152, + s_mad_hi = 153, + u_mad_sat = 154, + s_mad_sat = 155, + s_max = 156, + u_max = 157, + s_min = 158, + u_min = 159, + s_mul_hi = 160, + rotate = 161, + s_sub_sat = 162, + u_sub_sat = 163, + u_upsample = 164, + s_upsample = 165, + popcount = 166, + s_mad24 = 167, + u_mad24 = 168, + s_mul24 = 169, + u_mul24 = 170, + vloadn = 171, + vstoren = 172, + vload_half = 173, + vload_halfn = 174, + vstore_half = 175, + vstore_half_r = 176, + vstore_halfn = 177, + vstore_halfn_r = 178, + vloada_halfn = 179, + vstorea_halfn = 180, + vstorea_halfn_r = 181, + shuffle = 182, + shuffle2 = 183, + printf = 184, + prefetch = 185, + bitselect = 186, + select = 187, + u_abs = 201, + u_abs_diff = 202, + u_mul_hi = 203, + u_mad_hi = 204, +}; + +pub const Zig = enum(u16) { + InvocationGlobal = 0, +}; pub const ImageOperands = packed struct { bias: bool = false, lod: bool = false, @@ -5484,335 +5541,10 @@ pub const TensorOperands = packed struct { _reserved_bit_31: bool = false, }; }; -pub const @"DebugInfo.DebugInfoFlags" = packed struct { - flag_is_protected: bool = false, - flag_is_private: bool = false, - flag_is_local: bool = false, - flag_is_definition: bool = false, - flag_fwd_decl: bool = false, - flag_artificial: bool = false, - flag_explicit: bool = false, - flag_prototyped: bool = false, - flag_object_pointer: bool = false, - flag_static_member: bool = false, - flag_indirect_variable: bool = false, - flag_l_value_reference: bool = false, - flag_r_value_reference: bool = false, - flag_is_optimized: bool = false, - _reserved_bit_14: bool = false, - _reserved_bit_15: bool = false, - _reserved_bit_16: bool = false, - _reserved_bit_17: bool = false, - _reserved_bit_18: bool = false, - _reserved_bit_19: bool = false, - _reserved_bit_20: bool = false, - _reserved_bit_21: bool = false, - _reserved_bit_22: bool = false, - _reserved_bit_23: bool = false, - _reserved_bit_24: bool = false, - _reserved_bit_25: bool = false, - _reserved_bit_26: bool = false, - _reserved_bit_27: bool = false, - _reserved_bit_28: bool = false, - _reserved_bit_29: bool = false, - _reserved_bit_30: bool = false, - _reserved_bit_31: bool = false, -}; -pub const @"DebugInfo.DebugBaseTypeAttributeEncoding" = enum(u32) { - unspecified = 0, - address = 1, - boolean = 2, - float = 4, - signed = 5, - signed_char = 6, - unsigned = 7, - unsigned_char = 8, -}; -pub const @"DebugInfo.DebugCompositeType" = enum(u32) { - class = 0, - structure = 1, - @"union" = 2, -}; -pub const @"DebugInfo.DebugTypeQualifier" = enum(u32) { - const_type = 0, - volatile_type = 1, - restrict_type = 2, -}; -pub const @"DebugInfo.DebugOperation" = enum(u32) { - deref = 0, - plus = 1, - minus = 2, - plus_uconst = 3, - bit_piece = 4, - swap = 5, - xderef = 6, - stack_value = 7, - constu = 8, - - pub const Extended = union(@"DebugInfo.DebugOperation") { - deref, - plus, - minus, - plus_uconst: struct { literal_integer: LiteralInteger }, - bit_piece: struct { literal_integer_0: LiteralInteger, literal_integer_1: LiteralInteger }, - swap, - xderef, - stack_value, - constu: struct { literal_integer: LiteralInteger }, - }; -}; -pub const @"OpenCL.DebugInfo.100.DebugInfoFlags" = packed struct { - flag_is_protected: bool = false, - flag_is_private: bool = false, - flag_is_local: bool = false, - flag_is_definition: bool = false, - flag_fwd_decl: bool = false, - flag_artificial: bool = false, - flag_explicit: bool = false, - flag_prototyped: bool = false, - flag_object_pointer: bool = false, - flag_static_member: bool = false, - flag_indirect_variable: bool = false, - flag_l_value_reference: bool = false, - flag_r_value_reference: bool = false, - flag_is_optimized: bool = false, - flag_is_enum_class: bool = false, - flag_type_pass_by_value: bool = false, - flag_type_pass_by_reference: bool = false, - _reserved_bit_17: bool = false, - _reserved_bit_18: bool = false, - _reserved_bit_19: bool = false, - _reserved_bit_20: bool = false, - _reserved_bit_21: bool = false, - _reserved_bit_22: bool = false, - _reserved_bit_23: bool = false, - _reserved_bit_24: bool = false, - _reserved_bit_25: bool = false, - _reserved_bit_26: bool = false, - _reserved_bit_27: bool = false, - _reserved_bit_28: bool = false, - _reserved_bit_29: bool = false, - _reserved_bit_30: bool = false, - _reserved_bit_31: bool = false, -}; -pub const @"OpenCL.DebugInfo.100.DebugBaseTypeAttributeEncoding" = enum(u32) { - unspecified = 0, - address = 1, - boolean = 2, - float = 3, - signed = 4, - signed_char = 5, - unsigned = 6, - unsigned_char = 7, -}; -pub const @"OpenCL.DebugInfo.100.DebugCompositeType" = enum(u32) { - class = 0, - structure = 1, - @"union" = 2, -}; -pub const @"OpenCL.DebugInfo.100.DebugTypeQualifier" = enum(u32) { - const_type = 0, - volatile_type = 1, - restrict_type = 2, - atomic_type = 3, -}; -pub const @"OpenCL.DebugInfo.100.DebugOperation" = enum(u32) { - deref = 0, - plus = 1, - minus = 2, - plus_uconst = 3, - bit_piece = 4, - swap = 5, - xderef = 6, - stack_value = 7, - constu = 8, - fragment = 9, - - pub const Extended = union(@"OpenCL.DebugInfo.100.DebugOperation") { - deref, - plus, - minus, - plus_uconst: struct { literal_integer: LiteralInteger }, - bit_piece: struct { literal_integer_0: LiteralInteger, literal_integer_1: LiteralInteger }, - swap, - xderef, - stack_value, - constu: struct { literal_integer: LiteralInteger }, - fragment: struct { literal_integer_0: LiteralInteger, literal_integer_1: LiteralInteger }, - }; -}; -pub const @"OpenCL.DebugInfo.100.DebugImportedEntity" = enum(u32) { - imported_module = 0, - imported_declaration = 1, -}; -pub const @"NonSemantic.ClspvReflection.6.KernelPropertyFlags" = packed struct { - may_use_printf: bool = false, - _reserved_bit_1: bool = false, - _reserved_bit_2: bool = false, - _reserved_bit_3: bool = false, - _reserved_bit_4: bool = false, - _reserved_bit_5: bool = false, - _reserved_bit_6: bool = false, - _reserved_bit_7: bool = false, - _reserved_bit_8: bool = false, - _reserved_bit_9: bool = false, - _reserved_bit_10: bool = false, - _reserved_bit_11: bool = false, - _reserved_bit_12: bool = false, - _reserved_bit_13: bool = false, - _reserved_bit_14: bool = false, - _reserved_bit_15: bool = false, - _reserved_bit_16: bool = false, - _reserved_bit_17: bool = false, - _reserved_bit_18: bool = false, - _reserved_bit_19: bool = false, - _reserved_bit_20: bool = false, - _reserved_bit_21: bool = false, - _reserved_bit_22: bool = false, - _reserved_bit_23: bool = false, - _reserved_bit_24: bool = false, - _reserved_bit_25: bool = false, - _reserved_bit_26: bool = false, - _reserved_bit_27: bool = false, - _reserved_bit_28: bool = false, - _reserved_bit_29: bool = false, - _reserved_bit_30: bool = false, - _reserved_bit_31: bool = false, -}; -pub const @"NonSemantic.Shader.DebugInfo.100.DebugInfoFlags" = packed struct { - flag_is_protected: bool = false, - flag_is_private: bool = false, - flag_is_local: bool = false, - flag_is_definition: bool = false, - flag_fwd_decl: bool = false, - flag_artificial: bool = false, - flag_explicit: bool = false, - flag_prototyped: bool = false, - flag_object_pointer: bool = false, - flag_static_member: bool = false, - flag_indirect_variable: bool = false, - flag_l_value_reference: bool = false, - flag_r_value_reference: bool = false, - flag_is_optimized: bool = false, - flag_is_enum_class: bool = false, - flag_type_pass_by_value: bool = false, - flag_type_pass_by_reference: bool = false, - flag_unknown_physical_layout: bool = false, - _reserved_bit_18: bool = false, - _reserved_bit_19: bool = false, - _reserved_bit_20: bool = false, - _reserved_bit_21: bool = false, - _reserved_bit_22: bool = false, - _reserved_bit_23: bool = false, - _reserved_bit_24: bool = false, - _reserved_bit_25: bool = false, - _reserved_bit_26: bool = false, - _reserved_bit_27: bool = false, - _reserved_bit_28: bool = false, - _reserved_bit_29: bool = false, - _reserved_bit_30: bool = false, - _reserved_bit_31: bool = false, -}; -pub const @"NonSemantic.Shader.DebugInfo.100.BuildIdentifierFlags" = packed struct { - identifier_possible_duplicates: bool = false, - _reserved_bit_1: bool = false, - _reserved_bit_2: bool = false, - _reserved_bit_3: bool = false, - _reserved_bit_4: bool = false, - _reserved_bit_5: bool = false, - _reserved_bit_6: bool = false, - _reserved_bit_7: bool = false, - _reserved_bit_8: bool = false, - _reserved_bit_9: bool = false, - _reserved_bit_10: bool = false, - _reserved_bit_11: bool = false, - _reserved_bit_12: bool = false, - _reserved_bit_13: bool = false, - _reserved_bit_14: bool = false, - _reserved_bit_15: bool = false, - _reserved_bit_16: bool = false, - _reserved_bit_17: bool = false, - _reserved_bit_18: bool = false, - _reserved_bit_19: bool = false, - _reserved_bit_20: bool = false, - _reserved_bit_21: bool = false, - _reserved_bit_22: bool = false, - _reserved_bit_23: bool = false, - _reserved_bit_24: bool = false, - _reserved_bit_25: bool = false, - _reserved_bit_26: bool = false, - _reserved_bit_27: bool = false, - _reserved_bit_28: bool = false, - _reserved_bit_29: bool = false, - _reserved_bit_30: bool = false, - _reserved_bit_31: bool = false, -}; -pub const @"NonSemantic.Shader.DebugInfo.100.DebugBaseTypeAttributeEncoding" = enum(u32) { - unspecified = 0, - address = 1, - boolean = 2, - float = 3, - signed = 4, - signed_char = 5, - unsigned = 6, - unsigned_char = 7, -}; -pub const @"NonSemantic.Shader.DebugInfo.100.DebugCompositeType" = enum(u32) { - class = 0, - structure = 1, - @"union" = 2, -}; -pub const @"NonSemantic.Shader.DebugInfo.100.DebugTypeQualifier" = enum(u32) { - const_type = 0, - volatile_type = 1, - restrict_type = 2, - atomic_type = 3, -}; -pub const @"NonSemantic.Shader.DebugInfo.100.DebugOperation" = enum(u32) { - deref = 0, - plus = 1, - minus = 2, - plus_uconst = 3, - bit_piece = 4, - swap = 5, - xderef = 6, - stack_value = 7, - constu = 8, - fragment = 9, - - pub const Extended = union(@"NonSemantic.Shader.DebugInfo.100.DebugOperation") { - deref, - plus, - minus, - plus_uconst: struct { id_ref: Id }, - bit_piece: struct { id_ref_0: Id, id_ref_1: Id }, - swap, - xderef, - stack_value, - constu: struct { id_ref: Id }, - fragment: struct { id_ref_0: Id, id_ref_1: Id }, - }; -}; -pub const @"NonSemantic.Shader.DebugInfo.100.DebugImportedEntity" = enum(u32) { - imported_module = 0, - imported_declaration = 1, -}; pub const InstructionSet = enum { core, - spv_amd_shader_trinary_minmax, - spv_ext_inst_type_tosa_001000_1, - non_semantic_vksp_reflection, - spv_amd_shader_explicit_vertex_parameter, - debug_info, - non_semantic_debug_break, - open_cl_debug_info_100, - non_semantic_clspv_reflection_6, - glsl_std_450, - spv_amd_shader_ballot, - non_semantic_debug_printf, - spv_amd_gcn_shader, - open_cl_std, - non_semantic_shader_debug_info_100, + @"GLSL.std.450", + @"OpenCL.std", zig, pub fn instructions(self: InstructionSet) []const Instruction { @@ -14078,480 +13810,321 @@ pub const InstructionSet = enum { }, }, }, - .spv_amd_shader_trinary_minmax => &.{ + .@"GLSL.std.450" => &.{ .{ - .name = "FMin3AMD", + .name = "Round", .opcode = 1, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "UMin3AMD", + .name = "RoundEven", .opcode = 2, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SMin3AMD", + .name = "Trunc", .opcode = 3, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "FMax3AMD", + .name = "FAbs", .opcode = 4, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "UMax3AMD", + .name = "SAbs", .opcode = 5, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SMax3AMD", + .name = "FSign", .opcode = 6, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "FMid3AMD", + .name = "SSign", .opcode = 7, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "UMid3AMD", + .name = "Floor", .opcode = 8, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SMid3AMD", + .name = "Ceil", .opcode = 9, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, - }, - .spv_ext_inst_type_tosa_001000_1 => &.{ .{ - .name = "ARGMAX", - .opcode = 0, + .name = "Fract", + .opcode = 10, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "AVG_POOL2D", - .opcode = 1, + .name = "Radians", + .opcode = 11, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "CONV2D", - .opcode = 2, + .name = "Degrees", + .opcode = 12, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "CONV3D", - .opcode = 3, + .name = "Sin", + .opcode = 13, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DEPTHWISE_CONV2D", - .opcode = 4, + .name = "Cos", + .opcode = 14, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "FFT2D", - .opcode = 5, + .name = "Tan", + .opcode = 15, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "MATMUL", - .opcode = 6, + .name = "Asin", + .opcode = 16, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "MAX_POOL2D", - .opcode = 7, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "RFFT2D", - .opcode = 8, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "TRANSPOSE_CONV2D", - .opcode = 9, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "CLAMP", - .opcode = 10, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ERF", - .opcode = 11, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SIGMOID", - .opcode = 12, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "TANH", - .opcode = 13, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ADD", - .opcode = 14, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ARITHMETIC_RIGHT_SHIFT", - .opcode = 15, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "BITWISE_AND", - .opcode = 16, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "BITWISE_OR", + .name = "Acos", .opcode = 17, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "BITWISE_XOR", + .name = "Atan", .opcode = 18, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "INTDIV", + .name = "Sinh", .opcode = 19, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOGICAL_AND", + .name = "Cosh", .opcode = 20, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOGICAL_LEFT_SHIFT", + .name = "Tanh", .opcode = 21, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOGICAL_RIGHT_SHIFT", + .name = "Asinh", .opcode = 22, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOGICAL_OR", + .name = "Acosh", .opcode = 23, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOGICAL_XOR", + .name = "Atanh", .opcode = 24, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "MAXIMUM", + .name = "Atan2", .opcode = 25, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "MINIMUM", + .name = "Pow", .opcode = 26, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "MUL", + .name = "Exp", .opcode = 27, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "POW", + .name = "Log", .opcode = 28, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SUB", + .name = "Exp2", .opcode = 29, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "TABLE", + .name = "Log2", .opcode = 30, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "ABS", + .name = "Sqrt", .opcode = 31, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "BITWISE_NOT", + .name = "InverseSqrt", .opcode = 32, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "CEIL", + .name = "Determinant", .opcode = 33, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "CLZ", + .name = "MatrixInverse", .opcode = 34, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "COS", + .name = "Modf", .opcode = 35, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "EXP", + .name = "ModfStruct", .opcode = 36, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "FLOOR", + .name = "FMin", .opcode = 37, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOG", + .name = "UMin", .opcode = 38, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "LOGICAL_NOT", + .name = "SMin", .opcode = 39, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "NEGATE", + .name = "FMax", .opcode = 40, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "RECIPROCAL", + .name = "UMax", .opcode = 41, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "RSQRT", + .name = "SMax", .opcode = 42, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SIN", + .name = "FClamp", .opcode = 43, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SELECT", + .name = "UClamp", .opcode = 44, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, @@ -14560,31 +14133,34 @@ pub const InstructionSet = enum { }, }, .{ - .name = "EQUAL", + .name = "SClamp", .opcode = 45, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "GREATER", + .name = "FMix", .opcode = 46, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "GREATER_EQUAL", + .name = "IMix", .opcode = 47, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "REDUCE_ALL", + .name = "Step", .opcode = 48, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, @@ -14592,15 +14168,16 @@ pub const InstructionSet = enum { }, }, .{ - .name = "REDUCE_ANY", + .name = "SmoothStep", .opcode = 49, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "REDUCE_MAX", + .name = "Fma", .opcode = 50, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, @@ -14609,24 +14186,22 @@ pub const InstructionSet = enum { }, }, .{ - .name = "REDUCE_MIN", + .name = "Frexp", .opcode = 51, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "REDUCE_PRODUCT", + .name = "FrexpStruct", .opcode = 52, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "REDUCE_SUM", + .name = "Ldexp", .opcode = 53, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, @@ -14634,3368 +14209,1112 @@ pub const InstructionSet = enum { }, }, .{ - .name = "CONCAT", + .name = "PackSnorm4x8", .opcode = 54, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "PAD", + .name = "PackUnorm4x8", .opcode = 55, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "RESHAPE", + .name = "PackSnorm2x16", .opcode = 56, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "REVERSE", + .name = "PackUnorm2x16", .opcode = 57, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SLICE", + .name = "PackHalf2x16", .opcode = 58, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "TILE", + .name = "PackDouble2x32", .opcode = 59, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "TRANSPOSE", + .name = "UnpackSnorm2x16", .opcode = 60, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "GATHER", + .name = "UnpackUnorm2x16", .opcode = 61, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SCATTER", + .name = "UnpackHalf2x16", .opcode = 62, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "RESIZE", + .name = "UnpackSnorm4x8", .opcode = 63, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "CAST", + .name = "UnpackUnorm4x8", .opcode = 64, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "RESCALE", + .name = "UnpackDouble2x32", .opcode = 65, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "Length", + .opcode = 66, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "Distance", + .opcode = 67, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "Cross", + .opcode = 68, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "Normalize", + .opcode = 69, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "FaceForward", + .opcode = 70, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, - }, - .non_semantic_vksp_reflection => &.{ .{ - .name = "Configuration", - .opcode = 1, + .name = "Reflect", + .opcode = 71, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "Refract", + .opcode = 72, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "FindILsb", + .opcode = 73, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "FindSMsb", + .opcode = 74, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "StartCounter", - .opcode = 2, + .name = "FindUMsb", + .opcode = 75, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "StopCounter", - .opcode = 3, + .name = "InterpolateAtCentroid", + .opcode = 76, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "PushConstants", - .opcode = 4, + .name = "InterpolateAtSample", + .opcode = 77, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "InterpolateAtOffset", + .opcode = 78, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "SpecializationMapEntry", - .opcode = 5, + .name = "NMin", + .opcode = 79, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DescriptorSetBuffer", - .opcode = 6, + .name = "NMax", + .opcode = 80, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DescriptorSetImage", - .opcode = 7, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DescriptorSetSampler", - .opcode = 8, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "NClamp", + .opcode = 81, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, }, - .spv_amd_shader_explicit_vertex_parameter => &.{ + .@"OpenCL.std" => &.{ .{ - .name = "InterpolateAtVertexAMD", - .opcode = 1, + .name = "acos", + .opcode = 0, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, - }, - .debug_info => &.{ - .{ - .name = "DebugInfoNone", - .opcode = 0, - .operands = &.{}, - }, .{ - .name = "DebugCompilationUnit", + .name = "acosh", .opcode = 1, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "DebugTypeBasic", + .name = "acospi", .opcode = 2, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_base_type_attribute_encoding, .quantifier = .required }, }, }, .{ - .name = "DebugTypePointer", + .name = "asin", .opcode = 3, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .storage_class, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, }, }, .{ - .name = "DebugTypeQualifier", + .name = "asinh", .opcode = 4, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_type_qualifier, .quantifier = .required }, }, }, .{ - .name = "DebugTypeArray", + .name = "asinpi", .opcode = 5, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeVector", + .name = "atan", .opcode = 6, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "DebugTypedef", + .name = "atan2", .opcode = 7, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeFunction", + .name = "atanh", .opcode = 8, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeEnum", + .name = "atanpi", .opcode = 9, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - .{ .kind = .pair_id_ref_id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeComposite", + .name = "atan2pi", .opcode = 10, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_composite_type, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeMember", - .opcode = 11, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugTypeInheritance", - .opcode = 12, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypePtrToMember", - .opcode = 13, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeTemplate", - .opcode = 14, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeTemplateParameter", - .opcode = 15, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeTemplateTemplateParameter", - .opcode = 16, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeTemplateParameterPack", - .opcode = 17, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugGlobalVariable", - .opcode = 18, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugFunctionDeclaration", - .opcode = 19, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - }, - }, - .{ - .name = "DebugFunction", - .opcode = 20, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .debug_info_debug_info_flags, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugLexicalBlock", - .opcode = 21, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugLexicalBlockDiscriminator", - .opcode = 22, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugScope", - .opcode = 23, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugNoScope", - .opcode = 24, - .operands = &.{}, - }, - .{ - .name = "DebugInlinedAt", - .opcode = 25, - .operands = &.{ - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugLocalVariable", - .opcode = 26, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugInlinedVariable", - .opcode = 27, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugDeclare", - .opcode = 28, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugValue", - .opcode = 29, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugOperation", - .opcode = 30, - .operands = &.{ - .{ .kind = .debug_info_debug_operation, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugExpression", - .opcode = 31, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugMacroDef", - .opcode = 32, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugMacroUndef", - .opcode = 33, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - }, - .non_semantic_debug_break => &.{ - .{ - .name = "DebugBreak", - .opcode = 1, - .operands = &.{}, - }, - }, - .open_cl_debug_info_100 => &.{ - .{ - .name = "DebugInfoNone", - .opcode = 0, - .operands = &.{}, - }, - .{ - .name = "DebugCompilationUnit", - .opcode = 1, - .operands = &.{ - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .source_language, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeBasic", - .opcode = 2, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_base_type_attribute_encoding, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypePointer", - .opcode = 3, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .storage_class, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeQualifier", - .opcode = 4, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_type_qualifier, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeArray", - .opcode = 5, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeVector", - .opcode = 6, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypedef", - .opcode = 7, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeFunction", - .opcode = 8, - .operands = &.{ - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeEnum", - .opcode = 9, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .pair_id_ref_id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeComposite", - .opcode = 10, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_composite_type, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeMember", - .opcode = 11, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugTypeInheritance", - .opcode = 12, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypePtrToMember", - .opcode = 13, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeTemplate", - .opcode = 14, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugTypeTemplateParameter", - .opcode = 15, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeTemplateTemplateParameter", - .opcode = 16, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - }, - }, - .{ - .name = "DebugTypeTemplateParameterPack", - .opcode = 17, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugGlobalVariable", - .opcode = 18, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugFunctionDeclaration", - .opcode = 19, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - }, - }, - .{ - .name = "DebugFunction", - .opcode = 20, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugLexicalBlock", - .opcode = 21, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugLexicalBlockDiscriminator", - .opcode = 22, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugScope", - .opcode = 23, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugNoScope", - .opcode = 24, - .operands = &.{}, - }, - .{ - .name = "DebugInlinedAt", - .opcode = 25, - .operands = &.{ - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugLocalVariable", - .opcode = 26, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_info_flags, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugInlinedVariable", - .opcode = 27, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugDeclare", - .opcode = 28, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugValue", - .opcode = 29, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugOperation", - .opcode = 30, - .operands = &.{ - .{ .kind = .open_cl_debug_info_100_debug_operation, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugExpression", - .opcode = 31, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "DebugMacroDef", - .opcode = 32, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugMacroUndef", - .opcode = 33, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugImportedEntity", - .opcode = 34, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .open_cl_debug_info_100_debug_imported_entity, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "DebugSource", - .opcode = 35, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "DebugModuleINTEL", - .opcode = 36, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, - }, - }, - }, - .non_semantic_clspv_reflection_6 => &.{ - .{ - .name = "Kernel", - .opcode = 1, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - .{ .kind = .id_ref, .quantifier = .optional }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentInfo", - .opcode = 2, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - .{ .kind = .id_ref, .quantifier = .optional }, - .{ .kind = .id_ref, .quantifier = .optional }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentStorageBuffer", - .opcode = 3, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentUniform", - .opcode = 4, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentPodStorageBuffer", - .opcode = 5, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentPodUniform", - .opcode = 6, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentPodPushConstant", - .opcode = 7, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentSampledImage", - .opcode = 8, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentStorageImage", - .opcode = 9, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentSampler", - .opcode = 10, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentWorkgroup", - .opcode = 11, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "SpecConstantWorkgroupSize", - .opcode = 12, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SpecConstantGlobalOffset", - .opcode = 13, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SpecConstantWorkDim", - .opcode = 14, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PushConstantGlobalOffset", - .opcode = 15, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PushConstantEnqueuedLocalSize", - .opcode = 16, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PushConstantGlobalSize", - .opcode = 17, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PushConstantRegionOffset", - .opcode = 18, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PushConstantNumWorkgroups", - .opcode = 19, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PushConstantRegionGroupOffset", - .opcode = 20, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ConstantDataStorageBuffer", - .opcode = 21, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ConstantDataUniform", - .opcode = 22, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "LiteralSampler", - .opcode = 23, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PropertyRequiredWorkgroupSize", - .opcode = 24, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SpecConstantSubgroupMaxSize", - .opcode = 25, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ArgumentPointerPushConstant", - .opcode = 26, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentPointerUniform", - .opcode = 27, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ProgramScopeVariablesStorageBuffer", - .opcode = 28, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ProgramScopeVariablePointerRelocation", - .opcode = 29, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ImageArgumentInfoChannelOrderPushConstant", - .opcode = 30, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ImageArgumentInfoChannelDataTypePushConstant", - .opcode = 31, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ImageArgumentInfoChannelOrderUniform", - .opcode = 32, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ImageArgumentInfoChannelDataTypeUniform", - .opcode = 33, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ArgumentStorageTexelBuffer", - .opcode = 34, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ArgumentUniformTexelBuffer", - .opcode = 35, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, - }, - }, - .{ - .name = "ConstantDataPointerPushConstant", - .opcode = 36, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ProgramScopeVariablePointerPushConstant", - .opcode = 37, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PrintfInfo", - .opcode = 38, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - .{ - .name = "PrintfBufferStorageBuffer", - .opcode = 39, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PrintfBufferPointerPushConstant", - .opcode = 40, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "NormalizedSamplerMaskPushConstant", - .opcode = 41, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "WorkgroupVariableSize", - .opcode = 42, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - }, - .glsl_std_450 => &.{ - .{ - .name = "Round", - .opcode = 1, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "RoundEven", - .opcode = 2, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Trunc", - .opcode = 3, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FAbs", - .opcode = 4, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SAbs", - .opcode = 5, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FSign", - .opcode = 6, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SSign", - .opcode = 7, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Floor", - .opcode = 8, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Ceil", - .opcode = 9, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Fract", - .opcode = 10, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Radians", - .opcode = 11, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Degrees", - .opcode = 12, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Sin", - .opcode = 13, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Cos", - .opcode = 14, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Tan", - .opcode = 15, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Asin", - .opcode = 16, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Acos", - .opcode = 17, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Atan", - .opcode = 18, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Sinh", - .opcode = 19, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Cosh", - .opcode = 20, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Tanh", - .opcode = 21, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Asinh", - .opcode = 22, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Acosh", - .opcode = 23, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Atanh", - .opcode = 24, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Atan2", - .opcode = 25, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Pow", - .opcode = 26, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Exp", - .opcode = 27, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Log", - .opcode = 28, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Exp2", - .opcode = 29, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Log2", - .opcode = 30, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Sqrt", - .opcode = 31, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "InverseSqrt", - .opcode = 32, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Determinant", - .opcode = 33, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "MatrixInverse", - .opcode = 34, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Modf", - .opcode = 35, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ModfStruct", - .opcode = 36, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FMin", - .opcode = 37, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UMin", - .opcode = 38, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SMin", - .opcode = 39, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FMax", - .opcode = 40, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UMax", - .opcode = 41, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SMax", - .opcode = 42, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FClamp", - .opcode = 43, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UClamp", - .opcode = 44, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SClamp", - .opcode = 45, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FMix", - .opcode = 46, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "IMix", - .opcode = 47, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Step", - .opcode = 48, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SmoothStep", - .opcode = 49, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Fma", - .opcode = 50, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Frexp", - .opcode = 51, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FrexpStruct", - .opcode = 52, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Ldexp", - .opcode = 53, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PackSnorm4x8", - .opcode = 54, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PackUnorm4x8", - .opcode = 55, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PackSnorm2x16", - .opcode = 56, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PackUnorm2x16", - .opcode = 57, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PackHalf2x16", - .opcode = 58, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "PackDouble2x32", - .opcode = 59, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UnpackSnorm2x16", - .opcode = 60, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UnpackUnorm2x16", - .opcode = 61, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UnpackHalf2x16", - .opcode = 62, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UnpackSnorm4x8", - .opcode = 63, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UnpackUnorm4x8", - .opcode = 64, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "UnpackDouble2x32", - .opcode = 65, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Length", - .opcode = 66, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Distance", - .opcode = 67, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Cross", - .opcode = 68, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Normalize", - .opcode = 69, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FaceForward", - .opcode = 70, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Reflect", - .opcode = 71, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "Refract", - .opcode = 72, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FindILsb", - .opcode = 73, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FindSMsb", - .opcode = 74, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "FindUMsb", - .opcode = 75, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "InterpolateAtCentroid", - .opcode = 76, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "InterpolateAtSample", - .opcode = 77, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "InterpolateAtOffset", - .opcode = 78, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "NMin", - .opcode = 79, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "NMax", - .opcode = 80, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "NClamp", - .opcode = 81, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - }, - .spv_amd_shader_ballot => &.{ - .{ - .name = "SwizzleInvocationsAMD", - .opcode = 1, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "SwizzleInvocationsMaskedAMD", - .opcode = 2, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "WriteInvocationAMD", - .opcode = 3, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "MbcntAMD", - .opcode = 4, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - }, - .non_semantic_debug_printf => &.{ - .{ - .name = "DebugPrintf", - .opcode = 1, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, - }, - }, - }, - .spv_amd_gcn_shader => &.{ - .{ - .name = "CubeFaceIndexAMD", - .opcode = 1, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "CubeFaceCoordAMD", - .opcode = 2, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "TimeAMD", - .opcode = 3, - .operands = &.{}, - }, - }, - .open_cl_std => &.{ - .{ - .name = "acos", - .opcode = 0, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "acosh", - .opcode = 1, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "acospi", - .opcode = 2, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "asin", - .opcode = 3, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "asinh", - .opcode = 4, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "asinpi", - .opcode = 5, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "atan", - .opcode = 6, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "atan2", - .opcode = 7, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "atanh", - .opcode = 8, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "atanpi", - .opcode = 9, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "atan2pi", - .opcode = 10, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "cbrt", - .opcode = 11, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ceil", - .opcode = 12, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "copysign", - .opcode = 13, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "cos", - .opcode = 14, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "cosh", - .opcode = 15, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "cospi", - .opcode = 16, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "erfc", - .opcode = 17, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "erf", - .opcode = 18, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "exp", - .opcode = 19, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "exp2", - .opcode = 20, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "exp10", - .opcode = 21, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "expm1", - .opcode = 22, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fabs", - .opcode = 23, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fdim", - .opcode = 24, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "floor", - .opcode = 25, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fma", - .opcode = 26, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fmax", - .opcode = 27, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fmin", - .opcode = 28, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fmod", - .opcode = 29, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "fract", - .opcode = 30, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "frexp", - .opcode = 31, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "hypot", - .opcode = 32, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ilogb", - .opcode = 33, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "ldexp", - .opcode = 34, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "lgamma", - .opcode = 35, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "lgamma_r", - .opcode = 36, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "log", - .opcode = 37, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "log2", - .opcode = 38, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "log10", - .opcode = 39, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "log1p", - .opcode = 40, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "logb", - .opcode = 41, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "mad", - .opcode = 42, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "maxmag", - .opcode = 43, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "minmag", - .opcode = 44, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "modf", - .opcode = 45, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "nan", - .opcode = 46, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "nextafter", - .opcode = 47, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "pow", - .opcode = 48, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "pown", - .opcode = 49, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "powr", - .opcode = 50, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "remainder", - .opcode = 51, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "remquo", - .opcode = 52, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "rint", - .opcode = 53, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "rootn", - .opcode = 54, - .operands = &.{ - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - }, - }, - .{ - .name = "round", - .opcode = 55, - .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "rsqrt", - .opcode = 56, + .name = "cbrt", + .opcode = 11, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "sin", - .opcode = 57, + .name = "ceil", + .opcode = 12, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "sincos", - .opcode = 58, + .name = "copysign", + .opcode = 13, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "sinh", - .opcode = 59, + .name = "cos", + .opcode = 14, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "sinpi", - .opcode = 60, + .name = "cosh", + .opcode = 15, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "sqrt", - .opcode = 61, + .name = "cospi", + .opcode = 16, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "tan", - .opcode = 62, + .name = "erfc", + .opcode = 17, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "tanh", - .opcode = 63, + .name = "erf", + .opcode = 18, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "tanpi", - .opcode = 64, + .name = "exp", + .opcode = 19, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "tgamma", - .opcode = 65, + .name = "exp2", + .opcode = 20, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "trunc", - .opcode = 66, + .name = "exp10", + .opcode = 21, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_cos", - .opcode = 67, + .name = "expm1", + .opcode = 22, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_divide", - .opcode = 68, + .name = "fabs", + .opcode = 23, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_exp", - .opcode = 69, + .name = "fdim", + .opcode = 24, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_exp2", - .opcode = 70, + .name = "floor", + .opcode = 25, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_exp10", - .opcode = 71, + .name = "fma", + .opcode = 26, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_log", - .opcode = 72, + .name = "fmax", + .opcode = 27, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_log2", - .opcode = 73, + .name = "fmin", + .opcode = 28, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_log10", - .opcode = 74, + .name = "fmod", + .opcode = 29, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_powr", - .opcode = 75, + .name = "fract", + .opcode = 30, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_recip", - .opcode = 76, + .name = "frexp", + .opcode = 31, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_rsqrt", - .opcode = 77, + .name = "hypot", + .opcode = 32, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_sin", - .opcode = 78, + .name = "ilogb", + .opcode = 33, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_sqrt", - .opcode = 79, + .name = "ldexp", + .opcode = 34, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "half_tan", - .opcode = 80, + .name = "lgamma", + .opcode = 35, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_cos", - .opcode = 81, + .name = "lgamma_r", + .opcode = 36, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_divide", - .opcode = 82, + .name = "log", + .opcode = 37, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_exp", - .opcode = 83, + .name = "log2", + .opcode = 38, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_exp2", - .opcode = 84, + .name = "log10", + .opcode = 39, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_exp10", - .opcode = 85, + .name = "log1p", + .opcode = 40, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_log", - .opcode = 86, + .name = "logb", + .opcode = 41, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_log2", - .opcode = 87, + .name = "mad", + .opcode = 42, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_log10", - .opcode = 88, + .name = "maxmag", + .opcode = 43, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_powr", - .opcode = 89, + .name = "minmag", + .opcode = 44, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_recip", - .opcode = 90, + .name = "modf", + .opcode = 45, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_rsqrt", - .opcode = 91, + .name = "nan", + .opcode = 46, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_sin", - .opcode = 92, + .name = "nextafter", + .opcode = 47, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_sqrt", - .opcode = 93, + .name = "pow", + .opcode = 48, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "native_tan", - .opcode = 94, + .name = "pown", + .opcode = 49, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "fclamp", - .opcode = 95, + .name = "powr", + .opcode = 50, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "degrees", - .opcode = 96, + .name = "remainder", + .opcode = 51, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "fmax_common", - .opcode = 97, + .name = "remquo", + .opcode = 52, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "fmin_common", - .opcode = 98, + .name = "rint", + .opcode = 53, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "mix", - .opcode = 99, + .name = "rootn", + .opcode = 54, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "radians", - .opcode = 100, + .name = "round", + .opcode = 55, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "step", - .opcode = 101, + .name = "rsqrt", + .opcode = 56, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "smoothstep", - .opcode = 102, + .name = "sin", + .opcode = 57, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "sincos", + .opcode = 58, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "sign", - .opcode = 103, + .name = "sinh", + .opcode = 59, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "cross", - .opcode = 104, + .name = "sinpi", + .opcode = 60, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "distance", - .opcode = 105, + .name = "sqrt", + .opcode = 61, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "length", - .opcode = 106, + .name = "tan", + .opcode = 62, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "normalize", - .opcode = 107, + .name = "tanh", + .opcode = 63, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "fast_distance", - .opcode = 108, + .name = "tanpi", + .opcode = 64, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "fast_length", - .opcode = 109, + .name = "tgamma", + .opcode = 65, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "fast_normalize", - .opcode = 110, + .name = "trunc", + .opcode = 66, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_abs", - .opcode = 141, + .name = "half_cos", + .opcode = 67, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_abs_diff", - .opcode = 142, + .name = "half_divide", + .opcode = 68, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_add_sat", - .opcode = 143, + .name = "half_exp", + .opcode = 69, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_add_sat", - .opcode = 144, + .name = "half_exp2", + .opcode = 70, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_hadd", - .opcode = 145, + .name = "half_exp10", + .opcode = 71, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_hadd", - .opcode = 146, + .name = "half_log", + .opcode = 72, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_rhadd", - .opcode = 147, + .name = "half_log2", + .opcode = 73, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_rhadd", - .opcode = 148, + .name = "half_log10", + .opcode = 74, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_clamp", - .opcode = 149, + .name = "half_powr", + .opcode = 75, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_clamp", - .opcode = 150, + .name = "half_recip", + .opcode = 76, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "clz", - .opcode = 151, + .name = "half_rsqrt", + .opcode = 77, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "ctz", - .opcode = 152, + .name = "half_sin", + .opcode = 78, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_mad_hi", - .opcode = 153, + .name = "half_sqrt", + .opcode = 79, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_mad_sat", - .opcode = 154, + .name = "half_tan", + .opcode = 80, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_mad_sat", - .opcode = 155, + .name = "native_cos", + .opcode = 81, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_max", - .opcode = 156, + .name = "native_divide", + .opcode = 82, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_max", - .opcode = 157, + .name = "native_exp", + .opcode = 83, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_min", - .opcode = 158, + .name = "native_exp2", + .opcode = 84, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_min", - .opcode = 159, + .name = "native_exp10", + .opcode = 85, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_mul_hi", - .opcode = 160, + .name = "native_log", + .opcode = 86, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "rotate", - .opcode = 161, + .name = "native_log2", + .opcode = 87, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_sub_sat", - .opcode = 162, + .name = "native_log10", + .opcode = 88, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_sub_sat", - .opcode = 163, + .name = "native_powr", + .opcode = 89, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_upsample", - .opcode = 164, + .name = "native_recip", + .opcode = 90, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_upsample", - .opcode = 165, + .name = "native_rsqrt", + .opcode = 91, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "popcount", - .opcode = 166, + .name = "native_sin", + .opcode = 92, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_mad24", - .opcode = 167, + .name = "native_sqrt", + .opcode = 93, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_mad24", - .opcode = 168, + .name = "native_tan", + .opcode = 94, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "s_mul24", - .opcode = 169, + .name = "fclamp", + .opcode = 95, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_mul24", - .opcode = 170, + .name = "degrees", + .opcode = 96, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "vloadn", - .opcode = 171, + .name = "fmax_common", + .opcode = 97, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "vstoren", - .opcode = 172, + .name = "fmin_common", + .opcode = 98, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "vload_half", - .opcode = 173, + .name = "mix", + .opcode = 99, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "vload_halfn", - .opcode = 174, + .name = "radians", + .opcode = 100, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "vstore_half", - .opcode = 175, + .name = "step", + .opcode = 101, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "vstore_half_r", - .opcode = 176, + .name = "smoothstep", + .opcode = 102, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .fp_rounding_mode, .quantifier = .required }, }, }, .{ - .name = "vstore_halfn", - .opcode = 177, + .name = "sign", + .opcode = 103, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "vstore_halfn_r", - .opcode = 178, + .name = "cross", + .opcode = 104, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .fp_rounding_mode, .quantifier = .required }, }, }, .{ - .name = "vloada_halfn", - .opcode = 179, + .name = "distance", + .opcode = 105, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "vstorea_halfn", - .opcode = 180, + .name = "length", + .opcode = 106, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "vstorea_halfn_r", - .opcode = 181, + .name = "normalize", + .opcode = 107, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .fp_rounding_mode, .quantifier = .required }, }, }, .{ - .name = "shuffle", - .opcode = 182, + .name = "fast_distance", + .opcode = 108, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "shuffle2", - .opcode = 183, + .name = "fast_length", + .opcode = 109, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "printf", - .opcode = 184, + .name = "fast_normalize", + .opcode = 110, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "prefetch", - .opcode = 185, + .name = "s_abs", + .opcode = 141, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "bitselect", - .opcode = 186, + .name = "s_abs_diff", + .opcode = 142, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "select", - .opcode = 187, + .name = "s_add_sat", + .opcode = 143, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_abs", - .opcode = 201, + .name = "u_add_sat", + .opcode = 144, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_abs_diff", - .opcode = 202, + .name = "s_hadd", + .opcode = 145, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_mul_hi", - .opcode = 203, + .name = "u_hadd", + .opcode = 146, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "u_mad_hi", - .opcode = 204, + .name = "s_rhadd", + .opcode = 147, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, - }, - .non_semantic_shader_debug_info_100 => &.{ - .{ - .name = "DebugInfoNone", - .opcode = 0, - .operands = &.{}, - }, .{ - .name = "DebugCompilationUnit", - .opcode = 1, + .name = "u_rhadd", + .opcode = 148, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeBasic", - .opcode = 2, + .name = "s_clamp", + .opcode = 149, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypePointer", - .opcode = 3, + .name = "u_clamp", + .opcode = 150, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, @@ -18003,216 +15322,179 @@ pub const InstructionSet = enum { }, }, .{ - .name = "DebugTypeQualifier", - .opcode = 4, + .name = "clz", + .opcode = 151, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeArray", - .opcode = 5, + .name = "ctz", + .opcode = 152, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeVector", - .opcode = 6, + .name = "s_mad_hi", + .opcode = 153, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypedef", - .opcode = 7, + .name = "u_mad_sat", + .opcode = 154, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "s_mad_sat", + .opcode = 155, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeFunction", - .opcode = 8, + .name = "s_max", + .opcode = 156, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeEnum", - .opcode = 9, + .name = "u_max", + .opcode = 157, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .pair_id_ref_id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeComposite", - .opcode = 10, + .name = "s_min", + .opcode = 158, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugTypeMember", - .opcode = 11, + .name = "u_min", + .opcode = 159, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, }, }, .{ - .name = "DebugTypeInheritance", - .opcode = 12, + .name = "s_mul_hi", + .opcode = 160, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypePtrToMember", - .opcode = 13, + .name = "rotate", + .opcode = 161, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeTemplate", - .opcode = 14, + .name = "s_sub_sat", + .opcode = 162, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeTemplateParameter", - .opcode = 15, + .name = "u_sub_sat", + .opcode = 163, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeTemplateTemplateParameter", - .opcode = 16, + .name = "u_upsample", + .opcode = 164, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeTemplateParameterPack", - .opcode = 17, + .name = "s_upsample", + .opcode = 165, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugGlobalVariable", - .opcode = 18, + .name = "popcount", + .opcode = 166, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, }, }, .{ - .name = "DebugFunctionDeclaration", - .opcode = 19, + .name = "s_mad24", + .opcode = 167, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugFunction", - .opcode = 20, + .name = "u_mad24", + .opcode = 168, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "s_mul24", + .opcode = 169, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, }, }, .{ - .name = "DebugLexicalBlock", - .opcode = 21, + .name = "u_mul24", + .opcode = 170, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + }, + }, + .{ + .name = "vloadn", + .opcode = 171, + .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, + .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "DebugLexicalBlockDiscriminator", - .opcode = 22, + .name = "vstoren", + .opcode = 172, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, @@ -18220,183 +15502,165 @@ pub const InstructionSet = enum { }, }, .{ - .name = "DebugScope", - .opcode = 23, + .name = "vload_half", + .opcode = 173, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugNoScope", - .opcode = 24, - .operands = &.{}, - }, - .{ - .name = "DebugInlinedAt", - .opcode = 25, + .name = "vload_halfn", + .opcode = 174, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, + .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "DebugLocalVariable", - .opcode = 26, + .name = "vstore_half", + .opcode = 175, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, }, }, .{ - .name = "DebugInlinedVariable", - .opcode = 27, + .name = "vstore_half_r", + .opcode = 176, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .fp_rounding_mode, .quantifier = .required }, }, }, .{ - .name = "DebugDeclare", - .opcode = 28, + .name = "vstore_halfn", + .opcode = 177, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugValue", - .opcode = 29, + .name = "vstore_halfn_r", + .opcode = 178, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, + .{ .kind = .fp_rounding_mode, .quantifier = .required }, }, }, .{ - .name = "DebugOperation", - .opcode = 30, + .name = "vloada_halfn", + .opcode = 179, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .variadic }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .literal_integer, .quantifier = .required }, }, }, .{ - .name = "DebugExpression", - .opcode = 31, + .name = "vstorea_halfn", + .opcode = 180, .operands = &.{ - .{ .kind = .id_ref, .quantifier = .variadic }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugMacroDef", - .opcode = 32, + .name = "vstorea_halfn_r", + .opcode = 181, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, + .{ .kind = .fp_rounding_mode, .quantifier = .required }, }, }, .{ - .name = "DebugMacroUndef", - .opcode = 33, + .name = "shuffle", + .opcode = 182, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugImportedEntity", - .opcode = 34, + .name = "shuffle2", + .opcode = 183, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugSource", - .opcode = 35, + .name = "printf", + .opcode = 184, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .optional }, + .{ .kind = .id_ref, .quantifier = .variadic }, }, }, .{ - .name = "DebugFunctionDefinition", - .opcode = 101, + .name = "prefetch", + .opcode = 185, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugSourceContinued", - .opcode = 102, + .name = "bitselect", + .opcode = 186, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugLine", - .opcode = 103, + .name = "select", + .opcode = 187, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugNoLine", - .opcode = 104, - .operands = &.{}, - }, - .{ - .name = "DebugBuildIdentifier", - .opcode = 105, + .name = "u_abs", + .opcode = 201, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugStoragePath", - .opcode = 106, + .name = "u_abs_diff", + .opcode = 202, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, + .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugEntryPoint", - .opcode = 107, + .name = "u_mul_hi", + .opcode = 203, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, - .{ .kind = .id_ref, .quantifier = .required }, }, }, .{ - .name = "DebugTypeMatrix", - .opcode = 108, + .name = "u_mad_hi", + .opcode = 204, .operands = &.{ .{ .kind = .id_ref, .quantifier = .required }, .{ .kind = .id_ref, .quantifier = .required }, diff --git a/src/dev.zig b/src/dev.zig index bf2d957c0ddf..ac30cedecd83 100644 --- a/src/dev.zig +++ b/src/dev.zig @@ -191,6 +191,7 @@ pub const Env = enum { .spirv => switch (feature) { .spirv_backend, .spirv_linker, + .legalize, => true, else => Env.sema.supports(feature), }, diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 01e88ce2d9fd..8c42d75954e1 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -1,62 +1,36 @@ -//! SPIR-V Spec documentation: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html -//! According to above documentation, a SPIR-V module has the following logical layout: -//! Header. -//! OpCapability instructions. -//! OpExtension instructions. -//! OpExtInstImport instructions. -//! A single OpMemoryModel instruction. -//! All entry points, declared with OpEntryPoint instructions. -//! All execution-mode declarators; OpExecutionMode and OpExecutionModeId instructions. -//! Debug instructions: -//! - First, OpString, OpSourceExtension, OpSource, OpSourceContinued (no forward references). -//! - OpName and OpMemberName instructions. -//! - OpModuleProcessed instructions. -//! All annotation (decoration) instructions. -//! All type declaration instructions, constant instructions, global variable declarations, (preferably) OpUndef instructions. -//! All function declarations without a body (extern functions presumably). -//! All regular functions. - -// Because SPIR-V requires re-compilation anyway, and so hot swapping will not work -// anyway, we simply generate all the code in flush. This keeps -// things considerably simpler. - -const SpirV = @This(); - const std = @import("std"); const Allocator = std.mem.Allocator; +const Path = std.Build.Cache.Path; const assert = std.debug.assert; const log = std.log.scoped(.link); -const Path = std.Build.Cache.Path; const Zcu = @import("../Zcu.zig"); const InternPool = @import("../InternPool.zig"); const Compilation = @import("../Compilation.zig"); const link = @import("../link.zig"); -const codegen = @import("../codegen/spirv.zig"); -const trace = @import("../tracy.zig").trace; -const build_options = @import("build_options"); const Air = @import("../Air.zig"); const Type = @import("../Type.zig"); -const Value = @import("../Value.zig"); +const BinaryModule = @import("SpirV/BinaryModule.zig"); +const CodeGen = @import("../codegen/spirv/CodeGen.zig"); +const Module = @import("../codegen/spirv/Module.zig"); +const trace = @import("../tracy.zig").trace; -const SpvModule = @import("../codegen/spirv/Module.zig"); -const Section = @import("../codegen/spirv/Section.zig"); const spec = @import("../codegen/spirv/spec.zig"); const Id = spec.Id; const Word = spec.Word; -const BinaryModule = @import("SpirV/BinaryModule.zig"); +const Linker = @This(); base: link.File, - -object: codegen.Object, +module: Module, +cg: CodeGen, pub fn createEmpty( arena: Allocator, comp: *Compilation, emit: Path, options: link.File.OpenOptions, -) !*SpirV { +) !*Linker { const gpa = comp.gpa; const target = &comp.root_mod.resolved_target.result; @@ -72,8 +46,8 @@ pub fn createEmpty( else => unreachable, // Caught by Compilation.Config.resolve. } - const self = try arena.create(SpirV); - self.* = .{ + const linker = try arena.create(Linker); + linker.* = .{ .base = .{ .tag = .spirv, .comp = comp, @@ -85,17 +59,30 @@ pub fn createEmpty( .file = null, .build_id = options.build_id, }, - .object = codegen.Object.init(gpa, comp.getTarget()), + .module = .{ + .gpa = gpa, + .arena = arena, + .zcu = comp.zcu.?, + }, + .cg = .{ + // These fields are populated in generate() + .pt = undefined, + .air = undefined, + .liveness = undefined, + .owner_nav = undefined, + .module = undefined, + .control_flow = .{ .structured = .{} }, + .base_line = undefined, + }, }; - errdefer self.deinit(); + errdefer linker.deinit(); - // TODO: read the file and keep valid parts instead of truncating - self.base.file = try emit.root_dir.handle.createFile(emit.sub_path, .{ + linker.base.file = try emit.root_dir.handle.createFile(emit.sub_path, .{ .truncate = true, .read = true, }); - return self; + return linker; } pub fn open( @@ -103,27 +90,90 @@ pub fn open( comp: *Compilation, emit: Path, options: link.File.OpenOptions, -) !*SpirV { +) !*Linker { return createEmpty(arena, comp, emit, options); } -pub fn deinit(self: *SpirV) void { - self.object.deinit(); +pub fn deinit(linker: *Linker) void { + linker.cg.deinit(); + linker.module.deinit(); } -pub fn updateNav(self: *SpirV, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void { - if (build_options.skip_non_native) { - @panic("Attempted to compile for architecture that was disabled by build configuration"); - } +fn generate( + linker: *Linker, + pt: Zcu.PerThread, + nav_index: InternPool.Nav.Index, + air: Air, + liveness: Air.Liveness, + do_codegen: bool, +) !void { + const zcu = pt.zcu; + const gpa = zcu.gpa; + const structured_cfg = zcu.navFileScope(nav_index).mod.?.structured_cfg; + + linker.cg.control_flow.deinit(gpa); + linker.cg.args.clearRetainingCapacity(); + linker.cg.inst_results.clearRetainingCapacity(); + linker.cg.id_scratch.clearRetainingCapacity(); + linker.cg.prologue.reset(); + linker.cg.body.reset(); + + linker.cg = .{ + .pt = pt, + .air = air, + .liveness = liveness, + .owner_nav = nav_index, + .module = &linker.module, + .control_flow = switch (structured_cfg) { + true => .{ .structured = .{} }, + false => .{ .unstructured = .{} }, + }, + .base_line = zcu.navSrcLine(nav_index), + .args = linker.cg.args, + .inst_results = linker.cg.inst_results, + .id_scratch = linker.cg.id_scratch, + .prologue = linker.cg.prologue, + .body = linker.cg.body, + }; + + linker.cg.genNav(do_codegen) catch |err| switch (err) { + error.CodegenFail => switch (zcu.codegenFailMsg(nav_index, linker.cg.error_msg.?)) { + error.CodegenFail => {}, + error.OutOfMemory => |e| return e, + }, + else => |other| { + // There might be an error that happened *after* linker.error_msg + // was already allocated, so be sure to free it. + if (linker.cg.error_msg) |error_msg| { + error_msg.deinit(gpa); + } + + return other; + }, + }; +} + +pub fn updateFunc( + linker: *Linker, + pt: Zcu.PerThread, + func_index: InternPool.Index, + air: *const Air, + liveness: *const ?Air.Liveness, +) !void { + const nav = pt.zcu.funcInfo(func_index).owner_nav; + // TODO: Separate types for generating decls and functions? + try linker.generate(pt, nav, air.*, liveness.*.?, true); +} + +pub fn updateNav(linker: *Linker, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void { const ip = &pt.zcu.intern_pool; log.debug("lowering nav {f}({d})", .{ ip.getNav(nav).fqn.fmt(ip), nav }); - - try self.object.updateNav(pt, nav); + try linker.generate(pt, nav, undefined, undefined, false); } pub fn updateExports( - self: *SpirV, + linker: *Linker, pt: Zcu.PerThread, exported: Zcu.Exported, export_indices: []const Zcu.Export.Index, @@ -134,13 +184,13 @@ pub fn updateExports( .nav => |nav| nav, .uav => |uav| { _ = uav; - @panic("TODO: implement SpirV linker code for exporting a constant value"); + @panic("TODO: implement Linker linker code for exporting a constant value"); }, }; const nav_ty = ip.getNav(nav_index).typeOf(ip); const target = zcu.getTarget(); if (ip.isFunctionType(nav_ty)) { - const spv_decl_index = try self.object.resolveNav(zcu, nav_index); + const spv_decl_index = try linker.module.resolveNav(ip, nav_index); const cc = Type.fromInterned(nav_ty).fnCallingConvention(zcu); const exec_model: spec.ExecutionModel = switch (target.os.tag) { .vulkan, .opengl => switch (cc) { @@ -162,7 +212,7 @@ pub fn updateExports( for (export_indices) |export_idx| { const exp = export_idx.ptr(zcu); - try self.object.spv.declareEntryPoint( + try linker.module.declareEntryPoint( spv_decl_index, exp.opts.name.toSlice(ip), exec_model, @@ -175,7 +225,7 @@ pub fn updateExports( } pub fn flush( - self: *SpirV, + linker: *Linker, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node, @@ -185,35 +235,29 @@ pub fn flush( // InternPool. _ = tid; - if (build_options.skip_non_native) { - @panic("Attempted to compile for architecture that was disabled by build configuration"); - } - const tracy = trace(@src()); defer tracy.end(); const sub_prog_node = prog_node.start("Flush Module", 0); defer sub_prog_node.end(); - const comp = self.base.comp; - const spv = &self.object.spv; + const comp = linker.base.comp; const diags = &comp.link_diags; const gpa = comp.gpa; // We need to export the list of error names somewhere so that we can pretty-print them in the // executor. This is not really an important thing though, so we can just dump it in any old // nonsemantic instruction. For now, just put it in OpSourceExtension with a special name. - var error_info: std.io.Writer.Allocating = .init(self.object.gpa); + var error_info: std.io.Writer.Allocating = .init(linker.module.gpa); defer error_info.deinit(); error_info.writer.writeAll("zig_errors:") catch return error.OutOfMemory; - const ip = &self.base.comp.zcu.?.intern_pool; + const ip = &linker.base.comp.zcu.?.intern_pool; for (ip.global_error_set.getNamesFromMainThread()) |name| { // Errors can contain pretty much any character - to encode them in a string we must escape // them somehow. Easiest here is to use some established scheme, one which also preseves the // name if it contains no strange characters is nice for debugging. URI encoding fits the bill. // We're using : as separator, which is a reserved character. - error_info.writer.writeByte(':') catch return error.OutOfMemory; std.Uri.Component.percentEncode( &error_info.writer, @@ -228,36 +272,34 @@ pub fn flush( }.isValidChar, ) catch return error.OutOfMemory; } - try spv.sections.debug_strings.emit(gpa, .OpSourceExtension, .{ + try linker.module.sections.debug_strings.emit(gpa, .OpSourceExtension, .{ .extension = error_info.getWritten(), }); - const module = try spv.finalize(arena); + const module = try linker.module.finalize(arena); errdefer arena.free(module); - const linked_module = self.linkModule(arena, module, sub_prog_node) catch |err| switch (err) { + const linked_module = linker.linkModule(arena, module, sub_prog_node) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, else => |other| return diags.fail("error while linking: {s}", .{@errorName(other)}), }; - self.base.file.?.writeAll(std.mem.sliceAsBytes(linked_module)) catch |err| + linker.base.file.?.writeAll(std.mem.sliceAsBytes(linked_module)) catch |err| return diags.fail("failed to write: {s}", .{@errorName(err)}); } -fn linkModule(self: *SpirV, a: Allocator, module: []Word, progress: std.Progress.Node) ![]Word { - _ = self; +fn linkModule(linker: *Linker, arena: Allocator, module: []Word, progress: std.Progress.Node) ![]Word { + _ = linker; const lower_invocation_globals = @import("SpirV/lower_invocation_globals.zig"); const prune_unused = @import("SpirV/prune_unused.zig"); - const dedup = @import("SpirV/deduplicate.zig"); - var parser = try BinaryModule.Parser.init(a); + var parser = try BinaryModule.Parser.init(arena); defer parser.deinit(); var binary = try parser.parse(module); try lower_invocation_globals.run(&parser, &binary, progress); try prune_unused.run(&parser, &binary, progress); - try dedup.run(&parser, &binary, progress); - return binary.finalize(a); + return binary.finalize(arena); } diff --git a/src/link/SpirV/deduplicate.zig b/src/link/SpirV/deduplicate.zig deleted file mode 100644 index 04b916e13022..000000000000 --- a/src/link/SpirV/deduplicate.zig +++ /dev/null @@ -1,553 +0,0 @@ -const std = @import("std"); -const Allocator = std.mem.Allocator; -const log = std.log.scoped(.spirv_link); -const assert = std.debug.assert; - -const BinaryModule = @import("BinaryModule.zig"); -const Section = @import("../../codegen/spirv/Section.zig"); -const spec = @import("../../codegen/spirv/spec.zig"); -const Opcode = spec.Opcode; -const ResultId = spec.Id; -const Word = spec.Word; - -fn canDeduplicate(opcode: Opcode) bool { - return switch (opcode) { - .OpTypeForwardPointer => false, // Don't need to handle these - .OpGroupDecorate, .OpGroupMemberDecorate => { - // These are deprecated, so don't bother supporting them for now. - return false; - }, - // Debug decoration-style instructions - .OpName, .OpMemberName => true, - else => switch (opcode.class()) { - .type_declaration, - .constant_creation, - .annotation, - => true, - else => false, - }, - }; -} - -const ModuleInfo = struct { - /// This models a type, decoration or constant instruction - /// and its dependencies. - const Entity = struct { - /// The type that this entity represents. This is just - /// the instruction opcode. - kind: Opcode, - /// The offset of this entity's operands, in - /// `binary.instructions`. - first_operand: u32, - /// The number of operands in this entity - num_operands: u16, - /// The (first_operand-relative) offset of the result-id, - /// or the entity that is affected by this entity if this entity - /// is a decoration. - result_id_index: u16, - /// The first decoration in `self.decorations`. - first_decoration: u32, - - fn operands(self: Entity, binary: *const BinaryModule) []const Word { - return binary.instructions[self.first_operand..][0..self.num_operands]; - } - }; - - /// Maps result-id to Entity's - entities: std.AutoArrayHashMapUnmanaged(ResultId, Entity), - /// A bit set that keeps track of which operands are result-ids. - /// Note: This also includes any result-id! - /// Because we need these values when recoding the module anyway, - /// it contains the status of ALL operands in the module. - operand_is_id: std.DynamicBitSetUnmanaged, - /// Store of decorations for each entity. - decorations: []const Entity, - - pub fn parse( - arena: Allocator, - parser: *BinaryModule.Parser, - binary: BinaryModule, - ) !ModuleInfo { - var entities = std.AutoArrayHashMap(ResultId, Entity).init(arena); - var id_offsets = std.ArrayList(u16).init(arena); - var operand_is_id = try std.DynamicBitSetUnmanaged.initEmpty(arena, binary.instructions.len); - var decorations = std.MultiArrayList(struct { target_id: ResultId, entity: Entity }){}; - - var it = binary.iterateInstructions(); - while (it.next()) |inst| { - id_offsets.items.len = 0; - try parser.parseInstructionResultIds(binary, inst, &id_offsets); - - const first_operand_offset: u32 = @intCast(inst.offset + 1); - for (id_offsets.items) |offset| { - operand_is_id.set(first_operand_offset + offset); - } - - if (!canDeduplicate(inst.opcode)) continue; - - const result_id_index: u16 = switch (inst.opcode.class()) { - .type_declaration, .annotation, .debug => 0, - .constant_creation => 1, - else => unreachable, - }; - - const result_id: ResultId = @enumFromInt(inst.operands[id_offsets.items[result_id_index]]); - const entity = Entity{ - .kind = inst.opcode, - .first_operand = first_operand_offset, - .num_operands = @intCast(inst.operands.len), - .result_id_index = result_id_index, - .first_decoration = undefined, // Filled in later - }; - - switch (inst.opcode.class()) { - .annotation, .debug => { - try decorations.append(arena, .{ - .target_id = result_id, - .entity = entity, - }); - }, - .type_declaration, .constant_creation => { - const entry = try entities.getOrPut(result_id); - if (entry.found_existing) { - log.err("type or constant {f} has duplicate definition", .{result_id}); - return error.DuplicateId; - } - entry.value_ptr.* = entity; - }, - else => unreachable, - } - } - - // Sort decorations by the index of the result-id in `entities. - // This ensures not only that the decorations of a particular reuslt-id - // are continuous, but the subsequences also appear in the same order as in `entities`. - - const SortContext = struct { - entities: std.AutoArrayHashMapUnmanaged(ResultId, Entity), - ids: []const ResultId, - - pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool { - // If any index is not in the entities set, its because its not a - // deduplicatable result-id. Those should be considered largest and - // float to the end. - const entity_index_a = ctx.entities.getIndex(ctx.ids[a_index]) orelse return false; - const entity_index_b = ctx.entities.getIndex(ctx.ids[b_index]) orelse return true; - - return entity_index_a < entity_index_b; - } - }; - - decorations.sort(SortContext{ - .entities = entities.unmanaged, - .ids = decorations.items(.target_id), - }); - - // Now go through the decorations and add the offsets to the entities list. - var decoration_i: u32 = 0; - const target_ids = decorations.items(.target_id); - for (entities.keys(), entities.values()) |id, *entity| { - entity.first_decoration = decoration_i; - - // Scan ahead to the next decoration - while (decoration_i < target_ids.len and target_ids[decoration_i] == id) { - decoration_i += 1; - } - } - - return .{ - .entities = entities.unmanaged, - .operand_is_id = operand_is_id, - // There may be unrelated decorations at the end, so make sure to - // slice those off. - .decorations = decorations.items(.entity)[0..decoration_i], - }; - } - - fn entityDecorationsByIndex(self: ModuleInfo, index: usize) []const Entity { - const values = self.entities.values(); - const first_decoration = values[index].first_decoration; - if (index == values.len - 1) { - return self.decorations[first_decoration..]; - } else { - const next_first_decoration = values[index + 1].first_decoration; - return self.decorations[first_decoration..next_first_decoration]; - } - } -}; - -const EntityContext = struct { - a: Allocator, - ptr_map_a: std.AutoArrayHashMapUnmanaged(ResultId, void) = .empty, - ptr_map_b: std.AutoArrayHashMapUnmanaged(ResultId, void) = .empty, - info: *const ModuleInfo, - binary: *const BinaryModule, - - fn deinit(self: *EntityContext) void { - self.ptr_map_a.deinit(self.a); - self.ptr_map_b.deinit(self.a); - - self.* = undefined; - } - - fn equalizeMapCapacity(self: *EntityContext) !void { - const cap = @max(self.ptr_map_a.capacity(), self.ptr_map_b.capacity()); - try self.ptr_map_a.ensureTotalCapacity(self.a, cap); - try self.ptr_map_b.ensureTotalCapacity(self.a, cap); - } - - fn hash(self: *EntityContext, id: ResultId) !u64 { - var hasher = std.hash.Wyhash.init(0); - self.ptr_map_a.clearRetainingCapacity(); - try self.hashInner(&hasher, id); - return hasher.final(); - } - - fn hashInner(self: *EntityContext, hasher: *std.hash.Wyhash, id: ResultId) error{OutOfMemory}!void { - const index = self.info.entities.getIndex(id) orelse { - // Index unknown, the type or constant may depend on another result-id - // that couldn't be deduplicated and so it wasn't added to info.entities. - // In this case, just has the ID itself. - std.hash.autoHash(hasher, id); - return; - }; - - const entity = self.info.entities.values()[index]; - - // If the current pointer is recursive, don't immediately add it to the map. This is to ensure that - // if the current pointer is already recursive, it gets the same hash a pointer that points to the - // same child but has a different result-id. - if (entity.kind == .OpTypePointer) { - // This may be either a pointer that is forward-referenced in the future, - // or a forward reference to a pointer. - // Note: We use the **struct** here instead of the pointer itself, to avoid an edge case like this: - // - // A - C*' - // \ - // C - C*' - // / - // B - C*" - // - // In this case, hashing A goes like - // A -> C*' -> C -> C*' recursion - // And hashing B goes like - // B -> C*" -> C -> C*' -> C -> C*' recursion - // The are several calls to ptrType in codegen that may C*' and C*" to be generated as separate - // types. This is not a problem for C itself though - this can only be generated through resolveType() - // and so ensures equality by Zig's type system. Technically the above problem is still present, but it - // would only be present in a structure such as - // - // A - C*' - C' - // \ - // C*" - C - C* - // / - // B - // - // where there is a duplicate definition of struct C. Resolving this requires a much more time consuming - // algorithm though, and because we don't expect any correctness issues with it, we leave that for now. - - // TODO: Do we need to mind the storage class here? Its going to be recursive regardless, right? - const struct_id: ResultId = @enumFromInt(entity.operands(self.binary)[2]); - const entry = try self.ptr_map_a.getOrPut(self.a, struct_id); - if (entry.found_existing) { - // Pointer already seen. Hash the index instead of recursing into its children. - std.hash.autoHash(hasher, entry.index); - return; - } - } - - try self.hashEntity(hasher, entity); - - // Process decorations. - const decorations = self.info.entityDecorationsByIndex(index); - for (decorations) |decoration| { - try self.hashEntity(hasher, decoration); - } - - if (entity.kind == .OpTypePointer) { - const struct_id: ResultId = @enumFromInt(entity.operands(self.binary)[2]); - assert(self.ptr_map_a.swapRemove(struct_id)); - } - } - - fn hashEntity(self: *EntityContext, hasher: *std.hash.Wyhash, entity: ModuleInfo.Entity) !void { - std.hash.autoHash(hasher, entity.kind); - // Process operands - const operands = entity.operands(self.binary); - for (operands, 0..) |operand, i| { - if (i == entity.result_id_index) { - // Not relevant, skip... - continue; - } else if (self.info.operand_is_id.isSet(entity.first_operand + i)) { - // Operand is ID - try self.hashInner(hasher, @enumFromInt(operand)); - } else { - // Operand is merely data - std.hash.autoHash(hasher, operand); - } - } - } - - fn eql(self: *EntityContext, a: ResultId, b: ResultId) !bool { - self.ptr_map_a.clearRetainingCapacity(); - self.ptr_map_b.clearRetainingCapacity(); - - return try self.eqlInner(a, b); - } - - fn eqlInner(self: *EntityContext, id_a: ResultId, id_b: ResultId) error{OutOfMemory}!bool { - const maybe_index_a = self.info.entities.getIndex(id_a); - const maybe_index_b = self.info.entities.getIndex(id_b); - - if (maybe_index_a == null and maybe_index_b == null) { - // Both indices unknown. In this case the type or constant - // may depend on another result-id that couldn't be deduplicated - // (so it wasn't added to info.entities). In this case, that particular - // result-id should be the same one. - return id_a == id_b; - } - - const index_a = maybe_index_a orelse return false; - const index_b = maybe_index_b orelse return false; - - const entity_a = self.info.entities.values()[index_a]; - const entity_b = self.info.entities.values()[index_b]; - - if (entity_a.kind != entity_b.kind) { - return false; - } - - if (entity_a.kind == .OpTypePointer) { - // May be a forward reference, or should be saved as a potential - // forward reference in the future. Whatever the case, it should - // be the same for both a and b. - const struct_id_a: ResultId = @enumFromInt(entity_a.operands(self.binary)[2]); - const struct_id_b: ResultId = @enumFromInt(entity_b.operands(self.binary)[2]); - - const entry_a = try self.ptr_map_a.getOrPut(self.a, struct_id_a); - const entry_b = try self.ptr_map_b.getOrPut(self.a, struct_id_b); - - if (entry_a.found_existing != entry_b.found_existing) return false; - if (entry_a.index != entry_b.index) return false; - - if (entry_a.found_existing) { - // No need to recurse. - return true; - } - } - - if (!try self.eqlEntities(entity_a, entity_b)) { - return false; - } - - // Compare decorations. - const decorations_a = self.info.entityDecorationsByIndex(index_a); - const decorations_b = self.info.entityDecorationsByIndex(index_b); - if (decorations_a.len != decorations_b.len) { - return false; - } - - for (decorations_a, decorations_b) |decoration_a, decoration_b| { - if (!try self.eqlEntities(decoration_a, decoration_b)) { - return false; - } - } - - if (entity_a.kind == .OpTypePointer) { - const struct_id_a: ResultId = @enumFromInt(entity_a.operands(self.binary)[2]); - const struct_id_b: ResultId = @enumFromInt(entity_b.operands(self.binary)[2]); - - assert(self.ptr_map_a.swapRemove(struct_id_a)); - assert(self.ptr_map_b.swapRemove(struct_id_b)); - } - - return true; - } - - fn eqlEntities(self: *EntityContext, entity_a: ModuleInfo.Entity, entity_b: ModuleInfo.Entity) !bool { - if (entity_a.kind != entity_b.kind) { - return false; - } else if (entity_a.result_id_index != entity_a.result_id_index) { - return false; - } - - const operands_a = entity_a.operands(self.binary); - const operands_b = entity_b.operands(self.binary); - - // Note: returns false for operands that have explicit defaults in optional operands... oh well - if (operands_a.len != operands_b.len) { - return false; - } - - for (operands_a, operands_b, 0..) |operand_a, operand_b, i| { - const a_is_id = self.info.operand_is_id.isSet(entity_a.first_operand + i); - const b_is_id = self.info.operand_is_id.isSet(entity_b.first_operand + i); - if (a_is_id != b_is_id) { - return false; - } else if (i == entity_a.result_id_index) { - // result-id for both... - continue; - } else if (a_is_id) { - // Both are IDs, so recurse. - if (!try self.eqlInner(@enumFromInt(operand_a), @enumFromInt(operand_b))) { - return false; - } - } else if (operand_a != operand_b) { - return false; - } - } - - return true; - } -}; - -/// This struct is a wrapper around EntityContext that adapts it for -/// use in a hash map. Because EntityContext allocates, it cannot be -/// used. This wrapper simply assumes that the maps have been allocated -/// the max amount of memory they are going to use. -/// This is done by pre-hashing all keys. -const EntityHashContext = struct { - entity_context: *EntityContext, - - pub fn hash(self: EntityHashContext, key: ResultId) u64 { - return self.entity_context.hash(key) catch unreachable; - } - - pub fn eql(self: EntityHashContext, a: ResultId, b: ResultId) bool { - return self.entity_context.eql(a, b) catch unreachable; - } -}; - -pub fn run(parser: *BinaryModule.Parser, binary: *BinaryModule, progress: std.Progress.Node) !void { - const sub_node = progress.start("deduplicate", 0); - defer sub_node.end(); - - var arena = std.heap.ArenaAllocator.init(parser.a); - defer arena.deinit(); - const a = arena.allocator(); - - const info = try ModuleInfo.parse(a, parser, binary.*); - - // Hash all keys once so that the maps can be allocated the right size. - var ctx = EntityContext{ - .a = a, - .info = &info, - .binary = binary, - }; - - for (info.entities.keys()) |id| { - _ = try ctx.hash(id); - } - - // hash only uses ptr_map_a, so allocate ptr_map_b too - try ctx.equalizeMapCapacity(); - - // Figure out which entities can be deduplicated. - var map = std.HashMap(ResultId, void, EntityHashContext, 80).initContext(a, .{ - .entity_context = &ctx, - }); - var replace = std.AutoArrayHashMap(ResultId, ResultId).init(a); - for (info.entities.keys()) |id| { - const entry = try map.getOrPut(id); - if (entry.found_existing) { - try replace.putNoClobber(id, entry.key_ptr.*); - } - } - - sub_node.setEstimatedTotalItems(binary.instructions.len); - - // Now process the module, and replace instructions where needed. - var section = Section{}; - var it = binary.iterateInstructions(); - var new_functions_section: ?usize = null; - var new_operands = std.ArrayList(u32).init(a); - var emitted_ptrs = std.AutoHashMap(ResultId, void).init(a); - while (it.next()) |inst| { - defer sub_node.setCompletedItems(inst.offset); - - // Result-id can only be the first or second operand - const inst_spec = parser.getInstSpec(inst.opcode).?; - - const maybe_result_id_offset: ?u16 = for (0..2) |i| { - if (inst_spec.operands.len > i and inst_spec.operands[i].kind == .id_result) { - break @intCast(i); - } - } else null; - - if (maybe_result_id_offset) |offset| { - const result_id: ResultId = @enumFromInt(inst.operands[offset]); - if (replace.contains(result_id)) continue; - } - - switch (inst.opcode) { - .OpFunction => if (new_functions_section == null) { - new_functions_section = section.instructions.items.len; - }, - .OpTypeForwardPointer => continue, // We re-emit these where needed - else => {}, - } - - switch (inst.opcode.class()) { - .annotation, .debug => { - // For decoration-style instructions, only emit them - // if the target is not removed. - const target: ResultId = @enumFromInt(inst.operands[0]); - if (replace.contains(target)) continue; - }, - else => {}, - } - - // Re-emit the instruction, but replace all the IDs. - - new_operands.items.len = 0; - try new_operands.appendSlice(inst.operands); - - for (new_operands.items, 0..) |*operand, i| { - const is_id = info.operand_is_id.isSet(inst.offset + 1 + i); - if (!is_id) continue; - - if (replace.get(@enumFromInt(operand.*))) |new_id| { - operand.* = @intFromEnum(new_id); - } - - if (maybe_result_id_offset == null or maybe_result_id_offset.? != i) { - // Only emit forward pointers before type, constant, and global instructions. - // Debug and Annotation instructions don't need the forward pointer, and it - // messes up the logical layout of the module. - switch (inst.opcode.class()) { - .type_declaration, .constant_creation, .memory => {}, - else => continue, - } - - const id: ResultId = @enumFromInt(operand.*); - const index = info.entities.getIndex(id) orelse continue; - const entity = info.entities.values()[index]; - if (entity.kind == .OpTypePointer and !emitted_ptrs.contains(id)) { - // Grab the pointer's storage class from its operands in the original - // module. - const storage_class: spec.StorageClass = @enumFromInt(entity.operands(binary)[1]); - try section.emit(a, .OpTypeForwardPointer, .{ - .pointer_type = id, - .storage_class = storage_class, - }); - try emitted_ptrs.put(id, {}); - } - } - } - - if (inst.opcode == .OpTypePointer) { - const result_id: ResultId = @enumFromInt(new_operands.items[maybe_result_id_offset.?]); - try emitted_ptrs.put(result_id, {}); - } - - try section.emitRawInstruction(a, inst.opcode, new_operands.items); - } - - for (replace.keys()) |key| { - _ = binary.ext_inst_map.remove(key); - _ = binary.arith_type_width.remove(key); - } - - binary.instructions = try parser.a.dupe(Word, section.toWords()); - binary.sections.functions = new_functions_section orelse binary.instructions.len; -} diff --git a/test/behavior/packed-union.zig b/test/behavior/packed-union.zig index 17cfa82cd735..ad31e590babe 100644 --- a/test/behavior/packed-union.zig +++ b/test/behavior/packed-union.zig @@ -140,6 +140,7 @@ test "packed union initialized with a runtime value" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; const Fields = packed struct { timestamp: u50, diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index 22e4e06dfe8d..5d1a0e811448 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -1036,6 +1036,8 @@ test "sentinel-terminated 0-length slices" { } test "peer slices keep abi alignment with empty struct" { + if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; + var cond: bool = undefined; cond = false; const slice = if (cond) &[1]u32{42} else &.{}; diff --git a/tools/gen_spirv_spec.zig b/tools/gen_spirv_spec.zig index c8a3cf5cedc7..912c4e4de1c0 100644 --- a/tools/gen_spirv_spec.zig +++ b/tools/gen_spirv_spec.zig @@ -12,6 +12,7 @@ const ExtendedStructSet = std.StringHashMap(void); const Extension = struct { name: []const u8, + opcode_name: []const u8, spec: ExtensionRegistry, }; @@ -44,23 +45,11 @@ const OperandKindMap = std.ArrayHashMap(StringPair, OperandKind, StringPairConte /// Khronos made it so that these names are not defined explicitly, so /// we need to hardcode it (like they did). -/// See https://github.com/KhronosGroup/SPIRV-Registry/ -const set_names = std.StaticStringMap([]const u8).initComptime(.{ - .{ "opencl.std.100", "OpenCL.std" }, - .{ "glsl.std.450", "GLSL.std.450" }, - .{ "opencl.debuginfo.100", "OpenCL.DebugInfo.100" }, - .{ "spv-amd-shader-ballot", "SPV_AMD_shader_ballot" }, - .{ "nonsemantic.shader.debuginfo.100", "NonSemantic.Shader.DebugInfo.100" }, - .{ "nonsemantic.vkspreflection", "NonSemantic.VkspReflection" }, - .{ "nonsemantic.clspvreflection", "NonSemantic.ClspvReflection.6" }, // This version needs to be handled manually - .{ "spv-amd-gcn-shader", "SPV_AMD_gcn_shader" }, - .{ "spv-amd-shader-trinary-minmax", "SPV_AMD_shader_trinary_minmax" }, - .{ "debuginfo", "DebugInfo" }, - .{ "nonsemantic.debugprintf", "NonSemantic.DebugPrintf" }, - .{ "spv-amd-shader-explicit-vertex-parameter", "SPV_AMD_shader_explicit_vertex_parameter" }, - .{ "nonsemantic.debugbreak", "NonSemantic.DebugBreak" }, - .{ "tosa.001000.1", "SPV_EXT_INST_TYPE_TOSA_001000_1" }, - .{ "zig", "zig" }, +/// See https://github.com/KhronosGroup/SPIRV-Registry +const set_names = std.StaticStringMap(struct { []const u8, []const u8 }).initComptime(.{ + .{ "opencl.std.100", .{ "OpenCL.std", "OpenClOpcode" } }, + .{ "glsl.std.450", .{ "GLSL.std.450", "GlslOpcode" } }, + .{ "zig", .{ "zig", "Zig" } }, }); var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator); @@ -78,7 +67,7 @@ pub fn main() !void { const dir = try std.fs.cwd().openDir(json_path, .{ .iterate = true }); const core_spec = try readRegistry(CoreRegistry, dir, "spirv.core.grammar.json"); - std.sort.block(Instruction, core_spec.instructions, CmpInst{}, CmpInst.lt); + std.mem.sortUnstable(Instruction, core_spec.instructions, CmpInst{}, CmpInst.lt); var exts = std.ArrayList(Extension).init(allocator); @@ -134,14 +123,24 @@ fn readExtRegistry(exts: *std.ArrayList(Extension), dir: std.fs.Dir, sub_path: [ const name = filename["extinst.".len .. filename.len - ".grammar.json".len]; const spec = try readRegistry(ExtensionRegistry, dir, sub_path); + const set_name = set_names.get(name) orelse { + std.log.info("ignored instruction set '{s}'", .{name}); + return; + }; + std.sort.block(Instruction, spec.instructions, CmpInst{}, CmpInst.lt); - try exts.append(.{ .name = set_names.get(name).?, .spec = spec }); + try exts.append(.{ + .name = set_name.@"0", + .opcode_name = set_name.@"1", + .spec = spec, + }); } fn readRegistry(comptime RegistryType: type, dir: std.fs.Dir, path: []const u8) !RegistryType { const spec = try dir.readFileAlloc(allocator, path, std.math.maxInt(usize)); // Required for json parsing. + // TODO: ALI @setEvalBranchQuota(10000); var scanner = std.json.Scanner.initCompleteInput(allocator, spec); @@ -191,7 +190,11 @@ fn tagPriorityScore(tag: []const u8) usize { } } -fn render(writer: *std.io.Writer, registry: CoreRegistry, extensions: []const Extension) !void { +fn render( + writer: *std.io.Writer, + registry: CoreRegistry, + extensions: []const Extension, +) !void { try writer.writeAll( \\//! This file is auto-generated by tools/gen_spirv_spec.zig. \\ @@ -221,6 +224,16 @@ fn render(writer: *std.io.Writer, registry: CoreRegistry, extensions: []const Ex \\ } \\}; \\ + \\pub const IdRange = struct { + \\ base: u32, + \\ len: u32, + \\ + \\ pub fn at(range: IdRange, i: usize) Id { + \\ std.debug.assert(i < range.len); + \\ return @enumFromInt(range.base + i); + \\ } + \\}; + \\ \\pub const LiteralInteger = Word; \\pub const LiteralFloat = Word; \\pub const LiteralString = []const u8; @@ -307,13 +320,18 @@ fn render(writer: *std.io.Writer, registry: CoreRegistry, extensions: []const Ex // Note: extensions don't seem to have class. try renderClass(writer, registry.instructions); try renderOperandKind(writer, all_operand_kinds.values()); - try renderOpcodes(writer, registry.instructions, extended_structs); + + try renderOpcodes(writer, "Opcode", true, registry.instructions, extended_structs); + for (extensions) |ext| { + try renderOpcodes(writer, ext.opcode_name, false, ext.spec.instructions, extended_structs); + } + try renderOperandKinds(writer, all_operand_kinds.values(), extended_structs); try renderInstructionSet(writer, registry, extensions, all_operand_kinds); } fn renderInstructionSet( - writer: anytype, + writer: *std.io.Writer, core: CoreRegistry, extensions: []const Extension, all_operand_kinds: OperandKindMap, @@ -324,7 +342,7 @@ fn renderInstructionSet( ); for (extensions) |ext| { - try writer.print("{f},\n", .{formatId(ext.name)}); + try writer.print("{f},\n", .{std.zig.fmtId(ext.name)}); } try writer.writeAll( @@ -348,7 +366,7 @@ fn renderInstructionSet( } fn renderInstructionsCase( - writer: anytype, + writer: *std.io.Writer, set_name: []const u8, instructions: []const Instruction, all_operand_kinds: OperandKindMap, @@ -357,7 +375,7 @@ fn renderInstructionsCase( // but there aren't so many total aliases and that would add more overhead in total. We will // just filter those out when needed. - try writer.print(".{f} => &.{{\n", .{formatId(set_name)}); + try writer.print(".{f} => &.{{\n", .{std.zig.fmtId(set_name)}); for (instructions) |inst| { try writer.print( @@ -395,7 +413,7 @@ fn renderInstructionsCase( ); } -fn renderClass(writer: anytype, instructions: []const Instruction) !void { +fn renderClass(writer: *std.io.Writer, instructions: []const Instruction) !void { var class_map = std.StringArrayHashMap(void).init(allocator); for (instructions) |inst| { @@ -444,7 +462,7 @@ fn formatId(identifier: []const u8) std.fmt.Alt(Formatter, Formatter.format) { return .{ .data = .{ .data = identifier } }; } -fn renderOperandKind(writer: anytype, operands: []const OperandKind) !void { +fn renderOperandKind(writer: *std.io.Writer, operands: []const OperandKind) !void { try writer.writeAll( \\pub const OperandKind = enum { \\ opcode, @@ -500,7 +518,7 @@ fn renderOperandKind(writer: anytype, operands: []const OperandKind) !void { try writer.writeAll("};\n}\n};\n"); } -fn renderEnumerant(writer: anytype, enumerant: Enumerant) !void { +fn renderEnumerant(writer: *std.io.Writer, enumerant: Enumerant) !void { try writer.print(".{{.name = \"{s}\", .value = ", .{enumerant.enumerant}); switch (enumerant.value) { .bitflag => |flag| try writer.writeAll(flag), @@ -517,7 +535,9 @@ fn renderEnumerant(writer: anytype, enumerant: Enumerant) !void { } fn renderOpcodes( - writer: anytype, + writer: *std.io.Writer, + opcode_type_name: []const u8, + want_operands: bool, instructions: []const Instruction, extended_structs: ExtendedStructSet, ) !void { @@ -528,7 +548,9 @@ fn renderOpcodes( try aliases.ensureTotalCapacity(instructions.len); for (instructions, 0..) |inst, i| { - if (std.mem.eql(u8, inst.class.?, "@exclude")) continue; + if (inst.class) |class| { + if (std.mem.eql(u8, class, "@exclude")) continue; + } const result = inst_map.getOrPutAssumeCapacity(inst.opcode); if (!result.found_existing) { @@ -552,58 +574,67 @@ fn renderOpcodes( const instructions_indices = inst_map.values(); - try writer.writeAll("pub const Opcode = enum(u16) {\n"); + try writer.print("\npub const {f} = enum(u16) {{\n", .{std.zig.fmtId(opcode_type_name)}); for (instructions_indices) |i| { const inst = instructions[i]; try writer.print("{f} = {},\n", .{ std.zig.fmtId(inst.opname), inst.opcode }); } - try writer.writeAll( - \\ - ); + try writer.writeAll("\n"); for (aliases.items) |alias| { - try writer.print("pub const {f} = Opcode.{f};\n", .{ + try writer.print("pub const {f} = {f}.{f};\n", .{ formatId(instructions[alias.inst].opname), + std.zig.fmtId(opcode_type_name), formatId(instructions[alias.alias].opname), }); } - try writer.writeAll( - \\ - \\pub fn Operands(comptime self: Opcode) type { - \\ return switch (self) { - \\ - ); + if (want_operands) { + try writer.print( + \\ + \\pub fn Operands(comptime self: {f}) type {{ + \\ return switch (self) {{ + \\ + , .{std.zig.fmtId(opcode_type_name)}); - for (instructions_indices) |i| { - const inst = instructions[i]; - try renderOperand(writer, .instruction, inst.opname, inst.operands, extended_structs, false); - } + for (instructions_indices) |i| { + const inst = instructions[i]; + try renderOperand(writer, .instruction, inst.opname, inst.operands, extended_structs, false); + } - try writer.writeAll( - \\ }; - \\} - \\pub fn class(self: Opcode) Class { - \\ return switch (self) { - \\ - ); + try writer.writeAll( + \\ }; + \\} + \\ + ); - for (instructions_indices) |i| { - const inst = instructions[i]; - try writer.print(".{f} => .{f},\n", .{ std.zig.fmtId(inst.opname), formatId(inst.class.?) }); + try writer.print( + \\pub fn class(self: {f}) Class {{ + \\ return switch (self) {{ + \\ + , .{std.zig.fmtId(opcode_type_name)}); + + for (instructions_indices) |i| { + const inst = instructions[i]; + try writer.print(".{f} => .{f},\n", .{ std.zig.fmtId(inst.opname), formatId(inst.class.?) }); + } + + try writer.writeAll( + \\ }; + \\} + \\ + ); } try writer.writeAll( - \\ }; - \\} \\}; \\ ); } fn renderOperandKinds( - writer: anytype, + writer: *std.io.Writer, kinds: []const OperandKind, extended_structs: ExtendedStructSet, ) !void { @@ -617,7 +648,7 @@ fn renderOperandKinds( } fn renderValueEnum( - writer: anytype, + writer: *std.io.Writer, enumeration: OperandKind, extended_structs: ExtendedStructSet, ) !void { @@ -695,7 +726,7 @@ fn renderValueEnum( } fn renderBitEnum( - writer: anytype, + writer: *std.io.Writer, enumeration: OperandKind, extended_structs: ExtendedStructSet, ) !void { @@ -778,7 +809,7 @@ fn renderBitEnum( } fn renderOperand( - writer: anytype, + writer: *std.io.Writer, kind: enum { @"union", instruction, @@ -862,7 +893,7 @@ fn renderOperand( try writer.writeAll(",\n"); } -fn renderFieldName(writer: anytype, operands: []const Operand, field_index: usize) !void { +fn renderFieldName(writer: *std.io.Writer, operands: []const Operand, field_index: usize) !void { const operand = operands[field_index]; derive_from_kind: {