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 {...@@ -150,7 +150,7 @@ pub const DeclGen = struct {
150150
151 fn resolve(self: *DeclGen, inst: *Inst) !ResultId {151 fn resolve(self: *DeclGen, inst: *Inst) !ResultId {
152 if (inst.value()) |val| {152 if (inst.value()) |val| {
153 return self.genConstant(inst.ty, val);153 return self.genConstant(inst.src, inst.ty, val);
154 }154 }
155155
156 return self.inst_results.get(inst).?; // Instruction does not dominate all uses!156 return self.inst_results.get(inst).?; // Instruction does not dominate all uses!
...@@ -252,11 +252,11 @@ pub const DeclGen = struct {...@@ -252,11 +252,11 @@ pub const DeclGen = struct {
252252
253 /// Generate a constant representing `val`.253 /// Generate a constant representing `val`.
254 /// TODO: Deduplication?254 /// 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 {
256 const target = self.module.getTarget();256 const target = self.module.getTarget();
257 const code = &self.spv.binary.types_globals_constants;257 const code = &self.spv.binary.types_globals_constants;
258 const result_id = self.spv.allocResultId();258 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
261 if (val.isUndef()) {261 if (val.isUndef()) {
262 try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id });262 try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id });
...@@ -268,7 +268,7 @@ pub const DeclGen = struct {...@@ -268,7 +268,7 @@ pub const DeclGen = struct {
268 const int_info = ty.intInfo(target);268 const int_info = ty.intInfo(target);
269 const backing_bits = self.backingIntBits(int_info.bits) orelse {269 const backing_bits = self.backingIntBits(int_info.bits) orelse {
270 // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits.270 // 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});
272 };272 };
273273
274 // We can just use toSignedInt/toUnsignedInt here as it returns u64 - a type large enough to hold any274 // 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 {...@@ -325,13 +325,13 @@ pub const DeclGen = struct {
325 else => unreachable,325 else => unreachable,
326 }326 }
327 },327 },
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()}),
329 }329 }
330330
331 return result_id;331 return result_id;
332 }332 }
333333
334 fn genType(self: *DeclGen, ty: Type) Error!ResultId {334 fn genType(self: *DeclGen, src: LazySrcLoc, ty: Type) Error!ResultId {
335 // We can't use getOrPut here so we can recursively generate types.335 // We can't use getOrPut here so we can recursively generate types.
336 if (self.spv.types.get(ty)) |already_generated| {336 if (self.spv.types.get(ty)) |already_generated| {
337 return already_generated;337 return already_generated;
...@@ -348,7 +348,7 @@ pub const DeclGen = struct {...@@ -348,7 +348,7 @@ pub const DeclGen = struct {
348 const int_info = ty.intInfo(target);348 const int_info = ty.intInfo(target);
349 const backing_bits = self.backingIntBits(int_info.bits) orelse {349 const backing_bits = self.backingIntBits(int_info.bits) orelse {
350 // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits.350 // 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});
352 };352 };
353353
354 // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here.354 // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here.
...@@ -374,7 +374,7 @@ pub const DeclGen = struct {...@@ -374,7 +374,7 @@ pub const DeclGen = struct {
374 };374 };
375375
376 if (!supported) {376 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});
378 }378 }
379379
380 try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits });380 try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits });
...@@ -382,19 +382,19 @@ pub const DeclGen = struct {...@@ -382,19 +382,19 @@ pub const DeclGen = struct {
382 .Fn => {382 .Fn => {
383 // We only support zig-calling-convention functions, no varargs.383 // We only support zig-calling-convention functions, no varargs.
384 if (ty.fnCallingConvention() != .Unspecified)384 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", .{});
386 if (ty.fnIsVarArgs())386 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
389 // In order to avoid a temporary here, first generate all the required types and then simply look them up389 // In order to avoid a temporary here, first generate all the required types and then simply look them up
390 // when generating the function type.390 // when generating the function type.
391 const params = ty.fnParamLen();391 const params = ty.fnParamLen();
392 var i: usize = 0;392 var i: usize = 0;
393 while (i < params) : (i += 1) {393 while (i < params) : (i += 1) {
394 _ = try self.genType(ty.fnParamType(i));394 _ = try self.genType(src, ty.fnParamType(i));
395 }395 }
396396
397 const return_type_id = try self.genType(ty.fnReturnType());397 const return_type_id = try self.genType(src, ty.fnReturnType());
398398
399 // result id + result type id + parameter type ids.399 // result id + result type id + parameter type ids.
400 try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen()));400 try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen()));
...@@ -407,7 +407,7 @@ pub const DeclGen = struct {...@@ -407,7 +407,7 @@ pub const DeclGen = struct {
407 }407 }
408 },408 },
409 // When recursively generating a type, we cannot infer the pointer's storage class. See genPointerType.409 // 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", .{}),
411 .Vector => {411 .Vector => {
412 // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations412 // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations
413 // which work on them), so simply use those.413 // which work on them), so simply use those.
...@@ -417,7 +417,7 @@ pub const DeclGen = struct {...@@ -417,7 +417,7 @@ pub const DeclGen = struct {
417 // is adequate at all for this.417 // is adequate at all for this.
418418
419 // TODO: Vectors are not yet supported by the self-hosted compiler itself it seems.419 // 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", .{});
421 },421 },
422 .Null,422 .Null,
423 .Undefined,423 .Undefined,
...@@ -429,7 +429,7 @@ pub const DeclGen = struct {...@@ -429,7 +429,7 @@ pub const DeclGen = struct {
429429
430 .BoundFn => unreachable, // this type will be deleted from the language.430 .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}),
433 }433 }
434434
435 try self.spv.types.putNoClobber(ty, result_id);435 try self.spv.types.putNoClobber(ty, result_id);
...@@ -438,7 +438,7 @@ pub const DeclGen = struct {...@@ -438,7 +438,7 @@ pub const DeclGen = struct {
438438
439 /// SPIR-V requires pointers to have a storage class (address space), and so we have a special function for that.439 /// SPIR-V requires pointers to have a storage class (address space), and so we have a special function for that.
440 /// TODO: The result of this needs to be cached.440 /// 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 {
442 std.debug.assert(ty.zigTypeTag() == .Pointer);442 std.debug.assert(ty.zigTypeTag() == .Pointer);
443443
444 const code = &self.spv.binary.types_globals_constants;444 const code = &self.spv.binary.types_globals_constants;
...@@ -447,7 +447,7 @@ pub const DeclGen = struct {...@@ -447,7 +447,7 @@ pub const DeclGen = struct {
447 // TODO: There are many constraints which are ignored for now: We may only create pointers to certain types, and to other types447 // TODO: There are many constraints which are ignored for now: We may only create pointers to certain types, and to other types
448 // if more capabilities are enabled. For example, we may only create pointers to f16 if Float16Buffer is enabled.448 // if more capabilities are enabled. For example, we may only create pointers to f16 if Float16Buffer is enabled.
449 // These also relates to the pointer's address space.449 // 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
452 try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id });452 try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id });
453453
...@@ -460,7 +460,7 @@ pub const DeclGen = struct {...@@ -460,7 +460,7 @@ pub const DeclGen = struct {
460460
461 if (decl.val.castTag(.function)) |func_payload| {461 if (decl.val.castTag(.function)) |func_payload| {
462 std.debug.assert(decl.ty.zigTypeTag() == .Fn);462 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);
464 try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{464 try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{
465 self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype.465 self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype.
466 result_id,466 result_id,
...@@ -538,7 +538,7 @@ pub const DeclGen = struct {...@@ -538,7 +538,7 @@ pub const DeclGen = struct {
538 const rhs_id = try self.resolve(inst.rhs);538 const rhs_id = try self.resolve(inst.rhs);
539539
540 const result_id = self.spv.allocResultId();540 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
543 // TODO: Is the result the same as the argument types?543 // TODO: Is the result the same as the argument types?
544 // This is supposed to be the case for SPIR-V.544 // This is supposed to be the case for SPIR-V.
...@@ -599,7 +599,7 @@ pub const DeclGen = struct {...@@ -599,7 +599,7 @@ pub const DeclGen = struct {
599 const rhs_id = try self.resolve(inst.rhs);599 const rhs_id = try self.resolve(inst.rhs);
600600
601 const result_id = self.spv.allocResultId();601 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
604 // All of these operations should be 2 equal types -> bool604 // All of these operations should be 2 equal types -> bool
605 std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty));605 std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty));
...@@ -643,7 +643,7 @@ pub const DeclGen = struct {...@@ -643,7 +643,7 @@ pub const DeclGen = struct {
643 const operand_id = try self.resolve(inst.operand);643 const operand_id = try self.resolve(inst.operand);
644644
645 const result_id = self.spv.allocResultId();645 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
648 const info = try self.arithmeticTypeInfo(inst.operand.ty);648 const info = try self.arithmeticTypeInfo(inst.operand.ty);
649649
...@@ -660,7 +660,7 @@ pub const DeclGen = struct {...@@ -660,7 +660,7 @@ pub const DeclGen = struct {
660660
661 fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId {661 fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId {
662 const storage_class = spec.StorageClass.Function;662 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);
664 const result_id = self.spv.allocResultId();664 const result_id = self.spv.allocResultId();
665665
666 try writeInstruction(&self.spv.binary.fn_decls, .OpVariable, &[_]Word{ result_type_id, result_id, @enumToInt(storage_class) });666 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 {...@@ -676,7 +676,7 @@ pub const DeclGen = struct {
676 fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId {676 fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId {
677 const operand_id = try self.resolve(inst.operand);677 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);
680 const result_id = self.spv.allocResultId();680 const result_id = self.spv.allocResultId();
681681
682 const operands = if (inst.base.ty.isVolatilePtr())682 const operands = if (inst.base.ty.isVolatilePtr())