authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-02-15 22:37:23+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-02-22 15:06:40+01:00
log21eb75749722668632da55499831eedc62120d61
tree9f7bbb1f5cf56763c1e82813f026fc2f1c14e8c8
parent41b179ca09f33a6a667ccdb063ebf8ff4cf076b8
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

llvm: Use inline variants of memcpy/memset intrinsics when using -fno-builtin.

This is a correctness issue: When -fno-builtin is used, we must assume that we could be compiling the memcpy/memset implementations, so generating calls to them is problematic.

2 files changed, 65 insertions(+), 9 deletions(-)

src/codegen/llvm.zig+61-7
...@@ -5771,6 +5771,7 @@ pub const FuncGen = struct {...@@ -5771,6 +5771,7 @@ pub const FuncGen = struct {
5771 try o.builder.intValue(.i8, 0xaa),5771 try o.builder.intValue(.i8, 0xaa),
5772 len,5772 len,
5773 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,5773 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
5774 self.ng.ownerModule().no_builtin,
5774 );5775 );
5775 const owner_mod = self.ng.ownerModule();5776 const owner_mod = self.ng.ownerModule();
5776 if (owner_mod.valgrind) {5777 if (owner_mod.valgrind) {
...@@ -5821,6 +5822,7 @@ pub const FuncGen = struct {...@@ -5821,6 +5822,7 @@ pub const FuncGen = struct {
5821 try o.builder.intValue(.i8, 0xaa),5822 try o.builder.intValue(.i8, 0xaa),
5822 len,5823 len,
5823 .normal,5824 .normal,
5825 self.ng.ownerModule().no_builtin,
5824 );5826 );
5825 const owner_mod = self.ng.ownerModule();5827 const owner_mod = self.ng.ownerModule();
5826 if (owner_mod.valgrind) {5828 if (owner_mod.valgrind) {
...@@ -9734,6 +9736,7 @@ pub const FuncGen = struct {...@@ -9734,6 +9736,7 @@ pub const FuncGen = struct {
9734 if (safety) try o.builder.intValue(.i8, 0xaa) else try o.builder.undefValue(.i8),9736 if (safety) try o.builder.intValue(.i8, 0xaa) else try o.builder.undefValue(.i8),
9735 len,9737 len,
9736 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,9738 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
9739 self.ng.ownerModule().no_builtin,
9737 );9740 );
9738 if (safety and owner_mod.valgrind) {9741 if (safety and owner_mod.valgrind) {
9739 try self.valgrindMarkUndef(dest_ptr, len);9742 try self.valgrindMarkUndef(dest_ptr, len);
...@@ -10041,9 +10044,22 @@ pub const FuncGen = struct {...@@ -10041,9 +10044,22 @@ pub const FuncGen = struct {
10041 try o.builder.undefValue(.i8);10044 try o.builder.undefValue(.i8);
10042 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);10045 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
10043 if (intrinsic_len0_traps) {10046 if (intrinsic_len0_traps) {
10044 try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, access_kind);10047 try self.safeWasmMemset(
10048 dest_ptr,
10049 fill_byte,
10050 len,
10051 dest_ptr_align,
10052 access_kind,
10053 );
10045 } else {10054 } else {
10046 _ = try self.wip.callMemSet(dest_ptr, dest_ptr_align, fill_byte, len, access_kind);10055 _ = try self.wip.callMemSet(
10056 dest_ptr,
10057 dest_ptr_align,
10058 fill_byte,
10059 len,
10060 access_kind,
10061 self.ng.ownerModule().no_builtin,
10062 );
10047 }10063 }
10048 const owner_mod = self.ng.ownerModule();10064 const owner_mod = self.ng.ownerModule();
10049 if (safety and owner_mod.valgrind) {10065 if (safety and owner_mod.valgrind) {
...@@ -10060,9 +10076,22 @@ pub const FuncGen = struct {...@@ -10060,9 +10076,22 @@ pub const FuncGen = struct {
10060 const fill_byte = try o.builder.intValue(.i8, byte_val);10076 const fill_byte = try o.builder.intValue(.i8, byte_val);
10061 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);10077 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
10062 if (intrinsic_len0_traps) {10078 if (intrinsic_len0_traps) {
10063 try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, access_kind);10079 try self.safeWasmMemset(
10080 dest_ptr,
10081 fill_byte,
10082 len,
10083 dest_ptr_align,
10084 access_kind,
10085 );
10064 } else {10086 } else {
10065 _ = try self.wip.callMemSet(dest_ptr, dest_ptr_align, fill_byte, len, access_kind);10087 _ = try self.wip.callMemSet(
10088 dest_ptr,
10089 dest_ptr_align,
10090 fill_byte,
10091 len,
10092 access_kind,
10093 self.ng.ownerModule().no_builtin,
10094 );
10066 }10095 }
10067 return .none;10096 return .none;
10068 }10097 }
...@@ -10077,9 +10106,22 @@ pub const FuncGen = struct {...@@ -10077,9 +10106,22 @@ pub const FuncGen = struct {
10077 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);10106 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
1007810107
10079 if (intrinsic_len0_traps) {10108 if (intrinsic_len0_traps) {
10080 try self.safeWasmMemset(dest_ptr, fill_byte, len, dest_ptr_align, access_kind);10109 try self.safeWasmMemset(
10110 dest_ptr,
10111 fill_byte,
10112 len,
10113 dest_ptr_align,
10114 access_kind,
10115 );
10081 } else {10116 } else {
10082 _ = try self.wip.callMemSet(dest_ptr, dest_ptr_align, fill_byte, len, access_kind);10117 _ = try self.wip.callMemSet(
10118 dest_ptr,
10119 dest_ptr_align,
10120 fill_byte,
10121 len,
10122 access_kind,
10123 self.ng.ownerModule().no_builtin,
10124 );
10083 }10125 }
10084 return .none;10126 return .none;
10085 }10127 }
...@@ -10131,6 +10173,7 @@ pub const FuncGen = struct {...@@ -10131,6 +10173,7 @@ pub const FuncGen = struct {
10131 elem_abi_align.toLlvm(),10173 elem_abi_align.toLlvm(),
10132 try o.builder.intValue(llvm_usize_ty, elem_abi_size),10174 try o.builder.intValue(llvm_usize_ty, elem_abi_size),
10133 access_kind,10175 access_kind,
10176 self.ng.ownerModule().no_builtin,
10134 );10177 );
10135 } else _ = try self.wip.store(access_kind, value, it_ptr.toValue(), it_ptr_align);10178 } else _ = try self.wip.store(access_kind, value, it_ptr.toValue(), it_ptr_align);
10136 const next_ptr = try self.wip.gep(.inbounds, elem_llvm_ty, it_ptr.toValue(), &.{10179 const next_ptr = try self.wip.gep(.inbounds, elem_llvm_ty, it_ptr.toValue(), &.{
...@@ -10158,7 +10201,14 @@ pub const FuncGen = struct {...@@ -10158,7 +10201,14 @@ pub const FuncGen = struct {
10158 const end_block = try self.wip.block(2, "MemsetTrapEnd");10201 const end_block = try self.wip.block(2, "MemsetTrapEnd");
10159 _ = try self.wip.brCond(cond, memset_block, end_block, .none);10202 _ = try self.wip.brCond(cond, memset_block, end_block, .none);
10160 self.wip.cursor = .{ .block = memset_block };10203 self.wip.cursor = .{ .block = memset_block };
10161 _ = try self.wip.callMemSet(dest_ptr, dest_ptr_align, fill_byte, len, access_kind);10204 _ = try self.wip.callMemSet(
10205 dest_ptr,
10206 dest_ptr_align,
10207 fill_byte,
10208 len,
10209 access_kind,
10210 self.ng.ownerModule().no_builtin,
10211 );
10162 _ = try self.wip.br(end_block);10212 _ = try self.wip.br(end_block);
10163 self.wip.cursor = .{ .block = end_block };10213 self.wip.cursor = .{ .block = end_block };
10164 }10214 }
...@@ -10200,6 +10250,7 @@ pub const FuncGen = struct {...@@ -10200,6 +10250,7 @@ pub const FuncGen = struct {
10200 src_ptr_ty.ptrAlignment(zcu).toLlvm(),10250 src_ptr_ty.ptrAlignment(zcu).toLlvm(),
10201 len,10251 len,
10202 access_kind,10252 access_kind,
10253 self.ng.ownerModule().no_builtin,
10203 );10254 );
10204 _ = try self.wip.br(end_block);10255 _ = try self.wip.br(end_block);
10205 self.wip.cursor = .{ .block = end_block };10256 self.wip.cursor = .{ .block = end_block };
...@@ -10213,6 +10264,7 @@ pub const FuncGen = struct {...@@ -10213,6 +10264,7 @@ pub const FuncGen = struct {
10213 src_ptr_ty.ptrAlignment(zcu).toLlvm(),10264 src_ptr_ty.ptrAlignment(zcu).toLlvm(),
10214 len,10265 len,
10215 access_kind,10266 access_kind,
10267 self.ng.ownerModule().no_builtin,
10216 );10268 );
10217 return .none;10269 return .none;
10218 }10270 }
...@@ -11346,6 +11398,7 @@ pub const FuncGen = struct {...@@ -11346,6 +11398,7 @@ pub const FuncGen = struct {
11346 ptr_alignment,11398 ptr_alignment,
11347 try o.builder.intValue(try o.lowerType(Type.usize), size_bytes),11399 try o.builder.intValue(try o.lowerType(Type.usize), size_bytes),
11348 access_kind,11400 access_kind,
11401 fg.ng.ownerModule().no_builtin,
11349 );11402 );
11350 return result_ptr;11403 return result_ptr;
11351 }11404 }
...@@ -11513,6 +11566,7 @@ pub const FuncGen = struct {...@@ -11513,6 +11566,7 @@ pub const FuncGen = struct {
11513 elem_ty.abiAlignment(zcu).toLlvm(),11566 elem_ty.abiAlignment(zcu).toLlvm(),
11514 try o.builder.intValue(try o.lowerType(Type.usize), elem_ty.abiSize(zcu)),11567 try o.builder.intValue(try o.lowerType(Type.usize), elem_ty.abiSize(zcu)),
11515 access_kind,11568 access_kind,
11569 self.ng.ownerModule().no_builtin,
11516 );11570 );
11517 }11571 }
1151811572
src/codegen/llvm/Builder.zig+4-2
...@@ -6102,6 +6102,7 @@ pub const WipFunction = struct {...@@ -6102,6 +6102,7 @@ pub const WipFunction = struct {
6102 src_align: Alignment,6102 src_align: Alignment,
6103 len: Value,6103 len: Value,
6104 kind: MemoryAccessKind,6104 kind: MemoryAccessKind,
6105 @"inline": bool,
6105 ) Allocator.Error!Instruction.Index {6106 ) Allocator.Error!Instruction.Index {
6106 var dst_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = dst_align })};6107 var dst_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = dst_align })};
6107 var src_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = src_align })};6108 var src_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = src_align })};
...@@ -6113,7 +6114,7 @@ pub const WipFunction = struct {...@@ -6113,7 +6114,7 @@ pub const WipFunction = struct {
6113 try self.builder.attrs(&dst_attrs),6114 try self.builder.attrs(&dst_attrs),
6114 try self.builder.attrs(&src_attrs),6115 try self.builder.attrs(&src_attrs),
6115 }),6116 }),
6116 .memcpy,6117 if (@"inline") .@"memcpy.inline" else .memcpy,
6117 &.{ dst.typeOfWip(self), src.typeOfWip(self), len.typeOfWip(self) },6118 &.{ dst.typeOfWip(self), src.typeOfWip(self), len.typeOfWip(self) },
6118 &.{ dst, src, len, switch (kind) {6119 &.{ dst, src, len, switch (kind) {
6119 .normal => Value.false,6120 .normal => Value.false,
...@@ -6131,12 +6132,13 @@ pub const WipFunction = struct {...@@ -6131,12 +6132,13 @@ pub const WipFunction = struct {
6131 val: Value,6132 val: Value,
6132 len: Value,6133 len: Value,
6133 kind: MemoryAccessKind,6134 kind: MemoryAccessKind,
6135 @"inline": bool,
6134 ) Allocator.Error!Instruction.Index {6136 ) Allocator.Error!Instruction.Index {
6135 var dst_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = dst_align })};6137 var dst_attrs = [_]Attribute.Index{try self.builder.attr(.{ .@"align" = dst_align })};
6136 const value = try self.callIntrinsic(6138 const value = try self.callIntrinsic(
6137 .normal,6139 .normal,
6138 try self.builder.fnAttrs(&.{ .none, .none, try self.builder.attrs(&dst_attrs) }),6140 try self.builder.fnAttrs(&.{ .none, .none, try self.builder.attrs(&dst_attrs) }),
6139 .memset,6141 if (@"inline") .@"memset.inline" else .memset,
6140 &.{ dst.typeOfWip(self), len.typeOfWip(self) },6142 &.{ dst.typeOfWip(self), len.typeOfWip(self) },
6141 &.{ dst, val, len, switch (kind) {6143 &.{ dst, val, len, switch (kind) {
6142 .normal => Value.false,6144 .normal => Value.false,