authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-13 10:42:05+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-05-13 10:42:05+02:00
log5b606d435d139487c23d6fd30e8061dbad741ada
tree7c1cb233429d26ea85249b21f7cb7f80e8527476
parenta365971a337116dd23df2b1bc86468d54885b4b2
parent9d8adb38a18169a16707acac2812dd6850de99be
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21882 from alexrp/compiler-fixes

compiler: Fix some real and theoretical miscompilations with `allowzero` and `volatile`

12 files changed, 512 insertions(+), 88 deletions(-)

build.zig+5
......@@ -550,6 +550,11 @@ pub fn build(b: *std.Build) !void {
550550 .skip_non_native = skip_non_native,
551551 .skip_libc = skip_libc,
552552 })) |test_debugger_step| test_step.dependOn(test_debugger_step);
553 if (tests.addLlvmIrTests(b, .{
554 .enable_llvm = enable_llvm,
555 .test_filters = test_filters,
556 .test_target_filters = test_target_filters,
557 })) |test_llvm_ir_step| test_step.dependOn(test_llvm_ir_step);
553558
554559 try addWasiUpdateStep(b, version);
555560
lib/std/Build/Module.zig+4
......@@ -33,6 +33,7 @@ omit_frame_pointer: ?bool,
3333error_tracing: ?bool,
3434link_libc: ?bool,
3535link_libcpp: ?bool,
36no_builtin: ?bool,
3637
3738/// Symbols to be exported when compiling to WebAssembly.
3839export_symbol_names: []const []const u8 = &.{},
......@@ -268,6 +269,7 @@ pub const CreateOptions = struct {
268269 /// more difficult to obtain stack traces. Has target-dependent effects.
269270 omit_frame_pointer: ?bool = null,
270271 error_tracing: ?bool = null,
272 no_builtin: ?bool = null,
271273};
272274
273275pub const Import = struct {
......@@ -314,6 +316,7 @@ pub fn init(
314316 .omit_frame_pointer = options.omit_frame_pointer,
315317 .error_tracing = options.error_tracing,
316318 .export_symbol_names = &.{},
319 .no_builtin = options.no_builtin,
317320 };
318321
319322 m.import_table.ensureUnusedCapacity(allocator, options.imports.len) catch @panic("OOM");
......@@ -564,6 +567,7 @@ pub fn appendZigProcessFlags(
564567 try addFlag(zig_args, m.valgrind, "-fvalgrind", "-fno-valgrind");
565568 try addFlag(zig_args, m.pic, "-fPIC", "-fno-PIC");
566569 try addFlag(zig_args, m.red_zone, "-mred-zone", "-mno-red-zone");
570 try addFlag(zig_args, m.no_builtin, "-fno-builtin", "-fbuiltin");
567571
568572 if (m.sanitize_c) |sc| switch (sc) {
569573 .off => try zig_args.append("-fno-sanitize-c"),
lib/std/Build/Step/Compile.zig-6
......@@ -229,8 +229,6 @@ is_linking_libc: bool = false,
229229/// Computed during make().
230230is_linking_libcpp: bool = false,
231231
232no_builtin: bool = false,
233
234232/// Populated during the make phase when there is a long-lived compiler process.
235233/// Managed by the build runner, not user build script.
236234zig_process: ?*Step.ZigProcess,
......@@ -1646,10 +1644,6 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
16461644 }
16471645 }
16481646
1649 if (compile.no_builtin) {
1650 try zig_args.append("-fno-builtin");
1651 }
1652
16531647 if (b.sysroot) |sysroot| {
16541648 try zig_args.appendSlice(&[_][]const u8{ "--sysroot", sysroot });
16551649 }
src/Air.zig+7-15
......@@ -1673,6 +1673,7 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
16731673 const data = air.instructions.items(.data)[@intFromEnum(inst)];
16741674 return switch (air.instructions.items(.tag)[@intFromEnum(inst)]) {
16751675 .arg,
1676 .assembly,
16761677 .block,
16771678 .loop,
16781679 .repeat,
......@@ -1816,12 +1817,8 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
18161817 .cmp_vector_optimized,
18171818 .is_null,
18181819 .is_non_null,
1819 .is_null_ptr,
1820 .is_non_null_ptr,
18211820 .is_err,
18221821 .is_non_err,
1823 .is_err_ptr,
1824 .is_non_err_ptr,
18251822 .bool_and,
18261823 .bool_or,
18271824 .fptrunc,
......@@ -1834,7 +1831,6 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
18341831 .unwrap_errunion_payload,
18351832 .unwrap_errunion_err,
18361833 .unwrap_errunion_payload_ptr,
1837 .unwrap_errunion_err_ptr,
18381834 .wrap_errunion_payload,
18391835 .wrap_errunion_err,
18401836 .struct_field_ptr,
......@@ -1879,17 +1875,13 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
18791875 .work_group_id,
18801876 => false,
18811877
1882 .assembly => {
1883 const extra = air.extraData(Air.Asm, data.ty_pl.payload);
1884 const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0;
1885 return is_volatile or if (extra.data.outputs_len == 1)
1886 @as(Air.Inst.Ref, @enumFromInt(air.extra[extra.end])) != .none
1887 else
1888 extra.data.outputs_len > 1;
1889 },
1890 .load => air.typeOf(data.ty_op.operand, ip).isVolatilePtrIp(ip),
1878 .is_non_null_ptr, .is_null_ptr, .is_non_err_ptr, .is_err_ptr => air.typeOf(data.un_op, ip).isVolatilePtrIp(ip),
1879 .load, .unwrap_errunion_err_ptr => air.typeOf(data.ty_op.operand, ip).isVolatilePtrIp(ip),
18911880 .slice_elem_val, .ptr_elem_val => air.typeOf(data.bin_op.lhs, ip).isVolatilePtrIp(ip),
1892 .atomic_load => air.typeOf(data.atomic_load.ptr, ip).isVolatilePtrIp(ip),
1881 .atomic_load => switch (data.atomic_load.order) {
1882 .unordered, .monotonic => air.typeOf(data.atomic_load.ptr, ip).isVolatilePtrIp(ip),
1883 else => true, // Stronger memory orderings have inter-thread side effects.
1884 },
18931885 };
18941886}
18951887
src/arch/riscv64/CodeGen.zig+33-13
......@@ -1460,7 +1460,7 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
14601460
14611461 .mul,
14621462 .mul_wrap,
1463 .div_trunc,
1463 .div_trunc,
14641464 .div_exact,
14651465 .rem,
14661466
......@@ -1478,13 +1478,13 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
14781478 .max,
14791479 => try func.airBinOp(inst, tag),
14801480
1481
1481
14821482 .ptr_add,
14831483 .ptr_sub => try func.airPtrArithmetic(inst, tag),
14841484
14851485 .mod,
1486 .div_float,
1487 .div_floor,
1486 .div_float,
1487 .div_floor,
14881488 => return func.fail("TODO: {s}", .{@tagName(tag)}),
14891489
14901490 .sqrt,
......@@ -1639,7 +1639,7 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {
16391639 .ptr_slice_ptr_ptr => try func.airPtrSlicePtrPtr(inst),
16401640
16411641 .array_elem_val => try func.airArrayElemVal(inst),
1642
1642
16431643 .slice_elem_val => try func.airSliceElemVal(inst),
16441644 .slice_elem_ptr => try func.airSliceElemPtr(inst),
16451645
......@@ -1769,8 +1769,15 @@ fn finishAirBookkeeping(func: *Func) void {
17691769fn finishAirResult(func: *Func, inst: Air.Inst.Index, result: MCValue) void {
17701770 if (func.liveness.isUnused(inst)) switch (result) {
17711771 .none, .dead, .unreach => {},
1772 else => unreachable, // Why didn't the result die?
1772 // Why didn't the result die?
1773 .register => |r| if (r != .zero) unreachable,
1774 else => unreachable,
17731775 } else {
1776 switch (result) {
1777 .register => |r| if (r == .zero) unreachable, // Why did we discard a used result?
1778 else => {},
1779 }
1780
17741781 tracking_log.debug("%{d} => {} (birth)", .{ inst, result });
17751782 func.inst_tracking.putAssumeCapacityNoClobber(inst, InstTracking.init(result));
17761783 // In some cases, an operand may be reused as the result.
......@@ -7729,9 +7736,12 @@ fn airAtomicLoad(func: *Func, inst: Air.Inst.Index) !void {
77297736 const ptr_mcv = try func.resolveInst(atomic_load.ptr);
77307737
77317738 const bit_size = elem_ty.bitSize(zcu);
7732 if (bit_size > 64) return func.fail("TODO: airAtomicStore > 64 bits", .{});
7739 if (bit_size > 64) return func.fail("TODO: airAtomicLoad > 64 bits", .{});
77337740
7734 const result_mcv = try func.allocRegOrMem(elem_ty, inst, true);
7741 const result_mcv: MCValue = if (func.liveness.isUnused(inst))
7742 .{ .register = .zero }
7743 else
7744 try func.allocRegOrMem(elem_ty, inst, true);
77357745 assert(result_mcv == .register); // should be less than 8 bytes
77367746
77377747 if (order == .seq_cst) {
......@@ -7747,11 +7757,10 @@ fn airAtomicLoad(func: *Func, inst: Air.Inst.Index) !void {
77477757 try func.load(result_mcv, ptr_mcv, ptr_ty);
77487758
77497759 switch (order) {
7750 // Don't guarnetee other memory operations to be ordered after the load.
7751 .unordered => {},
7752 .monotonic => {},
7753 // Make sure all previous reads happen before any reading or writing accurs.
7754 .seq_cst, .acquire => {
7760 // Don't guarantee other memory operations to be ordered after the load.
7761 .unordered, .monotonic => {},
7762 // Make sure all previous reads happen before any reading or writing occurs.
7763 .acquire, .seq_cst => {
77557764 _ = try func.addInst(.{
77567765 .tag = .fence,
77577766 .data = .{ .fence = .{
......@@ -7793,6 +7802,17 @@ fn airAtomicStore(func: *Func, inst: Air.Inst.Index, order: std.builtin.AtomicOr
77937802 }
77947803
77957804 try func.store(ptr_mcv, val_mcv, ptr_ty);
7805
7806 if (order == .seq_cst) {
7807 _ = try func.addInst(.{
7808 .tag = .fence,
7809 .data = .{ .fence = .{
7810 .pred = .rw,
7811 .succ = .rw,
7812 } },
7813 });
7814 }
7815
77967816 return func.finishAir(inst, .unreach, .{ bin_op.lhs, bin_op.rhs, .none });
77977817}
77987818
src/arch/x86_64/CodeGen.zig+17-11
......@@ -106219,23 +106219,29 @@ fn airAtomicRmw(self: *CodeGen, inst: Air.Inst.Index) !void {
106219106219
106220106220fn airAtomicLoad(self: *CodeGen, inst: Air.Inst.Index) !void {
106221106221 const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
106222 const result: MCValue = result: {
106223 const ptr_ty = self.typeOf(atomic_load.ptr);
106224 const ptr_mcv = try self.resolveInst(atomic_load.ptr);
106225 const ptr_lock = switch (ptr_mcv) {
106226 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
106227 else => null,
106228 };
106229 defer if (ptr_lock) |lock| self.register_manager.unlockReg(lock);
106222106230
106223 const ptr_ty = self.typeOf(atomic_load.ptr);
106224 const ptr_mcv = try self.resolveInst(atomic_load.ptr);
106225 const ptr_lock = switch (ptr_mcv) {
106226 .register => |reg| self.register_manager.lockRegAssumeUnused(reg),
106227 else => null,
106228 };
106229 defer if (ptr_lock) |lock| self.register_manager.unlockReg(lock);
106231 const unused = self.liveness.isUnused(inst);
106230106232
106231 const dst_mcv =
106232 if (self.reuseOperand(inst, atomic_load.ptr, 0, ptr_mcv))
106233 const dst_mcv: MCValue = if (unused)
106234 .{ .register = try self.register_manager.allocReg(null, self.regSetForType(ptr_ty.childType(self.pt.zcu))) }
106235 else if (self.reuseOperand(inst, atomic_load.ptr, 0, ptr_mcv))
106233106236 ptr_mcv
106234106237 else
106235106238 try self.allocRegOrMem(inst, true);
106236106239
106237 try self.load(dst_mcv, ptr_ty, ptr_mcv);
106238 return self.finishAir(inst, dst_mcv, .{ atomic_load.ptr, .none, .none });
106240 try self.load(dst_mcv, ptr_ty, ptr_mcv);
106241
106242 break :result if (unused) .unreach else dst_mcv;
106243 };
106244 return self.finishAir(inst, result, .{ atomic_load.ptr, .none, .none });
106239106245}
106240106246
106241106247fn airAtomicStore(self: *CodeGen, inst: Air.Inst.Index, order: std.builtin.AtomicOrder) !void {
src/codegen/llvm.zig+164-38
......@@ -1341,7 +1341,10 @@ pub const Object = struct {
13411341 try attributes.addParamAttr(llvm_arg_i, .@"noalias", &o.builder);
13421342 }
13431343 }
1344 if (param_ty.zigTypeTag(zcu) != .optional) {
1344 if (param_ty.zigTypeTag(zcu) != .optional and
1345 !ptr_info.flags.is_allowzero and
1346 ptr_info.flags.address_space == .generic)
1347 {
13451348 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);
13461349 }
13471350 if (ptr_info.flags.is_const) {
......@@ -1419,8 +1422,6 @@ pub const Object = struct {
14191422 }
14201423 }
14211424
1422 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
1423
14241425 const file, const subprogram = if (!wip.strip) debug_info: {
14251426 const file = try o.getDebugFile(file_scope);
14261427
......@@ -1517,6 +1518,17 @@ pub const Object = struct {
15171518 else => |e| return e,
15181519 };
15191520
1521 // If we saw any loads or stores involving `allowzero` pointers, we need to mark the whole
1522 // function as considering null pointers valid so that LLVM's optimizers don't remove these
1523 // operations on the assumption that they're undefined behavior.
1524 if (fg.allowzero_access) {
1525 try attributes.addFnAttr(.null_pointer_is_valid, &o.builder);
1526 } else {
1527 _ = try attributes.removeFnAttr(.null_pointer_is_valid);
1528 }
1529
1530 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
1531
15201532 if (fg.fuzz) |*f| {
15211533 {
15221534 const array_llvm_ty = try o.builder.arrayType(f.pcs.items.len, .i8);
......@@ -4349,7 +4361,10 @@ pub const Object = struct {
43494361 try attributes.addParamAttr(llvm_arg_i, .@"noalias", &o.builder);
43504362 }
43514363 }
4352 if (!param_ty.isPtrLikeOptional(zcu) and !ptr_info.flags.is_allowzero) {
4364 if (!param_ty.isPtrLikeOptional(zcu) and
4365 !ptr_info.flags.is_allowzero and
4366 ptr_info.flags.address_space == .generic)
4367 {
43534368 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);
43544369 }
43554370 switch (fn_info.cc) {
......@@ -4667,6 +4682,15 @@ pub const FuncGen = struct {
46674682
46684683 disable_intrinsics: bool,
46694684
4685 /// Have we seen loads or stores involving `allowzero` pointers?
4686 allowzero_access: bool = false,
4687
4688 pub fn maybeMarkAllowZeroAccess(self: *FuncGen, info: InternPool.Key.PtrType) void {
4689 // LLVM already considers null pointers to be valid in non-generic address spaces, so avoid
4690 // pessimizing optimization for functions with accesses to such pointers.
4691 if (info.flags.address_space == .generic and info.flags.is_allowzero) self.allowzero_access = true;
4692 }
4693
46704694 const Fuzz = struct {
46714695 counters_variable: Builder.Variable.Index,
46724696 pcs: std.ArrayListUnmanaged(Builder.Constant),
......@@ -5392,7 +5416,10 @@ pub const FuncGen = struct {
53925416 try attributes.addParamAttr(llvm_arg_i, .@"noalias", &o.builder);
53935417 }
53945418 }
5395 if (param_ty.zigTypeTag(zcu) != .optional) {
5419 if (param_ty.zigTypeTag(zcu) != .optional and
5420 !ptr_info.flags.is_allowzero and
5421 ptr_info.flags.address_space == .generic)
5422 {
53965423 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);
53975424 }
53985425 if (ptr_info.flags.is_const) {
......@@ -5519,7 +5546,7 @@ pub const FuncGen = struct {
55195546 ptr_ty.ptrAlignment(zcu).toLlvm(),
55205547 try o.builder.intValue(.i8, 0xaa),
55215548 len,
5522 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
5549 .normal,
55235550 self.disable_intrinsics,
55245551 );
55255552 const owner_mod = self.ng.ownerModule();
......@@ -5754,8 +5781,8 @@ pub const FuncGen = struct {
57545781 // of optionals that are not pointers.
57555782 const is_by_ref = isByRef(scalar_ty, zcu);
57565783 const opt_llvm_ty = try o.lowerType(scalar_ty);
5757 const lhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, lhs, is_by_ref);
5758 const rhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, rhs, is_by_ref);
5784 const lhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, lhs, is_by_ref, .normal);
5785 const rhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, rhs, is_by_ref, .normal);
57595786 const llvm_i2 = try o.builder.intType(2);
57605787 const lhs_non_null_i2 = try self.wip.cast(.zext, lhs_non_null, llvm_i2, "");
57615788 const rhs_non_null_i2 = try self.wip.cast(.zext, rhs_non_null, llvm_i2, "");
......@@ -6206,6 +6233,9 @@ pub const FuncGen = struct {
62066233 const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]);
62076234 const err_union_ty = self.typeOf(extra.data.ptr).childType(zcu);
62086235 const is_unused = self.liveness.isUnused(inst);
6236
6237 self.maybeMarkAllowZeroAccess(self.typeOf(extra.data.ptr).ptrInfo(zcu));
6238
62096239 return lowerTry(self, err_union_ptr, body, err_union_ty, true, true, is_unused, err_cold);
62106240 }
62116241
......@@ -6229,10 +6259,13 @@ pub const FuncGen = struct {
62296259
62306260 if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
62316261 const loaded = loaded: {
6262 const access_kind: Builder.MemoryAccessKind =
6263 if (err_union_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
6264
62326265 if (!payload_has_bits) {
62336266 // TODO add alignment to this load
62346267 break :loaded if (operand_is_ptr)
6235 try fg.wip.load(.normal, error_type, err_union, .default, "")
6268 try fg.wip.load(access_kind, error_type, err_union, .default, "")
62366269 else
62376270 err_union;
62386271 }
......@@ -6242,7 +6275,7 @@ pub const FuncGen = struct {
62426275 try fg.wip.gepStruct(err_union_llvm_ty, err_union, err_field_index, "");
62436276 // TODO add alignment to this load
62446277 break :loaded try fg.wip.load(
6245 .normal,
6278 if (operand_is_ptr) access_kind else .normal,
62466279 error_type,
62476280 err_field_ptr,
62486281 .default,
......@@ -6751,10 +6784,14 @@ pub const FuncGen = struct {
67516784 if (self.canElideLoad(body_tail))
67526785 return ptr;
67536786
6787 self.maybeMarkAllowZeroAccess(slice_ty.ptrInfo(zcu));
6788
67546789 const elem_alignment = elem_ty.abiAlignment(zcu).toLlvm();
6755 return self.loadByRef(ptr, elem_ty, elem_alignment, .normal);
6790 return self.loadByRef(ptr, elem_ty, elem_alignment, if (slice_ty.isVolatilePtr(zcu)) .@"volatile" else .normal);
67566791 }
67576792
6793 self.maybeMarkAllowZeroAccess(slice_ty.ptrInfo(zcu));
6794
67586795 return self.load(ptr, slice_ty);
67596796 }
67606797
......@@ -6824,10 +6861,15 @@ pub const FuncGen = struct {
68246861 &.{rhs}, "");
68256862 if (isByRef(elem_ty, zcu)) {
68266863 if (self.canElideLoad(body_tail)) return ptr;
6864
6865 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
6866
68276867 const elem_alignment = elem_ty.abiAlignment(zcu).toLlvm();
6828 return self.loadByRef(ptr, elem_ty, elem_alignment, .normal);
6868 return self.loadByRef(ptr, elem_ty, elem_alignment, if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal);
68296869 }
68306870
6871 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
6872
68316873 return self.load(ptr, ptr_ty);
68326874 }
68336875
......@@ -7235,6 +7277,8 @@ pub const FuncGen = struct {
72357277 }),
72367278 }
72377279
7280 self.maybeMarkAllowZeroAccess(output_ty.ptrInfo(zcu));
7281
72387282 // Pass any non-return outputs indirectly, if the constraint accepts a memory location
72397283 is_indirect.* = constraintAllowsMemory(constraint);
72407284 if (is_indirect.*) {
......@@ -7341,10 +7385,11 @@ pub const FuncGen = struct {
73417385
73427386 // In the case of indirect inputs, LLVM requires the callsite to have
73437387 // an elementtype(<ty>) attribute.
7344 llvm_param_attrs[llvm_param_i] = if (constraint[0] == '*')
7345 try o.lowerPtrElemTy(if (is_by_ref) arg_ty else arg_ty.childType(zcu))
7346 else
7347 .none;
7388 llvm_param_attrs[llvm_param_i] = if (constraint[0] == '*') blk: {
7389 if (!is_by_ref) self.maybeMarkAllowZeroAccess(arg_ty.ptrInfo(zcu));
7390
7391 break :blk try o.lowerPtrElemTy(if (is_by_ref) arg_ty else arg_ty.childType(zcu));
7392 } else .none;
73487393
73497394 llvm_param_i += 1;
73507395 total_i += 1;
......@@ -7367,7 +7412,13 @@ pub const FuncGen = struct {
73677412 llvm_param_types[llvm_param_i] = llvm_rw_val.typeOfWip(&self.wip);
73687413 } else {
73697414 const alignment = rw_ty.abiAlignment(zcu).toLlvm();
7370 const loaded = try self.wip.load(.normal, llvm_elem_ty, llvm_rw_val, alignment, "");
7415 const loaded = try self.wip.load(
7416 if (rw_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
7417 llvm_elem_ty,
7418 llvm_rw_val,
7419 alignment,
7420 "",
7421 );
73717422 llvm_param_values[llvm_param_i] = loaded;
73727423 llvm_param_types[llvm_param_i] = llvm_elem_ty;
73737424 }
......@@ -7530,9 +7581,13 @@ pub const FuncGen = struct {
75307581 if (output != .none) {
75317582 const output_ptr = try self.resolveInst(output);
75327583 const output_ptr_ty = self.typeOf(output);
7533
75347584 const alignment = output_ptr_ty.ptrAlignment(zcu).toLlvm();
7535 _ = try self.wip.store(.normal, output_value, output_ptr, alignment);
7585 _ = try self.wip.store(
7586 if (output_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
7587 output_value,
7588 output_ptr,
7589 alignment,
7590 );
75367591 } else {
75377592 ret_val = output_value;
75387593 }
......@@ -7557,9 +7612,15 @@ pub const FuncGen = struct {
75577612 const optional_ty = if (operand_is_ptr) operand_ty.childType(zcu) else operand_ty;
75587613 const optional_llvm_ty = try o.lowerType(optional_ty);
75597614 const payload_ty = optional_ty.optionalChild(zcu);
7615
7616 const access_kind: Builder.MemoryAccessKind =
7617 if (operand_is_ptr and operand_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7618
7619 if (operand_is_ptr) self.maybeMarkAllowZeroAccess(operand_ty.ptrInfo(zcu));
7620
75607621 if (optional_ty.optionalReprIsPayload(zcu)) {
75617622 const loaded = if (operand_is_ptr)
7562 try self.wip.load(.normal, optional_llvm_ty, operand, .default, "")
7623 try self.wip.load(access_kind, optional_llvm_ty, operand, .default, "")
75637624 else
75647625 operand;
75657626 if (payload_ty.isSlice(zcu)) {
......@@ -7577,14 +7638,14 @@ pub const FuncGen = struct {
75777638
75787639 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
75797640 const loaded = if (operand_is_ptr)
7580 try self.wip.load(.normal, optional_llvm_ty, operand, .default, "")
7641 try self.wip.load(access_kind, optional_llvm_ty, operand, .default, "")
75817642 else
75827643 operand;
75837644 return self.wip.icmp(cond, loaded, try o.builder.intValue(.i8, 0), "");
75847645 }
75857646
75867647 const is_by_ref = operand_is_ptr or isByRef(optional_ty, zcu);
7587 return self.optCmpNull(cond, optional_llvm_ty, operand, is_by_ref);
7648 return self.optCmpNull(cond, optional_llvm_ty, operand, is_by_ref, access_kind);
75887649 }
75897650
75907651 fn airIsErr(
......@@ -7604,6 +7665,9 @@ pub const FuncGen = struct {
76047665 const error_type = try o.errorIntType();
76057666 const zero = try o.builder.intValue(error_type, 0);
76067667
7668 const access_kind: Builder.MemoryAccessKind =
7669 if (operand_is_ptr and operand_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7670
76077671 if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
76087672 const val: Builder.Constant = switch (cond) {
76097673 .eq => .true, // 0 == 0
......@@ -7613,9 +7677,11 @@ pub const FuncGen = struct {
76137677 return val.toValue();
76147678 }
76157679
7680 if (operand_is_ptr) self.maybeMarkAllowZeroAccess(operand_ty.ptrInfo(zcu));
7681
76167682 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
76177683 const loaded = if (operand_is_ptr)
7618 try self.wip.load(.normal, try o.lowerType(err_union_ty), operand, .default, "")
7684 try self.wip.load(access_kind, try o.lowerType(err_union_ty), operand, .default, "")
76197685 else
76207686 operand;
76217687 return self.wip.icmp(cond, loaded, zero, "");
......@@ -7627,7 +7693,7 @@ pub const FuncGen = struct {
76277693 const err_union_llvm_ty = try o.lowerType(err_union_ty);
76287694 const err_field_ptr =
76297695 try self.wip.gepStruct(err_union_llvm_ty, operand, err_field_index, "");
7630 break :loaded try self.wip.load(.normal, error_type, err_field_ptr, .default, "");
7696 break :loaded try self.wip.load(access_kind, error_type, err_field_ptr, .default, "");
76317697 } else try self.wip.extractValue(operand, &.{err_field_index}, "");
76327698 return self.wip.icmp(cond, loaded, zero, "");
76337699 }
......@@ -7660,12 +7726,19 @@ pub const FuncGen = struct {
76607726 const zcu = pt.zcu;
76617727 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
76627728 const operand = try self.resolveInst(ty_op.operand);
7663 const optional_ty = self.typeOf(ty_op.operand).childType(zcu);
7729 const optional_ptr_ty = self.typeOf(ty_op.operand);
7730 const optional_ty = optional_ptr_ty.childType(zcu);
76647731 const payload_ty = optional_ty.optionalChild(zcu);
76657732 const non_null_bit = try o.builder.intValue(.i8, 1);
7733
7734 const access_kind: Builder.MemoryAccessKind =
7735 if (optional_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7736
76667737 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
7738 self.maybeMarkAllowZeroAccess(optional_ptr_ty.ptrInfo(zcu));
7739
76677740 // We have a pointer to a i8. We need to set it to 1 and then return the same pointer.
7668 _ = try self.wip.store(.normal, non_null_bit, operand, .default);
7741 _ = try self.wip.store(access_kind, non_null_bit, operand, .default);
76697742 return operand;
76707743 }
76717744 if (optional_ty.optionalReprIsPayload(zcu)) {
......@@ -7677,8 +7750,11 @@ pub const FuncGen = struct {
76777750 // First set the non-null bit.
76787751 const optional_llvm_ty = try o.lowerType(optional_ty);
76797752 const non_null_ptr = try self.wip.gepStruct(optional_llvm_ty, operand, 1, "");
7753
7754 self.maybeMarkAllowZeroAccess(optional_ptr_ty.ptrInfo(zcu));
7755
76807756 // TODO set alignment on this store
7681 _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default);
7757 _ = try self.wip.store(access_kind, non_null_bit, non_null_ptr, .default);
76827758
76837759 // Then return the payload pointer (only if it's used).
76847760 if (self.liveness.isUnused(inst)) return .none;
......@@ -7764,18 +7840,26 @@ pub const FuncGen = struct {
77647840 }
77657841 }
77667842
7843 const access_kind: Builder.MemoryAccessKind =
7844 if (operand_is_ptr and operand_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7845
77677846 const payload_ty = err_union_ty.errorUnionPayload(zcu);
77687847 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
77697848 if (!operand_is_ptr) return operand;
7770 return self.wip.load(.normal, error_type, operand, .default, "");
7849
7850 self.maybeMarkAllowZeroAccess(operand_ty.ptrInfo(zcu));
7851
7852 return self.wip.load(access_kind, error_type, operand, .default, "");
77717853 }
77727854
77737855 const offset = try errUnionErrorOffset(payload_ty, pt);
77747856
77757857 if (operand_is_ptr or isByRef(err_union_ty, zcu)) {
7858 if (operand_is_ptr) self.maybeMarkAllowZeroAccess(operand_ty.ptrInfo(zcu));
7859
77767860 const err_union_llvm_ty = try o.lowerType(err_union_ty);
77777861 const err_field_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, "");
7778 return self.wip.load(.normal, error_type, err_field_ptr, .default, "");
7862 return self.wip.load(access_kind, error_type, err_field_ptr, .default, "");
77797863 }
77807864
77817865 return self.wip.extractValue(operand, &.{offset}, "");
......@@ -7787,22 +7871,31 @@ pub const FuncGen = struct {
77877871 const zcu = pt.zcu;
77887872 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
77897873 const operand = try self.resolveInst(ty_op.operand);
7790 const err_union_ty = self.typeOf(ty_op.operand).childType(zcu);
7874 const err_union_ptr_ty = self.typeOf(ty_op.operand);
7875 const err_union_ty = err_union_ptr_ty.childType(zcu);
77917876
77927877 const payload_ty = err_union_ty.errorUnionPayload(zcu);
77937878 const non_error_val = try o.builder.intValue(try o.errorIntType(), 0);
7879
7880 const access_kind: Builder.MemoryAccessKind =
7881 if (err_union_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7882
77947883 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
7795 _ = try self.wip.store(.normal, non_error_val, operand, .default);
7884 self.maybeMarkAllowZeroAccess(err_union_ptr_ty.ptrInfo(zcu));
7885
7886 _ = try self.wip.store(access_kind, non_error_val, operand, .default);
77967887 return operand;
77977888 }
77987889 const err_union_llvm_ty = try o.lowerType(err_union_ty);
77997890 {
7891 self.maybeMarkAllowZeroAccess(err_union_ptr_ty.ptrInfo(zcu));
7892
78007893 const err_int_ty = try pt.errorIntType();
78017894 const error_alignment = err_int_ty.abiAlignment(zcu).toLlvm();
78027895 const error_offset = try errUnionErrorOffset(payload_ty, pt);
78037896 // First set the non-error value.
78047897 const non_null_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, error_offset, "");
7805 _ = try self.wip.store(.normal, non_error_val, non_null_ptr, error_alignment);
7898 _ = try self.wip.store(access_kind, non_error_val, non_null_ptr, error_alignment);
78067899 }
78077900 // Then return the payload pointer (only if it is used).
78087901 if (self.liveness.isUnused(inst)) return .none;
......@@ -8017,6 +8110,10 @@ pub const FuncGen = struct {
80178110 const index = try self.resolveInst(extra.lhs);
80188111 const operand = try self.resolveInst(extra.rhs);
80198112
8113 self.maybeMarkAllowZeroAccess(vector_ptr_ty.ptrInfo(zcu));
8114
8115 // TODO: Emitting a load here is a violation of volatile semantics. Not fixable in general.
8116 // https://github.com/ziglang/zig/issues/18652#issuecomment-2452844908
80208117 const access_kind: Builder.MemoryAccessKind =
80218118 if (vector_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
80228119 const elem_llvm_ty = try o.lowerType(vector_ptr_ty.childType(zcu));
......@@ -9482,6 +9579,8 @@ pub const FuncGen = struct {
94829579 return .none;
94839580 }
94849581
9582 self.maybeMarkAllowZeroAccess(ptr_info);
9583
94859584 const len = try o.builder.intValue(try o.lowerType(Type.usize), operand_ty.abiSize(zcu));
94869585 _ = try self.wip.callMemSet(
94879586 dest_ptr,
......@@ -9497,6 +9596,8 @@ pub const FuncGen = struct {
94979596 return .none;
94989597 }
94999598
9599 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
9600
95009601 const src_operand = try self.resolveInst(bin_op.rhs);
95019602 try self.store(dest_ptr, ptr_ty, src_operand, .none);
95029603 return .none;
......@@ -9539,6 +9640,9 @@ pub const FuncGen = struct {
95399640 if (!canElideLoad(fg, body_tail)) break :elide;
95409641 return ptr;
95419642 }
9643
9644 fg.maybeMarkAllowZeroAccess(ptr_info);
9645
95429646 return fg.load(ptr, ptr_ty);
95439647 }
95449648
......@@ -9598,6 +9702,8 @@ pub const FuncGen = struct {
95989702 new_value = try self.wip.conv(signedness, new_value, llvm_abi_ty, "");
95999703 }
96009704
9705 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
9706
96019707 const result = try self.wip.cmpxchg(
96029708 kind,
96039709 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
......@@ -9649,6 +9755,8 @@ pub const FuncGen = struct {
96499755 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
96509756 const ptr_alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
96519757
9758 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
9759
96529760 if (llvm_abi_ty != .none) {
96539761 // operand needs widening and truncating or bitcasting.
96549762 return self.wip.cast(if (is_float) .bitcast else .trunc, try self.wip.atomicrmw(
......@@ -9712,6 +9820,8 @@ pub const FuncGen = struct {
97129820 if (info.flags.is_volatile) .@"volatile" else .normal;
97139821 const elem_llvm_ty = try o.lowerType(elem_ty);
97149822
9823 self.maybeMarkAllowZeroAccess(info);
9824
97159825 if (llvm_abi_ty != .none) {
97169826 // operand needs widening and truncating
97179827 const loaded = try self.wip.loadAtomic(
......@@ -9761,6 +9871,9 @@ pub const FuncGen = struct {
97619871 "",
97629872 );
97639873 }
9874
9875 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
9876
97649877 try self.store(ptr, ptr_ty, element, ordering);
97659878 return .none;
97669879 }
......@@ -9778,6 +9891,8 @@ pub const FuncGen = struct {
97789891 const access_kind: Builder.MemoryAccessKind =
97799892 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
97809893
9894 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
9895
97819896 if (try self.air.value(bin_op.rhs, pt)) |elem_val| {
97829897 if (elem_val.isUndefDeep(zcu)) {
97839898 // Even if safety is disabled, we still emit a memset to undefined since it conveys
......@@ -9916,6 +10031,9 @@ pub const FuncGen = struct {
991610031 const access_kind: Builder.MemoryAccessKind = if (src_ptr_ty.isVolatilePtr(zcu) or
991710032 dest_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
991810033
10034 self.maybeMarkAllowZeroAccess(dest_ptr_ty.ptrInfo(zcu));
10035 self.maybeMarkAllowZeroAccess(src_ptr_ty.ptrInfo(zcu));
10036
991910037 _ = try self.wip.callMemCpy(
992010038 dest_ptr,
992110039 dest_ptr_ty.ptrAlignment(zcu).toLlvm(),
......@@ -9959,20 +10077,27 @@ pub const FuncGen = struct {
995910077 const pt = o.pt;
996010078 const zcu = pt.zcu;
996110079 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
9962 const un_ty = self.typeOf(bin_op.lhs).childType(zcu);
10080 const un_ptr_ty = self.typeOf(bin_op.lhs);
10081 const un_ty = un_ptr_ty.childType(zcu);
996310082 const layout = un_ty.unionGetLayout(zcu);
996410083 if (layout.tag_size == 0) return .none;
10084
10085 const access_kind: Builder.MemoryAccessKind =
10086 if (un_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
10087
10088 self.maybeMarkAllowZeroAccess(un_ptr_ty.ptrInfo(zcu));
10089
996510090 const union_ptr = try self.resolveInst(bin_op.lhs);
996610091 const new_tag = try self.resolveInst(bin_op.rhs);
996710092 if (layout.payload_size == 0) {
996810093 // TODO alignment on this store
9969 _ = try self.wip.store(.normal, new_tag, union_ptr, .default);
10094 _ = try self.wip.store(access_kind, new_tag, union_ptr, .default);
997010095 return .none;
997110096 }
997210097 const tag_index = @intFromBool(layout.tag_align.compare(.lt, layout.payload_align));
997310098 const tag_field_ptr = try self.wip.gepStruct(try o.lowerType(un_ty), union_ptr, tag_index, "");
997410099 // TODO alignment on this store
9975 _ = try self.wip.store(.normal, new_tag, tag_field_ptr, .default);
10100 _ = try self.wip.store(access_kind, new_tag, tag_field_ptr, .default);
997610101 return .none;
997710102 }
997810103
......@@ -10869,12 +10994,13 @@ pub const FuncGen = struct {
1086910994 opt_llvm_ty: Builder.Type,
1087010995 opt_handle: Builder.Value,
1087110996 is_by_ref: bool,
10997 access_kind: Builder.MemoryAccessKind,
1087210998 ) Allocator.Error!Builder.Value {
1087310999 const o = self.ng.object;
1087411000 const field = b: {
1087511001 if (is_by_ref) {
1087611002 const field_ptr = try self.wip.gepStruct(opt_llvm_ty, opt_handle, 1, "");
10877 break :b try self.wip.load(.normal, .i8, field_ptr, .default, "");
11003 break :b try self.wip.load(access_kind, .i8, field_ptr, .default, "");
1087811004 }
1087911005 break :b try self.wip.extractValue(opt_handle, &.{1}, "");
1088011006 };
......@@ -11183,7 +11309,7 @@ pub const FuncGen = struct {
1118311309 const vec_elem_ty = try o.lowerType(elem_ty);
1118411310 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
1118511311
11186 const loaded_vector = try self.wip.load(access_kind, vec_ty, ptr, ptr_alignment, "");
11312 const loaded_vector = try self.wip.load(.normal, vec_ty, ptr, ptr_alignment, "");
1118711313
1118811314 const modified_vector = try self.wip.insertElement(loaded_vector, elem, index_u32, "");
1118911315
......@@ -11196,7 +11322,7 @@ pub const FuncGen = struct {
1119611322 const containing_int_ty = try o.builder.intType(@intCast(info.packed_offset.host_size * 8));
1119711323 assert(ordering == .none);
1119811324 const containing_int =
11199 try self.wip.load(access_kind, containing_int_ty, ptr, ptr_alignment, "");
11325 try self.wip.load(.normal, containing_int_ty, ptr, ptr_alignment, "");
1120011326 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
1120111327 const shift_amt = try o.builder.intConst(containing_int_ty, info.packed_offset.bit_offset);
1120211328 // Convert to equally-sized integer type in order to perform the bit
src/main.zig+2-2
......@@ -471,8 +471,6 @@ const usage_build_generic =
471471 \\ -fno-dll-export-fns Force-disable marking exported functions as DLL exports
472472 \\ -freference-trace[=num] Show num lines of reference trace per compile error
473473 \\ -fno-reference-trace Disable reference trace
474 \\ -fbuiltin Enable implicit builtin knowledge of functions
475 \\ -fno-builtin Disable implicit builtin knowledge of functions
476474 \\ -ffunction-sections Places each function in a separate section
477475 \\ -fno-function-sections All functions go into same section
478476 \\ -fdata-sections Places each data in a separate section
......@@ -534,6 +532,8 @@ const usage_build_generic =
534532 \\ -fno-sanitize-thread Disable Thread Sanitizer
535533 \\ -ffuzz Enable fuzz testing instrumentation
536534 \\ -fno-fuzz Disable fuzz testing instrumentation
535 \\ -fbuiltin Enable implicit builtin knowledge of functions
536 \\ -fno-builtin Disable implicit builtin knowledge of functions
537537 \\ -funwind-tables Always produce unwind table entries for all functions
538538 \\ -fasync-unwind-tables Always produce asynchronous unwind table entries for all functions
539539 \\ -fno-unwind-tables Never produce unwind table entries
test/llvm_ir.zig created+122
......@@ -0,0 +1,122 @@
1pub fn addCases(cases: *tests.LlvmIrContext) void {
2 cases.addMatches("nonnull ptr load",
3 \\export fn entry(ptr: *i16) i16 {
4 \\ return ptr.*;
5 \\}
6 , &.{
7 "ptr nonnull",
8 "load i16, ptr %0",
9 }, .{});
10
11 cases.addMatches("nonnull ptr store",
12 \\export fn entry(ptr: *i16) void {
13 \\ ptr.* = 42;
14 \\}
15 , &.{
16 "ptr nonnull",
17 "store i16 42, ptr %0",
18 }, .{});
19
20 cases.addMatches("unused acquire atomic ptr load",
21 \\export fn entry(ptr: *i16) void {
22 \\ _ = @atomicLoad(i16, ptr, .acquire);
23 \\}
24 , &.{
25 "load atomic i16, ptr %0 acquire",
26 }, .{});
27
28 cases.addMatches("unused unordered atomic volatile ptr load",
29 \\export fn entry(ptr: *volatile i16) void {
30 \\ _ = @atomicLoad(i16, ptr, .unordered);
31 \\}
32 , &.{
33 "load atomic volatile i16, ptr %0 unordered",
34 }, .{});
35
36 cases.addMatches("unused volatile ptr load",
37 \\export fn entry(ptr: *volatile i16) void {
38 \\ _ = ptr.*;
39 \\}
40 , &.{
41 "load volatile i16, ptr %0",
42 }, .{});
43
44 cases.addMatches("dead volatile ptr store",
45 \\export fn entry(ptr: *volatile i16) void {
46 \\ ptr.* = 123;
47 \\ ptr.* = 321;
48 \\}
49 , &.{
50 "store volatile i16 123, ptr %0",
51 "store volatile i16 321, ptr %0",
52 }, .{});
53
54 cases.addMatches("unused volatile slice load",
55 \\export fn entry(ptr: *volatile i16) void {
56 \\ entry2(ptr[0..1]);
57 \\}
58 \\fn entry2(ptr: []volatile i16) void {
59 \\ _ = ptr[0];
60 \\}
61 , &.{
62 "load volatile i16, ptr",
63 }, .{});
64
65 cases.addMatches("dead volatile slice store",
66 \\export fn entry(ptr: *volatile i16) void {
67 \\ entry2(ptr[0..1]);
68 \\}
69 \\fn entry2(ptr: []volatile i16) void {
70 \\ ptr[0] = 123;
71 \\ ptr[0] = 321;
72 \\}
73 , &.{
74 "store volatile i16 123, ptr",
75 "store volatile i16 321, ptr",
76 }, .{});
77
78 cases.addMatches("allowzero ptr load",
79 \\export fn entry(ptr: *allowzero i16) i16 {
80 \\ return ptr.*;
81 \\}
82 , &.{
83 "null_pointer_is_valid",
84 "load i16, ptr %0",
85 }, .{});
86
87 cases.addMatches("allowzero ptr store",
88 \\export fn entry(ptr: *allowzero i16) void {
89 \\ ptr.* = 42;
90 \\}
91 , &.{
92 "null_pointer_is_valid",
93 "store i16 42, ptr %0",
94 }, .{});
95
96 cases.addMatches("allowzero slice load",
97 \\export fn entry(ptr: *allowzero i16) i16 {
98 \\ return entry2(ptr[0..1]);
99 \\}
100 \\fn entry2(ptr: []allowzero i16) i16 {
101 \\ return ptr[0];
102 \\}
103 , &.{
104 "null_pointer_is_valid",
105 "load i16, ptr",
106 }, .{});
107
108 cases.addMatches("allowzero slice store",
109 \\export fn entry(ptr: *allowzero i16) void {
110 \\ entry2(ptr[0..1]);
111 \\}
112 \\fn entry2(ptr: []allowzero i16) void {
113 \\ ptr[0] = 42;
114 \\}
115 , &.{
116 "null_pointer_is_valid",
117 "store i16 42, ptr",
118 }, .{});
119}
120
121const std = @import("std");
122const tests = @import("tests.zig");
test/src/Debugger.zig+4-2
......@@ -2442,8 +2442,10 @@ fn addTest(
24422442 db_argv2: []const []const u8,
24432443 expected_output: []const []const u8,
24442444) void {
2445 for (db.options.test_filters) |test_filter| {
2446 if (std.mem.indexOf(u8, name, test_filter)) |_| return;
2445 if (db.options.test_filters.len > 0) {
2446 for (db.options.test_filters) |test_filter| {
2447 if (std.mem.indexOf(u8, name, test_filter) != null) break;
2448 } else return;
24472449 }
24482450 if (db.options.test_target_filters.len > 0) {
24492451 const triple_txt = target.resolved.result.zigTriple(db.b.allocator) catch @panic("OOM");
test/src/LlvmIr.zig created+132
......@@ -0,0 +1,132 @@
1b: *std.Build,
2options: Options,
3root_step: *std.Build.Step,
4
5pub const Options = struct {
6 enable_llvm: bool,
7 test_filters: []const []const u8,
8 test_target_filters: []const []const u8,
9};
10
11const TestCase = struct {
12 name: []const u8,
13 source: []const u8,
14 check: union(enum) {
15 matches: []const []const u8,
16 exact: []const u8,
17 },
18 params: Params,
19
20 pub const Params = struct {
21 code_model: std.builtin.CodeModel = .default,
22 dll_export_fns: ?bool = null,
23 dwarf_format: ?std.dwarf.Format = null,
24 error_tracing: ?bool = null,
25 no_builtin: ?bool = null,
26 omit_frame_pointer: ?bool = null,
27 // For most cases, we want to test the LLVM IR that we output; we don't want to be in the
28 // business of testing LLVM's optimization passes. `Debug` gets us the closest to that as it
29 // disables the vast majority of passes in LLVM.
30 optimize: std.builtin.OptimizeMode = .Debug,
31 pic: ?bool = null,
32 pie: ?bool = null,
33 red_zone: ?bool = null,
34 sanitize_thread: ?bool = null,
35 single_threaded: ?bool = null,
36 stack_check: ?bool = null,
37 stack_protector: ?bool = null,
38 strip: ?bool = null,
39 target: std.Target.Query = .{},
40 unwind_tables: ?std.builtin.UnwindTables = null,
41 valgrind: ?bool = null,
42 };
43};
44
45pub fn addMatches(
46 self: *LlvmIr,
47 name: []const u8,
48 source: []const u8,
49 matches: []const []const u8,
50 params: TestCase.Params,
51) void {
52 self.addCase(.{
53 .name = name,
54 .source = source,
55 .check = .{ .matches = matches },
56 .params = params,
57 });
58}
59
60pub fn addExact(
61 self: *LlvmIr,
62 name: []const u8,
63 source: []const u8,
64 expected: []const []const u8,
65 params: TestCase.Params,
66) void {
67 self.addCase(.{
68 .name = name,
69 .source = source,
70 .check = .{ .exact = expected },
71 .params = params,
72 });
73}
74
75pub fn addCase(self: *LlvmIr, case: TestCase) void {
76 const target = self.b.resolveTargetQuery(case.params.target);
77 if (self.options.test_target_filters.len > 0) {
78 const triple_txt = target.result.zigTriple(self.b.allocator) catch @panic("OOM");
79 for (self.options.test_target_filters) |filter| {
80 if (std.mem.indexOf(u8, triple_txt, filter) != null) break;
81 } else return;
82 }
83
84 const name = std.fmt.allocPrint(self.b.allocator, "check llvm-ir {s}", .{case.name}) catch @panic("OOM");
85 if (self.options.test_filters.len > 0) {
86 for (self.options.test_filters) |filter| {
87 if (std.mem.indexOf(u8, name, filter) != null) break;
88 } else return;
89 }
90
91 const obj = self.b.addObject(.{
92 .name = "test",
93 .root_source_file = self.b.addWriteFiles().add("test.zig", case.source),
94 .use_llvm = true,
95
96 .code_model = case.params.code_model,
97 .error_tracing = case.params.error_tracing,
98 .omit_frame_pointer = case.params.omit_frame_pointer,
99 .optimize = case.params.optimize,
100 .pic = case.params.pic,
101 .sanitize_thread = case.params.sanitize_thread,
102 .single_threaded = case.params.single_threaded,
103 .strip = case.params.strip,
104 .target = target,
105 .unwind_tables = case.params.unwind_tables,
106 });
107
108 obj.dll_export_fns = case.params.dll_export_fns;
109 obj.pie = case.params.pie;
110
111 obj.root_module.dwarf_format = case.params.dwarf_format;
112 obj.root_module.no_builtin = case.params.no_builtin;
113 obj.root_module.red_zone = case.params.red_zone;
114 obj.root_module.stack_check = case.params.stack_check;
115 obj.root_module.stack_protector = case.params.stack_protector;
116 obj.root_module.valgrind = case.params.valgrind;
117
118 // This is not very sophisticated at the moment. Eventually, we should move towards something
119 // like LLVM's `FileCheck` utility (https://llvm.org/docs/CommandGuide/FileCheck.html), though
120 // likely a more simplified version as we probably don't want a full-blown regex engine in the
121 // standard library...
122 const check = self.b.addCheckFile(obj.getEmittedLlvmIr(), switch (case.check) {
123 .matches => |m| .{ .expected_matches = m },
124 .exact => |e| .{ .expected_exact = e },
125 });
126 check.setName(name);
127
128 self.root_step.dependOn(&check.step);
129}
130
131const LlvmIr = @This();
132const std = @import("std");
test/tests.zig+22-1
......@@ -11,6 +11,7 @@ const stack_traces = @import("stack_traces.zig");
1111const assemble_and_link = @import("assemble_and_link.zig");
1212const translate_c = @import("translate_c.zig");
1313const run_translated_c = @import("run_translated_c.zig");
14const llvm_ir = @import("llvm_ir.zig");
1415
1516// Implementations
1617pub const TranslateCContext = @import("src/TranslateC.zig");
......@@ -18,6 +19,7 @@ pub const RunTranslatedCContext = @import("src/RunTranslatedC.zig");
1819pub const CompareOutputContext = @import("src/CompareOutput.zig");
1920pub const StackTracesContext = @import("src/StackTrace.zig");
2021pub const DebuggerContext = @import("src/Debugger.zig");
22pub const LlvmIrContext = @import("src/LlvmIr.zig");
2123
2224const TestTarget = struct {
2325 linkage: ?std.builtin.LinkMode = null,
......@@ -1825,7 +1827,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
18251827 .zig_lib_dir = b.path("lib"),
18261828 });
18271829 these_tests.linkage = test_target.linkage;
1828 if (options.no_builtin) these_tests.no_builtin = true;
1830 if (options.no_builtin) these_tests.root_module.no_builtin = false;
18291831 if (options.build_options) |build_options| {
18301832 these_tests.root_module.addOptions("build_options", build_options);
18311833 }
......@@ -2125,3 +2127,22 @@ pub fn addIncrementalTests(b: *std.Build, test_step: *Step) !void {
21252127 test_step.dependOn(&run.step);
21262128 }
21272129}
2130
2131pub fn addLlvmIrTests(b: *std.Build, options: LlvmIrContext.Options) ?*Step {
2132 const step = b.step("test-llvm-ir", "Run the LLVM IR tests");
2133
2134 if (!options.enable_llvm) {
2135 step.dependOn(&b.addFail("test-llvm-ir requires -Denable-llvm").step);
2136 return null;
2137 }
2138
2139 var context: LlvmIrContext = .{
2140 .b = b,
2141 .options = options,
2142 .root_step = step,
2143 };
2144
2145 llvm_ir.addCases(&context);
2146
2147 return step;
2148}