authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-23 11:26:23+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-28 16:46:23+00:00
logbd8088bb9826b40be281d5cca89eac7ff7b76241
tree846600b2d3b17ed07d4bbe06fb21befb454ac5af
parent5941c9da08ae0d8ba4add37f0baeafdcd160dbd4
signaturelock-open Commit is signed but in an unrecognized format.

llvm: random enhancements and cleanups


4 files changed, 482 insertions(+), 551 deletions(-)

src/Air.zig+12-5
......@@ -870,13 +870,20 @@ pub const Inst = struct {
870870 /// Uses the `pl_op` field, payload represents the index of the target memory.
871871 wasm_memory_grow,
872872
873 /// Returns `true` if and only if the operand, an integer with
874 /// the same size as the error integer type, is less than the
875 /// total number of errors in the Module.
873 /// Returns `true` if and only if the operand, an integer with the same
874 /// size as the error integer type, is less than *or equal to* the total
875 /// number of errors in the Zcu. The "or equal to" is a consequence of
876 /// value 0 being reserved for the "non-error" status in error unions.
877 /// MLUGG TODO: rename this instruction to `cmp_lte_errors_len`
878 ///
879 /// This instruction exists (as opposed to just using `cmp_lte` against
880 /// a constant) because the number of errors in the Zcu is not known
881 /// until `Compilation.flush`. Before then, semantic analysis could
882 /// discover new errors at any time.
883 ///
876884 /// Result type is always `bool`.
885 ///
877886 /// Uses the `un_op` field.
878 /// Note that the number of errors in the Module cannot be considered stable until
879 /// flush().
880887 cmp_lt_errors_len,
881888
882889 /// Returns pointer to current error return trace.
src/Compilation.zig+1-1
......@@ -2485,7 +2485,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
24852485
24862486 if (use_llvm) {
24872487 if (opt_zcu) |zcu| {
2488 zcu.llvm_object = try LlvmObject.create(arena, comp);
2488 zcu.llvm_object = try LlvmObject.create(arena, zcu);
24892489 }
24902490 }
24912491
src/codegen/llvm.zig+111-96
......@@ -549,7 +549,7 @@ pub const Object = struct {
549549 /// type from the global error set.
550550 debug_anyerror_fwd_ref: Builder.Metadata.Optional,
551551
552 target: *const std.Target,
552 zcu: *Zcu,
553553 /// Ideally we would use `llvm_module.getNamedFunction` to go from *Decl to LLVM function,
554554 /// but that has some downsides:
555555 /// * we have to compute the fully qualified name every time we want to do the lookup
......@@ -574,9 +574,12 @@ pub const Object = struct {
574574 /// Note that the values are not added until `emit`, when all errors in
575575 /// the compilation are known.
576576 error_name_table: Builder.Variable.Index,
577
578 /// Memoizes a null `?usize` value.
579 null_opt_usize: Builder.Constant,
577 /// Constant variable whose value is the number of errors in the Zcu.
578 ///
579 /// Initially `.none`---populated lazily by `getErrorsLen`.
580 ///
581 /// If this is not `.none`, the variable's initializer is set in `emit`.
582 errors_len_variable: Builder.Variable.Index,
580583
581584 /// Values for `@llvm.used`.
582585 used: std.ArrayList(Builder.Constant),
......@@ -585,10 +588,11 @@ pub const Object = struct {
585588
586589 const TypeMap = std.AutoHashMapUnmanaged(InternPool.Index, Builder.Type);
587590
588 pub fn create(arena: Allocator, comp: *Compilation) !Ptr {
591 pub fn create(arena: Allocator, zcu: *Zcu) !Ptr {
589592 dev.check(.llvm_backend);
593 const comp = zcu.comp;
590594 const gpa = comp.gpa;
591 const target = &comp.root_mod.resolved_target.result;
595 const target = zcu.getTarget();
592596 const llvm_target_triple = try targetTriple(arena, target);
593597
594598 var builder = try Builder.init(.{
......@@ -616,10 +620,7 @@ pub const Object = struct {
616620 // way already, but here we throw all that sweet information
617621 // into the garbage can by converting into absolute paths. What
618622 // a terrible tragedy.
619 const compile_unit_dir = blk: {
620 const zcu = comp.zcu orelse break :blk comp.dirs.cwd;
621 break :blk try zcu.main_mod.root.toAbsolute(comp.dirs, arena);
622 };
623 const compile_unit_dir = try zcu.main_mod.root.toAbsolute(comp.dirs, arena);
623624
624625 const debug_file = try builder.debugFile(
625626 try builder.metadataString(comp.root_name),
......@@ -665,14 +666,14 @@ pub const Object = struct {
665666 .debug_file_map = .empty,
666667 .debug_types = .empty,
667668 .debug_anyerror_fwd_ref = .none,
668 .target = target,
669 .zcu = zcu,
669670 .nav_map = .empty,
670671 .uav_map = .empty,
671672 .enum_tag_name_map = .empty,
672673 .named_enum_map = .empty,
673674 .type_map = .empty,
674675 .error_name_table = .none,
675 .null_opt_usize = .no_init,
676 .errors_len_variable = .none,
676677 .used = .empty,
677678 };
678679 return obj;
......@@ -722,7 +723,7 @@ pub const Object = struct {
722723 name_variable_index.setLinkage(.private, &o.builder);
723724 name_variable_index.setMutability(.constant, &o.builder);
724725 name_variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
725 name_variable_index.setAlignment(comptime Builder.Alignment.fromByteUnits(1), &o.builder);
726 name_variable_index.setAlignment(comptime .fromByteUnits(1), &o.builder);
726727
727728 llvm_error.* = try o.builder.structConst(llvm_slice_ty, &.{
728729 name_variable_index.toConst(&o.builder),
......@@ -730,52 +731,17 @@ pub const Object = struct {
730731 });
731732 }
732733
733 const table_variable_index = try o.builder.addVariable(.empty, llvm_table_ty, .default);
734 try table_variable_index.setInitializer(
734 try o.error_name_table.setInitializer(
735735 try o.builder.arrayConst(llvm_table_ty, llvm_errors),
736736 &o.builder,
737737 );
738 table_variable_index.setLinkage(.private, &o.builder);
739 table_variable_index.setMutability(.constant, &o.builder);
740 table_variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
741 table_variable_index.setAlignment(
742 slice_ty.abiAlignment(zcu).toLlvm(),
743 &o.builder,
744 );
745
746 try o.error_name_table.setInitializer(table_variable_index.toConst(&o.builder), &o.builder);
747 }
748
749 fn genCmpLtErrorsLenFunction(o: *Object, pt: Zcu.PerThread) !void {
750 // If there is no such function in the module, it means the source code does not need it.
751 const name = o.builder.strtabStringIfExists(lt_errors_fn_name) orelse return;
752 const llvm_fn = o.builder.getGlobal(name) orelse return;
753 const errors_len = pt.zcu.intern_pool.global_error_set.getNamesFromMainThread().len;
754
755 var wip = try Builder.WipFunction.init(&o.builder, .{
756 .function = llvm_fn.ptrConst(&o.builder).kind.function,
757 .strip = true,
758 });
759 defer wip.deinit();
760 wip.cursor = .{ .block = try wip.block(0, "Entry") };
761
762 // Example source of the following LLVM IR:
763 // fn __zig_lt_errors_len(index: u16) bool {
764 // return index <= total_errors_len;
765 // }
766
767 const lhs = wip.arg(0);
768 const rhs = try o.builder.intValue(try o.errorIntType(pt), errors_len);
769 const is_lt = try wip.icmp(.ule, lhs, rhs, "");
770 _ = try wip.ret(is_lt);
771 try wip.finish();
772738 }
773739
774 fn genModuleLevelAssembly(object: *Object, pt: Zcu.PerThread) Allocator.Error!void {
740 fn genModuleLevelAssembly(object: *Object) Allocator.Error!void {
775741 const b = &object.builder;
776742 const gpa = b.gpa;
777743 b.module_asm.clearRetainingCapacity();
778 for (pt.zcu.global_assembly.values()) |assembly| {
744 for (object.zcu.global_assembly.values()) |assembly| {
779745 try b.module_asm.ensureUnusedCapacity(gpa, assembly.len + 1);
780746 b.module_asm.appendSliceAssumeCapacity(assembly);
781747 b.module_asm.appendAssumeCapacity('\n');
......@@ -808,9 +774,13 @@ pub const Object = struct {
808774 const diags = &comp.link_diags;
809775
810776 {
777 if (o.errors_len_variable != .none) {
778 const errors_len = zcu.intern_pool.global_error_set.getNamesFromMainThread().len;
779 const init_val = try o.builder.intConst(try o.errorIntType(), errors_len);
780 try o.errors_len_variable.setInitializer(init_val, &o.builder);
781 }
811782 try o.genErrorNameTable(pt);
812 try o.genCmpLtErrorsLenFunction(pt);
813 try o.genModuleLevelAssembly(pt);
783 try o.genModuleLevelAssembly();
814784
815785 if (o.used.items.len > 0) {
816786 const array_llvm_ty = try o.builder.arrayType(o.used.items.len, .ptr);
......@@ -827,7 +797,7 @@ pub const Object = struct {
827797
828798 if (!o.builder.strip) {
829799 if (o.debug_anyerror_fwd_ref.unwrap()) |fwd_ref| {
830 const debug_anyerror_type = try o.lowerDebugAnyerrorType(pt);
800 const debug_anyerror_type = try o.lowerDebugAnyerrorType();
831801 o.builder.resolveDebugForwardReference(fwd_ref, debug_anyerror_type);
832802 }
833803
......@@ -1454,7 +1424,7 @@ pub const Object = struct {
14541424 }
14551425
14561426 const file, const subprogram = if (!wip.strip) debug_info: {
1457 const file = try o.getDebugFile(pt, file_scope);
1427 const file = try o.getDebugFile(file_scope);
14581428
14591429 const line_number = zcu.navSrcLine(func.owner_nav) + 1;
14601430 const is_internal_linkage = ip.indexToKey(nav.resolved.?.value) != .@"extern";
......@@ -1658,7 +1628,7 @@ pub const Object = struct {
16581628 const line_number = zcu.navSrcLine(nav_index) + 1;
16591629
16601630 if (!mod.strip) {
1661 const debug_file = try o.getDebugFile(pt, file_scope);
1631 const debug_file = try o.getDebugFile(file_scope);
16621632
16631633 const debug_global_var = try o.builder.debugGlobalVar(
16641634 try o.builder.metadataString(nav.name.toSlice(ip)), // Name
......@@ -1796,7 +1766,7 @@ pub const Object = struct {
17961766 try global_index.rename(main_exp_name, &o.builder);
17971767 break :i global_index;
17981768 }
1799 const llvm_addr_space = toLlvmAddressSpace(.generic, o.target);
1769 const llvm_addr_space = toLlvmAddressSpace(.generic, zcu.getTarget());
18001770 const variable_index = try o.builder.addVariable(
18011771 main_exp_name,
18021772 try o.lowerType(pt, Type.fromInterned(ip.typeOf(exported_value))),
......@@ -2002,13 +1972,13 @@ pub const Object = struct {
20021972 }
20031973 }
20041974
2005 pub fn getDebugFile(o: *Object, pt: Zcu.PerThread, file_index: Zcu.File.Index) Allocator.Error!Builder.Metadata {
1975 pub fn getDebugFile(o: *Object, file_index: Zcu.File.Index) Allocator.Error!Builder.Metadata {
20061976 const gpa = o.gpa;
20071977 const gop = try o.debug_file_map.getOrPut(gpa, file_index);
20081978 errdefer assert(o.debug_file_map.remove(file_index));
20091979 if (gop.found_existing) return gop.value_ptr.*;
2010 const path = pt.zcu.fileByIndex(file_index).path;
2011 const abs_path = try path.toAbsolute(pt.zcu.comp.dirs, gpa);
1980 const path = o.zcu.fileByIndex(file_index).path;
1981 const abs_path = try path.toAbsolute(o.zcu.comp.dirs, gpa);
20121982 defer gpa.free(abs_path);
20131983
20141984 gop.value_ptr.* = try o.builder.debugFile(
......@@ -2035,8 +2005,8 @@ pub const Object = struct {
20352005 assert(!o.builder.strip);
20362006
20372007 const gpa = o.gpa;
2038 const target = o.target;
20392008 const zcu = pt.zcu;
2009 const target = zcu.getTarget();
20402010 const ip = &zcu.intern_pool;
20412011
20422012 const name = try o.builder.metadataStringFmt("{f}", .{ty.fmt(pt)});
......@@ -2386,7 +2356,7 @@ pub const Object = struct {
23862356
23872357 const struct_type = zcu.typeToStruct(ty).?;
23882358
2389 const file = try o.getDebugFile(pt, struct_type.zir_index.resolveFile(ip));
2359 const file = try o.getDebugFile(struct_type.zir_index.resolveFile(ip));
23902360 const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace|
23912361 try o.namespaceToDebugScope(pt, parent_namespace)
23922362 else
......@@ -2453,7 +2423,7 @@ pub const Object = struct {
24532423 .@"union" => {
24542424 const union_type = ip.loadUnionType(ty.toIntern());
24552425
2456 const file = try o.getDebugFile(pt, union_type.zir_index.resolveFile(ip));
2426 const file = try o.getDebugFile(union_type.zir_index.resolveFile(ip));
24572427 const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace|
24582428 try o.namespaceToDebugScope(pt, parent_namespace)
24592429 else
......@@ -2611,7 +2581,7 @@ pub const Object = struct {
26112581 );
26122582 },
26132583 .@"enum" => {
2614 const file = try o.getDebugFile(pt, ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip));
2584 const file = try o.getDebugFile(ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip));
26152585 const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace|
26162586 try o.namespaceToDebugScope(pt, parent_namespace)
26172587 else
......@@ -2672,7 +2642,7 @@ pub const Object = struct {
26722642 return o.builder.debugSignedType(name, 0);
26732643 }
26742644
2675 const file = try o.getDebugFile(pt, ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip));
2645 const file = try o.getDebugFile(ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip));
26762646 const scope = if (ty.getParentNamespace(zcu).unwrap()) |parent_namespace|
26772647 try o.namespaceToDebugScope(pt, parent_namespace)
26782648 else
......@@ -2697,8 +2667,8 @@ pub const Object = struct {
26972667 }
26982668
26992669 /// Called in `emit` so that the global error set is fully populated.
2700 fn lowerDebugAnyerrorType(o: *Object, pt: Zcu.PerThread) Allocator.Error!Builder.Metadata {
2701 const zcu = pt.zcu;
2670 fn lowerDebugAnyerrorType(o: *Object) Allocator.Error!Builder.Metadata {
2671 const zcu = o.zcu;
27022672 const ip = &zcu.intern_pool;
27032673 const gpa = zcu.comp.gpa;
27042674
......@@ -2732,7 +2702,7 @@ pub const Object = struct {
27322702 null, // file
27332703 o.debug_compile_unit.unwrap().?, // scope
27342704 0, // line
2735 try o.getDebugType(pt, try pt.intType(.unsigned, error_set_bits)),
2705 try o.builder.debugUnsignedType(null, error_set_bits),
27362706 Type.anyerror.abiSize(zcu) * 8,
27372707 Type.anyerror.abiAlignment(zcu).toByteUnits().? * 8,
27382708 try o.builder.metadataTuple(enumerators),
......@@ -2744,7 +2714,7 @@ pub const Object = struct {
27442714 fn namespaceToDebugScope(o: *Object, pt: Zcu.PerThread, namespace_index: InternPool.NamespaceIndex) !Builder.Metadata {
27452715 const zcu = pt.zcu;
27462716 const namespace = zcu.namespacePtr(namespace_index);
2747 if (namespace.parent == .none) return try o.getDebugFile(pt, namespace.file_scope);
2717 if (namespace.parent == .none) return try o.getDebugFile(namespace.file_scope);
27482718 return o.getDebugType(pt, .fromInterned(namespace.owner_type));
27492719 }
27502720
......@@ -3086,8 +3056,8 @@ pub const Object = struct {
30863056 return variable_index;
30873057 }
30883058
3089 pub fn errorIntType(o: *Object, pt: Zcu.PerThread) Allocator.Error!Builder.Type {
3090 return o.builder.intType(pt.zcu.errorSetBits());
3059 pub fn errorIntType(o: *Object) Allocator.Error!Builder.Type {
3060 return o.builder.intType(o.zcu.errorSetBits());
30913061 }
30923062
30933063 pub fn lowerType(o: *Object, pt: Zcu.PerThread, t: Type) Allocator.Error!Builder.Type {
......@@ -3146,7 +3116,7 @@ pub const Object = struct {
31463116 .bool_type => .i1,
31473117 .void_type => .void,
31483118 .type_type => unreachable,
3149 .anyerror_type => try o.errorIntType(pt),
3119 .anyerror_type => try o.errorIntType(),
31503120 .comptime_int_type,
31513121 .comptime_float_type,
31523122 .noreturn_type,
......@@ -3168,7 +3138,7 @@ pub const Object = struct {
31683138 .optional_noreturn_type => unreachable,
31693139 .anyerror_void_error_union_type,
31703140 .adhoc_inferred_error_set_type,
3171 => try o.errorIntType(pt),
3141 => try o.errorIntType(),
31723142 .generic_poison_type,
31733143 .empty_tuple_type,
31743144 => unreachable,
......@@ -3241,7 +3211,7 @@ pub const Object = struct {
32413211 .error_union_type => |error_union_type| {
32423212 // Must stay in sync with `codegen.errUnionPayloadOffset`.
32433213 // See logic in `lowerPtr`.
3244 const error_type = try o.errorIntType(pt);
3214 const error_type = try o.errorIntType();
32453215 if (!Type.fromInterned(error_union_type.payload_type).hasRuntimeBits(zcu))
32463216 return error_type;
32473217 const payload_type = try o.lowerType(pt, Type.fromInterned(error_union_type.payload_type));
......@@ -3474,7 +3444,7 @@ pub const Object = struct {
34743444 },
34753445 .enum_type => try o.lowerType(pt, t.intTagType(zcu)),
34763446 .func_type => |func_type| try o.lowerFnType(pt, func_type),
3477 .error_set_type, .inferred_error_set_type => try o.errorIntType(pt),
3447 .error_set_type, .inferred_error_set_type => try o.errorIntType(),
34783448 // values, not types
34793449 .undef,
34803450 .simple_value,
......@@ -3624,7 +3594,7 @@ pub const Object = struct {
36243594 },
36253595 .err => |err| {
36263596 const int = try pt.getErrorValue(err.name);
3627 const llvm_int = try o.builder.intConst(try o.errorIntType(pt), int);
3597 const llvm_int = try o.builder.intConst(try o.errorIntType(), int);
36283598 return llvm_int;
36293599 },
36303600 .error_union => |error_union| {
......@@ -4288,29 +4258,40 @@ pub const Object = struct {
42884258 if (byval) try attributes.addParamAttr(llvm_arg_i, .{ .byval = param_llvm_ty }, &o.builder);
42894259 }
42904260
4291 /// MLUGG TODO: this is super dumb
4292 pub fn getCmpLtErrorsLenFunction(o: *Object, pt: Zcu.PerThread) !Builder.Function.Index {
4293 const name = try o.builder.strtabString(lt_errors_fn_name);
4294 if (o.builder.getGlobal(name)) |llvm_fn| return llvm_fn.ptrConst(&o.builder).kind.function;
4261 pub fn getErrorNameTable(o: *Object) Allocator.Error!Builder.Variable.Index {
4262 if (o.error_name_table != .none) return o.error_name_table;
42954263
4296 const zcu = pt.zcu;
4297 const target = &zcu.root_mod.resolved_target.result;
4298 const function_index = try o.builder.addFunction(
4299 try o.builder.fnType(.i1, &.{try o.errorIntType(pt)}, .normal),
4300 name,
4301 toLlvmAddressSpace(.generic, target),
4264 const name = try o.builder.strtabString("__zig_error_name_table");
4265 // TODO: Address space
4266 const variable_index = try o.builder.addVariable(name, .ptr, .default);
4267 variable_index.setLinkage(.private, &o.builder);
4268 variable_index.setMutability(.constant, &o.builder);
4269 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
4270 variable_index.setAlignment(
4271 Type.slice_const_u8_sentinel_0.abiAlignment(o.zcu).toLlvm(),
4272 &o.builder,
43024273 );
43034274
4304 var attributes: Builder.FunctionAttributes.Wip = .{};
4305 defer attributes.deinit(&o.builder);
4306 try o.addCommonFnAttributes(&attributes, zcu.root_mod, zcu.root_mod.omit_frame_pointer);
4275 o.error_name_table = variable_index;
4276 return variable_index;
4277 }
43074278
4308 function_index.setLinkage(if (o.builder.strip) .private else .internal, &o.builder);
4309 function_index.setCallConv(.fastcc, &o.builder);
4310 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
4311 return function_index;
4279 pub fn getErrorsLen(o: *Object) Allocator.Error!Builder.Variable.Index {
4280 const builder = &o.builder;
4281 if (o.errors_len_variable == .none) {
4282 const llvm_err_int_ty = try o.errorIntType();
4283 const name = try builder.strtabString("__zig_errors_len");
4284 const variable_index = try builder.addVariable(name, llvm_err_int_ty, .default);
4285 variable_index.setLinkage(.private, builder);
4286 variable_index.setMutability(.constant, builder);
4287 variable_index.setUnnamedAddr(.unnamed_addr, builder);
4288 variable_index.setAlignment(Type.errorAbiAlignment(o.zcu).toLlvm(), builder);
4289 o.errors_len_variable = variable_index;
4290 }
4291 return o.errors_len_variable;
43124292 }
43134293
4294 /// MLUGG TODO: this also needs incremental updates dumbass
43144295 pub fn getEnumTagNameFunction(o: *Object, pt: Zcu.PerThread, enum_ty: Type) !Builder.Function.Index {
43154296 const zcu = pt.zcu;
43164297 const ip = &zcu.intern_pool;
......@@ -4394,7 +4375,25 @@ pub const Object = struct {
43944375 return o.lazy_abi_aligns.items[@intFromEnum(index)];
43954376 }
43964377
4397 pub fn updateIsNamedEnumValueFunction(
4378 pub fn getIsNamedEnumValueFunction(o: *Object, pt: Zcu.PerThread, enum_ty: Type) !Builder.Function.Index {
4379 const zcu = pt.zcu;
4380 const ip = &zcu.intern_pool;
4381
4382 const gop = try o.named_enum_map.getOrPut(o.gpa, enum_ty.toIntern());
4383 if (gop.found_existing) return gop.value_ptr.*;
4384 errdefer assert(o.named_enum_map.remove(enum_ty.toIntern()));
4385 const function_index = try o.builder.addFunction(
4386 // Dummy function type; `updateIsNamedEnumValue` will replace it with the correct type.
4387 // TODO: change the builder API so we don't need to do this.
4388 try o.builder.fnType(.void, &.{}, .normal),
4389 try o.builder.strtabStringFmt("__zig_is_named_enum_value_{f}", .{enum_ty.containerTypeName(ip).fmt(ip)}),
4390 toLlvmAddressSpace(.generic, zcu.getTarget()),
4391 );
4392 gop.value_ptr.* = function_index;
4393 try o.updateIsNamedEnumValueFunction(pt, enum_ty, function_index);
4394 return function_index;
4395 }
4396 fn updateIsNamedEnumValueFunction(
43984397 o: *Object,
43994398 pt: Zcu.PerThread,
44004399 enum_ty: Type,
......@@ -4445,6 +4444,24 @@ pub const Object = struct {
44454444
44464445 try wip.finish();
44474446 }
4447
4448 pub fn getLibcFunction(
4449 o: *Object,
4450 fn_name: Builder.StrtabString,
4451 param_types: []const Builder.Type,
4452 return_type: Builder.Type,
4453 ) Allocator.Error!Builder.Function.Index {
4454 if (o.builder.getGlobal(fn_name)) |global| return switch (global.ptrConst(&o.builder).kind) {
4455 .alias => |alias| alias.getAliasee(&o.builder).ptrConst(&o.builder).kind.function,
4456 .function => |function| function,
4457 .variable, .replaced => unreachable,
4458 };
4459 return o.builder.addFunction(
4460 try o.builder.fnType(return_type, param_types, .normal),
4461 fn_name,
4462 toLlvmAddressSpace(.generic, o.zcu.getTarget()),
4463 );
4464 }
44484465};
44494466
44504467const CallingConventionInfo = struct {
......@@ -4796,8 +4813,6 @@ const struct_layout_version = 2;
47964813// https://github.com/llvm/llvm-project/issues/56585/ is fixed
47974814pub const optional_layout_version = 3;
47984815
4799const lt_errors_fn_name = "__zig_lt_errors_len";
4800
48014816pub fn initializeLLVMTarget(arch: std.Target.Cpu.Arch) void {
48024817 switch (arch) {
48034818 .aarch64, .aarch64_be => {
src/codegen/llvm/FuncGen.zig+358-449
......@@ -1,7 +1,5 @@
11const FuncGen = @This();
22
3pub const Error = Zcu.CodegenFailError;
4
53object: *Object,
64nav_index: InternPool.Nav.Index,
75pt: Zcu.PerThread,
......@@ -63,7 +61,17 @@ disable_intrinsics: bool,
6361/// Have we seen loads or stores involving `allowzero` pointers?
6462allowzero_access: bool,
6563
66fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) Error {
64/// In general, codegen should never emit errors; we cannot report useful source locations for them
65/// and they don't really play nicely with incremental compilation. The LLVM backend mostly obeys
66/// this rule. Where it does not, it calls `todo` to emit an error, and results in this error set
67/// being used for the function
68///
69/// Please avoid using this error set in new code. Ideally, every fallible function in this file
70/// should have the error set `Allocator.Error`.
71const TodoError = Zcu.CodegenFailError;
72
73/// Avoid introducing new calls to this function---see documentation comment on `TodoError`.
74fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {
6775 @branchHint(.cold);
6876 return fg.pt.zcu.codegenFail(
6977 fg.nav_index,
......@@ -167,7 +175,11 @@ fn resolveValue(self: *FuncGen, val: Value) Allocator.Error!Builder.Constant {
167175 }
168176}
169177
170pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air.CoveragePoint) Error!void {
178fn lowerType(fg: *const FuncGen, ty: Type) Allocator.Error!Builder.Type {
179 return fg.object.lowerType(fg.pt, ty);
180}
181
182pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air.CoveragePoint) TodoError!void {
171183 const o = self.object;
172184 const zcu = self.pt.zcu;
173185 const ip = &zcu.intern_pool;
......@@ -443,13 +455,16 @@ pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air
443455
444456 // Instructions which may be `noreturn`.
445457 .block => res: {
446 const res = try self.airBlock(inst);
447 if (self.typeOfIndex(inst).isNoReturn(zcu)) return;
458 const block = self.air.unwrapBlock(inst);
459 const res = try self.lowerBlock(inst, null, block.body);
460 if (block.ty.isNoReturn(zcu)) return;
448461 break :res res;
449462 },
450463 .dbg_inline_block => res: {
451 const res = try self.airDbgInlineBlock(inst);
452 if (self.typeOfIndex(inst).isNoReturn(zcu)) return;
464 const block = self.air.unwrapDbgBlock(inst);
465 self.arg_inline_index = 0;
466 const res = try self.lowerBlock(inst, block.func, block.body);
467 if (block.ty.isNoReturn(zcu)) return;
453468 break :res res;
454469 },
455470 .call, .call_always_tail, .call_never_tail, .call_never_inline => |tag| res: {
......@@ -479,7 +494,7 @@ fn genBodyDebugScope(
479494 maybe_inline_func: ?InternPool.Index,
480495 body: []const Air.Inst.Index,
481496 coverage_point: Air.CoveragePoint,
482) Error!void {
497) TodoError!void {
483498 const o = self.object;
484499
485500 if (self.wip.strip) return self.genBody(body, coverage_point);
......@@ -508,7 +523,7 @@ fn genBodyDebugScope(
508523 const file_scope = zcu.navFileScopeIndex(func.owner_nav);
509524 const mod = zcu.fileByIndex(file_scope).mod.?;
510525
511 self.file = try o.getDebugFile(pt, file_scope);
526 self.file = try o.getDebugFile(file_scope);
512527
513528 self.base_line = zcu.navSrcLine(func.owner_nav);
514529 const line_number = self.base_line + 1;
......@@ -562,7 +577,7 @@ const CallAttr = enum {
562577 AlwaysInline,
563578};
564579
565fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !Builder.Value {
580fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) Allocator.Error!Builder.Value {
566581 const air_call = self.air.unwrapCall(inst);
567582 const args = air_call.args;
568583 const o = self.object;
......@@ -598,7 +613,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
598613 }
599614
600615 const ret_ptr = if (!sret) null else blk: {
601 const llvm_ret_ty = try o.lowerType(pt, return_type);
616 const llvm_ret_ty = try self.lowerType(return_type);
602617 try attributes.addParamAttr(0, .{ .sret = llvm_ret_ty }, &o.builder);
603618
604619 const alignment = return_type.abiAlignment(zcu).toLlvm();
......@@ -620,7 +635,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
620635 const arg = args[it.zig_index - 1];
621636 const param_ty = self.typeOf(arg);
622637 const llvm_arg = try self.resolveInst(arg);
623 const llvm_param_ty = try o.lowerType(pt, param_ty);
638 const llvm_param_ty = try self.lowerType(param_ty);
624639 if (isByRef(param_ty, zcu)) {
625640 const alignment = param_ty.abiAlignment(zcu).toLlvm();
626641 const loaded = try self.wip.load(.normal, llvm_param_ty, llvm_arg, alignment, "");
......@@ -649,7 +664,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
649664 const llvm_arg = try self.resolveInst(arg);
650665
651666 const alignment = param_ty.abiAlignment(zcu).toLlvm();
652 const param_llvm_ty = try o.lowerType(pt, param_ty);
667 const param_llvm_ty = try self.lowerType(param_ty);
653668 const arg_ptr = try self.buildAlloca(param_llvm_ty, alignment);
654669 if (isByRef(param_ty, zcu)) {
655670 const loaded = try self.wip.load(.normal, param_llvm_ty, llvm_arg, alignment, "");
......@@ -719,7 +734,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
719734 llvm_arg = ptr;
720735 }
721736
722 const float_ty = try o.lowerType(pt, aarch64_c_abi.getFloatArrayType(arg_ty, zcu).?);
737 const float_ty = try self.lowerType(aarch64_c_abi.getFloatArrayType(arg_ty, zcu).?);
723738 const array_ty = try o.builder.arrayType(count, float_ty);
724739
725740 const loaded = try self.wip.load(.normal, array_ty, llvm_arg, alignment, "");
......@@ -760,7 +775,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
760775 .byref => {
761776 const param_index = it.zig_index - 1;
762777 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[param_index]);
763 const param_llvm_ty = try o.lowerType(pt, param_ty);
778 const param_llvm_ty = try self.lowerType(param_ty);
764779 const alignment = param_ty.abiAlignment(zcu).toLlvm();
765780 try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment, it.byval_attr, param_llvm_ty);
766781 },
......@@ -812,7 +827,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
812827 },
813828 toLlvmCallConvTag(fn_info.cc, target).?,
814829 try attributes.finish(&o.builder),
815 try o.lowerType(pt, zig_fn_ty),
830 try self.lowerType(zig_fn_ty),
816831 llvm_fn,
817832 llvm_args.items,
818833 "",
......@@ -826,7 +841,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
826841 return .none;
827842 }
828843
829 const llvm_ret_ty = try o.lowerType(pt, return_type);
844 const llvm_ret_ty = try self.lowerType(return_type);
830845 if (ret_ptr) |rp| {
831846 if (isByRef(return_type, zcu)) {
832847 return rp;
......@@ -864,7 +879,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
864879 }
865880}
866881
867fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) !void {
882fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!void {
868883 const o = fg.object;
869884 const pt = fg.pt;
870885 const zcu = pt.zcu;
......@@ -888,7 +903,7 @@ fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) !void {
888903 _ = try fg.wip.@"unreachable"();
889904}
890905
891fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !void {
906fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!void {
892907 const o = self.object;
893908 const pt = self.pt;
894909 const zcu = pt.zcu;
......@@ -910,7 +925,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !void {
910925 // https://github.com/ziglang/zig/issues/15337
911926 break :undef;
912927 }
913 const len = try o.builder.intValue(try o.lowerType(pt, Type.usize), ret_ty.abiSize(zcu));
928 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
914929 _ = try self.wip.callMemSet(
915930 self.ret_ptr,
916931 ptr_ty.ptrAlignment(zcu).toLlvm(),
......@@ -946,7 +961,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !void {
946961 // Functions with an empty error set are emitted with an error code
947962 // return type and return zero so they can be function pointers coerced
948963 // to functions that return anyerror.
949 _ = try self.wip.ret(try o.builder.intValue(try o.errorIntType(pt), 0));
964 _ = try self.wip.ret(try o.builder.intValue(try o.errorIntType(), 0));
950965 } else {
951966 _ = try self.wip.retVoid();
952967 }
......@@ -961,7 +976,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !void {
961976 if (val_is_undef and safety) {
962977 const llvm_ret_ty = operand.typeOfWip(&self.wip);
963978 const rp = try self.buildAlloca(llvm_ret_ty, alignment);
964 const len = try o.builder.intValue(try o.lowerType(pt, Type.usize), ret_ty.abiSize(zcu));
979 const len = try o.builder.intValue(try self.lowerType(Type.usize), ret_ty.abiSize(zcu));
965980 _ = try self.wip.callMemSet(
966981 rp,
967982 alignment,
......@@ -997,7 +1012,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !void {
9971012 return;
9981013}
9991014
1000fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) !void {
1015fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
10011016 const o = self.object;
10021017 const pt = self.pt;
10031018 const zcu = pt.zcu;
......@@ -1011,7 +1026,7 @@ fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) !void {
10111026 // Functions with an empty error set are emitted with an error code
10121027 // return type and return zero so they can be function pointers coerced
10131028 // to functions that return anyerror.
1014 _ = try self.wip.ret(try o.builder.intValue(try o.errorIntType(pt), 0));
1029 _ = try self.wip.ret(try o.builder.intValue(try o.errorIntType(), 0));
10151030 } else {
10161031 _ = try self.wip.retVoid();
10171032 }
......@@ -1028,25 +1043,22 @@ fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) !void {
10281043 return;
10291044}
10301045
1031fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1032 const o = self.object;
1033 const pt = self.pt;
1046fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
10341047 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
10351048 const list = try self.resolveInst(ty_op.operand);
10361049 const arg_ty = ty_op.ty.toType();
1037 const llvm_arg_ty = try o.lowerType(pt, arg_ty);
1050 const llvm_arg_ty = try self.lowerType(arg_ty);
10381051
10391052 return self.wip.vaArg(list, llvm_arg_ty, "");
10401053}
10411054
1042fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1043 const o = self.object;
1055fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
10441056 const pt = self.pt;
10451057 const zcu = pt.zcu;
10461058 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
10471059 const src_list = try self.resolveInst(ty_op.operand);
10481060 const va_list_ty = ty_op.ty.toType();
1049 const llvm_va_list_ty = try o.lowerType(pt, va_list_ty);
1061 const llvm_va_list_ty = try self.lowerType(va_list_ty);
10501062
10511063 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();
10521064 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
......@@ -1058,7 +1070,7 @@ fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
10581070 try self.wip.load(.normal, llvm_va_list_ty, dest_list, result_alignment, "");
10591071}
10601072
1061fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1073fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
10621074 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
10631075 const src_list = try self.resolveInst(un_op);
10641076
......@@ -1066,12 +1078,11 @@ fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
10661078 return .none;
10671079}
10681080
1069fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1070 const o = self.object;
1081fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
10711082 const pt = self.pt;
10721083 const zcu = pt.zcu;
10731084 const va_list_ty = self.typeOfIndex(inst);
1074 const llvm_va_list_ty = try o.lowerType(pt, va_list_ty);
1085 const llvm_va_list_ty = try self.lowerType(va_list_ty);
10751086
10761087 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();
10771088 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
......@@ -1088,7 +1099,7 @@ fn airCmp(
10881099 inst: Air.Inst.Index,
10891100 op: math.CompareOperator,
10901101 fast: Builder.FastMathKind,
1091) !Builder.Value {
1102) Allocator.Error!Builder.Value {
10921103 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
10931104 const lhs = try self.resolveInst(bin_op.lhs);
10941105 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -1097,7 +1108,7 @@ fn airCmp(
10971108 return self.cmp(fast, op, operand_ty, lhs, rhs);
10981109}
10991110
1100fn airCmpVector(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
1111fn airCmpVector(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
11011112 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
11021113 const extra = self.air.extraData(Air.VectorCmp, ty_pl.payload).data;
11031114
......@@ -1109,21 +1120,20 @@ fn airCmpVector(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind
11091120 return self.cmp(fast, cmp_op, vec_ty, lhs, rhs);
11101121}
11111122
1112fn airCmpLtErrorsLen(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1123fn airCmpLtErrorsLen(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
11131124 const o = self.object;
1114 const pt = self.pt;
11151125 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
11161126 const operand = try self.resolveInst(un_op);
1117 const llvm_fn = try o.getCmpLtErrorsLenFunction(pt);
1118 return self.wip.call(
1127 const errors_len_ptr = try o.getErrorsLen();
1128 const errors_len_val = try self.wip.load(
11191129 .normal,
1120 .fastcc,
1121 .none,
1122 llvm_fn.typeOf(&o.builder),
1123 llvm_fn.toValue(&o.builder),
1124 &.{operand},
1130 try o.errorIntType(),
1131 errors_len_ptr.toValue(&o.builder),
1132 Type.errorAbiAlignment(o.zcu).toLlvm(),
11251133 "",
11261134 );
1135 // Despite the name, this instruction is actually lte. MLUGG TODO RENAME
1136 return self.wip.icmp(.ule, operand, errors_len_val, "");
11271137}
11281138
11291139fn cmp(
......@@ -1228,18 +1238,12 @@ fn cmp(
12281238 return self.wip.icmp(cond, lhs, rhs, "");
12291239}
12301240
1231fn airBlock(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1232 const block = self.air.unwrapBlock(inst);
1233 return self.lowerBlock(inst, null, block.body);
1234}
1235
12361241fn lowerBlock(
12371242 self: *FuncGen,
12381243 inst: Air.Inst.Index,
12391244 maybe_inline_func: ?InternPool.Index,
12401245 body: []const Air.Inst.Index,
1241) !Builder.Value {
1242 const o = self.object;
1246) TodoError!Builder.Value {
12431247 const pt = self.pt;
12441248 const zcu = pt.zcu;
12451249 const inst_ty = self.typeOfIndex(inst);
......@@ -1267,7 +1271,7 @@ fn lowerBlock(
12671271
12681272 // Create a phi node only if the block returns a value.
12691273 if (have_block_result) {
1270 const raw_llvm_ty = try o.lowerType(pt, inst_ty);
1274 const raw_llvm_ty = try self.lowerType(inst_ty);
12711275 const llvm_ty: Builder.Type = ty: {
12721276 // If the zig tag type is a function, this represents an actual function body; not
12731277 // a pointer to it. LLVM IR allows the call instruction to use function bodies instead
......@@ -1289,7 +1293,7 @@ fn lowerBlock(
12891293 }
12901294}
12911295
1292fn airBr(self: *FuncGen, inst: Air.Inst.Index) !void {
1296fn airBr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
12931297 const zcu = self.pt.zcu;
12941298 const branch = self.air.instructions.items(.data)[@intFromEnum(inst)].br;
12951299 const block = self.blocks.get(branch.block_inst).?;
......@@ -1306,7 +1310,7 @@ fn airBr(self: *FuncGen, inst: Air.Inst.Index) !void {
13061310 _ = try self.wip.br(block.parent_bb);
13071311}
13081312
1309fn airRepeat(self: *FuncGen, inst: Air.Inst.Index) !void {
1313fn airRepeat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
13101314 const repeat = self.air.instructions.items(.data)[@intFromEnum(inst)].repeat;
13111315 const loop_bb = self.loops.get(repeat.loop_inst).?;
13121316 loop_bb.ptr(&self.wip).incoming += 1;
......@@ -1318,7 +1322,7 @@ fn lowerSwitchDispatch(
13181322 switch_inst: Air.Inst.Index,
13191323 cond_ref: Air.Inst.Ref,
13201324 dispatch_info: SwitchDispatchInfo,
1321) !void {
1325) Allocator.Error!void {
13221326 const o = self.object;
13231327 const pt = self.pt;
13241328 const zcu = pt.zcu;
......@@ -1384,7 +1388,7 @@ fn lowerSwitchDispatch(
13841388 const table_index = try self.wip.conv(
13851389 .unsigned,
13861390 try self.wip.bin(.@"sub nuw", cond, jmp_table.min.toValue(), ""),
1387 try o.lowerType(pt, .usize),
1391 try self.lowerType(.usize),
13881392 "",
13891393 );
13901394 const target_ptr_ptr = try self.ptraddScaled(
......@@ -1409,7 +1413,7 @@ fn lowerSwitchDispatch(
14091413 // The switch prongs will correspond to our scalar cases. Ranges will
14101414 // be handled by conditional branches in the `else` prong.
14111415
1412 const llvm_usize = try o.lowerType(pt, Type.usize);
1416 const llvm_usize = try self.lowerType(Type.usize);
14131417 const cond_int = if (cond.typeOfWip(&self.wip).isPointer(&o.builder))
14141418 try self.wip.cast(.ptrtoint, cond, llvm_usize, "")
14151419 else
......@@ -1501,13 +1505,13 @@ fn lowerSwitchDispatch(
15011505 }
15021506}
15031507
1504fn airSwitchDispatch(self: *FuncGen, inst: Air.Inst.Index) !void {
1508fn airSwitchDispatch(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
15051509 const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br;
15061510 const dispatch_info = self.switch_dispatch_info.get(br.block_inst).?;
15071511 return self.lowerSwitchDispatch(br.block_inst, br.operand, dispatch_info);
15081512}
15091513
1510fn airCondBr(self: *FuncGen, inst: Air.Inst.Index) !void {
1514fn airCondBr(self: *FuncGen, inst: Air.Inst.Index) TodoError!void {
15111515 const cond_br = self.air.unwrapCondBr(inst);
15121516 const cond = try self.resolveInst(cond_br.condition);
15131517 const then_body = cond_br.then_body;
......@@ -1567,7 +1571,7 @@ fn airCondBr(self: *FuncGen, inst: Air.Inst.Index) !void {
15671571 // No need to reset the insert cursor since this instruction is noreturn.
15681572}
15691573
1570fn airTry(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) !Builder.Value {
1574fn airTry(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builder.Value {
15711575 const unwrapped_try = self.air.unwrapTry(inst);
15721576 const err_union = try self.resolveInst(unwrapped_try.error_union);
15731577 const body = unwrapped_try.else_body;
......@@ -1576,7 +1580,7 @@ fn airTry(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) !Builder.Value {
15761580 return lowerTry(self, err_union, body, err_union_ty, false, .none, is_unused, err_cold);
15771581}
15781582
1579fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) !Builder.Value {
1583fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builder.Value {
15801584 const zcu = self.pt.zcu;
15811585 const unwrapped_try = self.air.unwrapTryPtr(inst);
15821586 const err_union_ptr = try self.resolveInst(unwrapped_try.error_union_ptr);
......@@ -1599,13 +1603,13 @@ fn lowerTry(
15991603 operand_ptr_align: InternPool.Alignment,
16001604 is_unused: bool,
16011605 err_cold: bool,
1602) !Builder.Value {
1606) TodoError!Builder.Value {
16031607 const o = fg.object;
16041608 const pt = fg.pt;
16051609 const zcu = pt.zcu;
16061610 const payload_ty = err_union_ty.errorUnionPayload(zcu);
16071611 const payload_has_bits = payload_ty.hasRuntimeBits(zcu);
1608 const error_type = try o.errorIntType(pt);
1612 const error_type = try o.errorIntType();
16091613
16101614 const err_set_align: InternPool.Alignment, const payload_align: InternPool.Alignment = if (operand_is_ptr) .{
16111615 operand_ptr_align.minStrict(Type.anyerror.abiAlignment(zcu)),
......@@ -1657,11 +1661,11 @@ fn lowerTry(
16571661 } else if (isByRef(payload_ty, zcu)) {
16581662 return fg.loadByRef(payload_ptr, payload_ty, payload_align.toLlvm(), .normal);
16591663 } else {
1660 return fg.wip.load(.normal, try o.lowerType(pt, payload_ty), payload_ptr, payload_align.toLlvm(), "");
1664 return fg.wip.load(.normal, try fg.lowerType(payload_ty), payload_ptr, payload_align.toLlvm(), "");
16611665 }
16621666}
16631667
1664fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) !void {
1668fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) TodoError!void {
16651669 const o = self.object;
16661670 const pt = self.pt;
16671671 const zcu = pt.zcu;
......@@ -1909,7 +1913,7 @@ fn switchCaseItemRange(self: *FuncGen, switch_br: Air.UnwrappedSwitch) ?[2]Value
19091913 return .{ min.?, max.? };
19101914}
19111915
1912fn airLoop(self: *FuncGen, inst: Air.Inst.Index) !void {
1916fn airLoop(self: *FuncGen, inst: Air.Inst.Index) TodoError!void {
19131917 const block = self.air.unwrapBlock(inst);
19141918 const body = block.body;
19151919 const loop_block = try self.wip.block(1, "Loop"); // `airRepeat` will increment incoming each time
......@@ -1922,21 +1926,21 @@ fn airLoop(self: *FuncGen, inst: Air.Inst.Index) !void {
19221926 try self.genBodyDebugScope(null, body, .none);
19231927}
19241928
1925fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1929fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
19261930 const o = self.object;
19271931 const pt = self.pt;
19281932 const zcu = pt.zcu;
19291933 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
19301934 const operand_ty = self.typeOf(ty_op.operand);
19311935 const array_ty = operand_ty.childType(zcu);
1932 const llvm_usize = try o.lowerType(pt, Type.usize);
1936 const llvm_usize = try self.lowerType(.usize);
19331937 const len = try o.builder.intValue(llvm_usize, array_ty.arrayLen(zcu));
1934 const slice_llvm_ty = try o.lowerType(pt, self.typeOfIndex(inst));
1938 const slice_llvm_ty = try self.lowerType(self.typeOfIndex(inst));
19351939 const operand = try self.resolveInst(ty_op.operand);
19361940 return self.wip.buildAggregate(slice_llvm_ty, &.{ operand, len }, "");
19371941}
19381942
1939fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
1943fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
19401944 const o = self.object;
19411945 const pt = self.pt;
19421946 const zcu = pt.zcu;
......@@ -1949,7 +1953,7 @@ fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
19491953
19501954 const dest_ty = self.typeOfIndex(inst);
19511955 const dest_scalar_ty = dest_ty.scalarType(zcu);
1952 const dest_llvm_ty = try o.lowerType(pt, dest_ty);
1956 const dest_llvm_ty = try self.lowerType(dest_ty);
19531957 const target = zcu.getTarget();
19541958
19551959 if (intrinsicsAllowed(dest_scalar_ty, target)) return self.wip.conv(
......@@ -1987,7 +1991,7 @@ fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
19871991 extended = try self.wip.cast(.bitcast, extended, param_type, "");
19881992 }
19891993
1990 const libc_fn = try self.getLibcFunction(fn_name, &.{param_type}, dest_llvm_ty);
1994 const libc_fn = try o.getLibcFunction(fn_name, &.{param_type}, dest_llvm_ty);
19911995 return self.wip.call(
19921996 .normal,
19931997 .ccc,
......@@ -2003,7 +2007,7 @@ fn airIntFromFloat(
20032007 self: *FuncGen,
20042008 inst: Air.Inst.Index,
20052009 fast: Builder.FastMathKind,
2006) !Builder.Value {
2010) TodoError!Builder.Value {
20072011 _ = fast;
20082012
20092013 const o = self.object;
......@@ -2018,7 +2022,7 @@ fn airIntFromFloat(
20182022
20192023 const dest_ty = self.typeOfIndex(inst);
20202024 const dest_scalar_ty = dest_ty.scalarType(zcu);
2021 const dest_llvm_ty = try o.lowerType(pt, dest_ty);
2025 const dest_llvm_ty = try self.lowerType(dest_ty);
20222026
20232027 if (intrinsicsAllowed(operand_scalar_ty, target)) {
20242028 // TODO set fast math flag
......@@ -2052,8 +2056,8 @@ fn airIntFromFloat(
20522056 compiler_rt_dest_abbrev,
20532057 });
20542058
2055 const operand_llvm_ty = try o.lowerType(pt, operand_ty);
2056 const libc_fn = try self.getLibcFunction(fn_name, &.{operand_llvm_ty}, libc_ret_ty);
2059 const operand_llvm_ty = try self.lowerType(operand_ty);
2060 const libc_fn = try o.getLibcFunction(fn_name, &.{operand_llvm_ty}, libc_ret_ty);
20572061 var result = try self.wip.call(
20582062 .normal,
20592063 .ccc,
......@@ -2078,7 +2082,7 @@ fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.
20782082 const o = fg.object;
20792083 const pt = fg.pt;
20802084 const zcu = pt.zcu;
2081 const llvm_usize = try o.lowerType(pt, Type.usize);
2085 const llvm_usize = try fg.lowerType(.usize);
20822086 switch (ty.ptrSize(zcu)) {
20832087 .slice => {
20842088 const len = try fg.wip.extractValue(ptr, &.{1}, "");
......@@ -2098,20 +2102,20 @@ fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.
20982102 }
20992103}
21002104
2101fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: u32) !Builder.Value {
2105fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: u32) Allocator.Error!Builder.Value {
21022106 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
21032107 const operand = try self.resolveInst(ty_op.operand);
21042108 return self.wip.extractValue(operand, &.{index}, "");
21052109}
21062110
2107fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: u1) !Builder.Value {
2111fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: u1) Allocator.Error!Builder.Value {
21082112 const zcu = self.pt.zcu;
21092113 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
21102114 const slice_ptr = try self.resolveInst(ty_op.operand);
21112115 return self.ptraddConst(slice_ptr, index * Type.usize.abiSize(zcu));
21122116}
21132117
2114fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2118fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
21152119 const pt = self.pt;
21162120 const zcu = pt.zcu;
21172121 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -2133,7 +2137,7 @@ fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
21332137 }
21342138}
21352139
2136fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2140fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
21372141 const pt = self.pt;
21382142 const zcu = pt.zcu;
21392143 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
......@@ -2146,7 +2150,7 @@ fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
21462150 return self.ptraddScaled(base_ptr, index, slice_ty.childType(zcu).abiSize(zcu));
21472151}
21482152
2149fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2153fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
21502154 const pt = self.pt;
21512155 const zcu = pt.zcu;
21522156
......@@ -2169,7 +2173,7 @@ fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
21692173 return self.wip.extractElement(array_llvm_val, rhs, "");
21702174}
21712175
2172fn airPtrElemVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2176fn airPtrElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
21732177 const pt = self.pt;
21742178 const zcu = pt.zcu;
21752179 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -2189,7 +2193,7 @@ fn airPtrElemVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
21892193 return self.load(ptr, ptr_ty);
21902194}
21912195
2192fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2196fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
21932197 const pt = self.pt;
21942198 const zcu = pt.zcu;
21952199 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
......@@ -2207,7 +2211,7 @@ fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
22072211 return self.ptraddScaled(base_ptr, rhs, elem_ty.abiSize(zcu));
22082212}
22092213
2210fn airStructFieldPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2214fn airStructFieldPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
22112215 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
22122216 const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data;
22132217 const struct_ptr = try self.resolveInst(struct_field.struct_operand);
......@@ -2219,14 +2223,14 @@ fn airStructFieldPtrIndex(
22192223 self: *FuncGen,
22202224 inst: Air.Inst.Index,
22212225 field_index: u32,
2222) !Builder.Value {
2226) Allocator.Error!Builder.Value {
22232227 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
22242228 const struct_ptr = try self.resolveInst(ty_op.operand);
22252229 const struct_ptr_ty = self.typeOf(ty_op.operand);
22262230 return self.fieldPtr(struct_ptr, struct_ptr_ty, field_index);
22272231}
22282232
2229fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2233fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
22302234 const o = self.object;
22312235 const pt = self.pt;
22322236 const zcu = pt.zcu;
......@@ -2267,7 +2271,7 @@ fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
22672271 },
22682272 .float => {
22692273 // bitcast int->float
2270 return self.wip.cast(.bitcast, field_int_val, try o.lowerType(pt, field_ty), "");
2274 return self.wip.cast(.bitcast, field_int_val, try self.lowerType(field_ty), "");
22712275 },
22722276 }
22732277 }
......@@ -2292,7 +2296,7 @@ fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
22922296 }
22932297}
22942298
2295fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2299fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
22962300 const o = self.object;
22972301 const pt = self.pt;
22982302 const zcu = pt.zcu;
......@@ -2305,8 +2309,8 @@ fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
23052309 const field_offset = parent_ty.structFieldOffset(extra.field_index, zcu);
23062310 if (field_offset == 0) return field_ptr;
23072311
2308 const res_ty = try o.lowerType(pt, ty_pl.ty.toType());
2309 const llvm_usize = try o.lowerType(pt, Type.usize);
2312 const res_ty = try self.lowerType(ty_pl.ty.toType());
2313 const llvm_usize = try self.lowerType(Type.usize);
23102314
23112315 const field_ptr_int = try self.wip.cast(.ptrtoint, field_ptr, llvm_usize, "");
23122316 const base_ptr_int = try self.wip.bin(
......@@ -2318,19 +2322,19 @@ fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
23182322 return self.wip.cast(.inttoptr, base_ptr_int, res_ty, "");
23192323}
23202324
2321fn airNot(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2325fn airNot(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
23222326 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
23232327 const operand = try self.resolveInst(ty_op.operand);
23242328
23252329 return self.wip.not(operand, "");
23262330}
23272331
2328fn airUnreach(self: *FuncGen, inst: Air.Inst.Index) !void {
2332fn airUnreach(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
23292333 _ = inst;
23302334 _ = try self.wip.@"unreachable"();
23312335}
23322336
2333fn airDbgStmt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2337fn airDbgStmt(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
23342338 const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
23352339 self.prev_dbg_line = @intCast(self.base_line + dbg_stmt.line + 1);
23362340 self.prev_dbg_column = @intCast(dbg_stmt.column + 1);
......@@ -2345,19 +2349,13 @@ fn airDbgStmt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
23452349 return .none;
23462350}
23472351
2348fn airDbgEmptyStmt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2352fn airDbgEmptyStmt(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
23492353 _ = self;
23502354 _ = inst;
23512355 return .none;
23522356}
23532357
2354fn airDbgInlineBlock(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2355 const block = self.air.unwrapDbgBlock(inst);
2356 self.arg_inline_index = 0;
2357 return self.lowerBlock(inst, block.func, block.body);
2358}
2359
2360fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2358fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
23612359 const o = self.object;
23622360 const pt = self.pt;
23632361 const zcu = pt.zcu;
......@@ -2390,7 +2388,7 @@ fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
23902388 return .none;
23912389}
23922390
2393fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index, is_arg: bool) !Builder.Value {
2391fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index, is_arg: bool) Allocator.Error!Builder.Value {
23942392 const o = self.object;
23952393 const pt = self.pt;
23962394 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
......@@ -2468,7 +2466,7 @@ fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index, is_arg: bool) !Builder.Val
24682466 return .none;
24692467}
24702468
2471fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2469fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
24722470 // Eventually, the Zig compiler needs to be reworked to have inline
24732471 // assembly go through the same parsing code regardless of backend, and
24742472 // have LLVM-flavored inline assembly be *output* from that assembler.
......@@ -2530,7 +2528,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
25302528 const output_inst = try self.resolveInst(output.operand);
25312529 const output_ty = self.typeOf(output.operand);
25322530 assert(output_ty.zigTypeTag(zcu) == .pointer);
2533 const elem_llvm_ty = try o.lowerType(pt, output_ty.childType(zcu));
2531 const elem_llvm_ty = try self.lowerType(output_ty.childType(zcu));
25342532
25352533 switch (constraint[0]) {
25362534 '=' => {},
......@@ -2568,7 +2566,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
25682566 llvm_ret_indirect[output.index] = false;
25692567
25702568 const ret_ty = self.typeOfIndex(inst);
2571 llvm_ret_types[llvm_ret_i] = try o.lowerType(pt, ret_ty);
2569 llvm_ret_types[llvm_ret_i] = try self.lowerType(ret_ty);
25722570 llvm_ret_i += 1;
25732571 }
25742572
......@@ -2607,7 +2605,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
26072605 llvm_param_types[llvm_param_i] = arg_llvm_value.typeOfWip(&self.wip);
26082606 } else {
26092607 const alignment = arg_ty.abiAlignment(zcu).toLlvm();
2610 const arg_llvm_ty = try o.lowerType(pt, arg_ty);
2608 const arg_llvm_ty = try self.lowerType(arg_ty);
26112609 const load_inst =
26122610 try self.wip.load(.normal, arg_llvm_ty, arg_llvm_value, alignment, "");
26132611 llvm_param_values[llvm_param_i] = load_inst;
......@@ -2648,7 +2646,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
26482646 llvm_param_attrs[llvm_param_i] = if (constraint[0] == '*') blk: {
26492647 if (!is_by_ref) self.maybeMarkAllowZeroAccess(arg_ty.ptrInfo(zcu));
26502648
2651 break :blk try o.lowerType(pt, if (is_by_ref) arg_ty else arg_ty.childType(zcu));
2649 break :blk try self.lowerType(if (is_by_ref) arg_ty else arg_ty.childType(zcu));
26522650 } else .none;
26532651
26542652 llvm_param_i += 1;
......@@ -2662,7 +2660,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
26622660 if (constraint[0] != '+') continue;
26632661
26642662 const rw_ty = self.typeOf(output.operand);
2665 const llvm_elem_ty = try o.lowerType(pt, rw_ty.childType(zcu));
2663 const llvm_elem_ty = try self.lowerType(rw_ty.childType(zcu));
26662664 if (llvm_ret_indirect[output.index]) {
26672665 llvm_param_values[llvm_param_i] = llvm_rw_vals[output.index];
26682666 llvm_param_types[llvm_param_i] = llvm_rw_vals[output.index].typeOfWip(&self.wip);
......@@ -2855,7 +2853,7 @@ fn airIsNonNull(
28552853 inst: Air.Inst.Index,
28562854 operand_is_ptr: bool,
28572855 cond: Builder.IntegerCondition,
2858) !Builder.Value {
2856) Allocator.Error!Builder.Value {
28592857 const o = self.object;
28602858 const pt = self.pt;
28612859 const zcu = pt.zcu;
......@@ -2863,7 +2861,7 @@ fn airIsNonNull(
28632861 const operand = try self.resolveInst(un_op);
28642862 const operand_ty = self.typeOf(un_op);
28652863 const optional_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty;
2866 const optional_llvm_ty = try o.lowerType(pt, optional_ty);
2864 const optional_llvm_ty = try self.lowerType(optional_ty);
28672865 const payload_ty = optional_ty.optionalChild(zcu);
28682866
28692867 const access_kind: Builder.MemoryAccessKind =
......@@ -2905,7 +2903,7 @@ fn airIsErr(
29052903 inst: Air.Inst.Index,
29062904 cond: Builder.IntegerCondition,
29072905 operand_is_ptr: bool,
2908) !Builder.Value {
2906) Allocator.Error!Builder.Value {
29092907 const o = self.object;
29102908 const pt = self.pt;
29112909 const zcu = pt.zcu;
......@@ -2914,7 +2912,7 @@ fn airIsErr(
29142912 const operand_ty = self.typeOf(un_op);
29152913 const err_union_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty;
29162914 const payload_ty = err_union_ty.errorUnionPayload(zcu);
2917 const error_type = try o.errorIntType(pt);
2915 const error_type = try o.errorIntType();
29182916 const zero = try o.builder.intValue(error_type, 0);
29192917
29202918 const access_kind: Builder.MemoryAccessKind =
......@@ -2933,7 +2931,7 @@ fn airIsErr(
29332931
29342932 if (!payload_ty.hasRuntimeBits(zcu)) {
29352933 const loaded = if (operand_is_ptr)
2936 try self.wip.load(access_kind, try o.lowerType(pt, err_union_ty), operand, operand_ty.ptrAlignment(zcu).toLlvm(), "")
2934 try self.wip.load(access_kind, try self.lowerType(err_union_ty), operand, operand_ty.ptrAlignment(zcu).toLlvm(), "")
29372935 else
29382936 operand;
29392937 return self.wip.icmp(cond, loaded, zero, "");
......@@ -2957,7 +2955,7 @@ fn airOptionalPayloadPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!B
29572955 return operand;
29582956}
29592957
2960fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
2958fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
29612959 comptime assert(optional_layout_version == 3);
29622960
29632961 const o = self.object;
......@@ -3002,7 +3000,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value
30023000 return operand; // payload is at offset 0
30033001}
30043002
3005fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3003fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
30063004 const pt = self.pt;
30073005 const zcu = pt.zcu;
30083006 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
......@@ -3019,8 +3017,7 @@ fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
30193017 return self.optPayloadHandle(operand, optional_ty, false);
30203018}
30213019
3022fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool) !Builder.Value {
3023 const o = self.object;
3020fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool) Allocator.Error!Builder.Value {
30243021 const pt = self.pt;
30253022 const zcu = pt.zcu;
30263023 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
......@@ -3042,7 +3039,7 @@ fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool
30423039 if (isByRef(payload_ty, zcu)) {
30433040 return self.loadByRef(payload_ptr, payload_ty, payload_alignment, .normal);
30443041 } else {
3045 const payload_llvm_ty = try o.lowerType(pt, payload_ty);
3042 const payload_llvm_ty = try self.lowerType(payload_ty);
30463043 return self.wip.load(.normal, payload_llvm_ty, payload_ptr, payload_alignment, "");
30473044 }
30483045}
......@@ -3051,14 +3048,14 @@ fn airErrUnionErr(
30513048 self: *FuncGen,
30523049 inst: Air.Inst.Index,
30533050 operand_is_ptr: bool,
3054) !Builder.Value {
3051) Allocator.Error!Builder.Value {
30553052 const o = self.object;
30563053 const pt = self.pt;
30573054 const zcu = pt.zcu;
30583055 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
30593056 const operand = try self.resolveInst(ty_op.operand);
30603057 const operand_ty = self.typeOf(ty_op.operand);
3061 const error_type = try o.errorIntType(pt);
3058 const error_type = try o.errorIntType();
30623059 const err_union_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty;
30633060 if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
30643061 if (operand_is_ptr) {
......@@ -3094,7 +3091,7 @@ fn airErrUnionErr(
30943091 return self.wip.load(access_kind, error_type, err_field_ptr, err_align.toLlvm(), "");
30953092}
30963093
3097fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3094fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
30983095 const o = self.object;
30993096 const pt = self.pt;
31003097 const zcu = pt.zcu;
......@@ -3105,7 +3102,7 @@ fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value
31053102 const err_union_ptr_align = err_union_ptr_ty.ptrAlignment(zcu);
31063103
31073104 const payload_ty = err_union_ty.errorUnionPayload(zcu);
3108 const non_error_val = try o.builder.intValue(try o.errorIntType(pt), 0);
3105 const non_error_val = try o.builder.intValue(try o.errorIntType(), 0);
31093106
31103107 const access_kind: Builder.MemoryAccessKind =
31113108 if (err_union_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
......@@ -3124,18 +3121,18 @@ fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value
31243121 return self.ptraddConst(operand, codegen.errUnionPayloadOffset(payload_ty, zcu));
31253122}
31263123
3127fn airErrReturnTrace(self: *FuncGen, _: Air.Inst.Index) !Builder.Value {
3124fn airErrReturnTrace(self: *FuncGen, _: Air.Inst.Index) Allocator.Error!Builder.Value {
31283125 assert(self.err_ret_trace != .none);
31293126 return self.err_ret_trace;
31303127}
31313128
3132fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3129fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
31333130 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
31343131 self.err_ret_trace = try self.resolveInst(un_op);
31353132 return .none;
31363133}
31373134
3138fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3135fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
31393136 const pt = self.pt;
31403137 const zcu = pt.zcu;
31413138
......@@ -3184,7 +3181,7 @@ fn isNextRet(
31843181 return false;
31853182}
31863183
3187fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value {
3184fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
31883185 const o = self.object;
31893186 const pt = self.pt;
31903187 const zcu = pt.zcu;
......@@ -3198,7 +3195,7 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.V
31983195 const optional_ty = self.typeOfIndex(inst);
31993196 if (optional_ty.optionalReprIsPayload(zcu)) return operand;
32003197 assert(isByRef(optional_ty, zcu)); // optionals with runtime bits are by-ref unless `optionalReprIsPayload`
3201 const llvm_optional_ty = try o.lowerType(pt, optional_ty);
3198 const llvm_optional_ty = try self.lowerType(optional_ty);
32023199 const optional_ptr = if (self.isNextRet(body_tail))
32033200 self.ret_ptr
32043201 else brk: {
......@@ -3216,7 +3213,7 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.V
32163213 return optional_ptr;
32173214}
32183215
3219fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value {
3216fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
32203217 const o = self.object;
32213218 const pt = self.pt;
32223219 const zcu = pt.zcu;
......@@ -3227,8 +3224,8 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !Bu
32273224 const payload_ty = self.typeOf(ty_op.operand);
32283225 assert(payload_ty.hasRuntimeBits(zcu));
32293226 assert(isByRef(err_un_ty, zcu)); // error unions with runtime bits are always by-ref
3230 const ok_err_code = try o.builder.intValue(try o.errorIntType(pt), 0);
3231 const err_un_llvm_ty = try o.lowerType(pt, err_un_ty);
3227 const ok_err_code = try o.builder.intValue(try o.errorIntType(), 0);
3228 const err_un_llvm_ty = try self.lowerType(err_un_ty);
32323229
32333230 const result_ptr = if (self.isNextRet(body_tail))
32343231 self.ret_ptr
......@@ -3247,8 +3244,7 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !Bu
32473244 return result_ptr;
32483245}
32493246
3250fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value {
3251 const o = self.object;
3247fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
32523248 const pt = self.pt;
32533249 const zcu = pt.zcu;
32543250 const inst = body_tail[0];
......@@ -3258,7 +3254,7 @@ fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builde
32583254 const operand = try self.resolveInst(ty_op.operand);
32593255 if (!payload_ty.hasRuntimeBits(zcu)) return operand;
32603256 assert(isByRef(err_un_ty, zcu)); // error unions with runtime bits are always by-ref
3261 const err_un_llvm_ty = try o.lowerType(pt, err_un_ty);
3257 const err_un_llvm_ty = try self.lowerType(err_un_ty);
32623258
32633259 const result_ptr = if (self.isNextRet(body_tail))
32643260 self.ret_ptr
......@@ -3279,29 +3275,27 @@ fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builde
32793275 return result_ptr;
32803276}
32813277
3282fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3278fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
32833279 const o = self.object;
3284 const pt = self.pt;
32853280 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
32863281 const index = pl_op.payload;
3287 const llvm_usize = try o.lowerType(pt, Type.usize);
3282 const llvm_usize = try self.lowerType(Type.usize);
32883283 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{llvm_usize}, &.{
32893284 try o.builder.intValue(.i32, index),
32903285 }, "");
32913286}
32923287
3293fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3288fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
32943289 const o = self.object;
3295 const pt = self.pt;
32963290 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
32973291 const index = pl_op.payload;
3298 const llvm_isize = try o.lowerType(pt, Type.isize);
3292 const llvm_isize = try self.lowerType(Type.isize);
32993293 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{llvm_isize}, &.{
33003294 try o.builder.intValue(.i32, index), try self.resolveInst(pl_op.operand),
33013295 }, "");
33023296}
33033297
3304fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3298fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
33053299 const o = fg.object;
33063300 const pt = fg.pt;
33073301 const ty_nav = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;
......@@ -3309,8 +3303,7 @@ fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
33093303 return llvm_ptr_const.toValue();
33103304}
33113305
3312fn airMin(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3313 const o = self.object;
3306fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
33143307 const pt = self.pt;
33153308 const zcu = pt.zcu;
33163309 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -3324,14 +3317,13 @@ fn airMin(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
33243317 .normal,
33253318 .none,
33263319 if (scalar_ty.isSignedInt(zcu)) .smin else .umin,
3327 &.{try o.lowerType(pt, inst_ty)},
3320 &.{try self.lowerType(inst_ty)},
33283321 &.{ lhs, rhs },
33293322 "",
33303323 );
33313324}
33323325
3333fn airMax(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3334 const o = self.object;
3326fn airMax(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
33353327 const pt = self.pt;
33363328 const zcu = pt.zcu;
33373329 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -3345,24 +3337,22 @@ fn airMax(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
33453337 .normal,
33463338 .none,
33473339 if (scalar_ty.isSignedInt(zcu)) .smax else .umax,
3348 &.{try o.lowerType(pt, inst_ty)},
3340 &.{try self.lowerType(inst_ty)},
33493341 &.{ lhs, rhs },
33503342 "",
33513343 );
33523344}
33533345
3354fn airSlice(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3355 const o = self.object;
3356 const pt = self.pt;
3346fn airSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
33573347 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
33583348 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
33593349 const ptr = try self.resolveInst(bin_op.lhs);
33603350 const len = try self.resolveInst(bin_op.rhs);
33613351 const inst_ty = self.typeOfIndex(inst);
3362 return self.wip.buildAggregate(try o.lowerType(pt, inst_ty), &.{ ptr, len }, "");
3352 return self.wip.buildAggregate(try self.lowerType(inst_ty), &.{ ptr, len }, "");
33633353}
33643354
3365fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3355fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
33663356 const zcu = self.pt.zcu;
33673357 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
33683358 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -3379,7 +3369,7 @@ fn airSafeArithmetic(
33793369 inst: Air.Inst.Index,
33803370 signed_intrinsic: Builder.Intrinsic,
33813371 unsigned_intrinsic: Builder.Intrinsic,
3382) !Builder.Value {
3372) Allocator.Error!Builder.Value {
33833373 const o = fg.object;
33843374 const pt = fg.pt;
33853375 const zcu = pt.zcu;
......@@ -3391,7 +3381,7 @@ fn airSafeArithmetic(
33913381 const scalar_ty = inst_ty.scalarType(zcu);
33923382
33933383 const intrinsic = if (scalar_ty.isSignedInt(zcu)) signed_intrinsic else unsigned_intrinsic;
3394 const llvm_inst_ty = try o.lowerType(pt, inst_ty);
3384 const llvm_inst_ty = try fg.lowerType(inst_ty);
33953385 const results =
33963386 try fg.wip.callIntrinsic(.normal, .none, intrinsic, &.{llvm_inst_ty}, &.{ lhs, rhs }, "");
33973387
......@@ -3420,7 +3410,7 @@ fn airSafeArithmetic(
34203410 return fg.wip.extractValue(results, &.{0}, "");
34213411}
34223412
3423fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3413fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
34243414 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34253415 const lhs = try self.resolveInst(bin_op.lhs);
34263416 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3428,8 +3418,7 @@ fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
34283418 return self.wip.bin(.add, lhs, rhs, "");
34293419}
34303420
3431fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3432 const o = self.object;
3421fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
34333422 const pt = self.pt;
34343423 const zcu = pt.zcu;
34353424 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -3442,13 +3431,13 @@ fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
34423431 .normal,
34433432 .none,
34443433 if (scalar_ty.isSignedInt(zcu)) .@"sadd.sat" else .@"uadd.sat",
3445 &.{try o.lowerType(pt, inst_ty)},
3434 &.{try self.lowerType(inst_ty)},
34463435 &.{ lhs, rhs },
34473436 "",
34483437 );
34493438}
34503439
3451fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3440fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
34523441 const zcu = self.pt.zcu;
34533442 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34543443 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -3460,7 +3449,7 @@ fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Bui
34603449 return self.wip.bin(if (scalar_ty.isSignedInt(zcu)) .@"sub nsw" else .@"sub nuw", lhs, rhs, "");
34613450}
34623451
3463fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3452fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
34643453 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34653454 const lhs = try self.resolveInst(bin_op.lhs);
34663455 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3468,8 +3457,7 @@ fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
34683457 return self.wip.bin(.sub, lhs, rhs, "");
34693458}
34703459
3471fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3472 const o = self.object;
3460fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
34733461 const pt = self.pt;
34743462 const zcu = pt.zcu;
34753463 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -3482,13 +3470,13 @@ fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
34823470 .normal,
34833471 .none,
34843472 if (scalar_ty.isSignedInt(zcu)) .@"ssub.sat" else .@"usub.sat",
3485 &.{try o.lowerType(pt, inst_ty)},
3473 &.{try self.lowerType(inst_ty)},
34863474 &.{ lhs, rhs },
34873475 "",
34883476 );
34893477}
34903478
3491fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3479fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
34923480 const zcu = self.pt.zcu;
34933481 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
34943482 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -3500,7 +3488,7 @@ fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Bui
35003488 return self.wip.bin(if (scalar_ty.isSignedInt(zcu)) .@"mul nsw" else .@"mul nuw", lhs, rhs, "");
35013489}
35023490
3503fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3491fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
35043492 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35053493 const lhs = try self.resolveInst(bin_op.lhs);
35063494 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3508,8 +3496,7 @@ fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
35083496 return self.wip.bin(.mul, lhs, rhs, "");
35093497}
35103498
3511fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3512 const o = self.object;
3499fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
35133500 const pt = self.pt;
35143501 const zcu = pt.zcu;
35153502 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -3522,13 +3509,13 @@ fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
35223509 .normal,
35233510 .none,
35243511 if (scalar_ty.isSignedInt(zcu)) .@"smul.fix.sat" else .@"umul.fix.sat",
3525 &.{try o.lowerType(pt, inst_ty)},
3512 &.{try self.lowerType(inst_ty)},
35263513 &.{ lhs, rhs, .@"0" },
35273514 "",
35283515 );
35293516}
35303517
3531fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3518fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
35323519 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35333520 const lhs = try self.resolveInst(bin_op.lhs);
35343521 const rhs = try self.resolveInst(bin_op.rhs);
......@@ -3537,7 +3524,7 @@ fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35373524 return self.buildFloatOp(.div, fast, inst_ty, 2, .{ lhs, rhs });
35383525}
35393526
3540fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3527fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
35413528 const zcu = self.pt.zcu;
35423529 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
35433530 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -3552,7 +3539,7 @@ fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35523539 return self.wip.bin(if (scalar_ty.isSignedInt(zcu)) .sdiv else .udiv, lhs, rhs, "");
35533540}
35543541
3555fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3542fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
35563543 const o = self.object;
35573544 const pt = self.pt;
35583545 const zcu = pt.zcu;
......@@ -3567,7 +3554,7 @@ fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35673554 return self.buildFloatOp(.floor, fast, inst_ty, 1, .{result});
35683555 }
35693556 if (scalar_ty.isSignedInt(zcu)) {
3570 const inst_llvm_ty = try o.lowerType(pt, inst_ty);
3557 const inst_llvm_ty = try self.lowerType(inst_ty);
35713558
35723559 const ExpectedContents = [std.math.big.int.calcTwosCompLimbCount(256)]std.math.big.Limb;
35733560 var stack align(@max(
......@@ -3603,7 +3590,7 @@ fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
36033590 return self.wip.bin(.udiv, lhs, rhs, "");
36043591}
36053592
3606fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3593fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
36073594 const zcu = self.pt.zcu;
36083595 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
36093596 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -3620,7 +3607,7 @@ fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
36203607 );
36213608}
36223609
3623fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3610fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
36243611 const zcu = self.pt.zcu;
36253612 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
36263613 const lhs = try self.resolveInst(bin_op.lhs);
......@@ -3636,7 +3623,7 @@ fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Bui
36363623 .urem, lhs, rhs, "");
36373624}
36383625
3639fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
3626fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
36403627 const o = self.object;
36413628 const pt = self.pt;
36423629 const zcu = pt.zcu;
......@@ -3644,7 +3631,7 @@ fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Bui
36443631 const lhs = try self.resolveInst(bin_op.lhs);
36453632 const rhs = try self.resolveInst(bin_op.rhs);
36463633 const inst_ty = self.typeOfIndex(inst);
3647 const inst_llvm_ty = try o.lowerType(pt, inst_ty);
3634 const inst_llvm_ty = try self.lowerType(inst_ty);
36483635 const scalar_ty = inst_ty.scalarType(zcu);
36493636
36503637 if (scalar_ty.isRuntimeFloat()) {
......@@ -3690,7 +3677,7 @@ fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Bui
36903677 return self.wip.bin(.urem, lhs, rhs, "");
36913678}
36923679
3693fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3680fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
36943681 const zcu = self.pt.zcu;
36953682 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
36963683 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
......@@ -3705,14 +3692,14 @@ fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
37053692 return self.ptraddScaled(ptr, index, elem_ty.abiSize(zcu));
37063693}
37073694
3708fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
3695fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
37093696 const o = self.object;
37103697 const pt = self.pt;
37113698 const zcu = pt.zcu;
37123699 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
37133700 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
37143701 const ptr_or_slice = try self.resolveInst(bin_op.lhs);
3715 const llvm_usize_ty = try o.lowerType(pt, .usize);
3702 const llvm_usize_ty = try self.lowerType(.usize);
37163703 const ptr_ty = self.typeOf(bin_op.lhs);
37173704 const elem_ty = ptr_ty.indexableElem(zcu);
37183705 const ptr = switch (ptr_ty.ptrSize(zcu)) {
......@@ -3722,7 +3709,7 @@ fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
37223709 const scale_val = try o.builder.intValue(llvm_usize_ty, -@as(i65, elem_ty.abiSize(zcu)));
37233710 const positive_index = try self.resolveInst(bin_op.rhs);
37243711 const negative_offset = try self.wip.bin(.@"mul nsw", positive_index, scale_val, "");
3725 return self.ptradd(ptr, negative_offset);
3712 return self.wip.gep(.inbounds, .i8, ptr, &.{negative_offset}, "");
37263713}
37273714
37283715fn airOverflow(
......@@ -3730,8 +3717,7 @@ fn airOverflow(
37303717 inst: Air.Inst.Index,
37313718 signed_intrinsic: Builder.Intrinsic,
37323719 unsigned_intrinsic: Builder.Intrinsic,
3733) !Builder.Value {
3734 const o = self.object;
3720) Allocator.Error!Builder.Value {
37353721 const pt = self.pt;
37363722 const zcu = pt.zcu;
37373723 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
......@@ -3746,8 +3732,8 @@ fn airOverflow(
37463732 assert(isByRef(inst_ty, zcu)); // auto structs are by-ref
37473733
37483734 const intrinsic = if (scalar_ty.isSignedInt(zcu)) signed_intrinsic else unsigned_intrinsic;
3749 const llvm_inst_ty = try o.lowerType(pt, inst_ty);
3750 const llvm_lhs_ty = try o.lowerType(pt, lhs_ty);
3735 const llvm_inst_ty = try self.lowerType(inst_ty);
3736 const llvm_lhs_ty = try self.lowerType(lhs_ty);
37513737 const results =
37523738 try self.wip.callIntrinsic(.normal, .none, intrinsic, &.{llvm_lhs_ty}, &.{ lhs, rhs }, "");
37533739
......@@ -3778,7 +3764,7 @@ fn buildElementwiseCall(
37783764 args_vectors: []const Builder.Value,
37793765 result_vector: Builder.Value,
37803766 vector_len: usize,
3781) !Builder.Value {
3767) Allocator.Error!Builder.Value {
37823768 const o = self.object;
37833769 assert(args_vectors.len <= 3);
37843770
......@@ -3805,25 +3791,6 @@ fn buildElementwiseCall(
38053791 return result;
38063792}
38073793
3808fn getLibcFunction(
3809 self: *FuncGen,
3810 fn_name: Builder.StrtabString,
3811 param_types: []const Builder.Type,
3812 return_type: Builder.Type,
3813) Allocator.Error!Builder.Function.Index {
3814 const o = self.object;
3815 if (o.builder.getGlobal(fn_name)) |global| return switch (global.ptrConst(&o.builder).kind) {
3816 .alias => |alias| alias.getAliasee(&o.builder).ptrConst(&o.builder).kind.function,
3817 .function => |function| function,
3818 .variable, .replaced => unreachable,
3819 };
3820 return o.builder.addFunction(
3821 try o.builder.fnType(return_type, param_types, .normal),
3822 fn_name,
3823 toLlvmAddressSpace(.generic, self.pt.zcu.getTarget()),
3824 );
3825}
3826
38273794/// Creates a floating point comparison by lowering to the appropriate
38283795/// hardware instruction or softfloat routine for the target
38293796fn buildFloatCmp(
......@@ -3832,13 +3799,13 @@ fn buildFloatCmp(
38323799 pred: math.CompareOperator,
38333800 ty: Type,
38343801 params: [2]Builder.Value,
3835) !Builder.Value {
3802) Allocator.Error!Builder.Value {
38363803 const o = self.object;
38373804 const pt = self.pt;
38383805 const zcu = pt.zcu;
38393806 const target = zcu.getTarget();
38403807 const scalar_ty = ty.scalarType(zcu);
3841 const scalar_llvm_ty = try o.lowerType(pt, scalar_ty);
3808 const scalar_llvm_ty = try self.lowerType(scalar_ty);
38423809
38433810 if (intrinsicsAllowed(scalar_ty, target)) {
38443811 const cond: Builder.FloatCondition = switch (pred) {
......@@ -3864,7 +3831,7 @@ fn buildFloatCmp(
38643831 };
38653832 const fn_name = try o.builder.strtabStringFmt("__{s}{s}f2", .{ fn_base_name, compiler_rt_float_abbrev });
38663833
3867 const libc_fn = try self.getLibcFunction(fn_name, &.{ scalar_llvm_ty, scalar_llvm_ty }, .i32);
3834 const libc_fn = try o.getLibcFunction(fn_name, &.{ scalar_llvm_ty, scalar_llvm_ty }, .i32);
38683835
38693836 const int_cond: Builder.IntegerCondition = switch (pred) {
38703837 .eq => .eq,
......@@ -3939,13 +3906,13 @@ fn buildFloatOp(
39393906 ty: Type,
39403907 comptime params_len: usize,
39413908 params: [params_len]Builder.Value,
3942) !Builder.Value {
3909) Allocator.Error!Builder.Value {
39433910 const o = self.object;
39443911 const pt = self.pt;
39453912 const zcu = pt.zcu;
39463913 const target = zcu.getTarget();
39473914 const scalar_ty = ty.scalarType(zcu);
3948 const llvm_ty = try o.lowerType(pt, ty);
3915 const llvm_ty = try self.lowerType(ty);
39493916
39503917 if (op != .tan and intrinsicsAllowed(scalar_ty, target)) switch (op) {
39513918 // Some operations are dedicated LLVM instructions, not available as intrinsics
......@@ -4048,7 +4015,7 @@ fn buildFloatOp(
40484015 };
40494016
40504017 const scalar_llvm_ty = llvm_ty.scalarType(&o.builder);
4051 const libc_fn = try self.getLibcFunction(
4018 const libc_fn = try o.getLibcFunction(
40524019 fn_name,
40534020 ([1]Builder.Type{scalar_llvm_ty} ** 3)[0..params.len],
40544021 scalar_llvm_ty,
......@@ -4069,7 +4036,7 @@ fn buildFloatOp(
40694036 );
40704037}
40714038
4072fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4039fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
40734040 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
40744041 const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
40754042
......@@ -4081,8 +4048,7 @@ fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
40814048 return self.buildFloatOp(.fma, .normal, ty, 3, .{ mulend1, mulend2, addend });
40824049}
40834050
4084fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4085 const o = self.object;
4051fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
40864052 const pt = self.pt;
40874053 const zcu = pt.zcu;
40884054 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
......@@ -4092,15 +4058,19 @@ fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
40924058 const rhs = try self.resolveInst(extra.rhs);
40934059
40944060 const lhs_ty = self.typeOf(extra.lhs);
4095 if (lhs_ty.isVector(zcu) and !self.typeOf(extra.rhs).isVector(zcu))
4096 return self.todo("implement vector shifts with scalar rhs", .{});
4061 if (lhs_ty.isVector(zcu) and !self.typeOf(extra.rhs).isVector(zcu)) {
4062 // `Sema` does not currently emit this pattern---instead it is specific to `Air.Legalize`
4063 // features which we do not use. Therefore this branch is currently impossible.
4064 unreachable;
4065 }
4066
40974067 const lhs_scalar_ty = lhs_ty.scalarType(zcu);
40984068
40994069 const dest_ty = self.typeOfIndex(inst);
41004070 assert(isByRef(dest_ty, zcu)); // auto structs are by-ref
4101 const llvm_dest_ty = try o.lowerType(pt, dest_ty);
4071 const llvm_dest_ty = try self.lowerType(dest_ty);
41024072
4103 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(pt, lhs_ty), "");
4073 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
41044074
41054075 const result = try self.wip.bin(.shl, lhs, casted_rhs, "");
41064076 const reconstructed = try self.wip.bin(if (lhs_scalar_ty.isSignedInt(zcu))
......@@ -4128,29 +4098,28 @@ fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
41284098 return alloca_inst;
41294099}
41304100
4131fn airAnd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4101fn airAnd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41324102 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41334103 const lhs = try self.resolveInst(bin_op.lhs);
41344104 const rhs = try self.resolveInst(bin_op.rhs);
41354105 return self.wip.bin(.@"and", lhs, rhs, "");
41364106}
41374107
4138fn airOr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4108fn airOr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41394109 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41404110 const lhs = try self.resolveInst(bin_op.lhs);
41414111 const rhs = try self.resolveInst(bin_op.rhs);
41424112 return self.wip.bin(.@"or", lhs, rhs, "");
41434113}
41444114
4145fn airXor(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4115fn airXor(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41464116 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41474117 const lhs = try self.resolveInst(bin_op.lhs);
41484118 const rhs = try self.resolveInst(bin_op.rhs);
41494119 return self.wip.bin(.xor, lhs, rhs, "");
41504120}
41514121
4152fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4153 const o = self.object;
4122fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41544123 const pt = self.pt;
41554124 const zcu = pt.zcu;
41564125 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -4159,19 +4128,21 @@ fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
41594128 const rhs = try self.resolveInst(bin_op.rhs);
41604129
41614130 const lhs_ty = self.typeOf(bin_op.lhs);
4162 if (lhs_ty.isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu))
4163 return self.todo("implement vector shifts with scalar rhs", .{});
4131 if (lhs_ty.isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu)) {
4132 // `Sema` does not currently emit this pattern---instead it is specific to `Air.Legalize`
4133 // features which we do not use. Therefore this branch is currently impossible.
4134 unreachable;
4135 }
41644136 const lhs_scalar_ty = lhs_ty.scalarType(zcu);
41654137
4166 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(pt, lhs_ty), "");
4138 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
41674139 return self.wip.bin(if (lhs_scalar_ty.isSignedInt(zcu))
41684140 .@"shl nsw"
41694141 else
41704142 .@"shl nuw", lhs, casted_rhs, "");
41714143}
41724144
4173fn airShl(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4174 const o = self.object;
4145fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41754146 const pt = self.pt;
41764147 const zcu = pt.zcu;
41774148 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -4180,14 +4151,16 @@ fn airShl(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
41804151 const rhs = try self.resolveInst(bin_op.rhs);
41814152
41824153 const lhs_ty = self.typeOf(bin_op.lhs);
4183 if (lhs_ty.isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu))
4184 return self.todo("implement vector shifts with scalar rhs", .{});
4185
4186 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(pt, lhs_ty), "");
4154 if (lhs_ty.isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu)) {
4155 // `Sema` does not currently emit this pattern---instead it is specific to `Air.Legalize`
4156 // features which we do not use. Therefore this branch is currently impossible.
4157 unreachable;
4158 }
4159 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
41874160 return self.wip.bin(.shl, lhs, casted_rhs, "");
41884161}
41894162
4190fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4163fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41914164 const o = self.object;
41924165 const pt = self.pt;
41934166 const zcu = pt.zcu;
......@@ -4198,15 +4171,18 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
41984171
41994172 const lhs_ty = self.typeOf(bin_op.lhs);
42004173 const lhs_info = lhs_ty.intInfo(zcu);
4201 const llvm_lhs_ty = try o.lowerType(pt, lhs_ty);
4174 const llvm_lhs_ty = try self.lowerType(lhs_ty);
42024175 const llvm_lhs_scalar_ty = llvm_lhs_ty.scalarType(&o.builder);
42034176
42044177 const rhs_ty = self.typeOf(bin_op.rhs);
4205 if (lhs_ty.isVector(zcu) and !rhs_ty.isVector(zcu))
4206 return self.todo("implement vector shifts with scalar rhs", .{});
4178 if (lhs_ty.isVector(zcu) and !rhs_ty.isVector(zcu)) {
4179 // `Sema` does not currently emit this pattern---instead it is specific to `Air.Legalize`
4180 // features which we do not use. Therefore this branch is currently impossible.
4181 unreachable;
4182 }
42074183 const rhs_info = rhs_ty.intInfo(zcu);
42084184 assert(rhs_info.signedness == .unsigned);
4209 const llvm_rhs_ty = try o.lowerType(pt, rhs_ty);
4185 const llvm_rhs_ty = try self.lowerType(rhs_ty);
42104186 const llvm_rhs_scalar_ty = llvm_rhs_ty.scalarType(&o.builder);
42114187
42124188 const result = try self.wip.callIntrinsic(
......@@ -4267,8 +4243,7 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
42674243 return self.wip.select(.normal, in_range, result, lhs_sat, "");
42684244}
42694245
4270fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !Builder.Value {
4271 const o = self.object;
4246fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!Builder.Value {
42724247 const pt = self.pt;
42734248 const zcu = pt.zcu;
42744249 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -4277,11 +4252,14 @@ fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !Builder.Value {
42774252 const rhs = try self.resolveInst(bin_op.rhs);
42784253
42794254 const lhs_ty = self.typeOf(bin_op.lhs);
4280 if (lhs_ty.isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu))
4281 return self.todo("implement vector shifts with scalar rhs", .{});
4255 if (lhs_ty.isVector(zcu) and !self.typeOf(bin_op.rhs).isVector(zcu)) {
4256 // `Sema` does not currently emit this pattern---instead it is specific to `Air.Legalize`
4257 // features which we do not use. Therefore this branch is currently impossible.
4258 unreachable;
4259 }
42824260 const lhs_scalar_ty = lhs_ty.scalarType(zcu);
42834261
4284 const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(pt, lhs_ty), "");
4262 const casted_rhs = try self.wip.conv(.unsigned, rhs, try self.lowerType(lhs_ty), "");
42854263 const is_signed_int = lhs_scalar_ty.isSignedInt(zcu);
42864264
42874265 return self.wip.bin(if (is_exact)
......@@ -4289,7 +4267,7 @@ fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !Builder.Value {
42894267 else if (is_signed_int) .ashr else .lshr, lhs, casted_rhs, "");
42904268}
42914269
4292fn airAbs(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4270fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
42934271 const o = self.object;
42944272 const pt = self.pt;
42954273 const zcu = pt.zcu;
......@@ -4303,7 +4281,7 @@ fn airAbs(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
43034281 .normal,
43044282 .none,
43054283 .abs,
4306 &.{try o.lowerType(pt, operand_ty)},
4284 &.{try self.lowerType(operand_ty)},
43074285 &.{ operand, try o.builder.intValue(.i1, 0) },
43084286 "",
43094287 ),
......@@ -4312,13 +4290,13 @@ fn airAbs(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
43124290 }
43134291}
43144292
4315fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
4293fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
43164294 const o = fg.object;
43174295 const pt = fg.pt;
43184296 const zcu = pt.zcu;
43194297 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
43204298 const dest_ty = fg.typeOfIndex(inst);
4321 const dest_llvm_ty = try o.lowerType(pt, dest_ty);
4299 const dest_llvm_ty = try fg.lowerType(dest_ty);
43224300 const operand = try fg.resolveInst(ty_op.operand);
43234301 const operand_ty = fg.typeOf(ty_op.operand);
43244302 const operand_info = operand_ty.intInfo(zcu);
......@@ -4346,8 +4324,8 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
43464324
43474325 if (!have_min_check and !have_max_check) break :bounds_check;
43484326
4349 const operand_llvm_ty = try o.lowerType(pt, operand_ty);
4350 const operand_scalar_llvm_ty = try o.lowerType(pt, operand_scalar);
4327 const operand_llvm_ty = try fg.lowerType(operand_ty);
4328 const operand_scalar_llvm_ty = try fg.lowerType(operand_scalar);
43514329
43524330 const is_vector = operand_ty.zigTypeTag(zcu) == .vector;
43534331 assert(is_vector == (dest_ty.zigTypeTag(zcu) == .vector));
......@@ -4401,7 +4379,7 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
44014379 }, operand, dest_llvm_ty, "");
44024380
44034381 if (safety and dest_is_enum and !dest_ty.isNonexhaustiveEnum(zcu)) {
4404 const llvm_fn = try fg.getIsNamedEnumValueFunction(dest_ty);
4382 const llvm_fn = try o.getIsNamedEnumValueFunction(pt, dest_ty);
44054383 const is_valid_enum_val = try fg.wip.call(
44064384 .normal,
44074385 .fastcc,
......@@ -4422,16 +4400,14 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
44224400 return result;
44234401}
44244402
4425fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4426 const o = self.object;
4427 const pt = self.pt;
4403fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
44284404 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
44294405 const operand = try self.resolveInst(ty_op.operand);
4430 const dest_llvm_ty = try o.lowerType(pt, self.typeOfIndex(inst));
4406 const dest_llvm_ty = try self.lowerType(self.typeOfIndex(inst));
44314407 return self.wip.cast(.trunc, operand, dest_llvm_ty, "");
44324408}
44334409
4434fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4410fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
44354411 const o = self.object;
44364412 const pt = self.pt;
44374413 const zcu = pt.zcu;
......@@ -4442,10 +4418,10 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
44424418 const target = zcu.getTarget();
44434419
44444420 if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) {
4445 return self.wip.cast(.fptrunc, operand, try o.lowerType(pt, dest_ty), "");
4421 return self.wip.cast(.fptrunc, operand, try self.lowerType(dest_ty), "");
44464422 } else {
4447 const operand_llvm_ty = try o.lowerType(pt, operand_ty);
4448 const dest_llvm_ty = try o.lowerType(pt, dest_ty);
4423 const operand_llvm_ty = try self.lowerType(operand_ty);
4424 const dest_llvm_ty = try self.lowerType(dest_ty);
44494425
44504426 const dest_bits = dest_ty.floatBits(target);
44514427 const src_bits = operand_ty.floatBits(target);
......@@ -4453,7 +4429,7 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
44534429 compilerRtFloatAbbrev(src_bits), compilerRtFloatAbbrev(dest_bits),
44544430 });
44554431
4456 const libc_fn = try self.getLibcFunction(fn_name, &.{operand_llvm_ty}, dest_llvm_ty);
4432 const libc_fn = try o.getLibcFunction(fn_name, &.{operand_llvm_ty}, dest_llvm_ty);
44574433 return self.wip.call(
44584434 .normal,
44594435 .ccc,
......@@ -4466,7 +4442,7 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
44664442 }
44674443}
44684444
4469fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4445fn airFpext(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
44704446 const o = self.object;
44714447 const pt = self.pt;
44724448 const zcu = pt.zcu;
......@@ -4477,10 +4453,10 @@ fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
44774453 const target = zcu.getTarget();
44784454
44794455 if (intrinsicsAllowed(dest_ty, target) and intrinsicsAllowed(operand_ty, target)) {
4480 return self.wip.cast(.fpext, operand, try o.lowerType(pt, dest_ty), "");
4456 return self.wip.cast(.fpext, operand, try self.lowerType(dest_ty), "");
44814457 } else {
4482 const operand_llvm_ty = try o.lowerType(pt, operand_ty);
4483 const dest_llvm_ty = try o.lowerType(pt, dest_ty);
4458 const operand_llvm_ty = try self.lowerType(operand_ty);
4459 const dest_llvm_ty = try self.lowerType(dest_ty);
44844460
44854461 const dest_bits = dest_ty.scalarType(zcu).floatBits(target);
44864462 const src_bits = operand_ty.scalarType(zcu).floatBits(target);
......@@ -4488,7 +4464,7 @@ fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
44884464 compilerRtFloatAbbrev(src_bits), compilerRtFloatAbbrev(dest_bits),
44894465 });
44904466
4491 const libc_fn = try self.getLibcFunction(fn_name, &.{operand_llvm_ty}, dest_llvm_ty);
4467 const libc_fn = try o.getLibcFunction(fn_name, &.{operand_llvm_ty}, dest_llvm_ty);
44924468 if (dest_ty.isVector(zcu)) return self.buildElementwiseCall(
44934469 libc_fn,
44944470 &.{operand},
......@@ -4507,7 +4483,7 @@ fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
45074483 }
45084484}
45094485
4510fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4486fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
45114487 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
45124488 const operand_ty = self.typeOf(ty_op.operand);
45134489 const inst_ty = self.typeOfIndex(inst);
......@@ -4515,13 +4491,13 @@ fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
45154491 return self.bitCast(operand, operand_ty, inst_ty);
45164492}
45174493
4518fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) !Builder.Value {
4494fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) Allocator.Error!Builder.Value {
45194495 const o = self.object;
45204496 const pt = self.pt;
45214497 const zcu = pt.zcu;
45224498 const operand_is_ref = isByRef(operand_ty, zcu);
45234499 const result_is_ref = isByRef(inst_ty, zcu);
4524 const llvm_dest_ty = try o.lowerType(pt, inst_ty);
4500 const llvm_dest_ty = try self.lowerType(inst_ty);
45254501
45264502 if (operand_is_ref and result_is_ref) {
45274503 // They are both pointers, so just return the same opaque pointer :)
......@@ -4544,11 +4520,8 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
45444520 }
45454521
45464522 if (operand_ty.zigTypeTag(zcu) == .vector and inst_ty.zigTypeTag(zcu) == .array) {
4547 const elem_ty = inst_ty.childType(zcu);
4548 assert(elem_ty.toIntern() == operand_scalar_ty.toIntern());
4549 if (!result_is_ref) {
4550 return self.todo("implement bitcast vector to non-ref array", .{});
4551 }
4523 const elem_ty = operand_scalar_ty;
4524 assert(result_is_ref); // arrays are always by-ref provided they have runtime bits
45524525 const alignment = inst_ty.abiAlignment(zcu).toLlvm();
45534526 const array_ptr = try self.buildAlloca(llvm_dest_ty, alignment);
45544527 const bitcast_ok = elem_ty.bitSize(zcu) == elem_ty.abiSize(zcu) * 8;
......@@ -4569,9 +4542,8 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
45694542 return array_ptr;
45704543 } else if (operand_ty.zigTypeTag(zcu) == .array and inst_ty.zigTypeTag(zcu) == .vector) {
45714544 const elem_ty = operand_ty.childType(zcu);
4572 assert(elem_ty.toIntern() == inst_scalar_ty.toIntern());
4573 const llvm_vector_ty = try o.lowerType(pt, inst_ty);
4574 if (!operand_is_ref) return self.todo("implement bitcast non-ref array to vector", .{});
4545 assert(operand_is_ref); // arrays are always by-ref provided they have runtime bits
4546 const llvm_vector_ty = try self.lowerType(inst_ty);
45754547
45764548 const bitcast_ok = elem_ty.bitSize(zcu) == elem_ty.abiSize(zcu) * 8;
45774549 if (bitcast_ok) {
......@@ -4582,7 +4554,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
45824554 } else {
45834555 // If the ABI size of the element type is not evenly divisible by size in bits;
45844556 // a simple bitcast will not work, and we fall back to extractelement.
4585 const elem_llvm_ty = try o.lowerType(pt, elem_ty);
4557 const elem_llvm_ty = try self.lowerType(elem_ty);
45864558 const elem_size = elem_ty.abiSize(zcu);
45874559 const vector_len = operand_ty.arrayLen(zcu);
45884560 var vector = try o.builder.poisonValue(llvm_vector_ty);
......@@ -4624,7 +4596,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
46244596 return self.wip.cast(.bitcast, operand, llvm_dest_ty, "");
46254597}
46264598
4627fn airArg(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4599fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
46284600 const o = self.object;
46294601 const pt = self.pt;
46304602 const zcu = pt.zcu;
......@@ -4714,7 +4686,7 @@ fn airArg(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
47144686 return arg_val;
47154687}
47164688
4717fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4689fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
47184690 const o = self.object;
47194691 const pt = self.pt;
47204692 const zcu = pt.zcu;
......@@ -4723,12 +4695,12 @@ fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
47234695 if (!pointee_type.hasRuntimeBits(zcu))
47244696 return (try o.lowerPtrToVoid(pt, ptr_ty)).toValue();
47254697
4726 const pointee_llvm_ty = try o.lowerType(pt, pointee_type);
4698 const pointee_llvm_ty = try self.lowerType(pointee_type);
47274699 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
47284700 return self.buildAlloca(pointee_llvm_ty, alignment);
47294701}
47304702
4731fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4703fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
47324704 const o = self.object;
47334705 const pt = self.pt;
47344706 const zcu = pt.zcu;
......@@ -4737,7 +4709,7 @@ fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
47374709 if (!ret_ty.hasRuntimeBits(zcu))
47384710 return (try o.lowerPtrToVoid(pt, ptr_ty)).toValue();
47394711 if (self.ret_ptr != .none) return self.ret_ptr;
4740 const ret_llvm_ty = try o.lowerType(pt, ret_ty);
4712 const ret_llvm_ty = try self.lowerType(ret_ty);
47414713 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
47424714 return self.buildAlloca(ret_llvm_ty, alignment);
47434715}
......@@ -4753,7 +4725,7 @@ fn buildAlloca(
47534725 return buildAllocaInner(&self.wip, llvm_ty, alignment, target);
47544726}
47554727
4756fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
4728fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
47574729 const o = self.object;
47584730 const pt = self.pt;
47594731 const zcu = pt.zcu;
......@@ -4790,7 +4762,7 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
47904762
47914763 self.maybeMarkAllowZeroAccess(ptr_info);
47924764
4793 const len = try o.builder.intValue(try o.lowerType(pt, Type.usize), operand_ty.abiSize(zcu));
4765 const len = try o.builder.intValue(try self.lowerType(Type.usize), operand_ty.abiSize(zcu));
47944766 _ = try self.wip.callMemSet(
47954767 dest_ptr,
47964768 ptr_ty.ptrAlignment(zcu).toLlvm(),
......@@ -4812,7 +4784,7 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
48124784 return .none;
48134785}
48144786
4815fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4787fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48164788 const pt = fg.pt;
48174789 const zcu = pt.zcu;
48184790 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
......@@ -4823,7 +4795,7 @@ fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
48234795 return fg.load(ptr, ptr_ty);
48244796}
48254797
4826fn airTrap(self: *FuncGen, inst: Air.Inst.Index) !void {
4798fn airTrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
48274799 _ = inst;
48284800 const target = self.pt.zcu.getTarget();
48294801 if ((target.cpu.arch == .mips or target.cpu.arch == .mipsel) and
......@@ -4847,17 +4819,16 @@ fn airTrap(self: *FuncGen, inst: Air.Inst.Index) !void {
48474819 _ = try self.wip.@"unreachable"();
48484820}
48494821
4850fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4822fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48514823 _ = inst;
48524824 _ = try self.wip.callIntrinsic(.normal, .none, .debugtrap, &.{}, &.{}, "");
48534825 return .none;
48544826}
48554827
4856fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4828fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48574829 _ = inst;
48584830 const o = self.object;
4859 const pt = self.pt;
4860 const llvm_usize = try o.lowerType(pt, Type.usize);
4831 const llvm_usize = try self.lowerType(Type.usize);
48614832 if (!target_util.supportsReturnAddress(self.pt.zcu.getTarget(), self.ownerModule().optimize_mode)) {
48624833 // https://github.com/ziglang/zig/issues/11946
48634834 return o.builder.intValue(llvm_usize, 0);
......@@ -4866,19 +4837,17 @@ fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
48664837 return self.wip.cast(.ptrtoint, result, llvm_usize, "");
48674838}
48684839
4869fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4840fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
48704841 _ = inst;
4871 const o = self.object;
4872 const pt = self.pt;
48734842 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{.@"0"}, "");
4874 return self.wip.cast(.ptrtoint, result, try o.lowerType(pt, Type.usize), "");
4843 return self.wip.cast(.ptrtoint, result, try self.lowerType(Type.usize), "");
48754844}
48764845
48774846fn airCmpxchg(
48784847 self: *FuncGen,
48794848 inst: Air.Inst.Index,
48804849 kind: Builder.Function.Instruction.CmpXchg.Kind,
4881) !Builder.Value {
4850) Allocator.Error!Builder.Value {
48824851 const o = self.object;
48834852 const pt = self.pt;
48844853 const zcu = pt.zcu;
......@@ -4889,7 +4858,7 @@ fn airCmpxchg(
48894858 var expected_value = try self.resolveInst(extra.expected_value);
48904859 var new_value = try self.resolveInst(extra.new_value);
48914860 const operand_ty = ptr_ty.childType(zcu);
4892 const llvm_operand_ty = try o.lowerType(pt, operand_ty);
4861 const llvm_operand_ty = try self.lowerType(operand_ty);
48934862 const llvm_abi_ty = try self.getAtomicAbiType(operand_ty, false);
48944863 if (llvm_abi_ty != .none) {
48954864 // operand needs widening and truncating
......@@ -4932,7 +4901,7 @@ fn airCmpxchg(
49324901 const non_null_bit = try self.wip.not(success_bit, "");
49334902
49344903 const payload_align = operand_ty.abiAlignment(zcu).toLlvm();
4935 const alloca_inst = try self.buildAlloca(try o.lowerType(pt, optional_ty), payload_align);
4904 const alloca_inst = try self.buildAlloca(try self.lowerType(optional_ty), payload_align);
49364905
49374906 // Payload is always the first field at offset 0, so address is `alloca_inst`
49384907 _ = try self.wip.store(.normal, payload, alloca_inst, payload_align);
......@@ -4944,7 +4913,7 @@ fn airCmpxchg(
49444913 return alloca_inst;
49454914}
49464915
4947fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
4916fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
49484917 const o = self.object;
49494918 const pt = self.pt;
49504919 const zcu = pt.zcu;
......@@ -4959,7 +4928,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
49594928 const op = toLlvmAtomicRmwBinOp(extra.op(), is_signed_int, is_float);
49604929 const ordering = toLlvmAtomicOrdering(extra.ordering());
49614930 const llvm_abi_ty = try self.getAtomicAbiType(operand_ty, op == .xchg);
4962 const llvm_operand_ty = try o.lowerType(pt, operand_ty);
4931 const llvm_operand_ty = try self.lowerType(operand_ty);
49634932
49644933 const access_kind: Builder.MemoryAccessKind =
49654934 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
......@@ -5002,7 +4971,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
50024971 access_kind,
50034972 op,
50044973 ptr,
5005 try self.wip.cast(.ptrtoint, operand, try o.lowerType(pt, Type.usize), ""),
4974 try self.wip.cast(.ptrtoint, operand, try self.lowerType(Type.usize), ""),
50064975 self.sync_scope,
50074976 ordering,
50084977 ptr_alignment,
......@@ -5010,8 +4979,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
50104979 ), llvm_operand_ty, "");
50114980}
50124981
5013fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5014 const o = self.object;
4982fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
50154983 const pt = self.pt;
50164984 const zcu = pt.zcu;
50174985 const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
......@@ -5028,7 +4996,7 @@ fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
50284996 Type.fromInterned(info.child).abiAlignment(zcu)).toLlvm();
50294997 const access_kind: Builder.MemoryAccessKind =
50304998 if (info.flags.is_volatile) .@"volatile" else .normal;
5031 const elem_llvm_ty = try o.lowerType(pt, elem_ty);
4999 const elem_llvm_ty = try self.lowerType(elem_ty);
50325000
50335001 self.maybeMarkAllowZeroAccess(info);
50345002
......@@ -5060,7 +5028,7 @@ fn airAtomicStore(
50605028 self: *FuncGen,
50615029 inst: Air.Inst.Index,
50625030 ordering: Builder.AtomicOrdering,
5063) !Builder.Value {
5031) Allocator.Error!Builder.Value {
50645032 const pt = self.pt;
50655033 const zcu = pt.zcu;
50665034 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -5087,7 +5055,7 @@ fn airAtomicStore(
50875055 return .none;
50885056}
50895057
5090fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
5058fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
50915059 const o = self.object;
50925060 const pt = self.pt;
50935061 const zcu = pt.zcu;
......@@ -5186,7 +5154,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value
51865154 const body_block = try self.wip.block(1, "InlineMemsetBody");
51875155 const end_block = try self.wip.block(1, "InlineMemsetEnd");
51885156
5189 const llvm_usize_ty = try o.lowerType(pt, Type.usize);
5157 const llvm_usize_ty = try self.lowerType(Type.usize);
51905158 const end_ptr = switch (ptr_ty.ptrSize(zcu)) {
51915159 .slice => try self.ptraddScaled(
51925160 dest_ptr,
......@@ -5225,7 +5193,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value
52255193 return .none;
52265194}
52275195
5228fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5196fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
52295197 const pt = self.pt;
52305198 const zcu = pt.zcu;
52315199 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -5254,7 +5222,7 @@ fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
52545222 return .none;
52555223}
52565224
5257fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5225fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
52585226 const pt = self.pt;
52595227 const zcu = pt.zcu;
52605228 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
......@@ -5279,14 +5247,15 @@ fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
52795247 return .none;
52805248}
52815249
5282fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5250fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
52835251 const pt = self.pt;
52845252 const zcu = pt.zcu;
52855253 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
52865254 const un_ptr_ty = self.typeOf(bin_op.lhs);
52875255 const un_ty = un_ptr_ty.childType(zcu);
52885256 const layout = un_ty.unionGetLayout(zcu);
5289 assert(layout.tag_size != 0);
5257
5258 if (layout.tag_size == 0) return .none; // TODO: stop Sema emitting this
52905259
52915260 const access_kind: Builder.MemoryAccessKind =
52925261 if (un_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
......@@ -5309,7 +5278,7 @@ fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
53095278 return .none;
53105279}
53115280
5312fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5281fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
53135282 const o = self.object;
53145283 const pt = self.pt;
53155284 const zcu = pt.zcu;
......@@ -5319,7 +5288,7 @@ fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
53195288 assert(layout.tag_size != 0);
53205289 const union_ptr = try self.resolveInst(ty_op.operand);
53215290 if (isByRef(un_ty, zcu)) {
5322 const llvm_un_ty = try o.lowerType(pt, un_ty);
5291 const llvm_un_ty = try self.lowerType(un_ty);
53235292 if (layout.payload_size == 0)
53245293 return self.wip.load(.normal, llvm_un_ty, union_ptr, .default, "");
53255294 const tag_index = @intFromBool(layout.tag_align.compare(.lt, layout.payload_align));
......@@ -5333,7 +5302,7 @@ fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
53335302 }
53345303}
53355304
5336fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, comptime op: FloatOp) !Builder.Value {
5305fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, comptime op: FloatOp) Allocator.Error!Builder.Value {
53375306 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
53385307 const operand = try self.resolveInst(un_op);
53395308 const operand_ty = self.typeOf(un_op);
......@@ -5341,7 +5310,7 @@ fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, comptime op: FloatOp) !Build
53415310 return self.buildFloatOp(op, .normal, operand_ty, 1, .{operand});
53425311}
53435312
5344fn airNeg(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
5313fn airNeg(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
53455314 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
53465315 const operand = try self.resolveInst(un_op);
53475316 const operand_ty = self.typeOf(un_op);
......@@ -5349,9 +5318,7 @@ fn airNeg(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Bui
53495318 return self.buildFloatOp(.neg, fast, operand_ty, 1, .{operand});
53505319}
53515320
5352fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic) !Builder.Value {
5353 const o = self.object;
5354 const pt = self.pt;
5321fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic) Allocator.Error!Builder.Value {
53555322 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
53565323 const inst_ty = self.typeOfIndex(inst);
53575324 const operand_ty = self.typeOf(ty_op.operand);
......@@ -5361,16 +5328,14 @@ fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)
53615328 .normal,
53625329 .none,
53635330 intrinsic,
5364 &.{try o.lowerType(pt, operand_ty)},
5331 &.{try self.lowerType(operand_ty)},
53655332 &.{ operand, .false },
53665333 "",
53675334 );
5368 return self.wip.conv(.unsigned, result, try o.lowerType(pt, inst_ty), "");
5335 return self.wip.conv(.unsigned, result, try self.lowerType(inst_ty), "");
53695336}
53705337
5371fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic) !Builder.Value {
5372 const o = self.object;
5373 const pt = self.pt;
5338fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic) Allocator.Error!Builder.Value {
53745339 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
53755340 const inst_ty = self.typeOfIndex(inst);
53765341 const operand_ty = self.typeOf(ty_op.operand);
......@@ -5380,14 +5345,14 @@ fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)
53805345 .normal,
53815346 .none,
53825347 intrinsic,
5383 &.{try o.lowerType(pt, operand_ty)},
5348 &.{try self.lowerType(operand_ty)},
53845349 &.{operand},
53855350 "",
53865351 );
5387 return self.wip.conv(.unsigned, result, try o.lowerType(pt, inst_ty), "");
5352 return self.wip.conv(.unsigned, result, try self.lowerType(inst_ty), "");
53885353}
53895354
5390fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5355fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
53915356 const o = self.object;
53925357 const pt = self.pt;
53935358 const zcu = pt.zcu;
......@@ -5398,7 +5363,7 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
53985363
53995364 const inst_ty = self.typeOfIndex(inst);
54005365 var operand = try self.resolveInst(ty_op.operand);
5401 var llvm_operand_ty = try o.lowerType(pt, operand_ty);
5366 var llvm_operand_ty = try self.lowerType(operand_ty);
54025367
54035368 if (bits % 16 == 8) {
54045369 // If not an even byte-multiple, we need zero-extend + shift-left 1 byte
......@@ -5419,10 +5384,10 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
54195384
54205385 const result =
54215386 try self.wip.callIntrinsic(.normal, .none, .bswap, &.{llvm_operand_ty}, &.{operand}, "");
5422 return self.wip.conv(.unsigned, result, try o.lowerType(pt, inst_ty), "");
5387 return self.wip.conv(.unsigned, result, try self.lowerType(inst_ty), "");
54235388}
54245389
5425fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5390fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
54265391 const o = self.object;
54275392 const pt = self.pt;
54285393 const zcu = pt.zcu;
......@@ -5440,7 +5405,7 @@ fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
54405405
54415406 for (0..names.len) |name_index| {
54425407 const err_int = ip.getErrorValueIfExists(names.get(ip)[name_index]).?;
5443 const this_tag_int_value = try o.builder.intConst(try o.errorIntType(pt), err_int);
5408 const this_tag_int_value = try o.builder.intConst(try o.errorIntType(), err_int);
54445409 try wip_switch.addCase(this_tag_int_value, valid_block, &self.wip);
54455410 }
54465411 self.wip.cursor = .{ .block = valid_block };
......@@ -5455,13 +5420,13 @@ fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
54555420 return phi.toValue();
54565421}
54575422
5458fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5423fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
54595424 const o = self.object;
54605425 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
54615426 const operand = try self.resolveInst(un_op);
54625427 const enum_ty = self.typeOf(un_op);
54635428
5464 const llvm_fn = try self.getIsNamedEnumValueFunction(enum_ty);
5429 const llvm_fn = try o.getIsNamedEnumValueFunction(self.pt, enum_ty);
54655430 return self.wip.call(
54665431 .normal,
54675432 .fastcc,
......@@ -5473,28 +5438,7 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
54735438 );
54745439}
54755440
5476fn getIsNamedEnumValueFunction(self: *FuncGen, enum_ty: Type) !Builder.Function.Index {
5477 const o = self.object;
5478 const pt = self.pt;
5479 const zcu = pt.zcu;
5480 const ip = &zcu.intern_pool;
5481
5482 const gop = try o.named_enum_map.getOrPut(o.gpa, enum_ty.toIntern());
5483 if (gop.found_existing) return gop.value_ptr.*;
5484 errdefer assert(o.named_enum_map.remove(enum_ty.toIntern()));
5485 const function_index = try o.builder.addFunction(
5486 // Dummy function type; `updateIsNamedEnumValue` will replace it with the correct type.
5487 // TODO: change the builder API so we don't need to do this.
5488 try o.builder.fnType(.void, &.{}, .normal),
5489 try o.builder.strtabStringFmt("__zig_is_named_enum_value_{f}", .{enum_ty.containerTypeName(ip).fmt(ip)}),
5490 toLlvmAddressSpace(.generic, zcu.getTarget()),
5491 );
5492 gop.value_ptr.* = function_index;
5493 try o.updateIsNamedEnumValueFunction(pt, enum_ty, function_index);
5494 return function_index;
5495}
5496
5497fn airTagName(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5441fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
54985442 const o = self.object;
54995443 const pt = self.pt;
55005444 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
......@@ -5513,34 +5457,31 @@ fn airTagName(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
55135457 );
55145458}
55155459
5516fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5460fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
55175461 const o = self.object;
55185462 const pt = self.pt;
55195463 const zcu = pt.zcu;
55205464 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
55215465 const operand = try self.resolveInst(un_op);
55225466 const slice_ty = self.typeOfIndex(inst);
5523 const slice_llvm_ty = try o.lowerType(pt, slice_ty);
5467 const slice_llvm_ty = try self.lowerType(slice_ty);
55245468
55255469 // If operand is small (e.g. `u8`), then signedness becomes a problem -- GEP always treats the index as signed.
5526 const operand_usize = try self.wip.conv(.unsigned, operand, try o.lowerType(pt, .usize), "");
5470 const operand_usize = try self.wip.conv(.unsigned, operand, try self.lowerType(.usize), "");
55275471
5528 const error_name_table_ptr = try self.getErrorNameTable();
5529 const error_name_table = try self.wip.load(.normal, .ptr, error_name_table_ptr.toValue(&o.builder), .default, "");
5530 const error_name_ptr = try self.ptraddScaled(error_name_table, operand_usize, slice_ty.abiSize(zcu));
5472 const error_name_table_ptr = try o.getErrorNameTable();
5473 const error_name_ptr = try self.ptraddScaled(error_name_table_ptr.toValue(&o.builder), operand_usize, slice_ty.abiSize(zcu));
55315474 return self.wip.load(.normal, slice_llvm_ty, error_name_ptr, .default, "");
55325475}
55335476
5534fn airSplat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5535 const o = self.object;
5536 const pt = self.pt;
5477fn airSplat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
55375478 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
55385479 const scalar = try self.resolveInst(ty_op.operand);
55395480 const vector_ty = self.typeOfIndex(inst);
5540 return self.wip.splatVector(try o.lowerType(pt, vector_ty), scalar, "");
5481 return self.wip.splatVector(try self.lowerType(vector_ty), scalar, "");
55415482}
55425483
5543fn airSelect(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5484fn airSelect(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
55445485 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
55455486 const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
55465487 const pred = try self.resolveInst(pl_op.operand);
......@@ -5550,7 +5491,7 @@ fn airSelect(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
55505491 return self.wip.select(.normal, pred, a, b, "");
55515492}
55525493
5553fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5494fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
55545495 const o = fg.object;
55555496 const pt = fg.pt;
55565497 const zcu = pt.zcu;
......@@ -5561,9 +5502,9 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
55615502 const operand = try fg.resolveInst(unwrapped.operand);
55625503 const mask = unwrapped.mask;
55635504 const operand_ty = fg.typeOf(unwrapped.operand);
5564 const llvm_operand_ty = try o.lowerType(pt, operand_ty);
5565 const llvm_result_ty = try o.lowerType(pt, unwrapped.result_ty);
5566 const llvm_elem_ty = try o.lowerType(pt, unwrapped.result_ty.childType(zcu));
5505 const llvm_operand_ty = try fg.lowerType(operand_ty);
5506 const llvm_result_ty = try fg.lowerType(unwrapped.result_ty);
5507 const llvm_elem_ty = try fg.lowerType(unwrapped.result_ty.childType(zcu));
55675508 const llvm_poison_elem = try o.builder.poisonConst(llvm_elem_ty);
55685509 const llvm_poison_mask_elem = try o.builder.poisonConst(.i32);
55695510 const llvm_mask_ty = try o.builder.vectorType(.normal, @intCast(mask.len), .i32);
......@@ -5657,7 +5598,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
56575598 );
56585599}
56595600
5660fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5601fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
56615602 const o = fg.object;
56625603 const pt = fg.pt;
56635604 const zcu = pt.zcu;
......@@ -5666,7 +5607,7 @@ fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
56665607 const unwrapped = fg.air.unwrapShuffleTwo(zcu, inst);
56675608
56685609 const mask = unwrapped.mask;
5669 const llvm_elem_ty = try o.lowerType(pt, unwrapped.result_ty.childType(zcu));
5610 const llvm_elem_ty = try fg.lowerType(unwrapped.result_ty.childType(zcu));
56705611 const llvm_mask_ty = try o.builder.vectorType(.normal, @intCast(mask.len), .i32);
56715612 const llvm_poison_mask_elem = try o.builder.poisonConst(.i32);
56725613
......@@ -5756,10 +5697,9 @@ fn buildReducedCall(
57565697 operand_vector: Builder.Value,
57575698 vector_len: usize,
57585699 accum_init: Builder.Value,
5759) !Builder.Value {
5700) Allocator.Error!Builder.Value {
57605701 const o = self.object;
5761 const pt = self.pt;
5762 const usize_ty = try o.lowerType(pt, Type.usize);
5702 const usize_ty = try self.lowerType(Type.usize);
57635703 const llvm_vector_len = try o.builder.intValue(usize_ty, vector_len);
57645704 const llvm_result_ty = accum_init.typeOfWip(&self.wip);
57655705
......@@ -5811,7 +5751,7 @@ fn buildReducedCall(
58115751 return self.wip.load(.normal, llvm_result_ty, accum_ptr, .default, "");
58125752}
58135753
5814fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value {
5754fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
58155755 const o = self.object;
58165756 const pt = self.pt;
58175757 const zcu = pt.zcu;
......@@ -5820,9 +5760,9 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !
58205760 const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
58215761 const operand = try self.resolveInst(reduce.operand);
58225762 const operand_ty = self.typeOf(reduce.operand);
5823 const llvm_operand_ty = try o.lowerType(pt, operand_ty);
5763 const llvm_operand_ty = try self.lowerType(operand_ty);
58245764 const scalar_ty = self.typeOfIndex(inst);
5825 const llvm_scalar_ty = try o.lowerType(pt, scalar_ty);
5765 const llvm_scalar_ty = try self.lowerType(scalar_ty);
58265766
58275767 switch (reduce.operation) {
58285768 .And, .Or, .Xor => return self.wip.callIntrinsic(.normal, .none, switch (reduce.operation) {
......@@ -5890,8 +5830,7 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !
58905830 else => unreachable,
58915831 };
58925832
5893 const libc_fn =
5894 try self.getLibcFunction(fn_name, &.{ llvm_scalar_ty, llvm_scalar_ty }, llvm_scalar_ty);
5833 const libc_fn = try o.getLibcFunction(fn_name, &.{ llvm_scalar_ty, llvm_scalar_ty }, llvm_scalar_ty);
58955834 const init_val = switch (llvm_scalar_ty) {
58965835 .i16 => try o.builder.intValue(.i16, @as(i16, @bitCast(
58975836 @as(f16, switch (reduce.operation) {
......@@ -5922,7 +5861,7 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !
59225861 return self.buildReducedCall(libc_fn, operand, operand_ty.vectorLen(zcu), init_val);
59235862}
59245863
5925fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5864fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
59265865 const o = self.object;
59275866 const pt = self.pt;
59285867 const zcu = pt.zcu;
......@@ -5931,7 +5870,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
59315870 const result_ty = self.typeOfIndex(inst);
59325871 const len: usize = @intCast(result_ty.arrayLen(zcu));
59335872 const elements: []const Air.Inst.Ref = @ptrCast(self.air.extra.items[ty_pl.payload..][0..len]);
5934 const llvm_result_ty = try o.lowerType(pt, result_ty);
5873 const llvm_result_ty = try self.lowerType(result_ty);
59355874
59365875 switch (result_ty.zigTypeTag(zcu)) {
59375876 .vector => {
......@@ -5997,7 +5936,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
59975936 field_ptr_align.toLlvm(),
59985937 llvm_field_val,
59995938 field_ty.abiAlignment(zcu).toLlvm(),
6000 try o.builder.intValue(try o.lowerType(pt, .usize), field_ty.abiSize(zcu)),
5939 try o.builder.intValue(try self.lowerType(.usize), field_ty.abiSize(zcu)),
60015940 .normal,
60025941 self.disable_intrinsics,
60035942 );
......@@ -6042,7 +5981,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
60425981 }
60435982}
60445983
6045fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
5984fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
60465985 const o = self.object;
60475986 const pt = self.pt;
60485987 const zcu = pt.zcu;
......@@ -6050,7 +5989,7 @@ fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
60505989 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
60515990 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
60525991 const union_ty = self.typeOfIndex(inst);
6053 const union_llvm_ty = try o.lowerType(pt, union_ty);
5992 const union_llvm_ty = try self.lowerType(union_ty);
60545993 const union_obj = zcu.typeToUnion(union_ty).?;
60555994
60565995 assert(union_obj.layout != .@"packed");
......@@ -6086,7 +6025,7 @@ fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
60866025 return result_ptr;
60876026}
60886027
6089fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
6028fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
60906029 const o = self.object;
60916030 const prefetch = self.air.instructions.items(.data)[@intFromEnum(inst)].prefetch;
60926031
......@@ -6135,14 +6074,12 @@ fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
61356074 return .none;
61366075}
61376076
6138fn airAddrSpaceCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
6139 const o = self.object;
6140 const pt = self.pt;
6077fn airAddrSpaceCast(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
61416078 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
61426079 const inst_ty = self.typeOfIndex(inst);
61436080 const operand = try self.resolveInst(ty_op.operand);
61446081
6145 return self.wip.cast(.addrspacecast, operand, try o.lowerType(pt, inst_ty), "");
6082 return self.wip.cast(.addrspacecast, operand, try self.lowerType(inst_ty), "");
61466083}
61476084
61486085fn workIntrinsic(
......@@ -6150,7 +6087,7 @@ fn workIntrinsic(
61506087 dimension: u32,
61516088 default: u32,
61526089 comptime basename: []const u8,
6153) !Builder.Value {
6090) Allocator.Error!Builder.Value {
61546091 return self.wip.callIntrinsic(.normal, .none, switch (dimension) {
61556092 0 => @field(Builder.Intrinsic, basename ++ ".x"),
61566093 1 => @field(Builder.Intrinsic, basename ++ ".y"),
......@@ -6159,7 +6096,7 @@ fn workIntrinsic(
61596096 }, &.{}, &.{}, "");
61606097}
61616098
6162fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
6099fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
61636100 const target = self.pt.zcu.getTarget();
61646101
61656102 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
......@@ -6172,7 +6109,7 @@ fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
61726109 };
61736110}
61746111
6175fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
6112fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
61766113 const pt = self.pt;
61776114 const target = pt.zcu.getTarget();
61786115
......@@ -6200,7 +6137,7 @@ fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
62006137 }
62016138}
62026139
6203fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
6140fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
62046141 const target = self.pt.zcu.getTarget();
62056142
62066143 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
......@@ -6213,28 +6150,6 @@ fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
62136150 };
62146151}
62156152
6216fn getErrorNameTable(self: *FuncGen) Allocator.Error!Builder.Variable.Index {
6217 const o = self.object;
6218 const pt = self.pt;
6219
6220 const table = o.error_name_table;
6221 if (table != .none) return table;
6222
6223 // TODO: Address space
6224 const variable_index =
6225 try o.builder.addVariable(try o.builder.strtabString("__zig_err_name_table"), .ptr, .default);
6226 variable_index.setLinkage(.private, &o.builder);
6227 variable_index.setMutability(.constant, &o.builder);
6228 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
6229 variable_index.setAlignment(
6230 Type.slice_const_u8_sentinel_0.abiAlignment(pt.zcu).toLlvm(),
6231 &o.builder,
6232 );
6233
6234 o.error_name_table = variable_index;
6235 return variable_index;
6236}
6237
62386153/// Assumes that `Type.optionalReprIsPayload` is `false` for `opt_ty` and that the payload has bits.
62396154fn optCmpNull(
62406155 self: *FuncGen,
......@@ -6258,7 +6173,7 @@ fn optPayloadHandle(
62586173 opt_ptr: Builder.Value,
62596174 opt_ty: Type,
62606175 can_elide_load: bool,
6261) !Builder.Value {
6176) Allocator.Error!Builder.Value {
62626177 const pt = fg.pt;
62636178 const zcu = pt.zcu;
62646179 assert(isByRef(opt_ty, zcu));
......@@ -6281,7 +6196,7 @@ fn fieldPtr(
62816196 aggregate_ptr: Builder.Value,
62826197 aggregate_ptr_ty: Type,
62836198 field_index: u32,
6284) !Builder.Value {
6199) Allocator.Error!Builder.Value {
62856200 const pt = self.pt;
62866201 const zcu = pt.zcu;
62876202 const aggregate_ty = aggregate_ptr_ty.childType(zcu);
......@@ -6305,7 +6220,7 @@ fn loadTruncate(
63056220 payload_ty: Type,
63066221 payload_ptr: Builder.Value,
63076222 payload_alignment: Builder.Alignment,
6308) !Builder.Value {
6223) Allocator.Error!Builder.Value {
63096224 // from https://llvm.org/docs/LangRef.html#load-instruction :
63106225 // "When loading a value of a type like i20 with a size that is not an integral number of bytes, the result is undefined if the value was not originally written using a store of the same type. "
63116226 // => so load the byte aligned value and trunc the unwanted bits.
......@@ -6313,7 +6228,7 @@ fn loadTruncate(
63136228 const o = fg.object;
63146229 const pt = fg.pt;
63156230 const zcu = pt.zcu;
6316 const payload_llvm_ty = try o.lowerType(pt, payload_ty);
6231 const payload_llvm_ty = try fg.lowerType(payload_ty);
63176232 const abi_size = payload_ty.abiSize(zcu);
63186233
63196234 const load_llvm_ty = if (payload_ty.isAbiInt(zcu))
......@@ -6321,7 +6236,7 @@ fn loadTruncate(
63216236 else
63226237 payload_llvm_ty;
63236238 const loaded = try fg.wip.load(access_kind, load_llvm_ty, payload_ptr, payload_alignment, "");
6324 const shifted = if (payload_llvm_ty != load_llvm_ty and o.target.cpu.arch.endian() == .big)
6239 const shifted = if (payload_llvm_ty != load_llvm_ty and zcu.getTarget().cpu.arch.endian() == .big)
63256240 try fg.wip.bin(.lshr, loaded, try o.builder.intValue(
63266241 load_llvm_ty,
63276242 (payload_ty.abiSize(zcu) - (std.math.divCeil(u64, payload_ty.bitSize(zcu), 8) catch unreachable)) * 8,
......@@ -6339,10 +6254,10 @@ fn loadByRef(
63396254 pointee_type: Type,
63406255 ptr_alignment: Builder.Alignment,
63416256 access_kind: Builder.MemoryAccessKind,
6342) !Builder.Value {
6257) Allocator.Error!Builder.Value {
63436258 const o = fg.object;
63446259 const pt = fg.pt;
6345 const pointee_llvm_ty = try o.lowerType(pt, pointee_type);
6260 const pointee_llvm_ty = try fg.lowerType(pointee_type);
63466261 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)
63476262 .max(pointee_type.abiAlignment(pt.zcu)).toLlvm();
63486263 const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align);
......@@ -6352,7 +6267,7 @@ fn loadByRef(
63526267 result_align,
63536268 ptr,
63546269 ptr_alignment,
6355 try o.builder.intValue(try o.lowerType(pt, Type.usize), size_bytes),
6270 try o.builder.intValue(try fg.lowerType(.usize), size_bytes),
63566271 access_kind,
63576272 fg.disable_intrinsics,
63586273 );
......@@ -6362,7 +6277,7 @@ fn loadByRef(
63626277/// This function always performs a copy. For isByRef=true types, it creates a new
63636278/// alloca and copies the value into it, then returns the alloca instruction.
63646279/// For isByRef=false types, it creates a load instruction and returns it.
6365fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) !Builder.Value {
6280fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) Allocator.Error!Builder.Value {
63666281 const o = self.object;
63676282 const pt = self.pt;
63686283 const zcu = pt.zcu;
......@@ -6380,7 +6295,7 @@ fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) !Builder.Value {
63806295
63816296 if (info.flags.vector_index != .none) {
63826297 const index_u32 = try o.builder.intValue(.i32, info.flags.vector_index);
6383 const vec_elem_ty = try o.lowerType(pt, elem_ty);
6298 const vec_elem_ty = try self.lowerType(elem_ty);
63846299 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
63856300
63866301 const loaded_vector = try self.wip.load(access_kind, vec_ty, ptr, ptr_alignment, "");
......@@ -6401,7 +6316,7 @@ fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) !Builder.Value {
64016316 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
64026317 const shift_amt = try o.builder.intValue(containing_int_ty, info.packed_offset.bit_offset);
64036318 const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, "");
6404 const elem_llvm_ty = try o.lowerType(pt, elem_ty);
6319 const elem_llvm_ty = try self.lowerType(elem_ty);
64056320
64066321 if (isByRef(elem_ty, zcu)) {
64076322 const result_align = elem_ty.abiAlignment(zcu).toLlvm();
......@@ -6434,7 +6349,7 @@ fn store(
64346349 ptr_ty: Type,
64356350 elem: Builder.Value,
64366351 ordering: Builder.AtomicOrdering,
6437) !void {
6352) Allocator.Error!void {
64386353 const o = self.object;
64396354 const pt = self.pt;
64406355 const zcu = pt.zcu;
......@@ -6449,7 +6364,7 @@ fn store(
64496364
64506365 if (info.flags.vector_index != .none) {
64516366 const index_u32 = try o.builder.intValue(.i32, info.flags.vector_index);
6452 const vec_elem_ty = try o.lowerType(pt, elem_ty);
6367 const vec_elem_ty = try self.lowerType(elem_ty);
64536368 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
64546369
64556370 const loaded_vector = try self.wip.load(.normal, vec_ty, ptr, ptr_alignment, "");
......@@ -6518,7 +6433,7 @@ fn store(
65186433 ptr_alignment,
65196434 elem,
65206435 elem_ty.abiAlignment(zcu).toLlvm(),
6521 try o.builder.intValue(try o.lowerType(pt, Type.usize), elem_ty.abiSize(zcu)),
6436 try o.builder.intValue(try self.lowerType(Type.usize), elem_ty.abiSize(zcu)),
65226437 access_kind,
65236438 self.disable_intrinsics,
65246439 );
......@@ -6527,8 +6442,7 @@ fn store(
65276442fn valgrindMarkUndef(fg: *FuncGen, ptr: Builder.Value, len: Builder.Value) Allocator.Error!void {
65286443 const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
65296444 const o = fg.object;
6530 const pt = fg.pt;
6531 const usize_ty = try o.lowerType(pt, Type.usize);
6445 const usize_ty = try fg.lowerType(.usize);
65326446 const zero = try o.builder.intValue(usize_ty, 0);
65336447 const req = try o.builder.intValue(usize_ty, VG_USERREQ__MAKE_MEM_UNDEFINED);
65346448 const ptr_as_usize = try fg.wip.cast(.ptrtoint, ptr, usize_ty, "");
......@@ -6551,7 +6465,7 @@ fn valgrindClientRequest(
65516465 const target = zcu.getTarget();
65526466 if (!target_util.hasValgrindSupport(target, .stage2_llvm)) return default_value;
65536467
6554 const llvm_usize = try o.lowerType(pt, Type.usize);
6468 const llvm_usize = try fg.lowerType(.usize);
65556469 const usize_alignment = Type.usize.abiAlignment(zcu).toLlvm();
65566470
65576471 const array_llvm_ty = try o.builder.arrayType(6, llvm_usize);
......@@ -7360,11 +7274,12 @@ pub fn isByRef(ty: Type, zcu: *const Zcu) bool {
73607274 => false,
73617275
73627276 .array,
7363 .error_union,
73647277 .frame,
73657278 => ty.hasRuntimeBits(zcu),
73667279
7367 .optional => ty.hasRuntimeBits(zcu) and !ty.optionalReprIsPayload(zcu),
7280 .error_union => ty.errorUnionPayload(zcu).hasRuntimeBits(zcu),
7281
7282 .optional => !ty.optionalReprIsPayload(zcu) and ty.optionalChild(zcu).hasRuntimeBits(zcu),
73687283
73697284 .@"struct" => switch (ty.containerLayout(zcu)) {
73707285 .@"packed" => false,
......@@ -7402,25 +7317,19 @@ fn getAtomicAbiType(fg: *const FuncGen, ty: Type, is_rmw_xchg: bool) Allocator.E
74027317
74037318fn ptraddConst(fg: *FuncGen, ptr: Builder.Value, offset: u64) Allocator.Error!Builder.Value {
74047319 if (offset == 0) return ptr;
7405 const llvm_usize_ty = try fg.object.lowerType(fg.pt, .usize);
7320 const llvm_usize_ty = try fg.lowerType(.usize);
74067321 const offset_val = try fg.object.builder.intValue(llvm_usize_ty, offset);
7407 return fg.ptradd(ptr, offset_val);
7322 return fg.wip.gep(.inbounds, .i8, ptr, &.{offset_val}, "");
74087323}
74097324fn ptraddScaled(fg: *FuncGen, ptr: Builder.Value, index: Builder.Value, scale: u64) Allocator.Error!Builder.Value {
7410 switch (scale) {
7411 0 => return ptr,
7412 1 => return fg.ptradd(ptr, index),
7413 else => {
7414 const o = fg.object;
7415 const llvm_usize_ty = try o.lowerType(fg.pt, .usize);
7416 const scale_val = try o.builder.intValue(llvm_usize_ty, scale);
7417 const offset = try fg.wip.bin(.@"mul nuw", index, scale_val, "");
7418 return fg.ptradd(ptr, offset);
7419 },
7420 }
7421}
7422fn ptradd(fg: *FuncGen, ptr: Builder.Value, offset: Builder.Value) Allocator.Error!Builder.Value {
7423 return fg.wip.gep(.inbounds, .i8, ptr, &.{offset}, "");
7325 if (scale == 0) return ptr;
7326 // Right now LLVM seems to fare a bit worse with an explicit `mul nuw` instruction than it does
7327 // if we use a bigger type for the GEP, so we'll do that. As I understand it, it has not yet
7328 // been decided whether the planned `ptradd` instruction will accept a scale or not; if it does
7329 // not then presumably upstream will improve their handling of explicit `mul nuw` computing the
7330 // offset.
7331 const llvm_scale_ty = try fg.object.builder.arrayType(scale, .i8);
7332 return fg.wip.gep(.inbounds, llvm_scale_ty, ptr, &.{index}, "");
74247333}
74257334
74267335fn compilerRtIntBits(bits: u16) ?u16 {