mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
spirv: refactor
This commit is contained in:
parent
982c387753
commit
31de2c873f
19 changed files with 8319 additions and 8719 deletions
|
|
@ -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(),
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
6465
src/arch/spirv/CodeGen.zig
Normal file
6465
src/arch/spirv/CodeGen.zig
Normal file
File diff suppressed because it is too large
Load diff
755
src/arch/spirv/Module.zig
Normal file
755
src/arch/spirv/Module.zig
Normal file
|
|
@ -0,0 +1,755 @@
|
|||
//! 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();
|
||||
|
||||
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 InternPool = @import("../../InternPool.zig");
|
||||
const spec = @import("spec.zig");
|
||||
const Word = spec.Word;
|
||||
const Id = spec.Id;
|
||||
|
||||
const Section = @import("Section.zig");
|
||||
|
||||
/// Declarations, both functions and globals, can have dependencies. These are used for 2 things:
|
||||
/// - Globals must be declared before they are used, also between globals. The compiler processes
|
||||
/// globals unordered, so we must use the dependencies here to figure out how to order the globals
|
||||
/// in the final module. The Globals structure is also used for that.
|
||||
/// - Entry points must declare the complete list of OpVariable instructions that they access.
|
||||
/// For these we use the same dependency structure.
|
||||
/// In this mechanism, globals will only depend on other globals, while functions may depend on
|
||||
/// globals or other functions.
|
||||
pub const Decl = struct {
|
||||
/// Index to refer to a Decl by.
|
||||
pub const Index = enum(u32) { _ };
|
||||
|
||||
/// Useful to tell what kind of decl this is, and hold the result-id or field index
|
||||
/// to be used for this decl.
|
||||
pub const Kind = enum {
|
||||
func,
|
||||
global,
|
||||
invocation_global,
|
||||
};
|
||||
|
||||
/// See comment on Kind
|
||||
kind: Kind,
|
||||
/// The result-id associated to this decl. The specific meaning of this depends on `kind`:
|
||||
/// - For `func`, this is the result-id of the associated OpFunction instruction.
|
||||
/// - For `global`, this is the result-id of the associated OpVariable instruction.
|
||||
/// - 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,
|
||||
/// The past-end offset of the dependencies of this decl in the `decl_deps` array.
|
||||
end_dep: u32,
|
||||
};
|
||||
|
||||
/// This models a kernel entry point.
|
||||
pub const EntryPoint = struct {
|
||||
/// The declaration that should be exported.
|
||||
decl_index: Decl.Index,
|
||||
/// The name of the kernel to be exported.
|
||||
name: []const u8,
|
||||
/// Calling Convention
|
||||
exec_model: spec.ExecutionModel,
|
||||
exec_mode: ?spec.ExecutionMode = null,
|
||||
};
|
||||
|
||||
gpa: Allocator,
|
||||
target: *const std.Target,
|
||||
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 { InternPool.Index, spec.StorageClass, Repr },
|
||||
struct { ty_id: Id, fwd_emitted: bool },
|
||||
) = .{},
|
||||
/// 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,
|
||||
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 },
|
||||
} = .{},
|
||||
/// 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 = .{},
|
||||
} = .{},
|
||||
|
||||
/// 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,
|
||||
};
|
||||
|
||||
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.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.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.decls.deinit(module.gpa);
|
||||
module.decl_deps.deinit(module.gpa);
|
||||
|
||||
for (module.entry_points.values()) |ep| {
|
||||
module.gpa.free(ep.name);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
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 allocId(module: *Module) Id {
|
||||
return module.allocIds(1).at(0);
|
||||
}
|
||||
|
||||
pub fn idBound(module: Module) Word {
|
||||
return module.next_result_id;
|
||||
}
|
||||
|
||||
pub fn addEntryPointDeps(
|
||||
module: *Module,
|
||||
decl_index: Decl.Index,
|
||||
seen: *std.DynamicBitSetUnmanaged,
|
||||
interface: *std.ArrayList(Id),
|
||||
) !void {
|
||||
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;
|
||||
}
|
||||
|
||||
seen.set(@intFromEnum(decl_index));
|
||||
|
||||
if (decl.kind == .global) {
|
||||
try interface.append(decl.result_id);
|
||||
}
|
||||
|
||||
for (deps) |dep| {
|
||||
try module.addEntryPointDeps(dep, seen, interface);
|
||||
}
|
||||
}
|
||||
|
||||
fn entryPoints(module: *Module) !Section {
|
||||
var entry_points = Section{};
|
||||
errdefer entry_points.deinit(module.gpa);
|
||||
|
||||
var interface = std.ArrayList(Id).init(module.gpa);
|
||||
defer interface.deinit();
|
||||
|
||||
var seen = try std.DynamicBitSetUnmanaged.initEmpty(module.gpa, module.decls.items.len);
|
||||
defer seen.deinit(module.gpa);
|
||||
|
||||
for (module.entry_points.keys(), module.entry_points.values()) |entry_point_id, entry_point| {
|
||||
interface.items.len = 0;
|
||||
seen.setRangeValue(.{ .start = 0, .end = module.decls.items.len }, false);
|
||||
|
||||
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,
|
||||
.interface = interface.items,
|
||||
});
|
||||
|
||||
if (entry_point.exec_mode == null and entry_point.exec_model == .fragment) {
|
||||
switch (module.target.os.tag) {
|
||||
.vulkan, .opengl => |tag| {
|
||||
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,
|
||||
});
|
||||
},
|
||||
.opencl => {},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entry_points;
|
||||
}
|
||||
|
||||
pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
|
||||
const target = module.target;
|
||||
|
||||
// Emit capabilities and extensions
|
||||
switch (target.os.tag) {
|
||||
.opengl => {
|
||||
try module.addCapability(.shader);
|
||||
try module.addCapability(.matrix);
|
||||
},
|
||||
.vulkan => {
|
||||
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 module.addCapability(.kernel);
|
||||
try module.addCapability(.addresses);
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
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)) try module.addCapability(.float16);
|
||||
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 (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 module.addCapability(.int8);
|
||||
try module.addCapability(.int16);
|
||||
|
||||
// Emit memory model
|
||||
const addressing_model: spec.AddressingModel = switch (target.os.tag) {
|
||||
.opengl => .logical,
|
||||
.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 module.sections.memory_model.emit(module.gpa, .OpMemoryModel, .{
|
||||
.addressing_model = addressing_model,
|
||||
.memory_model = switch (target.os.tag) {
|
||||
.opencl => .open_cl,
|
||||
.vulkan, .opengl => .glsl450,
|
||||
else => unreachable,
|
||||
},
|
||||
});
|
||||
|
||||
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,
|
||||
version.toWord(),
|
||||
spec.zig_generator_id,
|
||||
module.idBound(),
|
||||
0, // Schema (currently reserved for future use)
|
||||
};
|
||||
|
||||
var source = Section{};
|
||||
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
|
||||
// correctly.
|
||||
// See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188
|
||||
.file = null,
|
||||
.source = null,
|
||||
});
|
||||
|
||||
// Note: needs to be kept in order according to section 2.3!
|
||||
const buffers = &[_][]const Word{
|
||||
&header,
|
||||
module.sections.capabilities.toWords(),
|
||||
module.sections.extensions.toWords(),
|
||||
module.sections.extended_instruction_set.toWords(),
|
||||
module.sections.memory_model.toWords(),
|
||||
entry_points.toWords(),
|
||||
module.sections.execution_modes.toWords(),
|
||||
source.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 gpa.alloc(Word, total_result_size);
|
||||
errdefer comptime unreachable;
|
||||
|
||||
var offset: usize = 0;
|
||||
for (buffers) |buffer| {
|
||||
@memcpy(result[offset..][0..buffer.len], buffer);
|
||||
offset += buffer.len;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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 module.sections.capabilities.emit(module.gpa, .OpCapability, .{ .capability = cap });
|
||||
}
|
||||
|
||||
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 module.sections.extensions.emit(module.gpa, .OpExtension, .{ .name = ext });
|
||||
}
|
||||
|
||||
/// Imports or returns the existing id of an extended instruction set
|
||||
pub fn importInstructionSet(module: *Module, set: spec.InstructionSet) !Id {
|
||||
assert(set != .core);
|
||||
|
||||
const gop = try module.cache.extended_instruction_set.getOrPut(module.gpa, set);
|
||||
if (gop.found_existing) return gop.value_ptr.*;
|
||||
|
||||
const result_id = module.allocId();
|
||||
try module.sections.extended_instruction_set.emit(module.gpa, .OpExtInstImport, .{
|
||||
.id_result = result_id,
|
||||
.name = @tagName(set),
|
||||
});
|
||||
gop.value_ptr.* = result_id;
|
||||
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn structType(module: *Module, result_id: Id, types: []const Id, maybe_names: ?[]const []const u8) !void {
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeStruct, .{
|
||||
.id_result = result_id,
|
||||
.id_ref = types,
|
||||
});
|
||||
|
||||
if (maybe_names) |names| {
|
||||
assert(names.len == types.len);
|
||||
for (names, 0..) |name, i| {
|
||||
try module.memberDebugName(result_id, @intCast(i), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn boolType(module: *Module) !Id {
|
||||
if (module.cache.bool_type) |id| return id;
|
||||
|
||||
const result_id = module.allocId();
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeBool, .{
|
||||
.id_result = result_id,
|
||||
});
|
||||
module.cache.bool_type = result_id;
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn voidType(module: *Module) !Id {
|
||||
if (module.cache.void_type) |id| return id;
|
||||
|
||||
const result_id = module.allocId();
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeVoid, .{
|
||||
.id_result = result_id,
|
||||
});
|
||||
module.cache.void_type = result_id;
|
||||
try module.debugName(result_id, "void");
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn intType(module: *Module, signedness: std.builtin.Signedness, bits: u16) !Id {
|
||||
assert(bits > 0);
|
||||
const entry = try module.cache.int_types.getOrPut(module.gpa, .{ .signedness = signedness, .bits = bits });
|
||||
if (!entry.found_existing) {
|
||||
const result_id = module.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeInt, .{
|
||||
.id_result = result_id,
|
||||
.width = bits,
|
||||
.signedness = switch (signedness) {
|
||||
.signed => 1,
|
||||
.unsigned => 0,
|
||||
},
|
||||
});
|
||||
|
||||
switch (signedness) {
|
||||
.signed => try module.debugNameFmt(result_id, "i{}", .{bits}),
|
||||
.unsigned => try module.debugNameFmt(result_id, "u{}", .{bits}),
|
||||
}
|
||||
}
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
pub fn floatType(module: *Module, bits: u16) !Id {
|
||||
assert(bits > 0);
|
||||
const entry = try module.cache.float_types.getOrPut(module.gpa, .{ .bits = bits });
|
||||
if (!entry.found_existing) {
|
||||
const result_id = module.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeFloat, .{
|
||||
.id_result = result_id,
|
||||
.width = bits,
|
||||
});
|
||||
try module.debugNameFmt(result_id, "f{}", .{bits});
|
||||
}
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
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 = module.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeVector, .{
|
||||
.id_result = result_id,
|
||||
.component_type = child_ty_id,
|
||||
.component_count = len,
|
||||
});
|
||||
}
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
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 = module.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeArray, .{
|
||||
.id_result = result_id,
|
||||
.element_type = child_ty_id,
|
||||
.length = len_id,
|
||||
});
|
||||
}
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
pub fn functionType(module: *Module, return_ty_id: Id, param_type_ids: []const Id) !Id {
|
||||
const result_id = module.allocId();
|
||||
try module.sections.globals.emit(module.gpa, .OpTypeFunction, .{
|
||||
.id_result = result_id,
|
||||
.return_type = return_ty_id,
|
||||
.id_ref_2 = param_type_ids,
|
||||
});
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn constant(module: *Module, result_ty_id: Id, value: spec.LiteralContextDependentNumber) !Id {
|
||||
const result_id = module.allocId();
|
||||
const section = &module.sections.globals;
|
||||
try section.emit(module.gpa, .OpConstant, .{
|
||||
.id_result_type = result_ty_id,
|
||||
.id_result = result_id,
|
||||
.value = value,
|
||||
});
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn constBool(module: *Module, value: bool) !Id {
|
||||
if (module.cache.bool_const[@intFromBool(value)]) |b| return b;
|
||||
|
||||
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 module.sections.globals.emit(
|
||||
module.gpa,
|
||||
if (value_ct) .OpConstantTrue else .OpConstantFalse,
|
||||
.{
|
||||
.id_result_type = result_ty_id,
|
||||
.id_result = result_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(module: *Module, result_ty_id: Id, spirv_builtin: spec.BuiltIn) !Decl.Index {
|
||||
const entry = try module.cache.builtins.getOrPut(module.gpa, .{ result_ty_id, spirv_builtin });
|
||||
if (!entry.found_existing) {
|
||||
const decl_index = try module.allocDecl(.global);
|
||||
const result_id = module.declPtr(decl_index).result_id;
|
||||
entry.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,
|
||||
});
|
||||
try module.decorate(result_id, .{ .built_in = .{ .built_in = spirv_builtin } });
|
||||
try module.declareDeclDeps(decl_index, &.{});
|
||||
}
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
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(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,
|
||||
});
|
||||
return result_id;
|
||||
}
|
||||
|
||||
/// Decorate a result-id.
|
||||
pub fn decorate(
|
||||
module: *Module,
|
||||
target: Id,
|
||||
decoration: spec.Decoration.Extended,
|
||||
) !void {
|
||||
const entry = try module.cache.decorations.getOrPut(module.gpa, .{ target, decoration });
|
||||
if (!entry.found_existing) {
|
||||
try module.sections.annotations.emit(module.gpa, .OpDecorate, .{
|
||||
.target = target,
|
||||
.decoration = decoration,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(
|
||||
module: *Module,
|
||||
structure_type: Id,
|
||||
member: u32,
|
||||
decoration: spec.Decoration.Extended,
|
||||
) !void {
|
||||
try module.sections.annotations.emit(module.gpa, .OpMemberDecorate, .{
|
||||
.structure_type = structure_type,
|
||||
.member = member,
|
||||
.decoration = decoration,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn allocDecl(module: *Module, kind: Decl.Kind) !Decl.Index {
|
||||
try module.decls.append(module.gpa, .{
|
||||
.kind = kind,
|
||||
.result_id = module.allocId(),
|
||||
.begin_dep = undefined,
|
||||
.end_dep = undefined,
|
||||
});
|
||||
|
||||
return @as(Decl.Index, @enumFromInt(@as(u32, @intCast(module.decls.items.len - 1))));
|
||||
}
|
||||
|
||||
pub fn declPtr(module: *Module, index: Decl.Index) *Decl {
|
||||
return &module.decls.items[@intFromEnum(index)];
|
||||
}
|
||||
|
||||
/// Declare ALL dependencies for a decl.
|
||||
pub fn declareDeclDeps(module: *Module, decl_index: Decl.Index, deps: []const Decl.Index) !void {
|
||||
const begin_dep: u32 = @intCast(module.decl_deps.items.len);
|
||||
try module.decl_deps.appendSlice(module.gpa, deps);
|
||||
const end_dep: u32 = @intCast(module.decl_deps.items.len);
|
||||
|
||||
const decl = module.declPtr(decl_index);
|
||||
decl.begin_dep = begin_dep;
|
||||
decl.end_dep = end_dep;
|
||||
}
|
||||
|
||||
/// 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(
|
||||
module: *Module,
|
||||
decl_index: Decl.Index,
|
||||
name: []const u8,
|
||||
exec_model: spec.ExecutionModel,
|
||||
exec_mode: ?spec.ExecutionMode,
|
||||
) !void {
|
||||
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 = 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(module: *Module, target: Id, name: []const u8) !void {
|
||||
try module.sections.debug_names.emit(module.gpa, .OpName, .{
|
||||
.target = target,
|
||||
.name = 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(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 storageClass(module: *Module, as: std.builtin.AddressSpace) spec.StorageClass {
|
||||
return switch (as) {
|
||||
.generic => if (module.target.cpu.has(.spirv, .generic_pointer)) .generic else .function,
|
||||
.global => switch (module.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,
|
||||
};
|
||||
}
|
||||
|
|
@ -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,7 +20,6 @@ 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;
|
||||
}
|
||||
|
|
@ -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,6 +64,16 @@ pub fn emitRawInstruction(
|
|||
section.writeWords(operands);
|
||||
}
|
||||
|
||||
pub fn emitAssumeCapacity(
|
||||
section: *Section,
|
||||
comptime opcode: spec.Opcode,
|
||||
operands: opcode.Operands(),
|
||||
) !void {
|
||||
const word_count = instructionSize(opcode, operands);
|
||||
section.writeWord(@as(Word, @intCast(word_count << 16)) | @intFromEnum(opcode));
|
||||
section.writeOperands(opcode.Operands(), operands);
|
||||
}
|
||||
|
||||
pub fn emit(
|
||||
section: *Section,
|
||||
allocator: Allocator,
|
||||
|
|
@ -86,25 +96,6 @@ pub fn emitBranch(
|
|||
});
|
||||
}
|
||||
|
||||
pub fn emitSpecConstantOp(
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn writeWord(section: *Section, word: Word) void {
|
||||
section.instructions.appendAssumeCapacity(word);
|
||||
}
|
||||
|
|
@ -126,7 +117,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 +124,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 +156,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 +207,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);
|
||||
return switch (operand) {
|
||||
inline else => |op, tag| {
|
||||
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;
|
||||
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 +239,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 +258,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;
|
||||
},
|
||||
.bool => if (@field(operand, field.name)) {
|
||||
any_set = true;
|
||||
},
|
||||
.bool => {},
|
||||
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,
|
||||
},
|
||||
.@"union" => switch (operand) {
|
||||
inline else => |op, tag| operandsSize(@FieldType(Operand, @tagName(tag)), op) + 1,
|
||||
},
|
||||
});
|
||||
|
||||
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 },
|
||||
else => unreachable,
|
||||
},
|
||||
});
|
||||
|
||||
try testing.expectEqualSlices(Word, &.{
|
||||
(@as(Word, 6) << 16) | @intFromEnum(Opcode.OpExecutionMode),
|
||||
888,
|
||||
@intFromEnum(spec.ExecutionMode.LocalSize),
|
||||
4,
|
||||
8,
|
||||
16,
|
||||
}, section.instructions.items);
|
||||
};
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -5799,20 +5809,20 @@ pub const @"NonSemantic.Shader.DebugInfo.100.DebugImportedEntity" = enum(u32) {
|
|||
};
|
||||
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,
|
||||
SPV_AMD_shader_trinary_minmax,
|
||||
SPV_EXT_INST_TYPE_TOSA_001000_1,
|
||||
@"NonSemantic.VkspReflection",
|
||||
SPV_AMD_shader_explicit_vertex_parameter,
|
||||
DebugInfo,
|
||||
@"NonSemantic.DebugBreak",
|
||||
@"OpenCL.DebugInfo.100",
|
||||
@"NonSemantic.ClspvReflection.6",
|
||||
@"GLSL.std.450",
|
||||
SPV_AMD_shader_ballot,
|
||||
@"NonSemantic.DebugPrintf",
|
||||
SPV_AMD_gcn_shader,
|
||||
@"OpenCL.std",
|
||||
@"NonSemantic.Shader.DebugInfo.100",
|
||||
zig,
|
||||
|
||||
pub fn instructions(self: InstructionSet) []const Instruction {
|
||||
|
|
@ -14078,7 +14088,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.spv_amd_shader_trinary_minmax => &.{
|
||||
.SPV_AMD_shader_trinary_minmax => &.{
|
||||
.{
|
||||
.name = "FMin3AMD",
|
||||
.opcode = 1,
|
||||
|
|
@ -14161,7 +14171,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.spv_ext_inst_type_tosa_001000_1 => &.{
|
||||
.SPV_EXT_INST_TYPE_TOSA_001000_1 => &.{
|
||||
.{
|
||||
.name = "ARGMAX",
|
||||
.opcode = 0,
|
||||
|
|
@ -14743,7 +14753,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.non_semantic_vksp_reflection => &.{
|
||||
.@"NonSemantic.VkspReflection" => &.{
|
||||
.{
|
||||
.name = "Configuration",
|
||||
.opcode = 1,
|
||||
|
|
@ -14878,7 +14888,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.spv_amd_shader_explicit_vertex_parameter => &.{
|
||||
.SPV_AMD_shader_explicit_vertex_parameter => &.{
|
||||
.{
|
||||
.name = "InterpolateAtVertexAMD",
|
||||
.opcode = 1,
|
||||
|
|
@ -14888,7 +14898,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.debug_info => &.{
|
||||
.DebugInfo => &.{
|
||||
.{
|
||||
.name = "DebugInfoNone",
|
||||
.opcode = 0,
|
||||
|
|
@ -15235,14 +15245,14 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.non_semantic_debug_break => &.{
|
||||
.@"NonSemantic.DebugBreak" => &.{
|
||||
.{
|
||||
.name = "DebugBreak",
|
||||
.opcode = 1,
|
||||
.operands = &.{},
|
||||
},
|
||||
},
|
||||
.open_cl_debug_info_100 => &.{
|
||||
.@"OpenCL.DebugInfo.100" => &.{
|
||||
.{
|
||||
.name = "DebugInfoNone",
|
||||
.opcode = 0,
|
||||
|
|
@ -15629,7 +15639,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.non_semantic_clspv_reflection_6 => &.{
|
||||
.@"NonSemantic.ClspvReflection.6" => &.{
|
||||
.{
|
||||
.name = "Kernel",
|
||||
.opcode = 1,
|
||||
|
|
@ -16044,7 +16054,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.glsl_std_450 => &.{
|
||||
.@"GLSL.std.450" => &.{
|
||||
.{
|
||||
.name = "Round",
|
||||
.opcode = 1,
|
||||
|
|
@ -16652,7 +16662,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.spv_amd_shader_ballot => &.{
|
||||
.SPV_AMD_shader_ballot => &.{
|
||||
.{
|
||||
.name = "SwizzleInvocationsAMD",
|
||||
.opcode = 1,
|
||||
|
|
@ -16686,7 +16696,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.non_semantic_debug_printf => &.{
|
||||
.@"NonSemantic.DebugPrintf" => &.{
|
||||
.{
|
||||
.name = "DebugPrintf",
|
||||
.opcode = 1,
|
||||
|
|
@ -16696,7 +16706,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.spv_amd_gcn_shader => &.{
|
||||
.SPV_AMD_gcn_shader => &.{
|
||||
.{
|
||||
.name = "CubeFaceIndexAMD",
|
||||
.opcode = 1,
|
||||
|
|
@ -16717,7 +16727,7 @@ pub const InstructionSet = enum {
|
|||
.operands = &.{},
|
||||
},
|
||||
},
|
||||
.open_cl_std => &.{
|
||||
.@"OpenCL.std" => &.{
|
||||
.{
|
||||
.name = "acos",
|
||||
.opcode = 0,
|
||||
|
|
@ -17967,7 +17977,7 @@ pub const InstructionSet = enum {
|
|||
},
|
||||
},
|
||||
},
|
||||
.non_semantic_shader_debug_info_100 => &.{
|
||||
.@"NonSemantic.Shader.DebugInfo.100" => &.{
|
||||
.{
|
||||
.name = "DebugInfoNone",
|
||||
.opcode = 0,
|
||||
|
|
@ -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("arch/spirv/CodeGen.zig"),
|
||||
.stage2_wasm => @import("arch/wasm/CodeGen.zig"),
|
||||
.stage2_x86, .stage2_x86_64 => @import("arch/x86_64/CodeGen.zig"),
|
||||
_ => unreachable,
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -1,782 +0,0 @@
|
|||
//! 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();
|
||||
|
||||
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 spec = @import("spec.zig");
|
||||
const Word = spec.Word;
|
||||
const Id = spec.Id;
|
||||
|
||||
const Section = @import("Section.zig");
|
||||
|
||||
/// 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();
|
||||
}
|
||||
|
||||
/// 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;
|
||||
}
|
||||
};
|
||||
|
||||
/// Declarations, both functions and globals, can have dependencies. These are used for 2 things:
|
||||
/// - Globals must be declared before they are used, also between globals. The compiler processes
|
||||
/// globals unordered, so we must use the dependencies here to figure out how to order the globals
|
||||
/// in the final module. The Globals structure is also used for that.
|
||||
/// - Entry points must declare the complete list of OpVariable instructions that they access.
|
||||
/// For these we use the same dependency structure.
|
||||
/// In this mechanism, globals will only depend on other globals, while functions may depend on
|
||||
/// globals or other functions.
|
||||
pub const Decl = struct {
|
||||
/// Index to refer to a Decl by.
|
||||
pub const Index = enum(u32) { _ };
|
||||
|
||||
/// Useful to tell what kind of decl this is, and hold the result-id or field index
|
||||
/// to be used for this decl.
|
||||
pub const Kind = enum {
|
||||
func,
|
||||
global,
|
||||
invocation_global,
|
||||
};
|
||||
|
||||
/// See comment on Kind
|
||||
kind: Kind,
|
||||
/// The result-id associated to this decl. The specific meaning of this depends on `kind`:
|
||||
/// - For `func`, this is the result-id of the associated OpFunction instruction.
|
||||
/// - For `global`, this is the result-id of the associated OpVariable instruction.
|
||||
/// - 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,
|
||||
/// The past-end offset of the dependencies of this decl in the `decl_deps` array.
|
||||
end_dep: u32,
|
||||
};
|
||||
|
||||
/// This models a kernel entry point.
|
||||
pub const EntryPoint = struct {
|
||||
/// The declaration that should be exported.
|
||||
decl_index: ?Decl.Index = null,
|
||||
/// The name of the kernel to be exported.
|
||||
name: ?[]const u8 = null,
|
||||
/// Calling Convention
|
||||
exec_model: ?spec.ExecutionModel = null,
|
||||
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,
|
||||
|
||||
/// 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;
|
||||
};
|
||||
|
||||
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 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);
|
||||
|
||||
self.decls.deinit(self.gpa);
|
||||
self.decl_deps.deinit(self.gpa);
|
||||
self.entry_points.deinit(self.gpa);
|
||||
|
||||
self.arena.deinit();
|
||||
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub const IdRange = struct {
|
||||
base: u32,
|
||||
len: u32,
|
||||
|
||||
pub fn at(range: IdRange, i: usize) Id {
|
||||
assert(i < range.len);
|
||||
return @enumFromInt(range.base + i);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn allocIds(self: *Module, n: u32) IdRange {
|
||||
defer self.next_result_id += n;
|
||||
return .{
|
||||
.base = self.next_result_id,
|
||||
.len = n,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn allocId(self: *Module) Id {
|
||||
return self.allocIds(1).at(0);
|
||||
}
|
||||
|
||||
pub fn idBound(self: Module) Word {
|
||||
return self.next_result_id;
|
||||
}
|
||||
|
||||
pub fn hasFeature(self: *Module, feature: std.Target.spirv.Feature) bool {
|
||||
return self.target.cpu.has(.spirv, feature);
|
||||
}
|
||||
|
||||
fn addEntryPointDeps(
|
||||
self: *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];
|
||||
|
||||
if (seen.isSet(@intFromEnum(decl_index))) {
|
||||
return;
|
||||
}
|
||||
|
||||
seen.set(@intFromEnum(decl_index));
|
||||
|
||||
if (decl.kind == .global) {
|
||||
try interface.append(decl.result_id);
|
||||
}
|
||||
|
||||
for (deps) |dep| {
|
||||
try self.addEntryPointDeps(dep, seen, interface);
|
||||
}
|
||||
}
|
||||
|
||||
fn entryPoints(self: *Module) !Section {
|
||||
var entry_points = Section{};
|
||||
errdefer entry_points.deinit(self.gpa);
|
||||
|
||||
var interface = std.ArrayList(Id).init(self.gpa);
|
||||
defer interface.deinit();
|
||||
|
||||
var seen = try std.DynamicBitSetUnmanaged.initEmpty(self.gpa, self.decls.items.len);
|
||||
defer seen.deinit(self.gpa);
|
||||
|
||||
for (self.entry_points.keys(), self.entry_points.values()) |entry_point_id, entry_point| {
|
||||
interface.items.len = 0;
|
||||
seen.setRangeValue(.{ .start = 0, .end = self.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.?,
|
||||
.entry_point = entry_point_id,
|
||||
.name = entry_point.name.?,
|
||||
.interface = interface.items,
|
||||
});
|
||||
|
||||
if (entry_point.exec_mode == null and entry_point.exec_model == .fragment) {
|
||||
switch (self.target.os.tag) {
|
||||
.vulkan, .opengl => |tag| {
|
||||
try self.sections.execution_modes.emit(self.gpa, .OpExecutionMode, .{
|
||||
.entry_point = entry_point_id,
|
||||
.mode = if (tag == .vulkan) .origin_upper_left else .origin_lower_left,
|
||||
});
|
||||
},
|
||||
.opencl => {},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entry_points;
|
||||
}
|
||||
|
||||
pub fn finalize(self: *Module, a: Allocator) ![]Word {
|
||||
// Emit capabilities and extensions
|
||||
switch (self.target.os.tag) {
|
||||
.opengl => {
|
||||
try self.addCapability(.shader);
|
||||
try self.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);
|
||||
}
|
||||
},
|
||||
.opencl, .amdhsa => {
|
||||
try self.addCapability(.kernel);
|
||||
try self.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 (self.target.cpu.has(.spirv, .arbitrary_precision_integers)) {
|
||||
try self.addExtension("SPV_INTEL_arbitrary_precision_integers");
|
||||
try self.addCapability(.arbitrary_precision_integers_intel);
|
||||
}
|
||||
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);
|
||||
}
|
||||
// These are well supported
|
||||
try self.addCapability(.int8);
|
||||
try self.addCapability(.int16);
|
||||
|
||||
// Emit memory model
|
||||
const addressing_model: spec.AddressingModel = switch (self.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,
|
||||
.amdhsa => .physical64,
|
||||
else => unreachable,
|
||||
};
|
||||
try self.sections.memory_model.emit(self.gpa, .OpMemoryModel, .{
|
||||
.addressing_model = addressing_model,
|
||||
.memory_model = switch (self.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);
|
||||
|
||||
const header = [_]Word{
|
||||
spec.magic_number,
|
||||
self.version.toWord(),
|
||||
spec.zig_generator_id,
|
||||
self.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, .{
|
||||
.source_language = .zig,
|
||||
.version = 0,
|
||||
// We cannot emit these because the Khronos translator does not parse this instruction
|
||||
// correctly.
|
||||
// See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188
|
||||
.file = null,
|
||||
.source = null,
|
||||
});
|
||||
|
||||
// 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(),
|
||||
entry_points.toWords(),
|
||||
self.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(),
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
var offset: usize = 0;
|
||||
for (buffers) |buffer| {
|
||||
@memcpy(result[offset..][0..buffer.len], buffer);
|
||||
offset += buffer.len;
|
||||
}
|
||||
|
||||
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);
|
||||
if (entry.found_existing) return;
|
||||
try self.sections.capabilities.emit(self.gpa, .OpCapability, .{ .capability = cap });
|
||||
}
|
||||
|
||||
pub fn addExtension(self: *Module, ext: []const u8) !void {
|
||||
const entry = try self.cache.extensions.getOrPut(self.gpa, ext);
|
||||
if (entry.found_existing) return;
|
||||
try self.sections.extensions.emit(self.gpa, .OpExtension, .{ .name = ext });
|
||||
}
|
||||
|
||||
/// Imports or returns the existing id of an extended instruction set
|
||||
pub fn importInstructionSet(self: *Module, set: spec.InstructionSet) !Id {
|
||||
assert(set != .core);
|
||||
|
||||
const gop = try self.cache.extended_instruction_set.getOrPut(self.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, .{
|
||||
.id_result = result_id,
|
||||
.name = @tagName(set),
|
||||
});
|
||||
gop.value_ptr.* = result_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 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, .{
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn boolType(self: *Module) !Id {
|
||||
if (self.cache.bool_type) |id| return id;
|
||||
|
||||
const result_id = self.allocId();
|
||||
try self.sections.types_globals_constants.emit(self.gpa, .OpTypeBool, .{
|
||||
.id_result = result_id,
|
||||
});
|
||||
self.cache.bool_type = result_id;
|
||||
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, .{
|
||||
.id_result = result_id,
|
||||
});
|
||||
self.cache.void_type = result_id;
|
||||
try self.debugName(result_id, "void");
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn intType(self: *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 });
|
||||
if (!entry.found_existing) {
|
||||
const result_id = self.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try self.sections.types_globals_constants.emit(self.gpa, .OpTypeInt, .{
|
||||
.id_result = result_id,
|
||||
.width = bits,
|
||||
.signedness = switch (signedness) {
|
||||
.signed => 1,
|
||||
.unsigned => 0,
|
||||
},
|
||||
});
|
||||
|
||||
switch (signedness) {
|
||||
.signed => try self.debugNameFmt(result_id, "i{}", .{bits}),
|
||||
.unsigned => try self.debugNameFmt(result_id, "u{}", .{bits}),
|
||||
}
|
||||
}
|
||||
return entry.value_ptr.*;
|
||||
}
|
||||
|
||||
pub fn floatType(self: *Module, bits: u16) !Id {
|
||||
assert(bits > 0);
|
||||
const entry = try self.cache.float_types.getOrPut(self.gpa, .{ .bits = bits });
|
||||
if (!entry.found_existing) {
|
||||
const result_id = self.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try self.sections.types_globals_constants.emit(self.gpa, .OpTypeFloat, .{
|
||||
.id_result = result_id,
|
||||
.width = bits,
|
||||
});
|
||||
try self.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 });
|
||||
if (!entry.found_existing) {
|
||||
const result_id = self.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try self.sections.types_globals_constants.emit(self.gpa, .OpTypeVector, .{
|
||||
.id_result = result_id,
|
||||
.component_type = child_ty_id,
|
||||
.component_count = len,
|
||||
});
|
||||
}
|
||||
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 });
|
||||
if (!entry.found_existing) {
|
||||
const result_id = self.allocId();
|
||||
entry.value_ptr.* = result_id;
|
||||
try self.sections.types_globals_constants.emit(self.gpa, .OpTypeArray, .{
|
||||
.id_result = result_id,
|
||||
.element_type = child_ty_id,
|
||||
.length = len_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, .{
|
||||
.id_result = result_id,
|
||||
.return_type = return_ty_id,
|
||||
.id_ref_2 = param_type_ids,
|
||||
});
|
||||
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,
|
||||
.id_result = result_id,
|
||||
.value = value,
|
||||
});
|
||||
return result_id;
|
||||
}
|
||||
|
||||
pub fn constBool(self: *Module, value: bool) !Id {
|
||||
if (self.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;
|
||||
|
||||
switch (value) {
|
||||
inline else => |value_ct| try self.sections.types_globals_constants.emit(
|
||||
self.gpa,
|
||||
if (value_ct) .OpConstantTrue else .OpConstantFalse,
|
||||
.{
|
||||
.id_result_type = result_ty_id,
|
||||
.id_result = result_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, .{
|
||||
.id_result_type = result_ty_id,
|
||||
.id_result = result_id,
|
||||
.storage_class = .input,
|
||||
});
|
||||
try self.decorate(result_id, .{ .built_in = .{ .built_in = spirv_builtin } });
|
||||
try self.declareDeclDeps(decl_index, &.{});
|
||||
}
|
||||
return entry.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, .{
|
||||
.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, .{
|
||||
.id_result_type = ty_id,
|
||||
.id_result = result_id,
|
||||
});
|
||||
return result_id;
|
||||
}
|
||||
|
||||
/// Decorate a result-id.
|
||||
pub fn decorate(
|
||||
self: *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, .{
|
||||
.target = target,
|
||||
.decoration = decoration,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
structure_type: Id,
|
||||
member: u32,
|
||||
decoration: spec.Decoration.Extended,
|
||||
) !void {
|
||||
try self.sections.annotations.emit(self.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, .{
|
||||
.kind = kind,
|
||||
.result_id = self.allocId(),
|
||||
.begin_dep = undefined,
|
||||
.end_dep = undefined,
|
||||
});
|
||||
|
||||
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)];
|
||||
}
|
||||
|
||||
/// 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;
|
||||
}
|
||||
|
||||
/// 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,
|
||||
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);
|
||||
gop.value_ptr.decl_index = decl_index;
|
||||
gop.value_ptr.name = try self.arena.allocator().dupe(u8, 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, .{
|
||||
.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 memberDebugName(self: *Module, target: Id, member: u32, name: []const u8) !void {
|
||||
try self.sections.debug_names.emit(self.gpa, .OpMemberName, .{
|
||||
.type = target,
|
||||
.member = member,
|
||||
.name = name,
|
||||
});
|
||||
}
|
||||
|
|
@ -191,6 +191,7 @@ pub const Env = enum {
|
|||
.spirv => switch (feature) {
|
||||
.spirv_backend,
|
||||
.spirv_linker,
|
||||
.legalize,
|
||||
=> true,
|
||||
else => Env.sema.supports(feature),
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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("../arch/spirv/CodeGen.zig");
|
||||
const SpvModule = @import("../arch/spirv/Module.zig");
|
||||
const Section = @import("../arch/spirv/Section.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 spec = @import("../arch/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: SpvModule,
|
||||
|
||||
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,7 +46,7 @@ pub fn createEmpty(
|
|||
else => unreachable, // Caught by Compilation.Config.resolve.
|
||||
}
|
||||
|
||||
const self = try arena.create(SpirV);
|
||||
const self = try arena.create(Linker);
|
||||
self.* = .{
|
||||
.base = .{
|
||||
.tag = .spirv,
|
||||
|
|
@ -85,11 +59,10 @@ pub fn createEmpty(
|
|||
.file = null,
|
||||
.build_id = options.build_id,
|
||||
},
|
||||
.object = codegen.Object.init(gpa, comp.getTarget()),
|
||||
.module = .{ .gpa = gpa, .target = comp.getTarget() },
|
||||
};
|
||||
errdefer self.deinit();
|
||||
|
||||
// TODO: read the file and keep valid parts instead of truncating
|
||||
self.base.file = try emit.root_dir.handle.createFile(emit.sub_path, .{
|
||||
.truncate = true,
|
||||
.read = true,
|
||||
|
|
@ -103,27 +76,77 @@ 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(self: *Linker) void {
|
||||
self.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 genNav(
|
||||
self: *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;
|
||||
|
||||
var nav_gen: CodeGen = .{
|
||||
.pt = pt,
|
||||
.module = &self.module,
|
||||
.owner_nav = nav_index,
|
||||
.air = air,
|
||||
.liveness = liveness,
|
||||
.control_flow = switch (structured_cfg) {
|
||||
true => .{ .structured = .{} },
|
||||
false => .{ .unstructured = .{} },
|
||||
},
|
||||
.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: *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 self.genNav(pt, nav, air.*, liveness.*.?, true);
|
||||
}
|
||||
|
||||
pub fn updateNav(self: *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 self.genNav(pt, nav, undefined, undefined, false);
|
||||
}
|
||||
|
||||
pub fn updateExports(
|
||||
self: *SpirV,
|
||||
self: *Linker,
|
||||
pt: Zcu.PerThread,
|
||||
exported: Zcu.Exported,
|
||||
export_indices: []const Zcu.Export.Index,
|
||||
|
|
@ -134,13 +157,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 self.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 +185,7 @@ pub fn updateExports(
|
|||
|
||||
for (export_indices) |export_idx| {
|
||||
const exp = export_idx.ptr(zcu);
|
||||
try self.object.spv.declareEntryPoint(
|
||||
try self.module.declareEntryPoint(
|
||||
spv_decl_index,
|
||||
exp.opts.name.toSlice(ip),
|
||||
exec_model,
|
||||
|
|
@ -175,7 +198,7 @@ pub fn updateExports(
|
|||
}
|
||||
|
||||
pub fn flush(
|
||||
self: *SpirV,
|
||||
self: *Linker,
|
||||
arena: Allocator,
|
||||
tid: Zcu.PerThread.Id,
|
||||
prog_node: std.Progress.Node,
|
||||
|
|
@ -185,10 +208,6 @@ 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();
|
||||
|
||||
|
|
@ -196,14 +215,13 @@ pub fn flush(
|
|||
defer sub_prog_node.end();
|
||||
|
||||
const comp = self.base.comp;
|
||||
const spv = &self.object.spv;
|
||||
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(self.module.gpa);
|
||||
defer error_info.deinit();
|
||||
|
||||
error_info.writer.writeAll("zig_errors:") catch return error.OutOfMemory;
|
||||
|
|
@ -213,7 +231,6 @@ pub fn flush(
|
|||
// 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,11 +245,11 @@ pub fn flush(
|
|||
}.isValidChar,
|
||||
) catch return error.OutOfMemory;
|
||||
}
|
||||
try spv.sections.debug_strings.emit(gpa, .OpSourceExtension, .{
|
||||
try self.module.sections.debug_strings.emit(gpa, .OpSourceExtension, .{
|
||||
.extension = error_info.getWritten(),
|
||||
});
|
||||
|
||||
const module = try spv.finalize(arena);
|
||||
const module = try self.module.finalize(arena);
|
||||
errdefer arena.free(module);
|
||||
|
||||
const linked_module = self.linkModule(arena, module, sub_prog_node) catch |err| switch (err) {
|
||||
|
|
@ -244,14 +261,14 @@ pub fn flush(
|
|||
return diags.fail("failed to write: {s}", .{@errorName(err)});
|
||||
}
|
||||
|
||||
fn linkModule(self: *SpirV, a: Allocator, module: []Word, progress: std.Progress.Node) ![]Word {
|
||||
fn linkModule(self: *Linker, arena: Allocator, module: []Word, progress: std.Progress.Node) ![]Word {
|
||||
_ = self;
|
||||
|
||||
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);
|
||||
|
||||
|
|
@ -259,5 +276,5 @@ fn linkModule(self: *SpirV, a: Allocator, module: []Word, progress: std.Progress
|
|||
try prune_unused.run(&parser, &binary, progress);
|
||||
try dedup.run(&parser, &binary, progress);
|
||||
|
||||
return binary.finalize(a);
|
||||
return binary.finalize(arena);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ const assert = std.debug.assert;
|
|||
const Allocator = std.mem.Allocator;
|
||||
const log = std.log.scoped(.spirv_parse);
|
||||
|
||||
const spec = @import("../../codegen/spirv/spec.zig");
|
||||
const spec = @import("../../arch/spirv/spec.zig");
|
||||
const Opcode = spec.Opcode;
|
||||
const Word = spec.Word;
|
||||
const InstructionSet = spec.InstructionSet;
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ 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 Section = @import("../../arch/spirv/Section.zig");
|
||||
const spec = @import("../../arch/spirv/spec.zig");
|
||||
const Opcode = spec.Opcode;
|
||||
const ResultId = spec.Id;
|
||||
const Word = spec.Word;
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ const assert = std.debug.assert;
|
|||
const log = std.log.scoped(.spirv_link);
|
||||
|
||||
const BinaryModule = @import("BinaryModule.zig");
|
||||
const Section = @import("../../codegen/spirv/Section.zig");
|
||||
const spec = @import("../../codegen/spirv/spec.zig");
|
||||
const Section = @import("../../arch/spirv/Section.zig");
|
||||
const spec = @import("../../arch/spirv/spec.zig");
|
||||
const ResultId = spec.Id;
|
||||
const Word = spec.Word;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ const assert = std.debug.assert;
|
|||
const log = std.log.scoped(.spirv_link);
|
||||
|
||||
const BinaryModule = @import("BinaryModule.zig");
|
||||
const Section = @import("../../codegen/spirv/Section.zig");
|
||||
const spec = @import("../../codegen/spirv/spec.zig");
|
||||
const Section = @import("../../arch/spirv/Section.zig");
|
||||
const spec = @import("../../arch/spirv/spec.zig");
|
||||
const Opcode = spec.Opcode;
|
||||
const ResultId = spec.Id;
|
||||
const Word = spec.Word;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 &.{};
|
||||
|
|
|
|||
|
|
@ -221,6 +221,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;
|
||||
|
|
@ -324,7 +334,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(
|
||||
|
|
@ -357,7 +367,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(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue