[llvm] 973ea04 - Revert "[Analysis][EphemeralValuesAnalysis][NFCI] Remove EphemeralValuesCache class (#132454)"
Vasileios Porpodas via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 22 10:14:14 PDT 2025
Author: Vasileios Porpodas
Date: 2025-03-22T10:13:39-07:00
New Revision: 973ea045aa89febc533cea165e78686e976b3805
URL: https://github.com/llvm/llvm-project/commit/973ea045aa89febc533cea165e78686e976b3805
DIFF: https://github.com/llvm/llvm-project/commit/973ea045aa89febc533cea165e78686e976b3805.diff
LOG: Revert "[Analysis][EphemeralValuesAnalysis][NFCI] Remove EphemeralValuesCache class (#132454)"
This reverts commit 4adefcfb856aa304b7b0a9de1eec1814f3820e83.
Added:
Modified:
llvm/include/llvm/Analysis/EphemeralValuesCache.h
llvm/include/llvm/Analysis/InlineCost.h
llvm/lib/Analysis/EphemeralValuesCache.cpp
llvm/lib/Analysis/InlineCost.cpp
llvm/lib/Transforms/IPO/Inliner.cpp
llvm/test/Transforms/Inline/cgscc-incremental-invalidate.ll
llvm/unittests/Analysis/EphemeralValuesCacheTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/EphemeralValuesCache.h b/llvm/include/llvm/Analysis/EphemeralValuesCache.h
index 5e9664e00d961..2b50de9d22259 100644
--- a/llvm/include/llvm/Analysis/EphemeralValuesCache.h
+++ b/llvm/include/llvm/Analysis/EphemeralValuesCache.h
@@ -23,13 +23,35 @@ class Function;
class AssumptionCache;
class Value;
+/// A cache of ephemeral values within a function.
+class EphemeralValuesCache {
+ SmallPtrSet<const Value *, 32> EphValues;
+ Function &F;
+ AssumptionCache &AC;
+ bool Collected = false;
+
+ void collectEphemeralValues();
+
+public:
+ EphemeralValuesCache(Function &F, AssumptionCache &AC) : F(F), AC(AC) {}
+ void clear() {
+ EphValues.clear();
+ Collected = false;
+ }
+ const SmallPtrSetImpl<const Value *> &ephValues() {
+ if (!Collected)
+ collectEphemeralValues();
+ return EphValues;
+ }
+};
+
class EphemeralValuesAnalysis
: public AnalysisInfoMixin<EphemeralValuesAnalysis> {
friend AnalysisInfoMixin<EphemeralValuesAnalysis>;
static AnalysisKey Key;
public:
- using Result = SmallPtrSet<const Value *, 32>;
+ using Result = EphemeralValuesCache;
Result run(Function &F, FunctionAnalysisManager &FAM);
};
diff --git a/llvm/include/llvm/Analysis/InlineCost.h b/llvm/include/llvm/Analysis/InlineCost.h
index 8a51f6cc356cf..90ee75773957a 100644
--- a/llvm/include/llvm/Analysis/InlineCost.h
+++ b/llvm/include/llvm/Analysis/InlineCost.h
@@ -15,7 +15,6 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/STLFunctionalExtras.h"
-#include "llvm/Analysis/EphemeralValuesCache.h"
#include "llvm/Analysis/InlineModelFeatureMaps.h"
#include "llvm/IR/PassManager.h"
#include <cassert>
@@ -32,6 +31,7 @@ class Function;
class ProfileSummaryInfo;
class TargetTransformInfo;
class TargetLibraryInfo;
+class EphemeralValuesCache;
namespace InlineConstants {
// Various thresholds used by inline cost analysis.
@@ -280,24 +280,23 @@ InlineCost getInlineCost(
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
ProfileSummaryInfo *PSI = nullptr, OptimizationRemarkEmitter *ORE = nullptr,
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache = nullptr);
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache =
+ nullptr);
/// Get an InlineCost with the callee explicitly specified.
/// This allows you to calculate the cost of inlining a function via a
/// pointer. This behaves exactly as the version with no explicit callee
/// parameter in all other respects.
//
-InlineCost
-getInlineCost(CallBase &Call, Function *Callee, const InlineParams &Params,
- TargetTransformInfo &CalleeTTI,
- function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
- function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
- function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
- ProfileSummaryInfo *PSI = nullptr,
- OptimizationRemarkEmitter *ORE = nullptr,
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache = nullptr);
+InlineCost getInlineCost(
+ CallBase &Call, Function *Callee, const InlineParams &Params,
+ TargetTransformInfo &CalleeTTI,
+ function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
+ function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
+ function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
+ ProfileSummaryInfo *PSI = nullptr, OptimizationRemarkEmitter *ORE = nullptr,
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache =
+ nullptr);
/// Returns InlineResult::success() if the call site should be always inlined
/// because of user directives, and the inlining is viable. Returns
diff --git a/llvm/lib/Analysis/EphemeralValuesCache.cpp b/llvm/lib/Analysis/EphemeralValuesCache.cpp
index f3cf655beef55..8b68df46950e1 100644
--- a/llvm/lib/Analysis/EphemeralValuesCache.cpp
+++ b/llvm/lib/Analysis/EphemeralValuesCache.cpp
@@ -12,14 +12,17 @@
namespace llvm {
+void EphemeralValuesCache::collectEphemeralValues() {
+ CodeMetrics::collectEphemeralValues(&F, &AC, EphValues);
+ Collected = true;
+}
+
AnalysisKey EphemeralValuesAnalysis::Key;
-EphemeralValuesAnalysis::Result
+EphemeralValuesCache
EphemeralValuesAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
auto &AC = FAM.getResult<AssumptionAnalysis>(F);
- SmallPtrSet<const Value *, 32> EphValues;
- CodeMetrics::collectEphemeralValues(&F, &AC, EphValues);
- return EphValues;
+ return EphemeralValuesCache(F, AC);
}
} // namespace llvm
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index 76c3dd8bb418a..df212eb31950d 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -271,8 +271,7 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
CallBase &CandidateCall;
/// Getter for the cache of ephemeral values.
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache = nullptr;
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache = nullptr;
/// Extension points for handling callsite features.
// Called before a basic block was analyzed.
@@ -516,8 +515,8 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
function_ref<const TargetLibraryInfo &(Function &)> GetTLI = nullptr,
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr,
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache = nullptr)
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache =
+ nullptr)
: TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
GetTLI(GetTLI), PSI(PSI), F(Callee), DL(F.getDataLayout()), ORE(ORE),
CandidateCall(Call), GetEphValuesCache(GetEphValuesCache) {}
@@ -1134,8 +1133,8 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
ProfileSummaryInfo *PSI = nullptr,
OptimizationRemarkEmitter *ORE = nullptr, bool BoostIndirect = true,
bool IgnoreThreshold = false,
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache = nullptr)
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache =
+ nullptr)
: CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, GetTLI, PSI,
ORE, GetEphValuesCache),
ComputeFullInlineCost(OptComputeFullInlineCost ||
@@ -2795,7 +2794,7 @@ InlineResult CallAnalyzer::analyze() {
SmallPtrSet<const Value *, 32> EphValuesStorage;
const SmallPtrSetImpl<const Value *> *EphValues = &EphValuesStorage;
if (GetEphValuesCache)
- EphValues = &GetEphValuesCache(F);
+ EphValues = &GetEphValuesCache(F).ephValues();
else
CodeMetrics::collectEphemeralValues(&F, &GetAssumptionCache(F),
EphValuesStorage);
@@ -2981,8 +2980,7 @@ InlineCost llvm::getInlineCost(
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE,
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache) {
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache) {
return getInlineCost(Call, Call.getCalledFunction(), Params, CalleeTTI,
GetAssumptionCache, GetTLI, GetBFI, PSI, ORE,
GetEphValuesCache);
@@ -3106,8 +3104,7 @@ InlineCost llvm::getInlineCost(
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE,
- function_ref<EphemeralValuesAnalysis::Result &(Function &)>
- GetEphValuesCache) {
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache) {
auto UserDecision =
llvm::getAttributeBasedInliningDecision(Call, Callee, CalleeTTI, GetTLI);
diff --git a/llvm/lib/Transforms/IPO/Inliner.cpp b/llvm/lib/Transforms/IPO/Inliner.cpp
index d2eeb4468ea32..6b819a447bd77 100644
--- a/llvm/lib/Transforms/IPO/Inliner.cpp
+++ b/llvm/lib/Transforms/IPO/Inliner.cpp
@@ -391,8 +391,7 @@ PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
}
// TODO: Shouldn't we be invalidating all analyses on F here?
// The caller was modified, so invalidate Ephemeral Values.
- FAM.invalidate(
- F, PreservedAnalyses::all().abandon(EphemeralValuesAnalysis::ID()));
+ FAM.getResult<EphemeralValuesAnalysis>(F).clear();
DidInline = true;
InlinedCallees.insert(&Callee);
diff --git a/llvm/test/Transforms/Inline/cgscc-incremental-invalidate.ll b/llvm/test/Transforms/Inline/cgscc-incremental-invalidate.ll
index 7443581cf26b1..bc31bca614a40 100644
--- a/llvm/test/Transforms/Inline/cgscc-incremental-invalidate.ll
+++ b/llvm/test/Transforms/Inline/cgscc-incremental-invalidate.ll
@@ -12,15 +12,18 @@
; CHECK: Invalidating analysis: LoopAnalysis on test1_f
; CHECK: Invalidating analysis: BranchProbabilityAnalysis on test1_f
; CHECK: Invalidating analysis: BlockFrequencyAnalysis on test1_f
+; CHECK: Invalidating analysis: EphemeralValuesAnalysis on test1_f
; CHECK: Running analysis: DominatorTreeAnalysis on test1_g
; CHECK: Invalidating analysis: DominatorTreeAnalysis on test1_g
; CHECK: Invalidating analysis: LoopAnalysis on test1_g
; CHECK: Invalidating analysis: BranchProbabilityAnalysis on test1_g
; CHECK: Invalidating analysis: BlockFrequencyAnalysis on test1_g
+; CHECK: Invalidating analysis: EphemeralValuesAnalysis on test1_g
; CHECK: Invalidating analysis: DominatorTreeAnalysis on test1_h
; CHECK: Invalidating analysis: LoopAnalysis on test1_h
; CHECK: Invalidating analysis: BranchProbabilityAnalysis on test1_h
; CHECK: Invalidating analysis: BlockFrequencyAnalysis on test1_h
+; CHECK: Invalidating analysis: EphemeralValuesAnalysis on test1_h
; CHECK-NOT: Invalidating analysis:
; CHECK: Running pass: DominatorTreeVerifierPass on test1_g
; CHECK-NEXT: Running analysis: DominatorTreeAnalysis on test1_g
diff --git a/llvm/unittests/Analysis/EphemeralValuesCacheTest.cpp b/llvm/unittests/Analysis/EphemeralValuesCacheTest.cpp
index e3a6ca1c9a081..c926cf97fcd69 100644
--- a/llvm/unittests/Analysis/EphemeralValuesCacheTest.cpp
+++ b/llvm/unittests/Analysis/EphemeralValuesCacheTest.cpp
@@ -8,11 +8,9 @@
#include "llvm/Analysis/EphemeralValuesCache.h"
#include "llvm/Analysis/AssumptionCache.h"
-#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
-#include "llvm/IR/PassInstrumentation.h"
#include "llvm/Support/SourceMgr.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
@@ -48,20 +46,29 @@ define void @foo(i8 %arg0, i8 %arg1) {
)IR");
Function *F = M->getFunction("foo");
auto *BB = &*F->begin();
+ AssumptionCache AC(*F);
+ EphemeralValuesCache EVC(*F, AC);
auto It = BB->begin();
auto *C0 = &*It++;
auto *Assume0 = &*It++;
[[maybe_unused]] auto *NotEph = &*It++;
auto *C1 = &*It++;
auto *Assume1 = &*It++;
-
- FunctionAnalysisManager FAM;
- FAM.registerPass([] { return EphemeralValuesAnalysis(); });
- FAM.registerPass([] { return PassInstrumentationAnalysis(); });
- FAM.registerPass([] { return AssumptionAnalysis(); });
- FAM.registerPass([] { return TargetIRAnalysis(); });
- EXPECT_THAT(FAM.getResult<EphemeralValuesAnalysis>(*F),
+ [[maybe_unused]] auto *Ret = &*It++;
+ // Check emphemeral values.
+ EXPECT_THAT(EVC.ephValues(),
+ testing::UnorderedElementsAre(C0, Assume0, C1, Assume1));
+ // Clear the cache and try again.
+ EVC.clear();
+ EXPECT_THAT(EVC.ephValues(),
+ testing::UnorderedElementsAre(C0, Assume0, C1, Assume1));
+ // Modify the IR, clear cache and recompute.
+ Assume1->eraseFromParent();
+ C1->eraseFromParent();
+ EXPECT_THAT(EVC.ephValues(),
testing::UnorderedElementsAre(C0, Assume0, C1, Assume1));
+ EVC.clear();
+ EXPECT_THAT(EVC.ephValues(), testing::UnorderedElementsAre(C0, Assume0));
}
} // namespace
More information about the llvm-commits
mailing list