authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-30 18:41:31+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-30 19:43:37+02:00
log3c4cc1eedb959aa804ca752e20268ccecc14ccef
tree6da038c4f48f98c6ff63630d53a1ecc75a6972e1
parent0c41945a01a3a5254e8b4266a0677f2d8224aa1a
signaturelock-open Commit is signed but in an unrecognized format.

spirv: eliminate remaining uses of emitConstant


2 files changed, 14 insertions(+), 63 deletions(-)

src/codegen/spirv.zig+13-43
...@@ -432,8 +432,6 @@ pub const DeclGen = struct {...@@ -432,8 +432,6 @@ pub const DeclGen = struct {
432 dg: *DeclGen,432 dg: *DeclGen,
433 /// Cached reference of the u32 type.433 /// Cached reference of the u32 type.
434 u32_ty_ref: CacheRef,434 u32_ty_ref: CacheRef,
435 /// Cached type id of the u32 type.
436 u32_ty_id: IdRef,
437 /// The members of the resulting structure type435 /// The members of the resulting structure type
438 members: std.ArrayList(CacheRef),436 members: std.ArrayList(CacheRef),
439 /// The initializers of each of the members.437 /// The initializers of each of the members.
...@@ -466,9 +464,7 @@ pub const DeclGen = struct {...@@ -466,9 +464,7 @@ pub const DeclGen = struct {
466 }464 }
467465
468 const word = @bitCast(Word, self.partial_word.buffer);466 const word = @bitCast(Word, self.partial_word.buffer);
469 const result_id = self.dg.spv.allocId();467 const result_id = try self.dg.spv.constInt(self.u32_ty_ref, word);
470 // TODO: Integrate with caching mechanism
471 try self.dg.spv.emitConstant(self.u32_ty_id, result_id, .{ .uint32 = word });
472 try self.members.append(self.u32_ty_ref);468 try self.members.append(self.u32_ty_ref);
473 try self.initializers.append(result_id);469 try self.initializers.append(result_id);
474470
...@@ -519,11 +515,7 @@ pub const DeclGen = struct {...@@ -519,11 +515,7 @@ pub const DeclGen = struct {
519 }515 }
520516
521 fn addNullPtr(self: *@This(), ptr_ty_ref: CacheRef) !void {517 fn addNullPtr(self: *@This(), ptr_ty_ref: CacheRef) !void {
522 const result_id = self.dg.spv.allocId();518 const result_id = try self.dg.spv.constNull(ptr_ty_ref);
523 try self.dg.spv.sections.types_globals_constants.emit(self.dg.spv.gpa, .OpConstantNull, .{
524 .id_result_type = self.dg.typeId(ptr_ty_ref),
525 .id_result = result_id,
526 });
527 try self.addPtr(ptr_ty_ref, result_id);519 try self.addPtr(ptr_ty_ref, result_id);
528 }520 }
529521
...@@ -909,7 +901,6 @@ pub const DeclGen = struct {...@@ -909,7 +901,6 @@ pub const DeclGen = struct {
909 var icl = IndirectConstantLowering{901 var icl = IndirectConstantLowering{
910 .dg = self,902 .dg = self,
911 .u32_ty_ref = u32_ty_ref,903 .u32_ty_ref = u32_ty_ref,
912 .u32_ty_id = self.typeId(u32_ty_ref),
913 .members = std.ArrayList(CacheRef).init(self.gpa),904 .members = std.ArrayList(CacheRef).init(self.gpa),
914 .initializers = std.ArrayList(IdRef).init(self.gpa),905 .initializers = std.ArrayList(IdRef).init(self.gpa),
915 .decl_deps = std.AutoArrayHashMap(SpvModule.Decl.Index, void).init(self.gpa),906 .decl_deps = std.AutoArrayHashMap(SpvModule.Decl.Index, void).init(self.gpa),
...@@ -978,19 +969,12 @@ pub const DeclGen = struct {...@@ -978,19 +969,12 @@ pub const DeclGen = struct {
978 /// This function should only be called during function code generation.969 /// This function should only be called during function code generation.
979 fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef {970 fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef {
980 const target = self.getTarget();971 const target = self.getTarget();
981 const section = &self.spv.sections.types_globals_constants;
982 const result_ty_ref = try self.resolveType(ty, repr);972 const result_ty_ref = try self.resolveType(ty, repr);
983 const result_ty_id = self.typeId(result_ty_ref);
984973
985 log.debug("constant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtValue(ty, self.module) });974 log.debug("constant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtValue(ty, self.module) });
986975
987 if (val.isUndef()) {976 if (val.isUndef()) {
988 const result_id = self.spv.allocId();977 return self.spv.constUndef(result_ty_ref);
989 try section.emit(self.spv.gpa, .OpUndef, .{
990 .id_result_type = result_ty_id,
991 .id_result = result_id,
992 });
993 return result_id;
994 }978 }
995979
996 switch (ty.zigTypeTag()) {980 switch (ty.zigTypeTag()) {
...@@ -1002,28 +986,15 @@ pub const DeclGen = struct {...@@ -1002,28 +986,15 @@ pub const DeclGen = struct {
1002 }986 }
1003 },987 },
1004 .Bool => switch (repr) {988 .Bool => switch (repr) {
1005 .direct => {989 .direct => return try self.spv.constBool(result_ty_ref, val.toBool()),
1006 const result_id = self.spv.allocId();
1007 const operands = .{ .id_result_type = result_ty_id, .id_result = result_id };
1008 if (val.toBool()) {
1009 try section.emit(self.spv.gpa, .OpConstantTrue, operands);
1010 } else {
1011 try section.emit(self.spv.gpa, .OpConstantFalse, operands);
1012 }
1013 return result_id;
1014 },
1015 .indirect => return try self.spv.constInt(result_ty_ref, @boolToInt(val.toBool())),990 .indirect => return try self.spv.constInt(result_ty_ref, @boolToInt(val.toBool())),
1016 },991 },
1017 .Float => {992 .Float => return switch (ty.floatBits(target)) {
1018 const result_id = self.spv.allocId();993 16 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float16 = val.toFloat(f16) } } }),
1019 switch (ty.floatBits(target)) {994 32 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float32 = val.toFloat(f32) } } }),
1020 16 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f16) }),995 64 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float64 = val.toFloat(f64) } } }),
1021 32 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f32) }),996 80, 128 => unreachable, // TODO
1022 64 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float64 = val.toFloat(f64) }),997 else => unreachable,
1023 80, 128 => unreachable, // TODO
1024 else => unreachable,
1025 }
1026 return result_id;
1027 },998 },
1028 .ErrorSet => {999 .ErrorSet => {
1029 const value = switch (val.tag()) {1000 const value = switch (val.tag()) {
...@@ -1081,7 +1052,7 @@ pub const DeclGen = struct {...@@ -1081,7 +1052,7 @@ pub const DeclGen = struct {
1081 try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {});1052 try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {});
10821053
1083 try self.func.body.emit(self.spv.gpa, .OpLoad, .{1054 try self.func.body.emit(self.spv.gpa, .OpLoad, .{
1084 .id_result_type = result_ty_id,1055 .id_result_type = self.typeId(result_ty_ref),
1085 .id_result = result_id,1056 .id_result = result_id,
1086 .pointer = self.spv.declPtr(spv_decl_index).result_id,1057 .pointer = self.spv.declPtr(spv_decl_index).result_id,
1087 });1058 });
...@@ -2607,9 +2578,8 @@ pub const DeclGen = struct {...@@ -2607,9 +2578,8 @@ pub const DeclGen = struct {
2607 .Struct => switch (object_ty.containerLayout()) {2578 .Struct => switch (object_ty.containerLayout()) {
2608 .Packed => unreachable, // TODO2579 .Packed => unreachable, // TODO
2609 else => {2580 else => {
2610 const u32_ty_id = self.typeId(try self.intType(.unsigned, 32));2581 const field_index_ty_ref = try self.intType(.unsigned, 32);
2611 const field_index_id = self.spv.allocId();2582 const field_index_id = try self.spv.constInt(field_index_ty_ref, field_index);
2612 try self.spv.emitConstant(u32_ty_id, field_index_id, .{ .uint32 = field_index });
2613 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);2583 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
2614 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index_id});2584 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index_id});
2615 },2585 },
src/codegen/spirv/Module.zig+1-20
...@@ -127,7 +127,7 @@ sections: struct {...@@ -127,7 +127,7 @@ sections: struct {
127 // OpModuleProcessed - skip for now.127 // OpModuleProcessed - skip for now.
128 /// Annotation instructions (OpDecorate etc).128 /// Annotation instructions (OpDecorate etc).
129 annotations: Section = .{},129 annotations: Section = .{},
130 /// Global variable declarations130 /// Type declarations, constants, global variables
131 /// From this section, OpLine and OpNoLine is allowed.131 /// From this section, OpLine and OpNoLine is allowed.
132 /// According to the SPIR-V documentation, this section normally132 /// According to the SPIR-V documentation, this section normally
133 /// also holds type and constant instructions. These are managed133 /// also holds type and constant instructions. These are managed
...@@ -135,10 +135,6 @@ sections: struct {...@@ -135,10 +135,6 @@ sections: struct {
135 /// manages that section. These will be inserted between this and135 /// manages that section. These will be inserted between this and
136 /// the previous section when emitting the final binary.136 /// the previous section when emitting the final binary.
137 /// TODO: Do we need this section? Globals are also managed with another mechanism.137 /// TODO: Do we need this section? Globals are also managed with another mechanism.
138 /// The only thing that needs to be kept here is OpUndef
139 globals: Section = .{},
140 /// Type declarations, constants, global variables
141 /// Below this section, OpLine and OpNoLine is allowed.
142 types_globals_constants: Section = .{},138 types_globals_constants: Section = .{},
143 // Functions without a body - skip for now.139 // Functions without a body - skip for now.
144 /// Regular function definitions.140 /// Regular function definitions.
...@@ -192,7 +188,6 @@ pub fn deinit(self: *Module) void {...@@ -192,7 +188,6 @@ pub fn deinit(self: *Module) void {
192 self.sections.debug_strings.deinit(self.gpa);188 self.sections.debug_strings.deinit(self.gpa);
193 self.sections.debug_names.deinit(self.gpa);189 self.sections.debug_names.deinit(self.gpa);
194 self.sections.annotations.deinit(self.gpa);190 self.sections.annotations.deinit(self.gpa);
195 self.sections.globals.deinit(self.gpa);
196 self.sections.functions.deinit(self.gpa);191 self.sections.functions.deinit(self.gpa);
197192
198 self.source_file_names.deinit(self.gpa);193 self.source_file_names.deinit(self.gpa);
...@@ -365,7 +360,6 @@ pub fn flush(self: *Module, file: std.fs.File) !void {...@@ -365,7 +360,6 @@ pub fn flush(self: *Module, file: std.fs.File) !void {
365 self.sections.annotations.toWords(),360 self.sections.annotations.toWords(),
366 types_constants.toWords(),361 types_constants.toWords(),
367 self.sections.types_globals_constants.toWords(),362 self.sections.types_globals_constants.toWords(),
368 self.sections.globals.toWords(),
369 globals.toWords(),363 globals.toWords(),
370 self.sections.functions.toWords(),364 self.sections.functions.toWords(),
371 };365 };
...@@ -484,19 +478,6 @@ pub fn constComposite(self: *Module, ty_ref: CacheRef, members: []const IdRef) !...@@ -484,19 +478,6 @@ pub fn constComposite(self: *Module, ty_ref: CacheRef, members: []const IdRef) !
484 return result_id;478 return result_id;
485}479}
486480
487pub fn emitConstant(
488 self: *Module,
489 ty_id: IdRef,
490 result_id: IdRef,
491 value: spec.LiteralContextDependentNumber,
492) !void {
493 try self.sections.types_globals_constants.emit(self.gpa, .OpConstant, .{
494 .id_result_type = ty_id,
495 .id_result = result_id,
496 .value = value,
497 });
498}
499
500/// Decorate a result-id.481/// Decorate a result-id.
501pub fn decorate(482pub fn decorate(
502 self: *Module,483 self: *Module,