authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-08 01:37:00+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:51:54+02:00
log3f2025f59e4488bff9e046cabbbff047fa1db508
treeed6f62e5f92e3d593870284c1dedbdb0fb6082f0
parent405f7298acaa4818a26fdc93991c48705c19de15
signaturelock-open Commit is signed but in an unrecognized format.

spirv: emit interface variables for entry points

Also actually implement generating the OpEntryPoint instructions.

3 files changed, 101 insertions(+), 18 deletions(-)

src/codegen/spirv.zig+15-9
......@@ -1343,7 +1343,7 @@ pub const DeclGen = struct {
13431343 /// OpFunctionEnd
13441344 /// TODO is to also write out the error as a function call parameter, and to somehow fetch
13451345 /// the name of an error in the text executor.
1346 fn generateTestEntryPoint(self: *DeclGen, name: []const u8, func: IdResult) !void {
1346 fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void {
13471347 const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct);
13481348 const ptr_anyerror_ty_ref = try self.spv.ptrType(anyerror_ty_ref, .CrossWorkgroup, null);
13491349 const void_ty_ref = try self.resolveType(Type.void, .direct);
......@@ -1357,7 +1357,11 @@ pub const DeclGen = struct {
13571357 break :blk try self.spv.resolveType(SpvType.initPayload(&proto_payload.base));
13581358 };
13591359
1360 const kernel_id = self.spv.allocId();
1360 const test_id = self.spv.declPtr(spv_test_decl_index).result_id;
1361
1362 const spv_decl_index = try self.spv.allocDecl(.func);
1363 const kernel_id = self.spv.declPtr(spv_decl_index).result_id;
1364
13611365 const error_id = self.spv.allocId();
13621366 const p_error_id = self.spv.allocId();
13631367
......@@ -1378,7 +1382,7 @@ pub const DeclGen = struct {
13781382 try section.emit(self.spv.gpa, .OpFunctionCall, .{
13791383 .id_result_type = self.typeId(anyerror_ty_ref),
13801384 .id_result = error_id,
1381 .function = func,
1385 .function = test_id,
13821386 });
13831387 try section.emit(self.spv.gpa, .OpStore, .{
13841388 .pointer = p_error_id,
......@@ -1387,11 +1391,13 @@ pub const DeclGen = struct {
13871391 try section.emit(self.spv.gpa, .OpReturn, {});
13881392 try section.emit(self.spv.gpa, .OpFunctionEnd, {});
13891393
1390 try self.spv.sections.entry_points.emit(self.spv.gpa, .OpEntryPoint, .{
1391 .execution_model = .Kernel,
1392 .entry_point = kernel_id,
1393 .name = name,
1394 });
1394 try self.spv.declareDeclDeps(spv_decl_index, &.{spv_test_decl_index});
1395
1396 // Just generate a quick other name because the intel runtime crashes when the entry-
1397 // point name is the same as a different OpName.
1398 const test_name = try std.fmt.allocPrint(self.gpa, "test {s}", .{name});
1399 defer self.gpa.free(test_name);
1400 try self.spv.declareEntryPoint(spv_decl_index, test_name);
13951401 }
13961402
13971403 fn genDecl(self: *DeclGen) !void {
......@@ -1451,7 +1457,7 @@ pub const DeclGen = struct {
14511457 });
14521458
14531459 if (self.module.test_functions.contains(self.decl_index)) {
1454 try self.generateTestEntryPoint(fqn, decl_id);
1460 try self.generateTestEntryPoint(fqn, spv_decl_index);
14551461 }
14561462 } else {
14571463 const init_val = if (decl.val.castTag(.variable)) |payload|
src/codegen/spirv/Assembler.zig+3-1
......@@ -435,10 +435,12 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
435435 .Annotation => &self.spv.sections.annotations,
436436 .TypeDeclaration => unreachable, // Handled elsewhere.
437437 else => switch (self.inst.opcode) {
438 .OpEntryPoint => &self.spv.sections.entry_points,
438 // TODO: This should emit a proper entry point.
439 .OpEntryPoint => unreachable, // &self.spv.sections.entry_points,
439440 .OpExecutionMode, .OpExecutionModeId => &self.spv.sections.execution_modes,
440441 .OpVariable => switch (@intToEnum(spec.StorageClass, operands[2].value)) {
441442 .Function => &self.func.prologue,
443 // TODO: Emit a decl dependency
442444 else => &self.spv.sections.types_globals_constants,
443445 },
444446 // Default case - to be worked out further.
src/codegen/spirv/Module.zig+83-8
......@@ -93,6 +93,14 @@ pub const Global = struct {
9393 end_inst: u32,
9494};
9595
96/// This models a kernel entry point.
97pub const EntryPoint = struct {
98 /// The declaration that should be exported.
99 decl_index: Decl.Index,
100 /// The name of the kernel to be exported.
101 name: []const u8,
102};
103
96104/// A general-purpose allocator which may be used to allocate resources for this module
97105gpa: Allocator,
98106
......@@ -107,8 +115,7 @@ sections: struct {
107115 extensions: Section = .{},
108116 // OpExtInstImport instructions - skip for now.
109117 // memory model defined by target, not required here.
110 /// OpEntryPoint instructions.
111 entry_points: Section = .{},
118 /// OpEntryPoint instructions - Handled by `self.entry_points`.
112119 /// OpExecutionMode and OpExecutionModeId instructions.
113120 execution_modes: Section = .{},
114121 /// OpString, OpSourcExtension, OpSource, OpSourceContinued.
......@@ -143,8 +150,13 @@ type_cache: TypeCache = .{},
143150/// Set of Decls, referred to by Decl.Index.
144151decls: std.ArrayListUnmanaged(Decl) = .{},
145152
153/// List of dependencies, per decl. This list holds all the dependencies, sliced by the
154/// begin_dep and end_dep in `self.decls`.
146155decl_deps: std.ArrayListUnmanaged(Decl.Index) = .{},
147156
157/// The list of entry points that should be exported from this module.
158entry_points: std.ArrayListUnmanaged(EntryPoint) = .{},
159
148160/// The fields in this structure help to maintain the required order for global variables.
149161globals: struct {
150162 /// Set of globals, referred to by Decl.Index.
......@@ -166,7 +178,6 @@ pub fn init(gpa: Allocator, arena: Allocator) Module {
166178pub fn deinit(self: *Module) void {
167179 self.sections.capabilities.deinit(self.gpa);
168180 self.sections.extensions.deinit(self.gpa);
169 self.sections.entry_points.deinit(self.gpa);
170181 self.sections.execution_modes.deinit(self.gpa);
171182 self.sections.debug_strings.deinit(self.gpa);
172183 self.sections.debug_names.deinit(self.gpa);
......@@ -180,6 +191,8 @@ pub fn deinit(self: *Module) void {
180191 self.decls.deinit(self.gpa);
181192 self.decl_deps.deinit(self.gpa);
182193
194 self.entry_points.deinit(self.gpa);
195
183196 self.globals.globals.deinit(self.gpa);
184197 self.globals.section.deinit(self.gpa);
185198
......@@ -202,16 +215,16 @@ pub fn idBound(self: Module) Word {
202215
203216fn orderGlobalsInto(
204217 self: *Module,
205 index: Decl.Index,
218 decl_index: Decl.Index,
206219 section: *Section,
207220 seen: *std.DynamicBitSetUnmanaged,
208221) !void {
209 const decl = self.declPtr(index);
222 const decl = self.declPtr(decl_index);
210223 const deps = self.decl_deps.items[decl.begin_dep..decl.end_dep];
211 const global = self.globalPtr(index).?;
224 const global = self.globalPtr(decl_index).?;
212225 const insts = self.globals.section.instructions.items[global.begin_inst..global.end_inst];
213226
214 seen.set(@enumToInt(index));
227 seen.set(@enumToInt(decl_index));
215228
216229 for (deps) |dep| {
217230 if (!seen.isSet(@enumToInt(dep))) {
......@@ -229,6 +242,8 @@ fn orderGlobals(self: *Module) !Section {
229242 defer seen.deinit(self.gpa);
230243
231244 var ordered_globals = Section{};
245 errdefer ordered_globals.deinit(self.gpa);
246
232247 for (globals) |decl_index| {
233248 if (!seen.isSet(@enumToInt(decl_index))) {
234249 try self.orderGlobalsInto(decl_index, &ordered_globals, &seen);
......@@ -238,6 +253,56 @@ fn orderGlobals(self: *Module) !Section {
238253 return ordered_globals;
239254}
240255
256fn addEntryPointDeps(
257 self: *Module,
258 decl_index: Decl.Index,
259 seen: *std.DynamicBitSetUnmanaged,
260 interface: *std.ArrayList(IdRef),
261) !void {
262 const decl = self.declPtr(decl_index);
263 const deps = self.decl_deps.items[decl.begin_dep..decl.end_dep];
264
265 seen.set(@enumToInt(decl_index));
266
267 if (self.globalPtr(decl_index)) |global| {
268 try interface.append(global.result_id);
269 }
270
271 for (deps) |dep| {
272 if (!seen.isSet(@enumToInt(dep))) {
273 try self.addEntryPointDeps(dep, seen, interface);
274 }
275 }
276}
277
278fn entryPoints(self: *Module) !Section {
279 var entry_points = Section{};
280 errdefer entry_points.deinit(self.gpa);
281
282 var interface = std.ArrayList(IdRef).init(self.gpa);
283 defer interface.deinit();
284
285 var seen = try std.DynamicBitSetUnmanaged.initEmpty(self.gpa, self.decls.items.len);
286 defer seen.deinit(self.gpa);
287
288 for (self.entry_points.items) |entry_point| {
289 interface.items.len = 0;
290 seen.setRangeValue(.{ .start = 0, .end = self.decls.items.len }, false);
291
292 try self.addEntryPointDeps(entry_point.decl_index, &seen, &interface);
293
294 const entry_point_id = self.declPtr(entry_point.decl_index).result_id;
295 try entry_points.emit(self.gpa, .OpEntryPoint, .{
296 .execution_model = .Kernel,
297 .entry_point = entry_point_id,
298 .name = entry_point.name,
299 .interface = interface.items,
300 });
301 }
302
303 return entry_points;
304}
305
241306/// Emit this module as a spir-v binary.
242307pub fn flush(self: *Module, file: std.fs.File) !void {
243308 // See SPIR-V Spec section 2.3, "Physical Layout of a SPIR-V Module and Instruction"
......@@ -256,12 +321,15 @@ pub fn flush(self: *Module, file: std.fs.File) !void {
256321 var globals = try self.orderGlobals();
257322 defer globals.deinit(self.gpa);
258323
324 var entry_points = try self.entryPoints();
325 defer entry_points.deinit(self.gpa);
326
259327 // Note: needs to be kept in order according to section 2.3!
260328 const buffers = &[_][]const Word{
261329 &header,
262330 self.sections.capabilities.toWords(),
263331 self.sections.extensions.toWords(),
264 self.sections.entry_points.toWords(),
332 entry_points.toWords(),
265333 self.sections.execution_modes.toWords(),
266334 self.sections.debug_strings.toWords(),
267335 self.sections.debug_names.toWords(),
......@@ -795,3 +863,10 @@ pub fn endGlobal(self: *Module, global_index: Decl.Index, begin_inst: u32) void
795863 global.begin_inst = begin_inst;
796864 global.end_inst = @intCast(u32, self.globals.section.instructions.items.len);
797865}
866
867pub fn declareEntryPoint(self: *Module, decl_index: Decl.Index, name: []const u8) !void {
868 try self.entry_points.append(self.gpa, .{
869 .decl_index = decl_index,
870 .name = try self.arena.dupe(u8, name),
871 });
872}