| ... | @@ -256,7 +256,7 @@ pub const DeclGen = struct { | ... | @@ -256,7 +256,7 @@ pub const DeclGen = struct { |
| 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.getOrGenType(ty); | 259 | const result_type_id = try self.genType(ty); |
| 260 | | 260 | |
| 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 }); |
| ... | @@ -304,7 +304,7 @@ pub const DeclGen = struct { | ... | @@ -304,7 +304,7 @@ pub const DeclGen = struct { |
| 304 | }, | 304 | }, |
| 305 | .Float => { | 305 | .Float => { |
| 306 | // At this point we are guaranteed that the target floating point type is supported, otherwise the function | 306 | // At this point we are guaranteed that the target floating point type is supported, otherwise the function |
| 307 | // would have exited at getOrGenType(ty). | 307 | // would have exited at genType(ty). |
| 308 | | 308 | |
| 309 | // f16 and f32 require one word of storage. f64 requires 2, low-order first. | 309 | // f16 and f32 require one word of storage. f64 requires 2, low-order first. |
| 310 | | 310 | |
| ... | @@ -320,7 +320,7 @@ pub const DeclGen = struct { | ... | @@ -320,7 +320,7 @@ pub const DeclGen = struct { |
| 320 | @truncate(u32, float_bits >> @bitSizeOf(u32)), | 320 | @truncate(u32, float_bits >> @bitSizeOf(u32)), |
| 321 | }); | 321 | }); |
| 322 | }, | 322 | }, |
| 323 | 128 => unreachable, // Filtered out in the call to getOrGenType. | 323 | 128 => unreachable, // Filtered out in the call to genType. |
| 324 | // TODO: Insert case for long double when the layout for that is determined. | 324 | // TODO: Insert case for long double when the layout for that is determined. |
| 325 | else => unreachable, | 325 | else => unreachable, |
| 326 | } | 326 | } |
| ... | @@ -331,7 +331,7 @@ pub const DeclGen = struct { | ... | @@ -331,7 +331,7 @@ pub const DeclGen = struct { |
| 331 | return result_id; | 331 | return result_id; |
| 332 | } | 332 | } |
| 333 | | 333 | |
| 334 | fn getOrGenType(self: *DeclGen, ty: Type) Error!ResultId { | 334 | fn genType(self: *DeclGen, 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; |
| ... | @@ -391,10 +391,10 @@ pub const DeclGen = struct { | ... | @@ -391,10 +391,10 @@ pub const DeclGen = struct { |
| 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.getOrGenType(ty.fnParamType(i)); | 394 | _ = try self.genType(ty.fnParamType(i)); |
| 395 | } | 395 | } |
| 396 | | 396 | |
| 397 | const return_type_id = try self.getOrGenType(ty.fnReturnType()); | 397 | const return_type_id = try self.genType(ty.fnReturnType()); |
| 398 | | 398 | |
| 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())); |
| ... | @@ -406,6 +406,8 @@ pub const DeclGen = struct { | ... | @@ -406,6 +406,8 @@ pub const DeclGen = struct { |
| 406 | try code.append(param_type_id); | 406 | try code.append(param_type_id); |
| 407 | } | 407 | } |
| 408 | }, | 408 | }, |
| | 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", .{}), |
| 409 | .Vector => { | 411 | .Vector => { |
| 410 | // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations | 412 | // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations |
| 411 | // which work on them), so simply use those. | 413 | // which work on them), so simply use those. |
| ... | @@ -434,13 +436,31 @@ pub const DeclGen = struct { | ... | @@ -434,13 +436,31 @@ pub const DeclGen = struct { |
| 434 | return result_id; | 436 | return result_id; |
| 435 | } | 437 | } |
| 436 | | 438 | |
| | 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. |
| | 441 | fn genPointerType(self: *DeclGen, ty: Type, storage_class: spec.StorageClass) !ResultId { |
| | 442 | std.debug.assert(ty.zigTypeTag() == .Pointer); |
| | 443 | |
| | 444 | const code = &self.spv.binary.types_globals_constants; |
| | 445 | const result_id = self.spv.allocResultId(); |
| | 446 | |
| | 447 | // 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. |
| | 449 | // These also relates to the pointer's address space. |
| | 450 | const child_id = try self.genType(ty.elemType()); |
| | 451 | |
| | 452 | try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id }); |
| | 453 | |
| | 454 | return result_id; |
| | 455 | } |
| | 456 | |
| 437 | pub fn gen(self: *DeclGen) !void { | 457 | pub fn gen(self: *DeclGen) !void { |
| 438 | const decl = self.decl; | 458 | const decl = self.decl; |
| 439 | const result_id = decl.fn_link.spirv.id; | 459 | const result_id = decl.fn_link.spirv.id; |
| 440 | | 460 | |
| 441 | if (decl.val.castTag(.function)) |func_payload| { | 461 | if (decl.val.castTag(.function)) |func_payload| { |
| 442 | std.debug.assert(decl.ty.zigTypeTag() == .Fn); | 462 | std.debug.assert(decl.ty.zigTypeTag() == .Fn); |
| 443 | const prototype_id = try self.getOrGenType(decl.ty); | 463 | const prototype_id = try self.genType(decl.ty); |
| 444 | try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ | 464 | try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ |
| 445 | 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. |
| 446 | result_id, | 466 | result_id, |
| ... | @@ -496,13 +516,17 @@ pub const DeclGen = struct { | ... | @@ -496,13 +516,17 @@ pub const DeclGen = struct { |
| 496 | .bool_and => try self.genBinOp(inst.castTag(.bool_and).?), | 516 | .bool_and => try self.genBinOp(inst.castTag(.bool_and).?), |
| 497 | .bool_or => try self.genBinOp(inst.castTag(.bool_or).?), | 517 | .bool_or => try self.genBinOp(inst.castTag(.bool_or).?), |
| 498 | .not => try self.genUnOp(inst.castTag(.not).?), | 518 | .not => try self.genUnOp(inst.castTag(.not).?), |
| | 519 | .alloc => try self.genAlloc(inst.castTag(.alloc).?), |
| 499 | .arg => self.genArg(), | 520 | .arg => self.genArg(), |
| 500 | // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them | 521 | // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them |
| 501 | // throughout the IR. | 522 | // throughout the IR. |
| 502 | .breakpoint => null, | 523 | .breakpoint => null, |
| | 524 | .constant => unreachable, |
| 503 | .dbg_stmt => null, | 525 | .dbg_stmt => null, |
| | 526 | .load => try self.genLoad(inst.castTag(.load).?), |
| 504 | .ret => self.genRet(inst.castTag(.ret).?), | 527 | .ret => self.genRet(inst.castTag(.ret).?), |
| 505 | .retvoid => self.genRetVoid(), | 528 | .retvoid => self.genRetVoid(), |
| | 529 | .store => try self.genStore(inst.castTag(.store).?), |
| 506 | .unreach => self.genUnreach(), | 530 | .unreach => self.genUnreach(), |
| 507 | else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), | 531 | else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), |
| 508 | }; | 532 | }; |
| ... | @@ -514,7 +538,7 @@ pub const DeclGen = struct { | ... | @@ -514,7 +538,7 @@ pub const DeclGen = struct { |
| 514 | const rhs_id = try self.resolve(inst.rhs); | 538 | const rhs_id = try self.resolve(inst.rhs); |
| 515 | | 539 | |
| 516 | const result_id = self.spv.allocResultId(); | 540 | const result_id = self.spv.allocResultId(); |
| 517 | const result_type_id = try self.getOrGenType(inst.base.ty); | 541 | const result_type_id = try self.genType(inst.base.ty); |
| 518 | | 542 | |
| 519 | // TODO: Is the result the same as the argument types? | 543 | // TODO: Is the result the same as the argument types? |
| 520 | // This is supposed to be the case for SPIR-V. | 544 | // This is supposed to be the case for SPIR-V. |
| ... | @@ -527,10 +551,11 @@ pub const DeclGen = struct { | ... | @@ -527,10 +551,11 @@ pub const DeclGen = struct { |
| 527 | // instead. | 551 | // instead. |
| 528 | const info = try self.arithmeticTypeInfo(inst.lhs.ty); | 552 | const info = try self.arithmeticTypeInfo(inst.lhs.ty); |
| 529 | | 553 | |
| 530 | if (info.class == .composite_integer) | 554 | if (info.class == .composite_integer) { |
| 531 | return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); | 555 | return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); |
| 532 | else if (info.class == .strange_integer) | 556 | } else if (info.class == .strange_integer) { |
| 533 | return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for strange integers", .{}); | 557 | return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for strange integers", .{}); |
| | 558 | } |
| 534 | | 559 | |
| 535 | const is_bool = info.class == .bool; | 560 | const is_bool = info.class == .bool; |
| 536 | const is_float = info.class == .float; | 561 | const is_float = info.class == .float; |
| ... | @@ -574,7 +599,7 @@ pub const DeclGen = struct { | ... | @@ -574,7 +599,7 @@ pub const DeclGen = struct { |
| 574 | const rhs_id = try self.resolve(inst.rhs); | 599 | const rhs_id = try self.resolve(inst.rhs); |
| 575 | | 600 | |
| 576 | const result_id = self.spv.allocResultId(); | 601 | const result_id = self.spv.allocResultId(); |
| 577 | const result_type_id = try self.getOrGenType(inst.base.ty); | 602 | const result_type_id = try self.genType(inst.base.ty); |
| 578 | | 603 | |
| 579 | // All of these operations should be 2 equal types -> bool | 604 | // All of these operations should be 2 equal types -> bool |
| 580 | std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty)); | 605 | std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty)); |
| ... | @@ -586,10 +611,11 @@ pub const DeclGen = struct { | ... | @@ -586,10 +611,11 @@ pub const DeclGen = struct { |
| 586 | // from either of the operands. | 611 | // from either of the operands. |
| 587 | const info = try self.arithmeticTypeInfo(inst.lhs.ty); | 612 | const info = try self.arithmeticTypeInfo(inst.lhs.ty); |
| 588 | | 613 | |
| 589 | if (info.class == .composite_integer) | 614 | if (info.class == .composite_integer) { |
| 590 | return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); | 615 | return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); |
| 591 | else if (info.class == .strange_integer) | 616 | } else if (info.class == .strange_integer) { |
| 592 | return self.fail(inst.base.src, "TODO: SPIR-V backend: comparison for strange integers", .{}); | 617 | return self.fail(inst.base.src, "TODO: SPIR-V backend: comparison for strange integers", .{}); |
| | 618 | } |
| 593 | | 619 | |
| 594 | const is_bool = info.class == .bool; | 620 | const is_bool = info.class == .bool; |
| 595 | const is_float = info.class == .float; | 621 | const is_float = info.class == .float; |
| ... | @@ -617,7 +643,7 @@ pub const DeclGen = struct { | ... | @@ -617,7 +643,7 @@ pub const DeclGen = struct { |
| 617 | const operand_id = try self.resolve(inst.operand); | 643 | const operand_id = try self.resolve(inst.operand); |
| 618 | | 644 | |
| 619 | const result_id = self.spv.allocResultId(); | 645 | const result_id = self.spv.allocResultId(); |
| 620 | const result_type_id = try self.getOrGenType(inst.base.ty); | 646 | const result_type_id = try self.genType(inst.base.ty); |
| 621 | | 647 | |
| 622 | const info = try self.arithmeticTypeInfo(inst.operand.ty); | 648 | const info = try self.arithmeticTypeInfo(inst.operand.ty); |
| 623 | | 649 | |
| ... | @@ -632,11 +658,37 @@ pub const DeclGen = struct { | ... | @@ -632,11 +658,37 @@ pub const DeclGen = struct { |
| 632 | return result_id; | 658 | return result_id; |
| 633 | } | 659 | } |
| 634 | | 660 | |
| | 661 | fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId { |
| | 662 | const storage_class = spec.StorageClass.Function; |
| | 663 | const result_type_id = try self.genPointerType(inst.base.ty, storage_class); |
| | 664 | const result_id = self.spv.allocResultId(); |
| | 665 | |
| | 666 | try writeInstruction(&self.spv.binary.fn_decls, .OpVariable, &[_]Word{ result_type_id, result_id, @enumToInt(storage_class) }); |
| | 667 | |
| | 668 | return result_id; |
| | 669 | } |
| | 670 | |
| 635 | fn genArg(self: *DeclGen) ResultId { | 671 | fn genArg(self: *DeclGen) ResultId { |
| 636 | defer self.next_arg_index += 1; | 672 | defer self.next_arg_index += 1; |
| 637 | return self.args.items[self.next_arg_index]; | 673 | return self.args.items[self.next_arg_index]; |
| 638 | } | 674 | } |
| 639 | | 675 | |
| | 676 | fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { |
| | 677 | const operand_id = try self.resolve(inst.operand); |
| | 678 | |
| | 679 | const result_type_id = try self.genType(inst.base.ty); |
| | 680 | const result_id = self.spv.allocResultId(); |
| | 681 | |
| | 682 | const operands = if (inst.base.ty.isVolatilePtr()) |
| | 683 | &[_]Word{ result_type_id, result_id, operand_id, @bitCast(u32, spec.MemoryAccess{.Volatile = true}) } |
| | 684 | else |
| | 685 | &[_]Word{ result_type_id, result_id, operand_id}; |
| | 686 | |
| | 687 | try writeInstruction(&self.spv.binary.fn_decls, .OpLoad, operands); |
| | 688 | |
| | 689 | return result_id; |
| | 690 | } |
| | 691 | |
| 640 | fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { | 692 | fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { |
| 641 | const operand_id = try self.resolve(inst.operand); | 693 | const operand_id = try self.resolve(inst.operand); |
| 642 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? | 694 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? |
| ... | @@ -650,6 +702,19 @@ pub const DeclGen = struct { | ... | @@ -650,6 +702,19 @@ pub const DeclGen = struct { |
| 650 | return null; | 702 | return null; |
| 651 | } | 703 | } |
| 652 | | 704 | |
| | 705 | fn genStore(self: *DeclGen, inst: *Inst.BinOp) !?ResultId { |
| | 706 | const dst_ptr_id = try self.resolve(inst.lhs); |
| | 707 | const src_val_id = try self.resolve(inst.rhs); |
| | 708 | |
| | 709 | const operands = if (inst.lhs.ty.isVolatilePtr()) |
| | 710 | &[_]Word{ dst_ptr_id, src_val_id, @bitCast(u32, spec.MemoryAccess{.Volatile = true}) } |
| | 711 | else |
| | 712 | &[_]Word{ dst_ptr_id, src_val_id }; |
| | 713 | |
| | 714 | try writeInstruction(&self.spv.binary.fn_decls, .OpStore, operands); |
| | 715 | return null; |
| | 716 | } |
| | 717 | |
| 653 | fn genUnreach(self: *DeclGen) !?ResultId { | 718 | fn genUnreach(self: *DeclGen) !?ResultId { |
| 654 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? | 719 | // TODO: This instruction needs to be the last in a block. Is that guaranteed? |
| 655 | try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{}); | 720 | try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{}); |