Skip to content

[SandboxIR] Implement ConstantExpr #109491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1179,6 +1179,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
Loading