Skip to content

Commit 1155f01

Browse files
authored
Rollup merge of rust-lang#138073 - tmiasko:inline-asm-critical-edges, r=bjorn3
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. Fixes rust-lang#137867. r? ``@bjorn3``
2 parents f42c933 + 02d7fc1 commit 1155f01

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

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

+44-17
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,51 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
4040
let mut new_blocks = Vec::new();
4141

4242
let cur_len = body.basic_blocks.len();
43+
let mut new_block = |source_info: SourceInfo, is_cleanup: bool, target: BasicBlock| {
44+
let block = BasicBlockData {
45+
statements: vec![],
46+
is_cleanup,
47+
terminator: Some(Terminator { source_info, kind: TerminatorKind::Goto { target } }),
48+
};
49+
let idx = cur_len + new_blocks.len();
50+
new_blocks.push(block);
51+
BasicBlock::new(idx)
52+
};
4353

4454
for block in body.basic_blocks_mut() {
4555
match block.terminator {
4656
Some(Terminator {
4757
kind: TerminatorKind::Call { target: Some(ref mut destination), unwind, .. },
4858
source_info,
4959
}) if pred_count[*destination] > 1
50-
&& (matches!(
51-
unwind,
52-
UnwindAction::Cleanup(_) | UnwindAction::Terminate(_)
53-
) || self == &AllCallEdges) =>
60+
&& (generates_invoke(unwind) || self == &AllCallEdges) =>
5461
{
5562
// It's a critical edge, break it
56-
let call_guard = BasicBlockData {
57-
statements: vec![],
58-
is_cleanup: block.is_cleanup,
59-
terminator: Some(Terminator {
60-
source_info,
61-
kind: TerminatorKind::Goto { target: *destination },
62-
}),
63-
};
64-
65-
// Get the index it will be when inserted into the MIR
66-
let idx = cur_len + new_blocks.len();
67-
new_blocks.push(call_guard);
68-
*destination = BasicBlock::new(idx);
63+
*destination = new_block(source_info, block.is_cleanup, *destination);
64+
}
65+
Some(Terminator {
66+
kind:
67+
TerminatorKind::InlineAsm {
68+
asm_macro: InlineAsmMacro::Asm,
69+
ref mut targets,
70+
ref operands,
71+
unwind,
72+
..
73+
},
74+
source_info,
75+
}) if self == &CriticalCallEdges => {
76+
let has_outputs = operands.iter().any(|op| {
77+
matches!(op, InlineAsmOperand::InOut { .. } | InlineAsmOperand::Out { .. })
78+
});
79+
let has_labels =
80+
operands.iter().any(|op| matches!(op, InlineAsmOperand::Label { .. }));
81+
if has_outputs && (has_labels || generates_invoke(unwind)) {
82+
for target in targets.iter_mut() {
83+
if pred_count[*target] > 1 {
84+
*target = new_block(source_info, block.is_cleanup, *target);
85+
}
86+
}
87+
}
6988
}
7089
_ => {}
7190
}
@@ -80,3 +99,11 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
8099
true
81100
}
82101
}
102+
103+
/// Returns true if this unwind action is code generated as an invoke as opposed to a call.
104+
fn generates_invoke(unwind: UnwindAction) -> bool {
105+
match unwind {
106+
UnwindAction::Continue | UnwindAction::Unreachable => false,
107+
UnwindAction::Cleanup(_) | UnwindAction::Terminate(_) => true,
108+
}
109+
}

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)