Skip to content

Swift SIL: support for sub-pass bisecting: add PassContext.continueWithNextSubpassRun #52196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ let releaseDevirtualizerPass = FunctionPass(
// these we know that they don't have associated objects, which are
// _not_ released by the deinit method.
if let deallocStackRef = instruction as? DeallocStackRefInst {
if !context.continueWithNextSubpassRun(for: release) {
return
}
tryDevirtualizeReleaseOfObject(context, release, deallocStackRef)
lastRelease = nil
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ struct PassContext {

let _bridged: BridgedPassContext

func continueWithNextSubpassRun(for inst: Instruction? = nil) -> Bool {
let bridgedInst = OptionalBridgedInstruction(obj: inst?.bridged.obj)
return PassContext_continueWithNextSubpassRun(_bridged, bridgedInst) != 0
}

var aliasAnalysis: AliasAnalysis {
let bridgedAA = PassContext_getAliasAnalysis(_bridged)
return AliasAnalysis(bridged: bridgedAA)
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void Function_register(SwiftMetatype metatype,
FunctionCopyEffectsFn copyEffectsFn,
FunctionGetEffectFlagsFn hasEffectsFn);

SwiftInt PassContext_continueWithNextSubpassRun(BridgedPassContext passContext,
OptionalBridgedInstruction inst);
void PassContext_notifyChanges(BridgedPassContext passContext,
enum ChangeNotificationKind changeKind);
void PassContext_eraseInstruction(BridgedPassContext passContext,
Expand Down
6 changes: 6 additions & 0 deletions include/swift/SIL/SILBridgingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ template <class I = SILInstruction> I *castToInst(BridgedInstruction inst) {
return cast<I>(static_cast<SILNode *>(inst.obj)->castToInstruction());
}

template <class I = SILInstruction> I *castToInst(OptionalBridgedInstruction inst) {
if (!inst.obj)
return nullptr;
return cast<I>(static_cast<SILNode *>(inst.obj)->castToInstruction());
}

inline SILBasicBlock *castToBasicBlock(BridgedBasicBlock block) {
return static_cast<SILBasicBlock *>(block.obj);
}
Expand Down
7 changes: 6 additions & 1 deletion include/swift/SILOptimizer/PassManager/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class SwiftPassInvocation {
/// Backlink to the pass manager.
SILPassManager *passManager;

/// The current transform.
SILTransform *transform = nullptr;

/// The currently optimized function.
SILFunction *function = nullptr;

Expand All @@ -76,6 +79,8 @@ class SwiftPassInvocation {
passManager(passManager) {}

SILPassManager *getPassManager() const { return passManager; }

SILTransform *getTransform() const { return transform; }

SILFunction *getFunction() const { return function; }

Expand All @@ -94,7 +99,7 @@ class SwiftPassInvocation {
void notifyChanges(SILAnalysis::InvalidationKind invalidationKind);

/// Called by the pass manager before the pass starts running.
void startFunctionPassRun(SILFunction *function);
void startFunctionPassRun(SILFunctionTransform *transform);

/// Called by the SILCombiner before the instruction pass starts running.
void startInstructionPassRun(SILInstruction *inst);
Expand Down
20 changes: 15 additions & 5 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
assert(changeNotifications == SILAnalysis::InvalidationKind::Nothing
&& "change notifications not cleared");

swiftPassInvocation.startFunctionPassRun(F);
swiftPassInvocation.startFunctionPassRun(SFT);

// Run it!
SFT->run();
Expand Down Expand Up @@ -1209,9 +1209,10 @@ void SwiftPassInvocation::freeBlockSet(BasicBlockSet *set) {
}
}

void SwiftPassInvocation::startFunctionPassRun(SILFunction *function) {
assert(!this->function && "a pass is already running");
this->function = function;
void SwiftPassInvocation::startFunctionPassRun(SILFunctionTransform *transform) {
assert(!this->function && !this->transform && "a pass is already running");
this->function = transform->getFunction();
this->transform = transform;
}

void SwiftPassInvocation::startInstructionPassRun(SILInstruction *inst) {
Expand All @@ -1221,8 +1222,9 @@ void SwiftPassInvocation::startInstructionPassRun(SILInstruction *inst) {

void SwiftPassInvocation::finishedFunctionPassRun() {
endPassRunChecks();
assert(function && "not running a pass");
assert(function && transform && "not running a pass");
function = nullptr;
transform = nullptr;
}

void SwiftPassInvocation::finishedInstructionPassRun() {
Expand Down Expand Up @@ -1282,6 +1284,14 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
return toBridgedSlab(inv->freeSlab(castToSlab(slab)));
}

SwiftInt PassContext_continueWithNextSubpassRun(BridgedPassContext passContext,
OptionalBridgedInstruction inst) {
SwiftPassInvocation *inv = castToPassInvocation(passContext);
SILInstruction *i = castToInst(inst);
return inv->getPassManager()->continueWithNextSubpassRun(i,
inv->getFunction(), inv->getTransform()) ? 1: 0;
}

void PassContext_notifyChanges(BridgedPassContext passContext,
enum ChangeNotificationKind changeKind) {
SwiftPassInvocation *inv = castToPassInvocation(passContext);
Expand Down