Skip to content

Commit 5f92463

Browse files
authored
Merge pull request #34084 from meg-gupta/revertomefuncpassandcopyfwd
2 parents f436b60 + 77a76a8 commit 5f92463

File tree

11 files changed

+73
-106
lines changed

11 files changed

+73
-106
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,13 +1542,6 @@ template <typename ImplClass>
15421542
void SILCloner<ImplClass>::visitUncheckedValueCastInst(
15431543
UncheckedValueCastInst *Inst) {
15441544
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1545-
if (!getBuilder().hasOwnership()) {
1546-
recordClonedInstruction(Inst, getBuilder().createUncheckedBitwiseCast(
1547-
getOpLocation(Inst->getLoc()),
1548-
getOpValue(Inst->getOperand()),
1549-
getOpType(Inst->getType())));
1550-
return;
1551-
}
15521545
recordClonedInstruction(Inst, getBuilder().createUncheckedValueCast(
15531546
getOpLocation(Inst->getLoc()),
15541547
getOpValue(Inst->getOperand()),

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,6 @@ class SILModule {
263263
/// to ensure that the module is serialized only once.
264264
bool serialized;
265265

266-
/// Set if we have registered a deserialization notification handler for
267-
/// lowering ownership in non transparent functions.
268-
/// This gets set in NonTransparent OwnershipModelEliminator pass.
269-
bool regDeserializationNotificationHandlerForNonTransparentFuncOME;
270-
/// Set if we have registered a deserialization notification handler for
271-
/// lowering ownership in transparent functions.
272-
/// This gets set in OwnershipModelEliminator pass.
273-
bool regDeserializationNotificationHandlerForAllFuncOME;
274-
275266
/// Action to be executed for serializing the SILModule.
276267
ActionCallback SerializeSILAction;
277268

@@ -310,19 +301,6 @@ class SILModule {
310301
deserializationNotificationHandlers.erase(handler);
311302
}
312303

313-
bool hasRegisteredDeserializationNotificationHandlerForNonTransparentFuncOME() {
314-
return regDeserializationNotificationHandlerForNonTransparentFuncOME;
315-
}
316-
bool hasRegisteredDeserializationNotificationHandlerForAllFuncOME() {
317-
return regDeserializationNotificationHandlerForAllFuncOME;
318-
}
319-
void setRegisteredDeserializationNotificationHandlerForNonTransparentFuncOME() {
320-
regDeserializationNotificationHandlerForNonTransparentFuncOME = true;
321-
}
322-
void setRegisteredDeserializationNotificationHandlerForAllFuncOME() {
323-
regDeserializationNotificationHandlerForAllFuncOME = true;
324-
}
325-
326304
/// Add a delete notification handler \p Handler to the module context.
327305
void registerDeleteNotificationHandler(DeleteNotificationHandler* Handler);
328306

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,6 @@ class SILPassManager {
281281
/// Run the passes in Transform from \p FromTransIdx to \p ToTransIdx.
282282
void runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx);
283283

284-
/// Helper function to check if the function pass should be run mandatorily
285-
/// All passes in mandatory pass pipeline and ownership model elimination are
286-
/// mandatory function passes.
287-
bool isMandatoryFunctionPass(SILFunctionTransform *);
288-
289284
/// A helper function that returns (based on SIL stage and debug
290285
/// options) whether we should continue running passes.
291286
bool continueTransforming();

lib/SIL/IR/SILModule.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ class SILModule::SerializationCallback final
9595
SILModule::SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
9696
Lowering::TypeConverter &TC, const SILOptions &Options)
9797
: Stage(SILStage::Raw), Options(Options), serialized(false),
98-
regDeserializationNotificationHandlerForNonTransparentFuncOME(false),
99-
regDeserializationNotificationHandlerForAllFuncOME(false),
10098
SerializeSILAction(), Types(TC) {
10199
assert(!context.isNull());
102100
if (auto *file = context.dyn_cast<FileUnit *>()) {

lib/SILOptimizer/Mandatory/OwnershipModelEliminator.cpp

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static void prepareSILFunctionForOptimization(ModuleDecl *, SILFunction *F) {
370370

371371
namespace {
372372

373-
struct OwnershipModelEliminator : SILFunctionTransform {
373+
struct OwnershipModelEliminator : SILModuleTransform {
374374
bool SkipTransparent;
375375
bool SkipStdlibModule;
376376

@@ -379,50 +379,53 @@ struct OwnershipModelEliminator : SILFunctionTransform {
379379

380380
void run() override {
381381
if (DumpBefore.size()) {
382-
getFunction()->dump(DumpBefore.c_str());
382+
getModule()->dump(DumpBefore.c_str());
383383
}
384384

385-
auto *F = getFunction();
386-
auto &Mod = getFunction()->getModule();
385+
auto &Mod = *getModule();
387386

388387
// If we are supposed to skip the stdlib module and we are in the stdlib
389388
// module bail.
390389
if (SkipStdlibModule && Mod.isStdlibModule()) {
391390
return;
392391
}
393392

394-
if (!F->hasOwnership())
395-
return;
396-
397-
// If we were asked to not strip ownership from transparent functions in
398-
// /our/ module, return.
399-
if (SkipTransparent && F->isTransparent())
400-
return;
401-
402-
// Verify here to make sure ownership is correct before we strip.
403-
{
404-
// Add a pretty stack trace entry to tell users who see a verification
405-
// failure triggered by this verification check that they need to re-run
406-
// with -sil-verify-all to actually find the pass that introduced the
407-
// verification error.
408-
//
409-
// DISCUSSION: This occurs due to the crash from the verification
410-
// failure happening in the pass itself. This causes us to dump the
411-
// SILFunction and emit a msg that this pass (OME) is the culprit. This
412-
// is generally correct for most passes, but not for OME since we are
413-
// verifying before we have even modified the function to ensure that
414-
// all ownership invariants have been respected before we lower
415-
// ownership from the function.
416-
llvm::PrettyStackTraceString silVerifyAllMsgOnFailure(
417-
"Found verification error when verifying before lowering "
418-
"ownership. Please re-run with -sil-verify-all to identify the "
419-
"actual pass that introduced the verification error.");
420-
F->verify();
421-
}
393+
for (auto &F : Mod) {
394+
// If F does not have ownership, skip it. We have no further work to do.
395+
if (!F.hasOwnership())
396+
continue;
397+
398+
// If we were asked to not strip ownership from transparent functions in
399+
// /our/ module, continue.
400+
if (SkipTransparent && F.isTransparent())
401+
continue;
402+
403+
// Verify here to make sure ownership is correct before we strip.
404+
{
405+
// Add a pretty stack trace entry to tell users who see a verification
406+
// failure triggered by this verification check that they need to re-run
407+
// with -sil-verify-all to actually find the pass that introduced the
408+
// verification error.
409+
//
410+
// DISCUSSION: This occurs due to the crash from the verification
411+
// failure happening in the pass itself. This causes us to dump the
412+
// SILFunction and emit a msg that this pass (OME) is the culprit. This
413+
// is generally correct for most passes, but not for OME since we are
414+
// verifying before we have even modified the function to ensure that
415+
// all ownership invariants have been respected before we lower
416+
// ownership from the function.
417+
llvm::PrettyStackTraceString silVerifyAllMsgOnFailure(
418+
"Found verification error when verifying before lowering "
419+
"ownership. Please re-run with -sil-verify-all to identify the "
420+
"actual pass that introduced the verification error.");
421+
F.verify();
422+
}
422423

423-
if (stripOwnership(*F)) {
424-
auto InvalidKind = SILAnalysis::InvalidationKind::BranchesAndInstructions;
425-
invalidateAnalysis(InvalidKind);
424+
if (stripOwnership(F)) {
425+
auto InvalidKind =
426+
SILAnalysis::InvalidationKind::BranchesAndInstructions;
427+
invalidateAnalysis(&F, InvalidKind);
428+
}
426429
}
427430

428431
// If we were asked to strip transparent, we are at the beginning of the
@@ -432,19 +435,12 @@ struct OwnershipModelEliminator : SILFunctionTransform {
432435
FunctionBodyDeserializationNotificationHandler;
433436
std::unique_ptr<DeserializationNotificationHandler> ptr;
434437
if (SkipTransparent) {
435-
if (!Mod.hasRegisteredDeserializationNotificationHandlerForNonTransparentFuncOME()) {
436-
ptr.reset(new NotificationHandlerTy(
437-
prepareNonTransparentSILFunctionForOptimization));
438-
Mod.registerDeserializationNotificationHandler(std::move(ptr));
439-
Mod.setRegisteredDeserializationNotificationHandlerForNonTransparentFuncOME();
440-
}
438+
ptr.reset(new NotificationHandlerTy(
439+
prepareNonTransparentSILFunctionForOptimization));
441440
} else {
442-
if (!Mod.hasRegisteredDeserializationNotificationHandlerForAllFuncOME()) {
443-
ptr.reset(new NotificationHandlerTy(prepareSILFunctionForOptimization));
444-
Mod.registerDeserializationNotificationHandler(std::move(ptr));
445-
Mod.setRegisteredDeserializationNotificationHandlerForAllFuncOME();
446-
}
441+
ptr.reset(new NotificationHandlerTy(prepareSILFunctionForOptimization));
447442
}
443+
Mod.registerDeserializationNotificationHandler(std::move(ptr));
448444
}
449445
};
450446

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -383,35 +383,21 @@ void SILPassManager::dumpPassInfo(const char *Title, unsigned TransIdx,
383383
llvm::dbgs() << '\n';
384384
}
385385

386-
bool SILPassManager::isMandatoryFunctionPass(SILFunctionTransform *sft) {
387-
return isMandatory || sft->getPassKind() ==
388-
PassKind::NonTransparentFunctionOwnershipModelEliminator ||
389-
sft->getPassKind() == PassKind::OwnershipModelEliminator ||
390-
sft->getPassKind() ==
391-
PassKind::NonStdlibNonTransparentFunctionOwnershipModelEliminator;
392-
}
393-
394386
void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
395387

396388
assert(analysesUnlocked() && "Expected all analyses to be unlocked!");
397389

398390
auto *SFT = cast<SILFunctionTransform>(Transformations[TransIdx]);
399-
400-
if (!F->shouldOptimize() && !isMandatoryFunctionPass(SFT)) {
401-
return;
402-
}
403-
404391
SFT->injectPassManager(this);
405392
SFT->injectFunction(F);
406393

407394
PrettyStackTraceSILFunctionTransform X(SFT, NumPassesRun);
408395
DebugPrintEnabler DebugPrint(NumPassesRun);
409396

410397
// If nothing changed since the last run of this pass, we can skip this
411-
// pass if it is not mandatory
398+
// pass.
412399
CompletedPasses &completedPasses = CompletedPassesMap[F];
413-
if (!isMandatoryFunctionPass(SFT) &&
414-
completedPasses.test((size_t)SFT->getPassKind()) &&
400+
if (completedPasses.test((size_t)SFT->getPassKind()) &&
415401
!SILDisableSkippingPasses) {
416402
if (SILPrintPassName)
417403
dumpPassInfo("(Skip)", TransIdx, F);
@@ -527,7 +513,7 @@ runFunctionPasses(unsigned FromTransIdx, unsigned ToTransIdx) {
527513

528514
// Only include functions that are definitions, and which have not
529515
// been intentionally excluded from optimization.
530-
if (F.isDefinition())
516+
if (F.isDefinition() && (isMandatory || F.shouldOptimize()))
531517
FunctionWorklist.push_back(*I);
532518
}
533519

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,6 @@ void addFunctionPasses(SILPassPipelinePlan &P,
283283
// splits up copy_addr.
284284
P.addCopyForwarding();
285285

286-
// We earlier eliminated ownership if we are not compiling the stdlib. Now
287-
// handle the stdlib functions.
288-
P.addNonTransparentFunctionOwnershipModelEliminator();
289-
290286
// Optimize copies from a temporary (an "l-value") to a destination.
291287
P.addTempLValueOpt();
292288

@@ -493,7 +489,10 @@ static void addHighLevelFunctionPipeline(SILPassPipelinePlan &P) {
493489
// FIXME: update EagerSpecializer to be a function pass!
494490
P.addEagerSpecializer();
495491

496-
// stdlib ownership model elimination is done within addFunctionPasses
492+
// We earlier eliminated ownership if we are not compiling the stdlib. Now
493+
// handle the stdlib functions.
494+
P.addNonTransparentFunctionOwnershipModelEliminator();
495+
497496
addFunctionPasses(P, OptimizationLevelKind::HighLevel);
498497

499498
addHighLevelLoopOptPasses(P);
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
// RUN: %target-swift-frontend -g -emit-ir -Xllvm '-sil-inline-never-functions=next' %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -g -emit-ir %s | %FileCheck %s
22
// FIXME: This test should be testing a non-shadow-copied value instead.
33
for i in 0 ..< 3 {
44
// CHECK: %[[ALLOCA:[0-9]+]] = alloca %TSiSg
55
// CHECK: %i.debug = alloca i{{32|64}}
66
// CHECK-NEXT: call void @llvm.dbg.declare(metadata i{{32|64}}* %i.debug,
77
// CHECK-SAME: metadata ![[I:[0-9]+]],
8+
// CHECK: call swiftcc{{.*}} @{{.*}}next{{.*}}
9+
// CHECK: %[[LD:[0-9]+]] = load i{{32|64}}, i{{32|64}}*
10+
// CHECK: br i1 {{%.*}}, label %[[FAIL:.*]], label %[[SUCCESS:.*]],
11+
//
12+
// CHECK: [[SUCCESS]]:
13+
// CHECK: br label %[[NEXT_BB:.*]],
14+
//
15+
// CHECK: [[NEXT_BB]]:
16+
// CHECK: %[[PHI_VAL:.*]] = phi i{{32|64}} [ %[[LD]], %[[SUCCESS]] ]
17+
// CHECK: store i{{32|64}} %[[PHI_VAL]], i{{32|64}}* %i.debug
818
// CHECK: ![[I]] = !DILocalVariable(name: "i",
919
}

test/IRGen/big_types_corner_cases.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public func testGetFunc() {
204204
// CHECK: [[CALL1:%.*]] = call {{.*}} @__swift_instantiateConcreteTypeFromMangledName({{.*}} @"$sSayy22big_types_corner_cases9BigStructVcSgGMD"
205205
// CHECK: [[CALL2:%.*]] = call i8** @"$sSayy22big_types_corner_cases9BigStructVcSgGSayxGSlsWl
206206
// CHECK: call swiftcc void @"$sSlsE10firstIndex5where0B0QzSgSb7ElementQzKXE_tKF"(%TSq.{{.*}}* noalias nocapture sret %{{[0-9]+}}, i8* bitcast ({{.*}}* @"$s22big_types_corner_cases9BigStruct{{.*}}_TRTA{{(\.ptrauth)?}}" to i8*), %swift.opaque* %{{[0-9]+}}, %swift.type* %{{[0-9]+}}, i8** [[CALL2]]
207+
208+
// CHECK-LABEL: define{{( dllexport)?}}{{( protected)?}} hidden swiftcc void @"$s22big_types_corner_cases7TestBigC5test2yyF"(%T22big_types_corner_cases7TestBigC* swiftself %0)
209+
// CHECK: [[CALL1:%.*]] = call {{.*}} @__swift_instantiateConcreteTypeFromMangledName({{.*}} @"$sSaySS2ID_y22big_types_corner_cases9BigStructVcSg7handlertGMD"
210+
// CHECK: [[CALL2:%.*]] = call i8** @"$sSaySS2ID_y22big_types_corner_cases9BigStructVcSg7handlertGSayxGSlsWl"
211+
// CHECK: call swiftcc void @"$sSlss16IndexingIteratorVyxG0B0RtzrlE04makeB0ACyF"(%Ts16IndexingIteratorV{{.*}}* noalias nocapture sret {{.*}}, %swift.type* [[CALL1]], i8** [[CALL2]], %swift.opaque* noalias nocapture swiftself {{.*}})
212+
// CHECK: ret void
207213
class TestBig {
208214
typealias Handler = (BigStruct) -> Void
209215

test/SILOptimizer/copyforward_ossa.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ sil [ossa] @_TFSq4someU__fMGSqQ__FQ_GSqQ__ : $@convention(thin) <τ_0_0> (@in τ
265265

266266
sil [ossa] @_TFsoi2neU__FTGSqQ__Vs26_OptionalNilComparisonType_Sb : $@convention(thin) <τ_0_0> (@in Optional<τ_0_0>, _OptionalNilComparisonType) -> Bool
267267

268-
// CHECK-LABEL: sil hidden [ossa] @option_init :
268+
// CHECK-LABEL: sil hidden [ossa] @option_init
269269
// CHECK: alloc_stack
270270
// CHECK: alloc_stack
271271
// CHECK: alloc_stack

test/SILOptimizer/prespecialize.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
// CHECK-LABEL: sil [noinline] @$s13prespecialize4test_4sizeySaySiGz_SitF
99
//
10+
// function_ref specialized Collection<A where ...>.makeIterator() -> IndexingIterator<A>
11+
// CHECK: function_ref @$sSlss16IndexingIteratorVyxG0B0RtzrlE04makeB0ACyFSnySiG_Tg5
12+
//
13+
// function_ref specialized IndexingIterator.next() -> A.Element?
14+
// CHECK: function_ref @$ss16IndexingIteratorV4next7ElementQzSgyFSnySiG_Tg5
15+
//
1016
// Look for generic specialization <Swift.Int> of Swift.Array.subscript.getter : (Swift.Int) -> A
1117
// CHECK: function_ref @$sSayxSicigSi_Tg5
1218
// CHECK: return

0 commit comments

Comments
 (0)