Skip to content

Commit ab1aa80

Browse files
committed
SIL: Don't eagerly erase instructions in builder peepholes.
Even if the operand doesn't currently have any uses after the peephole, the caller may intend to use the operand for other purposes. If the operand is really unused in the end, then dead code elimination ought to clean it up. Fixes rdar://98418860.
1 parent fb1859d commit ab1aa80

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

lib/SIL/IR/SILBuilder.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,12 @@ SILBuilder::emitDestroyValue(SILLocation Loc, SILValue Operand) {
480480

481481
SILValue SILBuilder::emitThickToObjCMetatype(SILLocation Loc, SILValue Op,
482482
SILType Ty) {
483-
// If the operand is an otherwise-unused 'metatype' instruction in the
484-
// same basic block, zap it and create a 'metatype' instruction that
485-
// directly produces an Objective-C metatype.
483+
// If the operand is a 'metatype' instruction accessing a known static type's
484+
// metadata, create a 'metatype' instruction that
485+
// directly produces the Objective-C class object representation instead.
486486
if (auto metatypeInst = dyn_cast<MetatypeInst>(Op)) {
487-
if (metatypeInst->use_empty() &&
488-
metatypeInst->getParent() == getInsertionBB()) {
489-
auto origLoc = metatypeInst->getLoc();
490-
metatypeInst->eraseFromParent();
491-
return createMetatype(origLoc, Ty);
492-
}
487+
auto origLoc = metatypeInst->getLoc();
488+
return createMetatype(origLoc, Ty);
493489
}
494490

495491
// Just create the thick_to_objc_metatype instruction.
@@ -498,16 +494,12 @@ SILValue SILBuilder::emitThickToObjCMetatype(SILLocation Loc, SILValue Op,
498494

499495
SILValue SILBuilder::emitObjCToThickMetatype(SILLocation Loc, SILValue Op,
500496
SILType Ty) {
501-
// If the operand is an otherwise-unused 'metatype' instruction in the
502-
// same basic block, zap it and create a 'metatype' instruction that
503-
// directly produces a thick metatype.
497+
// If the operand is a 'metatype' instruction accessing a known static type's
498+
// metadata, create a 'metatype' instruction that directly produces the
499+
// Swift metatype representation instead.
504500
if (auto metatypeInst = dyn_cast<MetatypeInst>(Op)) {
505-
if (metatypeInst->use_empty() &&
506-
metatypeInst->getParent() == getInsertionBB()) {
507-
auto origLoc = metatypeInst->getLoc();
508-
metatypeInst->eraseFromParent();
509-
return createMetatype(origLoc, Ty);
510-
}
501+
auto origLoc = metatypeInst->getLoc();
502+
return createMetatype(origLoc, Ty);
511503
}
512504

513505
// Just create the objc_to_thick_metatype instruction.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-emit-silgen -enable-implicit-dynamic -verify %s
2+
// rdar://98418860
3+
import Foundation
4+
5+
@propertyWrapper
6+
struct ExamplePropertyWrapper {
7+
var wrappedValue: Bool = false
8+
}
9+
10+
@objcMembers
11+
class ExampleClass {
12+
@ExamplePropertyWrapper
13+
static var exampleProperty: Bool
14+
}
15+
16+
class ExampleCallingClass {
17+
func exampleSegfaultingCall() {
18+
ExampleClass.exampleProperty.toggle()
19+
}
20+
}

0 commit comments

Comments
 (0)