Skip to content

SILOptimizer: rename LibswiftPassInvocation -> SwiftPassInvocation #40733

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 1 commit into from
Jan 5, 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
2 changes: 1 addition & 1 deletion include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
SILInstructionResultArray getResultsImpl() const;

protected:
friend class LibswiftPassInvocation;
friend class SwiftPassInvocation;

SILInstruction() {
NumCreatedInstructions++;
Expand Down
7 changes: 0 additions & 7 deletions include/swift/SIL/SILInstructionWorklist.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,13 @@ template <typename VectorT = std::vector<SILInstruction *>,
class SILInstructionWorklist : SILInstructionWorklistBase {
BlotSetVector<SILInstruction *, VectorT, MapT> worklist;

/// For invoking Swift instruction passes in Swift.
LibswiftPassInvocation *libswiftPassInvocation = nullptr;

void operator=(const SILInstructionWorklist &rhs) = delete;
SILInstructionWorklist(const SILInstructionWorklist &worklist) = delete;

public:
SILInstructionWorklist(const char *loggingName = "InstructionWorklist")
: SILInstructionWorklistBase(loggingName) {}

void setLibswiftPassInvocation(LibswiftPassInvocation *invocation) {
libswiftPassInvocation = invocation;
}

/// Returns true if the worklist is empty.
bool isEmpty() const { return worklist.empty(); }

Expand Down
26 changes: 17 additions & 9 deletions include/swift/SILOptimizer/PassManager/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void executePassPipelinePlan(SILModule *SM, const SILPassPipelinePlan &plan,
irgen::IRGenModule *IRMod = nullptr);

/// Utility class to invoke Swift passes.
class LibswiftPassInvocation {
class SwiftPassInvocation {
/// Backlink to the pass manager.
SILPassManager *passManager;

Expand All @@ -58,12 +58,14 @@ class LibswiftPassInvocation {
/// All slabs, allocated by the pass.
SILModule::SlabList allocatedSlabs;

void endPassRunChecks();

public:
LibswiftPassInvocation(SILPassManager *passManager, SILFunction *function,
SwiftPassInvocation(SILPassManager *passManager, SILFunction *function,
SILCombiner *silCombiner) :
passManager(passManager), function(function), silCombiner(silCombiner) {}

LibswiftPassInvocation(SILPassManager *passManager) :
SwiftPassInvocation(SILPassManager *passManager) :
passManager(passManager) {}

SILPassManager *getPassManager() const { return passManager; }
Expand All @@ -81,10 +83,16 @@ class LibswiftPassInvocation {
void notifyChanges(SILAnalysis::InvalidationKind invalidationKind);

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

/// Called by the SILCombiner before the instruction pass starts running.
void startInstructionPassRun(SILInstruction *inst);

/// Called by the pass manager when the pass has finished.
void finishedPassRun();
void finishedFunctionPassRun();

/// Called by the SILCombiner when the instruction pass has finished.
void finishedInstructionPassRun();
};

/// The SIL pass manager.
Expand Down Expand Up @@ -126,7 +134,7 @@ class SILPassManager {
unsigned NumPassesRun = 0;

/// For invoking Swift passes.
LibswiftPassInvocation libswiftPassInvocation;
SwiftPassInvocation swiftPassInvocation;

/// Change notifications, collected during a bridged pass run.
SILAnalysis::InvalidationKind changeNotifications =
Expand Down Expand Up @@ -201,8 +209,8 @@ class SILPassManager {
/// pass manager.
irgen::IRGenModule *getIRGenModule() { return IRMod; }

LibswiftPassInvocation *getLibswiftPassInvocation() {
return &libswiftPassInvocation;
SwiftPassInvocation *getSwiftPassInvocation() {
return &swiftPassInvocation;
}

/// Restart the function pass pipeline on the same function
Expand Down Expand Up @@ -377,7 +385,7 @@ class SILPassManager {
void viewCallGraph();
};

inline void LibswiftPassInvocation::
inline void SwiftPassInvocation::
notifyChanges(SILAnalysis::InvalidationKind invalidationKind) {
passManager->notifyPassChanges(invalidationKind);
}
Expand Down
42 changes: 28 additions & 14 deletions lib/SILOptimizer/PassManager/PassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void swift::executePassPipelinePlan(SILModule *SM,
SILPassManager::SILPassManager(SILModule *M, bool isMandatory,
irgen::IRGenModule *IRMod)
: Mod(M), IRMod(IRMod),
libswiftPassInvocation(this),
swiftPassInvocation(this),
isMandatory(isMandatory), deserializationNotificationHandler(nullptr) {
#define ANALYSIS(NAME) \
Analyses.push_back(create##NAME##Analysis(Mod));
Expand Down Expand Up @@ -469,7 +469,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
assert(changeNotifications == SILAnalysis::InvalidationKind::Nothing
&& "change notifications not cleared");

libswiftPassInvocation.startPassRun(F);
swiftPassInvocation.startFunctionPassRun(F);

// Run it!
SFT->run();
Expand All @@ -478,7 +478,7 @@ void SILPassManager::runPassOnFunction(unsigned TransIdx, SILFunction *F) {
invalidateAnalysis(F, changeNotifications);
changeNotifications = SILAnalysis::InvalidationKind::Nothing;
}
libswiftPassInvocation.finishedPassRun();
swiftPassInvocation.finishedFunctionPassRun();

if (SILForceVerifyAll ||
SILForceVerifyAroundPass.end() !=
Expand Down Expand Up @@ -1078,13 +1078,13 @@ void SILPassManager::viewCallGraph() {
}

//===----------------------------------------------------------------------===//
// LibswiftPassInvocation
// SwiftPassInvocation
//===----------------------------------------------------------------------===//

static_assert(BridgedSlabCapacity == FixedSizeSlab::capacity,
"wrong bridged slab capacity");

FixedSizeSlab *LibswiftPassInvocation::allocSlab(FixedSizeSlab *afterSlab) {
FixedSizeSlab *SwiftPassInvocation::allocSlab(FixedSizeSlab *afterSlab) {
FixedSizeSlab *slab = passManager->getModule()->allocSlab();
if (afterSlab) {
allocatedSlabs.insert(std::next(afterSlab->getIterator()), *slab);
Expand All @@ -1094,7 +1094,7 @@ FixedSizeSlab *LibswiftPassInvocation::allocSlab(FixedSizeSlab *afterSlab) {
return slab;
}

FixedSizeSlab *LibswiftPassInvocation::freeSlab(FixedSizeSlab *slab) {
FixedSizeSlab *SwiftPassInvocation::freeSlab(FixedSizeSlab *slab) {
FixedSizeSlab *prev = nullptr;
assert(!allocatedSlabs.empty());
if (&allocatedSlabs.front() != slab)
Expand All @@ -1105,23 +1105,37 @@ FixedSizeSlab *LibswiftPassInvocation::freeSlab(FixedSizeSlab *slab) {
return prev;
}

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

void LibswiftPassInvocation::finishedPassRun() {
assert(allocatedSlabs.empty() && "StackList is leaking slabs");
void SwiftPassInvocation::startInstructionPassRun(SILInstruction *inst) {
assert(inst->getFunction() == function &&
"running instruction pass on wrong function");
}

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

void SwiftPassInvocation::finishedInstructionPassRun() {
endPassRunChecks();
}

void SwiftPassInvocation::endPassRunChecks() {
assert(allocatedSlabs.empty() && "StackList is leaking slabs");
}

//===----------------------------------------------------------------------===//
// Swift Bridging
//===----------------------------------------------------------------------===//

inline LibswiftPassInvocation *castToPassInvocation(BridgedPassContext ctxt) {
return const_cast<LibswiftPassInvocation *>(
static_cast<const LibswiftPassInvocation *>(ctxt.opaqueCtxt));
inline SwiftPassInvocation *castToPassInvocation(BridgedPassContext ctxt) {
return const_cast<SwiftPassInvocation *>(
static_cast<const SwiftPassInvocation *>(ctxt.opaqueCtxt));
}

inline FixedSizeSlab *castToSlab(BridgedSlab slab) {
Expand Down Expand Up @@ -1158,7 +1172,7 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,

void PassContext_notifyChanges(BridgedPassContext passContext,
enum ChangeNotificationKind changeKind) {
LibswiftPassInvocation *inv = castToPassInvocation(passContext);
SwiftPassInvocation *inv = castToPassInvocation(passContext);
switch (changeKind) {
case instructionsChanged:
inv->notifyChanges(SILAnalysis::InvalidationKind::Instructions);
Expand All @@ -1185,7 +1199,7 @@ SwiftInt PassContext_isSwift51RuntimeAvailable(BridgedPassContext context) {
}

BridgedAliasAnalysis PassContext_getAliasAnalysis(BridgedPassContext context) {
LibswiftPassInvocation *invocation = castToPassInvocation(context);
SwiftPassInvocation *invocation = castToPassInvocation(context);
SILPassManager *pm = invocation->getPassManager();
return {pm->getAnalysis<AliasAnalysis>(invocation->getFunction())};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/PassManager/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ static void runBridgedFunctionPass(BridgedFunctionPassRunFn &runFunction,
llvm::errs() << "SILFunction metatype is not registered\n";
abort();
}
runFunction({{f}, {passManager->getLibswiftPassInvocation()}});
runFunction({{f}, {passManager->getSwiftPassInvocation()}});
}

// Called from initializeSwiftModules().
Expand Down
15 changes: 7 additions & 8 deletions lib/SILOptimizer/SILCombiner/SILCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,19 @@ void SILCombiner::eraseInstIncludingUsers(SILInstruction *inst) {
/// Runs a Swift instruction pass.
void SILCombiner::runSwiftInstructionPass(SILInstruction *inst,
void (*runFunction)(BridgedInstructionPassCtxt)) {
Worklist.setLibswiftPassInvocation(&libswiftPassInvocation);
runFunction({ {inst->asSILNode()}, {&libswiftPassInvocation} });
Worklist.setLibswiftPassInvocation(nullptr);
libswiftPassInvocation.finishedPassRun();
swiftPassInvocation.startInstructionPassRun(inst);
runFunction({ {inst->asSILNode()}, {&swiftPassInvocation} });
swiftPassInvocation.finishedInstructionPassRun();
}

/// Registered briged instruction pass run functions.
static llvm::StringMap<BridgedInstructionPassRunFn> libswiftInstPasses;
static llvm::StringMap<BridgedInstructionPassRunFn> swiftInstPasses;
static bool passesRegistered = false;

// Called from initializeSwiftModules().
void SILCombine_registerInstructionPass(BridgedStringRef name,
BridgedInstructionPassRunFn runFn) {
libswiftInstPasses[getStringRef(name)] = runFn;
swiftInstPasses[getStringRef(name)] = runFn;
passesRegistered = true;
}

Expand All @@ -493,7 +492,7 @@ SILInstruction *SILCombiner::visit##INST(INST *inst) { \
static BridgedInstructionPassRunFn runFunction = nullptr; \
static bool runFunctionSet = false; \
if (!runFunctionSet) { \
runFunction = libswiftInstPasses[TAG]; \
runFunction = swiftInstPasses[TAG]; \
if (!runFunction && passesRegistered) { \
llvm::errs() << "Swift pass " << TAG << " is not registered\n"; \
abort(); \
Expand Down Expand Up @@ -572,7 +571,7 @@ SILTransform *swift::createSILCombine() {
// SwiftFunctionPassContext
//===----------------------------------------------------------------------===//

void LibswiftPassInvocation::eraseInstruction(SILInstruction *inst) {
void SwiftPassInvocation::eraseInstruction(SILInstruction *inst) {
if (silCombiner) {
silCombiner->eraseInstFromFunction(*inst);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/SILCombiner/SILCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class SILCombiner :
OwnershipFixupContext ownershipFixupContext;

/// For invoking Swift instruction passes.
LibswiftPassInvocation libswiftPassInvocation;
SwiftPassInvocation swiftPassInvocation;

public:
SILCombiner(SILFunctionTransform *parentTransform,
Expand Down Expand Up @@ -158,7 +158,7 @@ class SILCombiner :
[&](SILInstruction *I) { eraseInstFromFunction(*I); }),
deBlocks(&B.getFunction()),
ownershipFixupContext(getInstModCallbacks(), deBlocks),
libswiftPassInvocation(parentTransform->getPassManager(),
swiftPassInvocation(parentTransform->getPassManager(),
parentTransform->getFunction(), this) {}

bool runOnFunction(SILFunction &F);
Expand Down