[llvm] [SandboxIR] Boilerplate code (PR #95814)
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 27 16:33:14 PDT 2024
================
@@ -0,0 +1,154 @@
+//===- SandboxIR.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
+//
+//===----------------------------------------------------------------------===//
+//
+// Sandbox IR is a lightweight overlay transactional IR on top of LLVM IR.
+// Features:
+// - You can save/rollback the state of the IR at any time.
+// - Any changes made to Sandbox IR will automatically update the underlying
+// LLVM IR so both IRs are always in sync.
+// - Feels like LLVM IR, similar API.
+//
+// SandboxIR forms a class hierarcy that resembles that of LLVM IR
+// but is in the `sandboxir` namespace:
+//
+// namespace sandboxir {
+//
+// +- Argument +- BinaryOperator
+// | |
+// Value -+- BasicBlock +- BranchInst
+// | |
+// +- Function +- Constant +- CastInst
+// | | |
+// +- User ------+- Instruction -+- CallInst
+// |
+// +- CmpInst
+// |
+// +- ExtractElementInst
+// |
+// +- GetElementPtrInst
+// |
+// +- InsertElementInst
+// |
+// +- LoadInst
+// |
+// +- OpaqueInst
+// |
+// +- PHINode
+// |
+// +- RetInst
+// |
+// +- SelectInst
+// |
+// +- ShuffleVectorInst
+// |
+// +- StoreInst
+// |
+// +- UnaryOperator
+//
+// Use
+//
+// } // namespace sandboxir
+//
+
+#ifndef LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
+#define LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
+
+#include "llvm/IR/User.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+
+namespace sandboxir {
+
+class Context;
+
+/// A SandboxIR Value has users. This is the base class.
+class Value {
+public:
+ enum class ClassID : unsigned {
+#define DEF_VALUE(ID, CLASS) ID,
+#define DEF_USER(ID, CLASS) ID,
+#define DEF_INSTR(ID, OPC, CLASS) ID,
+#include "llvm/Transforms/SandboxIR/SandboxIRValues.def"
+ };
+
+protected:
+ static const char *getSubclassIDStr(ClassID ID) {
+ switch (ID) {
+ // clang-format off
+#define DEF_VALUE(ID, CLASS) case ClassID::ID: return #ID;
+#define DEF_USER(ID, CLASS) case ClassID::ID: return #ID;
+#define DEF_INSTR(ID, OPC, CLASS) case ClassID::ID: return #ID;
+ // clang-format on
+#include "llvm/Transforms/SandboxIR/SandboxIRValues.def"
+ }
+ llvm_unreachable("Unimplemented ID");
+ }
+
+ /// For isa/dyn_cast.
+ ClassID SubclassID;
+#ifndef NDEBUG
+ /// A unique ID used for forming the name (used for debugging).
+ unsigned UID;
+#endif
+ /// The LLVM Value that corresponds to this SandboxIR Value.
+ /// NOTE: Some SBInstructions, like Packs, may include more than one value.
+ llvm::Value *Val = nullptr;
+
+ /// All values point to the context.
+ Context &Ctxt;
+ // This is used by eraseFromParent().
+ void clearValue() { Val = nullptr; }
+ template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
+
+public:
+ Value(ClassID SubclassID, llvm::Value *Val, Context &Ctxt);
+ virtual ~Value() = default;
+ ClassID getSubclassID() const { return SubclassID; }
+
+ Type *getType() const { return Val->getType(); }
+
+ Context &getContext() const;
+#ifndef NDEBUG
+ /// Should crash if there is something wrong with the instruction.
+ virtual void verify() const = 0;
+ /// Returns the name in the form 'SB<number>.' like 'SB1.'
+ std::string getName() const;
+ virtual void dumpCommonHeader(raw_ostream &OS) const;
+ void dumpCommonFooter(raw_ostream &OS) const;
+ void dumpCommonPrefix(raw_ostream &OS) const;
+ void dumpCommonSuffix(raw_ostream &OS) const;
+ void printAsOperandCommon(raw_ostream &OS) const;
+#endif
+};
+
+class User : public Value {
+public:
+ User(ClassID ID, llvm::Value *V, Context &Ctxt) : Value(ID, V, Ctxt) {}
+ /// For isa/dyn_cast.
+ static bool classof(const Value *From);
+#ifndef NDEBUG
+ void verify() const override {
+ assert(isa<llvm::User>(Val) && "Expected User!");
+ }
+ void dumpCommonHeader(raw_ostream &OS) const final;
+#endif
+};
+
+class Context {
+protected:
+ LLVMContext &LLVMCtxt;
----------------
aeubanks wrote:
nit: context is almost always abbreviated `ctx` instead of `ctxt`, at least in LLVM
see `rg -i ctxt llvm/include/llvm` vs `rg -i ctx llvm/include/llvm`
https://github.com/llvm/llvm-project/pull/95814
More information about the llvm-commits
mailing list