| ... | ... | @@ -133,6 +133,16 @@ pub const DeclGen = struct { |
| 133 | 133 | class: Class, |
| 134 | 134 | }; |
| 135 | 135 | |
| 136 | /// Data can be lowered into in two basic representations: indirect, which is when |
| 137 | /// a type is stored in memory, and direct, which is how a type is stored when its |
| 138 | /// a direct SPIR-V value. |
| 139 | const Repr = enum { |
| 140 | /// A SPIR-V value as it would be used in operations. |
| 141 | direct, |
| 142 | /// A SPIR-V value as it is stored in memory. |
| 143 | indirect, |
| 144 | }; |
| 145 | |
| 136 | 146 | /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, |
| 137 | 147 | /// only set when `gen` is called. |
| 138 | 148 | pub fn init( |
| ... | ... | @@ -215,7 +225,7 @@ pub const DeclGen = struct { |
| 215 | 225 | /// Fetch the result-id for a previously generated instruction or constant. |
| 216 | 226 | fn resolve(self: *DeclGen, inst: Air.Inst.Ref) !IdRef { |
| 217 | 227 | if (self.air.value(inst)) |val| { |
| 218 | | return self.genConstant(self.air.typeOf(inst), val); |
| 228 | return self.genConstant(self.air.typeOf(inst), val, .direct); |
| 219 | 229 | } |
| 220 | 230 | const index = Air.refToIndex(inst).?; |
| 221 | 231 | return self.inst_results.get(index).?; // Assertion means instruction does not dominate usage. |
| ... | ... | @@ -329,9 +339,29 @@ pub const DeclGen = struct { |
| 329 | 339 | }; |
| 330 | 340 | } |
| 331 | 341 | |
| 342 | fn constInt(self: *DeclGen, ty_ref: SpvType.Ref, value: anytype) !IdRef { |
| 343 | const ty = self.spv.typeRefType(ty_ref); |
| 344 | const ty_id = self.typeId(ty_ref); |
| 345 | |
| 346 | const literal: spec.LiteralContextDependentNumber = switch (ty.intSignedness()) { |
| 347 | .signed => switch (ty.intFloatBits()) { |
| 348 | 1...32 => .{ .int32 = @intCast(i32, value) }, |
| 349 | 33...64 => .{ .int64 = @intCast(i64, value) }, |
| 350 | else => unreachable, // TODO: composite integer literals |
| 351 | }, |
| 352 | .unsigned => switch (ty.intFloatBits()) { |
| 353 | 1...32 => .{ .uint32 = @intCast(u32, value) }, |
| 354 | 33...64 => .{ .uint64 = @intCast(u64, value) }, |
| 355 | else => unreachable, |
| 356 | }, |
| 357 | }; |
| 358 | |
| 359 | return try self.spv.emitConstant(ty_id, literal); |
| 360 | } |
| 361 | |
| 332 | 362 | /// Generate a constant representing `val`. |
| 333 | 363 | /// TODO: Deduplication? |
| 334 | | fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!IdRef { |
| 364 | fn genConstant(self: *DeclGen, ty: Type, val: Value, repr: Repr) Error!IdRef { |
| 335 | 365 | if (ty.zigTypeTag() == .Fn) { |
| 336 | 366 | const fn_decl_index = switch (val.tag()) { |
| 337 | 367 | .extern_fn => val.castTag(.extern_fn).?.data.owner_decl, |
| ... | ... | @@ -345,56 +375,37 @@ pub const DeclGen = struct { |
| 345 | 375 | |
| 346 | 376 | const target = self.getTarget(); |
| 347 | 377 | const section = &self.spv.sections.types_globals_constants; |
| 348 | | const result_id = self.spv.allocId(); |
| 349 | | const result_type_id = try self.resolveTypeId(ty); |
| 378 | const result_ty_ref = try self.resolveType(ty, repr); |
| 379 | const result_ty_id = self.typeId(result_ty_ref); |
| 350 | 380 | |
| 351 | 381 | if (val.isUndef()) { |
| 352 | | try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_type_id, .id_result = result_id }); |
| 382 | const result_id = self.spv.allocId(); |
| 383 | try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_ty_id, .id_result = result_id }); |
| 353 | 384 | return result_id; |
| 354 | 385 | } |
| 355 | 386 | |
| 356 | 387 | switch (ty.zigTypeTag()) { |
| 357 | 388 | .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 | | }); |
| 389 | const int_bits = if (ty.isSignedInt()) @bitCast(u64, val.toSignedInt(target)) else val.toUnsignedInt(target); |
| 390 | return self.constInt(result_ty_ref, int_bits); |
| 384 | 391 | }, |
| 385 | | .Bool => { |
| 386 | | const operands = .{ .id_result_type = result_type_id, .id_result = result_id }; |
| 387 | | if (val.toBool()) { |
| 388 | | try section.emit(self.spv.gpa, .OpConstantTrue, operands); |
| 389 | | } else { |
| 390 | | try section.emit(self.spv.gpa, .OpConstantFalse, operands); |
| 391 | | } |
| 392 | .Bool => switch (repr) { |
| 393 | .direct => { |
| 394 | const result_id = self.spv.allocId(); |
| 395 | const operands = .{ .id_result_type = result_ty_id, .id_result = result_id }; |
| 396 | if (val.toBool()) { |
| 397 | try section.emit(self.spv.gpa, .OpConstantTrue, operands); |
| 398 | } else { |
| 399 | try section.emit(self.spv.gpa, .OpConstantFalse, operands); |
| 400 | } |
| 401 | return result_id; |
| 402 | }, |
| 403 | .indirect => return try self.constInt(result_ty_ref, @boolToInt(val.toBool())), |
| 392 | 404 | }, |
| 393 | 405 | .Float => { |
| 394 | 406 | // At this point we are guaranteed that the target floating point type is supported, otherwise the function |
| 395 | 407 | // would have exited at resolveTypeId(ty). |
| 396 | | |
| 397 | | const value: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { |
| 408 | const literal: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { |
| 398 | 409 | // Prevent upcasting to f32 by bitcasting and writing as a uint32. |
| 399 | 410 | 16 => .{ .uint32 = @bitCast(u16, val.toFloat(f16)) }, |
| 400 | 411 | 32 => .{ .float32 = val.toFloat(f32) }, |
| ... | ... | @@ -404,11 +415,7 @@ pub const DeclGen = struct { |
| 404 | 415 | else => unreachable, |
| 405 | 416 | }; |
| 406 | 417 | |
| 407 | | try section.emit(self.spv.gpa, .OpConstant, .{ |
| 408 | | .id_result_type = result_type_id, |
| 409 | | .id_result = result_id, |
| 410 | | .value = value, |
| 411 | | }); |
| 418 | return try self.spv.emitConstant(result_ty_id, literal); |
| 412 | 419 | }, |
| 413 | 420 | .Array => switch (val.tag()) { |
| 414 | 421 | .aggregate => { // todo: combine with Vector |
| ... | ... | @@ -417,14 +424,16 @@ pub const DeclGen = struct { |
| 417 | 424 | const len = @intCast(u32, ty.arrayLenIncludingSentinel()); // TODO: limit spir-v to 32 bit arrays in a more elegant way. |
| 418 | 425 | const constituents = try self.spv.gpa.alloc(IdRef, len); |
| 419 | 426 | defer self.spv.gpa.free(constituents); |
| 420 | | for (elem_vals[0..len]) |elem_val, i| { |
| 421 | | constituents[i] = try self.genConstant(elem_ty, elem_val); |
| 427 | for (elem_vals[0..len], 0..) |elem_val, i| { |
| 428 | constituents[i] = try self.genConstant(elem_ty, elem_val, repr); |
| 422 | 429 | } |
| 430 | const result_id = self.spv.allocId(); |
| 423 | 431 | try section.emit(self.spv.gpa, .OpConstantComposite, .{ |
| 424 | | .id_result_type = result_type_id, |
| 432 | .id_result_type = result_ty_id, |
| 425 | 433 | .id_result = result_id, |
| 426 | 434 | .constituents = constituents, |
| 427 | 435 | }); |
| 436 | return result_id; |
| 428 | 437 | }, |
| 429 | 438 | .repeated => { |
| 430 | 439 | const elem_val = val.castTag(.repeated).?.data; |
| ... | ... | @@ -433,18 +442,20 @@ pub const DeclGen = struct { |
| 433 | 442 | const constituents = try self.spv.gpa.alloc(IdRef, len); |
| 434 | 443 | defer self.spv.gpa.free(constituents); |
| 435 | 444 | |
| 436 | | const elem_val_id = try self.genConstant(elem_ty, elem_val); |
| 445 | const elem_val_id = try self.genConstant(elem_ty, elem_val, repr); |
| 437 | 446 | for (constituents[0..len]) |*elem| { |
| 438 | 447 | elem.* = elem_val_id; |
| 439 | 448 | } |
| 440 | 449 | if (ty.sentinel()) |sentinel| { |
| 441 | | constituents[len] = try self.genConstant(elem_ty, sentinel); |
| 450 | constituents[len] = try self.genConstant(elem_ty, sentinel, repr); |
| 442 | 451 | } |
| 452 | const result_id = self.spv.allocId(); |
| 443 | 453 | try section.emit(self.spv.gpa, .OpConstantComposite, .{ |
| 444 | | .id_result_type = result_type_id, |
| 454 | .id_result_type = result_ty_id, |
| 445 | 455 | .id_result = result_id, |
| 446 | 456 | .constituents = constituents, |
| 447 | 457 | }); |
| 458 | return result_id; |
| 448 | 459 | }, |
| 449 | 460 | else => return self.todo("array constant with tag {s}", .{@tagName(val.tag())}), |
| 450 | 461 | }, |
| ... | ... | @@ -457,39 +468,22 @@ pub const DeclGen = struct { |
| 457 | 468 | const elem_refs = try self.gpa.alloc(IdRef, vector_len); |
| 458 | 469 | defer self.gpa.free(elem_refs); |
| 459 | 470 | for (elem_refs, 0..) |*elem, i| { |
| 460 | | elem.* = try self.genConstant(elem_ty, elem_vals[i]); |
| 471 | elem.* = try self.genConstant(elem_ty, elem_vals[i], repr); |
| 461 | 472 | } |
| 473 | const result_id = self.spv.allocId(); |
| 462 | 474 | try section.emit(self.spv.gpa, .OpConstantComposite, .{ |
| 463 | | .id_result_type = result_type_id, |
| 475 | .id_result_type = result_ty_id, |
| 464 | 476 | .id_result = result_id, |
| 465 | 477 | .constituents = elem_refs, |
| 466 | 478 | }); |
| 479 | return result_id; |
| 467 | 480 | }, |
| 468 | 481 | else => return self.todo("vector constant with tag {s}", .{@tagName(val.tag())}), |
| 469 | 482 | }, |
| 470 | 483 | .Enum => { |
| 471 | | var ty_buffer: Type.Payload.Bits = undefined; |
| 472 | | const int_ty = ty.intTagType(&ty_buffer); |
| 473 | | const int_info = int_ty.intInfo(target); |
| 474 | | |
| 475 | | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 476 | | return self.todo("implement composite int constants for {}", .{int_ty.fmtDebug()}); |
| 477 | | }; |
| 478 | | |
| 479 | 484 | var int_buffer: Value.Payload.U64 = undefined; |
| 480 | 485 | const int_val = val.enumToInt(ty, &int_buffer).toUnsignedInt(target); // TODO: composite integer constants |
| 481 | | |
| 482 | | const value: spec.LiteralContextDependentNumber = switch (backing_bits) { |
| 483 | | 1...32 => .{ .uint32 = @truncate(u32, int_val) }, |
| 484 | | 33...64 => .{ .uint64 = int_val }, |
| 485 | | else => unreachable, |
| 486 | | }; |
| 487 | | |
| 488 | | try section.emit(self.spv.gpa, .OpConstant, .{ |
| 489 | | .id_result_type = result_type_id, |
| 490 | | .id_result = result_id, |
| 491 | | .value = value, |
| 492 | | }); |
| 486 | return self.constInt(result_ty_ref, int_val); |
| 493 | 487 | }, |
| 494 | 488 | .Struct => { |
| 495 | 489 | const constituents = if (ty.isSimpleTupleOrAnonStruct()) blk: { |
| ... | ... | @@ -498,10 +492,10 @@ pub const DeclGen = struct { |
| 498 | 492 | errdefer self.spv.gpa.free(constituents); |
| 499 | 493 | |
| 500 | 494 | var member_index: usize = 0; |
| 501 | | for (tuple.types) |field_ty, i| { |
| 495 | for (tuple.types, 0..) |field_ty, i| { |
| 502 | 496 | const field_val = tuple.values[i]; |
| 503 | 497 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBits()) continue; |
| 504 | | constituents[member_index] = try self.genConstant(field_ty, field_val); |
| 498 | constituents[member_index] = try self.genConstant(field_ty, field_val, repr); |
| 505 | 499 | member_index += 1; |
| 506 | 500 | } |
| 507 | 501 | |
| ... | ... | @@ -517,9 +511,9 @@ pub const DeclGen = struct { |
| 517 | 511 | const constituents = try self.spv.gpa.alloc(IdRef, struct_ty.fields.count()); |
| 518 | 512 | errdefer self.spv.gpa.free(constituents); |
| 519 | 513 | var member_index: usize = 0; |
| 520 | | for (struct_ty.fields.values()) |field, i| { |
| 514 | for (struct_ty.fields.values(), 0..) |field, i| { |
| 521 | 515 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; |
| 522 | | constituents[member_index] = try self.genConstant(field.ty, field_vals[i]); |
| 516 | constituents[member_index] = try self.genConstant(field.ty, field_vals[i], repr); |
| 523 | 517 | member_index += 1; |
| 524 | 518 | } |
| 525 | 519 | |
| ... | ... | @@ -527,24 +521,28 @@ pub const DeclGen = struct { |
| 527 | 521 | }; |
| 528 | 522 | defer self.spv.gpa.free(constituents); |
| 529 | 523 | |
| 524 | const result_id = self.spv.allocId(); |
| 530 | 525 | try section.emit(self.spv.gpa, .OpConstantComposite, .{ |
| 531 | | .id_result_type = result_type_id, |
| 526 | .id_result_type = result_ty_id, |
| 532 | 527 | .id_result = result_id, |
| 533 | 528 | .constituents = constituents, |
| 534 | 529 | }); |
| 530 | return result_id; |
| 535 | 531 | }, |
| 536 | 532 | .Void => unreachable, |
| 537 | 533 | .Fn => unreachable, |
| 538 | 534 | else => return self.todo("constant generation of type {s}: {}", .{ @tagName(ty.zigTypeTag()), ty.fmtDebug() }), |
| 539 | 535 | } |
| 540 | | |
| 541 | | return result_id; |
| 542 | 536 | } |
| 543 | 537 | |
| 544 | 538 | /// Turn a Zig type into a SPIR-V Type, and return its type result-id. |
| 545 | 539 | fn resolveTypeId(self: *DeclGen, ty: Type) !IdResultType { |
| 546 | | const type_ref = try self.resolveType(ty); |
| 547 | | return self.spv.typeResultId(type_ref); |
| 540 | const type_ref = try self.resolveType(ty, .direct); |
| 541 | return self.typeId(type_ref); |
| 542 | } |
| 543 | |
| 544 | fn typeId(self: *DeclGen, ty_ref: SpvType.Ref) IdRef { |
| 545 | return self.spv.typeId(ty_ref); |
| 548 | 546 | } |
| 549 | 547 | |
| 550 | 548 | /// Create an integer type suitable for storing at least 'bits' bits. |
| ... | ... | @@ -576,19 +574,20 @@ pub const DeclGen = struct { |
| 576 | 574 | |
| 577 | 575 | fn simpleStructTypeId(self: *DeclGen, members: []const SpvType.Payload.Struct.Member) !IdResultType { |
| 578 | 576 | const type_ref = try self.simpleStructType(members); |
| 579 | | return self.spv.typeResultId(type_ref); |
| 577 | return self.typeId(type_ref); |
| 580 | 578 | } |
| 581 | 579 | |
| 582 | 580 | /// Turn a Zig type into a SPIR-V Type, and return a reference to it. |
| 583 | | fn resolveType(self: *DeclGen, ty: Type) Error!SpvType.Ref { |
| 581 | fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!SpvType.Ref { |
| 584 | 582 | const target = self.getTarget(); |
| 585 | 583 | switch (ty.zigTypeTag()) { |
| 586 | 584 | .Void, .NoReturn => return try self.spv.resolveType(SpvType.initTag(.void)), |
| 587 | | .Bool => { |
| 585 | .Bool => switch (repr) { |
| 586 | .direct => return try self.spv.resolveType(SpvType.initTag(.bool)), |
| 588 | 587 | // SPIR-V booleans are opaque, which is fine for operations, but they cant be stored. |
| 589 | 588 | // This function returns the *stored* type, for values directly we convert this into a bool when |
| 590 | 589 | // it is loaded, and convert it back to this type when stored. |
| 591 | | return try self.intType(.unsigned, 1); |
| 590 | .indirect => return try self.intType(.unsigned, 1), |
| 592 | 591 | }, |
| 593 | 592 | .Int => { |
| 594 | 593 | const int_info = ty.intInfo(target); |
| ... | ... | @@ -596,9 +595,8 @@ pub const DeclGen = struct { |
| 596 | 595 | }, |
| 597 | 596 | .Enum => { |
| 598 | 597 | var buffer: Type.Payload.Bits = undefined; |
| 599 | | const int_ty = ty.intTagType(&buffer); |
| 600 | | const int_info = int_ty.intInfo(target); |
| 601 | | return try self.intType(.unsigned, int_info.bits); |
| 598 | const tag_ty = ty.intTagType(&buffer); |
| 599 | return self.resolveType(tag_ty, repr); |
| 602 | 600 | }, |
| 603 | 601 | .Float => { |
| 604 | 602 | // We can (and want) not really emulate floating points with other floating point types like with the integer types, |
| ... | ... | @@ -626,7 +624,7 @@ pub const DeclGen = struct { |
| 626 | 624 | |
| 627 | 625 | const payload = try self.spv.arena.create(SpvType.Payload.Array); |
| 628 | 626 | payload.* = .{ |
| 629 | | .element_type = try self.resolveType(elem_ty), |
| 627 | .element_type = try self.resolveType(elem_ty, repr), |
| 630 | 628 | .length = total_len, |
| 631 | 629 | }; |
| 632 | 630 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| ... | ... | @@ -636,12 +634,14 @@ pub const DeclGen = struct { |
| 636 | 634 | if (ty.fnIsVarArgs()) |
| 637 | 635 | return self.fail("VarArgs functions are unsupported for SPIR-V", .{}); |
| 638 | 636 | |
| 637 | // TODO: Parameter passing convention etc. |
| 638 | |
| 639 | 639 | const param_types = try self.spv.arena.alloc(SpvType.Ref, ty.fnParamLen()); |
| 640 | 640 | for (param_types, 0..) |*param, i| { |
| 641 | | param.* = try self.resolveType(ty.fnParamType(i)); |
| 641 | param.* = try self.resolveType(ty.fnParamType(i), .direct); |
| 642 | 642 | } |
| 643 | 643 | |
| 644 | | const return_type = try self.resolveType(ty.fnReturnType()); |
| 644 | const return_type = try self.resolveType(ty.fnReturnType(), .direct); |
| 645 | 645 | |
| 646 | 646 | const payload = try self.spv.arena.create(SpvType.Payload.Function); |
| 647 | 647 | payload.* = .{ .return_type = return_type, .parameters = param_types }; |
| ... | ... | @@ -653,7 +653,7 @@ pub const DeclGen = struct { |
| 653 | 653 | const ptr_payload = try self.spv.arena.create(SpvType.Payload.Pointer); |
| 654 | 654 | ptr_payload.* = .{ |
| 655 | 655 | .storage_class = spirvStorageClass(ptr_info.@"addrspace"), |
| 656 | | .child_type = try self.resolveType(ptr_info.pointee_type), |
| 656 | .child_type = try self.resolveType(ptr_info.pointee_type, .indirect), |
| 657 | 657 | // Note: only available in Kernels! |
| 658 | 658 | .alignment = ty.ptrAlignment(target) * 8, |
| 659 | 659 | }; |
| ... | ... | @@ -680,7 +680,7 @@ pub const DeclGen = struct { |
| 680 | 680 | |
| 681 | 681 | const payload = try self.spv.arena.create(SpvType.Payload.Vector); |
| 682 | 682 | payload.* = .{ |
| 683 | | .component_type = try self.resolveType(ty.elemType()), |
| 683 | .component_type = try self.resolveType(ty.elemType(), repr), |
| 684 | 684 | .component_count = @intCast(u32, ty.vectorLen()), |
| 685 | 685 | }; |
| 686 | 686 | return try self.spv.resolveType(SpvType.initPayload(&payload.base)); |
| ... | ... | @@ -690,11 +690,11 @@ pub const DeclGen = struct { |
| 690 | 690 | const tuple = ty.tupleFields(); |
| 691 | 691 | const members = try self.spv.arena.alloc(SpvType.Payload.Struct.Member, tuple.types.len); |
| 692 | 692 | var member_index: u32 = 0; |
| 693 | | for (tuple.types) |field_ty, i| { |
| 693 | for (tuple.types, 0..) |field_ty, i| { |
| 694 | 694 | const field_val = tuple.values[i]; |
| 695 | 695 | if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 696 | 696 | members[member_index] = .{ |
| 697 | | .ty = try self.resolveType(field_ty), |
| 697 | .ty = try self.resolveType(field_ty, repr), |
| 698 | 698 | }; |
| 699 | 699 | member_index += 1; |
| 700 | 700 | } |
| ... | ... | @@ -708,16 +708,16 @@ pub const DeclGen = struct { |
| 708 | 708 | const struct_ty = ty.castTag(.@"struct").?.data; |
| 709 | 709 | |
| 710 | 710 | if (struct_ty.layout == .Packed) { |
| 711 | | return try self.resolveType(struct_ty.backing_int_ty); |
| 711 | return try self.resolveType(struct_ty.backing_int_ty, repr); |
| 712 | 712 | } |
| 713 | 713 | |
| 714 | 714 | const members = try self.spv.arena.alloc(SpvType.Payload.Struct.Member, struct_ty.fields.count()); |
| 715 | 715 | var member_index: usize = 0; |
| 716 | | for (struct_ty.fields.values()) |field, i| { |
| 716 | for (struct_ty.fields.values(), 0..) |field, i| { |
| 717 | 717 | if (field.is_comptime or !field.ty.hasRuntimeBits()) continue; |
| 718 | 718 | |
| 719 | 719 | members[member_index] = .{ |
| 720 | | .ty = try self.resolveType(field.ty), |
| 720 | .ty = try self.resolveType(field.ty, repr), |
| 721 | 721 | .name = struct_ty.fields.keys()[i], |
| 722 | 722 | }; |
| 723 | 723 | member_index += 1; |
| ... | ... | @@ -957,21 +957,14 @@ pub const DeclGen = struct { |
| 957 | 957 | return result_id; |
| 958 | 958 | } |
| 959 | 959 | |
| 960 | | fn maskStrangeInt(self: *DeclGen, ty_id: IdResultType, int_id: IdRef, bits: u16) !IdRef { |
| 961 | | const backing_bits = self.backingIntBits(bits).?; |
| 960 | fn maskStrangeInt(self: *DeclGen, ty_ref: SpvType.Ref, value_id: IdRef, bits: u16) !IdRef { |
| 962 | 961 | const mask_value = if (bits == 64) 0xFFFF_FFFF_FFFF_FFFF else (@as(u64, 1) << @intCast(u6, bits)) - 1; |
| 963 | | const mask_lit: spec.LiteralContextDependentNumber = switch (backing_bits) { |
| 964 | | 1...32 => .{ .uint32 = @truncate(u32, mask_value) }, |
| 965 | | 33...64 => .{ .uint64 = mask_value }, |
| 966 | | else => unreachable, |
| 967 | | }; |
| 968 | | // TODO: We should probably optimize the amount of these constants a bit. |
| 969 | | const mask_id = try self.spv.emitConstant(ty_id, mask_lit); |
| 970 | 962 | const result_id = self.spv.allocId(); |
| 963 | const mask_id = try self.constInt(ty_ref, mask_value); |
| 971 | 964 | try self.func.body.emit(self.spv.gpa, .OpBitwiseAnd, .{ |
| 972 | | .id_result_type = ty_id, |
| 965 | .id_result_type = self.typeId(ty_ref), |
| 973 | 966 | .id_result = result_id, |
| 974 | | .operand_1 = int_id, |
| 967 | .operand_1 = value_id, |
| 975 | 968 | .operand_2 = mask_id, |
| 976 | 969 | }); |
| 977 | 970 | return result_id; |
| ... | ... | @@ -994,8 +987,7 @@ pub const DeclGen = struct { |
| 994 | 987 | var lhs_id = try self.resolve(bin_op.lhs); |
| 995 | 988 | var rhs_id = try self.resolve(bin_op.rhs); |
| 996 | 989 | |
| 997 | | const result_id = self.spv.allocId(); |
| 998 | | const result_type_id = try self.resolveTypeId(ty); |
| 990 | const result_ty_ref = try self.resolveType(ty, .direct); |
| 999 | 991 | |
| 1000 | 992 | assert(self.air.typeOf(bin_op.lhs).eql(ty, self.module)); |
| 1001 | 993 | assert(self.air.typeOf(bin_op.rhs).eql(ty, self.module)); |
| ... | ... | @@ -1010,8 +1002,8 @@ pub const DeclGen = struct { |
| 1010 | 1002 | }, |
| 1011 | 1003 | .strange_integer => blk: { |
| 1012 | 1004 | if (!modular) { |
| 1013 | | lhs_id = try self.maskStrangeInt(result_type_id, lhs_id, info.bits); |
| 1014 | | rhs_id = try self.maskStrangeInt(result_type_id, rhs_id, info.bits); |
| 1005 | lhs_id = try self.maskStrangeInt(result_ty_ref, lhs_id, info.bits); |
| 1006 | rhs_id = try self.maskStrangeInt(result_ty_ref, rhs_id, info.bits); |
| 1015 | 1007 | } |
| 1016 | 1008 | break :blk switch (info.signedness) { |
| 1017 | 1009 | .signed => @as(usize, 1), |
| ... | ... | @@ -1026,8 +1018,9 @@ pub const DeclGen = struct { |
| 1026 | 1018 | .bool => unreachable, |
| 1027 | 1019 | }; |
| 1028 | 1020 | |
| 1021 | const result_id = self.spv.allocId(); |
| 1029 | 1022 | const operands = .{ |
| 1030 | | .id_result_type = result_type_id, |
| 1023 | .id_result_type = self.typeId(result_ty_ref), |
| 1031 | 1024 | .id_result = result_id, |
| 1032 | 1025 | .operand_1 = lhs_id, |
| 1033 | 1026 | .operand_2 = rhs_id, |
| ... | ... | @@ -1068,7 +1061,7 @@ pub const DeclGen = struct { |
| 1068 | 1061 | const result_type_id = try self.resolveTypeId(result_ty); |
| 1069 | 1062 | |
| 1070 | 1063 | const overflow_member_ty = try self.intType(.unsigned, info.bits); |
| 1071 | | const overflow_member_ty_id = self.spv.typeResultId(overflow_member_ty); |
| 1064 | const overflow_member_ty_id = self.typeId(overflow_member_ty); |
| 1072 | 1065 | |
| 1073 | 1066 | const op_result_id = blk: { |
| 1074 | 1067 | // Construct the SPIR-V result type. |
| ... | ... | @@ -1181,9 +1174,9 @@ pub const DeclGen = struct { |
| 1181 | 1174 | .float => 0, |
| 1182 | 1175 | .bool => 1, |
| 1183 | 1176 | .strange_integer => blk: { |
| 1184 | | const op_ty_id = try self.resolveTypeId(op_ty); |
| 1185 | | lhs_id = try self.maskStrangeInt(op_ty_id, lhs_id, info.bits); |
| 1186 | | rhs_id = try self.maskStrangeInt(op_ty_id, rhs_id, info.bits); |
| 1177 | const op_ty_ref = try self.resolveType(op_ty, .direct); |
| 1178 | lhs_id = try self.maskStrangeInt(op_ty_ref, lhs_id, info.bits); |
| 1179 | rhs_id = try self.maskStrangeInt(op_ty_ref, rhs_id, info.bits); |
| 1187 | 1180 | break :blk switch (info.signedness) { |
| 1188 | 1181 | .signed => @as(usize, 1), |
| 1189 | 1182 | .unsigned => @as(usize, 2), |
| ... | ... | @@ -1425,7 +1418,7 @@ pub const DeclGen = struct { |
| 1425 | 1418 | .Struct => switch (object_ty.containerLayout()) { |
| 1426 | 1419 | .Packed => unreachable, // TODO |
| 1427 | 1420 | else => { |
| 1428 | | const u32_ty_id = self.spv.typeResultId(try self.intType(.unsigned, 32)); |
| 1421 | const u32_ty_id = self.typeId(try self.intType(.unsigned, 32)); |
| 1429 | 1422 | const field_index_id = try self.spv.emitConstant(u32_ty_id, .{ .uint32 = field_index }); |
| 1430 | 1423 | const result_id = self.spv.allocId(); |
| 1431 | 1424 | const result_type_id = try self.resolveTypeId(result_ptr_ty); |
| ... | ... | @@ -1740,7 +1733,7 @@ pub const DeclGen = struct { |
| 1740 | 1733 | return self.todo("switch on runtime value???", .{}); |
| 1741 | 1734 | }; |
| 1742 | 1735 | const int_val = switch (cond_ty.zigTypeTag()) { |
| 1743 | | .Int => if (cond_ty.isSignedInt()) @bitCast(u64, value.toSignedInt()) else value.toUnsignedInt(target), |
| 1736 | .Int => if (cond_ty.isSignedInt()) @bitCast(u64, value.toSignedInt(target)) else value.toUnsignedInt(target), |
| 1744 | 1737 | .Enum => blk: { |
| 1745 | 1738 | var int_buffer: Value.Payload.U64 = undefined; |
| 1746 | 1739 | // TODO: figure out of cond_ty is correct (something with enum literals) |