llvm-project/llvm/lib/IR/PassInstrumentation.cpp
Danila Malyutin b3e720b4de
[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.
2025-07-22 20:51:49 +04:00

55 lines
1.8 KiB
C++

//===- PassInstrumentation.cpp - Pass Instrumentation interface -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file provides the implementation of PassInstrumentation class.
///
//===----------------------------------------------------------------------===//
#include "llvm/IR/PassInstrumentation.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/PassManager.h"
namespace llvm {
template struct LLVM_EXPORT_TEMPLATE Any::TypeId<const Module *>;
template struct LLVM_EXPORT_TEMPLATE Any::TypeId<const Function *>;
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
PassInstrumentationCallbacks::getPassNameForClassName(StringRef ClassName) {
if (!ClassToPassNameCallbacks.empty()) {
for (auto &Fn : ClassToPassNameCallbacks)
Fn();
ClassToPassNameCallbacks.clear();
}
auto PassNameIter = ClassToPassName.find(ClassName);
if (PassNameIter != ClassToPassName.end())
return PassNameIter->second;
return {};
}
AnalysisKey PassInstrumentationAnalysis::Key;
bool isSpecialPass(StringRef PassID, const std::vector<StringRef> &Specials) {
size_t Pos = PassID.find('<');
StringRef Prefix = PassID;
if (Pos != StringRef::npos)
Prefix = PassID.substr(0, Pos);
return any_of(Specials,
[Prefix](StringRef S) { return Prefix.ends_with(S); });
}
} // namespace llvm