Skip to content

Commit 3488cd3

Browse files
committed
SIL: remove instruction leaks checking
It's not thread safe and can cause false alarms in case multiple modules exist in different threads. E.g. when building swiftmodules from interfaces. The leaking check is not important anymore because the builder APIs enforce that instructions are not leaking. I.e. it's not possible to create an instruction without inserting it into a basic block. Also, it's not possible to remove an instruction from a block without deleting it. rdar://122169263
1 parent 47ae9bd commit 3488cd3

File tree

10 files changed

+0
-104
lines changed

10 files changed

+0
-104
lines changed

include/swift/AST/SILOptions.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,6 @@ class SILOptions {
270270
/// Are we parsing the stdlib, i.e. -parse-stdlib?
271271
bool ParseStdlib = false;
272272

273-
/// If true, check for leaking instructions when the SILModule is destructed.
274-
///
275-
/// Warning: this is not thread safe. It can only be enabled in case there
276-
/// is a single SILModule in a single thread.
277-
bool checkSILModuleLeaks = false;
278-
279273
/// Are we building in embedded Swift mode?
280274
bool EmbeddedSwift = false;
281275

include/swift/SIL/SILInstruction.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -900,11 +900,6 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
900900
return NumDeletedInstructions;
901901
}
902902

903-
static void resetInstructionCounts() {
904-
NumCreatedInstructions = 0;
905-
NumDeletedInstructions = 0;
906-
}
907-
908903
/// Pretty-print the value.
909904
void dump() const;
910905
void print(raw_ostream &OS) const;

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -939,19 +939,6 @@ class SILModule {
939939
/// Check linear OSSA lifetimes, assuming complete OSSA.
940940
void verifyOwnership() const;
941941

942-
/// Check if there are any leaking instructions.
943-
///
944-
/// Aborts with an error if more instructions are allocated than contained in
945-
/// the module.
946-
void checkForLeaks() const;
947-
948-
/// Check if there are any leaking instructions after the SILModule is
949-
/// destructed.
950-
///
951-
/// The SILModule destructor already calls checkForLeaks(). This function is
952-
/// useful to check if the destructor itself destroys all data structures.
953-
static void checkForLeaksAfterDestruction();
954-
955942
/// Pretty-print the module.
956943
void dump(bool Verbose = false) const;
957944

lib/DriverTool/sil_opt_main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,6 @@ int sil_opt_main(ArrayRef<const char *> argv, void *MainAddr) {
708708
SILOpts.VerifySILOwnership = !options.DisableSILOwnershipVerifier;
709709
SILOpts.OptRecordFile = options.RemarksFilename;
710710
SILOpts.OptRecordPasses = options.RemarksPasses;
711-
SILOpts.checkSILModuleLeaks = true;
712711
SILOpts.EnableStackProtection = true;
713712
SILOpts.EnableMoveInoutStackProtection = options.EnableMoveInoutStackProtection;
714713

lib/FrontendTool/FrontendTool.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,10 +2235,6 @@ int swift::performFrontend(ArrayRef<const char *> Args,
22352235
llvm::setBugReportMsg(nullptr);
22362236
}
22372237

2238-
/// Enable leaks checking because this SILModule is the only one in the process
2239-
/// (leaks checking is not thread safe).
2240-
Invocation.getSILOptions().checkSILModuleLeaks = true;
2241-
22422238
PrettyStackTraceFrontend frontendTrace(Invocation);
22432239

22442240
// Make an array of PrettyStackTrace objects to dump the configuration files

lib/IRGen/IRGen.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,7 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
12311231
// Free the memory occupied by the SILModule.
12321232
// Execute this task in parallel to the embedding of bitcode.
12331233
auto SILModuleRelease = [&SILMod]() {
1234-
bool checkForLeaks = SILMod->getOptions().checkSILModuleLeaks;
12351234
SILMod.reset(nullptr);
1236-
if (checkForLeaks)
1237-
SILModule::checkForLeaksAfterDestruction();
12381235
};
12391236
auto Thread = std::thread(SILModuleRelease);
12401237
// Wait for the thread to terminate.
@@ -1546,10 +1543,7 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
15461543
// Free the memory occupied by the SILModule.
15471544
// Execute this task in parallel to the LLVM compilation.
15481545
auto SILModuleRelease = [&SILMod]() {
1549-
bool checkForLeaks = SILMod->getOptions().checkSILModuleLeaks;
15501546
SILMod.reset(nullptr);
1551-
if (checkForLeaks)
1552-
SILModule::checkForLeaksAfterDestruction();
15531547
};
15541548
auto releaseModuleThread = std::thread(SILModuleRelease);
15551549

lib/SIL/IR/SILModule.cpp

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ SILModule::SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
126126

127127
SILModule::~SILModule() {
128128
#ifndef NDEBUG
129-
checkForLeaks();
130-
131129
NumSlabsAllocated += numAllocatedSlabs;
132130
assert(numAllocatedSlabs == freeSlabs.size() && "leaking slabs in SILModule");
133131
#endif
@@ -163,65 +161,6 @@ SILModule::~SILModule() {
163161
flushDeletedInsts();
164162
}
165163

166-
void SILModule::checkForLeaks() const {
167-
168-
/// Leak checking is not thread safe, because the instruction counters are
169-
/// global non-atomic variables. Leak checking can only be done in case there
170-
/// is a single SILModule in a single thread.
171-
if (!getOptions().checkSILModuleLeaks)
172-
return;
173-
174-
int instsInModule = scheduledForDeletion.size();
175-
176-
for (const SILFunction &F : *this) {
177-
const SILFunction *sn = &F;
178-
do {
179-
for (const SILBasicBlock &block : *sn) {
180-
instsInModule += std::distance(block.begin(), block.end());
181-
}
182-
} while ((sn = sn->snapshots) != nullptr);
183-
}
184-
for (const SILFunction &F : zombieFunctions) {
185-
const SILFunction *sn = &F;
186-
do {
187-
for (const SILBasicBlock &block : F) {
188-
instsInModule += std::distance(block.begin(), block.end());
189-
}
190-
} while ((sn = sn->snapshots) != nullptr);
191-
}
192-
for (const SILGlobalVariable &global : getSILGlobals()) {
193-
instsInModule += std::distance(global.StaticInitializerBlock.begin(),
194-
global.StaticInitializerBlock.end());
195-
}
196-
197-
int numAllocated = SILInstruction::getNumCreatedInstructions() -
198-
SILInstruction::getNumDeletedInstructions();
199-
200-
if (numAllocated != instsInModule) {
201-
llvm::errs() << "Leaking instructions!\n";
202-
llvm::errs() << "Allocated instructions: " << numAllocated << '\n';
203-
llvm::errs() << "Instructions in module: " << instsInModule << '\n';
204-
llvm_unreachable("leaking instructions");
205-
}
206-
207-
assert(PlaceholderValue::getNumPlaceholderValuesAlive() == 0 &&
208-
"leaking placeholders");
209-
}
210-
211-
void SILModule::checkForLeaksAfterDestruction() {
212-
// Disabled in release (non-assert) builds because this check fails in rare
213-
// cases in lldb, causing crashes. rdar://70826934
214-
#ifndef NDEBUG
215-
int numAllocated = SILInstruction::getNumCreatedInstructions() -
216-
SILInstruction::getNumDeletedInstructions();
217-
218-
if (numAllocated != 0) {
219-
llvm::errs() << "Leaking " << numAllocated << " instructions!\n";
220-
llvm_unreachable("leaking instructions");
221-
}
222-
#endif
223-
}
224-
225164
std::unique_ptr<SILModule> SILModule::createEmptyModule(
226165
llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
227166
Lowering::TypeConverter &TC, const SILOptions &Options,

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ ParseSILModuleRequest::evaluate(Evaluator &evaluator,
101101
auto bufferID = SF->getBufferID();
102102
assert(bufferID);
103103

104-
// For leak detection.
105-
SILInstruction::resetInstructionCounts();
106-
107104
auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
108105
desc.opts);
109106
SILParserState parserState(*silMod.get());

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7255,8 +7255,6 @@ void SILModule::verify(CalleeCache *calleeCache,
72557255
if (!verificationEnabled(*this))
72567256
return;
72577257

7258-
checkForLeaks();
7259-
72607258
// Uniquing set to catch symbol name collisions.
72617259
llvm::DenseSet<StringRef> symbolNames;
72627260

lib/SILGen/SILGen.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,9 +2115,6 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
21152115
return evaluateOrFatal(evaluator, ParseSILModuleRequest{desc});
21162116
}
21172117

2118-
// For leak detection.
2119-
SILInstruction::resetInstructionCounts();
2120-
21212118
auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
21222119
desc.opts, desc.irgenOptions);
21232120

0 commit comments

Comments
 (0)