Skip to content

Commit

Permalink
[SandboxIR] Implement BlockAddress
Browse files Browse the repository at this point in the history
This patch implements sandboxir::BlockAddress mirroring llvm:BlockAddress.
  • Loading branch information
vporpo committed Sep 9, 2024
1 parent ae02211 commit 5a29af5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
29 changes: 29 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class ConstantFP;
class ConstantAggregateZero;
class ConstantPointerNull;
class PoisonValue;
class BlockAddress;
class Context;
class Function;
class Instruction;
Expand Down Expand Up @@ -323,6 +324,7 @@ class Value {
friend class ConstantPointerNull; // For `Val`.
friend class UndefValue; // For `Val`.
friend class PoisonValue; // For `Val`.
friend class BlockAddress; // For `Val`.

/// All values point to the context.
Context &Ctx;
Expand Down Expand Up @@ -1112,6 +1114,33 @@ class PoisonValue final : public UndefValue {
#endif
};

class BlockAddress final : public Constant {
BlockAddress(llvm::BlockAddress *C, Context &Ctx)
: Constant(ClassID::BlockAddress, C, Ctx) {}
friend class Context; // For constructor.

public:
/// Return a BlockAddress for the specified function and basic block.
static BlockAddress *get(Function *F, BasicBlock *BB);

/// Return a BlockAddress for the specified basic block. The basic
/// block must be embedded into a function.
static BlockAddress *get(BasicBlock *BB);

/// Lookup an existing \c BlockAddress constant for the given BasicBlock.
///
/// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
static BlockAddress *lookup(const BasicBlock *BB);

Function *getFunction() const;
BasicBlock *getBasicBlock() const;

/// For isa/dyn_cast.
static bool classof(const sandboxir::Value *From) {
return From->getSubclassID() == ClassID::BlockAddress;
}
};

/// Iterator for `Instruction`s in a `BasicBlock.
/// \Returns an sandboxir::Instruction & when derereferenced.
class BBIterator {
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/SandboxIRValues.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ DEF_CONST(ConstantAggregateZero, ConstantAggregateZero)
DEF_CONST(ConstantPointerNull, ConstantPointerNull)
DEF_CONST(UndefValue, UndefValue)
DEF_CONST(PoisonValue, PoisonValue)
DEF_CONST(BlockAddress, BlockAddress)

#ifndef DEF_INSTR
#define DEF_INSTR(ID, OPCODE, CLASS)
Expand Down
32 changes: 31 additions & 1 deletion llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,32 @@ PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
cast<llvm::PoisonValue>(Val)->getElementValue(Idx)));
}

BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
auto *LLVMC = llvm::BlockAddress::get(cast<llvm::Function>(F->Val),
cast<llvm::BasicBlock>(BB->Val));
return cast<BlockAddress>(F->getContext().getOrCreateConstant(LLVMC));
}

BlockAddress *BlockAddress::get(BasicBlock *BB) {
auto *LLVMC = llvm::BlockAddress::get(cast<llvm::BasicBlock>(BB->Val));
return cast<BlockAddress>(BB->getContext().getOrCreateConstant(LLVMC));
}

BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
auto *LLVMC = llvm::BlockAddress::lookup(cast<llvm::BasicBlock>(BB->Val));
return cast_or_null<BlockAddress>(BB->getContext().getValue(LLVMC));
}

Function *BlockAddress::getFunction() const {
return cast<Function>(
Ctx.getValue(cast<llvm::BlockAddress>(Val)->getFunction()));
}

BasicBlock *BlockAddress::getBasicBlock() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::BlockAddress>(Val)->getBasicBlock()));
}

FunctionType *Function::getFunctionType() const {
return cast<FunctionType>(
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
Expand Down Expand Up @@ -2585,6 +2611,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
It->second = std::unique_ptr<ConstantFP>(
new ConstantFP(cast<llvm::ConstantFP>(C), *this));
return It->second.get();
case llvm::Value::BlockAddressVal:
It->second = std::unique_ptr<BlockAddress>(
new BlockAddress(cast<llvm::BlockAddress>(C), *this));
return It->second.get();
case llvm::Value::ConstantAggregateZeroVal: {
auto *CAZ = cast<llvm::ConstantAggregateZero>(C);
It->second = std::unique_ptr<ConstantAggregateZero>(
Expand Down Expand Up @@ -2640,7 +2670,7 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
return It->second.get();
}
if (auto *BB = dyn_cast<llvm::BasicBlock>(LLVMV)) {
assert(isa<BlockAddress>(U) &&
assert(isa<llvm::BlockAddress>(U) &&
"This won't create a SBBB, don't call this function directly!");
if (auto *SBBB = getValue(BB))
return SBBB;
Expand Down
48 changes: 48 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,54 @@ define void @foo() {
EXPECT_EQ(UndefStruct->getNumElements(), 2u);
}

TEST_F(SandboxIRTest, BlockAddress) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
bb0:
store ptr blockaddress(@foo, %bb0), ptr %ptr
ret void
bb1:
ret void
bb2:
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);

auto &F = *Ctx.createFunction(&LLVMF);
auto *BB0 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "bb0")));
auto *BB1 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "bb1")));
auto *BB2 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "bb2")));
auto It = BB0->begin();
auto *SI = cast<sandboxir::StoreInst>(&*It++);
[[maybe_unused]] auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

// Check classof(), creation, getFunction(), getBasicBlock().
auto *BB0Addr = cast<sandboxir::BlockAddress>(SI->getValueOperand());
EXPECT_EQ(BB0Addr->getBasicBlock(), BB0);
EXPECT_EQ(BB0Addr->getFunction(), &F);
// Check get(F, BB).
auto *NewBB0Addr = sandboxir::BlockAddress::get(&F, BB0);
EXPECT_EQ(NewBB0Addr, BB0Addr);
// Check get(BB).
auto *NewBB0Addr2 = sandboxir::BlockAddress::get(BB0);
EXPECT_EQ(NewBB0Addr2, BB0Addr);
auto *BB1Addr = sandboxir::BlockAddress::get(BB1);
EXPECT_EQ(BB1Addr->getBasicBlock(), BB1);
EXPECT_NE(BB1Addr, BB0Addr);
// Check lookup().
auto *LookupBB0Addr = sandboxir::BlockAddress::lookup(BB0);
EXPECT_EQ(LookupBB0Addr, BB0Addr);
auto *LookupBB1Addr = sandboxir::BlockAddress::lookup(BB1);
EXPECT_EQ(LookupBB1Addr, BB1Addr);
auto *LookupBB2Addr = sandboxir::BlockAddress::lookup(BB2);
EXPECT_EQ(LookupBB2Addr, nullptr);
}

TEST_F(SandboxIRTest, Use) {
parseIR(C, R"IR(
define i32 @foo(i32 %v0, i32 %v1) {
Expand Down

0 comments on commit 5a29af5

Please sign in to comment.