[llvm] b3e720b - [PassInstrumentation] Don't insert extra entries in getPassNameForClassName (#150029)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 22 09:51:53 PDT 2025
Author: Danila Malyutin
Date: 2025-07-22T20:51:49+04:00
New Revision: b3e720b4deb481df11cb6be09e5a2ad7a4d4a7eb
URL: https://github.com/llvm/llvm-project/commit/b3e720b4deb481df11cb6be09e5a2ad7a4d4a7eb
DIFF: https://github.com/llvm/llvm-project/commit/b3e720b4deb481df11cb6be09e5a2ad7a4d4a7eb.diff
LOG: [PassInstrumentation] Don't insert extra entries in getPassNameForClassName (#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.
Added:
Modified:
llvm/include/llvm/IR/PassInstrumentation.h
llvm/lib/IR/PassInstrumentation.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/PassInstrumentation.h b/llvm/include/llvm/IR/PassInstrumentation.h
index 031571599f9ad..33eda5a4222f1 100644
--- a/llvm/include/llvm/IR/PassInstrumentation.h
+++ b/llvm/include/llvm/IR/PassInstrumentation.h
@@ -164,7 +164,7 @@ 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.
+ /// Get the pass name for a given pass class name. Empty if no match found.
LLVM_ABI StringRef getPassNameForClassName(StringRef ClassName);
private:
diff --git a/llvm/lib/IR/PassInstrumentation.cpp b/llvm/lib/IR/PassInstrumentation.cpp
index 94ad124a6c770..70bbe8f6234b1 100644
--- a/llvm/lib/IR/PassInstrumentation.cpp
+++ b/llvm/lib/IR/PassInstrumentation.cpp
@@ -23,6 +23,7 @@ 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());
}
@@ -33,7 +34,10 @@ PassInstrumentationCallbacks::getPassNameForClassName(StringRef ClassName) {
Fn();
ClassToPassNameCallbacks.clear();
}
- return ClassToPassName[ClassName];
+ auto PassNameIter = ClassToPassName.find(ClassName);
+ if (PassNameIter != ClassToPassName.end())
+ return PassNameIter->second;
+ return {};
}
AnalysisKey PassInstrumentationAnalysis::Key;
More information about the llvm-commits
mailing list