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 {
432432 dg: *DeclGen,
433433 /// Cached reference of the u32 type.
434434 u32_ty_ref: CacheRef,
435 /// Cached type id of the u32 type.
436 u32_ty_id: IdRef,
437435 /// The members of the resulting structure type
438436 members: std.ArrayList(CacheRef),
439437 /// The initializers of each of the members.
......@@ -466,9 +464,7 @@ pub const DeclGen = struct {
466464 }
467465
468466 const word = @bitCast(Word, self.partial_word.buffer);
469 const result_id = self.dg.spv.allocId();
470 // TODO: Integrate with caching mechanism
471 try self.dg.spv.emitConstant(self.u32_ty_id, result_id, .{ .uint32 = word });
467 const result_id = try self.dg.spv.constInt(self.u32_ty_ref, word);
472468 try self.members.append(self.u32_ty_ref);
473469 try self.initializers.append(result_id);
474470
......@@ -519,11 +515,7 @@ pub const DeclGen = struct {
519515 }
520516
521517 fn addNullPtr(self: *@This(), ptr_ty_ref: CacheRef) !void {
522 const result_id = self.dg.spv.allocId();
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 });
518 const result_id = try self.dg.spv.constNull(ptr_ty_ref);
527519 try self.addPtr(ptr_ty_ref, result_id);
528520 }
529521
......@@ -909,7 +901,6 @@ pub const DeclGen = struct {
909901 var icl = IndirectConstantLowering{
910902 .dg = self,
911903 .u32_ty_ref = u32_ty_ref,
912 .u32_ty_id = self.typeId(u32_ty_ref),
913904 .members = std.ArrayList(CacheRef).init(self.gpa),
914905 .initializers = std.ArrayList(IdRef).init(self.gpa),
915906 .decl_deps = std.AutoArrayHashMap(SpvModule.Decl.Index, void).init(self.gpa),
......@@ -978,19 +969,12 @@ pub const DeclGen = struct {
978969 /// This function should only be called during function code generation.
979970 fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef {
980971 const target = self.getTarget();
981 const section = &self.spv.sections.types_globals_constants;
982972 const result_ty_ref = try self.resolveType(ty, repr);
983 const result_ty_id = self.typeId(result_ty_ref);
984973
985974 log.debug("constant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtValue(ty, self.module) });
986975
987976 if (val.isUndef()) {
988 const result_id = self.spv.allocId();
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;
977 return self.spv.constUndef(result_ty_ref);
994978 }
995979
996980 switch (ty.zigTypeTag()) {
......@@ -1002,28 +986,15 @@ pub const DeclGen = struct {
1002986 }
1003987 },
1004988 .Bool => switch (repr) {
1005 .direct => {
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 },
989 .direct => return try self.spv.constBool(result_ty_ref, val.toBool()),
1015990 .indirect => return try self.spv.constInt(result_ty_ref, @boolToInt(val.toBool())),
1016991 },
1017 .Float => {
1018 const result_id = self.spv.allocId();
1019 switch (ty.floatBits(target)) {
1020 16 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f16) }),
1021 32 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f32) }),
1022 64 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float64 = val.toFloat(f64) }),
1023 80, 128 => unreachable, // TODO
1024 else => unreachable,
1025 }
1026 return result_id;
992 .Float => return switch (ty.floatBits(target)) {
993 16 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float16 = val.toFloat(f16) } } }),
994 32 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float32 = val.toFloat(f32) } } }),
995 64 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float64 = val.toFloat(f64) } } }),
996 80, 128 => unreachable, // TODO
997 else => unreachable,
1027998 },
1028999 .ErrorSet => {
10291000 const value = switch (val.tag()) {
......@@ -1081,7 +1052,7 @@ pub const DeclGen = struct {
10811052 try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {});
10821053
10831054 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),
10851056 .id_result = result_id,
10861057 .pointer = self.spv.declPtr(spv_decl_index).result_id,
10871058 });
......@@ -2607,9 +2578,8 @@ pub const DeclGen = struct {
26072578 .Struct => switch (object_ty.containerLayout()) {
26082579 .Packed => unreachable, // TODO
26092580 else => {
2610 const u32_ty_id = self.typeId(try self.intType(.unsigned, 32));
2611 const field_index_id = self.spv.allocId();
2612 try self.spv.emitConstant(u32_ty_id, field_index_id, .{ .uint32 = field_index });
2581 const field_index_ty_ref = try self.intType(.unsigned, 32);
2582 const field_index_id = try self.spv.constInt(field_index_ty_ref, field_index);
26132583 const result_ty_ref = try self.resolveType(result_ptr_ty, .direct);
26142584 return try self.accessChain(result_ty_ref, object_ptr, &.{field_index_id});
26152585 },
src/codegen/spirv/Module.zig+1-20
......@@ -127,7 +127,7 @@ sections: struct {
127127 // OpModuleProcessed - skip for now.
128128 /// Annotation instructions (OpDecorate etc).
129129 annotations: Section = .{},
130 /// Global variable declarations
130 /// Type declarations, constants, global variables
131131 /// From this section, OpLine and OpNoLine is allowed.
132132 /// According to the SPIR-V documentation, this section normally
133133 /// also holds type and constant instructions. These are managed
......@@ -135,10 +135,6 @@ sections: struct {
135135 /// manages that section. These will be inserted between this and
136136 /// the previous section when emitting the final binary.
137137 /// 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.
142138 types_globals_constants: Section = .{},
143139 // Functions without a body - skip for now.
144140 /// Regular function definitions.
......@@ -192,7 +188,6 @@ pub fn deinit(self: *Module) void {
192188 self.sections.debug_strings.deinit(self.gpa);
193189 self.sections.debug_names.deinit(self.gpa);
194190 self.sections.annotations.deinit(self.gpa);
195 self.sections.globals.deinit(self.gpa);
196191 self.sections.functions.deinit(self.gpa);
197192
198193 self.source_file_names.deinit(self.gpa);
......@@ -365,7 +360,6 @@ pub fn flush(self: *Module, file: std.fs.File) !void {
365360 self.sections.annotations.toWords(),
366361 types_constants.toWords(),
367362 self.sections.types_globals_constants.toWords(),
368 self.sections.globals.toWords(),
369363 globals.toWords(),
370364 self.sections.functions.toWords(),
371365 };
......@@ -484,19 +478,6 @@ pub fn constComposite(self: *Module, ty_ref: CacheRef, members: []const IdRef) !
484478 return result_id;
485479}
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
500481/// Decorate a result-id.
501482pub fn decorate(
502483 self: *Module,