Skip to content

Commit f588020

Browse files
authored
Merge pull request #62862 from eeckstein/silcombine-keypath-opt
SILCombine: a better fix for optimizing keypath-offset-of
2 parents 25c9a6a + 7c54be2 commit f588020

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,11 @@ bool SILCombiner::tryOptimizeKeypathOffsetOf(ApplyInst *AI,
343343

344344
KeyPathPattern *pattern = kp->getPattern();
345345
SubstitutionMap patternSubs = kp->getSubstitutions();
346-
CanType rootTy = pattern->getRootType().subst(patternSubs)->getCanonicalType();
346+
SILFunction *f = AI->getFunction();
347+
SILType rootTy = f->getLoweredType(Lowering::AbstractionPattern::getOpaque(),
348+
pattern->getRootType().subst(patternSubs)->getCanonicalType());
347349

348-
// TODO: support lowering of the rootTy if it's not a legal SIL type in the
349-
// first place, e.g. if it contains an AnyFunctionType.
350-
if (!rootTy->isLegalSILType())
351-
return false;
352-
353-
CanType parentTy = rootTy;
350+
SILType parentTy = rootTy;
354351

355352
// First check if _storedInlineOffset would return an offset or nil. Basically
356353
// only stored struct and tuple elements produce an offset. Everything else
@@ -387,14 +384,15 @@ bool SILCombiner::tryOptimizeKeypathOffsetOf(ApplyInst *AI,
387384
hasOffset = false;
388385
break;
389386
}
390-
parentTy = component.getComponentType();
387+
parentTy = f->getLoweredType(Lowering::AbstractionPattern::getOpaque(),
388+
component.getComponentType());
391389
}
392390

393391
SILLocation loc = AI->getLoc();
394392
SILValue result;
395393

396394
if (hasOffset) {
397-
SILType rootAddrTy = SILType::getPrimitiveAddressType(rootTy);
395+
SILType rootAddrTy = rootTy.getAddressType();
398396
SILValue rootAddr = Builder.createBaseAddrForOffset(loc, rootAddrTy);
399397

400398
auto projector = KeyPathProjector::create(kp, rootAddr, loc, Builder);

test/SILOptimizer/keypath_offset.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ func testTupleOffsets() {
159159
printOffset(TLayout.offset(of: \Tuple<Int, Int>.1))
160160
}
161161

162-
// Just check that we don't crash.
162+
// CHECK-LABEL: sil {{.*}} @$s4test0A16TupleOfFunctionsyyF
163+
// CHECK-NOT: _storedInlineOffset
164+
// CHECK-NOT: class_method
165+
// CHECK: } // end sil function '$s4test0A16TupleOfFunctionsyyF'
163166
@inline(never)
164167
func testTupleOfFunctions() {
165168
printOffset(MemoryLayout<TupleOfFunctions>.offset(of: \.b))

0 commit comments

Comments
 (0)