From 07114e6bc69106fb77beb879a8a2f78a4ba4b256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Mon, 30 Jun 2025 07:01:35 +0200 Subject: [PATCH] llvm: Disable the machine outliner pass on RISC-V --- src/codegen/llvm.zig | 3 +++ src/codegen/llvm/bindings.zig | 1 + src/zig_llvm.cpp | 16 ++++++++++------ src/zig_llvm.h | 1 + 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index a48fb6835215bb9ce140249217eddd2398b3117c..a5e6d35fd68f28eca36df2dbd46f9091c8023639 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1068,6 +1068,9 @@ pub const Object = struct { .full => .FullPreLink, }, .allow_fast_isel = true, + // LLVM's RISC-V backend for some reason enables the machine outliner by default even + // though it's clearly not ready and produces multiple miscompilations in our std tests. + .allow_machine_outliner = !comp.root_mod.resolved_target.result.cpu.arch.isRISCV(), .asm_filename = null, .bin_filename = options.bin_path, .llvm_ir_filename = options.post_ir_path, diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig index 44ca599fdc75f630bab2a1f60a71b628e3c18e8a..5d2d4c2a99304911809d9677a0e2e8667b97b19c 100644 --- a/src/codegen/llvm/bindings.zig +++ b/src/codegen/llvm/bindings.zig @@ -92,6 +92,7 @@ pub const TargetMachine = opaque { sancov: bool, lto: LtoPhase, allow_fast_isel: bool, + allow_machine_outliner: bool, asm_filename: ?[*:0]const u8, bin_filename: ?[*:0]const u8, llvm_ir_filename: ?[*:0]const u8, diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index cb624ea256e6adf75ec259ae2e6e3fb0487634b7..fe4e421dc3238380cc5c159506074cfcefd5c39c 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -260,6 +260,16 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi TargetMachine &target_machine = *reinterpret_cast(targ_machine_ref); + if (options->allow_fast_isel) { + target_machine.setO0WantsFastISel(true); + } else { + target_machine.setFastISel(false); + } + + if (!options->allow_machine_outliner) { + target_machine.setMachineOutliner(false); + } + Module &llvm_module = *unwrap(module_ref); // Pipeline configurations @@ -385,12 +395,6 @@ ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machi } } - if (options->allow_fast_isel) { - target_machine.setO0WantsFastISel(true); - } else { - target_machine.setFastISel(false); - } - // Optimization phase module_pm.run(llvm_module, module_am); diff --git a/src/zig_llvm.h b/src/zig_llvm.h index f75e119988a6e54e9747dec84bfbbff9e40a0a48..e2dba338c3af31aa89d4255c2dd8f266a1d64ee5 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -71,6 +71,7 @@ struct ZigLLVMEmitOptions { bool sancov; ZigLLVMThinOrFullLTOPhase lto; bool allow_fast_isel; + bool allow_machine_outliner; const char *asm_filename; const char *bin_filename; const char *llvm_ir_filename; -- 2.54.0