authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-11-02 05:44:19+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-12 17:07:50+02:00
log44bf64a7099a825394e686a78abd3d465c7ec3f7
treea1ce13d3e45e8541e460b7901cdf79cb67a80b56
parent427810f3edea1beba77e3f84dd695dd0a8c35f00
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

llvm: Fix a bunch of volatile semantics violations.

Also fix some cases where we were being overzealous in applying volatile.

1 files changed, 73 insertions(+), 33 deletions(-)

src/codegen/llvm.zig+73-33
......@@ -5546,7 +5546,7 @@ pub const FuncGen = struct {
55465546 ptr_ty.ptrAlignment(zcu).toLlvm(),
55475547 try o.builder.intValue(.i8, 0xaa),
55485548 len,
5549 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
5549 .normal,
55505550 self.disable_intrinsics,
55515551 );
55525552 const owner_mod = self.ng.ownerModule();
......@@ -5781,8 +5781,8 @@ pub const FuncGen = struct {
57815781 // of optionals that are not pointers.
57825782 const is_by_ref = isByRef(scalar_ty, zcu);
57835783 const opt_llvm_ty = try o.lowerType(scalar_ty);
5784 const lhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, lhs, is_by_ref);
5785 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);
57865786 const llvm_i2 = try o.builder.intType(2);
57875787 const lhs_non_null_i2 = try self.wip.cast(.zext, lhs_non_null, llvm_i2, "");
57885788 const rhs_non_null_i2 = try self.wip.cast(.zext, rhs_non_null, llvm_i2, "");
......@@ -6259,10 +6259,13 @@ pub const FuncGen = struct {
62596259
62606260 if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
62616261 const loaded = loaded: {
6262 const access_kind: Builder.MemoryAccessKind =
6263 if (err_union_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
6264
62626265 if (!payload_has_bits) {
62636266 // TODO add alignment to this load
62646267 break :loaded if (operand_is_ptr)
6265 try fg.wip.load(.normal, error_type, err_union, .default, "")
6268 try fg.wip.load(access_kind, error_type, err_union, .default, "")
62666269 else
62676270 err_union;
62686271 }
......@@ -6272,7 +6275,7 @@ pub const FuncGen = struct {
62726275 try fg.wip.gepStruct(err_union_llvm_ty, err_union, err_field_index, "");
62736276 // TODO add alignment to this load
62746277 break :loaded try fg.wip.load(
6275 .normal,
6278 if (operand_is_ptr) access_kind else .normal,
62766279 error_type,
62776280 err_field_ptr,
62786281 .default,
......@@ -6784,7 +6787,7 @@ pub const FuncGen = struct {
67846787 self.maybeMarkAllowZeroAccess(slice_ty.ptrInfo(zcu));
67856788
67866789 const elem_alignment = elem_ty.abiAlignment(zcu).toLlvm();
6787 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);
67886791 }
67896792
67906793 self.maybeMarkAllowZeroAccess(slice_ty.ptrInfo(zcu));
......@@ -6862,7 +6865,7 @@ pub const FuncGen = struct {
68626865 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
68636866
68646867 const elem_alignment = elem_ty.abiAlignment(zcu).toLlvm();
6865 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);
68666869 }
68676870
68686871 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
......@@ -7409,7 +7412,13 @@ pub const FuncGen = struct {
74097412 llvm_param_types[llvm_param_i] = llvm_rw_val.typeOfWip(&self.wip);
74107413 } else {
74117414 const alignment = rw_ty.abiAlignment(zcu).toLlvm();
7412 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 );
74137422 llvm_param_values[llvm_param_i] = loaded;
74147423 llvm_param_types[llvm_param_i] = llvm_elem_ty;
74157424 }
......@@ -7573,7 +7582,12 @@ pub const FuncGen = struct {
75737582 const output_ptr = try self.resolveInst(output);
75747583 const output_ptr_ty = self.typeOf(output);
75757584 const alignment = output_ptr_ty.ptrAlignment(zcu).toLlvm();
7576 _ = 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 );
75777591 } else {
75787592 ret_val = output_value;
75797593 }
......@@ -7599,11 +7613,14 @@ pub const FuncGen = struct {
75997613 const optional_llvm_ty = try o.lowerType(optional_ty);
76007614 const payload_ty = optional_ty.optionalChild(zcu);
76017615
7616 const access_kind: Builder.MemoryAccessKind =
7617 if (operand_is_ptr and operand_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7618
76027619 if (operand_is_ptr) self.maybeMarkAllowZeroAccess(operand_ty.ptrInfo(zcu));
76037620
76047621 if (optional_ty.optionalReprIsPayload(zcu)) {
76057622 const loaded = if (operand_is_ptr)
7606 try self.wip.load(.normal, optional_llvm_ty, operand, .default, "")
7623 try self.wip.load(access_kind, optional_llvm_ty, operand, .default, "")
76077624 else
76087625 operand;
76097626 if (payload_ty.isSlice(zcu)) {
......@@ -7621,14 +7638,14 @@ pub const FuncGen = struct {
76217638
76227639 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
76237640 const loaded = if (operand_is_ptr)
7624 try self.wip.load(.normal, optional_llvm_ty, operand, .default, "")
7641 try self.wip.load(access_kind, optional_llvm_ty, operand, .default, "")
76257642 else
76267643 operand;
76277644 return self.wip.icmp(cond, loaded, try o.builder.intValue(.i8, 0), "");
76287645 }
76297646
76307647 const is_by_ref = operand_is_ptr or isByRef(optional_ty, zcu);
7631 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);
76327649 }
76337650
76347651 fn airIsErr(
......@@ -7648,6 +7665,9 @@ pub const FuncGen = struct {
76487665 const error_type = try o.errorIntType();
76497666 const zero = try o.builder.intValue(error_type, 0);
76507667
7668 const access_kind: Builder.MemoryAccessKind =
7669 if (operand_is_ptr and operand_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7670
76517671 if (err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
76527672 const val: Builder.Constant = switch (cond) {
76537673 .eq => .true, // 0 == 0
......@@ -7661,7 +7681,7 @@ pub const FuncGen = struct {
76617681
76627682 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
76637683 const loaded = if (operand_is_ptr)
7664 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, "")
76657685 else
76667686 operand;
76677687 return self.wip.icmp(cond, loaded, zero, "");
......@@ -7673,7 +7693,7 @@ pub const FuncGen = struct {
76737693 const err_union_llvm_ty = try o.lowerType(err_union_ty);
76747694 const err_field_ptr =
76757695 try self.wip.gepStruct(err_union_llvm_ty, operand, err_field_index, "");
7676 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, "");
76777697 } else try self.wip.extractValue(operand, &.{err_field_index}, "");
76787698 return self.wip.icmp(cond, loaded, zero, "");
76797699 }
......@@ -7706,14 +7726,19 @@ pub const FuncGen = struct {
77067726 const zcu = pt.zcu;
77077727 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
77087728 const operand = try self.resolveInst(ty_op.operand);
7709 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);
77107731 const payload_ty = optional_ty.optionalChild(zcu);
77117732 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
77127737 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
7713 self.maybeMarkAllowZeroAccess(self.typeOf(ty_op.operand).ptrInfo(zcu));
7738 self.maybeMarkAllowZeroAccess(optional_ptr_ty.ptrInfo(zcu));
77147739
77157740 // We have a pointer to a i8. We need to set it to 1 and then return the same pointer.
7716 _ = try self.wip.store(.normal, non_null_bit, operand, .default);
7741 _ = try self.wip.store(access_kind, non_null_bit, operand, .default);
77177742 return operand;
77187743 }
77197744 if (optional_ty.optionalReprIsPayload(zcu)) {
......@@ -7726,10 +7751,10 @@ pub const FuncGen = struct {
77267751 const optional_llvm_ty = try o.lowerType(optional_ty);
77277752 const non_null_ptr = try self.wip.gepStruct(optional_llvm_ty, operand, 1, "");
77287753
7729 self.maybeMarkAllowZeroAccess(self.typeOf(ty_op.operand).ptrInfo(zcu));
7754 self.maybeMarkAllowZeroAccess(optional_ptr_ty.ptrInfo(zcu));
77307755
77317756 // TODO set alignment on this store
7732 _ = 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);
77337758
77347759 // Then return the payload pointer (only if it's used).
77357760 if (self.liveness.isUnused(inst)) return .none;
......@@ -7815,13 +7840,16 @@ pub const FuncGen = struct {
78157840 }
78167841 }
78177842
7843 const access_kind: Builder.MemoryAccessKind =
7844 if (operand_is_ptr and operand_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
7845
78187846 const payload_ty = err_union_ty.errorUnionPayload(zcu);
78197847 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
78207848 if (!operand_is_ptr) return operand;
78217849
78227850 self.maybeMarkAllowZeroAccess(operand_ty.ptrInfo(zcu));
78237851
7824 return self.wip.load(.normal, error_type, operand, .default, "");
7852 return self.wip.load(access_kind, error_type, operand, .default, "");
78257853 }
78267854
78277855 const offset = try errUnionErrorOffset(payload_ty, pt);
......@@ -7831,7 +7859,7 @@ pub const FuncGen = struct {
78317859
78327860 const err_union_llvm_ty = try o.lowerType(err_union_ty);
78337861 const err_field_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, "");
7834 return self.wip.load(.normal, error_type, err_field_ptr, .default, "");
7862 return self.wip.load(access_kind, error_type, err_field_ptr, .default, "");
78357863 }
78367864
78377865 return self.wip.extractValue(operand, &.{offset}, "");
......@@ -7843,26 +7871,31 @@ pub const FuncGen = struct {
78437871 const zcu = pt.zcu;
78447872 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
78457873 const operand = try self.resolveInst(ty_op.operand);
7846 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);
78477876
78487877 const payload_ty = err_union_ty.errorUnionPayload(zcu);
78497878 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
78507883 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
7851 self.maybeMarkAllowZeroAccess(self.typeOf(ty_op.operand).ptrInfo(zcu));
7884 self.maybeMarkAllowZeroAccess(err_union_ptr_ty.ptrInfo(zcu));
78527885
7853 _ = try self.wip.store(.normal, non_error_val, operand, .default);
7886 _ = try self.wip.store(access_kind, non_error_val, operand, .default);
78547887 return operand;
78557888 }
78567889 const err_union_llvm_ty = try o.lowerType(err_union_ty);
78577890 {
7858 self.maybeMarkAllowZeroAccess(self.typeOf(ty_op.operand).ptrInfo(zcu));
7891 self.maybeMarkAllowZeroAccess(err_union_ptr_ty.ptrInfo(zcu));
78597892
78607893 const err_int_ty = try pt.errorIntType();
78617894 const error_alignment = err_int_ty.abiAlignment(zcu).toLlvm();
78627895 const error_offset = try errUnionErrorOffset(payload_ty, pt);
78637896 // First set the non-error value.
78647897 const non_null_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, error_offset, "");
7865 _ = 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);
78667899 }
78677900 // Then return the payload pointer (only if it is used).
78687901 if (self.liveness.isUnused(inst)) return .none;
......@@ -8079,6 +8112,8 @@ pub const FuncGen = struct {
80798112
80808113 self.maybeMarkAllowZeroAccess(vector_ptr_ty.ptrInfo(zcu));
80818114
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
80828117 const access_kind: Builder.MemoryAccessKind =
80838118 if (vector_ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal;
80848119 const elem_llvm_ty = try o.lowerType(vector_ptr_ty.childType(zcu));
......@@ -10042,23 +10077,27 @@ pub const FuncGen = struct {
1004210077 const pt = o.pt;
1004310078 const zcu = pt.zcu;
1004410079 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
10045 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);
1004610082 const layout = un_ty.unionGetLayout(zcu);
1004710083 if (layout.tag_size == 0) return .none;
1004810084
10049 self.maybeMarkAllowZeroAccess(self.typeOf(bin_op.lhs).ptrInfo(zcu));
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));
1005010089
1005110090 const union_ptr = try self.resolveInst(bin_op.lhs);
1005210091 const new_tag = try self.resolveInst(bin_op.rhs);
1005310092 if (layout.payload_size == 0) {
1005410093 // TODO alignment on this store
10055 _ = try self.wip.store(.normal, new_tag, union_ptr, .default);
10094 _ = try self.wip.store(access_kind, new_tag, union_ptr, .default);
1005610095 return .none;
1005710096 }
1005810097 const tag_index = @intFromBool(layout.tag_align.compare(.lt, layout.payload_align));
1005910098 const tag_field_ptr = try self.wip.gepStruct(try o.lowerType(un_ty), union_ptr, tag_index, "");
1006010099 // TODO alignment on this store
10061 _ = try self.wip.store(.normal, new_tag, tag_field_ptr, .default);
10100 _ = try self.wip.store(access_kind, new_tag, tag_field_ptr, .default);
1006210101 return .none;
1006310102 }
1006410103
......@@ -10955,12 +10994,13 @@ pub const FuncGen = struct {
1095510994 opt_llvm_ty: Builder.Type,
1095610995 opt_handle: Builder.Value,
1095710996 is_by_ref: bool,
10997 access_kind: Builder.MemoryAccessKind,
1095810998 ) Allocator.Error!Builder.Value {
1095910999 const o = self.ng.object;
1096011000 const field = b: {
1096111001 if (is_by_ref) {
1096211002 const field_ptr = try self.wip.gepStruct(opt_llvm_ty, opt_handle, 1, "");
10963 break :b try self.wip.load(.normal, .i8, field_ptr, .default, "");
11003 break :b try self.wip.load(access_kind, .i8, field_ptr, .default, "");
1096411004 }
1096511005 break :b try self.wip.extractValue(opt_handle, &.{1}, "");
1096611006 };
......@@ -11269,7 +11309,7 @@ pub const FuncGen = struct {
1126911309 const vec_elem_ty = try o.lowerType(elem_ty);
1127011310 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
1127111311
11272 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, "");
1127311313
1127411314 const modified_vector = try self.wip.insertElement(loaded_vector, elem, index_u32, "");
1127511315
......@@ -11282,7 +11322,7 @@ pub const FuncGen = struct {
1128211322 const containing_int_ty = try o.builder.intType(@intCast(info.packed_offset.host_size * 8));
1128311323 assert(ordering == .none);
1128411324 const containing_int =
11285 try self.wip.load(access_kind, containing_int_ty, ptr, ptr_alignment, "");
11325 try self.wip.load(.normal, containing_int_ty, ptr, ptr_alignment, "");
1128611326 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
1128711327 const shift_amt = try o.builder.intConst(containing_int_ty, info.packed_offset.bit_offset);
1128811328 // Convert to equally-sized integer type in order to perform the bit