[llvm] [PassInstrumentation] Replace llvm::Any with a tagged IRUnitRef. (NFC) (PR #215341)
Alexis Engelke via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 12 01:30:44 PDT 2026
================
@@ -0,0 +1,101 @@
+//===- llvm/IR/IRUnitRef.h - Reference to an IR unit ------------*- 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 defines IRUnitRef, a type-erased reference to the IR unit a pass
+/// or analysis is running on, and IRUnitKindTraits, which IR units specialize
+/// to opt into being referred to by one.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_IR_IRUNITREF_H
+#define LLVM_IR_IRUNITREF_H
+
+#include "llvm/Support/Casting.h"
+#include <cstddef>
+#include <type_traits>
+
+namespace llvm {
+
+class Function;
+class Loop;
+class MachineFunction;
+class Module;
+
+/// The IR units a pass can run on.
+enum class IRUnitKind {
+ Module,
+ Function,
+ Loop,
+ MachineFunction,
+ LazyCallGraphSCC,
+};
+
+/// Map an IR unit type to its IRUnitKind.
+template <typename IRUnitT> struct IRUnitKindTraits {};
+
+template <> struct IRUnitKindTraits<Module> {
+ static constexpr IRUnitKind Kind = IRUnitKind::Module;
+};
+template <> struct IRUnitKindTraits<Function> {
+ static constexpr IRUnitKind Kind = IRUnitKind::Function;
+};
+template <> struct IRUnitKindTraits<Loop> {
+ static constexpr IRUnitKind Kind = IRUnitKind::Loop;
+};
+template <> struct IRUnitKindTraits<MachineFunction> {
+ static constexpr IRUnitKind Kind = IRUnitKind::MachineFunction;
+};
+
+// IRUnitKindTraits<LazyCallGraph::SCC> needs to be defined in
+// Analysis/LazyCallGraph.h.
+
+/// A type-erased reference to the IR unit a pass or analysis is running on,
+/// together with the kind of IR unit it refers to.
+class IRUnitRef {
+ const void *Ptr;
+ IRUnitKind Kind;
+
+public:
+ template <typename IRUnitT, IRUnitKind K = IRUnitKindTraits<IRUnitT>::Kind>
+ IRUnitRef(const IRUnitT &IR) : Ptr(&IR), Kind(K) {}
+
+ /// Which kind of IR unit is wrapped. Prefer isa/cast/dyn_cast.
+ IRUnitKind getKind() const { return Kind; }
+
+ /// The wrapped IR unit, type-erased. Prefer isa/cast/dyn_cast.
+ const void *getPointer() const { return Ptr; }
----------------
aengelke wrote:
Make these two private and friend CastInfo? No one else should use them.
https://github.com/llvm/llvm-project/pull/215341
More information about the llvm-commits
mailing list