Skip to content

Fix miscompilation of inline assembly with outputs in cases where we emit an invoke instead of call instruction. #95864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ impl<'ll, 'tcx> AsmBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
}
attributes::apply_to_callsite(result, llvm::AttributePlace::Function, &{ attrs });

// Switch to the 'normal' basic block if we did an `invoke` instead of a `call`
if let Some((dest, _, _)) = dest_catch_funclet {
self.switch_to_block(dest);
}

// Write results to outputs
for (idx, op) in operands.iter().enumerate() {
if let InlineAsmOperandRef::Out { reg, place: Some(place), .. }
Expand Down
17 changes: 15 additions & 2 deletions src/test/codegen/asm-may_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ impl Drop for Foo {
}
}

// CHECK-LABEL: @may_unwind
// CHECK-LABEL: @asm_may_unwind
#[no_mangle]
pub unsafe fn may_unwind() {
pub unsafe fn asm_may_unwind() {
let _m = Foo;
// CHECK: invoke void asm sideeffect alignstack inteldialect unwind ""
asm!("", options(may_unwind));
}

// CHECK-LABEL: @asm_with_result_may_unwind
#[no_mangle]
pub unsafe fn asm_with_result_may_unwind() -> u64 {
let _m = Foo;
let res: u64;
// CHECK: [[RES:%[0-9]+]] = invoke i64 asm sideeffect alignstack inteldialect unwind
// CHECK-NEXT: to label %[[NORMALBB:[a-b0-9]+]]
asm!("mov {}, 1", out(reg) res, options(may_unwind));
// CHECK: [[NORMALBB]]:
// CHECK: ret i64 [[RES:%[0-9]+]]
res
}