| ... | ... | @@ -150,7 +150,7 @@ pub const DeclGen = struct { |
| 150 | 150 | |
| 151 | 151 | fn resolve(self: *DeclGen, inst: *Inst) !ResultId { |
| 152 | 152 | if (inst.value()) |val| { |
| 153 | | return self.genConstant(inst.ty, val); |
| 153 | return self.genConstant(inst.src, inst.ty, val); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | return self.inst_results.get(inst).?; // Instruction does not dominate all uses! |
| ... | ... | @@ -252,11 +252,11 @@ pub const DeclGen = struct { |
| 252 | 252 | |
| 253 | 253 | /// Generate a constant representing `val`. |
| 254 | 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 | 256 | const target = self.module.getTarget(); |
| 257 | 257 | const code = &self.spv.binary.types_globals_constants; |
| 258 | 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); |
| 260 | 260 | |
| 261 | 261 | if (val.isUndef()) { |
| 262 | 262 | try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id }); |
| ... | ... | @@ -268,7 +268,7 @@ pub const DeclGen = struct { |
| 268 | 268 | const int_info = ty.intInfo(target); |
| 269 | 269 | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 270 | 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 | }; |
| 273 | 273 | |
| 274 | 274 | // 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 | 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 | } |
| 330 | 330 | |
| 331 | 331 | return result_id; |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | | fn genType(self: *DeclGen, ty: Type) Error!ResultId { |
| 334 | fn genType(self: *DeclGen, src: LazySrcLoc, ty: Type) Error!ResultId { |
| 335 | 335 | // We can't use getOrPut here so we can recursively generate types. |
| 336 | 336 | if (self.spv.types.get(ty)) |already_generated| { |
| 337 | 337 | return already_generated; |
| ... | ... | @@ -348,7 +348,7 @@ pub const DeclGen = struct { |
| 348 | 348 | const int_info = ty.intInfo(target); |
| 349 | 349 | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 350 | 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 | }; |
| 353 | 353 | |
| 354 | 354 | // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here. |
| ... | ... | @@ -374,7 +374,7 @@ pub const DeclGen = struct { |
| 374 | 374 | }; |
| 375 | 375 | |
| 376 | 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 | } |
| 379 | 379 | |
| 380 | 380 | try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits }); |
| ... | ... | @@ -382,19 +382,19 @@ pub const DeclGen = struct { |
| 382 | 382 | .Fn => { |
| 383 | 383 | // We only support zig-calling-convention functions, no varargs. |
| 384 | 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 | 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", .{}); |
| 388 | 388 | |
| 389 | 389 | // In order to avoid a temporary here, first generate all the required types and then simply look them up |
| 390 | 390 | // when generating the function type. |
| 391 | 391 | const params = ty.fnParamLen(); |
| 392 | 392 | var i: usize = 0; |
| 393 | 393 | while (i < params) : (i += 1) { |
| 394 | | _ = try self.genType(ty.fnParamType(i)); |
| 394 | _ = try self.genType(src, ty.fnParamType(i)); |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | | const return_type_id = try self.genType(ty.fnReturnType()); |
| 397 | const return_type_id = try self.genType(src, ty.fnReturnType()); |
| 398 | 398 | |
| 399 | 399 | // result id + result type id + parameter type ids. |
| 400 | 400 | try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen())); |
| ... | ... | @@ -407,7 +407,7 @@ pub const DeclGen = struct { |
| 407 | 407 | } |
| 408 | 408 | }, |
| 409 | 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 | 411 | .Vector => { |
| 412 | 412 | // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations |
| 413 | 413 | // which work on them), so simply use those. |
| ... | ... | @@ -417,7 +417,7 @@ pub const DeclGen = struct { |
| 417 | 417 | // is adequate at all for this. |
| 418 | 418 | |
| 419 | 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 | 422 | .Null, |
| 423 | 423 | .Undefined, |
| ... | ... | @@ -429,7 +429,7 @@ pub const DeclGen = struct { |
| 429 | 429 | |
| 430 | 430 | .BoundFn => unreachable, // this type will be deleted from the language. |
| 431 | 431 | |
| 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 | } |
| 434 | 434 | |
| 435 | 435 | try self.spv.types.putNoClobber(ty, result_id); |
| ... | ... | @@ -438,7 +438,7 @@ pub const DeclGen = struct { |
| 438 | 438 | |
| 439 | 439 | /// SPIR-V requires pointers to have a storage class (address space), and so we have a special function for that. |
| 440 | 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 | 442 | std.debug.assert(ty.zigTypeTag() == .Pointer); |
| 443 | 443 | |
| 444 | 444 | const code = &self.spv.binary.types_globals_constants; |
| ... | ... | @@ -447,7 +447,7 @@ pub const DeclGen = struct { |
| 447 | 447 | // TODO: There are many constraints which are ignored for now: We may only create pointers to certain types, and to other types |
| 448 | 448 | // if more capabilities are enabled. For example, we may only create pointers to f16 if Float16Buffer is enabled. |
| 449 | 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()); |
| 451 | 451 | |
| 452 | 452 | try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id }); |
| 453 | 453 | |
| ... | ... | @@ -460,7 +460,7 @@ pub const DeclGen = struct { |
| 460 | 460 | |
| 461 | 461 | if (decl.val.castTag(.function)) |func_payload| { |
| 462 | 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 | 464 | try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ |
| 465 | 465 | self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. |
| 466 | 466 | result_id, |
| ... | ... | @@ -538,7 +538,7 @@ pub const DeclGen = struct { |
| 538 | 538 | const rhs_id = try self.resolve(inst.rhs); |
| 539 | 539 | |
| 540 | 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); |
| 542 | 542 | |
| 543 | 543 | // TODO: Is the result the same as the argument types? |
| 544 | 544 | // This is supposed to be the case for SPIR-V. |
| ... | ... | @@ -599,7 +599,7 @@ pub const DeclGen = struct { |
| 599 | 599 | const rhs_id = try self.resolve(inst.rhs); |
| 600 | 600 | |
| 601 | 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); |
| 603 | 603 | |
| 604 | 604 | // All of these operations should be 2 equal types -> bool |
| 605 | 605 | std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty)); |
| ... | ... | @@ -643,7 +643,7 @@ pub const DeclGen = struct { |
| 643 | 643 | const operand_id = try self.resolve(inst.operand); |
| 644 | 644 | |
| 645 | 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); |
| 647 | 647 | |
| 648 | 648 | const info = try self.arithmeticTypeInfo(inst.operand.ty); |
| 649 | 649 | |
| ... | ... | @@ -660,7 +660,7 @@ pub const DeclGen = struct { |
| 660 | 660 | |
| 661 | 661 | fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId { |
| 662 | 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 | 664 | const result_id = self.spv.allocResultId(); |
| 665 | 665 | |
| 666 | 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 | 676 | fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { |
| 677 | 677 | const operand_id = try self.resolve(inst.operand); |
| 678 | 678 | |
| 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 | 680 | const result_id = self.spv.allocResultId(); |
| 681 | 681 | |
| 682 | 682 | const operands = if (inst.base.ty.isVolatilePtr()) |