Skip to content

[rebranch][GlobalISel] Ensure that translateInvoke adds all successors for inlineasm #3596

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 1 commit into from
Dec 1, 2021
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
47 changes: 21 additions & 26 deletions llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2515,41 +2515,31 @@ bool IRTranslator::translateInvoke(const User &U,
if (!isa<LandingPadInst>(EHPadBB->getFirstNonPHI()))
return false;

bool LowerInlineAsm = false;
if (I.isInlineAsm()) {
const InlineAsm *IA = cast<InlineAsm>(I.getCalledOperand());
if (!IA->canThrow()) {
// Fast path without emitting EH_LABELs.

if (!translateInlineAsm(I, MIRBuilder))
return false;

MachineBasicBlock *InvokeMBB = &MIRBuilder.getMBB(),
*ReturnMBB = &getMBB(*ReturnBB);

// Update successor info.
addSuccessorWithProb(InvokeMBB, ReturnMBB, BranchProbability::getOne());

MIRBuilder.buildBr(*ReturnMBB);
return true;
} else {
LowerInlineAsm = true;
}
}
bool LowerInlineAsm = I.isInlineAsm();
bool NeedEHLabel = true;
// If it can't throw then use a fast-path without emitting EH labels.
if (LowerInlineAsm)
NeedEHLabel = (cast<InlineAsm>(I.getCalledOperand()))->canThrow();

// Emit the actual call, bracketed by EH_LABELs so that the MF knows about
// the region covered by the try.
MCSymbol *BeginSymbol = Context.createTempSymbol();
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
MCSymbol *BeginSymbol = nullptr;
if (NeedEHLabel) {
BeginSymbol = Context.createTempSymbol();
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
}

if (LowerInlineAsm) {
if (!translateInlineAsm(I, MIRBuilder))
return false;
} else if (!translateCallBase(I, MIRBuilder))
return false;

MCSymbol *EndSymbol = Context.createTempSymbol();
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
MCSymbol *EndSymbol = nullptr;
if (NeedEHLabel) {
EndSymbol = Context.createTempSymbol();
MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
}

SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
BranchProbabilityInfo *BPI = FuncInfo.BPI;
Expand All @@ -2571,7 +2561,12 @@ bool IRTranslator::translateInvoke(const User &U,
}
InvokeMBB->normalizeSuccProbs();

MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
if (NeedEHLabel) {
assert(BeginSymbol && "Expected a begin symbol!");
assert(EndSymbol && "Expected an end symbol!");
MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
}

MIRBuilder.buildBr(ReturnMBB);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
; RUN: llc -O0 -global-isel -stop-after=irtranslator < %s | FileCheck %s

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
Expand All @@ -6,19 +7,45 @@ target triple = "aarch64-unknown-linux-gnu"
@.str.2 = private unnamed_addr constant [7 x i8] c"Boom!\0A\00", align 1

define dso_local void @trap() {
; CHECK-LABEL: name: trap
; CHECK: bb.1.entry:
entry:
unreachable
}

define dso_local void @test() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
; CHECK-LABEL: name: test
; CHECK: bb.1.entry:
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.3(0x40000000)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[GV:%[0-9]+]]:_(p0) = G_GLOBAL_VALUE @.str.2
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY [[GV]](p0)
; CHECK-NEXT: EH_LABEL <mcsymbol >
; CHECK-NEXT: INLINEASM &"bl trap", 1 /* sideeffect attdialect */
; CHECK-NEXT: EH_LABEL <mcsymbol >
; CHECK-NEXT: G_BR %bb.2
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2.invoke.cont:
; CHECK-NEXT: RET_ReallyLR
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.3.lpad (landing-pad):
; CHECK-NEXT: liveins: $x0, $x1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: EH_LABEL <mcsymbol >
; CHECK-NEXT: [[DEF:%[0-9]+]]:_(s128) = G_IMPLICIT_DEF
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(p0) = COPY $x0
; CHECK-NEXT: [[COPY2:%[0-9]+]]:_(p0) = COPY $x1
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY2]](p0)
; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp
; CHECK-NEXT: $x0 = COPY [[COPY]](p0)
; CHECK-NEXT: BL @printf, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0
; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp
; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp
; CHECK-NEXT: $x0 = COPY [[COPY1]](p0)
; CHECK-NEXT: BL @_Unwind_Resume, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0
; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp
entry:

; CHECK-LABEL: name: test
; CHECK: body:
; CHECK-NEXT: bb.1.entry
; CHECK: EH_LABEL
; CHECK-NEXT: INLINEASM
; CHECK-NEXT: EH_LABEL

invoke void asm sideeffect unwind "bl trap", ""()
to label %invoke.cont unwind label %lpad
Expand All @@ -27,8 +54,6 @@ invoke.cont:
ret void

lpad:
; CHECK: bb.3.lpad
; CHECK: EH_LABEL

%0 = landingpad { i8*, i32 }
cleanup
Expand All @@ -37,6 +62,40 @@ lpad:

}

define void @test2() #0 personality i32 (...)* @__gcc_personality_v0 {
; CHECK-LABEL: name: test2
; CHECK: bb.1 (%ir-block.0):
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.3(0x40000000)
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[DEF:%[0-9]+]]:_(p0) = G_IMPLICIT_DEF
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr64common = COPY [[DEF]](p0)
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */, 1572873 /* reguse:GPR64common */, [[COPY]]
; CHECK-NEXT: G_BR %bb.2
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2.a:
; CHECK-NEXT: RET_ReallyLR
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.3.b (landing-pad):
; CHECK-NEXT: liveins: $x0, $x1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: EH_LABEL <mcsymbol >
; CHECK-NEXT: [[DEF1:%[0-9]+]]:_(s128) = G_IMPLICIT_DEF
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(p0) = COPY $x0
; CHECK-NEXT: [[COPY2:%[0-9]+]]:_(p0) = COPY $x1
; CHECK-NEXT: [[PTRTOINT:%[0-9]+]]:_(s32) = G_PTRTOINT [[COPY2]](p0)
; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def $sp, implicit $sp
; CHECK-NEXT: $x0 = COPY [[COPY1]](p0)
; CHECK-NEXT: BL @_Unwind_Resume, csr_aarch64_aapcs, implicit-def $lr, implicit $sp, implicit $x0
; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def $sp, implicit $sp
invoke void asm sideeffect "", "r"(i64* undef) to label %a unwind label %b
a:
ret void
b:
%landing_pad = landingpad { i8*, i32 } cleanup
resume { i8*, i32 } %landing_pad
}

declare i32 @__gcc_personality_v0(...)
declare dso_local i32 @__gxx_personality_v0(...)

declare dso_local void @printf(i8*, ...)