Skip to content

SIL: remove instruction leaks checking #71810

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
Feb 22, 2024
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
6 changes: 0 additions & 6 deletions include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,6 @@ class SILOptions {
/// Are we parsing the stdlib, i.e. -parse-stdlib?
bool ParseStdlib = false;

/// If true, check for leaking instructions when the SILModule is destructed.
///
/// Warning: this is not thread safe. It can only be enabled in case there
/// is a single SILModule in a single thread.
bool checkSILModuleLeaks = false;

/// Are we building in embedded Swift mode?
bool EmbeddedSwift = false;

Expand Down
5 changes: 0 additions & 5 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,11 +900,6 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
return NumDeletedInstructions;
}

static void resetInstructionCounts() {
NumCreatedInstructions = 0;
NumDeletedInstructions = 0;
}

/// Pretty-print the value.
void dump() const;
void print(raw_ostream &OS) const;
Expand Down
13 changes: 0 additions & 13 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -939,19 +939,6 @@ class SILModule {
/// Check linear OSSA lifetimes, assuming complete OSSA.
void verifyOwnership() const;

/// Check if there are any leaking instructions.
///
/// Aborts with an error if more instructions are allocated than contained in
/// the module.
void checkForLeaks() const;

/// Check if there are any leaking instructions after the SILModule is
/// destructed.
///
/// The SILModule destructor already calls checkForLeaks(). This function is
/// useful to check if the destructor itself destroys all data structures.
static void checkForLeaksAfterDestruction();

/// Pretty-print the module.
void dump(bool Verbose = false) const;

Expand Down
1 change: 0 additions & 1 deletion lib/DriverTool/sil_opt_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ int sil_opt_main(ArrayRef<const char *> argv, void *MainAddr) {
SILOpts.VerifySILOwnership = !options.DisableSILOwnershipVerifier;
SILOpts.OptRecordFile = options.RemarksFilename;
SILOpts.OptRecordPasses = options.RemarksPasses;
SILOpts.checkSILModuleLeaks = true;
SILOpts.EnableStackProtection = true;
SILOpts.EnableMoveInoutStackProtection = options.EnableMoveInoutStackProtection;

Expand Down
4 changes: 0 additions & 4 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,10 +2235,6 @@ int swift::performFrontend(ArrayRef<const char *> Args,
llvm::setBugReportMsg(nullptr);
}

/// Enable leaks checking because this SILModule is the only one in the process
/// (leaks checking is not thread safe).
Invocation.getSILOptions().checkSILModuleLeaks = true;

PrettyStackTraceFrontend frontendTrace(Invocation);

// Make an array of PrettyStackTrace objects to dump the configuration files
Expand Down
6 changes: 0 additions & 6 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,7 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
// Free the memory occupied by the SILModule.
// Execute this task in parallel to the embedding of bitcode.
auto SILModuleRelease = [&SILMod]() {
bool checkForLeaks = SILMod->getOptions().checkSILModuleLeaks;
SILMod.reset(nullptr);
if (checkForLeaks)
SILModule::checkForLeaksAfterDestruction();
};
auto Thread = std::thread(SILModuleRelease);
// Wait for the thread to terminate.
Expand Down Expand Up @@ -1546,10 +1543,7 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
// Free the memory occupied by the SILModule.
// Execute this task in parallel to the LLVM compilation.
auto SILModuleRelease = [&SILMod]() {
bool checkForLeaks = SILMod->getOptions().checkSILModuleLeaks;
SILMod.reset(nullptr);
if (checkForLeaks)
SILModule::checkForLeaksAfterDestruction();
};
auto releaseModuleThread = std::thread(SILModuleRelease);

Expand Down
61 changes: 0 additions & 61 deletions lib/SIL/IR/SILModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ SILModule::SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,

SILModule::~SILModule() {
#ifndef NDEBUG
checkForLeaks();

NumSlabsAllocated += numAllocatedSlabs;
assert(numAllocatedSlabs == freeSlabs.size() && "leaking slabs in SILModule");
#endif
Expand Down Expand Up @@ -163,65 +161,6 @@ SILModule::~SILModule() {
flushDeletedInsts();
}

void SILModule::checkForLeaks() const {

/// Leak checking is not thread safe, because the instruction counters are
/// global non-atomic variables. Leak checking can only be done in case there
/// is a single SILModule in a single thread.
if (!getOptions().checkSILModuleLeaks)
return;

int instsInModule = scheduledForDeletion.size();

for (const SILFunction &F : *this) {
const SILFunction *sn = &F;
do {
for (const SILBasicBlock &block : *sn) {
instsInModule += std::distance(block.begin(), block.end());
}
} while ((sn = sn->snapshots) != nullptr);
}
for (const SILFunction &F : zombieFunctions) {
const SILFunction *sn = &F;
do {
for (const SILBasicBlock &block : F) {
instsInModule += std::distance(block.begin(), block.end());
}
} while ((sn = sn->snapshots) != nullptr);
}
for (const SILGlobalVariable &global : getSILGlobals()) {
instsInModule += std::distance(global.StaticInitializerBlock.begin(),
global.StaticInitializerBlock.end());
}

int numAllocated = SILInstruction::getNumCreatedInstructions() -
SILInstruction::getNumDeletedInstructions();

if (numAllocated != instsInModule) {
llvm::errs() << "Leaking instructions!\n";
llvm::errs() << "Allocated instructions: " << numAllocated << '\n';
llvm::errs() << "Instructions in module: " << instsInModule << '\n';
llvm_unreachable("leaking instructions");
}

assert(PlaceholderValue::getNumPlaceholderValuesAlive() == 0 &&
"leaking placeholders");
}

void SILModule::checkForLeaksAfterDestruction() {
// Disabled in release (non-assert) builds because this check fails in rare
// cases in lldb, causing crashes. rdar://70826934
#ifndef NDEBUG
int numAllocated = SILInstruction::getNumCreatedInstructions() -
SILInstruction::getNumDeletedInstructions();

if (numAllocated != 0) {
llvm::errs() << "Leaking " << numAllocated << " instructions!\n";
llvm_unreachable("leaking instructions");
}
#endif
}

std::unique_ptr<SILModule> SILModule::createEmptyModule(
llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
Lowering::TypeConverter &TC, const SILOptions &Options,
Expand Down
3 changes: 0 additions & 3 deletions lib/SIL/Parser/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ ParseSILModuleRequest::evaluate(Evaluator &evaluator,
auto bufferID = SF->getBufferID();
assert(bufferID);

// For leak detection.
SILInstruction::resetInstructionCounts();

auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
desc.opts);
SILParserState parserState(*silMod.get());
Expand Down
2 changes: 0 additions & 2 deletions lib/SIL/Verifier/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7255,8 +7255,6 @@ void SILModule::verify(CalleeCache *calleeCache,
if (!verificationEnabled(*this))
return;

checkForLeaks();

// Uniquing set to catch symbol name collisions.
llvm::DenseSet<StringRef> symbolNames;

Expand Down
3 changes: 0 additions & 3 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,9 +2115,6 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
return evaluateOrFatal(evaluator, ParseSILModuleRequest{desc});
}

// For leak detection.
SILInstruction::resetInstructionCounts();

auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
desc.opts, desc.irgenOptions);

Expand Down