Files
rust/tests/mir-opt
Chris Denton 5d2375f789 Rollup merge of #139042 - compiler-errors:do-not-optimize-switchint, r=saethlin
Do not remove trivial `SwitchInt` in analysis MIR

This PR ensures that we don't prematurely remove trivial `SwitchInt` terminators which affects both the borrow-checking and runtime semantics (i.e. UB) of the code. Previously the `SimplifyCfg` optimization was removing `SwitchInt` terminators when they was "trivial", i.e. when all arms branched to the same basic block, even if that `SwitchInt` terminator had the side-effect of reading an operand which (for example) may not be initialized or may point to an invalid place in memory.

This behavior is unlike all other optimizations, which are only applied after "analysis" (i.e. borrow-checking) is finished, and which Miri disables to make sure the compiler doesn't silently remove UB.

Fixing this code "breaks" (i.e. unmasks) code that used to borrow-check but no longer does, like:

```rust
fn foo() {
    let x;
    let (0 | _) = x;
}
```

This match expression should perform a read because `_` does not shadow the `0` literal pattern, and the compiler should have to read the match scrutinee to compare it to 0. I've checked that this behavior does not actually manifest in practice via a crater run which came back clean: https://github.com/rust-lang/rust/pull/139042#issuecomment-2767436367

As a side-note, it may be tempting to suggest that this is actually a good thing or that we should preserve this behavior. If we wanted to make this work (i.e. trivially optimize out reads from matches that are redundant like `0 | _`), then we should be enabling this behavior *after* fixing this. However, I think it's kinda unprincipled, and for example other variations of the code don't even work today, e.g.:

```rust
fn foo() {
    let x;
    let (0.. | _) = x;
}
```
2025-04-19 19:30:46 +00:00
..
2025-04-15 11:14:23 +02:00
2025-03-11 09:46:34 -07:00
2025-03-03 23:30:18 +00:00
2024-06-03 14:17:16 +10:00
2024-09-27 14:40:38 +01:00
2024-09-27 14:40:38 +01:00
2024-12-02 16:19:17 +11:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2025-03-10 12:20:05 +01:00
2024-08-18 15:52:23 -07:00
2025-04-02 19:59:26 +08:00
2025-04-02 19:59:26 +08:00
2025-04-04 10:55:42 +00:00
2025-04-02 19:59:26 +08:00
2025-04-02 19:59:26 +08:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-02-13 17:21:53 +00:00
2024-08-18 15:52:23 -07:00
2025-04-06 21:41:47 +02:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-06-16 18:23:48 +02:00
2025-01-11 15:56:58 -08:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-01-28 13:50:20 -06:00
2024-06-03 14:17:16 +10:00
2024-06-03 14:17:16 +10:00
2024-08-10 12:07:17 +02:00
2024-08-10 12:07:17 +02:00
2024-06-03 14:17:16 +10:00

This folder contains tests for MIR optimizations.

The mir-opt test format emits MIR to extra files that you can automatically update by specifying --bless on the command line (just like ui tests updating .stderr files).

--blessable test format

By default 32 bit and 64 bit targets use the same dump files, which can be problematic in the presence of pointers in constants or other bit width dependent things. In that case you can add

// EMIT_MIR_FOR_EACH_BIT_WIDTH

to your test, causing separate files to be generated for 32bit and 64bit systems.

Testing a particular MIR pass

If you are only testing the behavior of a particular mir-opt pass on some specific input (as is usually the case), you should add

//@ test-mir-pass: PassName

to the top of the file. This makes sure that other passes don't run which means you'll get the input you expected and your test won't break when other code changes. This also lets you test passes that are disabled by default.

Emit a diff of the mir for a specific optimization

This is what you want most often when you want to see how an optimization changes the MIR.

// EMIT_MIR $file_name_of_some_mir_dump.diff

Emit mir after a specific optimization

Use this if you are just interested in the final state after an optimization.

// EMIT_MIR $file_name_of_some_mir_dump.after.mir

Emit mir before a specific optimization

This exists mainly for completeness and is rarely useful.

// EMIT_MIR $file_name_of_some_mir_dump.before.mir

FileCheck directives

The LLVM FileCheck tool is used to verify the contents of output MIR against CHECK directives present in the test file. This works on the runtime MIR, generated by --emit=mir, and not on the output of a individual passes.

Use // skip-filecheck to prevent FileCheck from running.

To check MIR for function foo, start with a // CHECK-LABEL fn foo( directive.

{{regex}} syntax allows to match regex.

[[name:regex]] syntax allows to bind name to a string matching regex, and refer to it as [[name]] in later directives, regex should be written not to match a leading space. Use [[my_local:_.*]] to name a local, and [[my_bb:bb.*]] to name a block.

Documentation for FileCheck is available here: https://www.llvm.org/docs/CommandGuide/FileCheck.html