## Constant propagation is now on by default in nightly

Dec. 2, 2019 · Wesley Wiser on behalf of the MIR Optimizations WG

I'm pleased to announce that the Mid-level IR (MIR) constant propagation pass has been switched on by default on Rust nightly which will eventually become Rust 1.41!

## What is constant propagation?

Constant propagation is an optimization where the compiler recognizes code that can be run at compile time, evaluates it, and replaces the original code with the result.

For example:

``````const X: u32 = 2;

let y = X + X;
``````

Rather than evaluating `X + X` at runtime, the compiler can recognize that the value of `X` is known at compile time and replace it with the correct value resulting in:

``````const X: u32 = 2;

let y = 4;
``````

This optimization is opportunistic and automatically recognizes constants even when they are not declared as such:

``````struct Point {
x: u32,
y: u32,
}

let a = 2 + 2; // optimizes to 4
let b = [0, 1, 2, 3, 4, 5]; // optimizes to 3
let c = (Point { x: 21, y: 42 }).y; // optimizes to 42
``````

## Propagation into control flow

The constant propagation pass also handles propagating into control flow. For example:

``````const Foo: Option<u8> = Some(12);

let x = match Foo {
None => panic!("no value"),
Some(v) => v,
};
``````

becomes:

``````const Foo: Option<u8> = Some(12);

let x = 12;
``````

This is very helpful for checked math, the default in `debug` mode, which introduces additional control flow after every operation:

``````let x = 2 + 4 * 6;
``````

actually operates like this with overflow checking enabled:

``````let (_tmp0, overflowed) = CheckedMultiply(4, 6);
assert!(!overflowed, "attempt to multiply with overflow");

let (_tmp1, overflowed) = CheckedAdd(_tmp0, 2);
assert!(!overflowed, "attempt to add with overflow");

let x = _temp1;
``````

which adds quite a bit of control flow! Constant propagation evaluates the math at compile time and reduces this to:

``````let _tmp0 = 24;
assert!(!false, "attempt to multiply with overflow");

let _tmp1 = 26;
assert!(!false, "attempt to add with overflow");

let x = 26;
``````

which is further reduced to just:

``````let x = 26;
``````

## Compiler performance

As you might have guessed, reducing the amount of control flow processed by the Rust compiler has a positive effect on compile times. We're seeing 2-10% improvement on a variety of test cases in both debug and release mode. Even though LLVM has its own constant propagation pass, we see improvements because our pass operates on MIR while it is still generic. The more concrete instances of a generic function that are instantiated, the larger the payoff from this optimization.

We've suspected for a while that the verbose LLVM IR the Rust compiler generates contributes considerably to long compilation times. By implementing optimizations like this, we believe there is significant potential to lower compile times by generating better LLVM IR. If you'd like to get involved with the MIR Optimizations working group, stop by our Zulip channel and say hello!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8819123,"math_prob":0.95713705,"size":2788,"snap":"2021-31-2021-39","text_gpt3_token_len":677,"char_repetition_ratio":0.12428161,"word_repetition_ratio":0.059196617,"special_character_ratio":0.26111907,"punctuation_ratio":0.16545455,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9746726,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-22T14:02:47Z\",\"WARC-Record-ID\":\"<urn:uuid:1c0ea51f-e4f9-4a1b-8e3e-08a83c630b66>\",\"Content-Length\":\"11120\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f3e2502d-4060-4c62-bdfd-40a217ef746f>\",\"WARC-Concurrent-To\":\"<urn:uuid:1c3a0332-0d7a-47e3-b94c-81ef61f318f3>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://blog.rust-lang.org/inside-rust/2019/12/02/const-prop-on-by-default.html\",\"WARC-Payload-Digest\":\"sha1:6CYTIKO7FMTHGOKWBZ7L6PKMQADZ6KMA\",\"WARC-Block-Digest\":\"sha1:IWMCM5XXYZCOXV623QK32T2BJW4N7KFZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057366.40_warc_CC-MAIN-20210922132653-20210922162653-00432.warc.gz\"}"}