[llvm] [SandboxVec] Implement Pass class (PR #107617)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 08:37:27 PDT 2024


================
@@ -0,0 +1,82 @@
+//===- Pass.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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SANDBOXIR_PASS_H
+#define LLVM_SANDBOXIR_PASS_H
+
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm::sandboxir {
+
+class Function;
+
+/// The base class of a Sandbox IR Pass.
+class Pass {
+public:
+  enum class ClassID : unsigned {
+    FunctionPass,
+  };
+  static const char *getSubclassIDStr(ClassID ID) {
+    switch (ID) {
+    case ClassID::FunctionPass:
+      return "FunctionPass";
+    }
+    llvm_unreachable("Unimplemented ID");
+  }
+
+protected:
+  /// The pass name.
+  const std::string Name;
+  /// The command-line flag used to specify that this pass should run.
+  const std::string Flag;
+  /// Used for isa/cast/dyn_cast.
+  ClassID SubclassID;
+
+public:
+  Pass(StringRef Name, StringRef Flag, ClassID SubclassID)
+      : Name(Name), Flag(Flag), SubclassID(SubclassID) {}
+  virtual ~Pass() {}
+  /// \Returns the name of the pass.
+  StringRef getName() const { return Name; }
+  /// \Returns the command-line flag used to enable the pass.
+  StringRef getFlag() const { return Flag; }
+  ClassID getSubclassID() const { return SubclassID; }
+#ifndef NDEBUG
+  friend raw_ostream &operator<<(raw_ostream &OS, const Pass &Pass) {
+    Pass.dumpOS(OS);
+    return OS;
+  }
+  void dumpOS(raw_ostream &OS) const { OS << Name << " " << Flag; }
+  LLVM_DUMP_METHOD void dump() const;
+#endif
+};
+
+/// A pass that runs on a sandbox::Function.
+class FunctionPass : public Pass {
+protected:
+  FunctionPass(StringRef Name, StringRef Flag, ClassID PassID)
+      : Pass(Name, Flag, PassID) {}
+
+public:
+  FunctionPass(StringRef Name, StringRef Flag)
+      : Pass(Name, Flag, ClassID::FunctionPass) {}
+  /// For isa/dyn_cast etc.
+  static bool classof(const Pass *From) {
----------------
aeubanks wrote:

the thing about the legacy and new pass managers is that they run a bunch of function passes on one function at a time, then move to the next. they don't just run one function pass on every function then move on to the next. this is important for data locality for compile times, although perhaps that's not as important here. it's also fits in with the call graph walk where you want to see simplified callees 

if you want the above, you need some sort of scheduling, whether dynamically (legacy pass manager) or statically in the code via adaptors and managers (new pass manager)

if you don't want the above, then you shouldn't need `isa/dyn_cast`, just have subclasses implement a virtual `run()`

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


More information about the llvm-commits mailing list