-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[PassInstrumentation] Don't insert extra entries in getPassNameForClassName #150029
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
Conversation
Don't modify ClassToPassName map unless ClassName is found. Instead, return nullopt if there is no matching entry. This will prevent possible dangling references in ClassToPassName map in case of ClassName being freed. See https://github.com/llvm/llvm-project/pull/145059/files#r2219763671 for more context.
|
@llvm/pr-subscribers-lto @llvm/pr-subscribers-llvm-ir Author: Danila Malyutin (danilaml) ChangesDon't modify ClassToPassName map unless ClassName is found. Instead, return nullopt if there is no matching entry. This will prevent possible dangling references in ClassToPassName map in case of ClassName being freed. Full diff: https://github.com/llvm/llvm-project/pull/150029.diff 7 Files Affected:
diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h
index 031571599f9ad..628103c7fe759 100644
--- a/llvm/include/llvm/IR/PassInstrumentation.h
+++ b/llvm/include/llvm/IR/PassInstrumentation.h
@@ -165,7 +165,8 @@ class PassInstrumentationCallbacks {
/// Add a class name to pass name mapping for use by pass instrumentation.
LLVM_ABI void addClassToPassName(StringRef ClassName, StringRef PassName);
/// Get the pass name for a given pass class name.
- LLVM_ABI StringRef getPassNameForClassName(StringRef ClassName);
+ LLVM_ABI std::optional<StringRef>
+ getPassNameForClassName(StringRef ClassName);
private:
friend class PassInstrumentation;
@@ -339,7 +340,8 @@ class PassInstrumentation {
/// Get the pass name for a given pass class name.
StringRef getPassNameForClassName(StringRef ClassName) const {
if (Callbacks)
- return Callbacks->getPassNameForClassName(ClassName);
+ return Callbacks->getPassNameForClassName(ClassName).value_or(
+ StringRef());
return {};
}
};
diff --git a/llvm/include/llvm/Passes/CodeGenPassBuilder.h b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
index b0360f1903c0e..a392f457c2b3b 100644
--- a/llvm/include/llvm/Passes/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/Passes/CodeGenPassBuilder.h
@@ -681,7 +681,8 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::setStartStopPasses(
}
auto PassName = PIC->getPassNameForClassName(ClassName);
- if (Info.StartPass == PassName && ++Count == Info.StartInstanceNum)
+ if (PassName && Info.StartPass == *PassName &&
+ ++Count == Info.StartInstanceNum)
Started = !Info.StartAfter;
return Started;
diff --git a/llvm/lib/IR/PassInstrumentation.cpp b/llvm/lib/IR/PassInstrumentation.cpp
index 94ad124a6c770..ee0ed8807ed12 100644
--- a/llvm/lib/IR/PassInstrumentation.cpp
+++ b/llvm/lib/IR/PassInstrumentation.cpp
@@ -23,17 +23,21 @@ template struct LLVM_EXPORT_TEMPLATE Any::TypeId<const Loop *>;
void PassInstrumentationCallbacks::addClassToPassName(StringRef ClassName,
StringRef PassName) {
+ assert(!PassName.empty() && "PassName can't be empty!");
ClassToPassName.try_emplace(ClassName, PassName.str());
}
-StringRef
+std::optional<StringRef>
PassInstrumentationCallbacks::getPassNameForClassName(StringRef ClassName) {
if (!ClassToPassNameCallbacks.empty()) {
for (auto &Fn : ClassToPassNameCallbacks)
Fn();
ClassToPassNameCallbacks.clear();
}
- return ClassToPassName[ClassName];
+ auto PassNameIter = ClassToPassName.find(ClassName);
+ if (PassNameIter != ClassToPassName.end())
+ return PassNameIter->second;
+ return std::nullopt;
}
AnalysisKey PassInstrumentationAnalysis::Key;
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 5e8cd12fe040b..4d4370394dece 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -346,7 +346,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
raw_string_ostream OS(PipelineStr);
MPM.printPipeline(OS, [&PIC](StringRef ClassName) {
auto PassName = PIC.getPassNameForClassName(ClassName);
- return PassName.empty() ? ClassName : PassName;
+ return PassName ? ClassName : *PassName;
});
outs() << "pipeline-passes: " << PipelineStr << '\n';
}
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index f165e85baf611..0155ea44bb8e9 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -454,12 +454,12 @@ template <typename T>
void ChangeReporter<T>::registerRequiredCallbacks(
PassInstrumentationCallbacks &PIC) {
PIC.registerBeforeNonSkippedPassCallback([&PIC, this](StringRef P, Any IR) {
- saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P));
+ saveIRBeforePass(IR, P, PIC.getPassNameForClassName(P).value_or(""));
});
PIC.registerAfterPassCallback(
[&PIC, this](StringRef P, Any IR, const PreservedAnalyses &) {
- handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P));
+ handleIRAfterPass(IR, P, PIC.getPassNameForClassName(P).value_or(""));
});
PIC.registerAfterPassInvalidatedCallback(
[this](StringRef P, const PreservedAnalyses &) {
@@ -970,7 +970,7 @@ bool PrintIRInstrumentation::shouldPrintBeforePass(StringRef PassID) {
if (shouldPrintBeforeAll())
return true;
- StringRef PassName = PIC->getPassNameForClassName(PassID);
+ StringRef PassName = PIC->getPassNameForClassName(PassID).value_or("");
return is_contained(printBeforePasses(), PassName);
}
@@ -978,7 +978,7 @@ bool PrintIRInstrumentation::shouldPrintAfterPass(StringRef PassID) {
if (shouldPrintAfterAll())
return true;
- StringRef PassName = PIC->getPassNameForClassName(PassID);
+ StringRef PassName = PIC->getPassNameForClassName(PassID).value_or("");
return is_contained(printAfterPasses(), PassName);
}
@@ -1080,10 +1080,10 @@ void OptPassGateInstrumentation::registerCallbacks(
PIC.registerShouldRunOptionalPassCallback(
[this, &PIC](StringRef ClassName, Any IR) {
- StringRef PassName = PIC.getPassNameForClassName(ClassName);
- if (PassName.empty())
+ auto PassName = PIC.getPassNameForClassName(ClassName);
+ if (!PassName)
return this->shouldRun(ClassName, IR);
- return this->shouldRun(PassName, IR);
+ return this->shouldRun(*PassName, IR);
});
}
@@ -2501,7 +2501,8 @@ void PrintCrashIRInstrumentation::registerCallbacks(
raw_string_ostream OS(SavedIR);
OS << formatv("*** Dump of {0}IR Before Last Pass {1}",
llvm::forcePrintModuleIR() ? "Module " : "", PassID);
- if (!isInteresting(IR, PassID, PIC.getPassNameForClassName(PassID))) {
+ if (!isInteresting(IR, PassID,
+ PIC.getPassNameForClassName(PassID).value_or(""))) {
OS << " Filtered Out ***\n";
return;
}
diff --git a/llvm/tools/llc/NewPMDriver.cpp b/llvm/tools/llc/NewPMDriver.cpp
index fa82689ecf9ae..559aff3533a64 100644
--- a/llvm/tools/llc/NewPMDriver.cpp
+++ b/llvm/tools/llc/NewPMDriver.cpp
@@ -164,7 +164,7 @@ int llvm::compileModuleWithNewPM(
raw_string_ostream OS(PipelineStr);
MPM.printPipeline(OS, [&PIC](StringRef ClassName) {
auto PassName = PIC.getPassNameForClassName(ClassName);
- return PassName.empty() ? ClassName : PassName;
+ return PassName ? ClassName : *PassName;
});
outs() << PipelineStr << '\n';
return 0;
diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp
index 7d168a6ceb17c..dda7204718252 100644
--- a/llvm/tools/opt/NewPMDriver.cpp
+++ b/llvm/tools/opt/NewPMDriver.cpp
@@ -534,7 +534,7 @@ bool llvm::runPassPipeline(
raw_string_ostream SOS(Pipeline);
MPM.printPipeline(SOS, [&PIC](StringRef ClassName) {
auto PassName = PIC.getPassNameForClassName(ClassName);
- return PassName.empty() ? ClassName : PassName;
+ return PassName ? ClassName : *PassName;
});
outs() << Pipeline;
outs() << "\n";
|
snehasish
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
nikic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| auto PassNameIter = ClassToPassName.find(ClassName); | ||
| if (PassNameIter != ClassToPassName.end()) | ||
| return PassNameIter->second; | ||
| return {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return {}; | |
| return ""; |
is a bit clearer, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't StringRef() different from StringRef("")? They'd compare the same, but go through different constructors (no need to check and store the string literal). Not that it'd matter, but at least in other places {} is used so I'd go for consistency.
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/187/builds/8385 Here is the relevant piece of the build log for the reference |
…ssName (llvm#150029) Don't modify ClassToPassName map unless ClassName is found. Instead, just return empty StringRef if there is no matching entry. This will prevent possible dangling references in ClassToPassName map in case of ClassName being freed. See https://github.com/llvm/llvm-project/pull/145059/files#r2219763671 for more context.
Don't modify ClassToPassName map unless ClassName is found. Instead, just return empty StringRef if there is no matching entry. This will prevent possible dangling references in ClassToPassName map in case of ClassName being freed.
See https://github.com/llvm/llvm-project/pull/145059/files#r2219763671 for more context.