Skip to content

Commit 151b44d

Browse files
committedJul 14, 2014
Support lowering of empty aggregates.
This crash was pretty common while compiling Rust for iOS (armv7). Reason - SjLj preparation step was lowering aggregate arguments as ExtractValue + InsertValue. ExtractValue has assertion which checks that there is some data in value, which is not true in case of empty (no fields) structures. Rust uses them quite extensively so this patch uses a 'select true, %val, undef' instruction to lower the argument. Patch by Valerii Hiora. llvm-svn: 212922
1 parent 5c40508 commit 151b44d

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed
 

‎llvm/lib/CodeGen/SjLjEHPrepare.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,18 @@ void SjLjEHPrepare::lowerIncomingArguments(Function &F) {
249249
++AI) {
250250
Type *Ty = AI->getType();
251251

252-
// Aggregate types can't be cast, but are legal argument types, so we have
253-
// to handle them differently. We use an extract/insert pair as a
254-
// lightweight method to achieve the same goal.
255252
if (isa<StructType>(Ty) || isa<ArrayType>(Ty)) {
256-
Instruction *EI = ExtractValueInst::Create(AI, 0, "", AfterAllocaInsPt);
257-
Instruction *NI = InsertValueInst::Create(AI, EI, 0);
258-
NI->insertAfter(EI);
259-
AI->replaceAllUsesWith(NI);
260-
261-
// Set the operand of the instructions back to the AllocaInst.
262-
EI->setOperand(0, AI);
263-
NI->setOperand(0, AI);
253+
// Aggregate types can't be cast, but are legal argument types,
254+
// so we have to handle them differently. We use
255+
// select i8 true, %arg, undef to achieve the same goal
256+
Value *TrueValue = ConstantInt::getTrue(F.getContext());
257+
Value *UndefValue = UndefValue::get(Ty);
258+
Instruction *SI = SelectInst::Create(TrueValue, AI, UndefValue,
259+
AI->getName() + ".tmp",
260+
AfterAllocaInsPt);
261+
AI->replaceAllUsesWith(SI);
262+
263+
SI->setOperand(1, AI);
264264
} else {
265265
// This is always a no-op cast because we're casting AI to AI->getType()
266266
// so src and destination types are identical. BitCast is the only
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: llc -mtriple=armv7-apple-ios -O0 < %s | FileCheck %s
2+
; RUN: llc -mtriple=armv7-apple-ios -O1 < %s | FileCheck %s
3+
; RUN: llc -mtriple=armv7-apple-ios -O2 < %s | FileCheck %s
4+
; RUN: llc -mtriple=armv7-apple-ios -O3 < %s | FileCheck %s
5+
6+
; SjLjEHPrepare shouldn't crash when lowering empty structs.
7+
;
8+
; Checks that between in case of empty structs used as arguments
9+
; nothing happens, i.e. there are no instructions between
10+
; __Unwind_SjLj_Register and actual @bar invocation
11+
12+
13+
define i8* @foo({} %c) {
14+
entry:
15+
; CHECK: bl __Unwind_SjLj_Register
16+
; CHECK-NEXT: {{[A-Z][a-zA-Z0-9]*}}:
17+
; CHECK-NEXT: bl _bar
18+
invoke void @bar ()
19+
to label %unreachable unwind label %handler
20+
21+
unreachable:
22+
unreachable
23+
24+
handler:
25+
%tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @baz to i8*)
26+
cleanup
27+
resume { i8*, i32 } undef
28+
}
29+
30+
declare void @bar()
31+
declare i32 @baz(...)

0 commit comments

Comments
 (0)
Please sign in to comment.