Skip to content

Commit

Permalink
[SandboxIR] Implement ConstantExpr (#109491)
Browse files Browse the repository at this point in the history
This patch implements an empty sandboxir::ConstantExpr class, mirroring
llvm::ConstantExpr.
  • Loading branch information
vporpo committed Sep 23, 2024
1 parent e093bb9 commit 416c3ce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class GlobalVariable;
class GlobalAlias;
class NoCFIValue;
class ConstantPtrAuth;
class ConstantExpr;
class Context;
class Function;
class Instruction;
Expand Down Expand Up @@ -344,6 +345,7 @@ class Value {
friend class GlobalAlias; // For `Val`.
friend class NoCFIValue; // For `Val`.
friend class ConstantPtrAuth; // For `Val`.
friend class ConstantExpr; // For `Val`.

/// All values point to the context.
Context &Ctx;
Expand Down Expand Up @@ -1661,6 +1663,19 @@ class ConstantPtrAuth final : public Constant {
}
};

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

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

class BlockAddress final : public Constant {
BlockAddress(llvm::BlockAddress *C, Context &Ctx)
: Constant(ClassID::BlockAddress, C, Ctx) {}
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 @@ -40,6 +40,7 @@ DEF_CONST(GlobalAlias, GlobalAlias)
DEF_CONST(BlockAddress, BlockAddress)
DEF_CONST(NoCFIValue, NoCFIValue)
DEF_CONST(ConstantPtrAuth, ConstantPtrAuth)
DEF_CONST(ConstantExpr, ConstantExpr)
DEF_CONST(DSOLocalEquivalent, DSOLocalEquivalent)
DEF_CONST(ConstantTokenNone, ConstantTokenNone)

Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
It->second = std::unique_ptr<ConstantPtrAuth>(
new ConstantPtrAuth(cast<llvm::ConstantPtrAuth>(C), *this));
break;
case llvm::Value::ConstantExprVal:
It->second = std::unique_ptr<ConstantExpr>(
new ConstantExpr(cast<llvm::ConstantExpr>(C), *this));
break;
default:
It->second = std::unique_ptr<Constant>(new Constant(C, *this));
break;
Expand Down
18 changes: 18 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,24 @@ define ptr @foo() {
EXPECT_EQ(PtrAuth->getWithSameSchema(&F), PtrAuth);
}

TEST_F(SandboxIRTest, ConstantExpr) {
parseIR(C, R"IR(
define i32 @foo() {
ret i32 ptrtoint (ptr @foo to i32)
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);

auto &F = *Ctx.createFunction(&LLVMF);
auto *BB = &*F.begin();
auto It = BB->begin();
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
// Check classof(), creation.
[[maybe_unused]] auto *ConstExpr =
cast<sandboxir::ConstantExpr>(Ret->getReturnValue());
}

TEST_F(SandboxIRTest, BlockAddress) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
Expand Down

0 comments on commit 416c3ce

Please sign in to comment.