Skip to content

Commit d3f482a

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 d3f482a

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-silgen -enable-implicit-dynamic -verify %s
2+
// rdar://98418860
3+
4+
// REQUIRES: objc_interop
5+
6+
import Foundation
7+
8+
@propertyWrapper
9+
struct ExamplePropertyWrapper {
10+
var wrappedValue: Bool = false
11+
}
12+
13+
@objcMembers
14+
class ExampleClass {
15+
@ExamplePropertyWrapper
16+
static var exampleProperty: Bool
17+
}
18+
19+
class ExampleCallingClass {
20+
func exampleSegfaultingCall() {
21+
ExampleClass.exampleProperty.toggle()
22+
}
23+
}

0 commit comments

Comments
 (0)