Skip to content

Commit

Permalink
Fix CPU inference performance when building MSVC Rust debug
Browse files Browse the repository at this point in the history
Workaround for rust-lang/cmake-rs#240
  • Loading branch information
vlovich committed Feb 13, 2025
1 parent 251b97e commit 5a4dbd4
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions llama-cpp-sys-2/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ fn main() {
config.define("GGML_BLAS", "OFF");
}

if (cfg!(debug_assertions)
|| std::env::var("PROFILE").as_ref().map(String::as_str) == Ok("debug"))
&& matches!(target_os, TargetOs::Windows(WindowsVariant::Msvc))
&& profile == "Release"
{
// Debug Rust builds under MSVC turn off optimization even though we're ideally building the release profile of llama.cpp.
// Looks like an upstream bug:
// https://github.com/rust-lang/cmake-rs/issues/240
// For now explicitly reinject the optimization flags that a CMake Release build is expected to have on in this scenario.
// This fixes CPU inference performance when part of a Rust debug build.
for flag in &["/O2", "/DNDEBUG", "/Ob2"] {
config.cflag(flag);
config.cxxflag(flag);
}
}

config.static_crt(static_crt);

if matches!(target_os, TargetOs::Android) {
Expand Down

0 comments on commit 5a4dbd4

Please sign in to comment.