Skip to content

Commit eca01b0

Browse files
committed
[NewPM][Sancov] Make Sancov a Module Pass instead of 2 Passes
This patch merges the sancov module and funciton passes into one module pass. The reason for this is because we ran into an out of memory error when attempting to run asan fuzzer on some protobufs (pc.cc files). I traced the OOM error to the destructor of SanitizerCoverage where we only call appendTo[Compiler]Used which calls appendToUsedList. I'm not sure where precisely in appendToUsedList causes the OOM, but I am able to confirm that it's calling this function *repeatedly* that causes the OOM. (I hacked sancov a bit such that I can still create and destroy a new sancov on every function run, but only call appendToUsedList after all functions in the module have finished. This passes, but when I make it such that appendToUsedList is called on every sancov destruction, we hit OOM.) I don't think the OOM is from just adding to the SmallSet and SmallVector inside appendToUsedList since in either case for a given module, they'll have the same max size. I suspect that when the existing llvm.compiler.used global is erased, the memory behind it isn't freed. I could be wrong on this though. This patch works around the OOM issue by just calling appendToUsedList at the end of every module run instead of function run. The same amount of constants still get added to llvm.compiler.used, abd we make the pass usage and logic simpler by not having any inter-pass dependencies. Differential Revision: https://reviews.llvm.org/D66988 llvm-svn: 370971
1 parent 40e3760 commit eca01b0

37 files changed

+176
-300
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
224224
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
225225
auto Opts = getSancovOptsFromCGOpts(CGOpts);
226226
PM.add(createModuleSanitizerCoverageLegacyPassPass(Opts));
227-
PM.add(createSanitizerCoverageLegacyPassPass(Opts));
228227
}
229228

230229
// Check if ASan should use GC-friendly instrumentation for globals.
@@ -1159,11 +1158,6 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
11591158
[SancovOpts](ModulePassManager &MPM) {
11601159
MPM.addPass(ModuleSanitizerCoveragePass(SancovOpts));
11611160
});
1162-
PB.registerOptimizerLastEPCallback(
1163-
[SancovOpts](FunctionPassManager &FPM,
1164-
PassBuilder::OptimizationLevel Level) {
1165-
FPM.addPass(SanitizerCoveragePass(SancovOpts));
1166-
});
11671161
}
11681162

11691163
// Register callbacks to schedule sanitizer passes at the appropriate part of
@@ -1249,8 +1243,6 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
12491243
CodeGenOpts.SanitizeCoverageTraceCmp) {
12501244
auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
12511245
MPM.addPass(ModuleSanitizerCoveragePass(SancovOpts));
1252-
MPM.addPass(createModuleToFunctionPassAdaptor(
1253-
SanitizerCoveragePass(SancovOpts)));
12541246
}
12551247

12561248
addSanitizersAtO0(MPM, TargetTriple, LangOpts, CodeGenOpts);

llvm/include/llvm/InitializePasses.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ void initializeSROALegacyPassPass(PassRegistry&);
364364
void initializeSafeStackLegacyPassPass(PassRegistry&);
365365
void initializeSafepointIRVerifierPass(PassRegistry&);
366366
void initializeSampleProfileLoaderLegacyPassPass(PassRegistry&);
367-
void initializeSanitizerCoverageFunctionCheckLegacyPassPass(PassRegistry &);
368-
void initializeSanitizerCoverageLegacyPassPass(PassRegistry &);
369367
void initializeModuleSanitizerCoverageLegacyPassPass(PassRegistry &);
370368
void initializeScalarEvolutionWrapperPassPass(PassRegistry&);
371369
void initializeScalarizeMaskedMemIntrinPass(PassRegistry&);

llvm/include/llvm/Transforms/Instrumentation/SanitizerCoverage.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,16 @@
1616
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERCOVERAGE_H
1717
#define LLVM_TRANSFORMS_INSTRUMENTATION_SANITIZERCOVERAGE_H
1818

19-
#include "llvm/IR/Function.h"
2019
#include "llvm/IR/Module.h"
2120
#include "llvm/IR/PassManager.h"
2221
#include "llvm/Transforms/Instrumentation.h"
2322

2423
namespace llvm {
2524

26-
/// This is the SanitizerCoverage pass used in the new pass manager. The
27-
/// pass instruments functions for coverage.
28-
class SanitizerCoveragePass : public PassInfoMixin<SanitizerCoveragePass> {
29-
public:
30-
explicit SanitizerCoveragePass(
31-
SanitizerCoverageOptions Options = SanitizerCoverageOptions())
32-
: Options(Options) {}
33-
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
34-
35-
private:
36-
SanitizerCoverageOptions Options;
37-
};
38-
39-
/// This is the ModuleSanitizerCoverage pass used in the new pass manager. This
40-
/// adds initialization calls to the module for trace PC guards and 8bit
41-
/// counters if they are requested.
25+
/// This is the ModuleSanitizerCoverage pass used in the new pass manager. The
26+
/// pass instruments functions for coverage, adds initialization calls to the
27+
/// module for trace PC guards and 8bit counters if they are requested, and
28+
/// appends globals to llvm.compiler.used.
4229
class ModuleSanitizerCoveragePass
4330
: public PassInfoMixin<ModuleSanitizerCoveragePass> {
4431
public:
@@ -52,8 +39,6 @@ class ModuleSanitizerCoveragePass
5239
};
5340

5441
// Insert SanitizerCoverage instrumentation.
55-
FunctionPass *createSanitizerCoverageLegacyPassPass(
56-
const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
5742
ModulePass *createModuleSanitizerCoverageLegacyPassPass(
5843
const SanitizerCoverageOptions &Options = SanitizerCoverageOptions());
5944

llvm/lib/Passes/PassRegistry.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ FUNCTION_PASS("kasan", AddressSanitizerPass(true, false, false))
246246
FUNCTION_PASS("msan", MemorySanitizerPass({}))
247247
FUNCTION_PASS("kmsan", MemorySanitizerPass({0, false, /*Kernel=*/true}))
248248
FUNCTION_PASS("tsan", ThreadSanitizerPass())
249-
FUNCTION_PASS("sancov-func", SanitizerCoveragePass())
250249
#undef FUNCTION_PASS
251250

252251
#ifndef FUNCTION_PASS_WITH_PARAMS

llvm/lib/Transforms/Instrumentation/Instrumentation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void llvm::initializeInstrumentation(PassRegistry &Registry) {
116116
initializeMemorySanitizerLegacyPassPass(Registry);
117117
initializeHWAddressSanitizerLegacyPassPass(Registry);
118118
initializeThreadSanitizerLegacyPassPass(Registry);
119-
initializeSanitizerCoverageLegacyPassPass(Registry);
119+
initializeModuleSanitizerCoverageLegacyPassPass(Registry);
120120
initializeDataFlowSanitizerPass(Registry);
121121
}
122122

0 commit comments

Comments
 (0)