authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-20 14:03:04+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-22 16:11:56+02:00
log63d0576f1ccfd6ec1538459c6ac69b9f892b2142
treec2dc6fc79e3e7244cbc624ea0bd625a50a2277ba
parent6a121d9ccde34556ffb1baf3b8543defdf6136e0

SPIR-V: Preliminary alloc/store/load generation


1 files changed, 79 insertions(+), 14 deletions(-)

src/codegen/spirv.zig+79-14
......@@ -256,7 +256,7 @@ pub const DeclGen = struct {
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.getOrGenType(ty);
259 const result_type_id = try self.genType(ty);
260260
261261 if (val.isUndef()) {
262262 try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id });
......@@ -304,7 +304,7 @@ pub const DeclGen = struct {
304304 },
305305 .Float => {
306306 // 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).
308308
309309 // f16 and f32 require one word of storage. f64 requires 2, low-order first.
310310
......@@ -320,7 +320,7 @@ pub const DeclGen = struct {
320320 @truncate(u32, float_bits >> @bitSizeOf(u32)),
321321 });
322322 },
323 128 => unreachable, // Filtered out in the call to getOrGenType.
323 128 => unreachable, // Filtered out in the call to genType.
324324 // TODO: Insert case for long double when the layout for that is determined.
325325 else => unreachable,
326326 }
......@@ -331,7 +331,7 @@ pub const DeclGen = struct {
331331 return result_id;
332332 }
333333
334 fn getOrGenType(self: *DeclGen, ty: Type) Error!ResultId {
334 fn genType(self: *DeclGen, 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;
......@@ -391,10 +391,10 @@ pub const DeclGen = struct {
391391 const params = ty.fnParamLen();
392392 var i: usize = 0;
393393 while (i < params) : (i += 1) {
394 _ = try self.getOrGenType(ty.fnParamType(i));
394 _ = try self.genType(ty.fnParamType(i));
395395 }
396396
397 const return_type_id = try self.getOrGenType(ty.fnReturnType());
397 const return_type_id = try self.genType(ty.fnReturnType());
398398
399399 // result id + result type id + parameter type ids.
400400 try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen()));
......@@ -406,6 +406,8 @@ pub const DeclGen = struct {
406406 try code.append(param_type_id);
407407 }
408408 },
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", .{}),
409411 .Vector => {
410412 // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations
411413 // which work on them), so simply use those.
......@@ -434,13 +436,31 @@ pub const DeclGen = struct {
434436 return result_id;
435437 }
436438
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
437457 pub fn gen(self: *DeclGen) !void {
438458 const decl = self.decl;
439459 const result_id = decl.fn_link.spirv.id;
440460
441461 if (decl.val.castTag(.function)) |func_payload| {
442462 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);
444464 try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{
445465 self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype.
446466 result_id,
......@@ -496,13 +516,17 @@ pub const DeclGen = struct {
496516 .bool_and => try self.genBinOp(inst.castTag(.bool_and).?),
497517 .bool_or => try self.genBinOp(inst.castTag(.bool_or).?),
498518 .not => try self.genUnOp(inst.castTag(.not).?),
519 .alloc => try self.genAlloc(inst.castTag(.alloc).?),
499520 .arg => self.genArg(),
500521 // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them
501522 // throughout the IR.
502523 .breakpoint => null,
524 .constant => unreachable,
503525 .dbg_stmt => null,
526 .load => try self.genLoad(inst.castTag(.load).?),
504527 .ret => self.genRet(inst.castTag(.ret).?),
505528 .retvoid => self.genRetVoid(),
529 .store => try self.genStore(inst.castTag(.store).?),
506530 .unreach => self.genUnreach(),
507531 else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}),
508532 };
......@@ -514,7 +538,7 @@ pub const DeclGen = struct {
514538 const rhs_id = try self.resolve(inst.rhs);
515539
516540 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);
518542
519543 // TODO: Is the result the same as the argument types?
520544 // This is supposed to be the case for SPIR-V.
......@@ -527,10 +551,11 @@ pub const DeclGen = struct {
527551 // instead.
528552 const info = try self.arithmeticTypeInfo(inst.lhs.ty);
529553
530 if (info.class == .composite_integer)
554 if (info.class == .composite_integer) {
531555 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) {
533557 return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for strange integers", .{});
558 }
534559
535560 const is_bool = info.class == .bool;
536561 const is_float = info.class == .float;
......@@ -574,7 +599,7 @@ pub const DeclGen = struct {
574599 const rhs_id = try self.resolve(inst.rhs);
575600
576601 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);
578603
579604 // All of these operations should be 2 equal types -> bool
580605 std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty));
......@@ -586,10 +611,11 @@ pub const DeclGen = struct {
586611 // from either of the operands.
587612 const info = try self.arithmeticTypeInfo(inst.lhs.ty);
588613
589 if (info.class == .composite_integer)
614 if (info.class == .composite_integer) {
590615 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) {
592617 return self.fail(inst.base.src, "TODO: SPIR-V backend: comparison for strange integers", .{});
618 }
593619
594620 const is_bool = info.class == .bool;
595621 const is_float = info.class == .float;
......@@ -617,7 +643,7 @@ pub const DeclGen = struct {
617643 const operand_id = try self.resolve(inst.operand);
618644
619645 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);
621647
622648 const info = try self.arithmeticTypeInfo(inst.operand.ty);
623649
......@@ -632,11 +658,37 @@ pub const DeclGen = struct {
632658 return result_id;
633659 }
634660
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
635671 fn genArg(self: *DeclGen) ResultId {
636672 defer self.next_arg_index += 1;
637673 return self.args.items[self.next_arg_index];
638674 }
639675
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
640692 fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId {
641693 const operand_id = try self.resolve(inst.operand);
642694 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
......@@ -650,6 +702,19 @@ pub const DeclGen = struct {
650702 return null;
651703 }
652704
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
653718 fn genUnreach(self: *DeclGen) !?ResultId {
654719 // TODO: This instruction needs to be the last in a block. Is that guaranteed?
655720 try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{});