Skip to content

Commit f41e973

Browse files
committed
[SandboxIR] Add BasicBlock and adds functionality to Function and Context
We can now create SandboxIR from LLVM IR using the Context::create* functions.
1 parent 4c63672 commit f41e973

File tree

4 files changed

+509
-4
lines changed

4 files changed

+509
-4
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@
6262
#include "llvm/IR/User.h"
6363
#include "llvm/IR/Value.h"
6464
#include "llvm/Support/raw_ostream.h"
65+
#include <iterator>
6566

6667
namespace llvm {
6768

6869
namespace sandboxir {
6970

71+
class Function;
7072
class Context;
73+
class Instruction;
7174

7275
/// A SandboxIR Value has users. This is the base class.
7376
class Value {
@@ -106,6 +109,8 @@ class Value {
106109
/// NOTE: Some SBInstructions, like Packs, may include more than one value.
107110
llvm::Value *Val = nullptr;
108111

112+
friend class Context; // For getting `Val`.
113+
109114
/// All values point to the context.
110115
Context &Ctx;
111116
// This is used by eraseFromParent().
@@ -205,6 +210,48 @@ class Constant : public sandboxir::User {
205210
#endif
206211
};
207212

213+
/// The BasicBlock::iterator.
214+
class BBIterator {
215+
public:
216+
using difference_type = std::ptrdiff_t;
217+
using value_type = Instruction;
218+
using pointer = value_type *;
219+
using reference = value_type &;
220+
using iterator_category = std::bidirectional_iterator_tag;
221+
222+
private:
223+
llvm::BasicBlock *BB;
224+
llvm::BasicBlock::iterator It;
225+
Context *Ctx;
226+
pointer getInstr(llvm::BasicBlock::iterator It) const;
227+
228+
public:
229+
BBIterator() : BB(nullptr), Ctx(nullptr) {}
230+
BBIterator(llvm::BasicBlock *BB, llvm::BasicBlock::iterator It, Context *Ctx)
231+
: BB(BB), It(It), Ctx(Ctx) {}
232+
reference operator*() const { return *getInstr(It); }
233+
BBIterator &operator++();
234+
BBIterator operator++(int) {
235+
auto Copy = *this;
236+
++*this;
237+
return Copy;
238+
}
239+
BBIterator &operator--();
240+
BBIterator operator--(int) {
241+
auto Copy = *this;
242+
--*this;
243+
return Copy;
244+
}
245+
bool operator==(const BBIterator &Other) const {
246+
assert(Ctx == Other.Ctx && "BBIterators in different context!");
247+
return It == Other.It;
248+
}
249+
bool operator!=(const BBIterator &Other) const { return !(*this == Other); }
250+
/// \Returns the SBInstruction that corresponds to this iterator, or null if
251+
/// the instruction is not found in the IR-to-SandboxIR tables.
252+
pointer get() const { return getInstr(It); }
253+
};
254+
208255
/// A sandboxir::User with operands and opcode.
209256
class Instruction : public sandboxir::User {
210257
public:
@@ -231,6 +278,8 @@ class Instruction : public sandboxir::User {
231278
return OS;
232279
}
233280
#endif
281+
/// This is used by BasicBlock::iterator.
282+
virtual unsigned getNumOfIRInstrs() const = 0;
234283
/// For isa/dyn_cast.
235284
static bool classof(const sandboxir::Value *From);
236285

@@ -256,6 +305,7 @@ class OpaqueInst : public sandboxir::Instruction {
256305
static bool classof(const sandboxir::Value *From) {
257306
return From->getSubclassID() == ClassID::Opaque;
258307
}
308+
unsigned getNumOfIRInstrs() const final { return 1u; }
259309
#ifndef NDEBUG
260310
void verify() const final {
261311
// Nothing to do
@@ -270,6 +320,54 @@ class OpaqueInst : public sandboxir::Instruction {
270320
#endif
271321
};
272322

323+
class BasicBlock : public Value {
324+
/// Builds a graph that contains all values in \p BB in their original form
325+
/// i.e., no vectorization is taking place here.
326+
void buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB);
327+
friend class Context; // For `buildBasicBlockFromIR`
328+
329+
public:
330+
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
331+
: Value(ClassID::Block, BB, SBCtx) {
332+
buildBasicBlockFromLLVMIR(BB);
333+
}
334+
~BasicBlock() = default;
335+
/// For isa/dyn_cast.
336+
static bool classof(const Value *From) {
337+
return From->getSubclassID() == Value::ClassID::Block;
338+
}
339+
Function *getParent() const;
340+
using iterator = BBIterator;
341+
iterator begin() const;
342+
iterator end() const {
343+
auto *BB = cast<llvm::BasicBlock>(Val);
344+
return iterator(BB, BB->end(), &Ctx);
345+
}
346+
std::reverse_iterator<iterator> rbegin() const {
347+
return std::make_reverse_iterator(end());
348+
}
349+
std::reverse_iterator<iterator> rend() const {
350+
return std::make_reverse_iterator(begin());
351+
}
352+
Context &getContext() const { return Ctx; }
353+
Instruction *getTerminator() const;
354+
bool empty() const { return begin() == end(); }
355+
Instruction &front() const;
356+
Instruction &back() const;
357+
358+
#ifndef NDEBUG
359+
void verify() const final {
360+
assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!");
361+
}
362+
friend raw_ostream &operator<<(raw_ostream &OS, const BasicBlock &SBBB) {
363+
SBBB.dump(OS);
364+
return OS;
365+
}
366+
void dump(raw_ostream &OS) const final;
367+
LLVM_DUMP_METHOD void dump() const final;
368+
#endif
369+
};
370+
273371
class Context {
274372
protected:
275373
LLVMContext &LLVMCtx;
@@ -278,12 +376,54 @@ class Context {
278376
DenseMap<llvm::Value *, std::unique_ptr<sandboxir::Value>>
279377
LLVMValueToValueMap;
280378

379+
/// Take ownership of VPtr and store it in `LLVMValueToValueMap`.
380+
Value *registerValue(std::unique_ptr<Value> &&VPtr);
381+
382+
Value *getOrCreateValueInternal(llvm::Value *V, llvm::User *U = nullptr);
383+
384+
template <typename SBT, typename LLVMT>
385+
SBT *getOrCreateGeneric(llvm::Value *LLVMV) {
386+
auto Pair = LLVMValueToValueMap.insert({LLVMV, nullptr});
387+
auto It = Pair.first;
388+
if (Pair.second) {
389+
It->second = std::make_unique<SBT>(cast<LLVMT>(LLVMV), *this);
390+
return cast<SBT>(It->second.get());
391+
}
392+
return cast<SBT>(It->second.get());
393+
}
394+
395+
Value *getOrCreateValue(llvm::Value *LLVMV) {
396+
return getOrCreateValueInternal(LLVMV, 0);
397+
}
398+
399+
BasicBlock *createBasicBlock(llvm::BasicBlock *BB);
400+
401+
friend class BasicBlock; // For getOrCreateValue().
402+
281403
public:
282404
Context(LLVMContext &LLVMCtx) : LLVMCtx(LLVMCtx) {}
405+
283406
sandboxir::Value *getValue(llvm::Value *V) const;
407+
const sandboxir::Value *getValue(const llvm::Value *V) const {
408+
return getValue(const_cast<llvm::Value *>(V));
409+
}
410+
411+
Function *createFunction(llvm::Function *F);
412+
413+
/// \Returns the number of values registered with Context.
414+
size_t getNumValues() const { return LLVMValueToValueMap.size(); }
284415
};
285416

286417
class Function : public sandboxir::Value {
418+
/// Helper for mapped_iterator.
419+
struct LLVMBBToBB {
420+
Context &Ctx;
421+
LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
422+
BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
423+
return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
424+
}
425+
};
426+
287427
public:
288428
Function(llvm::Function *F, sandboxir::Context &Ctx)
289429
: sandboxir::Value(ClassID::Function, F, Ctx) {}
@@ -292,6 +432,24 @@ class Function : public sandboxir::Value {
292432
return From->getSubclassID() == ClassID::Function;
293433
}
294434

435+
Argument *getArg(unsigned Idx) const {
436+
llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
437+
return cast<Argument>(Ctx.getValue(Arg));
438+
}
439+
440+
size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
441+
bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
442+
443+
using iterator = mapped_iterator<llvm::Function::iterator, LLVMBBToBB>;
444+
iterator begin() const {
445+
LLVMBBToBB BBGetter(Ctx);
446+
return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
447+
}
448+
iterator end() const {
449+
LLVMBBToBB BBGetter(Ctx);
450+
return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
451+
}
452+
295453
#ifndef NDEBUG
296454
void verify() const final {
297455
assert(isa<llvm::Function>(Val) && "Expected Function!");

llvm/include/llvm/SandboxIR/SandboxIRValues.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ DEF_VALUE(Argument, Argument)
1717
#define DEF_USER(ID, CLASS)
1818
#endif
1919
DEF_USER(User, User)
20+
DEF_VALUE(Block, BasicBlock)
2021
DEF_USER(Constant, Constant)
2122

2223
#ifndef DEF_INSTR

0 commit comments

Comments
 (0)