-Cinline-threshold=0 doesn't have the effect of disallowing function inline

In the following code, I added the compilation option -Cinline-threshold=0, but the function test_function_base001 is still inlined. Why is it different from the official documentation?

fn test_function_base001() -> i32 {
    println!("\t[pat] test_function_base001");
    let x = 1;
    let y = 2;
    let z = 3;
    let k = x + y + z;
    k
}

pub fn test_function_base002() -> i32 {
    let res = test_function_base001();
    res + 1
}

The documentation says to set it to 0, only inlines always-inline functions. I didn't put #[inline (always)]
in front of the function. So what does "always-inline functions" mean?

Is there any compiler option that can be controlled to turn off all function inlining in rust?

-Cinline-threshold=0 allows inlining all functions. The 0 in the row you marked is for the opt-level. In other words, if you disable optimizations, the inliner will only inline #[inline(always)] functions. You could use something like -Cinline-threshold=9999999999999 to prevent inlining for all functions not marked as #[inline(always)]. To disable inlining even of #[inline(always)] functions, you have to disable the MIR inliner (-Zinline-mir=no I think) and the LLVM inliner (something with the -Cpasses argument I believe)

2 Likes

but It says where a higher threshold will allow more inlining in the documentation.
Codegen Options - The rustc book.

I tried adding -Cinline-threshold=999999999 and the test_function_base001 function is still inlined.

Right, in that case MIR inlining is likely the culprit. It probably doesn't look at -Cinline-threshold. If you are on nightly try adding -Zinline-mir=no next to your -Cinline-threshold=0.

Oh, The test result shows that the problem is caused by LLVM inlining. Does Rustc provide a general switch for disabling inlining, including llvm inline and mir inline?

There is no flag for that.