authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2025-04-25 13:46:06+03:30
committergravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2025-04-28 10:49:17+03:30
log710f9325ff951c187cf8c7d8eae14330e7b42cd6
treed3aa45d0d2082ca413f72267b9e3246049ad29b6
parent296b17f37b5c2c28dc909e37d0bb15853338eb79

Compilation: don't build `compiler_rt` for `amdgcn` and `ubsan_rt` for ptx


2 files changed, 31 insertions(+), 21 deletions(-)

src/Compilation.zig+4-21
......@@ -1352,8 +1352,8 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
13521352 // approach, since the ubsan runtime uses quite a lot of the standard library
13531353 // and this reduces unnecessary bloat.
13541354 const ubsan_rt_strat: RtStrat = s: {
1355 const is_spirv = options.root_mod.resolved_target.result.cpu.arch.isSpirV();
1356 const want_ubsan_rt = options.want_ubsan_rt orelse (!is_spirv and any_sanitize_c == .full and is_exe_or_dyn_lib);
1355 const can_build_ubsan_rt = target_util.canBuildLibUbsanRt(options.root_mod.resolved_target.result);
1356 const want_ubsan_rt = options.want_ubsan_rt orelse (can_build_ubsan_rt and any_sanitize_c == .full and is_exe_or_dyn_lib);
13571357 if (!want_ubsan_rt) break :s .none;
13581358 if (options.skip_linker_dependencies) break :s .none;
13591359 if (have_zcu) break :s .zcu;
......@@ -1768,8 +1768,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
17681768 errdefer comp.destroy();
17691769
17701770 const target = comp.root_mod.resolved_target.result;
1771
1772 const capable_of_building_compiler_rt = canBuildLibCompilerRt(target, comp.config.use_llvm);
1771 const can_build_compiler_rt = target_util.canBuildLibCompilerRt(target, comp.config.use_llvm, build_options.have_llvm);
17731772
17741773 // Add a `CObject` for each `c_source_files`.
17751774 try comp.c_object_table.ensureTotalCapacity(gpa, options.c_source_files.len);
......@@ -1939,7 +1938,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
19391938 comp.remaining_prelink_tasks += 1;
19401939 }
19411940
1942 if (capable_of_building_compiler_rt) {
1941 if (can_build_compiler_rt) {
19431942 if (comp.compiler_rt_strat == .lib) {
19441943 log.debug("queuing a job to build compiler_rt_lib", .{});
19451944 comp.queued_jobs.compiler_rt_lib = true;
......@@ -6558,22 +6557,6 @@ pub fn dump_argv(argv: []const []const u8) void {
65586557 nosuspend stderr.print("{s}\n", .{argv[argv.len - 1]}) catch {};
65596558}
65606559
6561fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
6562 switch (target.os.tag) {
6563 .plan9 => return false,
6564 else => {},
6565 }
6566 switch (target.cpu.arch) {
6567 .spirv, .spirv32, .spirv64 => return false,
6568 else => {},
6569 }
6570 return switch (target_util.zigBackend(target, use_llvm)) {
6571 .stage2_llvm => true,
6572 .stage2_x86_64 => if (target.ofmt == .elf or target.ofmt == .macho) true else build_options.have_llvm,
6573 else => build_options.have_llvm,
6574 };
6575}
6576
65776560pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {
65786561 const target = comp.root_mod.resolved_target.result;
65796562 return target_util.zigBackend(target, comp.config.use_llvm);
src/target.zig+27
......@@ -313,6 +313,33 @@ pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.OptimizeMod
313313 }
314314}
315315
316pub fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool, have_llvm: bool) bool {
317 switch (target.os.tag) {
318 .plan9 => return false,
319 else => {},
320 }
321 switch (target.cpu.arch) {
322 .spirv, .spirv32, .spirv64 => return false,
323 // Remove this once https://github.com/ziglang/zig/issues/23714 is fixed
324 .amdgcn => return false,
325 else => {},
326 }
327 return switch (zigBackend(target, use_llvm)) {
328 .stage2_llvm => true,
329 .stage2_x86_64 => if (target.ofmt == .elf or target.ofmt == .macho) true else have_llvm,
330 else => have_llvm,
331 };
332}
333
334pub fn canBuildLibUbsanRt(target: std.Target) bool {
335 switch (target.cpu.arch) {
336 .spirv, .spirv32, .spirv64 => return false,
337 // Remove this once https://github.com/ziglang/zig/issues/23715 is fixed
338 .nvptx, .nvptx64 => return false,
339 else => return true,
340 }
341}
342
316343pub fn hasRedZone(target: std.Target) bool {
317344 return switch (target.cpu.arch) {
318345 .aarch64,