Skip to content

Commit a928d55

Browse files
committed
[SandboxIR] Boilerplate code
This is the first patch in a series of patches for the Sandbox Vectorizer project. For a detailed description of the project please refer to the RFC: https://discourse.llvm.org/t/rfc-sandbox-vectorizer-an-experimental-modular-vectorizer/79059 This patch adds some basic boilerplate code for the SandboxIR which includes parts of the `SBValue` class.
1 parent d1a4f0c commit a928d55

File tree

7 files changed

+264
-0
lines changed

7 files changed

+264
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
//===- SandboxIR.h ----------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Sandbox IR is a lightweight overlay transactional IR on top of LLVM IR.
10+
// Features:
11+
// - You can save/rollback the state of the IR at any time.
12+
// - Any changes made to Sandbox IR will automatically update the underlying
13+
// LLVM IR so both IRs are always in sync.
14+
// - Feels like LLVM IR, similar API.
15+
//
16+
// SandboxIR forms a class hierarcy that resembles that of LLVM IR:
17+
//
18+
// +- SBArgument +- SBConstant +- SBOpaqueInstruction
19+
// | | |
20+
// SBValue -+- SBUser ------+- SBInstruction -+- SBInsertElementInstruction
21+
// | |
22+
// +- SBBasicBlock +- SBExtractElementInstruction
23+
// | |
24+
// +- SBFunction +- SBShuffleVectorInstruction
25+
// |
26+
// +- SBStoreInstruction
27+
// |
28+
// +- SBLoadInstruction
29+
// |
30+
// +- SBCmpInstruction
31+
// |
32+
// +- SBCastInstruction
33+
// |
34+
// +- SBPHINode
35+
// |
36+
// +- SBSelectInstruction
37+
// |
38+
// +- SBBinaryOperator
39+
// |
40+
// +- SBUnaryOperator
41+
//
42+
// SBUse
43+
//
44+
#ifndef LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
45+
#define LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
46+
47+
#include "llvm/IR/Value.h"
48+
#include "llvm/Support/raw_ostream.h"
49+
50+
namespace llvm {
51+
52+
class SBContext;
53+
54+
/// A SBValue has users. This is the base class.
55+
class SBValue {
56+
public:
57+
enum class ClassID : unsigned {
58+
#define DEF_VALUE(ID, CLASS) ID,
59+
#define DEF_USER(ID, CLASS) ID,
60+
#define DEF_INSTR(ID, OPC, CLASS) ID,
61+
#include "llvm/Transforms/SandboxIR/SandboxIRValues.def"
62+
};
63+
64+
protected:
65+
static const char *getSubclassIDStr(ClassID ID) {
66+
switch (ID) {
67+
// clang-format off
68+
#define DEF_VALUE(ID, CLASS) case ClassID::ID: return #ID;
69+
#define DEF_USER(ID, CLASS) case ClassID::ID: return #ID;
70+
#define DEF_INSTR(ID, OPC, CLASS) case ClassID::ID: return #ID;
71+
// clang-format on
72+
#include "llvm/Transforms/SandboxIR/SandboxIRValues.def"
73+
}
74+
llvm_unreachable("Unimplemented ID");
75+
}
76+
77+
/// For isa/dyn_cast.
78+
ClassID SubclassID;
79+
#ifndef NDEBUG
80+
/// A unique ID used for forming the name (used for debugging).
81+
unsigned UID;
82+
#endif
83+
/// The LLVM Value that corresponds to this SBValue.
84+
/// NOTE: Some SBInstructions, like Packs, may include more than one value.
85+
Value *Val = nullptr;
86+
friend class ValueAttorney; // For Val
87+
88+
/// All values point to the context.
89+
SBContext &Ctxt;
90+
// This is used by eraseFromParent().
91+
void clearValue() { Val = nullptr; }
92+
template <typename ItTy, typename SBTy> friend class LLVMOpUserItToSBTy;
93+
94+
public:
95+
SBValue(ClassID SubclassID, Value *Val, SBContext &Ctxt);
96+
virtual ~SBValue() = default;
97+
ClassID getSubclassID() const { return SubclassID; }
98+
99+
Type *getType() const { return Val->getType(); }
100+
101+
SBContext &getContext() const;
102+
virtual hash_code hashCommon() const {
103+
return hash_combine(SubclassID, &Ctxt, Val);
104+
}
105+
virtual hash_code hash() const = 0;
106+
friend hash_code hash_value(const SBValue &SBV) { return SBV.hash(); }
107+
#ifndef NDEBUG
108+
/// Should crash if there is something wrong with the instruction.
109+
virtual void verify() const = 0;
110+
/// Returns the name in the form 'SB<number>.' like 'SB1.'
111+
std::string getName() const;
112+
virtual void dumpCommonHeader(raw_ostream &OS) const;
113+
void dumpCommonFooter(raw_ostream &OS) const;
114+
void dumpCommonPrefix(raw_ostream &OS) const;
115+
void dumpCommonSuffix(raw_ostream &OS) const;
116+
void printAsOperandCommon(raw_ostream &OS) const;
117+
friend raw_ostream &operator<<(raw_ostream &OS, const SBValue &SBV) {
118+
SBV.dump(OS);
119+
return OS;
120+
}
121+
virtual void dump(raw_ostream &OS) const = 0;
122+
LLVM_DUMP_METHOD virtual void dump() const = 0;
123+
virtual void dumpVerbose(raw_ostream &OS) const = 0;
124+
LLVM_DUMP_METHOD virtual void dumpVerbose() const = 0;
125+
#endif
126+
};
127+
} // namespace llvm
128+
129+
#endif // LLVM_TRANSFORMS_SANDBOXIR_SANDBOXIR_H
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- SandboxIRValues.def --------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// clang-format off
10+
11+
// ClassID, Class
12+
// DEF_USER(Constant, SBConstant)
13+
14+
// clang-format on
15+
#ifdef DEF_VALUE
16+
#undef DEF_VALUE
17+
#endif
18+
#ifdef DEF_USER
19+
#undef DEF_USER
20+
#endif
21+
#ifdef DEF_INSTR
22+
#undef DEF_INSTR
23+
#endif
24+
#ifdef OPCODES
25+
#undef OPCODES
26+
#endif
27+
#ifdef OP
28+
#undef OP
29+
#endif

