[llvm] [InlineCost] Cache collectEphemeralValues() to save compile time (PR #130210)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 20 12:49:12 PDT 2025


================
@@ -0,0 +1,60 @@
+//===- llvm/Analysis/EphemeralValuesCache.h ---------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass caches ephemeral values, i.e., values that are only used by
+// @llvm.assume intrinsics, for cheap access after the initial collection.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_EPHEMERALVALUESCACHE_H
+#define LLVM_ANALYSIS_EPHEMERALVALUESCACHE_H
+
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+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() {
----------------
aeubanks wrote:

`CodeMetrics::collectEphemeralValues()` is the API if someone wants to calculate ephemeral values, I don't think we need another layer on top of that. I think `EphemeralValuesAnalysis::Result` could just be `SmallPtrSet<const Value *, 32>`

the pass should only call `FAM.getAnalysis` if it's going to use it, so the laziness should be done at the caller level

https://github.com/llvm/llvm-project/pull/130210


More information about the llvm-commits mailing list