[llvm] [SandboxIR] Boilerplate code (PR #95814)

Jorge Gorbe Moya via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 17 15:02:03 PDT 2024


================
@@ -0,0 +1,129 @@
+//===- 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:
+//
+//          +- SBArgument   +- SBConstant     +- SBOpaqueInstruction
+//          |               |                 |
+// SBValue -+- SBUser ------+- SBInstruction -+- SBInsertElementInstruction
+//          |                                 |
+//          +- SBBasicBlock                   +- SBExtractElementInstruction
+//          |                                 |
+//          +- SBFunction                     +- SBShuffleVectorInstruction
+//                                            |
+//                                            +- SBStoreInstruction
+//                                            |
+//                                            +- SBLoadInstruction
+//                                            |
+//                                            +- SBCmpInstruction
+//                                            |
+//                                            +- SBCastInstruction
+//                                            |
+//                                            +- SBPHINode
+//                                            |
+//                                            +- SBSelectInstruction
+//                                            |
+//                                            +- SBBinaryOperator
+//                                            |
+//                                            +- SBUnaryOperator
+//
+// SBUse
+//
+#ifndef LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
+#define LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
+
+#include "llvm/IR/Value.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+
+class SBContext;
+
+/// A SBValue has users. This is the base class.
+class SBValue {
+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 SBValue.
+  /// NOTE: Some SBInstructions, like Packs, may include more than one value.
+  Value *Val = nullptr;
+  friend class ValueAttorney; // For Val
----------------
slackito wrote:

Nit: should this friend declaration be added in the same patch as the `ValueAttorney` class? It's not added here. 

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


More information about the llvm-commits mailing list