From e9ac2ce1167f0f2f86d824549b8ab04d1028129c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 16 Jul 2025 18:33:15 +0200 Subject: [PATCH] compiler-rt: move strlen from libzigc to here LLVM 21 has started recognizing strlen-like idioms and optimizing them to strlen calls, so we need this function provided in compiler-rt for libc-less compilations. --- lib/c/string.zig | 5 ----- lib/compiler_rt.zig | 2 ++ lib/compiler_rt/strlen.zig | 10 ++++++++++ 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 lib/compiler_rt/strlen.zig diff --git a/lib/c/string.zig b/lib/c/string.zig index e846c4d960..a644093af5 100644 --- a/lib/c/string.zig +++ b/lib/c/string.zig @@ -4,7 +4,6 @@ const common = @import("common.zig"); comptime { @export(&strcmp, .{ .name = "strcmp", .linkage = common.linkage, .visibility = common.visibility }); - @export(&strlen, .{ .name = "strlen", .linkage = common.linkage, .visibility = common.visibility }); @export(&strncmp, .{ .name = "strncmp", .linkage = common.linkage, .visibility = common.visibility }); @export(&strcasecmp, .{ .name = "strcasecmp", .linkage = common.linkage, .visibility = common.visibility }); @export(&strncasecmp, .{ .name = "strncasecmp", .linkage = common.linkage, .visibility = common.visibility }); @@ -103,7 +102,3 @@ test strncmp { try std.testing.expect(strncmp(@ptrCast("b"), @ptrCast("a"), 1) > 0); try std.testing.expect(strncmp(@ptrCast("\xff"), @ptrCast("\x02"), 1) > 0); } - -fn strlen(s: [*:0]const c_char) callconv(.c) usize { - return std.mem.len(s); -} diff --git a/lib/compiler_rt.zig b/lib/compiler_rt.zig index b8723c56ee..aac81bf414 100644 --- a/lib/compiler_rt.zig +++ b/lib/compiler_rt.zig @@ -263,6 +263,8 @@ comptime { _ = @import("compiler_rt/memcmp.zig"); _ = @import("compiler_rt/bcmp.zig"); _ = @import("compiler_rt/ssp.zig"); + + _ = @import("compiler_rt/strlen.zig"); } // Temporarily used for uefi until https://github.com/ziglang/zig/issues/21630 is addressed. diff --git a/lib/compiler_rt/strlen.zig b/lib/compiler_rt/strlen.zig new file mode 100644 index 0000000000..01d4e760dc --- /dev/null +++ b/lib/compiler_rt/strlen.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const common = @import("common.zig"); + +comptime { + @export(&strlen, .{ .name = "strlen", .linkage = common.linkage, .visibility = common.visibility }); +} + +fn strlen(s: [*:0]const c_char) callconv(.c) usize { + return std.mem.len(s); +}