llvm/lib/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ add_subdirectory(ObjCARC)
1010
add_subdirectory(Coroutines)
1111
add_subdirectory(CFGuard)
1212
add_subdirectory(HipStdPar)
13+
add_subdirectory(SandboxIR)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_llvm_component_library(LLVMSandboxIR
2+
SandboxIR.cpp
3+
4+
ADDITIONAL_HEADER_DIRS
5+
${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/SandboxIR
6+
7+
LINK_COMPONENTS
8+
Core
9+
Support
10+
)
11+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===- SandboxIR.cpp - A transactional overlay IR on top of LLVM IR -------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/SandboxIR/SandboxIR.h"
10+
#include "llvm/Support/Debug.h"
11+
#include <sstream>
12+
13+
using namespace llvm;
14+
15+
SBValue::SBValue(ClassID SubclassID, Value *Val, SBContext &Ctxt)
16+
: SubclassID(SubclassID), Val(Val), Ctxt(Ctxt) {
17+
#ifndef NDEBUG
18+
UID = 0; // FIXME: Once SBContext is available.
19+
#endif
20+
}
21+
22+
#ifndef NDEBUG
23+
std::string SBValue::getName() const {
24+
std::stringstream SS;
25+
SS << "SB" << UID << ".";
26+
return SS.str();
27+
}
28+
29+
void SBValue::dumpCommonHeader(raw_ostream &OS) const {
30+
OS << getName() << " " << getSubclassIDStr(SubclassID) << " ";
31+
}
32+
33+
void SBValue::dumpCommonFooter(raw_ostream &OS) const {
34+
OS.indent(2) << "Val: ";
35+
if (Val)
36+
OS << *Val;
37+
else
38+
OS << "NULL";
39+
OS << "\n";
40+
}
41+
42+
void SBValue::dumpCommonPrefix(raw_ostream &OS) const {
43+
if (Val)
44+
OS << *Val;
45+
else
46+
OS << "NULL ";
47+
}
48+
49+
void SBValue::dumpCommonSuffix(raw_ostream &OS) const {
50+
OS << " ; " << getName() << " (" << getSubclassIDStr(SubclassID) << ") "
51+
<< this;
52+
}
53+
54+
void SBValue::printAsOperandCommon(raw_ostream &OS) const {
55+
if (Val)
56+
Val->printAsOperand(OS);
57+
else
58+
OS << "NULL ";
59+
}
60+
61+
void SBValue::dump() const {
62+
dump(dbgs());
63+
dbgs() << "\n";
64+
}
65+
#endif // NDEBUG
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
static_library("SandboxIR") {
2+
output_name = "LLVMSandboxIR"
3+
deps = [
4+
"//llvm/lib/IR",
5+
"//llvm/lib/Support",
6+
]
7+
sources = [
8+
"SandboxIR.cpp",
9+
]
10+
}

utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,23 @@ cc_library(
14671467
],
14681468
)
14691469

1470+
cc_library(
1471+
name = "SandboxIR",
1472+
srcs = glob([
1473+
"lib/Transforms/SandboxIR/*.cpp",
1474+
]),
1475+
hdrs = glob([
1476+
"include/llvm/Transforms/SandboxIR/*.h",
1477+
"include/llvm/Transforms/SandboxIR/*.def",
1478+
]),
1479+
copts = llvm_copts,
1480+
deps = [
1481+
":Core",
1482+
":Support",
1483+
":config",
1484+
],
1485+
)
1486+
14701487
cc_library(
14711488
name = "Vectorize",
14721489
srcs = glob([
@@ -1826,6 +1843,7 @@ cc_library(
18261843
":InstCombine",
18271844
":Instrumentation",
18281845
":ObjCARC",
1846+
":SandboxIR",
18291847
":Scalar",
18301848
":Vectorize",
18311849
],
@@ -2658,6 +2676,7 @@ cc_library(
26582676
":Instrumentation",
26592677
":MC",
26602678
":ObjCARC",
2679+
":SandboxIR",
26612680
":Scalar",
26622681
":Support",
26632682
":Target",

0 commit comments

Comments
 (0)