authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-20 17:50:17+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-22 16:11:56+02:00
log5edc5f973089aaec5a62c37a3b7d0470a90d45e3
treea4d0b80d0b9499706e4088f72b992afaab46b39b
parent63d0576f1ccfd6ec1538459c6ac69b9f892b2142

SPIR-V: Pass source location to genType and genConstant for better error reporting


1 files changed, 23 insertions(+), 23 deletions(-)

src/codegen/spirv.zig+23-23
......@@ -150,7 +150,7 @@ pub const DeclGen = struct {
150150
151151 fn resolve(self: *DeclGen, inst: *Inst) !ResultId {
152152 if (inst.value()) |val| {
153 return self.genConstant(inst.ty, val);
153 return self.genConstant(inst.src, inst.ty, val);
154154 }
155155
156156 return self.inst_results.get(inst).?; // Instruction does not dominate all uses!
......@@ -252,11 +252,11 @@ pub const DeclGen = struct {
252252
253253 /// Generate a constant representing `val`.
254254 /// TODO: Deduplication?
255 fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!ResultId {
255 fn genConstant(self: *DeclGen, src: LazySrcLoc, ty: Type, val: Value) Error!ResultId {
256256 const target = self.module.getTarget();
257257 const code = &self.spv.binary.types_globals_constants;
258258 const result_id = self.spv.allocResultId();
259 const result_type_id = try self.genType(ty);
259 const result_type_id = try self.genType(src, ty);
260260
261261 if (val.isUndef()) {
262262 try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id });
......@@ -268,7 +268,7 @@ pub const DeclGen = struct {
268268 const int_info = ty.intInfo(target);
269269 const backing_bits = self.backingIntBits(int_info.bits) orelse {
270270 // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits.
271 return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite int constants for {}", .{ty});
271 return self.fail(src, "TODO: SPIR-V backend: implement composite int constants for {}", .{ty});
272272 };
273273
274274 // We can just use toSignedInt/toUnsignedInt here as it returns u64 - a type large enough to hold any
......@@ -325,13 +325,13 @@ pub const DeclGen = struct {
325325 else => unreachable,
326326 }
327327 },
328 else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}),
328 else => return self.fail(src, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}),
329329 }
330330
331331 return result_id;
332332 }
333333
334 fn genType(self: *DeclGen, ty: Type) Error!ResultId {
334 fn genType(self: *DeclGen, src: LazySrcLoc, ty: Type) Error!ResultId {
335335 // We can't use getOrPut here so we can recursively generate types.
336336 if (self.spv.types.get(ty)) |already_generated| {
337337 return already_generated;
......@@ -348,7 +348,7 @@ pub const DeclGen = struct {
348348 const int_info = ty.intInfo(target);
349349 const backing_bits = self.backingIntBits(int_info.bits) orelse {
350350 // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits.
351 return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite int {}", .{ty});
351 return self.fail(src, "TODO: SPIR-V backend: implement composite int {}", .{ty});
352352 };
353353
354354 // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here.
......@@ -374,7 +374,7 @@ pub const DeclGen = struct {
374374 };
375375
376376 if (!supported) {
377 return self.fail(.{ .node_offset = 0 }, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits});
377 return self.fail(src, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits});
378378 }
379379
380380 try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits });
......@@ -382,19 +382,19 @@ pub const DeclGen = struct {
382382 .Fn => {
383383 // We only support zig-calling-convention functions, no varargs.
384384 if (ty.fnCallingConvention() != .Unspecified)
385 return self.fail(.{ .node_offset = 0 }, "Unsupported calling convention for SPIR-V", .{});
385 return self.fail(src, "Unsupported calling convention for SPIR-V", .{});
386386 if (ty.fnIsVarArgs())
387 return self.fail(.{ .node_offset = 0 }, "VarArgs unsupported for SPIR-V", .{});
387 return self.fail(src, "VarArgs unsupported for SPIR-V", .{});
388388
389389 // In order to avoid a temporary here, first generate all the required types and then simply look them up
390390 // when generating the function type.
391391 const params = ty.fnParamLen();
392392 var i: usize = 0;
393393 while (i < params) : (i += 1) {
394 _ = try self.genType(ty.fnParamType(i));
394 _ = try self.genType(src, ty.fnParamType(i));
395395 }
396396
397 const return_type_id = try self.genType(ty.fnReturnType());
397 const return_type_id = try self.genType(src, ty.fnReturnType());
398398
399399 // result id + result type id + parameter type ids.
400400 try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen()));
......@@ -407,7 +407,7 @@ pub const DeclGen = struct {
407407 }
408408 },
409409 // When recursively generating a type, we cannot infer the pointer's storage class. See genPointerType.
410 .Pointer => return self.fail(.{ .node_offset = 0 }, "Cannot create pointer with unkown storage class", .{}),
410 .Pointer => return self.fail(src, "Cannot create pointer with unkown storage class", .{}),
411411 .Vector => {
412412 // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations
413413 // which work on them), so simply use those.
......@@ -417,7 +417,7 @@ pub const DeclGen = struct {
417417 // is adequate at all for this.
418418
419419 // TODO: Vectors are not yet supported by the self-hosted compiler itself it seems.
420 return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type Vector", .{});
420 return self.fail(src, "TODO: SPIR-V backend: implement type Vector", .{});
421421 },
422422 .Null,
423423 .Undefined,
......@@ -429,7 +429,7 @@ pub const DeclGen = struct {
429429
430430 .BoundFn => unreachable, // this type will be deleted from the language.
431431
432 else => |tag| return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type {}s", .{tag}),
432 else => |tag| return self.fail(src, "TODO: SPIR-V backend: implement type {}s", .{tag}),
433433 }
434434
435435 try self.spv.types.putNoClobber(ty, result_id);
......@@ -438,7 +438,7 @@ pub const DeclGen = struct {
438438
439439 /// SPIR-V requires pointers to have a storage class (address space), and so we have a special function for that.
440440 /// TODO: The result of this needs to be cached.
441 fn genPointerType(self: *DeclGen, ty: Type, storage_class: spec.StorageClass) !ResultId {
441 fn genPointerType(self: *DeclGen, src: LazySrcLoc, ty: Type, storage_class: spec.StorageClass) !ResultId {
442442 std.debug.assert(ty.zigTypeTag() == .Pointer);
443443
444444 const code = &self.spv.binary.types_globals_constants;
......@@ -447,7 +447,7 @@ pub const DeclGen = struct {
447447 // TODO: There are many constraints which are ignored for now: We may only create pointers to certain types, and to other types
448448 // if more capabilities are enabled. For example, we may only create pointers to f16 if Float16Buffer is enabled.
449449 // These also relates to the pointer's address space.
450 const child_id = try self.genType(ty.elemType());
450 const child_id = try self.genType(src, ty.elemType());
451451
452452 try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id });
453453
......@@ -460,7 +460,7 @@ pub const DeclGen = struct {
460460
461461 if (decl.val.castTag(.function)) |func_payload| {
462462 std.debug.assert(decl.ty.zigTypeTag() == .Fn);
463 const prototype_id = try self.genType(decl.ty);
463 const prototype_id = try self.genType(.{ .node_offset = 0 }, decl.ty);
464464 try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{
465465 self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype.
466466 result_id,
......@@ -538,7 +538,7 @@ pub const DeclGen = struct {
538538 const rhs_id = try self.resolve(inst.rhs);
539539
540540 const result_id = self.spv.allocResultId();
541 const result_type_id = try self.genType(inst.base.ty);
541 const result_type_id = try self.genType(inst.base.src, inst.base.ty);
542542
543543 // TODO: Is the result the same as the argument types?
544544 // This is supposed to be the case for SPIR-V.
......@@ -599,7 +599,7 @@ pub const DeclGen = struct {
599599 const rhs_id = try self.resolve(inst.rhs);
600600
601601 const result_id = self.spv.allocResultId();
602 const result_type_id = try self.genType(inst.base.ty);
602 const result_type_id = try self.genType(inst.base.src, inst.base.ty);
603603
604604 // All of these operations should be 2 equal types -> bool
605605 std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty));
......@@ -643,7 +643,7 @@ pub const DeclGen = struct {
643643 const operand_id = try self.resolve(inst.operand);
644644
645645 const result_id = self.spv.allocResultId();
646 const result_type_id = try self.genType(inst.base.ty);
646 const result_type_id = try self.genType(inst.base.src, inst.base.ty);
647647
648648 const info = try self.arithmeticTypeInfo(inst.operand.ty);
649649
......@@ -660,7 +660,7 @@ pub const DeclGen = struct {
660660
661661 fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId {
662662 const storage_class = spec.StorageClass.Function;
663 const result_type_id = try self.genPointerType(inst.base.ty, storage_class);
663 const result_type_id = try self.genPointerType(inst.base.src, inst.base.ty, storage_class);
664664 const result_id = self.spv.allocResultId();
665665
666666 try writeInstruction(&self.spv.binary.fn_decls, .OpVariable, &[_]Word{ result_type_id, result_id, @enumToInt(storage_class) });
......@@ -676,7 +676,7 @@ pub const DeclGen = struct {
676676 fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId {
677677 const operand_id = try self.resolve(inst.operand);
678678
679 const result_type_id = try self.genType(inst.base.ty);
679 const result_type_id = try self.genType(inst.base.src, inst.base.ty);
680680 const result_id = self.spv.allocResultId();
681681
682682 const operands = if (inst.base.ty.isVolatilePtr())