Skip to content

Commit 5c3ea07

Browse files
committed
[RISCV] Do not outline CFI instructions when they are needed in EH
We saw a failure caused by unwinding with incomplete CFIs, so we can't outline CFI instructions when they are needed in EH. This is a recommit of 0d40688, which was reverted in ce83883 as related precommit test 360d44e caused some errors. Reviewed By: luismarques Differential Revision: https://reviews.llvm.org/D122634
1 parent 6ee890b commit 5c3ea07

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,12 @@ RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI,
12461246
if (MI.isPosition()) {
12471247
// We can manually strip out CFI instructions later.
12481248
if (MI.isCFIInstruction())
1249-
return outliner::InstrType::Invisible;
1249+
// If current function has exception handling code, we can't outline &
1250+
// strip these CFI instructions since it may break .eh_frame section
1251+
// needed in unwinding.
1252+
return MI.getMF()->getFunction().needsUnwindTableEntry()
1253+
? outliner::InstrType::Illegal
1254+
: outliner::InstrType::Invisible;
12501255

12511256
return outliner::InstrType::Illegal;
12521257
}

llvm/test/CodeGen/RISCV/machine-outliner-throw.ll

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
12
; RUN: llc -verify-machineinstrs -enable-machine-outliner -mattr=+m -mtriple=riscv64 < %s | FileCheck %s
23

34
; Ensure that we won't outline CFIs when they are needed in unwinding.
45

56
define i32 @func1(i32 %x) #0 {
67
; CHECK-LABEL: func1:
78
; CHECK: # %bb.0: # %entry
8-
; CHECK-NEXT: call t0, OUTLINED_FUNCTION_0
9+
; CHECK-NEXT: addi sp, sp, -16
10+
; CHECK-NEXT: .cfi_def_cfa_offset 16
11+
; CHECK-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
12+
; CHECK-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
13+
; CHECK-NEXT: .cfi_offset ra, -8
14+
; CHECK-NEXT: .cfi_offset s0, -16
15+
; CHECK-NEXT: mulw a0, a0, a0
16+
; CHECK-NEXT: addiw s0, a0, 1
17+
; CHECK-NEXT: li a0, 4
918
; CHECK-NEXT: call __cxa_allocate_exception@plt
1019
; CHECK-NEXT: sw s0, 0(a0)
1120
; CHECK-NEXT: lui a1, %hi(_ZTIi)
@@ -25,7 +34,15 @@ entry:
2534
define i32 @func2(i32 %x) #0 {
2635
; CHECK-LABEL: func2:
2736
; CHECK: # %bb.0: # %entry
28-
; CHECK-NEXT: call t0, OUTLINED_FUNCTION_0
37+
; CHECK-NEXT: addi sp, sp, -16
38+
; CHECK-NEXT: .cfi_def_cfa_offset 16
39+
; CHECK-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
40+
; CHECK-NEXT: sd s0, 0(sp) # 8-byte Folded Spill
41+
; CHECK-NEXT: .cfi_offset ra, -8
42+
; CHECK-NEXT: .cfi_offset s0, -16
43+
; CHECK-NEXT: mulw a0, a0, a0
44+
; CHECK-NEXT: addiw s0, a0, 1
45+
; CHECK-NEXT: li a0, 4
2946
; CHECK-NEXT: call __cxa_allocate_exception@plt
3047
; CHECK-NEXT: sw s0, 0(a0)
3148
; CHECK-NEXT: lui a1, %hi(_ZTIi)
@@ -42,15 +59,6 @@ entry:
4259
unreachable
4360
}
4461

45-
; CHECK-LABEL: OUTLINED_FUNCTION_0:
46-
; CHECK: # %bb.0:
47-
; CHECK-NEXT: addi sp, sp, -16
48-
; CHECK-NEXT: sd ra, 8(sp)
49-
; CHECK-NEXT: sd s0, 0(sp)
50-
; CHECK-NEXT: mulw a0, a0, a0
51-
; CHECK-NEXT: addiw s0, a0, 1
52-
; CHECK-NEXT: li a0, 4
53-
5462
@_ZTIi = external constant i8*
5563
declare i8* @__cxa_allocate_exception(i64)
5664
declare void @__cxa_throw(i8*, i8*, i8*)

0 commit comments

Comments
 (0)