Skip to content

Commit 50881d0

Browse files
authored
[SandboxIR] Add BasicBlock and adds functionality to Function and Context (#97637)
We can now create SandboxIR from LLVM IR using the Context::create* functions.
1 parent 28695dd commit 50881d0

File tree

4 files changed

+508
-4
lines changed

4 files changed

+508
-4
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 157 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,53 @@ 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+
Argument *getOrCreateArgument(llvm::Argument *LLVMArg) {
385+
auto Pair = LLVMValueToValueMap.insert({LLVMArg, nullptr});
386+
auto It = Pair.first;
387+
if (Pair.second) {
388+
It->second = std::make_unique<Argument>(LLVMArg, *this);
389+
return cast<Argument>(It->second.get());
390+
}
391+
return cast<Argument>(It->second.get());
392+
}
393+
394+
Value *getOrCreateValue(llvm::Value *LLVMV) {
395+
return getOrCreateValueInternal(LLVMV, 0);
396+
}
397+
398+
BasicBlock *createBasicBlock(llvm::BasicBlock *BB);
399+
400+
friend class BasicBlock; // For getOrCreateValue().
401+
281402
public:
282403
Context(LLVMContext &LLVMCtx) : LLVMCtx(LLVMCtx) {}
404+
283405
sandboxir::Value *getValue(llvm::Value *V) const;
406+
const sandboxir::Value *getValue(const llvm::Value *V) const {
407+
return getValue(const_cast<llvm::Value *>(V));
408+
}
409+
410+
Function *createFunction(llvm::Function *F);
411+
412+
/// \Returns the number of values registered with Context.
413+
size_t getNumValues() const { return LLVMValueToValueMap.size(); }
284414
};
285415

286416
class Function : public sandboxir::Value {
417+
/// Helper for mapped_iterator.
418+
struct LLVMBBToBB {
419+
Context &Ctx;
420+
LLVMBBToBB(Context &Ctx) : Ctx(Ctx) {}
421+
BasicBlock &operator()(llvm::BasicBlock &LLVMBB) const {
422+
return *cast<BasicBlock>(Ctx.getValue(&LLVMBB));
423+
}
424+
};
425+
287426
public:
288427
Function(llvm::Function *F, sandboxir::Context &Ctx)
289428
: sandboxir::Value(ClassID::Function, F, Ctx) {}
@@ -292,6 +431,24 @@ class Function : public sandboxir::Value {
292431
return From->getSubclassID() == ClassID::Function;
293432
}
294433

434+
Argument *getArg(unsigned Idx) const {
435+
llvm::Argument *Arg = cast<llvm::Function>(Val)->getArg(Idx);
436+
return cast<Argument>(Ctx.getValue(Arg));
437+
}
438+
439+
size_t arg_size() const { return cast<llvm::Function>(Val)->arg_size(); }
440+
bool arg_empty() const { return cast<llvm::Function>(Val)->arg_empty(); }
441+
442+
using iterator = mapped_iterator<llvm::Function::iterator, LLVMBBToBB>;
443+
iterator begin() const {
444+
LLVMBBToBB BBGetter(Ctx);
445+
return iterator(cast<llvm::Function>(Val)->begin(), BBGetter);
446+
}
447+
iterator end() const {
448+
LLVMBBToBB BBGetter(Ctx);
449+
return iterator(cast<llvm::Function>(Val)->end(), BBGetter);
450+
}
451+
295452
#ifndef NDEBUG
296453
void verify() const final {
297454
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)