[llvm] 416c3ce - [SandboxIR] Implement ConstantExpr (#109491)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 23 11:45:24 PDT 2024
Author: vporpo
Date: 2024-09-23T11:45:20-07:00
New Revision: 416c3ce0138ff4039dab13ff634ee6392b9a3c7b
URL: https://github.com/llvm/llvm-project/commit/416c3ce0138ff4039dab13ff634ee6392b9a3c7b
DIFF: https://github.com/llvm/llvm-project/commit/416c3ce0138ff4039dab13ff634ee6392b9a3c7b.diff
LOG: [SandboxIR] Implement ConstantExpr (#109491)
This patch implements an empty sandboxir::ConstantExpr class, mirroring
llvm::ConstantExpr.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/include/llvm/SandboxIR/SandboxIRValues.def
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 3f8946a7ae967e..4e751a75196ce1 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -135,6 +135,7 @@ class GlobalVariable;
class GlobalAlias;
class NoCFIValue;
class ConstantPtrAuth;
+class ConstantExpr;
class Context;
class Function;
class Instruction;
@@ -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;
@@ -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) {}
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index 3367c7d7794186..2a9ca6d3d73ce6 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -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)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index b96141f7c60f0b..b1ed72be1030d6 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -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;
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 9088a0f7b80ea4..ae143fd5b74ea1 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -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) {
More information about the llvm-commits
mailing list