authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:29:39+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:51:53+02:00
log8bbfbfc956af163434c734e196d5c2a77e77ff07
tree8ed95f240d2736a4d82e915ecc9269f15ba834e2
parent80b84355692606ac840584baa62aaafdd8ecd425
signature Commit is signed but in an unrecognized format.

spirv: improve linking globals

SPIR-V globals must be emitted in order, so that any declaration precedes usage. Zig, however, generates globals in random order. To this end we keep for each global a list of dependencies and perform a topological sort when flushing the module.

3 files changed, 298 insertions(+), 127 deletions(-)

src/codegen/spirv.zig+167-121
......@@ -32,12 +32,28 @@ const IncomingBlock = struct {
3232 break_value_id: IdRef,
3333};
3434
35pub const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, struct {
35const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, struct {
3636 label_id: IdRef,
3737 incoming_blocks: *std.ArrayListUnmanaged(IncomingBlock),
3838});
3939
40pub const DeclMap = std.AutoHashMap(Module.Decl.Index, IdResult);
40/// Linking information about a particular decl.
41/// The active field of this enum depends on the type of the corresponding decl.
42const DeclLink = union {
43 /// Linking information about a function.
44 /// Active when the decl is a function.
45 func: struct {
46 /// Result-id of the OpFunction instruction.
47 result_id: IdResult,
48 },
49 /// Linking information about a global. This index points into the
50 /// SPIR-V module's `globals` array.
51 /// Active when the decl is a variable.
52 global: SpvModule.Global.Index,
53};
54
55/// Maps Zig decl indices to linking SPIR-V linking information.
56pub const DeclLinkMap = std.AutoHashMap(Module.Decl.Index, DeclLink);
4157
4258/// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that.
4359pub const DeclGen = struct {
......@@ -61,8 +77,8 @@ pub const DeclGen = struct {
6177 /// Note: If the declaration is not a function, this value will be undefined!
6278 liveness: Liveness,
6379
64 /// Maps Zig Decl indices to SPIR-V result indices.
65 decl_ids: *DeclMap,
80 /// Maps Zig Decl indices to SPIR-V globals.
81 decl_link: *DeclLinkMap,
6682
6783 /// An array of function argument result-ids. Each index corresponds with the
6884 /// function argument of the same index.
......@@ -152,7 +168,7 @@ pub const DeclGen = struct {
152168 allocator: Allocator,
153169 module: *Module,
154170 spv: *SpvModule,
155 decl_ids: *DeclMap,
171 decl_link: *DeclLinkMap,
156172 ) DeclGen {
157173 return .{
158174 .gpa = allocator,
......@@ -161,7 +177,7 @@ pub const DeclGen = struct {
161177 .decl_index = undefined,
162178 .air = undefined,
163179 .liveness = undefined,
164 .decl_ids = decl_ids,
180 .decl_link = decl_link,
165181 .next_arg_index = undefined,
166182 .current_block_label_id = undefined,
167183 .error_msg = undefined,
......@@ -235,7 +251,8 @@ pub const DeclGen = struct {
235251 .function => val.castTag(.function).?.data.owner_decl,
236252 else => unreachable,
237253 };
238 return try self.resolveDecl(fn_decl_index);
254 const link = try self.resolveDecl(fn_decl_index);
255 return link.func.result_id;
239256 }
240257
241258 return try self.constant(ty, val);
......@@ -246,17 +263,22 @@ pub const DeclGen = struct {
246263
247264 /// Fetch or allocate a result id for decl index. This function also marks the decl as alive.
248265 /// Note: Function does not actually generate the decl.
249 fn resolveDecl(self: *DeclGen, decl_index: Module.Decl.Index) !IdResult {
266 fn resolveDecl(self: *DeclGen, decl_index: Module.Decl.Index) !DeclLink {
250267 const decl = self.module.declPtr(decl_index);
251268 self.module.markDeclAlive(decl);
252269
253 const entry = try self.decl_ids.getOrPut(decl_index);
254 if (entry.found_existing) {
255 return entry.value_ptr.*;
256 }
270 const entry = try self.decl_link.getOrPut(decl_index);
257271 const result_id = self.spv.allocId();
258 entry.value_ptr.* = result_id;
259 return result_id;
272
273 if (!entry.found_existing) {
274 if (decl.val.castTag(.function)) |_| {
275 entry.value_ptr.* = .{.func = .{ .result_id = result_id }};
276 } else {
277 entry.value_ptr.* = .{ .global = try self.spv.allocGlobal() };
278 }
279 }
280
281 return entry.value_ptr.*;
260282 }
261283
262284 /// Start a new SPIR-V block, Emits the label of the new block, and stores which
......@@ -363,7 +385,7 @@ pub const DeclGen = struct {
363385 // As of yet, there is no vector support in the self-hosted compiler.
364386 .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}),
365387 // TODO: For which types is this the case?
366 else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmtDebug()}),
388 else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}),
367389 };
368390 }
369391
......@@ -399,7 +421,7 @@ pub const DeclGen = struct {
399421 try self.spv.sections.types_globals_constants.emit(
400422 self.spv.gpa,
401423 .OpUndef,
402 .{ .id_result_type = self.typeId(ty_ref), .id_result = result_id },
424 .{ .id_result_type = self.typeId(ty_ref), .id_result = result_id }
403425 );
404426 return result_id;
405427 }
......@@ -423,6 +445,11 @@ pub const DeclGen = struct {
423445 /// If full, its flushed.
424446 partial_word: std.BoundedArray(u8, @sizeOf(Word)) = .{},
425447
448 /// Utility function to get the section that instructions should be lowered to.
449 fn section(self: *@This()) *SpvSection {
450 return &self.dg.spv.globals.section;
451 }
452
426453 /// Flush the partial_word to the members. If the partial_word is not
427454 /// filled, this adds padding bytes (which are undefined).
428455 fn flush(self: *@This()) !void {
......@@ -438,6 +465,7 @@ pub const DeclGen = struct {
438465
439466 const word = @bitCast(Word, self.partial_word.buffer);
440467 const result_id = self.dg.spv.allocId();
468 // TODO: Integrate with caching mechanism
441469 try self.dg.spv.emitConstant(self.u32_ty_id, result_id, .{ .uint32 = word });
442470 try self.members.append(.{ .ty = self.u32_ty_ref });
443471 try self.initializers.append(result_id);
......@@ -523,10 +551,52 @@ pub const DeclGen = struct {
523551 try self.addBytes(std.mem.asBytes(&int_bits)[0..@intCast(usize, len)]);
524552 }
525553
554 fn addDeclRef(self: *@This(), ty: Type, decl_index: Decl.Index) !void {
555 const dg = self.dg;
556
557 const ty_ref = try self.dg.resolveType(ty, .indirect);
558 const ty_id = dg.typeId(ty_ref);
559
560 const decl = dg.module.declPtr(decl_index);
561 const link = try dg.resolveDecl(decl_index);
562
563 switch (decl.val.tag()) {
564 .function => {
565 // TODO: Properly lower function pointers. For now we are going to hack around it and
566 // just generate an empty pointer. Function pointers are represented by usize for now,
567 // though.
568 try self.addInt(Type.usize, Value.initTag(.zero));
569 return;
570 },
571 .extern_fn => unreachable, // TODO
572 else => {
573 const result_id = dg.spv.allocId();
574 log.debug("addDeclRef {s} = {}", .{ decl.name, result_id.id });
575
576 const global = dg.spv.globalPtr(link.global);
577 try dg.spv.addGlobalDependency(link.global);
578 // TODO: Do we need a storage class cast here?
579 // TODO: We can probably eliminate these casts
580 try dg.spv.globals.section.emitSpecConstantOp(dg.spv.gpa, .OpBitcast, .{
581 .id_result_type = ty_id,
582 .id_result = result_id,
583 .operand = global.result_id,
584 });
585
586 try self.addPtr(ty_ref, result_id);
587 },
588 }
589 }
590
526591 fn lower(self: *@This(), ty: Type, val: Value) !void {
527592 const target = self.dg.getTarget();
528593 const dg = self.dg;
529594
595 if (val.isUndef()) {
596 const size = ty.abiSize(target);
597 return try self.addUndef(size);
598 }
599
530600 switch (ty.zigTypeTag()) {
531601 .Int => try self.addInt(ty, val),
532602 .Bool => try self.addConstBool(val.toBool()),
......@@ -558,22 +628,20 @@ pub const DeclGen = struct {
558628 try self.addByte(@intCast(u8, sentinel.toUnsignedInt(target)));
559629 }
560630 },
631 .bytes => {
632 const bytes = val.castTag(.bytes).?.data;
633 try self.addBytes(bytes);
634 },
561635 else => |tag| return dg.todo("indirect array constant with tag {s}", .{@tagName(tag)}),
562636 },
563637 .Pointer => switch (val.tag()) {
564638 .decl_ref_mut => {
565 const ptr_ty_ref = try dg.resolveType(ty, .indirect);
566 const ptr_id = dg.spv.allocId();
567639 const decl_index = val.castTag(.decl_ref_mut).?.data.decl_index;
568 try dg.genDeclRef(ptr_ty_ref, ptr_id, decl_index);
569 try self.addPtr(ptr_ty_ref, ptr_id);
640 try self.addDeclRef(ty, decl_index);
570641 },
571642 .decl_ref => {
572 const ptr_ty_ref = try dg.resolveType(ty, .indirect);
573 const ptr_id = dg.spv.allocId();
574643 const decl_index = val.castTag(.decl_ref).?.data;
575 try dg.genDeclRef(ptr_ty_ref, ptr_id, decl_index);
576 try self.addPtr(ptr_ty_ref, ptr_id);
644 try self.addDeclRef(ty, decl_index);
577645 },
578646 .slice => {
579647 const slice = val.castTag(.slice).?.data;
......@@ -730,22 +798,31 @@ pub const DeclGen = struct {
730798 // - Underaligned pointers. These need to be packed into the word array by using a mixture of
731799 // OpSpecConstantOp instructions such as OpConvertPtrToU, OpBitcast, OpShift, etc.
732800
733 log.debug("lowerIndirectConstant: ty = {}, val = {}", .{ ty.fmtDebug(), val.fmtDebug() });
801 assert(storage_class != .Generic and storage_class != .Function);
734802
735 const constant_section = &self.spv.sections.types_globals_constants;
803 log.debug("lowerIndirectConstant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtDebug() });
804
805 const section = &self.spv.globals.section;
736806
737807 const ty_ref = try self.resolveType(ty, .indirect);
738808 const ptr_ty_ref = try self.spv.ptrType(ty_ref, storage_class, alignment);
739809
810 const target = self.getTarget();
811
740812 if (val.isUndef()) {
741813 // Special case: the entire value is undefined. In this case, we can just
742814 // generate an OpVariable with no initializer.
743 try constant_section.emit(self.spv.gpa, .OpVariable, .{
815 return try section.emit(self.spv.gpa, .OpVariable, .{
744816 .id_result_type = self.typeId(ptr_ty_ref),
745817 .id_result = result_id,
746818 .storage_class = storage_class,
747819 });
748 return;
820 } else if (ty.abiSize(target) == 0) {
821 // Special case: if the type has no size, then return an undefined pointer.
822 return try section.emit(self.spv.gpa, .OpUndef, .{
823 .id_result_type = self.typeId(ptr_ty_ref),
824 .id_result = result_id,
825 });
749826 }
750827
751828 const u32_ty_ref = try self.intType(.unsigned, 32);
......@@ -757,62 +834,42 @@ pub const DeclGen = struct {
757834 .initializers = std.ArrayList(IdRef).init(self.gpa),
758835 };
759836
760 try icl.lower(ty, val);
761 try icl.flush();
762
763837 defer icl.members.deinit();
764838 defer icl.initializers.deinit();
765839
840 try icl.lower(ty, val);
841 try icl.flush();
842
766843 const constant_struct_ty_ref = try self.spv.simpleStructType(icl.members.items);
767844 const ptr_constant_struct_ty_ref = try self.spv.ptrType(constant_struct_ty_ref, storage_class, alignment);
768845
769846 const constant_struct_id = self.spv.allocId();
770 try constant_section.emit(self.spv.gpa, .OpSpecConstantComposite, .{
847 try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{
771848 .id_result_type = self.typeId(constant_struct_ty_ref),
772849 .id_result = constant_struct_id,
773850 .constituents = icl.initializers.items,
774851 });
775852
776853 const var_id = self.spv.allocId();
777 switch (storage_class) {
778 .Generic => unreachable,
779 .Function => {
780 try self.func.prologue.emit(self.spv.gpa, .OpVariable, .{
781 .id_result_type = self.typeId(ptr_constant_struct_ty_ref),
782 .id_result = var_id,
783 .storage_class = storage_class,
784 .initializer = constant_struct_id,
785 });
786 // TODO: Set alignment of OpVariable.
787
788 try self.func.body.emit(self.spv.gpa, .OpBitcast, .{
789 .id_result_type = self.typeId(ptr_ty_ref),
790 .id_result = result_id,
791 .operand = var_id,
792 });
793 },
794 else => {
795 try constant_section.emit(self.spv.gpa, .OpVariable, .{
796 .id_result_type = self.typeId(ptr_constant_struct_ty_ref),
797 .id_result = var_id,
798 .storage_class = storage_class,
799 .initializer = constant_struct_id,
800 });
801 // TODO: Set alignment of OpVariable.
802
803 try constant_section.emitSpecConstantOp(self.spv.gpa, .OpBitcast, .{
804 .id_result_type = self.typeId(ptr_ty_ref),
805 .id_result = result_id,
806 .operand = var_id,
807 });
808 },
809 }
854 try section.emit(self.spv.gpa, .OpVariable, .{
855 .id_result_type = self.typeId(ptr_constant_struct_ty_ref),
856 .id_result = var_id,
857 .storage_class = storage_class,
858 .initializer = constant_struct_id,
859 });
860 // TODO: Set alignment of OpVariable.
861 // TODO: We may be able to eliminate this cast.
862 try section.emitSpecConstantOp(self.spv.gpa, .OpBitcast, .{
863 .id_result_type = self.typeId(ptr_ty_ref),
864 .id_result = result_id,
865 .operand = var_id,
866 });
810867 }
811868
812869 /// This function generates a load for a constant in direct (ie, non-memory) representation.
813870 /// When the constant is simple, it can be generated directly using OpConstant instructions. When
814871 /// the constant is more complicated however, it needs to be lowered to an indirect constant, which
815 /// is then loaded using OpLoad. Such values are loaded into the Function address space by default.
872 /// is then loaded using OpLoad. Such values are loaded into the UniformConstant storage class by default.
816873 /// This function should only be called during function code generation.
817874 fn constant(self: *DeclGen, ty: Type, val: Value) !IdRef {
818875 const target = self.getTarget();
......@@ -846,53 +903,27 @@ pub const DeclGen = struct {
846903 }
847904 },
848905 else => {
849 // The value cannot be generated directly, so generate it as an indirect function-local
850 // constant, and then perform an OpLoad.
851 const ptr_id = self.spv.allocId();
906 // The value cannot be generated directly, so generate it as an indirect constant,
907 // and then perform an OpLoad.
852908 const alignment = ty.abiAlignment(target);
853 try self.lowerIndirectConstant(ptr_id, ty, val, .Function, alignment);
909 const global_index = try self.spv.allocGlobal();
910 log.debug("constant {}", .{global_index});
911 const ptr_id = self.spv.beginGlobal(global_index);
912 defer self.spv.endGlobal();
913 try self.lowerIndirectConstant(ptr_id, ty, val, .UniformConstant, alignment);
854914 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
855915 .id_result_type = result_ty_id,
856916 .id_result = result_id,
857917 .pointer = ptr_id,
858918 });
859 // TODO: Convert bools? This logic should hook into `load`.
919 // TODO: Convert bools? This logic should hook into `load`. It should be a dead
920 // path though considering .Bool is handled above.
860921 },
861922 }
862923
863924 return result_id;
864925 }
865926
866 fn genDeclRef(self: *DeclGen, result_ty_ref: SpvType.Ref, result_id: IdRef, decl_index: Decl.Index) Error!void {
867 // TODO: Clean up
868 const decl = self.module.declPtr(decl_index);
869 self.module.markDeclAlive(decl);
870 // _ = result_ty_ref;
871 // const decl_id = try self.constant(decl.ty, decl.val, .indirect);
872 // try self.variable(.global, result_id, result_ty_ref, decl_id);
873 const result_storage_class = self.spv.typeRefType(result_ty_ref).payload(.pointer).storage_class;
874 const indirect_result_id = if (result_storage_class != .CrossWorkgroup)
875 self.spv.allocId()
876 else
877 result_id;
878
879 try self.lowerIndirectConstant(
880 indirect_result_id,
881 decl.ty,
882 decl.val,
883 .CrossWorkgroup, // TODO: Make this .Function if required
884 decl.@"align",
885 );
886 const section = &self.spv.sections.types_globals_constants;
887 if (result_storage_class != .CrossWorkgroup) {
888 try section.emitSpecConstantOp(self.spv.gpa, .OpPtrCastToGeneric, .{
889 .id_result_type = self.typeId(result_ty_ref),
890 .id_result = result_id,
891 .pointer = indirect_result_id,
892 });
893 }
894 }
895
896927 /// Turn a Zig type into a SPIR-V Type, and return its type result-id.
897928 fn resolveTypeId(self: *DeclGen, ty: Type) !IdResultType {
898929 const type_ref = try self.resolveType(ty, .direct);
......@@ -996,7 +1027,7 @@ pub const DeclGen = struct {
9961027
9971028 /// Turn a Zig type into a SPIR-V Type, and return a reference to it.
9981029 fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!SpvType.Ref {
999 log.debug("resolveType: ty = {}", .{ty.fmtDebug()});
1030 log.debug("resolveType: ty = {}", .{ty.fmt(self.module)});
10001031 const target = self.getTarget();
10011032 switch (ty.zigTypeTag()) {
10021033 .Void, .NoReturn => return try self.spv.resolveType(SpvType.initTag(.void)),
......@@ -1042,23 +1073,30 @@ pub const DeclGen = struct {
10421073 };
10431074 return try self.spv.arrayType(total_len, elem_ty_ref);
10441075 },
1045 .Fn => {
1046 // TODO: Put this somewhere in Sema.zig
1047 if (ty.fnIsVarArgs())
1048 return self.fail("VarArgs functions are unsupported for SPIR-V", .{});
1076 .Fn => switch (repr) {
1077 .direct => {
1078 // TODO: Put this somewhere in Sema.zig
1079 if (ty.fnIsVarArgs())
1080 return self.fail("VarArgs functions are unsupported for SPIR-V", .{});
10491081
1050 // TODO: Parameter passing convention etc.
1082 // TODO: Parameter passing convention etc.
10511083
1052 const param_types = try self.spv.arena.alloc(SpvType.Ref, ty.fnParamLen());
1053 for (param_types, 0..) |*param, i| {
1054 param.* = try self.resolveType(ty.fnParamType(i), .direct);
1055 }
1084 const param_types = try self.spv.arena.alloc(SpvType.Ref, ty.fnParamLen());
1085 for (param_types, 0..) |*param, i| {
1086 param.* = try self.resolveType(ty.fnParamType(i), .direct);
1087 }
10561088
1057 const return_type = try self.resolveType(ty.fnReturnType(), .direct);
1089 const return_type = try self.resolveType(ty.fnReturnType(), .direct);
10581090
1059 const payload = try self.spv.arena.create(SpvType.Payload.Function);
1060 payload.* = .{ .return_type = return_type, .parameters = param_types };
1061 return try self.spv.resolveType(SpvType.initPayload(&payload.base));
1091 const payload = try self.spv.arena.create(SpvType.Payload.Function);
1092 payload.* = .{ .return_type = return_type, .parameters = param_types };
1093 return try self.spv.resolveType(SpvType.initPayload(&payload.base));
1094 },
1095 .indirect => {
1096 // TODO: Represent function pointers properly.
1097 // For now, just use an usize type.
1098 return try self.sizeType();
1099 },
10621100 },
10631101 .Pointer => {
10641102 const ptr_info = ty.ptrInfo().data;
......@@ -1196,14 +1234,16 @@ pub const DeclGen = struct {
11961234
11971235 fn genDecl(self: *DeclGen) !void {
11981236 const decl = self.module.declPtr(self.decl_index);
1199 const result_id = try self.resolveDecl(self.decl_index);
1237 const link = try self.resolveDecl(self.decl_index);
12001238
12011239 if (decl.val.castTag(.function)) |_| {
1240 log.debug("genDecl function {s} = {}", .{decl.name, link.func.result_id.id});
1241
12021242 assert(decl.ty.zigTypeTag() == .Fn);
12031243 const prototype_id = try self.resolveTypeId(decl.ty);
12041244 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
12051245 .id_result_type = try self.resolveTypeId(decl.ty.fnReturnType()),
1206 .id_result = result_id,
1246 .id_result = link.func.result_id,
12071247 .function_control = .{}, // TODO: We can set inline here if the type requires it.
12081248 .function_type = prototype_id,
12091249 });
......@@ -1243,7 +1283,7 @@ pub const DeclGen = struct {
12431283 defer self.module.gpa.free(fqn);
12441284
12451285 try self.spv.sections.debug_names.emit(self.gpa, .OpName, .{
1246 .target = result_id,
1286 .target = link.func.result_id,
12471287 .name = fqn,
12481288 });
12491289 } else {
......@@ -1264,9 +1304,13 @@ pub const DeclGen = struct {
12641304 else => storage_class,
12651305 };
12661306
1307 const global_result_id = self.spv.beginGlobal(link.global);
1308 defer self.spv.endGlobal();
1309 log.debug("genDecl {}", .{link.global});
1310
12671311 const var_result_id = switch (storage_class) {
12681312 .Generic => self.spv.allocId(),
1269 else => result_id,
1313 else => global_result_id,
12701314 };
12711315
12721316 try self.lowerIndirectConstant(
......@@ -1278,12 +1322,13 @@ pub const DeclGen = struct {
12781322 );
12791323
12801324 if (storage_class == .Generic) {
1281 const section = &self.spv.sections.types_globals_constants;
1325 const section = &self.spv.globals.section;
12821326 const ty_ref = try self.resolveType(decl.ty, .indirect);
12831327 const ptr_ty_ref = try self.spv.ptrType(ty_ref, storage_class, decl.@"align");
1328 // TODO: Can we eliminate this cast?
12841329 try section.emitSpecConstantOp(self.spv.gpa, .OpPtrCastToGeneric, .{
12851330 .id_result_type = self.typeId(ptr_ty_ref),
1286 .id_result = result_id,
1331 .id_result = global_result_id,
12871332 .pointer = var_result_id,
12881333 });
12891334 }
......@@ -1972,6 +2017,7 @@ pub const DeclGen = struct {
19722017 .id_result = result_id,
19732018 .pointer = alloc_result_id,
19742019 }),
2020 // TODO: Can we do without this cast or move it to runtime?
19752021 else => try section.emitSpecConstantOp(self.spv.gpa, .OpPtrCastToGeneric, .{
19762022 .id_result_type = self.typeId(ptr_ty_ref),
19772023 .id_result = result_id,
src/codegen/spirv/Module.zig+126-1
......@@ -55,6 +55,27 @@ pub const Fn = struct {
5555 }
5656};
5757
58/// Globals must be kept in order: operations involving globals must be ordered
59/// so that the global declaration precedes any usage.
60pub const Global = struct {
61 /// Index type to refer to a global by.
62 pub const Index = enum(u32) { _ };
63
64 /// The result-id to be used for this global declaration. Note that this does not
65 /// necessarily refer to an OpVariable instruction - it may also be the final result
66 /// id of a number of OpSpecConstantOp instructions.
67 result_id: IdRef,
68 /// The offset into `self.globals.section` of the first instruction of this global
69 /// declaration.
70 begin_inst: u32,
71 /// The past-end offset into `self.flobals.section`.
72 end_inst: u32,
73 /// The first dependency in the `self.globals.dependencies` array list.
74 begin_dep: u32,
75 /// The past-end dependency in `self.globals.dependencies`.
76 end_dep: u32,
77};
78
5879/// A general-purpose allocator which may be used to allocate resources for this module
5980gpa: Allocator,
6081
......@@ -102,6 +123,20 @@ source_file_names: std.StringHashMapUnmanaged(IdRef) = .{},
102123/// Note: Uses ArrayHashMap which is insertion ordered, so that we may refer to other types by index (Type.Ref).
103124type_cache: TypeCache = .{},
104125
126/// The fields in this structure help to maintain the required order for global variables.
127globals: struct {
128 /// The graph nodes of global variables present in the module.
129 nodes: std.ArrayListUnmanaged(Global) = .{},
130 /// This pseudo-section contains the initialization code for all the globals. Instructions from
131 /// here are reordered when flushing the module. Its contents should be part of the
132 /// `types_globals_constants` SPIR-V section.
133 section: Section = .{},
134 /// Holds a list of dependent global variables for each global variable.
135 dependencies: std.ArrayListUnmanaged(Global.Index) = .{},
136 /// The global that initialization code/dependencies are currently being generated for, if any.
137 current_global: ?Global.Index = null,
138} = .{},
139
105140pub fn init(gpa: Allocator, arena: Allocator) Module {
106141 return .{
107142 .gpa = gpa,
......@@ -124,6 +159,10 @@ pub fn deinit(self: *Module) void {
124159 self.source_file_names.deinit(self.gpa);
125160 self.type_cache.deinit(self.gpa);
126161
162 self.globals.nodes.deinit(self.gpa);
163 self.globals.section.deinit(self.gpa);
164 self.globals.dependencies.deinit(self.gpa);
165
127166 self.* = undefined;
128167}
129168
......@@ -141,18 +180,60 @@ pub fn idBound(self: Module) Word {
141180 return self.next_result_id;
142181}
143182
183fn orderGlobalsInto(
184 self: Module,
185 global_index: Global.Index,
186 section: *Section,
187 seen: *std.DynamicBitSetUnmanaged,
188) !void {
189 const node = self.globals.nodes.items[@enumToInt(global_index)];
190 const deps = self.globals.dependencies.items[node.begin_dep .. node.end_dep];
191 const insts = self.globals.section.instructions.items[node.begin_inst .. node.end_inst];
192
193 seen.set(@enumToInt(global_index));
194
195 for (deps) |dep| {
196 if (!seen.isSet(@enumToInt(dep))) {
197 try self.orderGlobalsInto(dep, section, seen);
198 }
199 }
200
201 try section.instructions.appendSlice(self.gpa, insts);
202}
203
204fn orderGlobals(self: Module) !Section {
205 const nodes = self.globals.nodes.items;
206
207 var seen = try std.DynamicBitSetUnmanaged.initEmpty(self.gpa, nodes.len);
208 defer seen.deinit(self.gpa);
209
210 var ordered_globals = Section{};
211
212 for (0..nodes.len) |global_index| {
213 if (!seen.isSet(global_index)) {
214 try self.orderGlobalsInto(@intToEnum(Global.Index, @intCast(u32, global_index)), &ordered_globals, &seen);
215 }
216 }
217
218 return ordered_globals;
219}
220
144221/// Emit this module as a spir-v binary.
145222pub fn flush(self: Module, file: std.fs.File) !void {
146223 // See SPIR-V Spec section 2.3, "Physical Layout of a SPIR-V Module and Instruction"
147224
148225 const header = [_]Word{
149226 spec.magic_number,
150 (1 << 16) | (5 << 8),
227 (1 << 16) | (4 << 8), // TODO: From cpu features
151228 0, // TODO: Register Zig compiler magic number.
152229 self.idBound(),
153230 0, // Schema (currently reserved for future use)
154231 };
155232
233 // TODO: Perform topological sort on the globals.
234 var globals = try self.orderGlobals();
235 defer globals.deinit(self.gpa);
236
156237 // Note: needs to be kept in order according to section 2.3!
157238 const buffers = &[_][]const Word{
158239 &header,
......@@ -164,6 +245,7 @@ pub fn flush(self: Module, file: std.fs.File) !void {
164245 self.sections.debug_names.toWords(),
165246 self.sections.annotations.toWords(),
166247 self.sections.types_globals_constants.toWords(),
248 globals.toWords(),
167249 self.sections.functions.toWords(),
168250 };
169251
......@@ -279,6 +361,8 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
279361 .i64,
280362 .int,
281363 => {
364 // TODO: Kernels do not support OpTypeInt that is signed. We can probably
365 // can get rid of the signedness all together, in Shaders also.
282366 const bits = ty.intFloatBits();
283367 const signedness: spec.LiteralInteger = switch (ty.intSignedness()) {
284368 .unsigned => 0,
......@@ -634,3 +718,44 @@ pub fn decorateMember(
634718 .decoration = decoration,
635719 });
636720}
721
722pub fn allocGlobal(self: *Module) !Global.Index {
723 try self.globals.nodes.append(self.gpa, .{
724 .result_id = self.allocId(),
725 .begin_inst = undefined,
726 .end_inst = undefined,
727 .begin_dep = undefined,
728 .end_dep = undefined,
729 });
730 return @intToEnum(Global.Index, @intCast(u32, self.globals.nodes.items.len - 1));
731}
732
733pub fn globalPtr(self: *Module, index: Global.Index) *Global {
734 return &self.globals.nodes.items[@enumToInt(index)];
735}
736
737/// Begin generating the global for `index`. The previous global is finalized
738/// at this point, and the global for `index` is made active. Any new calls to
739/// `addGlobalDependency` will affect this global. After a new call to this function,
740/// the prior active global cannot be modified again.
741pub fn beginGlobal(self: *Module, index: Global.Index) IdRef {
742 const global = self.globalPtr(index);
743 global.begin_inst = @intCast(u32, self.globals.section.instructions.items.len);
744 global.begin_dep = @intCast(u32, self.globals.dependencies.items.len);
745 self.globals.current_global = index;
746 return global.result_id;
747}
748
749/// Finalize the global. After this point, the current global cannot be modified anymore.
750pub fn endGlobal(self: *Module) void {
751 const global = self.globalPtr(self.globals.current_global.?);
752 global.end_inst = @intCast(u32, self.globals.section.instructions.items.len);
753 global.end_dep = @intCast(u32, self.globals.dependencies.items.len);
754 self.globals.current_global = null;
755}
756
757pub fn addGlobalDependency(self: *Module, dependency: Global.Index) !void {
758 assert(self.globals.current_global != null);
759 assert(self.globals.current_global.? != dependency);
760 try self.globals.dependencies.append(self.gpa, dependency);
761}
src/link/SpirV.zig+5-5
......@@ -46,7 +46,7 @@ base: link.File,
4646
4747spv: SpvModule,
4848spv_arena: ArenaAllocator,
49decl_ids: codegen.DeclMap,
49decl_link: codegen.DeclLinkMap,
5050
5151pub fn createEmpty(gpa: Allocator, options: link.Options) !*SpirV {
5252 const self = try gpa.create(SpirV);
......@@ -59,7 +59,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*SpirV {
5959 },
6060 .spv = undefined,
6161 .spv_arena = ArenaAllocator.init(gpa),
62 .decl_ids = codegen.DeclMap.init(self.base.allocator),
62 .decl_link = codegen.DeclLinkMap.init(self.base.allocator),
6363 };
6464 self.spv = SpvModule.init(gpa, self.spv_arena.allocator());
6565 errdefer self.deinit();
......@@ -100,7 +100,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
100100pub fn deinit(self: *SpirV) void {
101101 self.spv.deinit();
102102 self.spv_arena.deinit();
103 self.decl_ids.deinit();
103 self.decl_link.deinit();
104104}
105105
106106pub fn updateFunc(self: *SpirV, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
......@@ -108,7 +108,7 @@ pub fn updateFunc(self: *SpirV, module: *Module, func: *Module.Fn, air: Air, liv
108108 @panic("Attempted to compile for architecture that was disabled by build configuration");
109109 }
110110
111 var decl_gen = codegen.DeclGen.init(self.base.allocator, module, &self.spv, &self.decl_ids);
111 var decl_gen = codegen.DeclGen.init(self.base.allocator, module, &self.spv, &self.decl_link);
112112 defer decl_gen.deinit();
113113
114114 if (try decl_gen.gen(func.owner_decl, air, liveness)) |msg| {
......@@ -121,7 +121,7 @@ pub fn updateDecl(self: *SpirV, module: *Module, decl_index: Module.Decl.Index)
121121 @panic("Attempted to compile for architecture that was disabled by build configuration");
122122 }
123123
124 var decl_gen = codegen.DeclGen.init(self.base.allocator, module, &self.spv, &self.decl_ids);
124 var decl_gen = codegen.DeclGen.init(self.base.allocator, module, &self.spv, &self.decl_link);
125125 defer decl_gen.deinit();
126126
127127 if (try decl_gen.gen(decl_index, undefined, undefined)) |msg| {