| ... | ... | @@ -19,6 +19,7 @@ const Word = spec.Word; |
| 19 | 19 | const IdRef = spec.IdRef; |
| 20 | 20 | const IdResult = spec.IdResult; |
| 21 | 21 | const IdResultType = spec.IdResultType; |
| 22 | const StorageClass = spec.StorageClass; |
| 22 | 23 | |
| 23 | 24 | const SpvModule = @import("spirv/Module.zig"); |
| 24 | 25 | const SpvSection = @import("spirv/Section.zig"); |
| ... | ... | @@ -32,11 +33,14 @@ const IncomingBlock = struct { |
| 32 | 33 | break_value_id: IdRef, |
| 33 | 34 | }; |
| 34 | 35 | |
| 35 | | pub const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, struct { |
| 36 | const BlockMap = std.AutoHashMapUnmanaged(Air.Inst.Index, struct { |
| 36 | 37 | label_id: IdRef, |
| 37 | 38 | incoming_blocks: *std.ArrayListUnmanaged(IncomingBlock), |
| 38 | 39 | }); |
| 39 | 40 | |
| 41 | /// Maps Zig decl indices to linking SPIR-V linking information. |
| 42 | pub const DeclLinkMap = std.AutoHashMap(Module.Decl.Index, SpvModule.Decl.Index); |
| 43 | |
| 40 | 44 | /// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. |
| 41 | 45 | pub const DeclGen = struct { |
| 42 | 46 | /// A general-purpose allocator that can be used for any allocations for this DeclGen. |
| ... | ... | @@ -59,7 +63,8 @@ pub const DeclGen = struct { |
| 59 | 63 | /// Note: If the declaration is not a function, this value will be undefined! |
| 60 | 64 | liveness: Liveness, |
| 61 | 65 | |
| 62 | | ids: *const std.AutoHashMap(Decl.Index, IdResult), |
| 66 | /// Maps Zig Decl indices to SPIR-V globals. |
| 67 | decl_link: *DeclLinkMap, |
| 63 | 68 | |
| 64 | 69 | /// An array of function argument result-ids. Each index corresponds with the |
| 65 | 70 | /// function argument of the same index. |
| ... | ... | @@ -133,13 +138,23 @@ pub const DeclGen = struct { |
| 133 | 138 | class: Class, |
| 134 | 139 | }; |
| 135 | 140 | |
| 141 | /// Data can be lowered into in two basic representations: indirect, which is when |
| 142 | /// a type is stored in memory, and direct, which is how a type is stored when its |
| 143 | /// a direct SPIR-V value. |
| 144 | const Repr = enum { |
| 145 | /// A SPIR-V value as it would be used in operations. |
| 146 | direct, |
| 147 | /// A SPIR-V value as it is stored in memory. |
| 148 | indirect, |
| 149 | }; |
| 150 | |
| 136 | 151 | /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, |
| 137 | 152 | /// only set when `gen` is called. |
| 138 | 153 | pub fn init( |
| 139 | 154 | allocator: Allocator, |
| 140 | 155 | module: *Module, |
| 141 | 156 | spv: *SpvModule, |
| 142 | | ids: *const std.AutoHashMap(Decl.Index, IdResult), |
| 157 | decl_link: *DeclLinkMap, |
| 143 | 158 | ) DeclGen { |
| 144 | 159 | return .{ |
| 145 | 160 | .gpa = allocator, |
| ... | ... | @@ -148,7 +163,7 @@ pub const DeclGen = struct { |
| 148 | 163 | .decl_index = undefined, |
| 149 | 164 | .air = undefined, |
| 150 | 165 | .liveness = undefined, |
| 151 | | .ids = ids, |
| 166 | .decl_link = decl_link, |
| 152 | 167 | .next_arg_index = undefined, |
| 153 | 168 | .current_block_label_id = undefined, |
| 154 | 169 | .error_msg = undefined, |
| ... | ... | @@ -215,19 +230,50 @@ pub const DeclGen = struct { |
| 215 | 230 | /// Fetch the result-id for a previously generated instruction or constant. |
| 216 | 231 | fn resolve(self: *DeclGen, inst: Air.Inst.Ref) !IdRef { |
| 217 | 232 | if (self.air.value(inst)) |val| { |
| 218 | | return self.genConstant(self.air.typeOf(inst), val); |
| 233 | const ty = self.air.typeOf(inst); |
| 234 | if (ty.zigTypeTag() == .Fn) { |
| 235 | const fn_decl_index = switch (val.tag()) { |
| 236 | .extern_fn => val.castTag(.extern_fn).?.data.owner_decl, |
| 237 | .function => val.castTag(.function).?.data.owner_decl, |
| 238 | else => unreachable, |
| 239 | }; |
| 240 | const spv_decl_index = try self.resolveDecl(fn_decl_index); |
| 241 | return self.spv.declPtr(spv_decl_index).result_id; |
| 242 | } |
| 243 | |
| 244 | return try self.constant(ty, val); |
| 219 | 245 | } |
| 220 | 246 | const index = Air.refToIndex(inst).?; |
| 221 | 247 | return self.inst_results.get(index).?; // Assertion means instruction does not dominate usage. |
| 222 | 248 | } |
| 223 | 249 | |
| 250 | /// Fetch or allocate a result id for decl index. This function also marks the decl as alive. |
| 251 | /// Note: Function does not actually generate the decl. |
| 252 | fn resolveDecl(self: *DeclGen, decl_index: Module.Decl.Index) !SpvModule.Decl.Index { |
| 253 | const decl = self.module.declPtr(decl_index); |
| 254 | self.module.markDeclAlive(decl); |
| 255 | |
| 256 | const entry = try self.decl_link.getOrPut(decl_index); |
| 257 | if (!entry.found_existing) { |
| 258 | // TODO: Extern fn? |
| 259 | const kind: SpvModule.DeclKind = if (decl.val.tag() == .function) |
| 260 | .func |
| 261 | else |
| 262 | .global; |
| 263 | |
| 264 | entry.value_ptr.* = try self.spv.allocDecl(kind); |
| 265 | } |
| 266 | |
| 267 | return entry.value_ptr.*; |
| 268 | } |
| 269 | |
| 224 | 270 | /// Start a new SPIR-V block, Emits the label of the new block, and stores which |
| 225 | 271 | /// block we are currently generating. |
| 226 | 272 | /// Note that there is no such thing as nested blocks like in ZIR or AIR, so we don't need to |
| 227 | 273 | /// keep track of the previous block. |
| 228 | 274 | fn beginSpvBlock(self: *DeclGen, label_id: IdResult) !void { |
| 229 | 275 | try self.func.body.emit(self.spv.gpa, .OpLabel, .{ .id_result = label_id }); |
| 230 | | self.current_block_label_id = label_id.toRef(); |
| 276 | self.current_block_label_id = label_id; |
| 231 | 277 | } |
| 232 | 278 | |
| 233 | 279 | /// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need |
| ... | ... | @@ -325,150 +371,752 @@ pub const DeclGen = struct { |
| 325 | 371 | // As of yet, there is no vector support in the self-hosted compiler. |
| 326 | 372 | .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}), |
| 327 | 373 | // TODO: For which types is this the case? |
| 328 | | else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmtDebug()}), |
| 374 | else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}), |
| 329 | 375 | }; |
| 330 | 376 | } |
| 331 | 377 | |
| 332 | | /// Generate a constant representing `val`. |
| 333 | | /// TODO: Deduplication? |
| 334 | | fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!IdRef { |
| 335 | | if (ty.zigTypeTag() == .Fn) { |
| 336 | | const fn_decl_index = switch (val.tag()) { |
| 337 | | .extern_fn => val.castTag(.extern_fn).?.data.owner_decl, |
| 338 | | .function => val.castTag(.function).?.data.owner_decl, |
| 378 | fn genConstInt(self: *DeclGen, ty_ref: SpvType.Ref, result_id: IdRef, value: anytype) !void { |
| 379 | const ty = self.spv.typeRefType(ty_ref); |
| 380 | const ty_id = self.typeId(ty_ref); |
| 381 | |
| 382 | const Lit = spec.LiteralContextDependentNumber; |
| 383 | const literal = switch (ty.intSignedness()) { |
| 384 | .signed => switch (ty.intFloatBits()) { |
| 385 | 1...32 => Lit{ .int32 = @intCast(i32, value) }, |
| 386 | 33...64 => Lit{ .int64 = @intCast(i64, value) }, |
| 387 | else => unreachable, // TODO: composite integer literals |
| 388 | }, |
| 389 | .unsigned => switch (ty.intFloatBits()) { |
| 390 | 1...32 => Lit{ .uint32 = @intCast(u32, value) }, |
| 391 | 33...64 => Lit{ .uint64 = @intCast(u64, value) }, |
| 339 | 392 | else => unreachable, |
| 393 | }, |
| 394 | }; |
| 395 | |
| 396 | try self.spv.emitConstant(ty_id, result_id, literal); |
| 397 | } |
| 398 | |
| 399 | fn constInt(self: *DeclGen, ty_ref: SpvType.Ref, value: anytype) !IdRef { |
| 400 | const result_id = self.spv.allocId(); |
| 401 | try self.genConstInt(ty_ref, result_id, value); |
| 402 | return result_id; |
| 403 | } |
| 404 | |
| 405 | fn genUndef(self: *DeclGen, ty_ref: SpvType.Ref) Error!IdRef { |
| 406 | const result_id = self.spv.allocId(); |
| 407 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = self.typeId(ty_ref), .id_result = result_id }); |
| 408 | return result_id; |
| 409 | } |
| 410 | |
| 411 | const IndirectConstantLowering = struct { |
| 412 | const undef = 0xAA; |
| 413 | |
| 414 | dg: *DeclGen, |
| 415 | /// Cached reference of the u32 type. |
| 416 | u32_ty_ref: SpvType.Ref, |
| 417 | /// Cached type id of the u32 type. |
| 418 | u32_ty_id: IdRef, |
| 419 | /// The members of the resulting structure type |
| 420 | members: std.ArrayList(SpvType.Payload.Struct.Member), |
| 421 | /// The initializers of each of the members. |
| 422 | initializers: std.ArrayList(IdRef), |
| 423 | /// The current size of the structure. Includes |
| 424 | /// the bytes in partial_word. |
| 425 | size: u32 = 0, |
| 426 | /// The partially filled last constant. |
| 427 | /// If full, its flushed. |
| 428 | partial_word: std.BoundedArray(u8, @sizeOf(Word)) = .{}, |
| 429 | /// The declaration dependencies of the constant we are lowering. |
| 430 | decl_deps: std.ArrayList(SpvModule.Decl.Index), |
| 431 | |
| 432 | /// Utility function to get the section that instructions should be lowered to. |
| 433 | fn section(self: *@This()) *SpvSection { |
| 434 | return &self.dg.spv.globals.section; |
| 435 | } |
| 436 | |
| 437 | /// Flush the partial_word to the members. If the partial_word is not |
| 438 | /// filled, this adds padding bytes (which are undefined). |
| 439 | fn flush(self: *@This()) !void { |
| 440 | if (self.partial_word.len == 0) { |
| 441 | // No need to add it there. |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | for (self.partial_word.unusedCapacitySlice()) |*unused| { |
| 446 | // TODO: Perhaps we should generate OpUndef for these bytes? |
| 447 | unused.* = undef; |
| 448 | } |
| 449 | |
| 450 | const word = @bitCast(Word, self.partial_word.buffer); |
| 451 | const result_id = self.dg.spv.allocId(); |
| 452 | // TODO: Integrate with caching mechanism |
| 453 | try self.dg.spv.emitConstant(self.u32_ty_id, result_id, .{ .uint32 = word }); |
| 454 | try self.members.append(.{ .ty = self.u32_ty_ref }); |
| 455 | try self.initializers.append(result_id); |
| 456 | |
| 457 | self.partial_word.len = 0; |
| 458 | self.size = std.mem.alignForwardGeneric(u32, self.size, @sizeOf(Word)); |
| 459 | } |
| 460 | |
| 461 | /// Fill the buffer with undefined values until the size is aligned to `align`. |
| 462 | fn fillToAlign(self: *@This(), alignment: u32) !void { |
| 463 | const target_size = std.mem.alignForwardGeneric(u32, self.size, alignment); |
| 464 | try self.addUndef(target_size - self.size); |
| 465 | } |
| 466 | |
| 467 | fn addUndef(self: *@This(), amt: u64) !void { |
| 468 | for (0..@intCast(usize, amt)) |_| { |
| 469 | try self.addByte(undef); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /// Add a single byte of data to the constant. |
| 474 | fn addByte(self: *@This(), data: u8) !void { |
| 475 | self.partial_word.append(data) catch { |
| 476 | try self.flush(); |
| 477 | self.partial_word.append(data) catch unreachable; |
| 340 | 478 | }; |
| 341 | | const decl = self.module.declPtr(fn_decl_index); |
| 342 | | self.module.markDeclAlive(decl); |
| 343 | | return self.ids.get(fn_decl_index).?.toRef(); |
| 479 | self.size += 1; |
| 480 | } |
| 481 | |
| 482 | /// Add many bytes of data to the constnat. |
| 483 | fn addBytes(self: *@This(), data: []const u8) !void { |
| 484 | // TODO: Improve performance by adding in bulk, or something? |
| 485 | for (data) |byte| { |
| 486 | try self.addByte(byte); |
| 487 | } |
| 344 | 488 | } |
| 345 | 489 | |
| 490 | fn addPtr(self: *@This(), ptr_ty_ref: SpvType.Ref, ptr_id: IdRef) !void { |
| 491 | // TODO: Double check pointer sizes here. |
| 492 | // shared pointers might be u32... |
| 493 | const target = self.dg.getTarget(); |
| 494 | const width = @divExact(target.cpu.arch.ptrBitWidth(), 8); |
| 495 | if (self.size % width != 0) { |
| 496 | return self.dg.todo("misaligned pointer constants", .{}); |
| 497 | } |
| 498 | try self.members.append(.{ .ty = ptr_ty_ref }); |
| 499 | try self.initializers.append(ptr_id); |
| 500 | self.size += width; |
| 501 | } |
| 502 | |
| 503 | fn addNullPtr(self: *@This(), ptr_ty_ref: SpvType.Ref) !void { |
| 504 | const result_id = self.dg.spv.allocId(); |
| 505 | try self.dg.spv.sections.types_globals_constants.emit(self.dg.spv.gpa, .OpConstantNull, .{ |
| 506 | .id_result_type = self.dg.typeId(ptr_ty_ref), |
| 507 | .id_result = result_id, |
| 508 | }); |
| 509 | try self.addPtr(ptr_ty_ref, result_id); |
| 510 | } |
| 511 | |
| 512 | fn addConstInt(self: *@This(), comptime T: type, value: T) !void { |
| 513 | if (@bitSizeOf(T) % 8 != 0) { |
| 514 | @compileError("todo: non byte aligned int constants"); |
| 515 | } |
| 516 | |
| 517 | // TODO: Swap endianness if the compiler is big endian. |
| 518 | try self.addBytes(std.mem.asBytes(&value)); |
| 519 | } |
| 520 | |
| 521 | fn addConstBool(self: *@This(), value: bool) !void { |
| 522 | try self.addByte(@boolToInt(value)); // TODO: Keep in sync with something? |
| 523 | } |
| 524 | |
| 525 | fn addInt(self: *@This(), ty: Type, val: Value) !void { |
| 526 | const target = self.dg.getTarget(); |
| 527 | const int_info = ty.intInfo(target); |
| 528 | const int_bits = switch (int_info.signedness) { |
| 529 | .signed => @bitCast(u64, val.toSignedInt(target)), |
| 530 | .unsigned => val.toUnsignedInt(target), |
| 531 | }; |
| 532 | |
| 533 | // TODO: Swap endianess if the compiler is big endian. |
| 534 | const len = ty.abiSize(target); |
| 535 | try self.addBytes(std.mem.asBytes(&int_bits)[0..@intCast(usize, len)]); |
| 536 | } |
| 537 | |
| 538 | fn addDeclRef(self: *@This(), ty: Type, decl_index: Decl.Index) !void { |
| 539 | const dg = self.dg; |
| 540 | |
| 541 | const ty_ref = try self.dg.resolveType(ty, .indirect); |
| 542 | const ty_id = dg.typeId(ty_ref); |
| 543 | |
| 544 | const decl = dg.module.declPtr(decl_index); |
| 545 | const spv_decl_index = try dg.resolveDecl(decl_index); |
| 546 | |
| 547 | switch (decl.val.tag()) { |
| 548 | .function => { |
| 549 | // TODO: Properly lower function pointers. For now we are going to hack around it and |
| 550 | // just generate an empty pointer. Function pointers are represented by usize for now, |
| 551 | // though. |
| 552 | try self.addInt(Type.usize, Value.initTag(.zero)); |
| 553 | return; |
| 554 | }, |
| 555 | .extern_fn => unreachable, // TODO |
| 556 | else => { |
| 557 | const result_id = dg.spv.allocId(); |
| 558 | log.debug("addDeclRef {s} = {}", .{ decl.name, result_id.id }); |
| 559 | |
| 560 | try self.decl_deps.append(spv_decl_index); |
| 561 | |
| 562 | const decl_id = dg.spv.declPtr(spv_decl_index).result_id; |
| 563 | // TODO: Do we need a storage class cast here? |
| 564 | // TODO: We can probably eliminate these casts |
| 565 | try dg.spv.globals.section.emitSpecConstantOp(dg.spv.gpa, .OpBitcast, .{ |
| 566 | .id_result_type = ty_id, |
| 567 | .id_result = result_id, |
| 568 | .operand = decl_id, |
| 569 | }); |
| 570 | |
| 571 | try self.addPtr(ty_ref, result_id); |
| 572 | }, |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | fn lower(self: *@This(), ty: Type, val: Value) !void { |
| 577 | const target = self.dg.getTarget(); |
| 578 | const dg = self.dg; |
| 579 | |
| 580 | if (val.isUndef()) { |
| 581 | const size = ty.abiSize(target); |
| 582 | return try self.addUndef(size); |
| 583 | } |
| 584 | |
| 585 | switch (ty.zigTypeTag()) { |
| 586 | .Int => try self.addInt(ty, val), |
| 587 | .Bool => try self.addConstBool(val.toBool()), |
| 588 | .Array => switch (val.tag()) { |
| 589 | .aggregate => { |
| 590 | const elem_vals = val.castTag(.aggregate).?.data; |
| 591 | const elem_ty = ty.elemType(); |
| 592 | const len = @intCast(u32, ty.arrayLenIncludingSentinel()); // TODO: limit spir-v to 32 bit arrays in a more elegant way. |
| 593 | for (elem_vals[0..len]) |elem_val| { |
| 594 | try self.lower(elem_ty, elem_val); |
| 595 | } |
| 596 | }, |
| 597 | .repeated => { |
| 598 | const elem_val = val.castTag(.repeated).?.data; |
| 599 | const elem_ty = ty.elemType(); |
| 600 | const len = @intCast(u32, ty.arrayLen()); |
| 601 | for (0..len) |_| { |
| 602 | try self.lower(elem_ty, elem_val); |
| 603 | } |
| 604 | if (ty.sentinel()) |sentinel| { |
| 605 | try self.lower(elem_ty, sentinel); |
| 606 | } |
| 607 | }, |
| 608 | .str_lit => { |
| 609 | const str_lit = val.castTag(.str_lit).?.data; |
| 610 | const bytes = dg.module.string_literal_bytes.items[str_lit.index..][0..str_lit.len]; |
| 611 | try self.addBytes(bytes); |
| 612 | if (ty.sentinel()) |sentinel| { |
| 613 | try self.addByte(@intCast(u8, sentinel.toUnsignedInt(target))); |
| 614 | } |
| 615 | }, |
| 616 | .bytes => { |
| 617 | const bytes = val.castTag(.bytes).?.data; |
| 618 | try self.addBytes(bytes); |
| 619 | }, |
| 620 | else => |tag| return dg.todo("indirect array constant with tag {s}", .{@tagName(tag)}), |
| 621 | }, |
| 622 | .Pointer => switch (val.tag()) { |
| 623 | .decl_ref_mut => { |
| 624 | const decl_index = val.castTag(.decl_ref_mut).?.data.decl_index; |
| 625 | try self.addDeclRef(ty, decl_index); |
| 626 | }, |
| 627 | .decl_ref => { |
| 628 | const decl_index = val.castTag(.decl_ref).?.data; |
| 629 | try self.addDeclRef(ty, decl_index); |
| 630 | }, |
| 631 | .slice => { |
| 632 | const slice = val.castTag(.slice).?.data; |
| 633 | |
| 634 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 635 | const ptr_ty = ty.slicePtrFieldType(&buf); |
| 636 | |
| 637 | try self.lower(ptr_ty, slice.ptr); |
| 638 | try self.addInt(Type.usize, slice.len); |
| 639 | }, |
| 640 | else => |tag| return dg.todo("pointer value of type {s}", .{@tagName(tag)}), |
| 641 | }, |
| 642 | .Struct => { |
| 643 | if (ty.isSimpleTupleOrAnonStruct()) { |
| 644 | unreachable; // TODO |
| 645 | } else { |
| 646 | const struct_ty = ty.castTag(.@"struct").?.data; |
| 647 | |
| 648 | if (struct_ty.layout == .Packed) { |
| 649 | return dg.todo("packed struct constants", .{}); |
| 650 | } |
| 651 | |
| 652 | const struct_begin = self.size; |
| 653 | const field_vals = val.castTag(.aggregate).?.data; |
| 654 | for (struct_ty.fields.values(), 0..) |field, i| { |
| 655 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; |
| 656 | try self.lower(field.ty, field_vals[i]); |
| 657 | |
| 658 | // Add padding if required. |
| 659 | // TODO: Add to type generation as well? |
| 660 | const unpadded_field_end = self.size - struct_begin; |
| 661 | const padded_field_end = ty.structFieldOffset(i + 1, target); |
| 662 | const padding = padded_field_end - unpadded_field_end; |
| 663 | try self.addUndef(padding); |
| 664 | } |
| 665 | } |
| 666 | }, |
| 667 | .Optional => { |
| 668 | var opt_buf: Type.Payload.ElemType = undefined; |
| 669 | const payload_ty = ty.optionalChild(&opt_buf); |
| 670 | const has_payload = !val.isNull(); |
| 671 | const abi_size = ty.abiSize(target); |
| 672 | |
| 673 | if (!payload_ty.hasRuntimeBits()) { |
| 674 | try self.addConstBool(has_payload); |
| 675 | return; |
| 676 | } else if (ty.optionalReprIsPayload()) { |
| 677 | // Optional representation is a nullable pointer. |
| 678 | if (val.castTag(.opt_payload)) |payload| { |
| 679 | try self.lower(payload_ty, payload.data); |
| 680 | } else if (has_payload) { |
| 681 | try self.lower(payload_ty, val); |
| 682 | } else { |
| 683 | const ptr_ty_ref = try dg.resolveType(ty, .indirect); |
| 684 | try self.addNullPtr(ptr_ty_ref); |
| 685 | } |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | // Optional representation is a structure. |
| 690 | // { Payload, Bool } |
| 691 | |
| 692 | // Subtract 1 for @sizeOf(bool). |
| 693 | // TODO: Make this not hardcoded. |
| 694 | const payload_size = payload_ty.abiSize(target); |
| 695 | const padding = abi_size - payload_size - 1; |
| 696 | |
| 697 | if (val.castTag(.opt_payload)) |payload| { |
| 698 | try self.lower(payload_ty, payload.data); |
| 699 | } else { |
| 700 | try self.addUndef(payload_size); |
| 701 | } |
| 702 | try self.addConstBool(has_payload); |
| 703 | try self.addUndef(padding); |
| 704 | }, |
| 705 | .Enum => { |
| 706 | var int_val_buffer: Value.Payload.U64 = undefined; |
| 707 | const int_val = val.enumToInt(ty, &int_val_buffer); |
| 708 | |
| 709 | var int_ty_buffer: Type.Payload.Bits = undefined; |
| 710 | const int_ty = ty.intTagType(&int_ty_buffer); |
| 711 | |
| 712 | try self.lower(int_ty, int_val); |
| 713 | }, |
| 714 | .Union => { |
| 715 | const tag_and_val = val.castTag(.@"union").?.data; |
| 716 | const layout = ty.unionGetLayout(target); |
| 717 | |
| 718 | if (layout.payload_size == 0) { |
| 719 | return try self.lower(ty.unionTagTypeSafety().?, tag_and_val.tag); |
| 720 | } |
| 721 | |
| 722 | const union_ty = ty.cast(Type.Payload.Union).?.data; |
| 723 | if (union_ty.layout == .Packed) { |
| 724 | return dg.todo("packed union constants", .{}); |
| 725 | } |
| 726 | |
| 727 | const active_field = ty.unionTagFieldIndex(tag_and_val.tag, dg.module).?; |
| 728 | const active_field_ty = union_ty.fields.values()[active_field].ty; |
| 729 | |
| 730 | const has_tag = layout.tag_size != 0; |
| 731 | const tag_first = layout.tag_align >= layout.payload_align; |
| 732 | |
| 733 | if (has_tag and tag_first) { |
| 734 | try self.lower(ty.unionTagTypeSafety().?, tag_and_val.tag); |
| 735 | } |
| 736 | |
| 737 | const active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime()) blk: { |
| 738 | try self.lower(active_field_ty, tag_and_val.val); |
| 739 | break :blk active_field_ty.abiSize(target); |
| 740 | } else 0; |
| 741 | |
| 742 | const payload_padding_len = layout.payload_size - active_field_size; |
| 743 | try self.addUndef(payload_padding_len); |
| 744 | |
| 745 | if (has_tag and !tag_first) { |
| 746 | try self.lower(ty.unionTagTypeSafety().?, tag_and_val.tag); |
| 747 | } |
| 748 | |
| 749 | try self.addUndef(layout.padding); |
| 750 | }, |
| 751 | .ErrorSet => switch (val.tag()) { |
| 752 | .@"error" => { |
| 753 | const err_name = val.castTag(.@"error").?.data.name; |
| 754 | const kv = try dg.module.getErrorValue(err_name); |
| 755 | try self.addConstInt(u16, @intCast(u16, kv.value)); |
| 756 | }, |
| 757 | .zero => { |
| 758 | // Unactivated error set. |
| 759 | try self.addConstInt(u16, 0); |
| 760 | }, |
| 761 | else => unreachable, |
| 762 | }, |
| 763 | .ErrorUnion => { |
| 764 | const payload_ty = ty.errorUnionPayload(); |
| 765 | const is_pl = val.errorUnionIsPayload(); |
| 766 | const error_val = if (!is_pl) val else Value.initTag(.zero); |
| 767 | |
| 768 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 769 | return try self.lower(Type.anyerror, error_val); |
| 770 | } |
| 771 | |
| 772 | const payload_align = payload_ty.abiAlignment(target); |
| 773 | const error_align = Type.anyerror.abiAlignment(target); |
| 774 | |
| 775 | const payload_size = payload_ty.abiSize(target); |
| 776 | const error_size = Type.anyerror.abiAlignment(target); |
| 777 | const ty_size = ty.abiSize(target); |
| 778 | const padding = ty_size - payload_size - error_size; |
| 779 | |
| 780 | const payload_val = if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef); |
| 781 | |
| 782 | if (error_align > payload_align) { |
| 783 | try self.lower(Type.anyerror, error_val); |
| 784 | try self.lower(payload_ty, payload_val); |
| 785 | } else { |
| 786 | try self.lower(payload_ty, payload_val); |
| 787 | try self.lower(Type.anyerror, error_val); |
| 788 | } |
| 789 | |
| 790 | try self.addUndef(padding); |
| 791 | }, |
| 792 | else => |tag| return dg.todo("indirect constant of type {s}", .{@tagName(tag)}), |
| 793 | } |
| 794 | } |
| 795 | }; |
| 796 | |
| 797 | /// Returns a pointer to `val`. The value is placed directly |
| 798 | /// into the storage class `storage_class`, and this is also where the resulting |
| 799 | /// pointer points to. Note: result is not necessarily an OpVariable instruction! |
| 800 | fn lowerIndirectConstant( |
| 801 | self: *DeclGen, |
| 802 | spv_decl_index: SpvModule.Decl.Index, |
| 803 | ty: Type, |
| 804 | val: Value, |
| 805 | storage_class: StorageClass, |
| 806 | cast_to_generic: bool, |
| 807 | alignment: u32, |
| 808 | ) Error!void { |
| 809 | // To simplify constant generation, we're going to generate constants as a word-array, and |
| 810 | // pointer cast the result to the right type. |
| 811 | // This means that the final constant will be generated as follows: |
| 812 | // %T = OpTypeStruct %members... |
| 813 | // %P = OpTypePointer %T |
| 814 | // %U = OpTypePointer %ty |
| 815 | // %1 = OpConstantComposite %T %initializers... |
| 816 | // %2 = OpVariable %P %1 |
| 817 | // %result_id = OpSpecConstantOp OpBitcast %U %2 |
| 818 | // |
| 819 | // The members consist of two options: |
| 820 | // - Literal values: ints, strings, etc. These are generated as u32 words. |
| 821 | // - Relocations, such as pointers: These are generated by embedding the pointer into the |
| 822 | // to-be-generated structure. There are two options here, depending on the alignment of the |
| 823 | // pointer value itself (not the alignment of the pointee). |
| 824 | // - Natively or over-aligned values. These can just be generated directly. |
| 825 | // - Underaligned pointers. These need to be packed into the word array by using a mixture of |
| 826 | // OpSpecConstantOp instructions such as OpConvertPtrToU, OpBitcast, OpShift, etc. |
| 827 | |
| 828 | // TODO: Implement alignment here. |
| 829 | // This is hoing to require some hacks because there is no real way to |
| 830 | // set an OpVariable's alignment. |
| 831 | _ = alignment; |
| 832 | |
| 833 | assert(storage_class != .Generic and storage_class != .Function); |
| 834 | |
| 835 | log.debug("lowerIndirectConstant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtDebug() }); |
| 836 | |
| 837 | const section = &self.spv.globals.section; |
| 838 | |
| 839 | const ty_ref = try self.resolveType(ty, .indirect); |
| 840 | const ptr_ty_ref = try self.spv.ptrType(ty_ref, storage_class, 0); |
| 841 | |
| 842 | // const target = self.getTarget(); |
| 843 | |
| 844 | // TODO: Fix the resulting global linking for these paths. |
| 845 | // if (val.isUndef()) { |
| 846 | // // Special case: the entire value is undefined. In this case, we can just |
| 847 | // // generate an OpVariable with no initializer. |
| 848 | // return try section.emit(self.spv.gpa, .OpVariable, .{ |
| 849 | // .id_result_type = self.typeId(ptr_ty_ref), |
| 850 | // .id_result = result_id, |
| 851 | // .storage_class = storage_class, |
| 852 | // }); |
| 853 | // } else if (ty.abiSize(target) == 0) { |
| 854 | // // Special case: if the type has no size, then return an undefined pointer. |
| 855 | // return try section.emit(self.spv.gpa, .OpUndef, .{ |
| 856 | // .id_result_type = self.typeId(ptr_ty_ref), |
| 857 | // .id_result = result_id, |
| 858 | // }); |
| 859 | // } |
| 860 | |
| 861 | // TODO: Capture the above stuff in here as well... |
| 862 | const begin_inst = self.spv.beginGlobal(); |
| 863 | |
| 864 | const u32_ty_ref = try self.intType(.unsigned, 32); |
| 865 | var icl = IndirectConstantLowering{ |
| 866 | .dg = self, |
| 867 | .u32_ty_ref = u32_ty_ref, |
| 868 | .u32_ty_id = self.typeId(u32_ty_ref), |
| 869 | .members = std.ArrayList(SpvType.Payload.Struct.Member).init(self.gpa), |
| 870 | .initializers = std.ArrayList(IdRef).init(self.gpa), |
| 871 | .decl_deps = std.ArrayList(SpvModule.Decl.Index).init(self.gpa), |
| 872 | }; |
| 873 | |
| 874 | defer icl.members.deinit(); |
| 875 | defer icl.initializers.deinit(); |
| 876 | defer icl.decl_deps.deinit(); |
| 877 | |
| 878 | try icl.lower(ty, val); |
| 879 | try icl.flush(); |
| 880 | |
| 881 | const constant_struct_ty_ref = try self.spv.simpleStructType(icl.members.items); |
| 882 | const ptr_constant_struct_ty_ref = try self.spv.ptrType(constant_struct_ty_ref, storage_class, 0); |
| 883 | |
| 884 | const constant_struct_id = self.spv.allocId(); |
| 885 | try section.emit(self.spv.gpa, .OpSpecConstantComposite, .{ |
| 886 | .id_result_type = self.typeId(constant_struct_ty_ref), |
| 887 | .id_result = constant_struct_id, |
| 888 | .constituents = icl.initializers.items, |
| 889 | }); |
| 890 | |
| 891 | const var_id = self.spv.allocId(); |
| 892 | self.spv.globalPtr(spv_decl_index).?.result_id = var_id; |
| 893 | try section.emit(self.spv.gpa, .OpVariable, .{ |
| 894 | .id_result_type = self.typeId(ptr_constant_struct_ty_ref), |
| 895 | .id_result = var_id, |
| 896 | .storage_class = storage_class, |
| 897 | .initializer = constant_struct_id, |
| 898 | }); |
| 899 | // TODO: Set alignment of OpVariable. |
| 900 | // TODO: We may be able to eliminate these casts. |
| 901 | |
| 902 | const const_ptr_id = try self.makePointerConstant(section, ptr_constant_struct_ty_ref, var_id); |
| 903 | const result_id = self.spv.declPtr(spv_decl_index).result_id; |
| 904 | |
| 905 | const bitcast_result_id = if (cast_to_generic) |
| 906 | self.spv.allocId() |
| 907 | else |
| 908 | result_id; |
| 909 | |
| 910 | try section.emitSpecConstantOp(self.spv.gpa, .OpBitcast, .{ |
| 911 | .id_result_type = self.typeId(ptr_ty_ref), |
| 912 | .id_result = bitcast_result_id, |
| 913 | .operand = const_ptr_id, |
| 914 | }); |
| 915 | |
| 916 | if (cast_to_generic) { |
| 917 | const generic_ptr_ty_ref = try self.spv.ptrType(ty_ref, .Generic, 0); |
| 918 | try section.emitSpecConstantOp(self.spv.gpa, .OpPtrCastToGeneric, .{ |
| 919 | .id_result_type = self.typeId(generic_ptr_ty_ref), |
| 920 | .id_result = result_id, |
| 921 | .pointer = bitcast_result_id, |
| 922 | }); |
| 923 | } |
| 924 | |
| 925 | try self.spv.declareDeclDeps(spv_decl_index, icl.decl_deps.items); |
| 926 | self.spv.endGlobal(spv_decl_index, begin_inst); |
| 927 | } |
| 928 | |
| 929 | /// This function generates a load for a constant in direct (ie, non-memory) representation. |
| 930 | /// When the constant is simple, it can be generated directly using OpConstant instructions. When |
| 931 | /// the constant is more complicated however, it needs to be lowered to an indirect constant, which |
| 932 | /// is then loaded using OpLoad. Such values are loaded into the UniformConstant storage class by default. |
| 933 | /// This function should only be called during function code generation. |
| 934 | fn constant(self: *DeclGen, ty: Type, val: Value) !IdRef { |
| 346 | 935 | const target = self.getTarget(); |
| 347 | 936 | const section = &self.spv.sections.types_globals_constants; |
| 937 | const result_ty_ref = try self.resolveType(ty, .direct); |
| 938 | const result_ty_id = self.typeId(result_ty_ref); |
| 348 | 939 | const result_id = self.spv.allocId(); |
| 349 | | const result_type_id = try self.resolveTypeId(ty); |
| 350 | 940 | |
| 351 | 941 | if (val.isUndef()) { |
| 352 | | try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_type_id, .id_result = result_id }); |
| 353 | | return result_id.toRef(); |
| 942 | try section.emit(self.spv.gpa, .OpUndef, .{ |
| 943 | .id_result_type = result_ty_id, |
| 944 | .id_result = result_id, |
| 945 | }); |
| 946 | return result_id; |
| 354 | 947 | } |
| 355 | 948 | |
| 356 | 949 | switch (ty.zigTypeTag()) { |
| 357 | 950 | .Int => { |
| 358 | | const int_info = ty.intInfo(target); |
| 359 | | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 360 | | // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits. |
| 361 | | return self.todo("implement composite int constants for {}", .{ty.fmtDebug()}); |
| 362 | | }; |
| 363 | | |
| 364 | | // We can just use toSignedInt/toUnsignedInt here as it returns u64 - a type large enough to hold any |
| 365 | | // SPIR-V native type (up to i/u64 with Int64). If SPIR-V ever supports native ints of a larger size, this |
| 366 | | // might need to be updated. |
| 367 | | assert(self.largestSupportedIntBits() <= @bitSizeOf(u64)); |
| 368 | | |
| 369 | | // Note, value is required to be sign-extended, so we don't need to mask off the upper bits. |
| 370 | | // See https://www.khronos.org/registry/SPIR-V/specs/unified1/SPIRV.html#Literal |
| 371 | | var int_bits = if (ty.isSignedInt()) @bitCast(u64, val.toSignedInt(target)) else val.toUnsignedInt(target); |
| 372 | | |
| 373 | | const value: spec.LiteralContextDependentNumber = switch (backing_bits) { |
| 374 | | 1...32 => .{ .uint32 = @truncate(u32, int_bits) }, |
| 375 | | 33...64 => .{ .uint64 = int_bits }, |
| 376 | | else => unreachable, |
| 377 | | }; |
| 378 | | |
| 379 | | try section.emit(self.spv.gpa, .OpConstant, .{ |
| 380 | | .id_result_type = result_type_id, |
| 381 | | .id_result = result_id, |
| 382 | | .value = value, |
| 383 | | }); |
| 951 | const int_bits = if (ty.isSignedInt()) |
| 952 | @bitCast(u64, val.toSignedInt(target)) |
| 953 | else |
| 954 | val.toUnsignedInt(target); |
| 955 | try self.genConstInt(result_ty_ref, result_id, int_bits); |
| 384 | 956 | }, |
| 385 | 957 | .Bool => { |
| 386 | | const operands = .{ .id_result_type = result_type_id, .id_result = result_id }; |
| 958 | const operands = .{ .id_result_type = result_ty_id, .id_result = result_id }; |
| 387 | 959 | if (val.toBool()) { |
| 388 | 960 | try section.emit(self.spv.gpa, .OpConstantTrue, operands); |
| 389 | 961 | } else { |
| 390 | 962 | try section.emit(self.spv.gpa, .OpConstantFalse, operands); |
| 391 | 963 | } |
| 392 | 964 | }, |
| 393 | | .Float => { |
| 394 | | // At this point we are guaranteed that the target floating point type is supported, otherwise the function |
| 395 | | // would have exited at resolveTypeId(ty). |
| 396 | | |
| 397 | | const value: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { |
| 398 | | // Prevent upcasting to f32 by bitcasting and writing as a uint32. |
| 399 | | 16 => .{ .uint32 = @bitCast(u16, val.toFloat(f16)) }, |
| 400 | | 32 => .{ .float32 = val.toFloat(f32) }, |
| 401 | | 64 => .{ .float64 = val.toFloat(f64) }, |
| 402 | | 128 => unreachable, // Filtered out in the call to resolveTypeId. |
| 403 | | // TODO: Insert case for long double when the layout for that is determined? |
| 404 | | else => unreachable, |
| 405 | | }; |
| 406 | | |
| 407 | | try section.emit(self.spv.gpa, .OpConstant, .{ |
| 408 | | .id_result_type = result_type_id, |
| 965 | // TODO: We can handle most pointers here (decl refs etc), because now they emit an extra |
| 966 | // OpVariable that is not really required. |
| 967 | else => { |
| 968 | // The value cannot be generated directly, so generate it as an indirect constant, |
| 969 | // and then perform an OpLoad. |
| 970 | const alignment = ty.abiAlignment(target); |
| 971 | const spv_decl_index = try self.spv.allocDecl(.global); |
| 972 | |
| 973 | try self.lowerIndirectConstant( |
| 974 | spv_decl_index, |
| 975 | ty, |
| 976 | val, |
| 977 | .UniformConstant, |
| 978 | false, |
| 979 | alignment, |
| 980 | ); |
| 981 | try self.func.decl_deps.append(self.spv.gpa, spv_decl_index); |
| 982 | |
| 983 | try self.func.body.emit(self.spv.gpa, .OpLoad, .{ |
| 984 | .id_result_type = result_ty_id, |
| 409 | 985 | .id_result = result_id, |
| 410 | | .value = value, |
| 986 | .pointer = self.spv.declPtr(spv_decl_index).result_id, |
| 411 | 987 | }); |
| 988 | // TODO: Convert bools? This logic should hook into `load`. It should be a dead |
| 989 | // path though considering .Bool is handled above. |
| 412 | 990 | }, |
| 413 | | .Vector => switch (val.tag()) { |
| 414 | | .aggregate => { |
| 415 | | const elem_vals = val.castTag(.aggregate).?.data; |
| 416 | | const vector_len = @intCast(usize, ty.vectorLen()); |
| 417 | | const elem_ty = ty.elemType(); |
| 418 | | |
| 419 | | const elem_refs = try self.gpa.alloc(IdRef, vector_len); |
| 420 | | defer self.gpa.free(elem_refs); |
| 421 | | for (elem_refs, 0..) |*elem, i| { |
| 422 | | elem.* = try self.genConstant(elem_ty, elem_vals[i]); |
| 423 | | } |
| 424 | | try section.emit(self.spv.gpa, .OpConstantComposite, .{ |
| 425 | | .id_result_type = result_type_id, |
| 426 | | .id_result = result_id, |
| 427 | | .constituents = elem_refs, |
| 428 | | }); |
| 429 | | }, |
| 430 | | else => unreachable, // TODO |
| 431 | | }, |
| 432 | | .Void => unreachable, |
| 433 | | .Fn => unreachable, |
| 434 | | else => return self.todo("constant generation of type {}", .{ty.fmtDebug()}), |
| 435 | 991 | } |
| 436 | 992 | |
| 437 | | return result_id.toRef(); |
| 993 | return result_id; |
| 438 | 994 | } |
| 439 | 995 | |
| 440 | 996 | /// Turn a Zig type into a SPIR-V Type, and return its type result-id. |
| 441 | 997 | fn resolveTypeId(self: *DeclGen, ty: Type) !IdResultType { |
| 442 | | const type_ref = try self.resolveType(ty); |
| 443 | | return self.spv.typeResultId(type_ref); |
| 998 | const type_ref = try self.resolveType(ty, .direct); |
| 999 | return self.typeId(type_ref); |
| 1000 | } |
| 1001 | |
| 1002 | fn typeId(self: *DeclGen, ty_ref: SpvType.Ref) IdRef { |
| 1003 | return self.spv.typeId(ty_ref); |
| 1004 | } |
| 1005 | |
| 1006 | /// Create an integer type suitable for storing at least 'bits' bits. |
| 1007 | fn intType(self: *DeclGen, signedness: std.builtin.Signedness, bits: u16) !SpvType.Ref { |
| 1008 | const backing_bits = self.backingIntBits(bits) orelse { |
| 1009 | // TODO: Integers too big for any native type are represented as "composite integers": |
| 1010 | // An array of largestSupportedIntBits. |
| 1011 | return self.todo("Implement {s} composite int type of {} bits", .{ @tagName(signedness), bits }); |
| 1012 | }; |
| 1013 | |
| 1014 | return try self.spv.resolveType(try SpvType.int(self.spv.arena, signedness, backing_bits)); |
| 1015 | } |
| 1016 | |
| 1017 | /// Create an integer type that represents 'usize'. |
| 1018 | fn sizeType(self: *DeclGen) !SpvType.Ref { |
| 1019 | return try self.intType(.unsigned, self.getTarget().cpu.arch.ptrBitWidth()); |
| 1020 | } |
| 1021 | |
| 1022 | /// Generate a union type, optionally with a known field. If the tag alignment is greater |
| 1023 | /// than that of the payload, a regular union (non-packed, with both tag and payload), will |
| 1024 | /// be generated as follows: |
| 1025 | /// If the active field is known: |
| 1026 | /// struct { |
| 1027 | /// tag: TagType, |
| 1028 | /// payload: ActivePayloadType, |
| 1029 | /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8, |
| 1030 | /// padding: [padding_size]u8, |
| 1031 | /// } |
| 1032 | /// If the payload alignment is greater than that of the tag: |
| 1033 | /// struct { |
| 1034 | /// payload: ActivePayloadType, |
| 1035 | /// payload_padding: [payload_size - @sizeOf(ActivePayloadType)]u8, |
| 1036 | /// tag: TagType, |
| 1037 | /// padding: [padding_size]u8, |
| 1038 | /// } |
| 1039 | /// If the active payload is unknown, it will default back to the most aligned field. This is |
| 1040 | /// to make sure that the overal struct has the correct alignment in spir-v. |
| 1041 | /// If any of the fields' size is 0, it will be omitted. |
| 1042 | /// NOTE: When the active field is set to something other than the most aligned field, the |
| 1043 | /// resulting struct will be *underaligned*. |
| 1044 | fn resolveUnionType(self: *DeclGen, ty: Type, maybe_active_field: ?usize) !SpvType.Ref { |
| 1045 | const target = self.getTarget(); |
| 1046 | const layout = ty.unionGetLayout(target); |
| 1047 | const union_ty = ty.cast(Type.Payload.Union).?.data; |
| 1048 | |
| 1049 | if (union_ty.layout == .Packed) { |
| 1050 | return self.todo("packed union types", .{}); |
| 1051 | } |
| 1052 | |
| 1053 | const tag_ty_ref = try self.resolveType(union_ty.tag_ty, .indirect); |
| 1054 | if (layout.payload_size == 0) { |
| 1055 | // No payload, so represent this as just the tag type. |
| 1056 | return tag_ty_ref; |
| 1057 | } |
| 1058 | |
| 1059 | var members = std.BoundedArray(SpvType.Payload.Struct.Member, 4){}; |
| 1060 | |
| 1061 | const has_tag = layout.tag_size != 0; |
| 1062 | const tag_first = layout.tag_align >= layout.payload_align; |
| 1063 | const tag_member = .{ .name = "tag", .ty = tag_ty_ref }; |
| 1064 | const u8_ty_ref = try self.intType(.unsigned, 8); // TODO: What if Int8Type is not enabled? |
| 1065 | |
| 1066 | if (has_tag and tag_first) { |
| 1067 | members.appendAssumeCapacity(tag_member); |
| 1068 | } |
| 1069 | |
| 1070 | const active_field = maybe_active_field orelse layout.most_aligned_field; |
| 1071 | const active_field_ty = union_ty.fields.values()[active_field].ty; |
| 1072 | |
| 1073 | const active_field_size = if (active_field_ty.hasRuntimeBitsIgnoreComptime()) blk: { |
| 1074 | const active_payload_ty_ref = try self.resolveType(active_field_ty, .indirect); |
| 1075 | members.appendAssumeCapacity(.{ .name = "payload", .ty = active_payload_ty_ref }); |
| 1076 | break :blk active_field_ty.abiSize(target); |
| 1077 | } else 0; |
| 1078 | |
| 1079 | const payload_padding_len = layout.payload_size - active_field_size; |
| 1080 | if (payload_padding_len != 0) { |
| 1081 | const payload_padding_ty_ref = try self.spv.arrayType(@intCast(u32, payload_padding_len), u8_ty_ref); |
| 1082 | members.appendAssumeCapacity(.{ .name = "padding_payload", .ty = payload_padding_ty_ref }); |
| 1083 | } |
| 1084 | |
| 1085 | if (has_tag and !tag_first) { |
| 1086 | members.appendAssumeCapacity(tag_member); |
| 1087 | } |
| 1088 | |
| 1089 | if (layout.padding != 0) { |
| 1090 | const padding_ty_ref = try self.spv.arrayType(layout.padding, u8_ty_ref); |
| 1091 | members.appendAssumeCapacity(.{ .name = "padding", .ty = padding_ty_ref }); |
| 1092 | } |
| 1093 | |
| 1094 | return try self.spv.simpleStructType(members.slice()); |
| 444 | 1095 | } |
| 445 | 1096 | |
| 446 | 1097 | /// Turn a Zig type into a SPIR-V Type, and return a reference to it. |
| 447 | | fn resolveType(self: *DeclGen, ty: Type) Error!SpvType.Ref { |
| 1098 | fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!SpvType.Ref { |
| 1099 | log.debug("resolveType: ty = {}", .{ty.fmt(self.module)}); |
| 448 | 1100 | const target = self.getTarget(); |
| 449 | | return switch (ty.zigTypeTag()) { |
| 450 | | .Void => try self.spv.resolveType(SpvType.initTag(.void)), |
| 451 | | .Bool => blk: { |
| 452 | | // TODO: SPIR-V booleans are opaque. For local variables this is fine, but for structs |
| 453 | | // members we want to use integer types instead. |
| 454 | | break :blk try self.spv.resolveType(SpvType.initTag(.bool)); |
| 1101 | switch (ty.zigTypeTag()) { |
| 1102 | .Void, .NoReturn => return try self.spv.resolveType(SpvType.initTag(.void)), |
| 1103 | .Bool => switch (repr) { |
| 1104 | .direct => return try self.spv.resolveType(SpvType.initTag(.bool)), |
| 1105 | // SPIR-V booleans are opaque, which is fine for operations, but they cant be stored. |
| 1106 | // This function returns the *stored* type, for values directly we convert this into a bool when |
| 1107 | // it is loaded, and convert it back to this type when stored. |
| 1108 | .indirect => return try self.intType(.unsigned, 1), |
| 455 | 1109 | }, |
| 456 | | .Int => blk: { |
| 1110 | .Int => { |
| 457 | 1111 | const int_info = ty.intInfo(target); |
| 458 | | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 459 | | // TODO: Integers too big for any native type are represented as "composite integers": |
| 460 | | // An array of largestSupportedIntBits. |
| 461 | | return self.todo("Implement composite int type {}", .{ty.fmtDebug()}); |
| 462 | | }; |
| 463 | | |
| 464 | | const payload = try self.spv.arena.create(SpvType.Payload.Int); |
| 465 | | payload.* = .{ |
| 466 | | .width = backing_bits, |
| 467 | | .signedness = int_info.signedness, |
| 468 | | }; |
| 469 | | break :blk try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1112 | return try self.intType(int_info.signedness, int_info.bits); |
| 1113 | }, |
| 1114 | .Enum => { |
| 1115 | var buffer: Type.Payload.Bits = undefined; |
| 1116 | const tag_ty = ty.intTagType(&buffer); |
| 1117 | return self.resolveType(tag_ty, repr); |
| 470 | 1118 | }, |
| 471 | | .Float => blk: { |
| 1119 | .Float => { |
| 472 | 1120 | // We can (and want) not really emulate floating points with other floating point types like with the integer types, |
| 473 | 1121 | // so if the float is not supported, just return an error. |
| 474 | 1122 | const bits = ty.floatBits(target); |
| ... | ... | @@ -484,43 +1132,58 @@ pub const DeclGen = struct { |
| 484 | 1132 | return self.fail("Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); |
| 485 | 1133 | } |
| 486 | 1134 | |
| 487 | | const payload = try self.spv.arena.create(SpvType.Payload.Float); |
| 488 | | payload.* = .{ |
| 489 | | .width = bits, |
| 1135 | return try self.spv.resolveType(SpvType.float(bits)); |
| 1136 | }, |
| 1137 | .Array => { |
| 1138 | const elem_ty = ty.childType(); |
| 1139 | const elem_ty_ref = try self.resolveType(elem_ty, .indirect); |
| 1140 | const total_len = std.math.cast(u32, ty.arrayLenIncludingSentinel()) orelse { |
| 1141 | return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel()}); |
| 490 | 1142 | }; |
| 491 | | break :blk try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1143 | return try self.spv.arrayType(total_len, elem_ty_ref); |
| 492 | 1144 | }, |
| 493 | | .Fn => blk: { |
| 494 | | // We only support C-calling-convention functions for now, no varargs. |
| 495 | | if (ty.fnCallingConvention() != .C) |
| 496 | | return self.fail("Unsupported calling convention for SPIR-V", .{}); |
| 497 | | if (ty.fnIsVarArgs()) |
| 498 | | return self.fail("VarArgs functions are unsupported for SPIR-V", .{}); |
| 499 | | |
| 500 | | const param_types = try self.spv.arena.alloc(SpvType.Ref, ty.fnParamLen()); |
| 501 | | for (param_types, 0..) |*param, i| { |
| 502 | | param.* = try self.resolveType(ty.fnParamType(i)); |
| 503 | | } |
| 1145 | .Fn => switch (repr) { |
| 1146 | .direct => { |
| 1147 | // TODO: Put this somewhere in Sema.zig |
| 1148 | if (ty.fnIsVarArgs()) |
| 1149 | return self.fail("VarArgs functions are unsupported for SPIR-V", .{}); |
| 1150 | |
| 1151 | // TODO: Parameter passing convention etc. |
| 1152 | |
| 1153 | const param_types = try self.spv.arena.alloc(SpvType.Ref, ty.fnParamLen()); |
| 1154 | for (param_types, 0..) |*param, i| { |
| 1155 | param.* = try self.resolveType(ty.fnParamType(i), .direct); |
| 1156 | } |
| 504 | 1157 | |
| 505 | | const return_type = try self.resolveType(ty.fnReturnType()); |
| 1158 | const return_type = try self.resolveType(ty.fnReturnType(), .direct); |
| 506 | 1159 | |
| 507 | | const payload = try self.spv.arena.create(SpvType.Payload.Function); |
| 508 | | payload.* = .{ .return_type = return_type, .parameters = param_types }; |
| 509 | | break :blk try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1160 | const payload = try self.spv.arena.create(SpvType.Payload.Function); |
| 1161 | payload.* = .{ .return_type = return_type, .parameters = param_types }; |
| 1162 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1163 | }, |
| 1164 | .indirect => { |
| 1165 | // TODO: Represent function pointers properly. |
| 1166 | // For now, just use an usize type. |
| 1167 | return try self.sizeType(); |
| 1168 | }, |
| 510 | 1169 | }, |
| 511 | | .Pointer => blk: { |
| 512 | | const payload = try self.spv.arena.create(SpvType.Payload.Pointer); |
| 513 | | payload.* = .{ |
| 514 | | .storage_class = spirvStorageClass(ty.ptrAddressSpace()), |
| 515 | | .child_type = try self.resolveType(ty.elemType()), |
| 516 | | .array_stride = 0, |
| 517 | | // Note: only available in Kernels! |
| 518 | | .alignment = null, |
| 519 | | .max_byte_offset = null, |
| 520 | | }; |
| 521 | | break :blk try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1170 | .Pointer => { |
| 1171 | const ptr_info = ty.ptrInfo().data; |
| 1172 | |
| 1173 | const storage_class = spvStorageClass(ptr_info.@"addrspace"); |
| 1174 | const child_ty_ref = try self.resolveType(ptr_info.pointee_type, .indirect); |
| 1175 | const ptr_ty_ref = try self.spv.ptrType(child_ty_ref, storage_class, 0); |
| 1176 | |
| 1177 | if (ptr_info.size != .Slice) { |
| 1178 | return ptr_ty_ref; |
| 1179 | } |
| 1180 | |
| 1181 | return try self.spv.simpleStructType(&.{ |
| 1182 | .{ .ty = ptr_ty_ref, .name = "ptr" }, |
| 1183 | .{ .ty = try self.sizeType(), .name = "len" }, |
| 1184 | }); |
| 522 | 1185 | }, |
| 523 | | .Vector => blk: { |
| 1186 | .Vector => { |
| 524 | 1187 | // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations |
| 525 | 1188 | // which work on them), so simply use those. |
| 526 | 1189 | // Note: SPIR-V vectors only support bools, ints and floats, so pointer vectors need to be supported another way. |
| ... | ... | @@ -532,10 +1195,112 @@ pub const DeclGen = struct { |
| 532 | 1195 | |
| 533 | 1196 | const payload = try self.spv.arena.create(SpvType.Payload.Vector); |
| 534 | 1197 | payload.* = .{ |
| 535 | | .component_type = try self.resolveType(ty.elemType()), |
| 1198 | .component_type = try self.resolveType(ty.elemType(), repr), |
| 536 | 1199 | .component_count = @intCast(u32, ty.vectorLen()), |
| 537 | 1200 | }; |
| 538 | | break :blk try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1201 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1202 | }, |
| 1203 | .Struct => { |
| 1204 | if (ty.isSimpleTupleOrAnonStruct()) { |
| 1205 | const tuple = ty.tupleFields(); |
| 1206 | const members = try self.spv.arena.alloc(SpvType.Payload.Struct.Member, tuple.types.len); |
| 1207 | var member_index: u32 = 0; |
| 1208 | for (tuple.types, 0..) |field_ty, i| { |
| 1209 | const field_val = tuple.values[i]; |
| 1210 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 1211 | members[member_index] = .{ |
| 1212 | .ty = try self.resolveType(field_ty, .indirect), |
| 1213 | }; |
| 1214 | member_index += 1; |
| 1215 | } |
| 1216 | const payload = try self.spv.arena.create(SpvType.Payload.Struct); |
| 1217 | payload.* = .{ |
| 1218 | .members = members[0..member_index], |
| 1219 | }; |
| 1220 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1221 | } |
| 1222 | |
| 1223 | const struct_ty = ty.castTag(.@"struct").?.data; |
| 1224 | |
| 1225 | if (struct_ty.layout == .Packed) { |
| 1226 | return try self.resolveType(struct_ty.backing_int_ty, .indirect); |
| 1227 | } |
| 1228 | |
| 1229 | const members = try self.spv.arena.alloc(SpvType.Payload.Struct.Member, struct_ty.fields.count()); |
| 1230 | var member_index: usize = 0; |
| 1231 | for (struct_ty.fields.values(), 0..) |field, i| { |
| 1232 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; |
| 1233 | |
| 1234 | members[member_index] = .{ |
| 1235 | .ty = try self.resolveType(field.ty, .indirect), |
| 1236 | .name = struct_ty.fields.keys()[i], |
| 1237 | }; |
| 1238 | member_index += 1; |
| 1239 | } |
| 1240 | |
| 1241 | const name = try struct_ty.getFullyQualifiedName(self.module); |
| 1242 | defer self.module.gpa.free(name); |
| 1243 | |
| 1244 | const payload = try self.spv.arena.create(SpvType.Payload.Struct); |
| 1245 | payload.* = .{ |
| 1246 | .members = members[0..member_index], |
| 1247 | .name = try self.spv.arena.dupe(u8, name), |
| 1248 | }; |
| 1249 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| 1250 | }, |
| 1251 | .Optional => { |
| 1252 | var buf: Type.Payload.ElemType = undefined; |
| 1253 | const payload_ty = ty.optionalChild(&buf); |
| 1254 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1255 | // Just use a bool. |
| 1256 | // Note: Always generate the bool with indirect format, to save on some sanity |
| 1257 | // Perform the converison to a direct bool when the field is extracted. |
| 1258 | return try self.resolveType(Type.bool, .indirect); |
| 1259 | } |
| 1260 | |
| 1261 | const payload_ty_ref = try self.resolveType(payload_ty, .indirect); |
| 1262 | if (ty.optionalReprIsPayload()) { |
| 1263 | // Optional is actually a pointer. |
| 1264 | return payload_ty_ref; |
| 1265 | } |
| 1266 | |
| 1267 | const bool_ty_ref = try self.resolveType(Type.bool, .indirect); |
| 1268 | |
| 1269 | // its an actual optional |
| 1270 | return try self.spv.simpleStructType(&.{ |
| 1271 | .{ .ty = payload_ty_ref, .name = "payload" }, |
| 1272 | .{ .ty = bool_ty_ref, .name = "valid" }, |
| 1273 | }); |
| 1274 | }, |
| 1275 | .Union => return try self.resolveUnionType(ty, null), |
| 1276 | .ErrorSet => return try self.intType(.unsigned, 16), |
| 1277 | .ErrorUnion => { |
| 1278 | const payload_ty = ty.errorUnionPayload(); |
| 1279 | const error_ty_ref = try self.resolveType(Type.anyerror, .indirect); |
| 1280 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1281 | return error_ty_ref; |
| 1282 | } |
| 1283 | |
| 1284 | const payload_ty_ref = try self.resolveType(payload_ty, .indirect); |
| 1285 | |
| 1286 | const payload_align = payload_ty.abiAlignment(target); |
| 1287 | const error_align = Type.anyerror.abiAlignment(target); |
| 1288 | |
| 1289 | var members = std.BoundedArray(SpvType.Payload.Struct.Member, 2){}; |
| 1290 | // Similar to unions, we're going to put the most aligned member first. |
| 1291 | if (error_align > payload_align) { |
| 1292 | // Put the error first |
| 1293 | members.appendAssumeCapacity(.{ .ty = error_ty_ref, .name = "error" }); |
| 1294 | members.appendAssumeCapacity(.{ .ty = payload_ty_ref, .name = "payload" }); |
| 1295 | // TODO: ABI padding? |
| 1296 | } else { |
| 1297 | // Put the payload first. |
| 1298 | members.appendAssumeCapacity(.{ .ty = payload_ty_ref, .name = "payload" }); |
| 1299 | members.appendAssumeCapacity(.{ .ty = error_ty_ref, .name = "error" }); |
| 1300 | // TODO: ABI padding? |
| 1301 | } |
| 1302 | |
| 1303 | return try self.spv.simpleStructType(members.slice()); |
| 539 | 1304 | }, |
| 540 | 1305 | |
| 541 | 1306 | .Null, |
| ... | ... | @@ -547,31 +1312,120 @@ pub const DeclGen = struct { |
| 547 | 1312 | => unreachable, // Must be comptime. |
| 548 | 1313 | |
| 549 | 1314 | else => |tag| return self.todo("Implement zig type '{}'", .{tag}), |
| 550 | | }; |
| 1315 | } |
| 551 | 1316 | } |
| 552 | 1317 | |
| 553 | | fn spirvStorageClass(as: std.builtin.AddressSpace) spec.StorageClass { |
| 1318 | fn spvStorageClass(as: std.builtin.AddressSpace) StorageClass { |
| 554 | 1319 | return switch (as) { |
| 555 | | .generic => .Generic, // TODO: Disallow? |
| 556 | | .gs, .fs, .ss => unreachable, |
| 1320 | .generic => .Generic, |
| 557 | 1321 | .shared => .Workgroup, |
| 558 | 1322 | .local => .Private, |
| 559 | | .global, .param, .constant, .flash, .flash1, .flash2, .flash3, .flash4, .flash5 => unreachable, |
| 1323 | .global => .CrossWorkgroup, |
| 1324 | .constant => .UniformConstant, |
| 1325 | .gs, |
| 1326 | .fs, |
| 1327 | .ss, |
| 1328 | .param, |
| 1329 | .flash, |
| 1330 | .flash1, |
| 1331 | .flash2, |
| 1332 | .flash3, |
| 1333 | .flash4, |
| 1334 | .flash5, |
| 1335 | => unreachable, |
| 560 | 1336 | }; |
| 561 | 1337 | } |
| 562 | 1338 | |
| 1339 | /// The SPIR-V backend is not yet advanced enough to support the std testing infrastructure. |
| 1340 | /// In order to be able to run tests, we "temporarily" lower test kernels into separate entry- |
| 1341 | /// points. The test executor will then be able to invoke these to run the tests. |
| 1342 | /// Note that tests are lowered according to std.builtin.TestFn, which is `fn () anyerror!void`. |
| 1343 | /// (anyerror!void has the same layout as anyerror). |
| 1344 | /// Each test declaration generates a function like. |
| 1345 | /// %anyerror = OpTypeInt 0 16 |
| 1346 | /// %p_anyerror = OpTypePointer CrossWorkgroup %anyerror |
| 1347 | /// %K = OpTypeFunction %void %p_anyerror |
| 1348 | /// |
| 1349 | /// %test = OpFunction %void %K |
| 1350 | /// %p_err = OpFunctionParameter %p_anyerror |
| 1351 | /// %lbl = OpLabel |
| 1352 | /// %result = OpFunctionCall %anyerror %func |
| 1353 | /// OpStore %p_err %result |
| 1354 | /// OpFunctionEnd |
| 1355 | /// TODO is to also write out the error as a function call parameter, and to somehow fetch |
| 1356 | /// the name of an error in the text executor. |
| 1357 | fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void { |
| 1358 | const anyerror_ty_ref = try self.resolveType(Type.anyerror, .direct); |
| 1359 | const ptr_anyerror_ty_ref = try self.spv.ptrType(anyerror_ty_ref, .CrossWorkgroup, 0); |
| 1360 | const void_ty_ref = try self.resolveType(Type.void, .direct); |
| 1361 | |
| 1362 | const kernel_proto_ty_ref = blk: { |
| 1363 | const proto_payload = try self.spv.arena.create(SpvType.Payload.Function); |
| 1364 | proto_payload.* = .{ |
| 1365 | .return_type = void_ty_ref, |
| 1366 | .parameters = try self.spv.arena.dupe(SpvType.Ref, &.{ptr_anyerror_ty_ref}), |
| 1367 | }; |
| 1368 | break :blk try self.spv.resolveType(SpvType.initPayload(&proto_payload.base)); |
| 1369 | }; |
| 1370 | |
| 1371 | const test_id = self.spv.declPtr(spv_test_decl_index).result_id; |
| 1372 | |
| 1373 | const spv_decl_index = try self.spv.allocDecl(.func); |
| 1374 | const kernel_id = self.spv.declPtr(spv_decl_index).result_id; |
| 1375 | |
| 1376 | const error_id = self.spv.allocId(); |
| 1377 | const p_error_id = self.spv.allocId(); |
| 1378 | |
| 1379 | const section = &self.spv.sections.functions; |
| 1380 | try section.emit(self.spv.gpa, .OpFunction, .{ |
| 1381 | .id_result_type = self.typeId(void_ty_ref), |
| 1382 | .id_result = kernel_id, |
| 1383 | .function_control = .{}, |
| 1384 | .function_type = self.typeId(kernel_proto_ty_ref), |
| 1385 | }); |
| 1386 | try section.emit(self.spv.gpa, .OpFunctionParameter, .{ |
| 1387 | .id_result_type = self.typeId(ptr_anyerror_ty_ref), |
| 1388 | .id_result = p_error_id, |
| 1389 | }); |
| 1390 | try section.emit(self.spv.gpa, .OpLabel, .{ |
| 1391 | .id_result = self.spv.allocId(), |
| 1392 | }); |
| 1393 | try section.emit(self.spv.gpa, .OpFunctionCall, .{ |
| 1394 | .id_result_type = self.typeId(anyerror_ty_ref), |
| 1395 | .id_result = error_id, |
| 1396 | .function = test_id, |
| 1397 | }); |
| 1398 | try section.emit(self.spv.gpa, .OpStore, .{ |
| 1399 | .pointer = p_error_id, |
| 1400 | .object = error_id, |
| 1401 | }); |
| 1402 | try section.emit(self.spv.gpa, .OpReturn, {}); |
| 1403 | try section.emit(self.spv.gpa, .OpFunctionEnd, {}); |
| 1404 | |
| 1405 | try self.spv.declareDeclDeps(spv_decl_index, &.{spv_test_decl_index}); |
| 1406 | |
| 1407 | // Just generate a quick other name because the intel runtime crashes when the entry- |
| 1408 | // point name is the same as a different OpName. |
| 1409 | const test_name = try std.fmt.allocPrint(self.gpa, "test {s}", .{name}); |
| 1410 | defer self.gpa.free(test_name); |
| 1411 | try self.spv.declareEntryPoint(spv_decl_index, test_name); |
| 1412 | } |
| 1413 | |
| 563 | 1414 | fn genDecl(self: *DeclGen) !void { |
| 564 | | const result_id = self.ids.get(self.decl_index).?; |
| 565 | 1415 | const decl = self.module.declPtr(self.decl_index); |
| 1416 | const spv_decl_index = try self.resolveDecl(self.decl_index); |
| 1417 | |
| 1418 | const decl_id = self.spv.declPtr(spv_decl_index).result_id; |
| 1419 | log.debug("genDecl {s} = {}", .{ decl.name, decl_id }); |
| 566 | 1420 | |
| 567 | 1421 | if (decl.val.castTag(.function)) |_| { |
| 568 | 1422 | assert(decl.ty.zigTypeTag() == .Fn); |
| 569 | 1423 | const prototype_id = try self.resolveTypeId(decl.ty); |
| 570 | 1424 | try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{ |
| 571 | 1425 | .id_result_type = try self.resolveTypeId(decl.ty.fnReturnType()), |
| 572 | | .id_result = result_id, |
| 1426 | .id_result = decl_id, |
| 573 | 1427 | .function_control = .{}, // TODO: We can set inline here if the type requires it. |
| 574 | | .function_type = prototype_id.toRef(), |
| 1428 | .function_type = prototype_id, |
| 575 | 1429 | }); |
| 576 | 1430 | |
| 577 | 1431 | const params = decl.ty.fnParamLen(); |
| ... | ... | @@ -585,7 +1439,7 @@ pub const DeclGen = struct { |
| 585 | 1439 | .id_result_type = param_type_id, |
| 586 | 1440 | .id_result = arg_result_id, |
| 587 | 1441 | }); |
| 588 | | self.args.appendAssumeCapacity(arg_result_id.toRef()); |
| 1442 | self.args.appendAssumeCapacity(arg_result_id); |
| 589 | 1443 | } |
| 590 | 1444 | |
| 591 | 1445 | // TODO: This could probably be done in a better way... |
| ... | ... | @@ -596,17 +1450,53 @@ pub const DeclGen = struct { |
| 596 | 1450 | try self.func.prologue.emit(self.spv.gpa, .OpLabel, .{ |
| 597 | 1451 | .id_result = root_block_id, |
| 598 | 1452 | }); |
| 599 | | self.current_block_label_id = root_block_id.toRef(); |
| 1453 | self.current_block_label_id = root_block_id; |
| 600 | 1454 | |
| 601 | 1455 | const main_body = self.air.getMainBody(); |
| 602 | 1456 | try self.genBody(main_body); |
| 603 | 1457 | |
| 604 | 1458 | // Append the actual code into the functions section. |
| 605 | 1459 | try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); |
| 606 | | try self.spv.addFunction(self.func); |
| 1460 | try self.spv.addFunction(spv_decl_index, self.func); |
| 1461 | |
| 1462 | const fqn = try decl.getFullyQualifiedName(self.module); |
| 1463 | defer self.module.gpa.free(fqn); |
| 1464 | |
| 1465 | try self.spv.sections.debug_names.emit(self.gpa, .OpName, .{ |
| 1466 | .target = decl_id, |
| 1467 | .name = fqn, |
| 1468 | }); |
| 1469 | |
| 1470 | // Temporarily generate a test kernel declaration if this is a test function. |
| 1471 | if (self.module.test_functions.contains(self.decl_index)) { |
| 1472 | try self.generateTestEntryPoint(fqn, spv_decl_index); |
| 1473 | } |
| 607 | 1474 | } else { |
| 608 | | // TODO |
| 609 | | // return self.todo("generate decl type {}", .{decl.ty.zigTypeTag()}); |
| 1475 | const init_val = if (decl.val.castTag(.variable)) |payload| |
| 1476 | payload.data.init |
| 1477 | else |
| 1478 | decl.val; |
| 1479 | |
| 1480 | if (init_val.tag() == .unreachable_value) { |
| 1481 | return self.todo("importing extern variables", .{}); |
| 1482 | } |
| 1483 | |
| 1484 | // TODO: integrate with variable(). |
| 1485 | |
| 1486 | const final_storage_class = spvStorageClass(decl.@"addrspace"); |
| 1487 | const actual_storage_class = switch (final_storage_class) { |
| 1488 | .Generic => .CrossWorkgroup, |
| 1489 | else => final_storage_class, |
| 1490 | }; |
| 1491 | |
| 1492 | try self.lowerIndirectConstant( |
| 1493 | spv_decl_index, |
| 1494 | decl.ty, |
| 1495 | init_val, |
| 1496 | actual_storage_class, |
| 1497 | final_storage_class == .Generic, |
| 1498 | decl.@"align", |
| 1499 | ); |
| 610 | 1500 | } |
| 611 | 1501 | } |
| 612 | 1502 | |
| ... | ... | @@ -618,11 +1508,25 @@ pub const DeclGen = struct { |
| 618 | 1508 | |
| 619 | 1509 | fn genInst(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 620 | 1510 | const air_tags = self.air.instructions.items(.tag); |
| 621 | | const result_id = switch (air_tags[inst]) { |
| 1511 | const maybe_result_id: ?IdRef = switch (air_tags[inst]) { |
| 622 | 1512 | // zig fmt: off |
| 623 | | .add, .addwrap => try self.airArithOp(inst, .OpFAdd, .OpIAdd, .OpIAdd), |
| 624 | | .sub, .subwrap => try self.airArithOp(inst, .OpFSub, .OpISub, .OpISub), |
| 625 | | .mul, .mulwrap => try self.airArithOp(inst, .OpFMul, .OpIMul, .OpIMul), |
| 1513 | .add, .addwrap => try self.airArithOp(inst, .OpFAdd, .OpIAdd, .OpIAdd, true), |
| 1514 | .sub, .subwrap => try self.airArithOp(inst, .OpFSub, .OpISub, .OpISub, true), |
| 1515 | .mul, .mulwrap => try self.airArithOp(inst, .OpFMul, .OpIMul, .OpIMul, true), |
| 1516 | |
| 1517 | .div_float, |
| 1518 | .div_float_optimized, |
| 1519 | // TODO: Check that this is the right operation. |
| 1520 | .div_trunc, |
| 1521 | .div_trunc_optimized, |
| 1522 | => try self.airArithOp(inst, .OpFDiv, .OpSDiv, .OpUDiv, false), |
| 1523 | // TODO: Check if this is the right operation |
| 1524 | // TODO: Make airArithOp for rem not emit a mask for the LHS. |
| 1525 | .rem, |
| 1526 | .rem_optimized, |
| 1527 | => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem, false), |
| 1528 | |
| 1529 | .add_with_overflow => try self.airOverflowArithOp(inst), |
| 626 | 1530 | |
| 627 | 1531 | .shuffle => try self.airShuffle(inst), |
| 628 | 1532 | |
| ... | ... | @@ -632,7 +1536,24 @@ pub const DeclGen = struct { |
| 632 | 1536 | .bool_and => try self.airBinOpSimple(inst, .OpLogicalAnd), |
| 633 | 1537 | .bool_or => try self.airBinOpSimple(inst, .OpLogicalOr), |
| 634 | 1538 | |
| 635 | | .not => try self.airNot(inst), |
| 1539 | .shl => try self.airShift(inst, .OpShiftLeftLogical), |
| 1540 | |
| 1541 | .bitcast => try self.airBitcast(inst), |
| 1542 | .intcast => try self.airIntcast(inst), |
| 1543 | .not => try self.airNot(inst), |
| 1544 | |
| 1545 | .slice_ptr => try self.airSliceField(inst, 0), |
| 1546 | .slice_len => try self.airSliceField(inst, 1), |
| 1547 | .slice_elem_ptr => try self.airSliceElemPtr(inst), |
| 1548 | .slice_elem_val => try self.airSliceElemVal(inst), |
| 1549 | .ptr_elem_ptr => try self.airPtrElemPtr(inst), |
| 1550 | |
| 1551 | .struct_field_val => try self.airStructFieldVal(inst), |
| 1552 | |
| 1553 | .struct_field_ptr_index_0 => try self.airStructFieldPtrIndex(inst, 0), |
| 1554 | .struct_field_ptr_index_1 => try self.airStructFieldPtrIndex(inst, 1), |
| 1555 | .struct_field_ptr_index_2 => try self.airStructFieldPtrIndex(inst, 2), |
| 1556 | .struct_field_ptr_index_3 => try self.airStructFieldPtrIndex(inst, 3), |
| 636 | 1557 | |
| 637 | 1558 | .cmp_eq => try self.airCmp(inst, .OpFOrdEqual, .OpLogicalEqual, .OpIEqual), |
| 638 | 1559 | .cmp_neq => try self.airCmp(inst, .OpFOrdNotEqual, .OpLogicalNotEqual, .OpINotEqual), |
| ... | ... | @@ -641,21 +1562,33 @@ pub const DeclGen = struct { |
| 641 | 1562 | .cmp_lt => try self.airCmp(inst, .OpFOrdLessThan, .OpSLessThan, .OpULessThan), |
| 642 | 1563 | .cmp_lte => try self.airCmp(inst, .OpFOrdLessThanEqual, .OpSLessThanEqual, .OpULessThanEqual), |
| 643 | 1564 | |
| 644 | | .arg => self.airArg(), |
| 645 | | .alloc => try self.airAlloc(inst), |
| 646 | | .block => (try self.airBlock(inst)) orelse return, |
| 647 | | .load => try self.airLoad(inst), |
| 1565 | .arg => self.airArg(), |
| 1566 | .alloc => try self.airAlloc(inst), |
| 1567 | // TODO: We probably need to have a special implementation of this for the C abi. |
| 1568 | .ret_ptr => try self.airAlloc(inst), |
| 1569 | .block => try self.airBlock(inst), |
| 1570 | |
| 1571 | .load => try self.airLoad(inst), |
| 1572 | .store => return self.airStore(inst), |
| 648 | 1573 | |
| 649 | 1574 | .br => return self.airBr(inst), |
| 650 | 1575 | .breakpoint => return, |
| 651 | 1576 | .cond_br => return self.airCondBr(inst), |
| 652 | 1577 | .constant => unreachable, |
| 1578 | .const_ty => unreachable, |
| 653 | 1579 | .dbg_stmt => return self.airDbgStmt(inst), |
| 654 | 1580 | .loop => return self.airLoop(inst), |
| 655 | 1581 | .ret => return self.airRet(inst), |
| 656 | | .store => return self.airStore(inst), |
| 1582 | .ret_load => return self.airRetLoad(inst), |
| 1583 | .switch_br => return self.airSwitchBr(inst), |
| 657 | 1584 | .unreach => return self.airUnreach(), |
| 658 | | .assembly => (try self.airAssembly(inst)) orelse return, |
| 1585 | |
| 1586 | .assembly => try self.airAssembly(inst), |
| 1587 | |
| 1588 | .call => try self.airCall(inst, .auto), |
| 1589 | .call_always_tail => try self.airCall(inst, .always_tail), |
| 1590 | .call_never_tail => try self.airCall(inst, .never_tail), |
| 1591 | .call_never_inline => try self.airCall(inst, .never_inline), |
| 659 | 1592 | |
| 660 | 1593 | .dbg_var_ptr => return, |
| 661 | 1594 | .dbg_var_val => return, |
| ... | ... | @@ -666,22 +1599,62 @@ pub const DeclGen = struct { |
| 666 | 1599 | else => |tag| return self.todo("implement AIR tag {s}", .{@tagName(tag)}), |
| 667 | 1600 | }; |
| 668 | 1601 | |
| 1602 | const result_id = maybe_result_id orelse return; |
| 669 | 1603 | try self.inst_results.putNoClobber(self.gpa, inst, result_id); |
| 670 | 1604 | } |
| 671 | 1605 | |
| 672 | | fn airBinOpSimple(self: *DeclGen, inst: Air.Inst.Index, comptime opcode: Opcode) !IdRef { |
| 1606 | fn airBinOpSimple(self: *DeclGen, inst: Air.Inst.Index, comptime opcode: Opcode) !?IdRef { |
| 1607 | if (self.liveness.isUnused(inst)) return null; |
| 1608 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1609 | const lhs_id = try self.resolve(bin_op.lhs); |
| 1610 | const rhs_id = try self.resolve(bin_op.rhs); |
| 1611 | const result_id = self.spv.allocId(); |
| 1612 | const result_type_id = try self.resolveTypeId(self.air.typeOfIndex(inst)); |
| 1613 | try self.func.body.emit(self.spv.gpa, opcode, .{ |
| 1614 | .id_result_type = result_type_id, |
| 1615 | .id_result = result_id, |
| 1616 | .operand_1 = lhs_id, |
| 1617 | .operand_2 = rhs_id, |
| 1618 | }); |
| 1619 | return result_id; |
| 1620 | } |
| 1621 | |
| 1622 | fn airShift(self: *DeclGen, inst: Air.Inst.Index, comptime opcode: Opcode) !?IdRef { |
| 1623 | if (self.liveness.isUnused(inst)) return null; |
| 673 | 1624 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 674 | 1625 | const lhs_id = try self.resolve(bin_op.lhs); |
| 675 | 1626 | const rhs_id = try self.resolve(bin_op.rhs); |
| 676 | | const result_id = self.spv.allocId(); |
| 677 | 1627 | const result_type_id = try self.resolveTypeId(self.air.typeOfIndex(inst)); |
| 1628 | |
| 1629 | // the shift and the base must be the same type in SPIR-V, but in Zig the shift is a smaller int. |
| 1630 | const shift_id = self.spv.allocId(); |
| 1631 | try self.func.body.emit(self.spv.gpa, .OpUConvert, .{ |
| 1632 | .id_result_type = result_type_id, |
| 1633 | .id_result = shift_id, |
| 1634 | .unsigned_value = rhs_id, |
| 1635 | }); |
| 1636 | |
| 1637 | const result_id = self.spv.allocId(); |
| 678 | 1638 | try self.func.body.emit(self.spv.gpa, opcode, .{ |
| 679 | 1639 | .id_result_type = result_type_id, |
| 680 | 1640 | .id_result = result_id, |
| 681 | | .operand_1 = lhs_id, |
| 682 | | .operand_2 = rhs_id, |
| 1641 | .base = lhs_id, |
| 1642 | .shift = shift_id, |
| 1643 | }); |
| 1644 | return result_id; |
| 1645 | } |
| 1646 | |
| 1647 | fn maskStrangeInt(self: *DeclGen, ty_ref: SpvType.Ref, value_id: IdRef, bits: u16) !IdRef { |
| 1648 | const mask_value = if (bits == 64) 0xFFFF_FFFF_FFFF_FFFF else (@as(u64, 1) << @intCast(u6, bits)) - 1; |
| 1649 | const result_id = self.spv.allocId(); |
| 1650 | const mask_id = try self.constInt(ty_ref, mask_value); |
| 1651 | try self.func.body.emit(self.spv.gpa, .OpBitwiseAnd, .{ |
| 1652 | .id_result_type = self.typeId(ty_ref), |
| 1653 | .id_result = result_id, |
| 1654 | .operand_1 = value_id, |
| 1655 | .operand_2 = mask_id, |
| 683 | 1656 | }); |
| 684 | | return result_id.toRef(); |
| 1657 | return result_id; |
| 685 | 1658 | } |
| 686 | 1659 | |
| 687 | 1660 | fn airArithOp( |
| ... | ... | @@ -690,16 +1663,18 @@ pub const DeclGen = struct { |
| 690 | 1663 | comptime fop: Opcode, |
| 691 | 1664 | comptime sop: Opcode, |
| 692 | 1665 | comptime uop: Opcode, |
| 693 | | ) !IdRef { |
| 1666 | /// true if this operation holds under modular arithmetic. |
| 1667 | comptime modular: bool, |
| 1668 | ) !?IdRef { |
| 1669 | if (self.liveness.isUnused(inst)) return null; |
| 694 | 1670 | // LHS and RHS are guaranteed to have the same type, and AIR guarantees |
| 695 | 1671 | // the result to be the same as the LHS and RHS, which matches SPIR-V. |
| 696 | 1672 | const ty = self.air.typeOfIndex(inst); |
| 697 | 1673 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 698 | | const lhs_id = try self.resolve(bin_op.lhs); |
| 699 | | const rhs_id = try self.resolve(bin_op.rhs); |
| 1674 | var lhs_id = try self.resolve(bin_op.lhs); |
| 1675 | var rhs_id = try self.resolve(bin_op.rhs); |
| 700 | 1676 | |
| 701 | | const result_id = self.spv.allocId(); |
| 702 | | const result_type_id = try self.resolveTypeId(ty); |
| 1677 | const result_ty_ref = try self.resolveType(ty, .direct); |
| 703 | 1678 | |
| 704 | 1679 | assert(self.air.typeOf(bin_op.lhs).eql(ty, self.module)); |
| 705 | 1680 | assert(self.air.typeOf(bin_op.rhs).eql(ty, self.module)); |
| ... | ... | @@ -712,19 +1687,27 @@ pub const DeclGen = struct { |
| 712 | 1687 | .composite_integer => { |
| 713 | 1688 | return self.todo("binary operations for composite integers", .{}); |
| 714 | 1689 | }, |
| 715 | | .strange_integer => { |
| 716 | | return self.todo("binary operations for strange integers", .{}); |
| 1690 | .strange_integer => blk: { |
| 1691 | if (!modular) { |
| 1692 | lhs_id = try self.maskStrangeInt(result_ty_ref, lhs_id, info.bits); |
| 1693 | rhs_id = try self.maskStrangeInt(result_ty_ref, rhs_id, info.bits); |
| 1694 | } |
| 1695 | break :blk switch (info.signedness) { |
| 1696 | .signed => @as(usize, 1), |
| 1697 | .unsigned => @as(usize, 2), |
| 1698 | }; |
| 717 | 1699 | }, |
| 718 | 1700 | .integer => switch (info.signedness) { |
| 719 | 1701 | .signed => @as(usize, 1), |
| 720 | 1702 | .unsigned => @as(usize, 2), |
| 721 | 1703 | }, |
| 722 | 1704 | .float => 0, |
| 723 | | else => unreachable, |
| 1705 | .bool => unreachable, |
| 724 | 1706 | }; |
| 725 | 1707 | |
| 1708 | const result_id = self.spv.allocId(); |
| 726 | 1709 | const operands = .{ |
| 727 | | .id_result_type = result_type_id, |
| 1710 | .id_result_type = self.typeId(result_ty_ref), |
| 728 | 1711 | .id_result = result_id, |
| 729 | 1712 | .operand_1 = lhs_id, |
| 730 | 1713 | .operand_2 = rhs_id, |
| ... | ... | @@ -739,10 +1722,90 @@ pub const DeclGen = struct { |
| 739 | 1722 | // TODO: Trap on overflow? Probably going to be annoying. |
| 740 | 1723 | // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap. |
| 741 | 1724 | |
| 742 | | return result_id.toRef(); |
| 1725 | return result_id; |
| 1726 | } |
| 1727 | |
| 1728 | fn airOverflowArithOp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 1729 | if (self.liveness.isUnused(inst)) return null; |
| 1730 | |
| 1731 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 1732 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1733 | const lhs = try self.resolve(extra.lhs); |
| 1734 | const rhs = try self.resolve(extra.rhs); |
| 1735 | |
| 1736 | const operand_ty = self.air.typeOf(extra.lhs); |
| 1737 | const result_ty = self.air.typeOfIndex(inst); |
| 1738 | |
| 1739 | const info = try self.arithmeticTypeInfo(operand_ty); |
| 1740 | switch (info.class) { |
| 1741 | .composite_integer => return self.todo("overflow ops for composite integers", .{}), |
| 1742 | .strange_integer => return self.todo("overflow ops for strange integers", .{}), |
| 1743 | .integer => {}, |
| 1744 | .float, .bool => unreachable, |
| 1745 | } |
| 1746 | |
| 1747 | const operand_ty_id = try self.resolveTypeId(operand_ty); |
| 1748 | const result_type_id = try self.resolveTypeId(result_ty); |
| 1749 | |
| 1750 | const overflow_member_ty = try self.intType(.unsigned, info.bits); |
| 1751 | const overflow_member_ty_id = self.typeId(overflow_member_ty); |
| 1752 | |
| 1753 | const op_result_id = blk: { |
| 1754 | // Construct the SPIR-V result type. |
| 1755 | // It is almost the same as the zig one, except that the fields must be the same type |
| 1756 | // and they must be unsigned. |
| 1757 | const overflow_result_ty_ref = try self.spv.simpleStructType(&.{ |
| 1758 | .{ .ty = overflow_member_ty, .name = "res" }, |
| 1759 | .{ .ty = overflow_member_ty, .name = "ov" }, |
| 1760 | }); |
| 1761 | const result_id = self.spv.allocId(); |
| 1762 | try self.func.body.emit(self.spv.gpa, .OpIAddCarry, .{ |
| 1763 | .id_result_type = self.typeId(overflow_result_ty_ref), |
| 1764 | .id_result = result_id, |
| 1765 | .operand_1 = lhs, |
| 1766 | .operand_2 = rhs, |
| 1767 | }); |
| 1768 | break :blk result_id; |
| 1769 | }; |
| 1770 | |
| 1771 | // Now convert the SPIR-V flavor result into a Zig-flavor result. |
| 1772 | // First, extract the two fields. |
| 1773 | const unsigned_result = try self.extractField(overflow_member_ty_id, op_result_id, 0); |
| 1774 | const overflow = try self.extractField(overflow_member_ty_id, op_result_id, 1); |
| 1775 | |
| 1776 | // We need to convert the results to the types that Zig expects here. |
| 1777 | // The `result` is the same type except unsigned, so we can just bitcast that. |
| 1778 | const result = try self.bitcast(operand_ty_id, unsigned_result); |
| 1779 | |
| 1780 | // The overflow needs to be converted into whatever is used to represent it in Zig. |
| 1781 | const casted_overflow = blk: { |
| 1782 | const ov_ty = result_ty.tupleFields().types[1]; |
| 1783 | const ov_ty_id = try self.resolveTypeId(ov_ty); |
| 1784 | const result_id = self.spv.allocId(); |
| 1785 | try self.func.body.emit(self.spv.gpa, .OpUConvert, .{ |
| 1786 | .id_result_type = ov_ty_id, |
| 1787 | .id_result = result_id, |
| 1788 | .unsigned_value = overflow, |
| 1789 | }); |
| 1790 | break :blk result_id; |
| 1791 | }; |
| 1792 | |
| 1793 | // TODO: If copying this function for borrow, make sure to convert -1 to 1 as appropriate. |
| 1794 | |
| 1795 | // Finally, construct the Zig type. |
| 1796 | // Layout is result, overflow. |
| 1797 | const result_id = self.spv.allocId(); |
| 1798 | const constituents = [_]IdRef{ result, casted_overflow }; |
| 1799 | try self.func.body.emit(self.spv.gpa, .OpCompositeConstruct, .{ |
| 1800 | .id_result_type = result_type_id, |
| 1801 | .id_result = result_id, |
| 1802 | .constituents = &constituents, |
| 1803 | }); |
| 1804 | return result_id; |
| 743 | 1805 | } |
| 744 | 1806 | |
| 745 | | fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !IdRef { |
| 1807 | fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 1808 | if (self.liveness.isUnused(inst)) return null; |
| 746 | 1809 | const ty = self.air.typeOfIndex(inst); |
| 747 | 1810 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 748 | 1811 | const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| ... | ... | @@ -774,15 +1837,16 @@ pub const DeclGen = struct { |
| 774 | 1837 | self.func.body.writeOperand(spec.LiteralInteger, unsigned); |
| 775 | 1838 | } |
| 776 | 1839 | } |
| 777 | | return result_id.toRef(); |
| 1840 | return result_id; |
| 778 | 1841 | } |
| 779 | 1842 | |
| 780 | | fn airCmp(self: *DeclGen, inst: Air.Inst.Index, comptime fop: Opcode, comptime sop: Opcode, comptime uop: Opcode) !IdRef { |
| 1843 | fn airCmp(self: *DeclGen, inst: Air.Inst.Index, comptime fop: Opcode, comptime sop: Opcode, comptime uop: Opcode) !?IdRef { |
| 1844 | if (self.liveness.isUnused(inst)) return null; |
| 781 | 1845 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 782 | | const lhs_id = try self.resolve(bin_op.lhs); |
| 783 | | const rhs_id = try self.resolve(bin_op.rhs); |
| 1846 | var lhs_id = try self.resolve(bin_op.lhs); |
| 1847 | var rhs_id = try self.resolve(bin_op.rhs); |
| 784 | 1848 | const result_id = self.spv.allocId(); |
| 785 | | const result_type_id = try self.resolveTypeId(Type.initTag(.bool)); |
| 1849 | const result_type_id = try self.resolveTypeId(Type.bool); |
| 786 | 1850 | const op_ty = self.air.typeOf(bin_op.lhs); |
| 787 | 1851 | assert(op_ty.eql(self.air.typeOf(bin_op.rhs), self.module)); |
| 788 | 1852 | |
| ... | ... | @@ -794,11 +1858,17 @@ pub const DeclGen = struct { |
| 794 | 1858 | .composite_integer => { |
| 795 | 1859 | return self.todo("binary operations for composite integers", .{}); |
| 796 | 1860 | }, |
| 797 | | .strange_integer => { |
| 798 | | return self.todo("comparison for strange integers", .{}); |
| 799 | | }, |
| 800 | 1861 | .float => 0, |
| 801 | 1862 | .bool => 1, |
| 1863 | .strange_integer => blk: { |
| 1864 | const op_ty_ref = try self.resolveType(op_ty, .direct); |
| 1865 | lhs_id = try self.maskStrangeInt(op_ty_ref, lhs_id, info.bits); |
| 1866 | rhs_id = try self.maskStrangeInt(op_ty_ref, rhs_id, info.bits); |
| 1867 | break :blk switch (info.signedness) { |
| 1868 | .signed => @as(usize, 1), |
| 1869 | .unsigned => @as(usize, 2), |
| 1870 | }; |
| 1871 | }, |
| 802 | 1872 | .integer => switch (info.signedness) { |
| 803 | 1873 | .signed => @as(usize, 1), |
| 804 | 1874 | .unsigned => @as(usize, 2), |
| ... | ... | @@ -819,41 +1889,336 @@ pub const DeclGen = struct { |
| 819 | 1889 | else => unreachable, |
| 820 | 1890 | } |
| 821 | 1891 | |
| 822 | | return result_id.toRef(); |
| 1892 | return result_id; |
| 1893 | } |
| 1894 | |
| 1895 | fn bitcast(self: *DeclGen, target_type_id: IdResultType, value_id: IdRef) !IdRef { |
| 1896 | const result_id = self.spv.allocId(); |
| 1897 | try self.func.body.emit(self.spv.gpa, .OpBitcast, .{ |
| 1898 | .id_result_type = target_type_id, |
| 1899 | .id_result = result_id, |
| 1900 | .operand = value_id, |
| 1901 | }); |
| 1902 | return result_id; |
| 1903 | } |
| 1904 | |
| 1905 | fn airBitcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 1906 | if (self.liveness.isUnused(inst)) return null; |
| 1907 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1908 | const operand_id = try self.resolve(ty_op.operand); |
| 1909 | const result_type_id = try self.resolveTypeId(self.air.typeOfIndex(inst)); |
| 1910 | return try self.bitcast(result_type_id, operand_id); |
| 1911 | } |
| 1912 | |
| 1913 | fn airIntcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 1914 | if (self.liveness.isUnused(inst)) return null; |
| 1915 | |
| 1916 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1917 | const operand_id = try self.resolve(ty_op.operand); |
| 1918 | const dest_ty = self.air.typeOfIndex(inst); |
| 1919 | const dest_info = try self.arithmeticTypeInfo(dest_ty); |
| 1920 | const dest_ty_id = try self.resolveTypeId(dest_ty); |
| 1921 | |
| 1922 | const result_id = self.spv.allocId(); |
| 1923 | switch (dest_info.signedness) { |
| 1924 | .signed => try self.func.body.emit(self.spv.gpa, .OpSConvert, .{ |
| 1925 | .id_result_type = dest_ty_id, |
| 1926 | .id_result = result_id, |
| 1927 | .signed_value = operand_id, |
| 1928 | }), |
| 1929 | .unsigned => try self.func.body.emit(self.spv.gpa, .OpUConvert, .{ |
| 1930 | .id_result_type = dest_ty_id, |
| 1931 | .id_result = result_id, |
| 1932 | .unsigned_value = operand_id, |
| 1933 | }), |
| 1934 | } |
| 1935 | return result_id; |
| 823 | 1936 | } |
| 824 | 1937 | |
| 825 | | fn airNot(self: *DeclGen, inst: Air.Inst.Index) !IdRef { |
| 1938 | fn airNot(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 1939 | if (self.liveness.isUnused(inst)) return null; |
| 826 | 1940 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 827 | 1941 | const operand_id = try self.resolve(ty_op.operand); |
| 828 | 1942 | const result_id = self.spv.allocId(); |
| 829 | | const result_type_id = try self.resolveTypeId(Type.initTag(.bool)); |
| 1943 | const result_type_id = try self.resolveTypeId(Type.bool); |
| 830 | 1944 | try self.func.body.emit(self.spv.gpa, .OpLogicalNot, .{ |
| 831 | 1945 | .id_result_type = result_type_id, |
| 832 | 1946 | .id_result = result_id, |
| 833 | 1947 | .operand = operand_id, |
| 834 | 1948 | }); |
| 835 | | return result_id.toRef(); |
| 1949 | return result_id; |
| 836 | 1950 | } |
| 837 | 1951 | |
| 838 | | fn airAlloc(self: *DeclGen, inst: Air.Inst.Index) !IdRef { |
| 839 | | const ty = self.air.typeOfIndex(inst); |
| 840 | | const result_type_id = try self.resolveTypeId(ty); |
| 1952 | fn extractField(self: *DeclGen, result_ty: IdResultType, object: IdRef, field: u32) !IdRef { |
| 841 | 1953 | const result_id = self.spv.allocId(); |
| 1954 | const indexes = [_]u32{field}; |
| 1955 | try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ |
| 1956 | .id_result_type = result_ty, |
| 1957 | .id_result = result_id, |
| 1958 | .composite = object, |
| 1959 | .indexes = &indexes, |
| 1960 | }); |
| 1961 | return result_id; |
| 1962 | } |
| 842 | 1963 | |
| 843 | | // Rather than generating into code here, we're just going to generate directly into the functions section so that |
| 844 | | // variable declarations appear in the first block of the function. |
| 845 | | const storage_class = spirvStorageClass(ty.ptrAddressSpace()); |
| 846 | | const section = if (storage_class == .Function) |
| 847 | | &self.func.prologue |
| 848 | | else |
| 849 | | &self.spv.sections.types_globals_constants; |
| 1964 | fn airSliceField(self: *DeclGen, inst: Air.Inst.Index, field: u32) !?IdRef { |
| 1965 | if (self.liveness.isUnused(inst)) return null; |
| 1966 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 1967 | return try self.extractField( |
| 1968 | try self.resolveTypeId(self.air.typeOfIndex(inst)), |
| 1969 | try self.resolve(ty_op.operand), |
| 1970 | field, |
| 1971 | ); |
| 1972 | } |
| 850 | 1973 | |
| 851 | | try section.emit(self.spv.gpa, .OpVariable, .{ |
| 1974 | fn airSliceElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 1975 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1976 | const slice_ty = self.air.typeOf(bin_op.lhs); |
| 1977 | if (!slice_ty.isVolatilePtr() and self.liveness.isUnused(inst)) return null; |
| 1978 | |
| 1979 | const slice = try self.resolve(bin_op.lhs); |
| 1980 | const index = try self.resolve(bin_op.rhs); |
| 1981 | |
| 1982 | const spv_ptr_ty = try self.resolveTypeId(self.air.typeOfIndex(inst)); |
| 1983 | |
| 1984 | const slice_ptr = blk: { |
| 1985 | const result_id = self.spv.allocId(); |
| 1986 | try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ |
| 1987 | .id_result_type = spv_ptr_ty, |
| 1988 | .id_result = result_id, |
| 1989 | .composite = slice, |
| 1990 | .indexes = &.{0}, |
| 1991 | }); |
| 1992 | break :blk result_id; |
| 1993 | }; |
| 1994 | |
| 1995 | const result_id = self.spv.allocId(); |
| 1996 | try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{ |
| 1997 | .id_result_type = spv_ptr_ty, |
| 1998 | .id_result = result_id, |
| 1999 | .base = slice_ptr, |
| 2000 | .element = index, |
| 2001 | }); |
| 2002 | return result_id; |
| 2003 | } |
| 2004 | |
| 2005 | fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 2006 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 2007 | const slice_ty = self.air.typeOf(bin_op.lhs); |
| 2008 | if (!slice_ty.isVolatilePtr() and self.liveness.isUnused(inst)) return null; |
| 2009 | |
| 2010 | const slice = try self.resolve(bin_op.lhs); |
| 2011 | const index = try self.resolve(bin_op.rhs); |
| 2012 | |
| 2013 | var slice_buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 2014 | const ptr_ty_id = try self.resolveTypeId(slice_ty.slicePtrFieldType(&slice_buf)); |
| 2015 | |
| 2016 | const slice_ptr = blk: { |
| 2017 | const result_id = self.spv.allocId(); |
| 2018 | try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ |
| 2019 | .id_result_type = ptr_ty_id, |
| 2020 | .id_result = result_id, |
| 2021 | .composite = slice, |
| 2022 | .indexes = &.{0}, |
| 2023 | }); |
| 2024 | break :blk result_id; |
| 2025 | }; |
| 2026 | |
| 2027 | const elem_ptr = blk: { |
| 2028 | const result_id = self.spv.allocId(); |
| 2029 | try self.func.body.emit(self.spv.gpa, .OpInBoundsPtrAccessChain, .{ |
| 2030 | .id_result_type = ptr_ty_id, |
| 2031 | .id_result = result_id, |
| 2032 | .base = slice_ptr, |
| 2033 | .element = index, |
| 2034 | }); |
| 2035 | break :blk result_id; |
| 2036 | }; |
| 2037 | |
| 2038 | return try self.load(slice_ty, elem_ptr); |
| 2039 | } |
| 2040 | |
| 2041 | fn airPtrElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 2042 | if (self.liveness.isUnused(inst)) return null; |
| 2043 | |
| 2044 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2045 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2046 | const ptr_ty = self.air.typeOf(bin_op.lhs); |
| 2047 | const result_ty = self.air.typeOfIndex(inst); |
| 2048 | const elem_ty = ptr_ty.childType(); |
| 2049 | // TODO: Make this return a null ptr or something |
| 2050 | if (!elem_ty.hasRuntimeBitsIgnoreComptime()) return null; |
| 2051 | |
| 2052 | const result_type_id = try self.resolveTypeId(result_ty); |
| 2053 | const base_ptr = try self.resolve(bin_op.lhs); |
| 2054 | const rhs = try self.resolve(bin_op.rhs); |
| 2055 | |
| 2056 | const result_id = self.spv.allocId(); |
| 2057 | const indexes = [_]IdRef{rhs}; |
| 2058 | try self.func.body.emit(self.spv.gpa, .OpInBoundsAccessChain, .{ |
| 852 | 2059 | .id_result_type = result_type_id, |
| 853 | 2060 | .id_result = result_id, |
| 854 | | .storage_class = storage_class, |
| 2061 | .base = base_ptr, |
| 2062 | .indexes = &indexes, |
| 2063 | }); |
| 2064 | return result_id; |
| 2065 | } |
| 2066 | |
| 2067 | fn airStructFieldVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 2068 | if (self.liveness.isUnused(inst)) return null; |
| 2069 | |
| 2070 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |
| 2071 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 2072 | |
| 2073 | const struct_ty = self.air.typeOf(struct_field.struct_operand); |
| 2074 | const object = try self.resolve(struct_field.struct_operand); |
| 2075 | const field_index = struct_field.field_index; |
| 2076 | const field_ty = struct_ty.structFieldType(field_index); |
| 2077 | const field_ty_id = try self.resolveTypeId(field_ty); |
| 2078 | |
| 2079 | if (!field_ty.hasRuntimeBitsIgnoreComptime()) return null; |
| 2080 | |
| 2081 | assert(struct_ty.zigTypeTag() == .Struct); // Cannot do unions yet. |
| 2082 | |
| 2083 | const result_id = self.spv.allocId(); |
| 2084 | const indexes = [_]u32{field_index}; |
| 2085 | try self.func.body.emit(self.spv.gpa, .OpCompositeExtract, .{ |
| 2086 | .id_result_type = field_ty_id, |
| 2087 | .id_result = result_id, |
| 2088 | .composite = object, |
| 2089 | .indexes = &indexes, |
| 855 | 2090 | }); |
| 856 | | return result_id.toRef(); |
| 2091 | return result_id; |
| 2092 | } |
| 2093 | |
| 2094 | fn structFieldPtr( |
| 2095 | self: *DeclGen, |
| 2096 | result_ptr_ty: Type, |
| 2097 | object_ptr_ty: Type, |
| 2098 | object_ptr: IdRef, |
| 2099 | field_index: u32, |
| 2100 | ) !?IdRef { |
| 2101 | const object_ty = object_ptr_ty.childType(); |
| 2102 | switch (object_ty.zigTypeTag()) { |
| 2103 | .Struct => switch (object_ty.containerLayout()) { |
| 2104 | .Packed => unreachable, // TODO |
| 2105 | else => { |
| 2106 | const u32_ty_id = self.typeId(try self.intType(.unsigned, 32)); |
| 2107 | const field_index_id = self.spv.allocId(); |
| 2108 | try self.spv.emitConstant(u32_ty_id, field_index_id, .{ .uint32 = field_index }); |
| 2109 | const result_id = self.spv.allocId(); |
| 2110 | const result_type_id = try self.resolveTypeId(result_ptr_ty); |
| 2111 | const indexes = [_]IdRef{field_index_id}; |
| 2112 | try self.func.body.emit(self.spv.gpa, .OpInBoundsAccessChain, .{ |
| 2113 | .id_result_type = result_type_id, |
| 2114 | .id_result = result_id, |
| 2115 | .base = object_ptr, |
| 2116 | .indexes = &indexes, |
| 2117 | }); |
| 2118 | return result_id; |
| 2119 | }, |
| 2120 | }, |
| 2121 | else => unreachable, // TODO |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | fn airStructFieldPtrIndex(self: *DeclGen, inst: Air.Inst.Index, field_index: u32) !?IdRef { |
| 2126 | if (self.liveness.isUnused(inst)) return null; |
| 2127 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 2128 | const struct_ptr = try self.resolve(ty_op.operand); |
| 2129 | const struct_ptr_ty = self.air.typeOf(ty_op.operand); |
| 2130 | const result_ptr_ty = self.air.typeOfIndex(inst); |
| 2131 | return try self.structFieldPtr(result_ptr_ty, struct_ptr_ty, struct_ptr, field_index); |
| 2132 | } |
| 2133 | |
| 2134 | /// We cannot use an OpVariable directly in an OpSpecConstantOp, but we can |
| 2135 | /// after we insert a dummy AccessChain... |
| 2136 | /// TODO: Get rid of this |
| 2137 | fn makePointerConstant( |
| 2138 | self: *DeclGen, |
| 2139 | section: *SpvSection, |
| 2140 | ptr_ty_ref: SpvType.Ref, |
| 2141 | ptr_id: IdRef, |
| 2142 | ) !IdRef { |
| 2143 | const result_id = self.spv.allocId(); |
| 2144 | try section.emitSpecConstantOp(self.spv.gpa, .OpInBoundsAccessChain, .{ |
| 2145 | .id_result_type = self.typeId(ptr_ty_ref), |
| 2146 | .id_result = result_id, |
| 2147 | .base = ptr_id, |
| 2148 | }); |
| 2149 | return result_id; |
| 2150 | } |
| 2151 | |
| 2152 | fn variable( |
| 2153 | self: *DeclGen, |
| 2154 | comptime context: enum { function, global }, |
| 2155 | result_id: IdRef, |
| 2156 | ptr_ty_ref: SpvType.Ref, |
| 2157 | initializer: ?IdRef, |
| 2158 | ) !void { |
| 2159 | const storage_class = self.spv.typeRefType(ptr_ty_ref).payload(.pointer).storage_class; |
| 2160 | const actual_storage_class = switch (storage_class) { |
| 2161 | .Generic => switch (context) { |
| 2162 | .function => .Function, |
| 2163 | .global => .CrossWorkgroup, |
| 2164 | }, |
| 2165 | else => storage_class, |
| 2166 | }; |
| 2167 | const actual_ptr_ty_ref = switch (storage_class) { |
| 2168 | .Generic => try self.spv.changePtrStorageClass(ptr_ty_ref, actual_storage_class), |
| 2169 | else => ptr_ty_ref, |
| 2170 | }; |
| 2171 | const alloc_result_id = switch (storage_class) { |
| 2172 | .Generic => self.spv.allocId(), |
| 2173 | else => result_id, |
| 2174 | }; |
| 2175 | |
| 2176 | const section = switch (actual_storage_class) { |
| 2177 | .Generic => unreachable, |
| 2178 | // SPIR-V requires that OpVariable declarations for locals go into the first block, so we are just going to |
| 2179 | // directly generate them into func.prologue instead of the body. |
| 2180 | .Function => &self.func.prologue, |
| 2181 | else => &self.spv.sections.types_globals_constants, |
| 2182 | }; |
| 2183 | try section.emit(self.spv.gpa, .OpVariable, .{ |
| 2184 | .id_result_type = self.typeId(actual_ptr_ty_ref), |
| 2185 | .id_result = alloc_result_id, |
| 2186 | .storage_class = actual_storage_class, |
| 2187 | .initializer = initializer, |
| 2188 | }); |
| 2189 | |
| 2190 | if (storage_class != .Generic) { |
| 2191 | return; |
| 2192 | } |
| 2193 | |
| 2194 | // Now we need to convert the pointer. |
| 2195 | // If this is a function local, we need to perform the conversion at runtime. Otherwise, we can do |
| 2196 | // it ahead of time using OpSpecConstantOp. |
| 2197 | switch (actual_storage_class) { |
| 2198 | .Function => try self.func.body.emit(self.spv.gpa, .OpPtrCastToGeneric, .{ |
| 2199 | .id_result_type = self.typeId(ptr_ty_ref), |
| 2200 | .id_result = result_id, |
| 2201 | .pointer = alloc_result_id, |
| 2202 | }), |
| 2203 | // TODO: Can we do without this cast or move it to runtime? |
| 2204 | else => { |
| 2205 | const const_ptr_id = try self.makePointerConstant(section, actual_ptr_ty_ref, alloc_result_id); |
| 2206 | try section.emitSpecConstantOp(self.spv.gpa, .OpPtrCastToGeneric, .{ |
| 2207 | .id_result_type = self.typeId(ptr_ty_ref), |
| 2208 | .id_result = result_id, |
| 2209 | .pointer = const_ptr_id, |
| 2210 | }); |
| 2211 | }, |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | fn airAlloc(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 2216 | if (self.liveness.isUnused(inst)) return null; |
| 2217 | const ty = self.air.typeOfIndex(inst); |
| 2218 | const result_ty_ref = try self.resolveType(ty, .direct); |
| 2219 | const result_id = self.spv.allocId(); |
| 2220 | try self.variable(.function, result_id, result_ty_ref, null); |
| 2221 | return result_id; |
| 857 | 2222 | } |
| 858 | 2223 | |
| 859 | 2224 | fn airArg(self: *DeclGen) IdRef { |
| ... | ... | @@ -873,7 +2238,7 @@ pub const DeclGen = struct { |
| 873 | 2238 | var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.gpa, 4); |
| 874 | 2239 | |
| 875 | 2240 | try self.blocks.putNoClobber(self.gpa, inst, .{ |
| 876 | | .label_id = label_id.toRef(), |
| 2241 | .label_id = label_id, |
| 877 | 2242 | .incoming_blocks = &incoming_blocks, |
| 878 | 2243 | }); |
| 879 | 2244 | defer { |
| ... | ... | @@ -890,7 +2255,7 @@ pub const DeclGen = struct { |
| 890 | 2255 | try self.beginSpvBlock(label_id); |
| 891 | 2256 | |
| 892 | 2257 | // If this block didn't produce a value, simply return here. |
| 893 | | if (!ty.hasRuntimeBits()) |
| 2258 | if (!ty.hasRuntimeBitsIgnoreComptime()) |
| 894 | 2259 | return null; |
| 895 | 2260 | |
| 896 | 2261 | // Combine the result from the blocks using the Phi instruction. |
| ... | ... | @@ -908,7 +2273,7 @@ pub const DeclGen = struct { |
| 908 | 2273 | self.func.body.writeOperand(spec.PairIdRefIdRef, .{ incoming.break_value_id, incoming.src_label_id }); |
| 909 | 2274 | } |
| 910 | 2275 | |
| 911 | | return result_id.toRef(); |
| 2276 | return result_id; |
| 912 | 2277 | } |
| 913 | 2278 | |
| 914 | 2279 | fn airBr(self: *DeclGen, inst: Air.Inst.Index) !void { |
| ... | ... | @@ -941,8 +2306,8 @@ pub const DeclGen = struct { |
| 941 | 2306 | |
| 942 | 2307 | try self.func.body.emit(self.spv.gpa, .OpBranchConditional, .{ |
| 943 | 2308 | .condition = condition_id, |
| 944 | | .true_label = then_label_id.toRef(), |
| 945 | | .false_label = else_label_id.toRef(), |
| 2309 | .true_label = then_label_id, |
| 2310 | .false_label = else_label_id, |
| 946 | 2311 | }); |
| 947 | 2312 | |
| 948 | 2313 | try self.beginSpvBlock(then_label_id); |
| ... | ... | @@ -961,26 +2326,80 @@ pub const DeclGen = struct { |
| 961 | 2326 | }); |
| 962 | 2327 | } |
| 963 | 2328 | |
| 964 | | fn airLoad(self: *DeclGen, inst: Air.Inst.Index) !IdRef { |
| 2329 | fn airLoad(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 965 | 2330 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| 966 | | const operand_id = try self.resolve(ty_op.operand); |
| 967 | | const ty = self.air.typeOfIndex(inst); |
| 2331 | const ptr_ty = self.air.typeOf(ty_op.operand); |
| 2332 | const operand = try self.resolve(ty_op.operand); |
| 2333 | if (!ptr_ty.isVolatilePtr() and self.liveness.isUnused(inst)) return null; |
| 968 | 2334 | |
| 969 | | const result_type_id = try self.resolveTypeId(ty); |
| 970 | | const result_id = self.spv.allocId(); |
| 2335 | return try self.load(ptr_ty, operand); |
| 2336 | } |
| 971 | 2337 | |
| 2338 | fn load(self: *DeclGen, ptr_ty: Type, ptr: IdRef) !IdRef { |
| 2339 | const value_ty = ptr_ty.childType(); |
| 2340 | const direct_result_ty_ref = try self.resolveType(value_ty, .direct); |
| 2341 | const indirect_result_ty_ref = try self.resolveType(value_ty, .indirect); |
| 2342 | const result_id = self.spv.allocId(); |
| 972 | 2343 | const access = spec.MemoryAccess.Extended{ |
| 973 | | .Volatile = ty.isVolatilePtr(), |
| 2344 | .Volatile = ptr_ty.isVolatilePtr(), |
| 974 | 2345 | }; |
| 975 | | |
| 976 | 2346 | try self.func.body.emit(self.spv.gpa, .OpLoad, .{ |
| 977 | | .id_result_type = result_type_id, |
| 2347 | .id_result_type = self.typeId(indirect_result_ty_ref), |
| 978 | 2348 | .id_result = result_id, |
| 979 | | .pointer = operand_id, |
| 2349 | .pointer = ptr, |
| 980 | 2350 | .memory_access = access, |
| 981 | 2351 | }); |
| 2352 | if (value_ty.zigTypeTag() == .Bool) { |
| 2353 | // Convert indirect bool to direct bool |
| 2354 | const zero_id = try self.constInt(indirect_result_ty_ref, 0); |
| 2355 | const casted_result_id = self.spv.allocId(); |
| 2356 | try self.func.body.emit(self.spv.gpa, .OpINotEqual, .{ |
| 2357 | .id_result_type = self.typeId(direct_result_ty_ref), |
| 2358 | .id_result = casted_result_id, |
| 2359 | .operand_1 = result_id, |
| 2360 | .operand_2 = zero_id, |
| 2361 | }); |
| 2362 | return casted_result_id; |
| 2363 | } |
| 2364 | return result_id; |
| 2365 | } |
| 2366 | |
| 2367 | fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 2368 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 2369 | const ptr_ty = self.air.typeOf(bin_op.lhs); |
| 2370 | const ptr = try self.resolve(bin_op.lhs); |
| 2371 | const value = try self.resolve(bin_op.rhs); |
| 982 | 2372 | |
| 983 | | return result_id.toRef(); |
| 2373 | try self.store(ptr_ty, ptr, value); |
| 2374 | } |
| 2375 | |
| 2376 | fn store(self: *DeclGen, ptr_ty: Type, ptr: IdRef, value: IdRef) !void { |
| 2377 | const value_ty = ptr_ty.childType(); |
| 2378 | const converted_value = switch (value_ty.zigTypeTag()) { |
| 2379 | .Bool => blk: { |
| 2380 | const indirect_bool_ty_ref = try self.resolveType(value_ty, .indirect); |
| 2381 | const result_id = self.spv.allocId(); |
| 2382 | const zero = try self.constInt(indirect_bool_ty_ref, 0); |
| 2383 | const one = try self.constInt(indirect_bool_ty_ref, 1); |
| 2384 | try self.func.body.emit(self.spv.gpa, .OpSelect, .{ |
| 2385 | .id_result_type = self.typeId(indirect_bool_ty_ref), |
| 2386 | .id_result = result_id, |
| 2387 | .condition = value, |
| 2388 | .object_1 = one, |
| 2389 | .object_2 = zero, |
| 2390 | }); |
| 2391 | break :blk result_id; |
| 2392 | }, |
| 2393 | else => value, |
| 2394 | }; |
| 2395 | const access = spec.MemoryAccess.Extended{ |
| 2396 | .Volatile = ptr_ty.isVolatilePtr(), |
| 2397 | }; |
| 2398 | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| 2399 | .pointer = ptr, |
| 2400 | .object = converted_value, |
| 2401 | .memory_access = access, |
| 2402 | }); |
| 984 | 2403 | } |
| 985 | 2404 | |
| 986 | 2405 | fn airLoop(self: *DeclGen, inst: Air.Inst.Index) !void { |
| ... | ... | @@ -990,13 +2409,13 @@ pub const DeclGen = struct { |
| 990 | 2409 | const loop_label_id = self.spv.allocId(); |
| 991 | 2410 | |
| 992 | 2411 | // Jump to the loop entry point |
| 993 | | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id.toRef() }); |
| 2412 | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id }); |
| 994 | 2413 | |
| 995 | 2414 | // TODO: Look into OpLoopMerge. |
| 996 | 2415 | try self.beginSpvBlock(loop_label_id); |
| 997 | 2416 | try self.genBody(body); |
| 998 | 2417 | |
| 999 | | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id.toRef() }); |
| 2418 | try self.func.body.emit(self.spv.gpa, .OpBranch, .{ .target_label = loop_label_id }); |
| 1000 | 2419 | } |
| 1001 | 2420 | |
| 1002 | 2421 | fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void { |
| ... | ... | @@ -1010,23 +2429,138 @@ pub const DeclGen = struct { |
| 1010 | 2429 | } |
| 1011 | 2430 | } |
| 1012 | 2431 | |
| 1013 | | fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 1014 | | const bin_op = self.air.instructions.items(.data)[inst].bin_op; |
| 1015 | | const dst_ptr_id = try self.resolve(bin_op.lhs); |
| 1016 | | const src_val_id = try self.resolve(bin_op.rhs); |
| 1017 | | const lhs_ty = self.air.typeOf(bin_op.lhs); |
| 2432 | fn airRetLoad(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 2433 | const un_op = self.air.instructions.items(.data)[inst].un_op; |
| 2434 | const ptr_ty = self.air.typeOf(un_op); |
| 2435 | const ret_ty = ptr_ty.childType(); |
| 1018 | 2436 | |
| 1019 | | const access = spec.MemoryAccess.Extended{ |
| 1020 | | .Volatile = lhs_ty.isVolatilePtr(), |
| 1021 | | }; |
| 2437 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { |
| 2438 | try self.func.body.emit(self.spv.gpa, .OpReturn, {}); |
| 2439 | return; |
| 2440 | } |
| 1022 | 2441 | |
| 1023 | | try self.func.body.emit(self.spv.gpa, .OpStore, .{ |
| 1024 | | .pointer = dst_ptr_id, |
| 1025 | | .object = src_val_id, |
| 1026 | | .memory_access = access, |
| 2442 | const ptr = try self.resolve(un_op); |
| 2443 | const value = try self.load(ptr_ty, ptr); |
| 2444 | try self.func.body.emit(self.spv.gpa, .OpReturnValue, .{ |
| 2445 | .value = value, |
| 1027 | 2446 | }); |
| 1028 | 2447 | } |
| 1029 | 2448 | |
| 2449 | fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 2450 | const target = self.getTarget(); |
| 2451 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 2452 | const cond = try self.resolve(pl_op.operand); |
| 2453 | const cond_ty = self.air.typeOf(pl_op.operand); |
| 2454 | const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload); |
| 2455 | |
| 2456 | const cond_words: u32 = switch (cond_ty.zigTypeTag()) { |
| 2457 | .Int => blk: { |
| 2458 | const bits = cond_ty.intInfo(target).bits; |
| 2459 | const backing_bits = self.backingIntBits(bits) orelse { |
| 2460 | return self.todo("implement composite int switch", .{}); |
| 2461 | }; |
| 2462 | break :blk if (backing_bits <= 32) @as(u32, 1) else 2; |
| 2463 | }, |
| 2464 | .Enum => blk: { |
| 2465 | var buffer: Type.Payload.Bits = undefined; |
| 2466 | const int_ty = cond_ty.intTagType(&buffer); |
| 2467 | const int_info = int_ty.intInfo(target); |
| 2468 | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 2469 | return self.todo("implement composite int switch", .{}); |
| 2470 | }; |
| 2471 | break :blk if (backing_bits <= 32) @as(u32, 1) else 2; |
| 2472 | }, |
| 2473 | else => return self.todo("implement switch for type {s}", .{@tagName(cond_ty.zigTypeTag())}), // TODO: Figure out which types apply here, and work around them as we can only do integers. |
| 2474 | }; |
| 2475 | |
| 2476 | const num_cases = switch_br.data.cases_len; |
| 2477 | |
| 2478 | // Compute the total number of arms that we need. |
| 2479 | // Zig switches are grouped by condition, so we need to loop through all of them |
| 2480 | const num_conditions = blk: { |
| 2481 | var extra_index: usize = switch_br.end; |
| 2482 | var case_i: u32 = 0; |
| 2483 | var num_conditions: u32 = 0; |
| 2484 | while (case_i < num_cases) : (case_i += 1) { |
| 2485 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 2486 | const case_body = self.air.extra[case.end + case.data.items_len ..][0..case.data.body_len]; |
| 2487 | extra_index = case.end + case.data.items_len + case_body.len; |
| 2488 | num_conditions += case.data.items_len; |
| 2489 | } |
| 2490 | break :blk num_conditions; |
| 2491 | }; |
| 2492 | |
| 2493 | // First, pre-allocate the labels for the cases. |
| 2494 | const first_case_label = self.spv.allocIds(num_cases); |
| 2495 | // We always need the default case - if zig has none, we will generate unreachable there. |
| 2496 | const default = self.spv.allocId(); |
| 2497 | |
| 2498 | // Emit the instruction before generating the blocks. |
| 2499 | try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions); |
| 2500 | self.func.body.writeOperand(IdRef, cond); |
| 2501 | self.func.body.writeOperand(IdRef, default); |
| 2502 | |
| 2503 | // Emit each of the cases |
| 2504 | { |
| 2505 | var extra_index: usize = switch_br.end; |
| 2506 | var case_i: u32 = 0; |
| 2507 | while (case_i < num_cases) : (case_i += 1) { |
| 2508 | // SPIR-V needs a literal here, which' width depends on the case condition. |
| 2509 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 2510 | const items = @ptrCast([]const Air.Inst.Ref, self.air.extra[case.end..][0..case.data.items_len]); |
| 2511 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 2512 | extra_index = case.end + case.data.items_len + case_body.len; |
| 2513 | |
| 2514 | const label = IdRef{ .id = first_case_label.id + case_i }; |
| 2515 | |
| 2516 | for (items) |item| { |
| 2517 | const value = self.air.value(item) orelse { |
| 2518 | return self.todo("switch on runtime value???", .{}); |
| 2519 | }; |
| 2520 | const int_val = switch (cond_ty.zigTypeTag()) { |
| 2521 | .Int => if (cond_ty.isSignedInt()) @bitCast(u64, value.toSignedInt(target)) else value.toUnsignedInt(target), |
| 2522 | .Enum => blk: { |
| 2523 | var int_buffer: Value.Payload.U64 = undefined; |
| 2524 | // TODO: figure out of cond_ty is correct (something with enum literals) |
| 2525 | break :blk value.enumToInt(cond_ty, &int_buffer).toUnsignedInt(target); // TODO: composite integer constants |
| 2526 | }, |
| 2527 | else => unreachable, |
| 2528 | }; |
| 2529 | const int_lit: spec.LiteralContextDependentNumber = switch (cond_words) { |
| 2530 | 1 => .{ .uint32 = @intCast(u32, int_val) }, |
| 2531 | 2 => .{ .uint64 = int_val }, |
| 2532 | else => unreachable, |
| 2533 | }; |
| 2534 | self.func.body.writeOperand(spec.LiteralContextDependentNumber, int_lit); |
| 2535 | self.func.body.writeOperand(IdRef, label); |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | // Now, finally, we can start emitting each of the cases. |
| 2541 | var extra_index: usize = switch_br.end; |
| 2542 | var case_i: u32 = 0; |
| 2543 | while (case_i < num_cases) : (case_i += 1) { |
| 2544 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); |
| 2545 | const items = @ptrCast([]const Air.Inst.Ref, self.air.extra[case.end..][0..case.data.items_len]); |
| 2546 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 2547 | extra_index = case.end + case.data.items_len + case_body.len; |
| 2548 | |
| 2549 | const label = IdResult{ .id = first_case_label.id + case_i }; |
| 2550 | |
| 2551 | try self.beginSpvBlock(label); |
| 2552 | try self.genBody(case_body); |
| 2553 | } |
| 2554 | |
| 2555 | const else_body = self.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 2556 | try self.beginSpvBlock(default); |
| 2557 | if (else_body.len != 0) { |
| 2558 | try self.genBody(else_body); |
| 2559 | } else { |
| 2560 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 1030 | 2564 | fn airUnreach(self: *DeclGen) !void { |
| 1031 | 2565 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 1032 | 2566 | } |
| ... | ... | @@ -1158,4 +2692,47 @@ pub const DeclGen = struct { |
| 1158 | 2692 | |
| 1159 | 2693 | return null; |
| 1160 | 2694 | } |
| 2695 | |
| 2696 | fn airCall(self: *DeclGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !?IdRef { |
| 2697 | _ = modifier; |
| 2698 | |
| 2699 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; |
| 2700 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 2701 | const args = @ptrCast([]const Air.Inst.Ref, self.air.extra[extra.end..][0..extra.data.args_len]); |
| 2702 | const callee_ty = self.air.typeOf(pl_op.operand); |
| 2703 | const zig_fn_ty = switch (callee_ty.zigTypeTag()) { |
| 2704 | .Fn => callee_ty, |
| 2705 | .Pointer => return self.fail("cannot call function pointers", .{}), |
| 2706 | else => unreachable, |
| 2707 | }; |
| 2708 | const fn_info = zig_fn_ty.fnInfo(); |
| 2709 | const return_type = fn_info.return_type; |
| 2710 | |
| 2711 | const result_type_id = try self.resolveTypeId(return_type); |
| 2712 | const result_id = self.spv.allocId(); |
| 2713 | const callee_id = try self.resolve(pl_op.operand); |
| 2714 | |
| 2715 | try self.func.body.emitRaw(self.spv.gpa, .OpFunctionCall, 3 + args.len); |
| 2716 | self.func.body.writeOperand(spec.IdResultType, result_type_id); |
| 2717 | self.func.body.writeOperand(spec.IdResult, result_id); |
| 2718 | self.func.body.writeOperand(spec.IdRef, callee_id); |
| 2719 | |
| 2720 | for (args) |arg| { |
| 2721 | const arg_id = try self.resolve(arg); |
| 2722 | const arg_ty = self.air.typeOf(arg); |
| 2723 | if (!arg_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 2724 | |
| 2725 | self.func.body.writeOperand(spec.IdRef, arg_id); |
| 2726 | } |
| 2727 | |
| 2728 | if (return_type.isNoReturn()) { |
| 2729 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 2730 | } |
| 2731 | |
| 2732 | if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBitsIgnoreComptime()) { |
| 2733 | return null; |
| 2734 | } |
| 2735 | |
| 2736 | return result_id; |
| 2737 | } |
| 1161 | 2738 | }; |