authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-23 14:49:11+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-28 16:46:59+00:00
logc0f3a238314315a1e4a578b23cc66237eac90689
treec1ce31a04892cea98f61ef903444861cd85682ab
parentbd8088bb9826b40be281d5cca89eac7ff7b76241
signaturelock-open Commit is signed but in an unrecognized format.

llvm: get rid of a bunch of `PerThread` usages

Also, notably, remove `Air.value`! The `onePossibleValue` check was actually dead code, because it is a bug if Sema ever emits code which considers a value of OPV type to be runtime-known---and at that point `Air.value` is just a thin wrapper around `Air.Ref.toInterned`.

14 files changed, 620 insertions(+), 706 deletions(-)

src/Air.zig-9
......@@ -1843,15 +1843,6 @@ pub fn internedToRef(ip_index: InternPool.Index) Inst.Ref {
18431843 return .fromIntern(ip_index);
18441844}
18451845
1846/// Returns `null` if runtime-known.
1847pub fn value(air: Air, inst: Inst.Ref, pt: Zcu.PerThread) !?Value {
1848 if (inst.toInterned()) |ip_index| {
1849 return .fromInterned(ip_index);
1850 }
1851 const index = inst.toIndex().?;
1852 return air.typeOfIndex(index, &pt.zcu.intern_pool).onePossibleValue(pt);
1853}
1854
18551846pub const NullTerminatedString = enum(u32) {
18561847 none = std.math.maxInt(u32),
18571848 _,
src/Sema.zig+1-1
......@@ -18896,7 +18896,7 @@ fn finishStructInit(
1889618896 var bit_offset: u16 = 0;
1889718897 for (field_inits) |field_init| {
1889818898 const field_val = sema.resolveValue(field_init).?;
18899 field_val.writeToPackedMemory(pt, buf, bit_offset) catch |err| switch (err) {
18899 field_val.writeToPackedMemory(zcu, buf, bit_offset) catch |err| switch (err) {
1890018900 error.ReinterpretDeclRef => unreachable, // bitpack fields cannot be pointers
1890118901 error.OutOfMemory => |e| return e,
1890218902 };
src/Sema/bitcast.zig+2-2
......@@ -443,7 +443,7 @@ const UnpackValueBits = struct {
443443 // This @intCast is okay because no primitive can exceed the size of a u16.
444444 const int_ty = try unpack.pt.intType(.unsigned, @intCast(bit_count));
445445 const buf = try unpack.arena.alloc(u8, @intCast((val_bits + 7) / 8));
446 try val.writeToPackedMemory(unpack.pt, buf, 0);
446 try val.writeToPackedMemory(zcu, buf, 0);
447447 const sub_val = try Value.readFromPackedMemory(int_ty, unpack.pt, buf, @intCast(bit_offset), unpack.arena);
448448 try unpack.primitive(sub_val);
449449 },
......@@ -722,7 +722,7 @@ const PackValueBits = struct {
722722 const val = Value.fromInterned(ip_val);
723723 const ty = val.typeOf(zcu);
724724 if (!val.isUndef(zcu)) {
725 try val.writeToPackedMemory(pt, buf, cur_bit_off);
725 try val.writeToPackedMemory(zcu, buf, cur_bit_off);
726726 }
727727 cur_bit_off += @intCast(ty.bitSize(zcu));
728728 }
src/Value.zig+32-36
......@@ -245,13 +245,12 @@ pub fn toBool(val: Value) bool {
245245///
246246/// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past
247247/// the end of the value in memory.
248pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
248pub fn writeToMemory(val: Value, zcu: *const Zcu, buffer: []u8) error{
249249 ReinterpretDeclRef,
250250 IllDefinedMemoryLayout,
251251 Unimplemented,
252252 OutOfMemory,
253253}!void {
254 const zcu = pt.zcu;
255254 const target = zcu.getTarget();
256255 const endian = target.cpu.arch.endian();
257256 const ip = &zcu.intern_pool;
......@@ -289,14 +288,18 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
289288 else => unreachable,
290289 },
291290 .array => {
291 const aggregate = ip.indexToKey(val.toIntern()).aggregate;
292292 const len = ty.arrayLen(zcu);
293293 const elem_ty = ty.childType(zcu);
294294 const elem_size: usize = @intCast(elem_ty.abiSize(zcu));
295295 var elem_i: usize = 0;
296296 var buf_off: usize = 0;
297297 while (elem_i < len) : (elem_i += 1) {
298 const elem_val = try val.elemValue(pt, elem_i);
299 try elem_val.writeToMemory(pt, buffer[buf_off..]);
298 switch (aggregate.storage) {
299 .bytes => |bytes| buffer[buf_off] = bytes.at(elem_i, ip),
300 .elems => |elems| try Value.fromInterned(elems[elem_i]).writeToMemory(zcu, buffer[buf_off..]),
301 .repeated_elem => |elem| try Value.fromInterned(elem).writeToMemory(zcu, buffer[buf_off..]),
302 }
300303 buf_off += elem_size;
301304 }
302305 },
......@@ -304,7 +307,7 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
304307 // We use byte_count instead of abi_size here, so that any padding bytes
305308 // follow the data bytes, on both big- and little-endian systems.
306309 const byte_count = (@as(usize, @intCast(ty.bitSize(zcu))) + 7) / 8;
307 return writeToPackedMemory(val, pt, buffer[0..byte_count], 0);
310 return writeToPackedMemory(val, zcu, buffer[0..byte_count], 0);
308311 },
309312 .@"struct" => {
310313 const struct_type = zcu.typeToStruct(ty) orelse return error.IllDefinedMemoryLayout;
......@@ -320,42 +323,33 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
320323 .elems => |elems| elems[field_index],
321324 .repeated_elem => |elem| elem,
322325 });
323 try writeToMemory(field_val, pt, buffer[off..]);
326 try writeToMemory(field_val, zcu, buffer[off..]);
324327 },
325328 .@"packed" => {
326329 const int_index = ip.indexToKey(val.toIntern()).bitpack.backing_int_val;
327 return Value.fromInterned(int_index).writeToMemory(pt, buffer);
330 return Value.fromInterned(int_index).writeToMemory(zcu, buffer);
328331 },
329332 }
330333 },
331334 .@"union" => switch (ty.containerLayout(zcu)) {
332335 .auto => return error.IllDefinedMemoryLayout, // Sema is supposed to have emitted a compile error already
333336 .@"extern" => {
334 if (val.unionTag(zcu)) |union_tag| {
335 const union_obj = zcu.typeToUnion(ty).?;
336 const field_index = zcu.unionTagFieldIndex(union_obj, union_tag).?;
337 const field_type = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
338 const field_val = try val.fieldValue(pt, field_index);
339 const byte_count: usize = @intCast(field_type.abiSize(zcu));
340 return writeToMemory(field_val, pt, buffer[0..byte_count]);
341 } else {
342 const backing_ty = try ty.externUnionBackingType(pt);
343 const byte_count: usize = @intCast(backing_ty.abiSize(zcu));
344 return writeToMemory(val.unionPayload(zcu), pt, buffer[0..byte_count]);
345 }
337 const payload_val = val.unionPayload(zcu);
338 return writeToMemory(payload_val, zcu, buffer);
346339 },
347340 .@"packed" => {
348341 const int_val: Value = .fromInterned(ip.indexToKey(val.toIntern()).bitpack.backing_int_val);
349 return writeToMemory(int_val, pt, buffer);
342 return writeToMemory(int_val, zcu, buffer);
350343 },
351344 },
352345 .optional => {
353346 if (!ty.isPtrLikeOptional(zcu)) return error.IllDefinedMemoryLayout;
354347 const opt_val = val.optionalValue(zcu);
355348 if (opt_val) |some| {
356 return some.writeToMemory(pt, buffer);
349 return some.writeToMemory(zcu, buffer);
357350 } else {
358 return writeToMemory(try pt.intValue(Type.usize, 0), pt, buffer);
351 const byte_count = Type.usize.abiSize(zcu);
352 @memset(buffer[0..@intCast(byte_count)], 0); // null pointer
359353 }
360354 },
361355 else => return error.Unimplemented,
......@@ -368,11 +362,10 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
368362/// big-endian packed memory layouts start at the end of the buffer.
369363pub fn writeToPackedMemory(
370364 val: Value,
371 pt: Zcu.PerThread,
365 zcu: *const Zcu,
372366 buffer: []u8,
373367 bit_offset: usize,
374368) error{ ReinterpretDeclRef, OutOfMemory }!void {
375 const zcu = pt.zcu;
376369 const ip = &zcu.intern_pool;
377370 const target = zcu.getTarget();
378371 const endian = target.cpu.arch.endian();
......@@ -399,7 +392,7 @@ pub fn writeToPackedMemory(
399392 },
400393 .@"enum" => {
401394 const int_val = val.intFromEnum(zcu);
402 return int_val.writeToPackedMemory(pt, buffer, bit_offset);
395 return int_val.writeToPackedMemory(zcu, buffer, bit_offset);
403396 },
404397 .pointer => {
405398 assert(!ty.isSlice(zcu)); // No well defined layout.
......@@ -430,25 +423,29 @@ pub fn writeToPackedMemory(
430423
431424 var bits: u16 = 0;
432425 var elem_i: usize = 0;
426 const aggregate = ip.indexToKey(val.toIntern()).aggregate;
433427 while (elem_i < len) : (elem_i += 1) {
434428 // On big-endian systems, LLVM reverses the element order of vectors by default
435429 const tgt_elem_i = if (endian == .big) len - elem_i - 1 else elem_i;
436 const elem_val = try val.elemValue(pt, tgt_elem_i);
437 try elem_val.writeToPackedMemory(pt, buffer, bit_offset + bits);
430 switch (aggregate.storage) {
431 .bytes => |bytes| std.mem.writePackedInt(u8, buffer, bit_offset + bits, bytes.at(tgt_elem_i, ip), endian),
432 .elems => |elems| try Value.fromInterned(elems[tgt_elem_i]).writeToPackedMemory(zcu, buffer, bit_offset + bits),
433 .repeated_elem => |elem| try Value.fromInterned(elem).writeToPackedMemory(zcu, buffer, bit_offset + bits),
434 }
438435 bits += elem_bit_size;
439436 }
440437 },
441438 .@"struct", .@"union" => {
442439 assert(ty.containerLayout(zcu) == .@"packed");
443440 const int_val: Value = .fromInterned(ip.indexToKey(val.toIntern()).bitpack.backing_int_val);
444 return int_val.writeToPackedMemory(pt, buffer, bit_offset);
441 return int_val.writeToPackedMemory(zcu, buffer, bit_offset);
445442 },
446443 .optional => {
447444 assert(ty.isPtrLikeOptional(zcu));
448445 if (val.optionalValue(zcu)) |ptr_val| {
449 return ptr_val.writeToPackedMemory(pt, buffer, bit_offset);
446 return ptr_val.writeToPackedMemory(zcu, buffer, bit_offset);
450447 } else {
451 return Value.zero_usize.writeToPackedMemory(pt, buffer, bit_offset);
448 return Value.zero_usize.writeToPackedMemory(zcu, buffer, bit_offset);
452449 }
453450 },
454451 else => @panic("TODO implement writeToPackedMemory for more types"),
......@@ -889,7 +886,7 @@ pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value {
889886 const sfba = sfba_state.get();
890887 const buf = try sfba.alloc(u8, @intCast((ty.bitSize(zcu) + 7) / 8));
891888 defer sfba.free(buf);
892 int_val.writeToPackedMemory(pt, buf, 0) catch |err| switch (err) {
889 int_val.writeToPackedMemory(zcu, buf, 0) catch |err| switch (err) {
893890 error.ReinterpretDeclRef => unreachable, // it's an integer
894891 error.OutOfMemory => |e| return e,
895892 };
......@@ -902,7 +899,7 @@ pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value {
902899 };
903900}
904901
905pub fn unionTag(val: Value, zcu: *Zcu) ?Value {
902pub fn unionTag(val: Value, zcu: *const Zcu) ?Value {
906903 return switch (zcu.intern_pool.indexToKey(val.toIntern())) {
907904 .undef, .enum_tag => val,
908905 .un => |un| if (un.tag != .none) Value.fromInterned(un.tag) else return null,
......@@ -910,7 +907,7 @@ pub fn unionTag(val: Value, zcu: *Zcu) ?Value {
910907 };
911908}
912909
913pub fn unionPayload(val: Value, zcu: *Zcu) Value {
910pub fn unionPayload(val: Value, zcu: *const Zcu) Value {
914911 return switch (zcu.intern_pool.indexToKey(val.toIntern())) {
915912 .un => |un| Value.fromInterned(un.val),
916913 else => unreachable,
......@@ -1605,15 +1602,14 @@ pub fn mulAddScalar(
16051602
16061603/// If the value is represented in-memory as a series of bytes that all
16071604/// have the same value, return that byte value, otherwise null.
1608pub fn hasRepeatedByteRepr(val: Value, pt: Zcu.PerThread) !?u8 {
1609 const zcu = pt.zcu;
1605pub fn hasRepeatedByteRepr(val: Value, zcu: *const Zcu) !?u8 {
16101606 const ty = val.typeOf(zcu);
16111607 const abi_size = std.math.cast(usize, ty.abiSize(zcu)) orelse return null;
16121608 assert(abi_size >= 1);
16131609 const byte_buffer = try zcu.gpa.alloc(u8, abi_size);
16141610 defer zcu.gpa.free(byte_buffer);
16151611
1616 writeToMemory(val, pt, byte_buffer) catch |err| switch (err) {
1612 writeToMemory(val, zcu, byte_buffer) catch |err| switch (err) {
16171613 error.OutOfMemory => return error.OutOfMemory,
16181614 error.ReinterpretDeclRef => return null,
16191615 // TODO: The writeToMemory function was originally created for the purpose
src/Zcu/PerThread.zig+1-1
......@@ -3751,7 +3751,7 @@ fn processExportsInner(
37513751 if (skip_linker_work) return;
37523752
37533753 if (zcu.llvm_object) |llvm_object| {
3754 try zcu.handleUpdateExports(export_indices, llvm_object.updateExports(pt, exported, export_indices));
3754 try zcu.handleUpdateExports(export_indices, llvm_object.updateExports(exported, export_indices));
37553755 } else if (zcu.comp.bin_file) |lf| {
37563756 try zcu.handleUpdateExports(export_indices, lf.updateExports(pt, exported, export_indices));
37573757 }
src/codegen/aarch64/Select.zig+1-1
......@@ -11364,7 +11364,7 @@ fn writeToMemory(isel: *Select, constant: Constant, buffer: []u8) error{OutOfMem
1136411364 const zcu = isel.pt.zcu;
1136511365 const ip = &zcu.intern_pool;
1136611366 if (try isel.writeKeyToMemory(ip.indexToKey(constant.toIntern()), buffer)) return true;
11367 constant.writeToMemory(isel.pt, buffer) catch |err| switch (err) {
11367 constant.writeToMemory(zcu, buffer) catch |err| switch (err) {
1136811368 error.OutOfMemory => return error.OutOfMemory,
1136911369 error.ReinterpretDeclRef, error.Unimplemented, error.IllDefinedMemoryLayout => return false,
1137011370 };
src/codegen/c.zig+12-12
......@@ -435,8 +435,7 @@ pub const Function = struct {
435435 fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue {
436436 const gop = try f.value_map.getOrPut(ref);
437437 if (!gop.found_existing) {
438 const val = try f.air.value(ref, f.dg.pt);
439 gop.value_ptr.* = .{ .constant = val.? };
438 gop.value_ptr.* = .{ .constant = .fromInterned(ref.toInterned().?) };
440439 }
441440 return gop.value_ptr.*;
442441 }
......@@ -3389,7 +3388,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
33893388 const ptr_val = try f.resolveInst(bin_op.lhs);
33903389 const src_ty = f.typeOf(bin_op.rhs);
33913390
3392 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndef(zcu) else false;
3391 const val_is_undef = if (bin_op.rhs.toInterned()) |ip_index| Value.fromInterned(ip_index).isUndef(zcu) else false;
33933392
33943393 const w = &f.code.writer;
33953394 if (val_is_undef) {
......@@ -3922,8 +3921,8 @@ fn airCall(
39223921
39233922 callee: {
39243923 known: {
3925 const callee_val = (try f.air.value(call.callee, pt)) orelse break :known;
3926 const fn_nav, const need_cast = switch (ip.indexToKey(callee_val.toIntern())) {
3924 const callee_ip_index = call.callee.toInterned() orelse break :known;
3925 const fn_nav, const need_cast = switch (ip.indexToKey(callee_ip_index)) {
39273926 .@"extern" => |@"extern"| .{ @"extern".owner_nav, false },
39283927 .func => |func| .{ func.owner_nav, Type.fromInterned(func.ty).fnCallingConvention(zcu) != .naked and
39293928 Type.fromInterned(func.uncoerced_ty).fnCallingConvention(zcu) == .naked },
......@@ -4027,7 +4026,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
40274026 const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)];
40284027 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
40294028 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
4030 const operand_is_undef = if (try f.air.value(pl_op.operand, pt)) |v| v.isUndef(zcu) else false;
4029 const operand_is_undef = if (pl_op.operand.toInterned()) |ip_index| Value.fromInterned(ip_index).isUndef(zcu) else false;
40314030 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
40324031
40334032 try reap(f, inst, &.{pl_op.operand});
......@@ -4204,7 +4203,8 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
42044203 const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
42054204 const w = &f.code.writer;
42064205
4207 if (try f.air.value(br.operand, pt)) |cond_val| {
4206 if (br.operand.toInterned()) |cond_ip_index| {
4207 const cond_val: Value = .fromInterned(cond_ip_index);
42084208 // Comptime-known dispatch. Iterate the cases to find the correct
42094209 // one, and branch directly to the corresponding case.
42104210 const switch_br = f.air.unwrapSwitch(br.block_inst);
......@@ -4539,12 +4539,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
45394539 try f.writeCValue(w, cond_val, .other);
45404540 try w.writeAll(", ");
45414541 }
4542 const item_value = try f.air.value(item, pt);
4542 const item_value: Value = .fromInterned(item.toInterned().?);
45434543 // If `item_value` is a pointer with a known integer address, print the address
45444544 // with no cast to avoid a warning.
45454545 write_val: {
45464546 if (cond_ty.zigTypeTag(zcu) == .pointer) {
4547 if (item_value.?.getUnsignedInt(zcu)) |item_int| {
4547 if (item_value.getUnsignedInt(zcu)) |item_int| {
45484548 try w.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_cond_ty, item_int))});
45494549 break :write_val;
45504550 }
......@@ -4552,7 +4552,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
45524552 try f.renderType(w, .usize);
45534553 try w.writeByte(')');
45544554 }
4555 try f.dg.renderValue(w, (try f.air.value(item, pt)).?, .other);
4555 try f.dg.renderValue(w, .fromInterned(item.toInterned().?), .other);
45564556 }
45574557 switch (cond_cint) {
45584558 .zig_u128, .zig_i128 => try w.writeByte(')'),
......@@ -4710,7 +4710,7 @@ fn lowerSwitchCmp(
47104710 try f.writeCValue(w, cond_val, .other);
47114711 try w.writeAll(if (use_builtin) ", " else compareOperatorC(operator));
47124712 if (class == .big) try w.writeByte('&');
4713 try f.dg.renderValue(w, (try f.air.value(case_inst, pt)).?, .other);
4713 try f.dg.renderValue(w, .fromInterned(case_inst.toInterned().?), .other);
47144714 if (use_builtin) {
47154715 try f.dg.renderBuiltinInfo(w, ty, if (class == .big) .bits else .none);
47164716 try w.writeByte(')');
......@@ -6100,7 +6100,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
61006100 const value = try f.resolveInst(bin_op.rhs);
61016101 const elem_ty = f.typeOf(bin_op.rhs);
61026102 const elem_abi_size = elem_ty.abiSize(zcu);
6103 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false;
6103 const val_is_undef = if (bin_op.rhs.toInterned()) |ip_index| Value.fromInterned(ip_index).isUndef(zcu) else false;
61046104 const w = &f.code.writer;
61056105
61066106 if (val_is_undef) {
src/codegen/llvm.zig+207-222
......@@ -696,11 +696,11 @@ pub const Object = struct {
696696 self.* = undefined;
697697 }
698698
699 fn genErrorNameTable(o: *Object, pt: Zcu.PerThread) Allocator.Error!void {
699 fn genErrorNameTable(o: *Object) Allocator.Error!void {
700700 // If o.error_name_table is null, then it was not referenced by any instructions.
701701 if (o.error_name_table == .none) return;
702702
703 const zcu = pt.zcu;
703 const zcu = o.zcu;
704704 const ip = &zcu.intern_pool;
705705
706706 const error_name_list = ip.global_error_set.getNamesFromMainThread();
......@@ -709,8 +709,8 @@ pub const Object = struct {
709709
710710 // TODO: Address space
711711 const slice_ty = Type.slice_const_u8_sentinel_0;
712 const llvm_usize_ty = try o.lowerType(pt, Type.usize);
713 const llvm_slice_ty = try o.lowerType(pt, slice_ty);
712 const llvm_usize_ty = try o.lowerType(.usize);
713 const llvm_slice_ty = try o.lowerType(slice_ty);
714714 const llvm_table_ty = try o.builder.arrayType(1 + error_name_list.len, llvm_slice_ty);
715715
716716 llvm_errors[0] = try o.builder.undefConst(llvm_slice_ty);
......@@ -768,7 +768,7 @@ pub const Object = struct {
768768 };
769769
770770 pub fn emit(o: *Object, pt: Zcu.PerThread, options: EmitOptions) error{ LinkFailure, OutOfMemory }!void {
771 const zcu = pt.zcu;
771 const zcu = o.zcu;
772772 const comp = zcu.comp;
773773 const io = comp.io;
774774 const diags = &comp.link_diags;
......@@ -779,7 +779,7 @@ pub const Object = struct {
779779 const init_val = try o.builder.intConst(try o.errorIntType(), errors_len);
780780 try o.errors_len_variable.setInitializer(init_val, &o.builder);
781781 }
782 try o.genErrorNameTable(pt);
782 try o.genErrorNameTable();
783783 try o.genModuleLevelAssembly();
784784
785785 if (o.used.items.len > 0) {
......@@ -1139,7 +1139,7 @@ pub const Object = struct {
11391139 air: *const Air,
11401140 liveness: *const ?Air.Liveness,
11411141 ) !void {
1142 const zcu = pt.zcu;
1142 const zcu = o.zcu;
11431143 const comp = zcu.comp;
11441144 const ip = &zcu.intern_pool;
11451145 const func = zcu.funcInfo(func_index);
......@@ -1150,7 +1150,7 @@ pub const Object = struct {
11501150 const fn_info = zcu.typeToFunc(fn_ty).?;
11511151 const target = &owner_mod.resolved_target.result;
11521152
1153 const function_index = try o.resolveLlvmFunction(pt, func.owner_nav);
1153 const function_index = try o.resolveLlvmFunction(func.owner_nav);
11541154
11551155 var attributes = try function_index.ptrConst(&o.builder).attributes.toWip(&o.builder);
11561156 defer attributes.deinit(&o.builder);
......@@ -1262,7 +1262,7 @@ pub const Object = struct {
12621262 defer args.deinit(gpa);
12631263
12641264 {
1265 var it = iterateParamTypes(o, pt, fn_info);
1265 var it = iterateParamTypes(o, fn_info);
12661266 while (try it.next()) |lowering| {
12671267 try args.ensureUnusedCapacity(gpa, 1);
12681268
......@@ -1289,7 +1289,7 @@ pub const Object = struct {
12891289 },
12901290 .byref => {
12911291 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1292 const param_llvm_ty = try o.lowerType(pt, param_ty);
1292 const param_llvm_ty = try o.lowerType(param_ty);
12931293 const param = wip.arg(llvm_arg_i);
12941294 const alignment = param_ty.abiAlignment(zcu).toLlvm();
12951295
......@@ -1304,7 +1304,7 @@ pub const Object = struct {
13041304 },
13051305 .byref_mut => {
13061306 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1307 const param_llvm_ty = try o.lowerType(pt, param_ty);
1307 const param_llvm_ty = try o.lowerType(param_ty);
13081308 const param = wip.arg(llvm_arg_i);
13091309 const alignment = param_ty.abiAlignment(zcu).toLlvm();
13101310
......@@ -1323,7 +1323,7 @@ pub const Object = struct {
13231323 const param = wip.arg(llvm_arg_i);
13241324 llvm_arg_i += 1;
13251325
1326 const param_llvm_ty = try o.lowerType(pt, param_ty);
1326 const param_llvm_ty = try o.lowerType(param_ty);
13271327 const alignment = param_ty.abiAlignment(zcu).toLlvm();
13281328 const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target);
13291329 _ = try wip.store(.normal, param, arg_ptr, alignment);
......@@ -1362,7 +1362,7 @@ pub const Object = struct {
13621362 const len_param = wip.arg(llvm_arg_i);
13631363 llvm_arg_i += 1;
13641364
1365 const slice_llvm_ty = try o.lowerType(pt, param_ty);
1365 const slice_llvm_ty = try o.lowerType(param_ty);
13661366 args.appendAssumeCapacity(
13671367 try wip.buildAggregate(slice_llvm_ty, &.{ ptr_param, len_param }, ""),
13681368 );
......@@ -1371,7 +1371,7 @@ pub const Object = struct {
13711371 assert(!it.byval_attr);
13721372 const field_types = it.types_buffer[0..it.types_len];
13731373 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1374 const param_llvm_ty = try o.lowerType(pt, param_ty);
1374 const param_llvm_ty = try o.lowerType(param_ty);
13751375 const param_alignment = param_ty.abiAlignment(zcu).toLlvm();
13761376 const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, param_alignment, target);
13771377 const llvm_ty = try o.builder.structType(.normal, field_types);
......@@ -1391,7 +1391,7 @@ pub const Object = struct {
13911391 },
13921392 .float_array => {
13931393 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1394 const param_llvm_ty = try o.lowerType(pt, param_ty);
1394 const param_llvm_ty = try o.lowerType(param_ty);
13951395 const param = wip.arg(llvm_arg_i);
13961396 llvm_arg_i += 1;
13971397
......@@ -1406,7 +1406,7 @@ pub const Object = struct {
14061406 },
14071407 .i32_array, .i64_array => {
14081408 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1409 const param_llvm_ty = try o.lowerType(pt, param_ty);
1409 const param_llvm_ty = try o.lowerType(param_ty);
14101410 const param = wip.arg(llvm_arg_i);
14111411 llvm_arg_i += 1;
14121412
......@@ -1560,7 +1560,7 @@ pub const Object = struct {
15601560 }
15611561
15621562 pub fn updateNav(o: *Object, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void {
1563 const zcu = pt.zcu;
1563 const zcu = o.zcu;
15641564 const ip = &zcu.intern_pool;
15651565
15661566 const nav = ip.getNav(nav_index);
......@@ -1573,12 +1573,12 @@ pub const Object = struct {
15731573 const ty: Type = .fromInterned(nav.resolved.?.type);
15741574
15751575 if (linkage != .internal and ip.isFunctionType(ty.toIntern())) {
1576 const function_index = try o.resolveLlvmFunction(pt, owner_nav);
1576 const function_index = try o.resolveLlvmFunction(owner_nav);
15771577 // Add parameter attributes which weren't set by `resolveLlvmFunction`
15781578 const fn_info = zcu.typeToFunc(ty).?;
15791579 var attributes = try function_index.ptrConst(&o.builder).attributes.toWip(&o.builder);
15801580 defer attributes.deinit(&o.builder);
1581 var it = iterateParamTypes(o, pt, fn_info);
1581 var it = iterateParamTypes(o, fn_info);
15821582 if (firstParamSRet(fn_info, zcu, zcu.getTarget())) it.llvm_index += 1;
15831583 if (fn_info.cc == .auto and zcu.comp.config.any_error_tracing) it.llvm_index += 1;
15841584 while (try it.next()) |lowering| switch (lowering) {
......@@ -1591,7 +1591,7 @@ pub const Object = struct {
15911591 },
15921592 .byref => {
15931593 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1594 const param_llvm_ty = try o.lowerType(pt, param_ty);
1594 const param_llvm_ty = try o.lowerType(param_ty);
15951595 const alignment = param_ty.abiAlignment(zcu);
15961596 try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment.toLlvm(), it.byval_attr, param_llvm_ty);
15971597 },
......@@ -1609,14 +1609,14 @@ pub const Object = struct {
16091609 };
16101610 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
16111611 } else {
1612 const variable_index = try o.resolveGlobalNav(pt, nav_index);
1612 const variable_index = try o.resolveGlobalNav(nav_index);
16131613 variable_index.setAlignment(zcu.navAlignment(nav_index).toLlvm(), &o.builder);
16141614 if (resolved.@"linksection".toSlice(ip)) |section|
16151615 variable_index.setSection(try o.builder.string(section), &o.builder);
16161616 if (resolved.@"const") variable_index.setMutability(.constant, &o.builder);
16171617 try variable_index.setInitializer(switch (init_val) {
16181618 .none => .no_init,
1619 else => try o.lowerValue(pt, init_val),
1619 else => try o.lowerValue(init_val),
16201620 }, &o.builder);
16211621 variable_index.setVisibility(visibility, &o.builder);
16221622
......@@ -1703,14 +1703,13 @@ pub const Object = struct {
17031703
17041704 pub fn updateExports(
17051705 self: *Object,
1706 pt: Zcu.PerThread,
17071706 exported: Zcu.Exported,
17081707 export_indices: []const Zcu.Export.Index,
17091708 ) link.File.UpdateExportsError!void {
1710 const zcu = pt.zcu;
1709 const zcu = self.zcu;
17111710 const nav_index = switch (exported) {
17121711 .nav => |nav| nav,
1713 .uav => |uav| return updateExportedValue(self, pt, uav, export_indices),
1712 .uav => |uav| return updateExportedValue(self, uav, export_indices),
17141713 };
17151714 const ip = &zcu.intern_pool;
17161715 const global_index = self.nav_map.get(nav_index).?;
......@@ -1751,11 +1750,10 @@ pub const Object = struct {
17511750
17521751 fn updateExportedValue(
17531752 o: *Object,
1754 pt: Zcu.PerThread,
17551753 exported_value: InternPool.Index,
17561754 export_indices: []const Zcu.Export.Index,
17571755 ) link.File.UpdateExportsError!void {
1758 const zcu = pt.zcu;
1756 const zcu = o.zcu;
17591757 const gpa = zcu.gpa;
17601758 const ip = &zcu.intern_pool;
17611759 const main_exp_name = try o.builder.strtabString(export_indices[0].ptr(zcu).opts.name.toSlice(ip));
......@@ -1769,13 +1767,13 @@ pub const Object = struct {
17691767 const llvm_addr_space = toLlvmAddressSpace(.generic, zcu.getTarget());
17701768 const variable_index = try o.builder.addVariable(
17711769 main_exp_name,
1772 try o.lowerType(pt, Type.fromInterned(ip.typeOf(exported_value))),
1770 try o.lowerType(.fromInterned(ip.typeOf(exported_value))),
17731771 llvm_addr_space,
17741772 );
17751773 const global_index = variable_index.ptrConst(&o.builder).global;
17761774 gop.value_ptr.* = global_index;
17771775 // This line invalidates `gop`.
1778 const init_val = try o.lowerValue(pt, exported_value);
1776 const init_val = try o.lowerValue(exported_value);
17791777 try variable_index.setInitializer(init_val, &o.builder);
17801778 break :i global_index;
17811779 };
......@@ -1894,7 +1892,7 @@ pub const Object = struct {
18941892 pub fn updateContainerType(o: *Object, pt: Zcu.PerThread, ty: InternPool.Index, success: bool) Allocator.Error!void {
18951893 try o.type_pool.updateContainerType(pt, .{ .llvm = o }, ty, success);
18961894 if (o.named_enum_map.get(ty)) |function_index| {
1897 try o.updateIsNamedEnumValueFunction(pt, .fromInterned(ty), function_index);
1895 try o.updateIsNamedEnumValueFunction(.fromInterned(ty), function_index);
18981896 }
18991897 }
19001898
......@@ -1902,7 +1900,8 @@ pub const Object = struct {
19021900 ///
19031901 /// `val` is always a type because `o.type_pool` only contains types.
19041902 pub fn addConst(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {
1905 const zcu = pt.zcu;
1903 _ = pt;
1904 const zcu = o.zcu;
19061905 const gpa = zcu.comp.gpa;
19071906 assert(zcu.intern_pool.typeOf(val) == .type_type);
19081907
......@@ -1928,7 +1927,7 @@ pub const Object = struct {
19281927 ///
19291928 /// `val` is always a type because `o.type_pool` only contains types.
19301929 pub fn updateConstIncomplete(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {
1931 const zcu = pt.zcu;
1930 const zcu = o.zcu;
19321931 assert(zcu.intern_pool.typeOf(val) == .type_type);
19331932
19341933 const ty: Type = .fromInterned(val);
......@@ -1950,7 +1949,7 @@ pub const Object = struct {
19501949 ///
19511950 /// `val` is always a type because `o.type_pool` only contains types.
19521951 pub fn updateConst(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {
1953 const zcu = pt.zcu;
1952 const zcu = o.zcu;
19541953 assert(zcu.intern_pool.typeOf(val) == .type_type);
19551954
19561955 const ty: Type = .fromInterned(val);
......@@ -2005,7 +2004,7 @@ pub const Object = struct {
20052004 assert(!o.builder.strip);
20062005
20072006 const gpa = o.gpa;
2008 const zcu = pt.zcu;
2007 const zcu = o.zcu;
20092008 const target = zcu.getTarget();
20102009 const ip = &zcu.intern_pool;
20112010
......@@ -2712,30 +2711,20 @@ pub const Object = struct {
27122711 }
27132712
27142713 fn namespaceToDebugScope(o: *Object, pt: Zcu.PerThread, namespace_index: InternPool.NamespaceIndex) !Builder.Metadata {
2715 const zcu = pt.zcu;
2714 const zcu = o.zcu;
27162715 const namespace = zcu.namespacePtr(namespace_index);
27172716 if (namespace.parent == .none) return try o.getDebugFile(namespace.file_scope);
27182717 return o.getDebugType(pt, .fromInterned(namespace.owner_type));
27192718 }
27202719
2721 fn allocTypeName(o: *Object, pt: Zcu.PerThread, ty: Type) Allocator.Error![:0]const u8 {
2722 var aw: Io.Writer.Allocating = .init(o.gpa);
2723 defer aw.deinit();
2724 ty.print(&aw.writer, pt, null) catch |err| switch (err) {
2725 error.WriteFailed => return error.OutOfMemory,
2726 };
2727 return aw.toOwnedSliceSentinel(0);
2728 }
2729
27302720 /// If the llvm function does not exist, create it.
27312721 /// Note that this can be called before the function's semantic analysis has
27322722 /// completed, so if any attributes rely on that, they must be done in updateFunc, not here.
27332723 pub fn resolveLlvmFunction(
27342724 o: *Object,
2735 pt: Zcu.PerThread,
27362725 nav_index: InternPool.Nav.Index,
27372726 ) Allocator.Error!Builder.Function.Index {
2738 const zcu = pt.zcu;
2727 const zcu = o.zcu;
27392728 const ip = &zcu.intern_pool;
27402729 const gpa = o.gpa;
27412730 const nav = ip.getNav(nav_index);
......@@ -2752,7 +2741,7 @@ pub const Object = struct {
27522741 else
27532742 .{ false, .none };
27542743 const function_index = try o.builder.addFunction(
2755 try o.lowerType(pt, ty),
2744 try o.lowerType(ty),
27562745 try o.builder.strtabString((if (is_extern) nav.name else nav.fqn).toSlice(ip)),
27572746 toLlvmAddressSpace(nav.resolved.?.@"addrspace", target),
27582747 );
......@@ -2785,7 +2774,7 @@ pub const Object = struct {
27852774 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);
27862775 try attributes.addParamAttr(llvm_arg_i, .@"noalias", &o.builder);
27872776
2788 const raw_llvm_ret_ty = try o.lowerType(pt, Type.fromInterned(fn_info.return_type));
2777 const raw_llvm_ret_ty = try o.lowerType(.fromInterned(fn_info.return_type));
27892778 try attributes.addParamAttr(llvm_arg_i, .{ .sret = raw_llvm_ret_ty }, &o.builder);
27902779
27912780 llvm_arg_i += 1;
......@@ -2965,7 +2954,6 @@ pub const Object = struct {
29652954
29662955 fn resolveGlobalUav(
29672956 o: *Object,
2968 pt: Zcu.PerThread,
29692957 uav: InternPool.Index,
29702958 llvm_addr_space: Builder.AddrSpace,
29712959 alignment: InternPool.Alignment,
......@@ -2983,17 +2971,17 @@ pub const Object = struct {
29832971 }
29842972 errdefer assert(o.uav_map.remove(uav));
29852973
2986 const zcu = pt.zcu;
2974 const zcu = o.zcu;
29872975 const decl_ty = zcu.intern_pool.typeOf(uav);
29882976
29892977 const variable_index = try o.builder.addVariable(
29902978 try o.builder.strtabStringFmt("__anon_{d}", .{@intFromEnum(uav)}),
2991 try o.lowerType(pt, Type.fromInterned(decl_ty)),
2979 try o.lowerType(.fromInterned(decl_ty)),
29922980 llvm_addr_space,
29932981 );
29942982 gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;
29952983
2996 try variable_index.setInitializer(try o.lowerValue(pt, uav), &o.builder);
2984 try variable_index.setInitializer(try o.lowerValue(uav), &o.builder);
29972985 variable_index.setLinkage(if (o.builder.strip) .private else .internal, &o.builder);
29982986 variable_index.setMutability(.constant, &o.builder);
29992987 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
......@@ -3003,14 +2991,13 @@ pub const Object = struct {
30032991
30042992 fn resolveGlobalNav(
30052993 o: *Object,
3006 pt: Zcu.PerThread,
30072994 nav_index: InternPool.Nav.Index,
30082995 ) Allocator.Error!Builder.Variable.Index {
30092996 const gop = try o.nav_map.getOrPut(o.gpa, nav_index);
30102997 if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.variable;
30112998 errdefer assert(o.nav_map.remove(nav_index));
30122999
3013 const zcu = pt.zcu;
3000 const zcu = o.zcu;
30143001 const ip = &zcu.intern_pool;
30153002 const nav = ip.getNav(nav_index);
30163003 const linkage: std.builtin.GlobalLinkage, const visibility: Builder.Visibility, const is_dll_import: bool = switch (nav.resolved.?.value) {
......@@ -3027,7 +3014,7 @@ pub const Object = struct {
30273014 .strong, .weak => nav.name,
30283015 .link_once => unreachable,
30293016 }.toSlice(ip)),
3030 try o.lowerType(pt, .fromInterned(nav.resolved.?.type)),
3017 try o.lowerType(.fromInterned(nav.resolved.?.type)),
30313018 toLlvmGlobalAddressSpace(nav.resolved.?.@"addrspace", zcu.getTarget()),
30323019 );
30333020 gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;
......@@ -3060,8 +3047,8 @@ pub const Object = struct {
30603047 return o.builder.intType(o.zcu.errorSetBits());
30613048 }
30623049
3063 pub fn lowerType(o: *Object, pt: Zcu.PerThread, t: Type) Allocator.Error!Builder.Type {
3064 const zcu = pt.zcu;
3050 pub fn lowerType(o: *Object, t: Type) Allocator.Error!Builder.Type {
3051 const zcu = o.zcu;
30653052 const target = zcu.getTarget();
30663053 const ip = &zcu.intern_pool;
30673054 return switch (t.toIntern()) {
......@@ -3134,7 +3121,7 @@ pub const Object = struct {
31343121 => .ptr,
31353122 .slice_const_u8_type,
31363123 .slice_const_u8_sentinel_0_type,
3137 => try o.builder.structType(.normal, &.{ .ptr, try o.lowerType(pt, Type.usize) }),
3124 => try o.builder.structType(.normal, &.{ .ptr, try o.lowerType(.usize) }),
31383125 .optional_noreturn_type => unreachable,
31393126 .anyerror_void_error_union_type,
31403127 .adhoc_inferred_error_set_type,
......@@ -3175,24 +3162,24 @@ pub const Object = struct {
31753162 .one, .many, .c => ptr_ty,
31763163 .slice => try o.builder.structType(.normal, &.{
31773164 ptr_ty,
3178 try o.lowerType(pt, Type.usize),
3165 try o.lowerType(.usize),
31793166 }),
31803167 };
31813168 },
31823169 .array_type => |array_type| o.builder.arrayType(
31833170 array_type.lenIncludingSentinel(),
3184 try o.lowerType(pt, Type.fromInterned(array_type.child)),
3171 try o.lowerType(.fromInterned(array_type.child)),
31853172 ),
31863173 .vector_type => |vector_type| o.builder.vectorType(
31873174 .normal,
31883175 vector_type.len,
3189 try o.lowerType(pt, Type.fromInterned(vector_type.child)),
3176 try o.lowerType(.fromInterned(vector_type.child)),
31903177 ),
31913178 .opt_type => |child_ty| {
31923179 // Must stay in sync with `opt_payload` logic in `lowerPtr`.
31933180 if (!Type.fromInterned(child_ty).hasRuntimeBits(zcu)) return .i8;
31943181
3195 const payload_ty = try o.lowerType(pt, Type.fromInterned(child_ty));
3182 const payload_ty = try o.lowerType(.fromInterned(child_ty));
31963183 if (t.optionalReprIsPayload(zcu)) return payload_ty;
31973184
31983185 comptime assert(optional_layout_version == 3);
......@@ -3214,7 +3201,7 @@ pub const Object = struct {
32143201 const error_type = try o.errorIntType();
32153202 if (!Type.fromInterned(error_union_type.payload_type).hasRuntimeBits(zcu))
32163203 return error_type;
3217 const payload_type = try o.lowerType(pt, Type.fromInterned(error_union_type.payload_type));
3204 const payload_type = try o.lowerType(.fromInterned(error_union_type.payload_type));
32183205
32193206 const payload_align = Type.fromInterned(error_union_type.payload_type).abiAlignment(zcu);
32203207 const error_align: InternPool.Alignment = .fromByteUnits(std.zig.target.intAlignment(target, zcu.errorSetBits()));
......@@ -3254,7 +3241,7 @@ pub const Object = struct {
32543241 const struct_type = ip.loadStructType(t.toIntern());
32553242
32563243 if (struct_type.layout == .@"packed") {
3257 const int_ty = try o.lowerType(pt, .fromInterned(struct_type.packed_backing_int_type));
3244 const int_ty = try o.lowerType(.fromInterned(struct_type.packed_backing_int_type));
32583245 try o.type_map.put(o.gpa, t.toIntern(), int_ty);
32593246 return int_ty;
32603247 }
......@@ -3290,7 +3277,7 @@ pub const Object = struct {
32903277
32913278 if (!field_ty.hasRuntimeBits(zcu)) continue;
32923279
3293 try llvm_field_types.append(o.gpa, try o.lowerType(pt, field_ty));
3280 try llvm_field_types.append(o.gpa, try o.lowerType(field_ty));
32943281
32953282 offset += field_ty.abiSize(zcu);
32963283 }
......@@ -3346,7 +3333,7 @@ pub const Object = struct {
33463333 if (!Type.fromInterned(field_ty).hasRuntimeBits(zcu)) {
33473334 continue;
33483335 }
3349 try llvm_field_types.append(o.gpa, try o.lowerType(pt, Type.fromInterned(field_ty)));
3336 try llvm_field_types.append(o.gpa, try o.lowerType(.fromInterned(field_ty)));
33503337
33513338 offset += Type.fromInterned(field_ty).abiSize(zcu);
33523339 }
......@@ -3367,7 +3354,7 @@ pub const Object = struct {
33673354 const union_obj = ip.loadUnionType(t.toIntern());
33683355
33693356 if (union_obj.layout == .@"packed") {
3370 const int_ty = try o.lowerType(pt, .fromInterned(union_obj.packed_backing_int_type));
3357 const int_ty = try o.lowerType(.fromInterned(union_obj.packed_backing_int_type));
33713358 try o.type_map.put(o.gpa, t.toIntern(), int_ty);
33723359 return int_ty;
33733360 }
......@@ -3375,13 +3362,13 @@ pub const Object = struct {
33753362 const layout = Type.getUnionLayout(union_obj, zcu);
33763363
33773364 if (layout.payload_size == 0) {
3378 const enum_tag_ty = try o.lowerType(pt, .fromInterned(union_obj.enum_tag_type));
3365 const enum_tag_ty = try o.lowerType(.fromInterned(union_obj.enum_tag_type));
33793366 try o.type_map.put(o.gpa, t.toIntern(), enum_tag_ty);
33803367 return enum_tag_ty;
33813368 }
33823369
33833370 const aligned_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[layout.most_aligned_field]);
3384 const aligned_field_llvm_ty = try o.lowerType(pt, aligned_field_ty);
3371 const aligned_field_llvm_ty = try o.lowerType(aligned_field_ty);
33853372
33863373 const payload_ty = ty: {
33873374 if (layout.most_aligned_field_size == layout.payload_size) {
......@@ -3407,7 +3394,7 @@ pub const Object = struct {
34073394 );
34083395 return ty;
34093396 }
3410 const enum_tag_ty = try o.lowerType(pt, .fromInterned(union_obj.enum_tag_type));
3397 const enum_tag_ty = try o.lowerType(.fromInterned(union_obj.enum_tag_type));
34113398
34123399 // Put the tag before or after the payload depending on which one's
34133400 // alignment is greater.
......@@ -3442,8 +3429,8 @@ pub const Object = struct {
34423429 }
34433430 return gop.value_ptr.*;
34443431 },
3445 .enum_type => try o.lowerType(pt, t.intTagType(zcu)),
3446 .func_type => |func_type| try o.lowerFnType(pt, func_type),
3432 .enum_type => try o.lowerType(t.intTagType(zcu)),
3433 .func_type => |func_type| try o.lowerFnType(func_type),
34473434 .error_set_type, .inferred_error_set_type => try o.errorIntType(),
34483435 // values, not types
34493436 .undef,
......@@ -3469,11 +3456,11 @@ pub const Object = struct {
34693456 };
34703457 }
34713458
3472 fn lowerFnType(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
3473 const zcu = pt.zcu;
3459 fn lowerFnType(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
3460 const zcu = o.zcu;
34743461 const ip = &zcu.intern_pool;
34753462 const target = zcu.getTarget();
3476 const ret_ty = try lowerFnRetTy(o, pt, fn_info);
3463 const ret_ty = try lowerFnRetTy(o, fn_info);
34773464
34783465 var llvm_params: std.ArrayList(Builder.Type) = .empty;
34793466 defer llvm_params.deinit(o.gpa);
......@@ -3488,12 +3475,12 @@ pub const Object = struct {
34883475 try llvm_params.append(o.gpa, llvm_ptr_ty);
34893476 }
34903477
3491 var it = iterateParamTypes(o, pt, fn_info);
3478 var it = iterateParamTypes(o, fn_info);
34923479 while (try it.next()) |lowering| switch (lowering) {
34933480 .no_bits => continue,
34943481 .byval => {
34953482 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
3496 try llvm_params.append(o.gpa, try o.lowerType(pt, param_ty));
3483 try llvm_params.append(o.gpa, try o.lowerType(param_ty));
34973484 },
34983485 .byref, .byref_mut => {
34993486 try llvm_params.append(o.gpa, .ptr);
......@@ -3508,7 +3495,7 @@ pub const Object = struct {
35083495 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
35093496 try llvm_params.appendSlice(o.gpa, &.{
35103497 try o.builder.ptrType(toLlvmAddressSpace(param_ty.ptrAddressSpace(zcu), target)),
3511 try o.lowerType(pt, Type.usize),
3498 try o.lowerType(.usize),
35123499 });
35133500 },
35143501 .multiple_llvm_types => {
......@@ -3516,7 +3503,7 @@ pub const Object = struct {
35163503 },
35173504 .float_array => |count| {
35183505 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
3519 const float_ty = try o.lowerType(pt, aarch64_c_abi.getFloatArrayType(param_ty, zcu).?);
3506 const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(param_ty, zcu).?);
35203507 try llvm_params.append(o.gpa, try o.builder.arrayType(count, float_ty));
35213508 },
35223509 .i32_array, .i64_array => |arr_len| {
......@@ -3535,8 +3522,8 @@ pub const Object = struct {
35353522 );
35363523 }
35373524
3538 pub fn lowerValue(o: *Object, pt: Zcu.PerThread, arg_val: InternPool.Index) Allocator.Error!Builder.Constant {
3539 const zcu = pt.zcu;
3525 pub fn lowerValue(o: *Object, arg_val: InternPool.Index) Allocator.Error!Builder.Constant {
3526 const zcu = o.zcu;
35403527 const ip = &zcu.intern_pool;
35413528 const target = zcu.getTarget();
35423529
......@@ -3544,7 +3531,7 @@ pub const Object = struct {
35443531 const val_key = ip.indexToKey(val.toIntern());
35453532
35463533 if (val.isUndef(zcu)) {
3547 return o.builder.undefConst(try o.lowerType(pt, Type.fromInterned(val_key.typeOf())));
3534 return o.builder.undefConst(try o.lowerType(.fromInterned(val_key.typeOf())));
35483535 }
35493536
35503537 const ty: Type = .fromInterned(val_key.typeOf());
......@@ -3580,45 +3567,45 @@ pub const Object = struct {
35803567 },
35813568 .enum_literal => unreachable, // non-runtime value
35823569 .@"extern" => |@"extern"| {
3583 const function_index = try o.resolveLlvmFunction(pt, @"extern".owner_nav);
3570 const function_index = try o.resolveLlvmFunction(@"extern".owner_nav);
35843571 return function_index.ptrConst(&o.builder).global.toConst();
35853572 },
35863573 .func => |func| {
3587 const function_index = try o.resolveLlvmFunction(pt, func.owner_nav);
3574 const function_index = try o.resolveLlvmFunction(func.owner_nav);
35883575 return function_index.ptrConst(&o.builder).global.toConst();
35893576 },
35903577 .int => {
35913578 var bigint_space: Value.BigIntSpace = undefined;
35923579 const bigint = val.toBigInt(&bigint_space, zcu);
3593 return lowerBigInt(o, pt, ty, bigint);
3580 const llvm_int_ty = try o.builder.intType(ty.intInfo(zcu).bits);
3581 return o.builder.bigIntConst(llvm_int_ty, bigint);
35943582 },
35953583 .err => |err| {
3596 const int = try pt.getErrorValue(err.name);
3597 const llvm_int = try o.builder.intConst(try o.errorIntType(), int);
3598 return llvm_int;
3584 const int = zcu.intern_pool.getErrorValueIfExists(err.name).?;
3585 return o.builder.intConst(try o.errorIntType(), int);
35993586 },
36003587 .error_union => |error_union| {
3601 const err_val = switch (error_union.val) {
3602 .err_name => |err_name| try pt.intern(.{ .err = .{
3603 .ty = ty.errorUnionSet(zcu).toIntern(),
3604 .name = err_name,
3605 } }),
3606 .payload => (try pt.intValue(try pt.errorIntType(), 0)).toIntern(),
3588 const llvm_error_ty = try o.errorIntType();
3589 const llvm_error_value = switch (error_union.val) {
3590 .err_name => |name| try o.builder.intConst(
3591 llvm_error_ty,
3592 zcu.intern_pool.getErrorValueIfExists(name).?,
3593 ),
3594 .payload => try o.builder.intConst(llvm_error_ty, 0),
36073595 };
3608 const err_int_ty = try pt.errorIntType();
3596
36093597 const payload_type = ty.errorUnionPayload(zcu);
36103598 if (!payload_type.hasRuntimeBits(zcu)) {
36113599 // We use the error type directly as the type.
3612 return o.lowerValue(pt, err_val);
3600 return llvm_error_value;
36133601 }
36143602
36153603 const payload_align = payload_type.abiAlignment(zcu);
3616 const error_align = err_int_ty.abiAlignment(zcu);
3617 const llvm_error_value = try o.lowerValue(pt, err_val);
3618 const llvm_payload_value = try o.lowerValue(pt, switch (error_union.val) {
3619 .err_name => try pt.intern(.{ .undef = payload_type.toIntern() }),
3620 .payload => |payload| payload,
3621 });
3604 const error_align = Type.errorAbiAlignment(zcu);
3605 const llvm_payload_value = switch (error_union.val) {
3606 .err_name => try o.builder.undefConst(try o.lowerType(payload_type)),
3607 .payload => |payload| try o.lowerValue(payload),
3608 };
36223609
36233610 var fields: [3]Builder.Type = undefined;
36243611 var vals: [3]Builder.Constant = undefined;
......@@ -3632,7 +3619,7 @@ pub const Object = struct {
36323619 fields[0] = vals[0].typeOf(&o.builder);
36333620 fields[1] = vals[1].typeOf(&o.builder);
36343621
3635 const llvm_ty = try o.lowerType(pt, ty);
3622 const llvm_ty = try o.lowerType(ty);
36363623 const llvm_ty_fields = llvm_ty.structFields(&o.builder);
36373624 if (llvm_ty_fields.len > 2) {
36383625 assert(llvm_ty_fields.len == 3);
......@@ -3644,7 +3631,7 @@ pub const Object = struct {
36443631 fields[0..llvm_ty_fields.len],
36453632 ), vals[0..llvm_ty_fields.len]);
36463633 },
3647 .enum_tag => |enum_tag| o.lowerValue(pt, enum_tag.int),
3634 .enum_tag => |enum_tag| o.lowerValue(enum_tag.int),
36483635 .float => switch (ty.floatBits(target)) {
36493636 16 => if (backendSupportsF16(target))
36503637 try o.builder.halfConst(val.toFloat(f16, zcu))
......@@ -3659,10 +3646,10 @@ pub const Object = struct {
36593646 128 => try o.builder.fp128Const(val.toFloat(f128, zcu)),
36603647 else => unreachable,
36613648 },
3662 .ptr => try o.lowerPtr(pt, arg_val, 0),
3663 .slice => |slice| return o.builder.structConst(try o.lowerType(pt, ty), &.{
3664 try o.lowerValue(pt, slice.ptr),
3665 try o.lowerValue(pt, slice.len),
3649 .ptr => try o.lowerPtr(arg_val, 0),
3650 .slice => |slice| return o.builder.structConst(try o.lowerType(ty), &.{
3651 try o.lowerValue(slice.ptr),
3652 try o.lowerValue(slice.len),
36663653 }),
36673654 .opt => |opt| {
36683655 comptime assert(optional_layout_version == 3);
......@@ -3672,7 +3659,7 @@ pub const Object = struct {
36723659 if (!payload_ty.hasRuntimeBits(zcu)) {
36733660 return non_null_bit;
36743661 }
3675 const llvm_ty = try o.lowerType(pt, ty);
3662 const llvm_ty = try o.lowerType(ty);
36763663 if (ty.optionalReprIsPayload(zcu)) return switch (opt.val) {
36773664 .none => switch (llvm_ty.tag(&o.builder)) {
36783665 .integer => try o.builder.intConst(llvm_ty, 0),
......@@ -3680,16 +3667,16 @@ pub const Object = struct {
36803667 .structure => try o.builder.zeroInitConst(llvm_ty),
36813668 else => unreachable,
36823669 },
3683 else => |payload| try o.lowerValue(pt, payload),
3670 else => |payload| try o.lowerValue(payload),
36843671 };
36853672 assert(payload_ty.zigTypeTag(zcu) != .@"fn");
36863673
36873674 var fields: [3]Builder.Type = undefined;
36883675 var vals: [3]Builder.Constant = undefined;
3689 vals[0] = try o.lowerValue(pt, switch (opt.val) {
3690 .none => try pt.intern(.{ .undef = payload_ty.toIntern() }),
3691 else => |payload| payload,
3692 });
3676 vals[0] = switch (opt.val) {
3677 .none => try o.builder.undefConst(try o.lowerType(payload_ty)),
3678 else => |payload| try o.lowerValue(payload),
3679 };
36933680 vals[1] = non_null_bit;
36943681 fields[0] = vals[0].typeOf(&o.builder);
36953682 fields[1] = vals[1].typeOf(&o.builder);
......@@ -3705,14 +3692,14 @@ pub const Object = struct {
37053692 fields[0..llvm_ty_fields.len],
37063693 ), vals[0..llvm_ty_fields.len]);
37073694 },
3708 .bitpack => |bitpack| return o.lowerValue(pt, bitpack.backing_int_val),
3695 .bitpack => |bitpack| return o.lowerValue(bitpack.backing_int_val),
37093696 .aggregate => |aggregate| switch (ip.indexToKey(ty.toIntern())) {
37103697 .array_type => |array_type| switch (aggregate.storage) {
37113698 .bytes => |bytes| try o.builder.stringConst(try o.builder.string(
37123699 bytes.toSlice(array_type.lenIncludingSentinel(), ip),
37133700 )),
37143701 .elems => |elems| {
3715 const array_ty = try o.lowerType(pt, ty);
3702 const array_ty = try o.lowerType(ty);
37163703 const elem_ty = array_ty.childType(&o.builder);
37173704 assert(elems.len == array_ty.aggregateLen(&o.builder));
37183705
......@@ -3732,7 +3719,7 @@ pub const Object = struct {
37323719
37333720 var need_unnamed = false;
37343721 for (vals, fields, elems) |*result_val, *result_field, elem| {
3735 result_val.* = try o.lowerValue(pt, elem);
3722 result_val.* = try o.lowerValue(elem);
37363723 result_field.* = result_val.typeOf(&o.builder);
37373724 if (result_field.* != elem_ty) need_unnamed = true;
37383725 }
......@@ -3744,7 +3731,7 @@ pub const Object = struct {
37443731 .repeated_elem => |elem| {
37453732 const len: usize = @intCast(array_type.len);
37463733 const len_including_sentinel: usize = @intCast(array_type.lenIncludingSentinel());
3747 const array_ty = try o.lowerType(pt, ty);
3734 const array_ty = try o.lowerType(ty);
37483735 const elem_ty = array_ty.childType(&o.builder);
37493736
37503737 const ExpectedContents = extern struct {
......@@ -3762,12 +3749,12 @@ pub const Object = struct {
37623749 defer allocator.free(fields);
37633750
37643751 var need_unnamed = false;
3765 @memset(vals[0..len], try o.lowerValue(pt, elem));
3752 @memset(vals[0..len], try o.lowerValue(elem));
37663753 @memset(fields[0..len], vals[0].typeOf(&o.builder));
37673754 if (fields[0] != elem_ty) need_unnamed = true;
37683755
37693756 if (array_type.sentinel != .none) {
3770 vals[len] = try o.lowerValue(pt, array_type.sentinel);
3757 vals[len] = try o.lowerValue(array_type.sentinel);
37713758 fields[len] = vals[len].typeOf(&o.builder);
37723759 if (fields[len] != elem_ty) need_unnamed = true;
37733760 }
......@@ -3779,7 +3766,7 @@ pub const Object = struct {
37793766 },
37803767 },
37813768 .vector_type => |vector_type| {
3782 const vector_ty = try o.lowerType(pt, ty);
3769 const vector_ty = try o.lowerType(ty);
37833770 switch (aggregate.storage) {
37843771 .bytes, .elems => {
37853772 const ExpectedContents = [Builder.expected_fields_len]Builder.Constant;
......@@ -3796,7 +3783,7 @@ pub const Object = struct {
37963783 result_val.* = try o.builder.intConst(.i8, byte);
37973784 },
37983785 .elems => |elems| for (vals, elems) |*result_val, elem| {
3799 result_val.* = try o.lowerValue(pt, elem);
3786 result_val.* = try o.lowerValue(elem);
38003787 },
38013788 .repeated_elem => unreachable,
38023789 }
......@@ -3804,12 +3791,12 @@ pub const Object = struct {
38043791 },
38053792 .repeated_elem => |elem| return o.builder.splatConst(
38063793 vector_ty,
3807 try o.lowerValue(pt, elem),
3794 try o.lowerValue(elem),
38083795 ),
38093796 }
38103797 },
38113798 .tuple_type => |tuple| {
3812 const struct_ty = try o.lowerType(pt, ty);
3799 const struct_ty = try o.lowerType(ty);
38133800 const llvm_len = struct_ty.aggregateLen(&o.builder);
38143801
38153802 const ExpectedContents = extern struct {
......@@ -3835,8 +3822,8 @@ pub const Object = struct {
38353822 tuple.types.get(ip),
38363823 tuple.values.get(ip),
38373824 0..,
3838 ) |field_ty, field_val, field_index| {
3839 if (field_val != .none) continue;
3825 ) |field_ty, field_comptime_val, field_index| {
3826 if (field_comptime_val != .none) continue;
38403827 if (!Type.fromInterned(field_ty).hasRuntimeBits(zcu)) continue;
38413828
38423829 const field_align = Type.fromInterned(field_ty).abiAlignment(zcu);
......@@ -3854,8 +3841,11 @@ pub const Object = struct {
38543841 llvm_index += 1;
38553842 }
38563843
3857 vals[llvm_index] =
3858 try o.lowerValue(pt, (try val.fieldValue(pt, field_index)).toIntern());
3844 vals[llvm_index] = switch (aggregate.storage) {
3845 .bytes => |bytes| try o.builder.intConst(.i8, bytes.at(field_index, ip)),
3846 .elems => |elems| try o.lowerValue(elems[field_index]),
3847 .repeated_elem => |elem| try o.lowerValue(elem),
3848 };
38593849 fields[llvm_index] = vals[llvm_index].typeOf(&o.builder);
38603850 if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index])
38613851 need_unnamed = true;
......@@ -3883,7 +3873,7 @@ pub const Object = struct {
38833873 },
38843874 .struct_type => {
38853875 const struct_type = ip.loadStructType(ty.toIntern());
3886 const struct_ty = try o.lowerType(pt, ty);
3876 const struct_ty = try o.lowerType(ty);
38873877 assert(struct_type.layout != .@"packed");
38883878 const llvm_len = struct_ty.aggregateLen(&o.builder);
38893879
......@@ -3927,10 +3917,11 @@ pub const Object = struct {
39273917 continue;
39283918 }
39293919
3930 vals[llvm_index] = try o.lowerValue(
3931 pt,
3932 (try val.fieldValue(pt, field_index)).toIntern(),
3933 );
3920 vals[llvm_index] = switch (aggregate.storage) {
3921 .bytes => |bytes| try o.builder.intConst(.i8, bytes.at(field_index, ip)),
3922 .elems => |elems| try o.lowerValue(elems[field_index]),
3923 .repeated_elem => |elem| try o.lowerValue(elem),
3924 };
39343925 fields[llvm_index] = vals[llvm_index].typeOf(&o.builder);
39353926 if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index])
39363927 need_unnamed = true;
......@@ -3959,9 +3950,9 @@ pub const Object = struct {
39593950 else => unreachable,
39603951 },
39613952 .un => |un| {
3962 const union_ty = try o.lowerType(pt, ty);
3953 const union_ty = try o.lowerType(ty);
39633954 const layout = ty.unionGetLayout(zcu);
3964 if (layout.payload_size == 0) return o.lowerValue(pt, un.tag);
3955 if (layout.payload_size == 0) return o.lowerValue(un.tag);
39653956
39663957 const union_obj = zcu.typeToUnion(ty).?;
39673958 const container_layout = union_obj.layout;
......@@ -3982,7 +3973,7 @@ pub const Object = struct {
39823973 const padding_len = layout.payload_size;
39833974 break :p try o.builder.undefConst(try o.builder.arrayType(padding_len, .i8));
39843975 }
3985 const payload = try o.lowerValue(pt, un.val);
3976 const payload = try o.lowerValue(un.val);
39863977 const payload_ty = payload.typeOf(&o.builder);
39873978 if (payload_ty != union_ty.structFields(&o.builder)[
39883979 @intFromBool(layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align))
......@@ -3997,7 +3988,7 @@ pub const Object = struct {
39973988 );
39983989 } else p: {
39993990 assert(layout.tag_size == 0);
4000 const union_val = try o.lowerValue(pt, un.val);
3991 const union_val = try o.lowerValue(un.val);
40013992 need_unnamed = true;
40023993 break :p union_val;
40033994 };
......@@ -4007,7 +3998,7 @@ pub const Object = struct {
40073998 try o.builder.structType(union_ty.structKind(&o.builder), &.{payload_ty})
40083999 else
40094000 union_ty, &.{payload});
4010 const tag = try o.lowerValue(pt, un.tag);
4001 const tag = try o.lowerValue(un.tag);
40114002 const tag_ty = tag.typeOf(&o.builder);
40124003 var fields: [3]Builder.Type = undefined;
40134004 var vals: [3]Builder.Constant = undefined;
......@@ -4033,52 +4024,45 @@ pub const Object = struct {
40334024 };
40344025 }
40354026
4036 fn lowerBigInt(
4037 o: *Object,
4038 pt: Zcu.PerThread,
4039 ty: Type,
4040 bigint: std.math.big.int.Const,
4041 ) Allocator.Error!Builder.Constant {
4042 const zcu = pt.zcu;
4043 return o.builder.bigIntConst(try o.builder.intType(ty.intInfo(zcu).bits), bigint);
4044 }
4045
40464027 fn lowerPtr(
40474028 o: *Object,
4048 pt: Zcu.PerThread,
40494029 ptr_val: InternPool.Index,
40504030 prev_offset: u64,
40514031 ) Allocator.Error!Builder.Constant {
4052 const zcu = pt.zcu;
4032 const zcu = o.zcu;
40534033 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;
40544034 const offset: u64 = prev_offset + ptr.byte_offset;
40554035 return switch (ptr.base_addr) {
40564036 .nav => |nav| {
4057 const base_ptr = try o.lowerNavRefValue(pt, nav);
4037 const base_ptr = try o.lowerNavRefValue(nav);
40584038 return o.builder.gepConst(.inbounds, .i8, base_ptr, null, &.{
40594039 try o.builder.intConst(.i64, offset),
40604040 });
40614041 },
40624042 .uav => |uav| {
4063 const base_ptr = try o.lowerUavRef(pt, uav);
4043 const orig_ptr_ty: Type = .fromInterned(uav.orig_ty);
4044 const base_ptr = try o.lowerUavRef(
4045 uav.val,
4046 orig_ptr_ty.ptrAlignment(zcu),
4047 orig_ptr_ty.ptrAddressSpace(zcu),
4048 );
40644049 return o.builder.gepConst(.inbounds, .i8, base_ptr, null, &.{
40654050 try o.builder.intConst(.i64, offset),
40664051 });
40674052 },
40684053 .int => try o.builder.castConst(
40694054 .inttoptr,
4070 try o.builder.intConst(try o.lowerType(pt, Type.usize), offset),
4071 try o.lowerType(pt, Type.fromInterned(ptr.ty)),
4055 try o.builder.intConst(try o.lowerType(.usize), offset),
4056 try o.lowerType(.fromInterned(ptr.ty)),
40724057 ),
40734058 .eu_payload => |eu_ptr| try o.lowerPtr(
4074 pt,
40754059 eu_ptr,
40764060 offset + codegen.errUnionPayloadOffset(
40774061 Value.fromInterned(eu_ptr).typeOf(zcu).childType(zcu),
40784062 zcu,
40794063 ),
40804064 ),
4081 .opt_payload => |opt_ptr| try o.lowerPtr(pt, opt_ptr, offset),
4065 .opt_payload => |opt_ptr| try o.lowerPtr(opt_ptr, offset),
40824066 .field => |field| {
40834067 const agg_ty = Value.fromInterned(field.base).typeOf(zcu).childType(zcu);
40844068 const field_off: u64 = switch (agg_ty.zigTypeTag(zcu)) {
......@@ -4096,13 +4080,13 @@ pub const Object = struct {
40964080 },
40974081 else => unreachable,
40984082 };
4099 return o.lowerPtr(pt, field.base, offset + field_off);
4083 return o.lowerPtr(field.base, offset + field_off);
41004084 },
41014085 .arr_elem => |arr_elem| {
41024086 const base_ptr_ty = Value.fromInterned(arr_elem.base).typeOf(zcu);
41034087 assert(base_ptr_ty.ptrSize(zcu) == .many);
41044088 const elem_size = base_ptr_ty.childType(zcu).abiSize(zcu);
4105 return o.lowerPtr(pt, arr_elem.base, offset + elem_size * arr_elem.index);
4089 return o.lowerPtr(arr_elem.base, offset + elem_size * arr_elem.index);
41064090 },
41074091 .comptime_field => unreachable,
41084092 .comptime_alloc => unreachable,
......@@ -4113,88 +4097,84 @@ pub const Object = struct {
41134097 /// Maybe the logic could be unified.
41144098 pub fn lowerUavRef(
41154099 o: *Object,
4116 pt: Zcu.PerThread,
4117 uav: InternPool.Key.Ptr.BaseAddr.Uav,
4100 uav_val: InternPool.Index,
4101 /// Must not be `.none`.
4102 @"align": InternPool.Alignment,
4103 @"addrspace": std.builtin.AddressSpace,
41184104 ) Allocator.Error!Builder.Constant {
4119 const zcu = pt.zcu;
4105 assert(@"align" != .none);
4106
4107 const zcu = o.zcu;
41204108 const ip = &zcu.intern_pool;
4121 const uav_val = uav.val;
4122 const uav_ty = Type.fromInterned(ip.typeOf(uav_val));
4123 const target = zcu.getTarget();
4109 const uav_ty: Type = .fromInterned(ip.typeOf(uav_val));
41244110
41254111 switch (ip.indexToKey(uav_val)) {
4126 .func => @panic("TODO"),
4127 .@"extern" => @panic("TODO"),
4112 .func => unreachable, // should be using a Nav ref
4113 .@"extern" => unreachable, // should be using a Nav ref
41284114 else => {},
41294115 }
41304116
4131 const ptr_ty = Type.fromInterned(uav.orig_ty);
4132
4133 if (!uav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) {
4134 return o.lowerPtrToVoid(pt, ptr_ty);
4117 if (!uav_ty.hasRuntimeBits(zcu)) {
4118 return o.lowerPtrToVoid(@"align", @"addrspace");
41354119 }
41364120
4137 assert(uav_ty.zigTypeTag(zcu) != .@"fn"); // should be using a Nav ref
4138
4139 const llvm_addr_space = toLlvmAddressSpace(ptr_ty.ptrAddressSpace(zcu), target);
4140 const alignment = ptr_ty.ptrAlignment(zcu);
4141 const llvm_global = (try o.resolveGlobalUav(pt, uav.val, llvm_addr_space, alignment)).ptrConst(&o.builder).global;
4121 const llvm_addrspace = toLlvmAddressSpace(@"addrspace", zcu.getTarget());
4122 const llvm_global = (try o.resolveGlobalUav(uav_val, llvm_addrspace, @"align")).ptrConst(&o.builder).global;
41424123
4143 const llvm_val = try o.builder.convConst(
4124 return o.builder.convConst(
41444125 llvm_global.toConst(),
4145 try o.builder.ptrType(llvm_addr_space),
4126 try o.builder.ptrType(llvm_addrspace),
41464127 );
4147
4148 return o.builder.convConst(llvm_val, try o.lowerType(pt, ptr_ty));
41494128 }
41504129
4151 pub fn lowerNavRefValue(o: *Object, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) Allocator.Error!Builder.Constant {
4152 const zcu = pt.zcu;
4130 pub fn lowerNavRefValue(o: *Object, nav_index: InternPool.Nav.Index) Allocator.Error!Builder.Constant {
4131 const zcu = o.zcu;
41534132 const ip = &zcu.intern_pool;
41544133
41554134 const nav = ip.getNav(nav_index);
41564135
41574136 const nav_ty: Type = .fromInterned(nav.resolved.?.type);
4158 const ptr_ty = try pt.navPtrType(nav_index);
41594137
41604138 if (nav.getExtern(ip) == null and !nav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) {
4161 return o.lowerPtrToVoid(pt, ptr_ty);
4139 return o.lowerPtrToVoid(nav.resolved.?.@"align", nav.resolved.?.@"addrspace");
41624140 }
41634141
41644142 const llvm_global = if (nav_ty.zigTypeTag(zcu) == .@"fn")
4165 (try o.resolveLlvmFunction(pt, nav_index)).ptrConst(&o.builder).global
4143 (try o.resolveLlvmFunction(nav_index)).ptrConst(&o.builder).global
41664144 else
4167 (try o.resolveGlobalNav(pt, nav_index)).ptrConst(&o.builder).global;
4145 (try o.resolveGlobalNav(nav_index)).ptrConst(&o.builder).global;
41684146
4169 const llvm_val = try o.builder.convConst(
4147 return try o.builder.convConst(
41704148 llvm_global.toConst(),
41714149 try o.builder.ptrType(toLlvmAddressSpace(nav.resolved.?.@"addrspace", zcu.getTarget())),
41724150 );
4173
4174 return o.builder.convConst(llvm_val, try o.lowerType(pt, ptr_ty));
41754151 }
41764152
4177 pub fn lowerPtrToVoid(o: *Object, pt: Zcu.PerThread, ptr_ty: Type) Allocator.Error!Builder.Constant {
4178 const zcu = pt.zcu;
4153 pub fn lowerPtrToVoid(
4154 o: *Object,
4155 @"align": InternPool.Alignment,
4156 @"addrspace": std.builtin.AddressSpace,
4157 ) Allocator.Error!Builder.Constant {
4158 const target = o.zcu.getTarget();
41794159 // Even though we are pointing at something which has zero bits (e.g. `void`),
41804160 // Pointers are defined to have bits. So we must return something here.
41814161 // The value cannot be undefined, because we use the `nonnull` annotation
41824162 // for non-optional pointers. We also need to respect the alignment, even though
41834163 // the address will never be dereferenced.
4184 const int: u64 = ptr_ty.ptrInfo(zcu).flags.alignment.toByteUnits() orelse
4164 const int: u64 = @"align".toByteUnits() orelse
41854165 // Note that these 0xaa values are appropriate even in release-optimized builds
41864166 // because we need a well-defined value that is not null, and LLVM does not
41874167 // have an "undef_but_not_null" attribute. As an example, if this `alloc` AIR
41884168 // instruction is followed by a `wrap_optional`, it will return this value
41894169 // verbatim, and the result should test as non-null.
4190 switch (zcu.getTarget().ptrBitWidth()) {
4170 switch (target.ptrBitWidth()) {
41914171 16 => 0xaaaa,
41924172 32 => 0xaaaaaaaa,
41934173 64 => 0xaaaaaaaa_aaaaaaaa,
41944174 else => unreachable,
41954175 };
4196 const llvm_usize = try o.lowerType(pt, Type.usize);
4197 const llvm_ptr_ty = try o.lowerType(pt, ptr_ty);
4176 const llvm_usize = try o.lowerType(.usize);
4177 const llvm_ptr_ty = try o.builder.ptrType(toLlvmAddressSpace(@"addrspace", target));
41984178 return o.builder.castConst(.inttoptr, try o.builder.intConst(llvm_usize, int), llvm_ptr_ty);
41994179 }
42004180
......@@ -4207,7 +4187,7 @@ pub const Object = struct {
42074187 fn_info: InternPool.Key.FuncType,
42084188 llvm_arg_i: u32,
42094189 ) Allocator.Error!void {
4210 const zcu = pt.zcu;
4190 const zcu = o.zcu;
42114191 if (param_ty.isPtrAtRuntime(zcu)) {
42124192 const ptr_info = param_ty.ptrInfo(zcu);
42134193 if (std.math.cast(u5, param_index)) |i| {
......@@ -4226,7 +4206,7 @@ pub const Object = struct {
42264206 .x86_64_interrupt,
42274207 .x86_interrupt,
42284208 => {
4229 const child_type = try lowerType(o, pt, Type.fromInterned(ptr_info.child));
4209 const child_type = try lowerType(o, Type.fromInterned(ptr_info.child));
42304210 try attributes.addParamAttr(llvm_arg_i, .{ .byval = child_type }, &o.builder);
42314211 },
42324212 }
......@@ -4292,8 +4272,8 @@ pub const Object = struct {
42924272 }
42934273
42944274 /// MLUGG TODO: this also needs incremental updates dumbass
4295 pub fn getEnumTagNameFunction(o: *Object, pt: Zcu.PerThread, enum_ty: Type) !Builder.Function.Index {
4296 const zcu = pt.zcu;
4275 pub fn getEnumTagNameFunction(o: *Object, enum_ty: Type) !Builder.Function.Index {
4276 const zcu = o.zcu;
42974277 const ip = &zcu.intern_pool;
42984278 const enum_type = ip.loadEnumType(enum_ty.toIntern());
42994279
......@@ -4301,11 +4281,12 @@ pub const Object = struct {
43014281 if (gop.found_existing) return gop.value_ptr.ptrConst(&o.builder).kind.function;
43024282 errdefer assert(o.enum_tag_name_map.remove(enum_ty.toIntern()));
43034283
4304 const usize_ty = try o.lowerType(pt, Type.usize);
4305 const ret_ty = try o.lowerType(pt, Type.slice_const_u8_sentinel_0);
4284 const usize_ty = try o.lowerType(.usize);
4285 const ret_ty = try o.lowerType(.slice_const_u8_sentinel_0);
4286 const llvm_int_ty = try o.lowerType(.fromInterned(enum_type.int_tag_type));
43064287 const target = &zcu.root_mod.resolved_target.result;
43074288 const function_index = try o.builder.addFunction(
4308 try o.builder.fnType(ret_ty, &.{try o.lowerType(pt, Type.fromInterned(enum_type.int_tag_type))}, .normal),
4289 try o.builder.fnType(ret_ty, &.{llvm_int_ty}, .normal),
43094290 try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_type.name.fmt(ip)}),
43104291 toLlvmAddressSpace(.generic, target),
43114292 );
......@@ -4353,11 +4334,11 @@ pub const Object = struct {
43534334 });
43544335
43554336 const return_block = try wip.block(1, "Name");
4356 const this_tag_int_value = try o.lowerValue(
4357 pt,
4358 (try pt.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(),
4359 );
4360 try wip_switch.addCase(this_tag_int_value, return_block, &wip);
4337 const llvm_tag_val = switch (enum_type.field_values.getOrNone(ip, field_index)) {
4338 .none => try o.builder.intConst(llvm_int_ty, field_index), // auto-numbered
4339 else => |tag_val_ip| try o.lowerValue(tag_val_ip),
4340 };
4341 try wip_switch.addCase(llvm_tag_val, return_block, &wip);
43614342
43624343 wip.cursor = .{ .block = return_block };
43634344 _ = try wip.ret(name_val);
......@@ -4375,8 +4356,8 @@ pub const Object = struct {
43754356 return o.lazy_abi_aligns.items[@intFromEnum(index)];
43764357 }
43774358
4378 pub fn getIsNamedEnumValueFunction(o: *Object, pt: Zcu.PerThread, enum_ty: Type) !Builder.Function.Index {
4379 const zcu = pt.zcu;
4359 pub fn getIsNamedEnumValueFunction(o: *Object, enum_ty: Type) !Builder.Function.Index {
4360 const zcu = o.zcu;
43804361 const ip = &zcu.intern_pool;
43814362
43824363 const gop = try o.named_enum_map.getOrPut(o.gpa, enum_ty.toIntern());
......@@ -4390,23 +4371,21 @@ pub const Object = struct {
43904371 toLlvmAddressSpace(.generic, zcu.getTarget()),
43914372 );
43924373 gop.value_ptr.* = function_index;
4393 try o.updateIsNamedEnumValueFunction(pt, enum_ty, function_index);
4374 try o.updateIsNamedEnumValueFunction(enum_ty, function_index);
43944375 return function_index;
43954376 }
43964377 fn updateIsNamedEnumValueFunction(
43974378 o: *Object,
4398 pt: Zcu.PerThread,
43994379 enum_ty: Type,
44004380 function_index: Builder.Function.Index,
44014381 ) Allocator.Error!void {
4402 const zcu = pt.zcu;
4382 const zcu = o.zcu;
44034383 const builder = &o.builder;
44044384 const loaded_enum = zcu.intern_pool.loadEnumType(enum_ty.toIntern());
4405 function_index.ptrConst(builder).global.ptr(builder).type = try builder.fnType(
4406 .i1,
4407 &.{try o.lowerType(pt, .fromInterned(loaded_enum.int_tag_type))},
4408 .normal,
4409 );
4385
4386 const llvm_int_ty = try o.lowerType(.fromInterned(loaded_enum.int_tag_type));
4387 function_index.ptrConst(builder).global.ptr(builder).type =
4388 try builder.fnType(.i1, &.{llvm_int_ty}, .normal);
44104389
44114390 var attributes: Builder.FunctionAttributes.Wip = .{};
44124391 defer attributes.deinit(builder);
......@@ -4429,13 +4408,19 @@ pub const Object = struct {
44294408 var wip_switch = try wip.@"switch"(tag_int_value, unnamed_block, @intCast(loaded_enum.field_names.len), .none);
44304409 defer wip_switch.finish(&wip);
44314410
4432 for (0..loaded_enum.field_names.len) |field_index| {
4433 const this_tag_int_value = try o.lowerValue(
4434 pt,
4435 (try pt.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(),
4436 );
4437 try wip_switch.addCase(this_tag_int_value, named_block, &wip);
4411 if (loaded_enum.field_values.len > 0) {
4412 for (loaded_enum.field_values.get(&zcu.intern_pool)) |tag_val_ip| {
4413 const llvm_tag_val = try o.lowerValue(tag_val_ip);
4414 try wip_switch.addCase(llvm_tag_val, named_block, &wip);
4415 }
4416 } else {
4417 // Auto-numbered.
4418 for (0..loaded_enum.field_names.len) |field_index| {
4419 const llvm_tag_val = try o.builder.intConst(llvm_int_ty, field_index);
4420 try wip_switch.addCase(llvm_tag_val, named_block, &wip);
4421 }
44384422 }
4423
44394424 wip.cursor = .{ .block = named_block };
44404425 _ = try wip.ret(.true);
44414426
src/codegen/llvm/FuncGen.zig+348-403
......@@ -73,7 +73,7 @@ const TodoError = Zcu.CodegenFailError;
7373/// Avoid introducing new calls to this function---see documentation comment on `TodoError`.
7474fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {
7575 @branchHint(.cold);
76 return fg.pt.zcu.codegenFail(
76 return fg.object.zcu.codegenFail(
7777 fg.nav_index,
7878 "TODO (LLVM): " ++ format,
7979 args,
......@@ -81,7 +81,7 @@ fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {
8181}
8282
8383fn ownerModule(fg: *const FuncGen) *Package.Module {
84 return fg.pt.zcu.navFileScope(fg.nav_index).mod.?;
84 return fg.object.zcu.navFileScope(fg.nav_index).mod.?;
8585}
8686
8787fn maybeMarkAllowZeroAccess(self: *FuncGen, info: InternPool.Key.PtrType) void {
......@@ -154,34 +154,35 @@ fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) Allocator.Error!Builder.Value
154154 const gop = try self.func_inst_table.getOrPut(gpa, inst);
155155 if (gop.found_existing) return gop.value_ptr.*;
156156
157 const llvm_val = try self.resolveValue((try self.air.value(inst, self.pt)).?);
157 const llvm_val = try self.resolveValue(.fromInterned(inst.toInterned().?));
158158 gop.value_ptr.* = llvm_val.toValue();
159159 return llvm_val.toValue();
160160}
161161
162162fn resolveValue(self: *FuncGen, val: Value) Allocator.Error!Builder.Constant {
163163 const o = self.object;
164 const pt = self.pt;
165 const zcu = pt.zcu;
164 const zcu = o.zcu;
166165 const ty = val.typeOf(zcu);
167166 if (!isByRef(ty, zcu)) {
168 return o.lowerValue(pt, val.toIntern());
167 return o.lowerValue(val.toIntern());
169168 } else {
170169 // We need a pointer to a global constant, i.e. a UAV.
171 return o.lowerUavRef(pt, .{
172 .val = val.toIntern(),
173 .orig_ty = (try pt.singleConstPtrType(ty)).toIntern(),
174 });
170 return o.lowerUavRef(
171 val.toIntern(),
172 ty.abiAlignment(zcu),
173 target_util.defaultAddressSpace(zcu.getTarget(), .global_constant),
174 );
175175 }
176176}
177177
178/// MLUGG TODO okay yeah prolly delete this again
178179fn lowerType(fg: *const FuncGen, ty: Type) Allocator.Error!Builder.Type {
179 return fg.object.lowerType(fg.pt, ty);
180 return fg.object.lowerType(ty);
180181}
181182
182183pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air.CoveragePoint) TodoError!void {
183184 const o = self.object;
184 const zcu = self.pt.zcu;
185 const zcu = self.object.zcu;
185186 const ip = &zcu.intern_pool;
186187 const air_tags = self.air.instructions.items(.tag);
187188 switch (coverage_point) {
......@@ -514,8 +515,7 @@ fn genBodyDebugScope(
514515 defer self.scope = old_scope;
515516
516517 if (maybe_inline_func) |inline_func| {
517 const pt = self.pt;
518 const zcu = pt.zcu;
518 const zcu = o.zcu;
519519 const ip = &zcu.intern_pool;
520520
521521 const func = zcu.funcInfo(inline_func);
......@@ -529,18 +529,13 @@ fn genBodyDebugScope(
529529 const line_number = self.base_line + 1;
530530 self.inlined_at = try self.wip.debug_location.toMetadata(&o.builder);
531531
532 const fn_ty = try pt.funcType(.{
533 .param_types = &.{},
534 .return_type = .void_type,
535 });
536
537532 self.scope = try o.builder.debugSubprogram(
538533 self.file,
539534 try o.builder.metadataString(nav.name.toSlice(&zcu.intern_pool)),
540535 try o.builder.metadataString(nav.fqn.toSlice(&zcu.intern_pool)),
541536 line_number,
542537 line_number + func.lbrace_line,
543 try o.getDebugType(pt, fn_ty),
538 try o.builder.debugSubroutineType(null),
544539 .{
545540 .di_flags = .{ .StaticMember = true },
546541 .sp_flags = .{
......@@ -582,7 +577,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
582577 const args = air_call.args;
583578 const o = self.object;
584579 const pt = self.pt;
585 const zcu = pt.zcu;
580 const zcu = o.zcu;
586581 const ip = &zcu.intern_pool;
587582 const callee_ty = self.typeOf(air_call.callee);
588583 const zig_fn_ty = switch (callee_ty.zigTypeTag(zcu)) {
......@@ -628,7 +623,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
628623 try llvm_args.append(self.err_ret_trace);
629624 }
630625
631 var it = iterateParamTypes(o, pt, fn_info);
626 var it = iterateParamTypes(o, fn_info);
632627 while (try it.nextCall(self, args)) |lowering| switch (lowering) {
633628 .no_bits => continue,
634629 .byval => {
......@@ -761,7 +756,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
761756
762757 {
763758 // Add argument attributes.
764 it = iterateParamTypes(o, pt, fn_info);
759 it = iterateParamTypes(o, fn_info);
765760 it.llvm_index += @intFromBool(sret);
766761 it.llvm_index += @intFromBool(err_return_tracing);
767762 while (try it.next()) |lowering| switch (lowering) {
......@@ -852,7 +847,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
852847 }
853848 }
854849
855 const abi_ret_ty = try lowerFnRetTy(o, pt, fn_info);
850 const abi_ret_ty = try lowerFnRetTy(o, fn_info);
856851
857852 if (abi_ret_ty != llvm_ret_ty) {
858853 // In this case the function return type is honoring the calling convention by having
......@@ -881,12 +876,11 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
881876
882877fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!void {
883878 const o = fg.object;
884 const pt = fg.pt;
885 const zcu = pt.zcu;
879 const zcu = o.zcu;
886880 const target = zcu.getTarget();
887881 const panic_func = zcu.funcInfo(zcu.builtin_decl_values.get(panic_id.toBuiltin()));
888882 const fn_info = zcu.typeToFunc(.fromInterned(panic_func.ty)).?;
889 const panic_global = try o.resolveLlvmFunction(pt, panic_func.owner_nav);
883 const panic_global = try o.resolveLlvmFunction(panic_func.owner_nav);
890884
891885 const has_err_trace = zcu.comp.config.any_error_tracing and fn_info.cc == .auto;
892886 if (has_err_trace) assert(fg.err_ret_trace != .none);
......@@ -905,30 +899,19 @@ fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!v
905899
906900fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!void {
907901 const o = self.object;
908 const pt = self.pt;
909 const zcu = pt.zcu;
902 const zcu = o.zcu;
910903 const ip = &zcu.intern_pool;
911904 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
912905 const ret_ty = self.typeOf(un_op);
913906
914907 if (self.ret_ptr != .none) {
915 const ptr_ty = try pt.singleMutPtrType(ret_ty);
916
917908 const operand = try self.resolveInst(un_op);
918 const val_is_undef = if (try self.air.value(un_op, pt)) |val| val.isUndef(zcu) else false;
919 if (val_is_undef and safety) undef: {
920 const ptr_info = ptr_ty.ptrInfo(zcu);
921 const needs_bitmask = (ptr_info.packed_offset.host_size != 0);
922 if (needs_bitmask) {
923 // TODO: only some bits are to be undef, we cannot write with a simple memset.
924 // meanwhile, ignore the write rather than stomping over valid bits.
925 // https://github.com/ziglang/zig/issues/15337
926 break :undef;
927 }
909 const val_is_undef = if (un_op.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
910 if (val_is_undef and safety) {
928911 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
929912 _ = try self.wip.callMemSet(
930913 self.ret_ptr,
931 ptr_ty.ptrAlignment(zcu).toLlvm(),
914 ret_ty.abiAlignment(zcu).toLlvm(),
932915 try o.builder.intValue(.i8, 0xaa),
933916 len,
934917 .normal,
......@@ -951,7 +934,12 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
951934 return;
952935 }
953936
954 try self.store(self.ret_ptr, ptr_ty, operand, .none);
937 try self.store(
938 self.ret_ptr,
939 .none,
940 operand,
941 ret_ty,
942 );
955943 _ = try self.wip.retVoid();
956944 return;
957945 }
......@@ -968,15 +956,15 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
968956 return;
969957 }
970958
971 const abi_ret_ty = try lowerFnRetTy(o, pt, fn_info);
959 const abi_ret_ty = try lowerFnRetTy(o, fn_info);
972960 const operand = try self.resolveInst(un_op);
973 const val_is_undef = if (try self.air.value(un_op, pt)) |val| val.isUndef(zcu) else false;
961 const val_is_undef = if (un_op.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
974962 const alignment = ret_ty.abiAlignment(zcu).toLlvm();
975963
976964 if (val_is_undef and safety) {
977965 const llvm_ret_ty = operand.typeOfWip(&self.wip);
978966 const rp = try self.buildAlloca(llvm_ret_ty, alignment);
979 const len = try o.builder.intValue(try self.lowerType(Type.usize), ret_ty.abiSize(zcu));
967 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
980968 _ = try self.wip.callMemSet(
981969 rp,
982970 alignment,
......@@ -1014,8 +1002,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
10141002
10151003fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
10161004 const o = self.object;
1017 const pt = self.pt;
1018 const zcu = pt.zcu;
1005 const zcu = o.zcu;
10191006 const ip = &zcu.intern_pool;
10201007 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
10211008 const ptr_ty = self.typeOf(un_op);
......@@ -1037,7 +1024,7 @@ fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
10371024 return;
10381025 }
10391026 const ptr = try self.resolveInst(un_op);
1040 const abi_ret_ty = try lowerFnRetTy(o, pt, fn_info);
1027 const abi_ret_ty = try lowerFnRetTy(o, fn_info);
10411028 const alignment = ret_ty.abiAlignment(zcu).toLlvm();
10421029 _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, ptr, alignment, ""));
10431030 return;
......@@ -1053,14 +1040,13 @@ fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
10531040}
10541041
10551042fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1056 const pt = self.pt;
1057 const zcu = pt.zcu;
1043 const zcu = self.object.zcu;
10581044 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
10591045 const src_list = try self.resolveInst(ty_op.operand);
10601046 const va_list_ty = ty_op.ty.toType();
10611047 const llvm_va_list_ty = try self.lowerType(va_list_ty);
10621048
1063 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();
1049 const result_alignment = va_list_ty.abiAlignment(zcu).toLlvm();
10641050 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
10651051
10661052 _ = try self.wip.callIntrinsic(.normal, .none, .va_copy, &.{dest_list.typeOfWip(&self.wip)}, &.{ dest_list, src_list }, "");
......@@ -1079,12 +1065,11 @@ fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
10791065}
10801066
10811067fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1082 const pt = self.pt;
1083 const zcu = pt.zcu;
1068 const zcu = self.object.zcu;
10841069 const va_list_ty = self.typeOfIndex(inst);
10851070 const llvm_va_list_ty = try self.lowerType(va_list_ty);
10861071
1087 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();
1072 const result_alignment = va_list_ty.abiAlignment(zcu).toLlvm();
10881073 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
10891074
10901075 _ = try self.wip.callIntrinsic(.normal, .none, .va_start, &.{dest_list.typeOfWip(&self.wip)}, &.{dest_list}, "");
......@@ -1145,8 +1130,7 @@ fn cmp(
11451130 rhs: Builder.Value,
11461131) Allocator.Error!Builder.Value {
11471132 const o = self.object;
1148 const pt = self.pt;
1149 const zcu = pt.zcu;
1133 const zcu = o.zcu;
11501134 const scalar_ty = operand_ty.scalarType(zcu);
11511135 const int_ty = switch (scalar_ty.zigTypeTag(zcu)) {
11521136 .@"enum" => scalar_ty.intTagType(zcu),
......@@ -1244,8 +1228,7 @@ fn lowerBlock(
12441228 maybe_inline_func: ?InternPool.Index,
12451229 body: []const Air.Inst.Index,
12461230) TodoError!Builder.Value {
1247 const pt = self.pt;
1248 const zcu = pt.zcu;
1231 const zcu = self.object.zcu;
12491232 const inst_ty = self.typeOfIndex(inst);
12501233
12511234 if (inst_ty.isNoReturn(zcu)) {
......@@ -1294,7 +1277,7 @@ fn lowerBlock(
12941277}
12951278
12961279fn airBr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
1297 const zcu = self.pt.zcu;
1280 const zcu = self.object.zcu;
12981281 const branch = self.air.instructions.items(.data)[@intFromEnum(inst)].br;
12991282 const block = self.blocks.get(branch.block_inst).?;
13001283
......@@ -1324,12 +1307,12 @@ fn lowerSwitchDispatch(
13241307 dispatch_info: SwitchDispatchInfo,
13251308) Allocator.Error!void {
13261309 const o = self.object;
1327 const pt = self.pt;
1328 const zcu = pt.zcu;
1310 const zcu = o.zcu;
13291311 const cond_ty = self.typeOf(cond_ref);
13301312 const switch_br = self.air.unwrapSwitch(switch_inst);
13311313
1332 if (try self.air.value(cond_ref, pt)) |cond_val| {
1314 if (cond_ref.toInterned()) |cond_ip_index| {
1315 const cond_val: Value = .fromInterned(cond_ip_index);
13331316 // Comptime-known dispatch. Iterate the cases to find the correct
13341317 // one, and branch to the corresponding element of `case_blocks`.
13351318 var it = switch_br.iterateCases();
......@@ -1413,7 +1396,7 @@ fn lowerSwitchDispatch(
14131396 // The switch prongs will correspond to our scalar cases. Ranges will
14141397 // be handled by conditional branches in the `else` prong.
14151398
1416 const llvm_usize = try self.lowerType(Type.usize);
1399 const llvm_usize = try self.lowerType(.usize);
14171400 const cond_int = if (cond.typeOfWip(&self.wip).isPointer(&o.builder))
14181401 try self.wip.cast(.ptrtoint, cond, llvm_usize, "")
14191402 else
......@@ -1581,7 +1564,7 @@ fn airTry(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builde
15811564}
15821565
15831566fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builder.Value {
1584 const zcu = self.pt.zcu;
1567 const zcu = self.object.zcu;
15851568 const unwrapped_try = self.air.unwrapTryPtr(inst);
15861569 const err_union_ptr = try self.resolveInst(unwrapped_try.error_union_ptr);
15871570 const body = unwrapped_try.else_body;
......@@ -1605,8 +1588,7 @@ fn lowerTry(
16051588 err_cold: bool,
16061589) TodoError!Builder.Value {
16071590 const o = fg.object;
1608 const pt = fg.pt;
1609 const zcu = pt.zcu;
1591 const zcu = o.zcu;
16101592 const payload_ty = err_union_ty.errorUnionPayload(zcu);
16111593 const payload_has_bits = payload_ty.hasRuntimeBits(zcu);
16121594 const error_type = try o.errorIntType();
......@@ -1667,8 +1649,7 @@ fn lowerTry(
16671649
16681650fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) TodoError!void {
16691651 const o = self.object;
1670 const pt = self.pt;
1671 const zcu = pt.zcu;
1652 const zcu = o.zcu;
16721653
16731654 const switch_br = self.air.unwrapSwitch(inst);
16741655
......@@ -1773,8 +1754,8 @@ fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) Tod
17731754 const table_includes_else = item_count != table_len;
17741755
17751756 break :jmp_table .{
1776 .min = try o.lowerValue(pt, min.toIntern()),
1777 .max = try o.lowerValue(pt, max.toIntern()),
1757 .min = try o.lowerValue(min.toIntern()),
1758 .max = try o.lowerValue(max.toIntern()),
17781759 .in_bounds_hint = if (table_includes_else) .none else switch (switch_br.getElseHint()) {
17791760 .none, .cold => .none,
17801761 .unpredictable => .unpredictable,
......@@ -1883,7 +1864,7 @@ fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) Tod
18831864}
18841865
18851866fn switchCaseItemRange(self: *FuncGen, switch_br: Air.UnwrappedSwitch) ?[2]Value {
1886 const zcu = self.pt.zcu;
1867 const zcu = self.object.zcu;
18871868 var it = switch_br.iterateCases();
18881869 var min: ?Value = null;
18891870 var max: ?Value = null;
......@@ -1928,8 +1909,7 @@ fn airLoop(self: *FuncGen, inst: Air.Inst.Index) TodoError!void {
19281909
19291910fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
19301911 const o = self.object;
1931 const pt = self.pt;
1932 const zcu = pt.zcu;
1912 const zcu = o.zcu;
19331913 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
19341914 const operand_ty = self.typeOf(ty_op.operand);
19351915 const array_ty = operand_ty.childType(zcu);
......@@ -1942,8 +1922,7 @@ fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
19421922
19431923fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
19441924 const o = self.object;
1945 const pt = self.pt;
1946 const zcu = pt.zcu;
1925 const zcu = o.zcu;
19471926 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
19481927
19491928 const operand = try self.resolveInst(ty_op.operand);
......@@ -1964,7 +1943,7 @@ fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value
19641943 );
19651944
19661945 const rt_int_bits = compilerRtIntBits(@intCast(operand_scalar_ty.bitSize(zcu))) orelse {
1967 return self.todo("float_from_int from '{f}' without intrinsics", .{operand_scalar_ty.fmt(pt)});
1946 return self.todo("float_from_int on {d} bit integer", .{operand_scalar_ty.bitSize(zcu)});
19681947 };
19691948 const rt_int_ty = try o.builder.intType(rt_int_bits);
19701949 var extended = try self.wip.conv(
......@@ -2011,8 +1990,7 @@ fn airIntFromFloat(
20111990 _ = fast;
20121991
20131992 const o = self.object;
2014 const pt = self.pt;
2015 const zcu = pt.zcu;
1993 const zcu = o.zcu;
20161994 const target = zcu.getTarget();
20171995 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
20181996
......@@ -2035,7 +2013,7 @@ fn airIntFromFloat(
20352013 }
20362014
20372015 const rt_int_bits = compilerRtIntBits(@intCast(dest_scalar_ty.bitSize(zcu))) orelse {
2038 return self.todo("int_from_float to '{f}' without intrinsics", .{dest_scalar_ty.fmt(pt)});
2016 return self.todo("int_from_float to {d} bit integer", .{dest_scalar_ty.bitSize(zcu)});
20392017 };
20402018 const ret_ty = try o.builder.intType(rt_int_bits);
20412019 const libc_ret_ty = if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) b: {
......@@ -2074,14 +2052,13 @@ fn airIntFromFloat(
20742052}
20752053
20762054fn sliceOrArrayPtr(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {
2077 const zcu = fg.pt.zcu;
2055 const zcu = fg.object.zcu;
20782056 return if (ty.isSlice(zcu)) fg.wip.extractValue(ptr, &.{0}, "") else ptr;
20792057}
20802058
20812059fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {
20822060 const o = fg.object;
2083 const pt = fg.pt;
2084 const zcu = pt.zcu;
2061 const zcu = o.zcu;
20852062 const llvm_usize = try fg.lowerType(.usize);
20862063 switch (ty.ptrSize(zcu)) {
20872064 .slice => {
......@@ -2109,15 +2086,14 @@ fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: u32) Allocator.Err
21092086}
21102087
21112088fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: u1) Allocator.Error!Builder.Value {
2112 const zcu = self.pt.zcu;
2089 const zcu = self.object.zcu;
21132090 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
21142091 const slice_ptr = try self.resolveInst(ty_op.operand);
21152092 return self.ptraddConst(slice_ptr, index * Type.usize.abiSize(zcu));
21162093}
21172094
21182095fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2119 const pt = self.pt;
2120 const zcu = pt.zcu;
2096 const zcu = self.object.zcu;
21212097 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
21222098 const slice_ty = self.typeOf(bin_op.lhs);
21232099 const slice = try self.resolveInst(bin_op.lhs);
......@@ -2138,8 +2114,7 @@ fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
21382114}
21392115
21402116fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2141 const pt = self.pt;
2142 const zcu = pt.zcu;
2117 const zcu = self.object.zcu;
21432118 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
21442119 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
21452120 const slice_ty = self.typeOf(bin_op.lhs);
......@@ -2151,8 +2126,7 @@ fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
21512126}
21522127
21532128fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2154 const pt = self.pt;
2155 const zcu = pt.zcu;
2129 const zcu = self.object.zcu;
21562130
21572131 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
21582132 const array_ty = self.typeOf(bin_op.lhs);
......@@ -2174,28 +2148,25 @@ fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
21742148}
21752149
21762150fn airPtrElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2177 const pt = self.pt;
2178 const zcu = pt.zcu;
2151 const zcu = self.object.zcu;
21792152 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
21802153 const ptr_ty = self.typeOf(bin_op.lhs);
21812154 const elem_ty = ptr_ty.indexableElem(zcu);
21822155 const base_ptr = try self.resolveInst(bin_op.lhs);
21832156 const rhs = try self.resolveInst(bin_op.rhs);
2184 const ptr = try self.ptraddScaled(base_ptr, rhs, elem_ty.abiSize(zcu));
2185 if (isByRef(elem_ty, zcu)) {
2186 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
2187 const ptr_align = (ptr_ty.ptrAlignment(zcu).min(elem_ty.abiAlignment(zcu))).toLlvm();
2188 return self.loadByRef(ptr, elem_ty, ptr_align, if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal);
2189 }
21902157
21912158 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
21922159
2193 return self.load(ptr, ptr_ty);
2160 return self.load(
2161 try self.ptraddScaled(base_ptr, rhs, elem_ty.abiSize(zcu)),
2162 elem_ty,
2163 ptr_ty.ptrAlignment(zcu).min(elem_ty.abiAlignment(zcu)).toLlvm(),
2164 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
2165 );
21942166}
21952167
21962168fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2197 const pt = self.pt;
2198 const zcu = pt.zcu;
2169 const zcu = self.object.zcu;
21992170 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
22002171 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
22012172 const ptr_ty = self.typeOf(bin_op.lhs);
......@@ -2232,8 +2203,7 @@ fn airStructFieldPtrIndex(
22322203
22332204fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
22342205 const o = self.object;
2235 const pt = self.pt;
2236 const zcu = pt.zcu;
2206 const zcu = o.zcu;
22372207 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
22382208 const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data;
22392209 const struct_ty = self.typeOf(struct_field.struct_operand);
......@@ -2298,8 +2268,7 @@ fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
22982268
22992269fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
23002270 const o = self.object;
2301 const pt = self.pt;
2302 const zcu = pt.zcu;
2271 const zcu = o.zcu;
23032272 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
23042273 const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
23052274
......@@ -2310,7 +2279,7 @@ fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
23102279 if (field_offset == 0) return field_ptr;
23112280
23122281 const res_ty = try self.lowerType(ty_pl.ty.toType());
2313 const llvm_usize = try self.lowerType(Type.usize);
2282 const llvm_usize = try self.lowerType(.usize);
23142283
23152284 const field_ptr_int = try self.wip.cast(.ptrtoint, field_ptr, llvm_usize, "");
23162285 const base_ptr_int = try self.wip.bin(
......@@ -2358,7 +2327,7 @@ fn airDbgEmptyStmt(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
23582327fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
23592328 const o = self.object;
23602329 const pt = self.pt;
2361 const zcu = pt.zcu;
2330 const zcu = o.zcu;
23622331 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
23632332 const operand = try self.resolveInst(pl_op.operand);
23642333 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
......@@ -2415,7 +2384,7 @@ fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index, is_arg: bool) Allocator.Er
24152384 try o.getDebugType(pt, operand_ty),
24162385 );
24172386
2418 const zcu = pt.zcu;
2387 const zcu = o.zcu;
24192388 const owner_mod = self.ownerModule();
24202389 if (isByRef(operand_ty, zcu)) {
24212390 _ = try self.wip.callIntrinsic(
......@@ -2501,8 +2470,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
25012470 // This stores whether we need to add an elementtype attribute and
25022471 // if so, the element type itself.
25032472 const llvm_param_attrs = try arena.alloc(Builder.Type, max_param_count);
2504 const pt = self.pt;
2505 const zcu = pt.zcu;
2473 const zcu = o.zcu;
25062474 const ip = &zcu.intern_pool;
25072475 const target = zcu.getTarget();
25082476
......@@ -2855,8 +2823,7 @@ fn airIsNonNull(
28552823 cond: Builder.IntegerCondition,
28562824) Allocator.Error!Builder.Value {
28572825 const o = self.object;
2858 const pt = self.pt;
2859 const zcu = pt.zcu;
2826 const zcu = o.zcu;
28602827 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
28612828 const operand = try self.resolveInst(un_op);
28622829 const operand_ty = self.typeOf(un_op);
......@@ -2905,8 +2872,7 @@ fn airIsErr(
29052872 operand_is_ptr: bool,
29062873) Allocator.Error!Builder.Value {
29072874 const o = self.object;
2908 const pt = self.pt;
2909 const zcu = pt.zcu;
2875 const zcu = o.zcu;
29102876 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
29112877 const operand = try self.resolveInst(un_op);
29122878 const operand_ty = self.typeOf(un_op);
......@@ -2959,8 +2925,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Erro
29592925 comptime assert(optional_layout_version == 3);
29602926
29612927 const o = self.object;
2962 const pt = self.pt;
2963 const zcu = pt.zcu;
2928 const zcu = o.zcu;
29642929 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
29652930 const operand = try self.resolveInst(ty_op.operand);
29662931 const optional_ptr_ty = self.typeOf(ty_op.operand);
......@@ -3001,8 +2966,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Erro
30012966}
30022967
30032968fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3004 const pt = self.pt;
3005 const zcu = pt.zcu;
2969 const zcu = self.object.zcu;
30062970 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30072971 const operand = try self.resolveInst(ty_op.operand);
30082972 const optional_ty = self.typeOf(ty_op.operand);
......@@ -3018,8 +2982,7 @@ fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Buil
30182982}
30192983
30202984fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool) Allocator.Error!Builder.Value {
3021 const pt = self.pt;
3022 const zcu = pt.zcu;
2985 const zcu = self.object.zcu;
30232986 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30242987 const operand = try self.resolveInst(ty_op.operand);
30252988 const operand_ty = self.typeOf(ty_op.operand);
......@@ -3050,8 +3013,7 @@ fn airErrUnionErr(
30503013 operand_is_ptr: bool,
30513014) Allocator.Error!Builder.Value {
30523015 const o = self.object;
3053 const pt = self.pt;
3054 const zcu = pt.zcu;
3016 const zcu = o.zcu;
30553017 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30563018 const operand = try self.resolveInst(ty_op.operand);
30573019 const operand_ty = self.typeOf(ty_op.operand);
......@@ -3093,8 +3055,7 @@ fn airErrUnionErr(
30933055
30943056fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
30953057 const o = self.object;
3096 const pt = self.pt;
3097 const zcu = pt.zcu;
3058 const zcu = o.zcu;
30983059 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30993060 const operand = try self.resolveInst(ty_op.operand);
31003061 const err_union_ptr_ty = self.typeOf(ty_op.operand);
......@@ -3133,8 +3094,7 @@ fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bu
31333094}
31343095
31353096fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3136 const pt = self.pt;
3137 const zcu = pt.zcu;
3097 const zcu = self.object.zcu;
31383098
31393099 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
31403100 const struct_ty = ty_pl.ty.toType();
......@@ -3150,12 +3110,7 @@ fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Er
31503110 };
31513111
31523112 const field_ptr = try self.ptraddConst(self.err_ret_trace, field_offset);
3153
3154 const field_ptr_ty = try pt.ptrType(.{
3155 .child = field_ty.toIntern(),
3156 .flags = .{ .alignment = field_align },
3157 });
3158 return self.load(field_ptr, field_ptr_ty);
3113 return self.load(field_ptr, field_ty, field_align.toLlvm(), .normal);
31593114}
31603115
31613116/// As an optimization, we want to avoid unnecessary copies of
......@@ -3183,8 +3138,7 @@ fn isNextRet(
31833138
31843139fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
31853140 const o = self.object;
3186 const pt = self.pt;
3187 const zcu = pt.zcu;
3141 const zcu = o.zcu;
31883142 const inst = body_tail[0];
31893143 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
31903144 const payload_ty = self.typeOf(ty_op.operand);
......@@ -3205,8 +3159,12 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.
32053159 };
32063160
32073161 const payload_ptr = optional_ptr; // payload always at offset 0
3208 const payload_ptr_ty = try pt.singleMutPtrType(payload_ty);
3209 try self.store(payload_ptr, payload_ptr_ty, operand, .none);
3162 try self.store(
3163 payload_ptr,
3164 .none,
3165 operand,
3166 payload_ty,
3167 );
32103168 // Non-null bit immediately after payload (no padding because the bit has alignment 1).
32113169 const non_null_ptr = try self.ptraddConst(optional_ptr, payload_ty.abiSize(zcu));
32123170 _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default);
......@@ -3215,8 +3173,7 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.
32153173
32163174fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
32173175 const o = self.object;
3218 const pt = self.pt;
3219 const zcu = pt.zcu;
3176 const zcu = o.zcu;
32203177 const inst = body_tail[0];
32213178 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
32223179 const err_un_ty = self.typeOfIndex(inst);
......@@ -3230,23 +3187,26 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) All
32303187 const result_ptr = if (self.isNextRet(body_tail))
32313188 self.ret_ptr
32323189 else brk: {
3233 const alignment = err_un_ty.abiAlignment(pt.zcu).toLlvm();
3190 const alignment = err_un_ty.abiAlignment(o.zcu).toLlvm();
32343191 const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment);
32353192 break :brk result_ptr;
32363193 };
32373194
32383195 const err_ptr = try self.ptraddConst(result_ptr, codegen.errUnionErrorOffset(payload_ty, zcu));
3239 const error_alignment = Type.anyerror.abiAlignment(pt.zcu).toLlvm();
3196 const error_alignment = Type.anyerror.abiAlignment(o.zcu).toLlvm();
32403197 _ = try self.wip.store(.normal, ok_err_code, err_ptr, error_alignment);
32413198 const payload_ptr = try self.ptraddConst(result_ptr, codegen.errUnionPayloadOffset(payload_ty, zcu));
3242 const payload_ptr_ty = try pt.singleMutPtrType(payload_ty);
3243 try self.store(payload_ptr, payload_ptr_ty, operand, .none);
3199 try self.store(
3200 payload_ptr,
3201 .none,
3202 operand,
3203 payload_ty,
3204 );
32443205 return result_ptr;
32453206}
32463207
32473208fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
3248 const pt = self.pt;
3249 const zcu = pt.zcu;
3209 const zcu = self.object.zcu;
32503210 const inst = body_tail[0];
32513211 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
32523212 const err_un_ty = self.typeOfIndex(inst);
......@@ -3268,10 +3228,8 @@ fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocat
32683228 const error_alignment = Type.anyerror.abiAlignment(zcu).toLlvm();
32693229 _ = try self.wip.store(.normal, operand, err_ptr, error_alignment);
32703230 const payload_ptr = try self.ptraddConst(result_ptr, codegen.errUnionPayloadOffset(payload_ty, zcu));
3271 const payload_ptr_ty = try pt.singleMutPtrType(payload_ty);
32723231 // TODO store undef to payload_ptr
32733232 _ = payload_ptr;
3274 _ = payload_ptr_ty;
32753233 return result_ptr;
32763234}
32773235
......@@ -3279,7 +3237,7 @@ fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
32793237 const o = self.object;
32803238 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
32813239 const index = pl_op.payload;
3282 const llvm_usize = try self.lowerType(Type.usize);
3240 const llvm_usize = try self.lowerType(.usize);
32833241 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{llvm_usize}, &.{
32843242 try o.builder.intValue(.i32, index),
32853243 }, "");
......@@ -3289,7 +3247,7 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
32893247 const o = self.object;
32903248 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
32913249 const index = pl_op.payload;
3292 const llvm_isize = try self.lowerType(Type.isize);
3250 const llvm_isize = try self.lowerType(.isize);
32933251 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{llvm_isize}, &.{
32943252 try o.builder.intValue(.i32, index), try self.resolveInst(pl_op.operand),
32953253 }, "");
......@@ -3297,15 +3255,13 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
32973255
32983256fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
32993257 const o = fg.object;
3300 const pt = fg.pt;
33013258 const ty_nav = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;
3302 const llvm_ptr_const = try o.lowerNavRefValue(pt, ty_nav.nav);
3259 const llvm_ptr_const = try o.lowerNavRefValue(ty_nav.nav);
33033260 return llvm_ptr_const.toValue();
33043261}
33053262
33063263fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3307 const pt = self.pt;
3308 const zcu = pt.zcu;
3264 const zcu = self.object.zcu;
33093265 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33103266 const lhs = try self.resolveInst(bin_op.lhs);
33113267 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3324,8 +3280,7 @@ fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
33243280}
33253281
33263282fn airMax(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3327 const pt = self.pt;
3328 const zcu = pt.zcu;
3283 const zcu = self.object.zcu;
33293284 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33303285 const lhs = try self.resolveInst(bin_op.lhs);
33313286 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3353,7 +3308,7 @@ fn airSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
33533308}
33543309
33553310fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3356 const zcu = self.pt.zcu;
3311 const zcu = self.object.zcu;
33573312 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33583313 const lhs = try self.resolveInst(bin_op.lhs);
33593314 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3371,8 +3326,7 @@ fn airSafeArithmetic(
33713326 unsigned_intrinsic: Builder.Intrinsic,
33723327) Allocator.Error!Builder.Value {
33733328 const o = fg.object;
3374 const pt = fg.pt;
3375 const zcu = pt.zcu;
3329 const zcu = o.zcu;
33763330
33773331 const bin_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33783332 const lhs = try fg.resolveInst(bin_op.lhs);
......@@ -3419,8 +3373,7 @@ fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
34193373}
34203374
34213375fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3422 const pt = self.pt;
3423 const zcu = pt.zcu;
3376 const zcu = self.object.zcu;
34243377 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34253378 const lhs = try self.resolveInst(bin_op.lhs);
34263379 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3438,7 +3391,7 @@ fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
34383391}
34393392
34403393fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3441 const zcu = self.pt.zcu;
3394 const zcu = self.object.zcu;
34423395 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34433396 const lhs = try self.resolveInst(bin_op.lhs);
34443397 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3458,8 +3411,7 @@ fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
34583411}
34593412
34603413fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3461 const pt = self.pt;
3462 const zcu = pt.zcu;
3414 const zcu = self.object.zcu;
34633415 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34643416 const lhs = try self.resolveInst(bin_op.lhs);
34653417 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3477,7 +3429,7 @@ fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
34773429}
34783430
34793431fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3480 const zcu = self.pt.zcu;
3432 const zcu = self.object.zcu;
34813433 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34823434 const lhs = try self.resolveInst(bin_op.lhs);
34833435 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3497,8 +3449,7 @@ fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
34973449}
34983450
34993451fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3500 const pt = self.pt;
3501 const zcu = pt.zcu;
3452 const zcu = self.object.zcu;
35023453 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35033454 const lhs = try self.resolveInst(bin_op.lhs);
35043455 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3525,7 +3476,7 @@ fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35253476}
35263477
35273478fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3528 const zcu = self.pt.zcu;
3479 const zcu = self.object.zcu;
35293480 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35303481 const lhs = try self.resolveInst(bin_op.lhs);
35313482 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3541,8 +3492,7 @@ fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35413492
35423493fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
35433494 const o = self.object;
3544 const pt = self.pt;
3545 const zcu = pt.zcu;
3495 const zcu = o.zcu;
35463496 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35473497 const lhs = try self.resolveInst(bin_op.lhs);
35483498 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3591,7 +3541,7 @@ fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35913541}
35923542
35933543fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3594 const zcu = self.pt.zcu;
3544 const zcu = self.object.zcu;
35953545 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35963546 const lhs = try self.resolveInst(bin_op.lhs);
35973547 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3608,7 +3558,7 @@ fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
36083558}
36093559
36103560fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3611 const zcu = self.pt.zcu;
3561 const zcu = self.object.zcu;
36123562 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
36133563 const lhs = try self.resolveInst(bin_op.lhs);
36143564 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3625,8 +3575,7 @@ fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo
36253575
36263576fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
36273577 const o = self.object;
3628 const pt = self.pt;
3629 const zcu = pt.zcu;
3578 const zcu = o.zcu;
36303579 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
36313580 const lhs = try self.resolveInst(bin_op.lhs);
36323581 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3678,7 +3627,7 @@ fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo
36783627}
36793628
36803629fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3681 const zcu = self.pt.zcu;
3630 const zcu = self.object.zcu;
36823631 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
36833632 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
36843633 const ptr_or_slice = try self.resolveInst(bin_op.lhs);
......@@ -3694,8 +3643,7 @@ fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
36943643
36953644fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
36963645 const o = self.object;
3697 const pt = self.pt;
3698 const zcu = pt.zcu;
3646 const zcu = o.zcu;
36993647 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
37003648 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
37013649 const ptr_or_slice = try self.resolveInst(bin_op.lhs);
......@@ -3718,8 +3666,7 @@ fn airOverflow(
37183666 signed_intrinsic: Builder.Intrinsic,
37193667 unsigned_intrinsic: Builder.Intrinsic,
37203668) Allocator.Error!Builder.Value {
3721 const pt = self.pt;
3722 const zcu = pt.zcu;
3669 const zcu = self.object.zcu;
37233670 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
37243671 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
37253672
......@@ -3801,8 +3748,7 @@ fn buildFloatCmp(
38013748 params: [2]Builder.Value,
38023749) Allocator.Error!Builder.Value {
38033750 const o = self.object;
3804 const pt = self.pt;
3805 const zcu = pt.zcu;
3751 const zcu = o.zcu;
38063752 const target = zcu.getTarget();
38073753 const scalar_ty = ty.scalarType(zcu);
38083754 const scalar_llvm_ty = try self.lowerType(scalar_ty);
......@@ -3908,8 +3854,7 @@ fn buildFloatOp(
39083854 params: [params_len]Builder.Value,
39093855) Allocator.Error!Builder.Value {
39103856 const o = self.object;
3911 const pt = self.pt;
3912 const zcu = pt.zcu;
3857 const zcu = o.zcu;
39133858 const target = zcu.getTarget();
39143859 const scalar_ty = ty.scalarType(zcu);
39153860 const llvm_ty = try self.lowerType(ty);
......@@ -4049,8 +3994,7 @@ fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
40493994}
40503995
40513996fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4052 const pt = self.pt;
4053 const zcu = pt.zcu;
3997 const zcu = self.object.zcu;
40543998 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
40553999 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
40564000
......@@ -4120,8 +4064,7 @@ fn airXor(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41204064}
41214065
41224066fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4123 const pt = self.pt;
4124 const zcu = pt.zcu;
4067 const zcu = self.object.zcu;
41254068 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41264069
41274070 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4143,8 +4086,7 @@ fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
41434086}
41444087
41454088fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4146 const pt = self.pt;
4147 const zcu = pt.zcu;
4089 const zcu = self.object.zcu;
41484090 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41494091
41504092 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4162,8 +4104,7 @@ fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41624104
41634105fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41644106 const o = self.object;
4165 const pt = self.pt;
4166 const zcu = pt.zcu;
4107 const zcu = o.zcu;
41674108 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41684109
41694110 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4244,8 +4185,7 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
42444185}
42454186
42464187fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!Builder.Value {
4247 const pt = self.pt;
4248 const zcu = pt.zcu;
4188 const zcu = self.object.zcu;
42494189 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
42504190
42514191 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -4269,8 +4209,7 @@ fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!
42694209
42704210fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
42714211 const o = self.object;
4272 const pt = self.pt;
4273 const zcu = pt.zcu;
4212 const zcu = o.zcu;
42744213 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
42754214 const operand = try self.resolveInst(ty_op.operand);
42764215 const operand_ty = self.typeOf(ty_op.operand);
......@@ -4292,8 +4231,7 @@ fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
42924231
42934232fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
42944233 const o = fg.object;
4295 const pt = fg.pt;
4296 const zcu = pt.zcu;
4234 const zcu = o.zcu;
42974235 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
42984236 const dest_ty = fg.typeOfIndex(inst);
42994237 const dest_llvm_ty = try fg.lowerType(dest_ty);
......@@ -4379,7 +4317,7 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
43794317 }, operand, dest_llvm_ty, "");
43804318
43814319 if (safety and dest_is_enum and !dest_ty.isNonexhaustiveEnum(zcu)) {
4382 const llvm_fn = try o.getIsNamedEnumValueFunction(pt, dest_ty);
4320 const llvm_fn = try o.getIsNamedEnumValueFunction(dest_ty);
43834321 const is_valid_enum_val = try fg.wip.call(
43844322 .normal,
43854323 .fastcc,
......@@ -4409,8 +4347,7 @@ fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
44094347
44104348fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
44114349 const o = self.object;
4412 const pt = self.pt;
4413 const zcu = pt.zcu;
4350 const zcu = o.zcu;
44144351 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
44154352 const operand = try self.resolveInst(ty_op.operand);
44164353 const operand_ty = self.typeOf(ty_op.operand);
......@@ -4444,8 +4381,7 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
44444381
44454382fn airFpext(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
44464383 const o = self.object;
4447 const pt = self.pt;
4448 const zcu = pt.zcu;
4384 const zcu = o.zcu;
44494385 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
44504386 const operand = try self.resolveInst(ty_op.operand);
44514387 const operand_ty = self.typeOf(ty_op.operand);
......@@ -4493,8 +4429,7 @@ fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
44934429
44944430fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) Allocator.Error!Builder.Value {
44954431 const o = self.object;
4496 const pt = self.pt;
4497 const zcu = pt.zcu;
4432 const zcu = o.zcu;
44984433 const operand_is_ref = isByRef(operand_ty, zcu);
44994434 const result_is_ref = isByRef(inst_ty, zcu);
45004435 const llvm_dest_ty = try self.lowerType(inst_ty);
......@@ -4599,7 +4534,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
45994534fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
46004535 const o = self.object;
46014536 const pt = self.pt;
4602 const zcu = pt.zcu;
4537 const zcu = o.zcu;
46034538 const arg_val = self.args[self.arg_index];
46044539 self.arg_index += 1;
46054540
......@@ -4688,13 +4623,13 @@ fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
46884623
46894624fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
46904625 const o = self.object;
4691 const pt = self.pt;
4692 const zcu = pt.zcu;
4626 const zcu = o.zcu;
46934627 const ptr_ty = self.typeOfIndex(inst);
46944628 const pointee_type = ptr_ty.childType(zcu);
4695 if (!pointee_type.hasRuntimeBits(zcu))
4696 return (try o.lowerPtrToVoid(pt, ptr_ty)).toValue();
4697
4629 if (!pointee_type.hasRuntimeBits(zcu)) {
4630 const ptr_info = ptr_ty.ptrInfo(zcu);
4631 return (try o.lowerPtrToVoid(ptr_info.flags.alignment, ptr_info.flags.address_space)).toValue();
4632 }
46984633 const pointee_llvm_ty = try self.lowerType(pointee_type);
46994634 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
47004635 return self.buildAlloca(pointee_llvm_ty, alignment);
......@@ -4702,12 +4637,13 @@ fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
47024637
47034638fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
47044639 const o = self.object;
4705 const pt = self.pt;
4706 const zcu = pt.zcu;
4640 const zcu = o.zcu;
47074641 const ptr_ty = self.typeOfIndex(inst);
47084642 const ret_ty = ptr_ty.childType(zcu);
4709 if (!ret_ty.hasRuntimeBits(zcu))
4710 return (try o.lowerPtrToVoid(pt, ptr_ty)).toValue();
4643 if (!ret_ty.hasRuntimeBits(zcu)) {
4644 const ptr_info = ptr_ty.ptrInfo(zcu);
4645 return (try o.lowerPtrToVoid(ptr_info.flags.alignment, ptr_info.flags.address_space)).toValue();
4646 }
47114647 if (self.ret_ptr != .none) return self.ret_ptr;
47124648 const ret_llvm_ty = try self.lowerType(ret_ty);
47134649 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
......@@ -4721,20 +4657,19 @@ fn buildAlloca(
47214657 llvm_ty: Builder.Type,
47224658 alignment: Builder.Alignment,
47234659) Allocator.Error!Builder.Value {
4724 const target = self.pt.zcu.getTarget();
4660 const target = self.object.zcu.getTarget();
47254661 return buildAllocaInner(&self.wip, llvm_ty, alignment, target);
47264662}
47274663
47284664fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
47294665 const o = self.object;
4730 const pt = self.pt;
4731 const zcu = pt.zcu;
4666 const zcu = o.zcu;
47324667 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
47334668 const dest_ptr = try self.resolveInst(bin_op.lhs);
47344669 const ptr_ty = self.typeOf(bin_op.lhs);
47354670 const operand_ty = ptr_ty.childType(zcu);
47364671
4737 const val_is_undef = if (try self.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false;
4672 const val_is_undef = if (bin_op.rhs.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
47384673 if (val_is_undef) {
47394674 const owner_mod = self.ownerModule();
47404675
......@@ -4762,7 +4697,7 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
47624697
47634698 self.maybeMarkAllowZeroAccess(ptr_info);
47644699
4765 const len = try o.builder.intValue(try self.lowerType(Type.usize), operand_ty.abiSize(zcu));
4700 const len = try o.builder.intValue(try self.lowerType(.usize), operand_ty.abiSize(zcu));
47664701 _ = try self.wip.callMemSet(
47674702 dest_ptr,
47684703 ptr_ty.ptrAlignment(zcu).toLlvm(),
......@@ -4780,24 +4715,75 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
47804715 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
47814716
47824717 const src_operand = try self.resolveInst(bin_op.rhs);
4783 try self.store(dest_ptr, ptr_ty, src_operand, .none);
4718 try self.storeFull(dest_ptr, ptr_ty, src_operand, .none);
47844719 return .none;
47854720}
47864721
47874722fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4788 const pt = fg.pt;
4789 const zcu = pt.zcu;
4723 const o = fg.object;
4724 const zcu = o.zcu;
47904725 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
47914726 const ptr_ty = fg.typeOf(ty_op.operand);
47924727 const ptr_info = ptr_ty.ptrInfo(zcu);
47934728 const ptr = try fg.resolveInst(ty_op.operand);
4729 const elem_ty = ptr_ty.childType(zcu);
4730 const llvm_ptr_align = ptr_ty.ptrAlignment(zcu).toLlvm();
4731
47944732 fg.maybeMarkAllowZeroAccess(ptr_info);
4795 return fg.load(ptr, ptr_ty);
4733
4734 const access_kind: Builder.MemoryAccessKind =
4735 if (ptr_info.flags.is_volatile) .@"volatile" else .normal;
4736
4737 if (ptr_info.flags.vector_index != .none) {
4738 const index_u32 = try o.builder.intValue(.i32, ptr_info.flags.vector_index);
4739 const vec_elem_ty = try fg.lowerType(elem_ty);
4740 const vec_ty = try o.builder.vectorType(.normal, ptr_info.packed_offset.host_size, vec_elem_ty);
4741
4742 const loaded_vector = try fg.wip.load(access_kind, vec_ty, ptr, llvm_ptr_align, "");
4743 return fg.wip.extractElement(loaded_vector, index_u32, "");
4744 }
4745
4746 if (ptr_info.packed_offset.host_size == 0) {
4747 return fg.load(ptr, elem_ty, llvm_ptr_align, access_kind);
4748 }
4749
4750 const containing_int_ty = try o.builder.intType(@intCast(ptr_info.packed_offset.host_size * 8));
4751 const containing_int =
4752 try fg.wip.load(access_kind, containing_int_ty, ptr, llvm_ptr_align, "");
4753
4754 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
4755 const shift_amt = try o.builder.intValue(containing_int_ty, ptr_info.packed_offset.bit_offset);
4756 const shifted_value = try fg.wip.bin(.lshr, containing_int, shift_amt, "");
4757 const elem_llvm_ty = try fg.lowerType(elem_ty);
4758
4759 if (isByRef(elem_ty, zcu)) {
4760 const result_align = elem_ty.abiAlignment(zcu).toLlvm();
4761 const result_ptr = try fg.buildAlloca(elem_llvm_ty, result_align);
4762
4763 const same_size_int = try o.builder.intType(@intCast(elem_bits));
4764 const truncated_int = try fg.wip.cast(.trunc, shifted_value, same_size_int, "");
4765 _ = try fg.wip.store(.normal, truncated_int, result_ptr, result_align);
4766 return result_ptr;
4767 }
4768
4769 if (elem_ty.zigTypeTag(zcu) == .float or elem_ty.zigTypeTag(zcu) == .vector) {
4770 const same_size_int = try o.builder.intType(@intCast(elem_bits));
4771 const truncated_int = try fg.wip.cast(.trunc, shifted_value, same_size_int, "");
4772 return fg.wip.cast(.bitcast, truncated_int, elem_llvm_ty, "");
4773 }
4774
4775 if (elem_ty.isPtrAtRuntime(zcu)) {
4776 const same_size_int = try o.builder.intType(@intCast(elem_bits));
4777 const truncated_int = try fg.wip.cast(.trunc, shifted_value, same_size_int, "");
4778 return fg.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, "");
4779 }
4780
4781 return fg.wip.cast(.trunc, shifted_value, elem_llvm_ty, "");
47964782}
47974783
47984784fn airTrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
47994785 _ = inst;
4800 const target = self.pt.zcu.getTarget();
4786 const target = self.object.zcu.getTarget();
48014787 if ((target.cpu.arch == .mips or target.cpu.arch == .mipsel) and
48024788 target.cpu.has(.mips, .notraps))
48034789 {
......@@ -4828,8 +4814,8 @@ fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V
48284814fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48294815 _ = inst;
48304816 const o = self.object;
4831 const llvm_usize = try self.lowerType(Type.usize);
4832 if (!target_util.supportsReturnAddress(self.pt.zcu.getTarget(), self.ownerModule().optimize_mode)) {
4817 const llvm_usize = try self.lowerType(.usize);
4818 if (!target_util.supportsReturnAddress(self.object.zcu.getTarget(), self.ownerModule().optimize_mode)) {
48334819 // https://github.com/ziglang/zig/issues/11946
48344820 return o.builder.intValue(llvm_usize, 0);
48354821 }
......@@ -4840,7 +4826,7 @@ fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
48404826fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48414827 _ = inst;
48424828 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{.@"0"}, "");
4843 return self.wip.cast(.ptrtoint, result, try self.lowerType(Type.usize), "");
4829 return self.wip.cast(.ptrtoint, result, try self.lowerType(.usize), "");
48444830}
48454831
48464832fn airCmpxchg(
......@@ -4849,8 +4835,7 @@ fn airCmpxchg(
48494835 kind: Builder.Function.Instruction.CmpXchg.Kind,
48504836) Allocator.Error!Builder.Value {
48514837 const o = self.object;
4852 const pt = self.pt;
4853 const zcu = pt.zcu;
4838 const zcu = o.zcu;
48544839 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
48554840 const extra = self.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
48564841 const ptr = try self.resolveInst(extra.ptr);
......@@ -4915,8 +4900,7 @@ fn airCmpxchg(
49154900
49164901fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
49174902 const o = self.object;
4918 const pt = self.pt;
4919 const zcu = pt.zcu;
4903 const zcu = o.zcu;
49204904 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
49214905 const extra = self.air.extraData(Air.AtomicRmw, pl_op.payload).data;
49224906 const ptr = try self.resolveInst(pl_op.operand);
......@@ -4971,7 +4955,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
49714955 access_kind,
49724956 op,
49734957 ptr,
4974 try self.wip.cast(.ptrtoint, operand, try self.lowerType(Type.usize), ""),
4958 try self.wip.cast(.ptrtoint, operand, try self.lowerType(.usize), ""),
49754959 self.sync_scope,
49764960 ordering,
49774961 ptr_alignment,
......@@ -4980,8 +4964,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
49804964}
49814965
49824966fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4983 const pt = self.pt;
4984 const zcu = pt.zcu;
4967 const zcu = self.object.zcu;
49854968 const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
49864969 const ptr = try self.resolveInst(atomic_load.ptr);
49874970 const ptr_ty = self.typeOf(atomic_load.ptr);
......@@ -5029,8 +5012,7 @@ fn airAtomicStore(
50295012 inst: Air.Inst.Index,
50305013 ordering: Builder.AtomicOrdering,
50315014) Allocator.Error!Builder.Value {
5032 const pt = self.pt;
5033 const zcu = pt.zcu;
5015 const zcu = self.object.zcu;
50345016 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
50355017 const ptr_ty = self.typeOf(bin_op.lhs);
50365018 const operand_ty = ptr_ty.childType(zcu);
......@@ -5051,14 +5033,13 @@ fn airAtomicStore(
50515033
50525034 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
50535035
5054 try self.store(ptr, ptr_ty, element, ordering);
5036 try self.storeFull(ptr, ptr_ty, element, ordering);
50555037 return .none;
50565038}
50575039
50585040fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
50595041 const o = self.object;
5060 const pt = self.pt;
5061 const zcu = pt.zcu;
5042 const zcu = o.zcu;
50625043 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
50635044 const dest_slice = try self.resolveInst(bin_op.lhs);
50645045 const ptr_ty = self.typeOf(bin_op.lhs);
......@@ -5070,7 +5051,8 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
50705051
50715052 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
50725053
5073 if (try self.air.value(bin_op.rhs, pt)) |elem_val| {
5054 if (bin_op.rhs.toInterned()) |elem_ip_index| {
5055 const elem_val: Value = .fromInterned(elem_ip_index);
50745056 if (elem_val.isUndef(zcu)) {
50755057 // Even if safety is disabled, we still emit a memset to undefined since it conveys
50765058 // extra information to LLVM. However, safety makes the difference between using
......@@ -5099,7 +5081,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
50995081 // repeating byte pattern, for example, `@as(u64, 0)` has a
51005082 // repeating byte pattern of 0 bytes. In such case, the memset
51015083 // intrinsic can be used.
5102 if (try elem_val.hasRepeatedByteRepr(pt)) |byte_val| {
5084 if (try elem_val.hasRepeatedByteRepr(zcu)) |byte_val| {
51035085 const fill_byte = try o.builder.intValue(.i8, byte_val);
51045086 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
51055087 _ = try self.wip.callMemSet(
......@@ -5154,7 +5136,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
51545136 const body_block = try self.wip.block(1, "InlineMemsetBody");
51555137 const end_block = try self.wip.block(1, "InlineMemsetEnd");
51565138
5157 const llvm_usize_ty = try self.lowerType(Type.usize);
5139 const llvm_usize_ty = try self.lowerType(.usize);
51585140 const end_ptr = switch (ptr_ty.ptrSize(zcu)) {
51595141 .slice => try self.ptraddScaled(
51605142 dest_ptr,
......@@ -5194,8 +5176,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
51945176}
51955177
51965178fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5197 const pt = self.pt;
5198 const zcu = pt.zcu;
5179 const zcu = self.object.zcu;
51995180 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
52005181 const dest_slice = try self.resolveInst(bin_op.lhs);
52015182 const dest_ptr_ty = self.typeOf(bin_op.lhs);
......@@ -5223,8 +5204,7 @@ fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
52235204}
52245205
52255206fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5226 const pt = self.pt;
5227 const zcu = pt.zcu;
5207 const zcu = self.object.zcu;
52285208 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
52295209 const dest_slice = try self.resolveInst(bin_op.lhs);
52305210 const dest_ptr_ty = self.typeOf(bin_op.lhs);
......@@ -5248,8 +5228,7 @@ fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
52485228}
52495229
52505230fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5251 const pt = self.pt;
5252 const zcu = pt.zcu;
5231 const zcu = self.object.zcu;
52535232 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
52545233 const un_ptr_ty = self.typeOf(bin_op.lhs);
52555234 const un_ty = un_ptr_ty.childType(zcu);
......@@ -5280,8 +5259,7 @@ fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.
52805259
52815260fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
52825261 const o = self.object;
5283 const pt = self.pt;
5284 const zcu = pt.zcu;
5262 const zcu = o.zcu;
52855263 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
52865264 const un_ty = self.typeOf(ty_op.operand);
52875265 const layout = un_ty.unionGetLayout(zcu);
......@@ -5354,8 +5332,7 @@ fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)
53545332
53555333fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
53565334 const o = self.object;
5357 const pt = self.pt;
5358 const zcu = pt.zcu;
5335 const zcu = o.zcu;
53595336 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
53605337 const operand_ty = self.typeOf(ty_op.operand);
53615338 var bits = operand_ty.intInfo(zcu).bits;
......@@ -5389,8 +5366,7 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
53895366
53905367fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
53915368 const o = self.object;
5392 const pt = self.pt;
5393 const zcu = pt.zcu;
5369 const zcu = o.zcu;
53945370 const ip = &zcu.intern_pool;
53955371 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
53965372 const operand = try self.resolveInst(ty_op.operand);
......@@ -5426,7 +5402,7 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bui
54265402 const operand = try self.resolveInst(un_op);
54275403 const enum_ty = self.typeOf(un_op);
54285404
5429 const llvm_fn = try o.getIsNamedEnumValueFunction(self.pt, enum_ty);
5405 const llvm_fn = try o.getIsNamedEnumValueFunction(enum_ty);
54305406 return self.wip.call(
54315407 .normal,
54325408 .fastcc,
......@@ -5440,12 +5416,11 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bui
54405416
54415417fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
54425418 const o = self.object;
5443 const pt = self.pt;
54445419 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
54455420 const operand = try self.resolveInst(un_op);
54465421 const enum_ty = self.typeOf(un_op);
54475422
5448 const llvm_fn = try o.getEnumTagNameFunction(pt, enum_ty);
5423 const llvm_fn = try o.getEnumTagNameFunction(enum_ty);
54495424 return self.wip.call(
54505425 .normal,
54515426 .fastcc,
......@@ -5459,8 +5434,7 @@ fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
54595434
54605435fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
54615436 const o = self.object;
5462 const pt = self.pt;
5463 const zcu = pt.zcu;
5437 const zcu = o.zcu;
54645438 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
54655439 const operand = try self.resolveInst(un_op);
54665440 const slice_ty = self.typeOfIndex(inst);
......@@ -5493,8 +5467,7 @@ fn airSelect(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
54935467
54945468fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
54955469 const o = fg.object;
5496 const pt = fg.pt;
5497 const zcu = pt.zcu;
5470 const zcu = o.zcu;
54985471 const gpa = zcu.gpa;
54995472
55005473 const unwrapped = fg.air.unwrapShuffleOne(zcu, inst);
......@@ -5534,7 +5507,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
55345507 .elem => llvm_poison_elem,
55355508 .value => |val| if (!Value.fromInterned(val).isUndef(zcu)) elem: {
55365509 any_defined_comptime_value = true;
5537 break :elem try o.lowerValue(pt, val);
5510 break :elem try o.lowerValue(val);
55385511 } else llvm_poison_elem,
55395512 };
55405513 }
......@@ -5600,8 +5573,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
56005573
56015574fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
56025575 const o = fg.object;
5603 const pt = fg.pt;
5604 const zcu = pt.zcu;
5576 const zcu = o.zcu;
56055577 const gpa = zcu.gpa;
56065578
56075579 const unwrapped = fg.air.unwrapShuffleTwo(zcu, inst);
......@@ -5699,7 +5671,7 @@ fn buildReducedCall(
56995671 accum_init: Builder.Value,
57005672) Allocator.Error!Builder.Value {
57015673 const o = self.object;
5702 const usize_ty = try self.lowerType(Type.usize);
5674 const usize_ty = try self.lowerType(.usize);
57035675 const llvm_vector_len = try o.builder.intValue(usize_ty, vector_len);
57045676 const llvm_result_ty = accum_init.typeOfWip(&self.wip);
57055677
......@@ -5753,8 +5725,7 @@ fn buildReducedCall(
57535725
57545726fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
57555727 const o = self.object;
5756 const pt = self.pt;
5757 const zcu = pt.zcu;
5728 const zcu = o.zcu;
57585729 const target = zcu.getTarget();
57595730
57605731 const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
......@@ -5863,8 +5834,7 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) A
58635834
58645835fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
58655836 const o = self.object;
5866 const pt = self.pt;
5867 const zcu = pt.zcu;
5837 const zcu = o.zcu;
58685838 const ip = &zcu.intern_pool;
58695839 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
58705840 const result_ty = self.typeOfIndex(inst);
......@@ -5960,19 +5930,18 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
59605930 const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment);
59615931
59625932 const array_info = result_ty.arrayInfo(zcu);
5963 const elem_ptr_ty = try pt.singleConstPtrType(array_info.elem_type);
59645933
59655934 const elem_size = array_info.elem_type.abiSize(zcu);
59665935
59675936 for (elements, 0..) |elem, i| {
59685937 const elem_ptr = try self.ptraddConst(alloca_inst, elem_size * i);
59695938 const llvm_elem = try self.resolveInst(elem);
5970 try self.store(elem_ptr, elem_ptr_ty, llvm_elem, .none);
5939 try self.store(elem_ptr, .none, llvm_elem, array_info.elem_type);
59715940 }
59725941 if (array_info.sentinel) |sent_val| {
59735942 const elem_ptr = try self.ptraddConst(alloca_inst, elem_size * array_info.len);
59745943 const llvm_elem = try self.resolveValue(sent_val);
5975 try self.store(elem_ptr, elem_ptr_ty, llvm_elem.toValue(), .none);
5944 try self.store(elem_ptr, .none, llvm_elem.toValue(), array_info.elem_type);
59765945 }
59775946
59785947 return alloca_inst;
......@@ -5983,8 +5952,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
59835952
59845953fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
59855954 const o = self.object;
5986 const pt = self.pt;
5987 const zcu = pt.zcu;
5955 const zcu = o.zcu;
59885956 const ip = &zcu.intern_pool;
59895957 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
59905958 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
......@@ -6006,18 +5974,19 @@ fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
60065974 assert(field_ty.hasRuntimeBits(zcu));
60075975
60085976 {
6009 const payload_ptr_ty = try pt.ptrType(.{
6010 .child = field_ty.toIntern(),
6011 .flags = .{ .alignment = layout.payload_align },
6012 });
60135977 const payload_ptr = try self.ptraddConst(result_ptr, layout.payloadOffset());
6014 try self.store(payload_ptr, payload_ptr_ty, llvm_payload, .none);
5978 try self.store(payload_ptr, layout.payload_align, llvm_payload, field_ty);
60155979 }
60165980
60175981 if (layout.tag_size != 0) {
6018 const tag_ty: Type = .fromInterned(union_obj.enum_tag_type);
6019 const tag_val = try pt.enumValueFieldIndex(tag_ty, extra.field_index);
6020 const llvm_tag_val = try o.lowerValue(pt, tag_val.toIntern());
5982 const loaded_enum = ip.loadEnumType(union_obj.enum_tag_type);
5983 const llvm_tag_val = switch (loaded_enum.field_values.getOrNone(ip, extra.field_index)) {
5984 .none => try o.builder.intConst(
5985 try o.lowerType(.fromInterned(union_obj.enum_tag_type)),
5986 extra.field_index, // auto-numbered
5987 ),
5988 else => |tag_val_ip| try o.lowerValue(tag_val_ip),
5989 };
60215990 const tag_ptr = try self.ptraddConst(result_ptr, layout.tagOffset());
60225991 _ = try self.wip.store(.normal, llvm_tag_val.toValue(), tag_ptr, layout.tag_align.toLlvm());
60235992 }
......@@ -6043,7 +6012,7 @@ fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
60436012 // by the target.
60446013 // To work around this, don't emit llvm.prefetch in this case.
60456014 // See https://bugs.llvm.org/show_bug.cgi?id=21037
6046 const zcu = self.pt.zcu;
6015 const zcu = self.object.zcu;
60476016 const target = zcu.getTarget();
60486017 switch (prefetch.cache) {
60496018 .instruction => switch (target.cpu.arch) {
......@@ -6097,7 +6066,7 @@ fn workIntrinsic(
60976066}
60986067
60996068fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
6100 const target = self.pt.zcu.getTarget();
6069 const target = self.object.zcu.getTarget();
61016070
61026071 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
61036072 const dimension = pl_op.payload;
......@@ -6110,8 +6079,7 @@ fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V
61106079}
61116080
61126081fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
6113 const pt = self.pt;
6114 const target = pt.zcu.getTarget();
6082 const target = self.object.zcu.getTarget();
61156083
61166084 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
61176085 const dimension = pl_op.payload;
......@@ -6138,7 +6106,7 @@ fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
61386106}
61396107
61406108fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
6141 const target = self.pt.zcu.getTarget();
6109 const target = self.object.zcu.getTarget();
61426110
61436111 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
61446112 const dimension = pl_op.payload;
......@@ -6158,7 +6126,7 @@ fn optCmpNull(
61586126 opt_ptr: Builder.Value,
61596127 access_kind: Builder.MemoryAccessKind,
61606128) Allocator.Error!Builder.Value {
6161 const zcu = self.pt.zcu;
6129 const zcu = self.object.zcu;
61626130 assert(isByRef(opt_ty, zcu));
61636131 comptime assert(optional_layout_version == 3);
61646132 // Non-null bit is always after the payload, with no padding because it has alignment 1.
......@@ -6174,8 +6142,7 @@ fn optPayloadHandle(
61746142 opt_ty: Type,
61756143 can_elide_load: bool,
61766144) Allocator.Error!Builder.Value {
6177 const pt = fg.pt;
6178 const zcu = pt.zcu;
6145 const zcu = fg.object.zcu;
61796146 assert(isByRef(opt_ty, zcu));
61806147 const payload_ty = opt_ty.optionalChild(zcu);
61816148
......@@ -6197,8 +6164,7 @@ fn fieldPtr(
61976164 aggregate_ptr_ty: Type,
61986165 field_index: u32,
61996166) Allocator.Error!Builder.Value {
6200 const pt = self.pt;
6201 const zcu = pt.zcu;
6167 const zcu = self.object.zcu;
62026168 const aggregate_ty = aggregate_ptr_ty.childType(zcu);
62036169 if (aggregate_ty.containerLayout(zcu) == .@"packed") {
62046170 // A pointer to a bitpack field is equivalent to a pointer to the whole bitpack; the
......@@ -6226,8 +6192,7 @@ fn loadTruncate(
62266192 // => so load the byte aligned value and trunc the unwanted bits.
62276193
62286194 const o = fg.object;
6229 const pt = fg.pt;
6230 const zcu = pt.zcu;
6195 const zcu = o.zcu;
62316196 const payload_llvm_ty = try fg.lowerType(payload_ty);
62326197 const abi_size = payload_ty.abiSize(zcu);
62336198
......@@ -6256,12 +6221,11 @@ fn loadByRef(
62566221 access_kind: Builder.MemoryAccessKind,
62576222) Allocator.Error!Builder.Value {
62586223 const o = fg.object;
6259 const pt = fg.pt;
62606224 const pointee_llvm_ty = try fg.lowerType(pointee_type);
62616225 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)
6262 .max(pointee_type.abiAlignment(pt.zcu)).toLlvm();
6226 .max(pointee_type.abiAlignment(o.zcu)).toLlvm();
62636227 const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align);
6264 const size_bytes = pointee_type.abiSize(pt.zcu);
6228 const size_bytes = pointee_type.abiSize(o.zcu);
62656229 _ = try fg.wip.callMemCpy(
62666230 result_ptr,
62676231 result_align,
......@@ -6274,76 +6238,24 @@ fn loadByRef(
62746238 return result_ptr;
62756239}
62766240
6277/// This function always performs a copy. For isByRef=true types, it creates a new
6278/// alloca and copies the value into it, then returns the alloca instruction.
6279/// For isByRef=false types, it creates a load instruction and returns it.
6280fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) Allocator.Error!Builder.Value {
6281 const o = self.object;
6282 const pt = self.pt;
6283 const zcu = pt.zcu;
6284 const info = ptr_ty.ptrInfo(zcu);
6285 const elem_ty = Type.fromInterned(info.child);
6286 if (!elem_ty.hasRuntimeBits(zcu)) return .none;
6287
6288 const ptr_alignment = (if (info.flags.alignment != .none)
6289 @as(InternPool.Alignment, info.flags.alignment)
6290 else
6291 elem_ty.abiAlignment(zcu)).toLlvm();
6292
6293 const access_kind: Builder.MemoryAccessKind =
6294 if (info.flags.is_volatile) .@"volatile" else .normal;
6295
6296 if (info.flags.vector_index != .none) {
6297 const index_u32 = try o.builder.intValue(.i32, info.flags.vector_index);
6298 const vec_elem_ty = try self.lowerType(elem_ty);
6299 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
6300
6301 const loaded_vector = try self.wip.load(access_kind, vec_ty, ptr, ptr_alignment, "");
6302 return self.wip.extractElement(loaded_vector, index_u32, "");
6303 }
6304
6305 if (info.packed_offset.host_size == 0) {
6306 if (isByRef(elem_ty, zcu)) {
6307 return self.loadByRef(ptr, elem_ty, ptr_alignment, access_kind);
6308 }
6309 return self.loadTruncate(access_kind, elem_ty, ptr, ptr_alignment);
6310 }
6311
6312 const containing_int_ty = try o.builder.intType(@intCast(info.packed_offset.host_size * 8));
6313 const containing_int =
6314 try self.wip.load(access_kind, containing_int_ty, ptr, ptr_alignment, "");
6315
6316 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
6317 const shift_amt = try o.builder.intValue(containing_int_ty, info.packed_offset.bit_offset);
6318 const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, "");
6319 const elem_llvm_ty = try self.lowerType(elem_ty);
6320
6241/// If `isByRef` returns `true` for `elem_ty`, this still performs a copy by memcpy'ing the value
6242/// into a new alloca.
6243fn load(
6244 fg: *FuncGen,
6245 ptr: Builder.Value,
6246 elem_ty: Type,
6247 ptr_alignment: Builder.Alignment,
6248 access_kind: Builder.MemoryAccessKind,
6249) Allocator.Error!Builder.Value {
6250 const zcu = fg.object.zcu;
63216251 if (isByRef(elem_ty, zcu)) {
6322 const result_align = elem_ty.abiAlignment(zcu).toLlvm();
6323 const result_ptr = try self.buildAlloca(elem_llvm_ty, result_align);
6324
6325 const same_size_int = try o.builder.intType(@intCast(elem_bits));
6326 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");
6327 _ = try self.wip.store(.normal, truncated_int, result_ptr, result_align);
6328 return result_ptr;
6329 }
6330
6331 if (elem_ty.zigTypeTag(zcu) == .float or elem_ty.zigTypeTag(zcu) == .vector) {
6332 const same_size_int = try o.builder.intType(@intCast(elem_bits));
6333 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");
6334 return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, "");
6335 }
6336
6337 if (elem_ty.isPtrAtRuntime(zcu)) {
6338 const same_size_int = try o.builder.intType(@intCast(elem_bits));
6339 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");
6340 return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, "");
6252 return fg.loadByRef(ptr, elem_ty, ptr_alignment, access_kind);
6253 } else {
6254 return fg.loadTruncate(access_kind, elem_ty, ptr, ptr_alignment);
63416255 }
6342
6343 return self.wip.cast(.trunc, shifted_value, elem_llvm_ty, "");
63446256}
63456257
6346fn store(
6258fn storeFull(
63476259 self: *FuncGen,
63486260 ptr: Builder.Value,
63496261 ptr_ty: Type,
......@@ -6351,8 +6263,7 @@ fn store(
63516263 ordering: Builder.AtomicOrdering,
63526264) Allocator.Error!void {
63536265 const o = self.object;
6354 const pt = self.pt;
6355 const zcu = pt.zcu;
6266 const zcu = o.zcu;
63566267 const info = ptr_ty.ptrInfo(zcu);
63576268 const elem_ty = Type.fromInterned(info.child);
63586269 if (!elem_ty.hasRuntimeBits(zcu)) {
......@@ -6433,12 +6344,51 @@ fn store(
64336344 ptr_alignment,
64346345 elem,
64356346 elem_ty.abiAlignment(zcu).toLlvm(),
6436 try o.builder.intValue(try self.lowerType(Type.usize), elem_ty.abiSize(zcu)),
6347 try o.builder.intValue(try self.lowerType(.usize), elem_ty.abiSize(zcu)),
64376348 access_kind,
64386349 self.disable_intrinsics,
64396350 );
64406351}
64416352
6353/// Non-atomic, non-volatile, non-packed store.
6354fn store(
6355 fg: *FuncGen,
6356 ptr: Builder.Value,
6357 ptr_align: InternPool.Alignment,
6358 elem: Builder.Value,
6359 elem_ty: Type,
6360) Allocator.Error!void {
6361 const o = fg.object;
6362 const zcu = o.zcu;
6363 const llvm_ptr_align = switch (ptr_align) {
6364 .none => elem_ty.abiAlignment(zcu).toLlvm(),
6365 else => ptr_align.toLlvm(),
6366 };
6367 if (isByRef(elem_ty, zcu)) {
6368 _ = try fg.wip.callMemCpy(
6369 ptr,
6370 llvm_ptr_align,
6371 elem,
6372 elem_ty.abiAlignment(zcu).toLlvm(),
6373 try o.builder.intValue(
6374 try fg.lowerType(.usize),
6375 elem_ty.abiSize(zcu),
6376 ),
6377 .normal,
6378 fg.disable_intrinsics,
6379 );
6380 } else {
6381 _ = try fg.wip.storeAtomic(
6382 .normal,
6383 elem,
6384 ptr,
6385 fg.sync_scope,
6386 .none,
6387 llvm_ptr_align,
6388 );
6389 }
6390}
6391
64426392fn valgrindMarkUndef(fg: *FuncGen, ptr: Builder.Value, len: Builder.Value) Allocator.Error!void {
64436393 const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
64446394 const o = fg.object;
......@@ -6460,8 +6410,7 @@ fn valgrindClientRequest(
64606410 a5: Builder.Value,
64616411) Allocator.Error!Builder.Value {
64626412 const o = fg.object;
6463 const pt = fg.pt;
6464 const zcu = pt.zcu;
6413 const zcu = o.zcu;
64656414 const target = zcu.getTarget();
64666415 if (!target_util.hasValgrindSupport(target, .stage2_llvm)) return default_value;
64676416
......@@ -6588,18 +6537,17 @@ fn valgrindClientRequest(
65886537}
65896538
65906539fn typeOf(fg: *FuncGen, inst: Air.Inst.Ref) Type {
6591 const zcu = fg.pt.zcu;
6540 const zcu = fg.object.zcu;
65926541 return fg.air.typeOf(inst, &zcu.intern_pool);
65936542}
65946543
65956544fn typeOfIndex(fg: *FuncGen, inst: Air.Inst.Index) Type {
6596 const zcu = fg.pt.zcu;
6545 const zcu = fg.object.zcu;
65976546 return fg.air.typeOfIndex(inst, &zcu.intern_pool);
65986547}
65996548
66006549const ParamTypeIterator = struct {
66016550 object: *Object,
6602 pt: Zcu.PerThread,
66036551 fn_info: InternPool.Key.FuncType,
66046552 zig_index: u32,
66056553 llvm_index: u32,
......@@ -6622,7 +6570,7 @@ const ParamTypeIterator = struct {
66226570
66236571 pub fn next(it: *ParamTypeIterator) Allocator.Error!?Lowering {
66246572 if (it.zig_index >= it.fn_info.param_types.len) return null;
6625 const ip = &it.pt.zcu.intern_pool;
6573 const ip = &it.object.zcu.intern_pool;
66266574 const ty = it.fn_info.param_types.get(ip)[it.zig_index];
66276575 it.byval_attr = false;
66286576 return nextInner(it, Type.fromInterned(ty));
......@@ -6630,8 +6578,7 @@ const ParamTypeIterator = struct {
66306578
66316579 /// `airCall` uses this instead of `next` so that it can take into account variadic functions.
66326580 fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) Allocator.Error!?Lowering {
6633 assert(std.meta.eql(it.pt, fg.pt));
6634 const ip = &it.pt.zcu.intern_pool;
6581 const ip = &it.object.zcu.intern_pool;
66356582 if (it.zig_index >= it.fn_info.param_types.len) {
66366583 if (it.zig_index >= args.len) {
66376584 return null;
......@@ -6644,8 +6591,7 @@ const ParamTypeIterator = struct {
66446591 }
66456592
66466593 fn nextInner(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering {
6647 const pt = it.pt;
6648 const zcu = pt.zcu;
6594 const zcu = it.object.zcu;
66496595 const target = zcu.getTarget();
66506596
66516597 if (!ty.hasRuntimeBits(zcu)) {
......@@ -6744,7 +6690,7 @@ const ParamTypeIterator = struct {
67446690 for (0..ty.structFieldCount(zcu)) |field_index| {
67456691 const field_ty = ty.fieldType(field_index, zcu);
67466692 if (!field_ty.hasRuntimeBits(zcu)) continue;
6747 it.types_buffer[it.types_len] = try it.object.lowerType(pt, field_ty);
6693 it.types_buffer[it.types_len] = try it.object.lowerType(field_ty);
67486694 it.types_len += 1;
67496695 }
67506696 it.llvm_index += it.types_len - 1;
......@@ -6760,7 +6706,7 @@ const ParamTypeIterator = struct {
67606706 return .byval;
67616707 } else {
67626708 var types_buffer: [8]Builder.Type = undefined;
6763 types_buffer[0] = try it.object.lowerType(pt, scalar_ty);
6709 types_buffer[0] = try it.object.lowerType(scalar_ty);
67646710 it.types_buffer = types_buffer;
67656711 it.types_len = 1;
67666712 it.llvm_index += 1;
......@@ -6785,7 +6731,7 @@ const ParamTypeIterator = struct {
67856731 }
67866732
67876733 fn nextWin64(it: *ParamTypeIterator, ty: Type) ?Lowering {
6788 const zcu = it.pt.zcu;
6734 const zcu = it.object.zcu;
67896735 switch (x86_64_abi.classifyWindows(ty, zcu, zcu.getTarget(), .arg)) {
67906736 .integer => {
67916737 if (isScalar(zcu, ty)) {
......@@ -6818,7 +6764,7 @@ const ParamTypeIterator = struct {
68186764 }
68196765
68206766 fn nextSystemV(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering {
6821 const zcu = it.pt.zcu;
6767 const zcu = it.object.zcu;
68226768 const ip = &zcu.intern_pool;
68236769 ty.assertHasLayout(zcu);
68246770 const classes = x86_64_abi.classifySystemV(ty, zcu, zcu.getTarget(), .arg);
......@@ -6909,10 +6855,9 @@ const ParamTypeIterator = struct {
69096855 return .multiple_llvm_types;
69106856 }
69116857};
6912pub fn iterateParamTypes(object: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) ParamTypeIterator {
6858pub fn iterateParamTypes(object: *Object, fn_info: InternPool.Key.FuncType) ParamTypeIterator {
69136859 return .{
69146860 .object = object,
6915 .pt = pt,
69166861 .fn_info = fn_info,
69176862 .zig_index = 0,
69186863 .llvm_index = 0,
......@@ -6976,8 +6921,8 @@ fn firstParamSRetSystemV(ty: Type, zcu: *Zcu, target: *const std.Target) bool {
69766921/// In order to support the C calling convention, some return types need to be lowered
69776922/// completely differently in the function prototype to honor the C ABI, and then
69786923/// be effectively bitcasted to the actual return type.
6979pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
6980 const zcu = pt.zcu;
6924pub fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
6925 const zcu = o.zcu;
69816926 const return_type = Type.fromInterned(fn_info.return_type);
69826927 if (!return_type.hasRuntimeBits(zcu)) {
69836928 assert(!return_type.isError(zcu));
......@@ -6986,27 +6931,27 @@ pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncT
69866931 const target = zcu.getTarget();
69876932 switch (fn_info.cc) {
69886933 .@"inline" => unreachable,
6989 .auto => return if (returnTypeByRef(zcu, target, return_type)) .void else o.lowerType(pt, return_type),
6934 .auto => return if (returnTypeByRef(zcu, target, return_type)) .void else o.lowerType(return_type),
69906935
6991 .x86_64_sysv => return lowerSystemVFnRetTy(o, pt, fn_info),
6992 .x86_64_win => return lowerWin64FnRetTy(o, pt, fn_info),
6993 .x86_stdcall => return if (isScalar(zcu, return_type)) o.lowerType(pt, return_type) else .void,
6994 .x86_sysv, .x86_win => return if (isByRef(return_type, zcu)) .void else o.lowerType(pt, return_type),
6936 .x86_64_sysv => return lowerSystemVFnRetTy(o, fn_info),
6937 .x86_64_win => return lowerWin64FnRetTy(o, fn_info),
6938 .x86_stdcall => return if (isScalar(zcu, return_type)) o.lowerType(return_type) else .void,
6939 .x86_sysv, .x86_win => return if (isByRef(return_type, zcu)) .void else o.lowerType(return_type),
69956940 .aarch64_aapcs, .aarch64_aapcs_darwin, .aarch64_aapcs_win => switch (aarch64_c_abi.classifyType(return_type, zcu)) {
69966941 .memory => return .void,
6997 .float_array => return o.lowerType(pt, return_type),
6998 .byval => return o.lowerType(pt, return_type),
6942 .float_array => return o.lowerType(return_type),
6943 .byval => return o.lowerType(return_type),
69996944 .integer => return o.builder.intType(@intCast(return_type.bitSize(zcu))),
70006945 .double_integer => return o.builder.arrayType(2, .i64),
70016946 },
70026947 .arm_aapcs, .arm_aapcs_vfp => switch (arm_c_abi.classifyType(return_type, zcu, .ret)) {
70036948 .memory, .i64_array => return .void,
70046949 .i32_array => |len| return if (len == 1) .i32 else .void,
7005 .byval => return o.lowerType(pt, return_type),
6950 .byval => return o.lowerType(return_type),
70066951 },
70076952 .mips_o32 => switch (mips_c_abi.classifyType(return_type, zcu, .ret)) {
70086953 .memory, .i32_array => return .void,
7009 .byval => return o.lowerType(pt, return_type),
6954 .byval => return o.lowerType(return_type),
70106955 },
70116956 .riscv64_lp64, .riscv32_ilp32 => switch (riscv_c_abi.classifyType(return_type, zcu)) {
70126957 .memory => return .void,
......@@ -7019,53 +6964,53 @@ pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncT
70196964 };
70206965 return o.builder.structType(.normal, &.{ integer, integer });
70216966 },
7022 .byval => return o.lowerType(pt, return_type),
6967 .byval => return o.lowerType(return_type),
70236968 .fields => {
70246969 var types_len: usize = 0;
70256970 var types: [8]Builder.Type = undefined;
70266971 for (0..return_type.structFieldCount(zcu)) |field_index| {
70276972 const field_ty = return_type.fieldType(field_index, zcu);
70286973 if (!field_ty.hasRuntimeBits(zcu)) continue;
7029 types[types_len] = try o.lowerType(pt, field_ty);
6974 types[types_len] = try o.lowerType(field_ty);
70306975 types_len += 1;
70316976 }
70326977 return o.builder.structType(.normal, types[0..types_len]);
70336978 },
70346979 },
70356980 .wasm_mvp => switch (wasm_c_abi.classifyType(return_type, zcu)) {
7036 .direct => |scalar_ty| return o.lowerType(pt, scalar_ty),
6981 .direct => |scalar_ty| return o.lowerType(scalar_ty),
70376982 .indirect => return .void,
70386983 },
70396984 // TODO investigate other callconvs
7040 else => return o.lowerType(pt, return_type),
6985 else => return o.lowerType(return_type),
70416986 }
70426987}
70436988
7044fn lowerWin64FnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
7045 const zcu = pt.zcu;
6989fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
6990 const zcu = o.zcu;
70466991 const return_type = Type.fromInterned(fn_info.return_type);
70476992 switch (x86_64_abi.classifyWindows(return_type, zcu, zcu.getTarget(), .ret)) {
70486993 .integer => {
70496994 if (isScalar(zcu, return_type)) {
7050 return o.lowerType(pt, return_type);
6995 return o.lowerType(return_type);
70516996 } else {
70526997 return o.builder.intType(@intCast(return_type.abiSize(zcu) * 8));
70536998 }
70546999 },
70557000 .win_i128 => return o.builder.vectorType(.normal, 2, .i64),
70567001 .memory => return .void,
7057 .sse => return o.lowerType(pt, return_type),
7002 .sse => return o.lowerType(return_type),
70587003 else => unreachable,
70597004 }
70607005}
70617006
7062fn lowerSystemVFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
7063 const zcu = pt.zcu;
7007fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
7008 const zcu = o.zcu;
70647009 const ip = &zcu.intern_pool;
70657010 const return_type = Type.fromInterned(fn_info.return_type);
70667011 return_type.assertHasLayout(zcu);
70677012 if (isScalar(zcu, return_type)) {
7068 return o.lowerType(pt, return_type);
7013 return o.lowerType(return_type);
70697014 }
70707015 const classes = x86_64_abi.classifySystemV(return_type, zcu, zcu.getTarget(), .ret);
70717016 var types_index: u32 = 0;
......@@ -7297,7 +7242,7 @@ pub fn isByRef(ty: Type, zcu: *const Zcu) bool {
72977242/// RMW exchange of floating-point values is bitcasted to same-sized integer
72987243/// types to work around a LLVM deficiency when targeting ARM/AArch64.
72997244fn getAtomicAbiType(fg: *const FuncGen, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type {
7300 const zcu = fg.pt.zcu;
7245 const zcu = fg.object.zcu;
73017246 switch (ty.zigTypeTag(zcu)) {
73027247 .int, .@"enum", .@"struct", .@"union" => {},
73037248 .float => {
src/codegen/riscv64/CodeGen.zig+2-2
......@@ -4956,8 +4956,8 @@ fn genCall(
49564956 // on linking.
49574957 switch (info) {
49584958 .air => |callee| {
4959 if (try func.air.value(callee, pt)) |func_value| {
4960 const func_key = zcu.intern_pool.indexToKey(func_value.ip_index);
4959 if (callee.toInterned()) |func_ip_index| {
4960 const func_key = zcu.intern_pool.indexToKey(func_ip_index);
49614961 switch (switch (func_key) {
49624962 else => func_key,
49634963 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {
src/codegen/sparc64/CodeGen.zig+2-2
......@@ -1310,7 +1310,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
13101310
13111311 // Due to incremental compilation, how function calls are generated depends
13121312 // on linking.
1313 if (try self.air.value(call.callee, pt)) |func_value| switch (ip.indexToKey(func_value.toIntern())) {
1313 if (call.callee.toInterned()) |func_ip_index| switch (ip.indexToKey(func_ip_index)) {
13141314 .func => {
13151315 return self.fail("TODO implement calling functions", .{});
13161316 },
......@@ -4487,7 +4487,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
44874487 return self.getResolvedInstValue(inst);
44884488 }
44894489
4490 return self.genTypedValue((try self.air.value(ref, pt)).?);
4490 return self.genTypedValue(.fromInterned(ref.toInterned().?));
44914491}
44924492
44934493fn ret(self: *Self, mcv: MCValue) !void {
src/codegen/spirv/CodeGen.zig+7-10
......@@ -387,13 +387,12 @@ fn importExtendedSet(cg: *CodeGen) !Id {
387387
388388/// Fetch the result-id for a previously generated instruction or constant.
389389fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id {
390 const pt = cg.pt;
391390 const zcu = cg.module.zcu;
392391 const ip = &zcu.intern_pool;
393 if (try cg.air.value(inst, pt)) |val| {
392 if (inst.toInterned()) |val_ip_index| {
394393 const ty = cg.typeOf(inst);
395394 if (ty.zigTypeTag(zcu) == .@"fn") {
396 const fn_nav = switch (zcu.intern_pool.indexToKey(val.ip_index)) {
395 const fn_nav = switch (zcu.intern_pool.indexToKey(val_ip_index)) {
397396 .@"extern" => |@"extern"| @"extern".owner_nav,
398397 .func => |func| func.owner_nav,
399398 else => unreachable,
......@@ -403,7 +402,7 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id {
403402 return cg.module.declPtr(spv_decl_index).result_id;
404403 }
405404
406 return try cg.constant(ty, val, .direct);
405 return try cg.constant(ty, .fromInterned(val_ip_index), .direct);
407406 }
408407 const index = inst.toIndex().?;
409408 return cg.inst_results.get(index).?; // Assertion means instruction does not dominate usage.
......@@ -5657,7 +5656,6 @@ fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id {
56575656
56585657fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void {
56595658 const gpa = cg.module.gpa;
5660 const pt = cg.pt;
56615659 const zcu = cg.module.zcu;
56625660 const target = cg.module.zcu.getTarget();
56635661 const switch_br = cg.air.unwrapSwitch(inst);
......@@ -5732,7 +5730,7 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void {
57325730 const label = case_labels.at(case.idx);
57335731
57345732 for (case.items) |item| {
5735 const value = (try cg.air.value(item, pt)) orelse unreachable;
5733 const value: Value = .fromInterned(item.toInterned().?);
57365734 const int_val: u64 = switch (cond_ty.zigTypeTag(zcu)) {
57375735 .bool, .int => if (cond_ty.isSignedInt(zcu)) @bitCast(value.toSignedInt(zcu)) else value.toUnsignedInt(zcu),
57385736 .@"enum" => blk: {
......@@ -5875,9 +5873,9 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id {
58755873
58765874 if (std.mem.eql(u8, in.constraint, "c")) {
58775875 // constant
5878 const val = (try cg.air.value(in.operand, cg.pt)) orelse {
5876 const val: Value = .fromInterned(in.operand.toInterned() orelse {
58795877 return cg.fail("assembly inputs with 'c' constraint have to be compile-time known", .{});
5880 };
5878 });
58815879
58825880 // TODO: This entire function should be handled a bit better...
58835881 const ip = &zcu.intern_pool;
......@@ -5911,8 +5909,7 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id {
59115909 if (input_ty.zigTypeTag(zcu) == .type) {
59125910 // This assembly input is a type instead of a value.
59135911 // That's fine for now, just make sure to resolve it as such.
5914 const val = (try cg.air.value(in.operand, cg.pt)).?;
5915 const ty_id = try cg.resolveType(val.toType(), .direct);
5912 const ty_id = try cg.resolveType(in.operand.toType(), .direct);
59165913 try ass.value_map.put(gpa, in.name, .{ .ty = ty_id });
59175914 } else {
59185915 const ty_id = try cg.resolveType(input_ty, .direct);
src/codegen/wasm/CodeGen.zig+3-3
......@@ -303,7 +303,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
303303
304304 const pt = cg.pt;
305305 const zcu = pt.zcu;
306 const val = (try cg.air.value(ref, pt)).?;
306 const val: Value = .fromInterned(ref.toInterned().?);
307307 const ty = cg.typeOf(ref);
308308 if (!ty.hasRuntimeBits(zcu) and !ty.isInt(zcu) and !ty.isError(zcu)) {
309309 gop.value_ptr.* = .none;
......@@ -2006,7 +2006,7 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifie
20062006 const first_param_sret = firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), zcu, cg.target);
20072007
20082008 const callee: ?InternPool.Nav.Index = blk: {
2009 const func_val = (try cg.air.value(call.callee, pt)) orelse break :blk null;
2009 const func_val: Value = .fromInterned(call.callee.toInterned() orelse break :blk null);
20102010
20112011 switch (ip.indexToKey(func_val.toIntern())) {
20122012 inline .func, .@"extern" => |x| break :blk x.owner_nav,
......@@ -4464,7 +4464,7 @@ fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {
44644464 .vector_type => {
44654465 assert(determineSimdStoreStrategy(ty, zcu, cg.target) == .direct);
44664466 var buf: [16]u8 = undefined;
4467 val.writeToMemory(pt, &buf) catch unreachable;
4467 val.writeToMemory(zcu, &buf) catch unreachable;
44684468 return cg.storeSimdImmd(buf);
44694469 },
44704470 .struct_type => unreachable, // packed structs use `bitpack`
src/codegen/x86_64/CodeGen.zig+2-2
......@@ -176185,8 +176185,8 @@ fn genCall(self: *CodeGen, info: union(enum) {
176185176185 // Due to incremental compilation, how function calls are generated depends
176186176186 // on linking.
176187176187 switch (info) {
176188 .air => |callee| if (try self.air.value(callee, pt)) |func_value| {
176189 const func_key = ip.indexToKey(func_value.ip_index);
176188 .air => |callee| if (callee.toInterned()) |func_ip_index| {
176189 const func_key = ip.indexToKey(func_ip_index);
176190176190 switch (switch (func_key) {
176191176191 else => func_key,
176192176192 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {