authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2024-02-19 03:18:28+03:30
committergravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2024-04-06 08:52:38+03:30
log97850149386a8ec54ffce285973d3e7010cefa00
tree34521620230eb04349d1ef2f4f5107088cca96d0
parent0f75143c621b239dd79abc32de0911a68fa7caa4

spirv: OpExtInstImport in assembler


4 files changed, 55 insertions(+), 47 deletions(-)

src/codegen/spirv.zig+17-26
......@@ -1016,7 +1016,7 @@ const DeclGen = struct {
10161016 const elem_ty = Type.fromInterned(array_type.child);
10171017 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
10181018
1019 const constituents = try self.gpa.alloc(IdRef, @as(u32, @intCast(ty.arrayLenIncludingSentinel(mod))));
1019 const constituents = try self.gpa.alloc(IdRef, @intCast(ty.arrayLenIncludingSentinel(mod)));
10201020 defer self.gpa.free(constituents);
10211021
10221022 switch (aggregate.storage) {
......@@ -1736,7 +1736,6 @@ const DeclGen = struct {
17361736 .EnumLiteral,
17371737 .ComptimeFloat,
17381738 .ComptimeInt,
1739 .Type,
17401739 => unreachable, // Must be comptime.
17411740
17421741 else => |tag| return self.todo("Implement zig type '{}'", .{tag}),
......@@ -2323,18 +2322,10 @@ const DeclGen = struct {
23232322
23242323 .div_float,
23252324 .div_float_optimized,
2326 // TODO: Check that this is the right operation.
23272325 .div_trunc,
2328 .div_trunc_optimized,
2329 => try self.airArithOp(inst, .OpFDiv, .OpSDiv, .OpUDiv),
2330 // TODO: Check if this is the right operation
2331 .rem,
2332 .rem_optimized,
2333 => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem),
2334 // TODO: Check if this is the right operation
2335 .mod,
2336 .mod_optimized,
2337 => try self.airArithOp(inst, .OpFMod, .OpSMod, .OpSMod),
2326 .div_trunc_optimized => try self.airArithOp(inst, .OpFDiv, .OpSDiv, .OpUDiv),
2327 .rem, .rem_optimized => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem),
2328 .mod, .mod_optimized => try self.airArithOp(inst, .OpFMod, .OpSMod, .OpSMod),
23382329
23392330
23402331 .add_with_overflow => try self.airAddSubOverflow(inst, .OpIAdd, .OpULessThan, .OpSLessThan),
......@@ -2348,7 +2339,7 @@ const DeclGen = struct {
23482339
23492340 .splat => try self.airSplat(inst),
23502341 .reduce, .reduce_optimized => try self.airReduce(inst),
2351 .shuffle => try self.airShuffle(inst),
2342 .shuffle => try self.airShuffle(inst),
23522343
23532344 .ptr_add => try self.airPtrAdd(inst),
23542345 .ptr_sub => try self.airPtrSub(inst),
......@@ -2742,8 +2733,8 @@ const DeclGen = struct {
27422733 else => unreachable,
27432734 };
27442735 const set_id = switch (target.os.tag) {
2745 .opencl => try self.spv.importInstructionSet(.opencl),
2746 .vulkan => try self.spv.importInstructionSet(.glsl),
2736 .opencl => try self.spv.importInstructionSet("OpenCL.std"),
2737 .vulkan => try self.spv.importInstructionSet("GLSL.std.450"),
27472738 else => unreachable,
27482739 };
27492740
......@@ -2796,8 +2787,8 @@ const DeclGen = struct {
27962787 return self.todo("binary operations for composite integers", .{});
27972788 },
27982789 .integer, .strange_integer => switch (info.signedness) {
2799 .signed => @as(usize, 1),
2800 .unsigned => @as(usize, 2),
2790 .signed => 1,
2791 .unsigned => 2,
28012792 },
28022793 .float => 0,
28032794 .bool => unreachable,
......@@ -5357,7 +5348,7 @@ const DeclGen = struct {
53575348 const backing_bits = self.backingIntBits(bits) orelse {
53585349 return self.todo("implement composite int switch", .{});
53595350 };
5360 break :blk if (backing_bits <= 32) @as(u32, 1) else 2;
5351 break :blk if (backing_bits <= 32) 1 else 2;
53615352 },
53625353 .Enum => blk: {
53635354 const int_ty = cond_ty.intTagType(mod);
......@@ -5365,7 +5356,7 @@ const DeclGen = struct {
53655356 const backing_bits = self.backingIntBits(int_info.bits) orelse {
53665357 return self.todo("implement composite int switch", .{});
53675358 };
5368 break :blk if (backing_bits <= 32) @as(u32, 1) else 2;
5359 break :blk if (backing_bits <= 32) 1 else 2;
53695360 },
53705361 .Pointer => blk: {
53715362 cond_indirect = try self.intFromPtr(cond_indirect);
......@@ -5419,7 +5410,7 @@ const DeclGen = struct {
54195410 for (0..num_cases) |case_i| {
54205411 // SPIR-V needs a literal here, which' width depends on the case condition.
54215412 const case = self.air.extraData(Air.SwitchBr.Case, extra_index);
5422 const items = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[case.end..][0..case.data.items_len]));
5413 const items: []const Air.Inst.Ref = @ptrCast(self.air.extra[case.end..][0..case.data.items_len]);
54235414 const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len];
54245415 extra_index = case.end + case.data.items_len + case_body.len;
54255416
......@@ -5428,7 +5419,7 @@ const DeclGen = struct {
54285419 for (items) |item| {
54295420 const value = (try self.air.value(item, mod)) orelse unreachable;
54305421 const int_val: u64 = switch (cond_ty.zigTypeTag(mod)) {
5431 .Bool, .Int => if (cond_ty.isSignedInt(mod)) @as(u64, @bitCast(value.toSignedInt(mod))) else value.toUnsignedInt(mod),
5422 .Bool, .Int => if (cond_ty.isSignedInt(mod)) @bitCast(value.toSignedInt(mod)) else value.toUnsignedInt(mod),
54325423 .Enum => blk: {
54335424 // TODO: figure out of cond_ty is correct (something with enum literals)
54345425 break :blk (try value.intFromEnum(cond_ty, mod)).toUnsignedInt(mod); // TODO: composite integer constants
......@@ -5550,14 +5541,14 @@ const DeclGen = struct {
55505541 const extra = self.air.extraData(Air.Asm, ty_pl.payload);
55515542
55525543 const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0;
5553 const clobbers_len = @as(u31, @truncate(extra.data.flags));
5544 const clobbers_len: u31 = @truncate(extra.data.flags);
55545545
55555546 if (!is_volatile and self.liveness.isUnused(inst)) return null;
55565547
55575548 var extra_i: usize = extra.end;
5558 const outputs = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]));
5549 const outputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]);
55595550 extra_i += outputs.len;
5560 const inputs = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]));
5551 const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]);
55615552 extra_i += inputs.len;
55625553
55635554 if (outputs.len > 1) {
......@@ -5679,7 +5670,7 @@ const DeclGen = struct {
56795670 const mod = self.module;
56805671 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
56815672 const extra = self.air.extraData(Air.Call, pl_op.payload);
5682 const args = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]));
5673 const args: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]);
56835674 const callee_ty = self.typeOf(pl_op.operand);
56845675 const zig_fn_ty = switch (callee_ty.zigTypeTag(mod)) {
56855676 .Fn => callee_ty,
src/codegen/spirv/Assembler.zig+26-9
......@@ -256,10 +256,15 @@ fn todo(self: *Assembler, comptime fmt: []const u8, args: anytype) Error {
256256/// If this function returns `error.AssembleFail`, an explanatory
257257/// error message has already been emitted into `self.errors`.
258258fn processInstruction(self: *Assembler) !void {
259 const result = switch (self.inst.opcode) {
259 const result: AsmValue = switch (self.inst.opcode) {
260260 .OpEntryPoint => {
261261 return self.fail(0, "cannot export entry points via OpEntryPoint, export the kernel using callconv(.Kernel)", .{});
262262 },
263 .OpExtInstImport => blk: {
264 const set_name_offset = self.inst.operands.items[1].string;
265 const set_name = std.mem.sliceTo(self.inst.string_bytes.items[set_name_offset..], 0);
266 break :blk .{ .value = try self.spv.importInstructionSet(set_name) };
267 },
263268 else => switch (self.inst.opcode.class()) {
264269 .TypeDeclaration => try self.processTypeInstruction(),
265270 else => if (try self.processGenericInstruction()) |result|
......@@ -309,7 +314,7 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
309314 return self.fail(0, "{} is not a valid bit count for floats (expected 16, 32 or 64)", .{bits});
310315 },
311316 }
312 break :blk try self.spv.resolve(.{ .float_type = .{ .bits = @as(u16, @intCast(bits)) } });
317 break :blk try self.spv.resolve(.{ .float_type = .{ .bits = @intCast(bits) } });
313318 },
314319 .OpTypeVector => try self.spv.resolve(.{ .vector_type = .{
315320 .component_type = try self.resolveTypeRef(operands[1].ref_id),
......@@ -364,6 +369,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
364369 .OpExecutionMode, .OpExecutionModeId => &self.spv.sections.execution_modes,
365370 .OpVariable => switch (@as(spec.StorageClass, @enumFromInt(operands[2].value))) {
366371 .Function => &self.func.prologue,
372 .UniformConstant => &self.spv.sections.types_globals_constants,
367373 else => {
368374 // This is currently disabled because global variables are required to be
369375 // emitted in the proper order, and this should be honored in inline assembly
......@@ -473,14 +479,14 @@ fn parseInstruction(self: *Assembler) !void {
473479 self.inst.string_bytes.shrinkRetainingCapacity(0);
474480
475481 const lhs_result_tok = self.currentToken();
476 const maybe_lhs_result = if (self.eatToken(.result_id_assign)) blk: {
482 const maybe_lhs_result: ?AsmValue.Ref = if (self.eatToken(.result_id_assign)) blk: {
477483 const name = self.tokenText(lhs_result_tok)[1..];
478484 const entry = try self.value_map.getOrPut(self.gpa, name);
479485 try self.expectToken(.equals);
480486 if (!entry.found_existing) {
481487 entry.value_ptr.* = .just_declared;
482488 }
483 break :blk @as(AsmValue.Ref, @intCast(entry.index));
489 break :blk @intCast(entry.index);
484490 } else null;
485491
486492 const opcode_tok = self.currentToken();
......@@ -550,6 +556,7 @@ fn parseOperand(self: *Assembler, kind: spec.OperandKind) Error!void {
550556 .LiteralInteger => try self.parseLiteralInteger(),
551557 .LiteralString => try self.parseString(),
552558 .LiteralContextDependentNumber => try self.parseContextDependentNumber(),
559 .LiteralExtInstInteger => try self.parseLiteralExtInstInteger(),
553560 .PairIdRefIdRef => try self.parsePhiSource(),
554561 else => return self.todo("parse operand of type {s}", .{@tagName(kind)}),
555562 },
......@@ -641,7 +648,7 @@ fn parseRefId(self: *Assembler) !void {
641648 entry.value_ptr.* = .unresolved_forward_reference;
642649 }
643650
644 const index = @as(AsmValue.Ref, @intCast(entry.index));
651 const index: AsmValue.Ref = @intCast(entry.index);
645652 try self.inst.operands.append(self.gpa, .{ .ref_id = index });
646653}
647654
......@@ -660,6 +667,16 @@ fn parseLiteralInteger(self: *Assembler) !void {
660667 try self.inst.operands.append(self.gpa, .{ .literal32 = value });
661668}
662669
670fn parseLiteralExtInstInteger(self: *Assembler) !void {
671 const tok = self.currentToken();
672 try self.expectToken(.value);
673 const text = self.tokenText(tok);
674 const value = std.fmt.parseInt(u32, text, 0) catch {
675 return self.fail(tok.start, "'{s}' is not a valid 32-bit integer literal", .{text});
676 };
677 try self.inst.operands.append(self.gpa, .{ .literal32 = value });
678}
679
663680fn parseString(self: *Assembler) !void {
664681 const tok = self.currentToken();
665682 try self.expectToken(.string);
......@@ -673,7 +690,7 @@ fn parseString(self: *Assembler) !void {
673690 else
674691 text[1..];
675692
676 const string_offset = @as(u32, @intCast(self.inst.string_bytes.items.len));
693 const string_offset: u32 = @intCast(self.inst.string_bytes.items.len);
677694 try self.inst.string_bytes.ensureUnusedCapacity(self.gpa, literal.len + 1);
678695 self.inst.string_bytes.appendSliceAssumeCapacity(literal);
679696 self.inst.string_bytes.appendAssumeCapacity(0);
......@@ -730,9 +747,9 @@ fn parseContextDependentInt(self: *Assembler, signedness: std.builtin.Signedness
730747
731748 // Note, we store the sign-extended version here.
732749 if (width <= @bitSizeOf(spec.Word)) {
733 try self.inst.operands.append(self.gpa, .{ .literal32 = @as(u32, @truncate(@as(u128, @bitCast(int)))) });
750 try self.inst.operands.append(self.gpa, .{ .literal32 = @truncate(@as(u128, @bitCast(int))) });
734751 } else {
735 try self.inst.operands.append(self.gpa, .{ .literal64 = @as(u64, @truncate(@as(u128, @bitCast(int)))) });
752 try self.inst.operands.append(self.gpa, .{ .literal64 = @truncate(@as(u128, @bitCast(int))) });
736753 }
737754 return;
738755 }
......@@ -753,7 +770,7 @@ fn parseContextDependentFloat(self: *Assembler, comptime width: u16) !void {
753770 return self.fail(tok.start, "'{s}' is not a valid {}-bit float literal", .{ text, width });
754771 };
755772
756 const float_bits = @as(Int, @bitCast(value));
773 const float_bits: Int = @bitCast(value);
757774 if (width <= @bitSizeOf(spec.Word)) {
758775 try self.inst.operands.append(self.gpa, .{ .literal32 = float_bits });
759776 } else {
src/codegen/spirv/Module.zig+2-2
......@@ -500,9 +500,9 @@ pub fn declPtr(self: *Module, index: Decl.Index) *Decl {
500500
501501/// Declare ALL dependencies for a decl.
502502pub fn declareDeclDeps(self: *Module, decl_index: Decl.Index, deps: []const Decl.Index) !void {
503 const begin_dep = @as(u32, @intCast(self.decl_deps.items.len));
503 const begin_dep: u32 = @intCast(self.decl_deps.items.len);
504504 try self.decl_deps.appendSlice(self.gpa, deps);
505 const end_dep = @as(u32, @intCast(self.decl_deps.items.len));
505 const end_dep: u32 = @intCast(self.decl_deps.items.len);
506506
507507 const decl = self.declPtr(decl_index);
508508 decl.begin_dep = begin_dep;
src/codegen/spirv/Section.zig+10-10
......@@ -115,8 +115,8 @@ pub fn writeWords(section: *Section, words: []const Word) void {
115115
116116pub fn writeDoubleWord(section: *Section, dword: DoubleWord) void {
117117 section.writeWords(&.{
118 @as(Word, @truncate(dword)),
119 @as(Word, @truncate(dword >> @bitSizeOf(Word))),
118 @truncate(dword),
119 @truncate(dword >> @bitSizeOf(Word)),
120120 });
121121}
122122
......@@ -196,12 +196,12 @@ fn writeString(section: *Section, str: []const u8) void {
196196
197197fn writeContextDependentNumber(section: *Section, operand: spec.LiteralContextDependentNumber) void {
198198 switch (operand) {
199 .int32 => |int| section.writeWord(@as(Word, @bitCast(int))),
200 .uint32 => |int| section.writeWord(@as(Word, @bitCast(int))),
201 .int64 => |int| section.writeDoubleWord(@as(DoubleWord, @bitCast(int))),
202 .uint64 => |int| section.writeDoubleWord(@as(DoubleWord, @bitCast(int))),
203 .float32 => |float| section.writeWord(@as(Word, @bitCast(float))),
204 .float64 => |float| section.writeDoubleWord(@as(DoubleWord, @bitCast(float))),
199 .int32 => |int| section.writeWord(@bitCast(int)),
200 .uint32 => |int| section.writeWord(@bitCast(int)),
201 .int64 => |int| section.writeDoubleWord(@bitCast(int)),
202 .uint64 => |int| section.writeDoubleWord(@bitCast(int)),
203 .float32 => |float| section.writeWord(@bitCast(float)),
204 .float64 => |float| section.writeDoubleWord(@bitCast(float)),
205205 }
206206}
207207
......@@ -274,8 +274,8 @@ fn operandSize(comptime Operand: type, operand: Operand) usize {
274274 spec.LiteralString => std.math.divCeil(usize, operand.len + 1, @sizeOf(Word)) catch unreachable, // Add one for zero-terminator
275275
276276 spec.LiteralContextDependentNumber => switch (operand) {
277 .int32, .uint32, .float32 => @as(usize, 1),
278 .int64, .uint64, .float64 => @as(usize, 2),
277 .int32, .uint32, .float32 => 1,
278 .int64, .uint64, .float64 => 2,
279279 },
280280
281281 // TODO: Where this type is used (OpSpecConstantOp) is currently not correct in the spec