Skip to content

Commit 5c1733e

Browse files
committed
Break critical edges in inline asm before code generation
An inline asm terminator defines outputs along its target edges -- a fallthrough target and labeled targets. Code generation implements this by inserting code directly into the target blocks. This approach works only if the target blocks don't have other predecessors. Establish required invariant by extending existing code that breaks critical edges before code generation.
1 parent c5b7a9c commit 5c1733e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Diff for: compiler/rustc_mir_transform/src/add_call_guards.rs

+26
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,32 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
6565
// It's a critical edge, break it
6666
*destination = new_block(source_info, block.is_cleanup, *destination);
6767
}
68+
Some(Terminator {
69+
kind:
70+
TerminatorKind::InlineAsm {
71+
asm_macro: InlineAsmMacro::Asm,
72+
ref mut targets,
73+
ref operands,
74+
unwind,
75+
..
76+
},
77+
source_info,
78+
}) if self == &CriticalCallEdges => {
79+
let has_outputs = operands.iter().any(|op| {
80+
matches!(op, InlineAsmOperand::InOut { .. } | InlineAsmOperand::Out { .. })
81+
});
82+
let has_labels =
83+
operands.iter().any(|op| matches!(op, InlineAsmOperand::Label { .. }));
84+
let invoke =
85+
matches!(unwind, UnwindAction::Cleanup(_) | UnwindAction::Terminate(_));
86+
if has_outputs && (has_labels || invoke) {
87+
for target in targets.iter_mut() {
88+
if pred_count[*target] > 1 {
89+
*target = new_block(source_info, block.is_cleanup, *target);
90+
}
91+
}
92+
}
93+
}
6894
_ => {}
6995
}
7096
}

Diff for: tests/codegen/asm/critical.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ only-x86_64
2+
//@ compile-flags: -C no-prepopulate-passes
3+
#![feature(asm_goto)]
4+
#![feature(asm_goto_with_outputs)]
5+
#![crate_type = "lib"]
6+
use std::arch::asm;
7+
8+
// Regression test for #137867. Check that critical edges have been split before code generation,
9+
// and so all stores to the asm output occur on disjoint paths without any of them jumping to
10+
// another callbr label.
11+
//
12+
// CHECK-LABEL: @f(
13+
// CHECK: [[OUT:%.*]] = callbr i32 asm
14+
// CHECK-NEXT: to label %[[BB0:.*]] [label %[[BB1:.*]], label %[[BB2:.*]]],
15+
// CHECK: [[BB1]]:
16+
// CHECK-NEXT: store i32 [[OUT]], ptr %a
17+
// CHECK-NEXT: br label %[[BBR:.*]]
18+
// CHECK: [[BB2]]:
19+
// CHECK-NEXT: store i32 [[OUT]], ptr %a
20+
// CHECK-NEXT: br label %[[BBR]]
21+
// CHECK: [[BB0]]:
22+
// CHECK-NEXT: store i32 [[OUT]], ptr %a
23+
// CHECK-NEXT: br label %[[BBR]]
24+
// CHECK: [[BBR]]:
25+
// CHECK-NEXT: [[RET:%.*]] = load i32, ptr %a
26+
// CHECK-NEXT: ret i32 [[RET]]
27+
#[unsafe(no_mangle)]
28+
pub unsafe fn f(mut a: u32) -> u32 {
29+
asm!(
30+
"jmp {}
31+
jmp {}",
32+
label {},
33+
label {},
34+
inout("eax") a,
35+
);
36+
a
37+
}

0 commit comments

Comments
 (0)