[llvm] [SandboxIR] Implement FreezeInst (PR #106133)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 26 13:40:48 PDT 2024
https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/106133
This patch implements sandboxir::FreezeInst mirroring llvm::FreezeInst.
>From 58d8960f49af88e4d75066db32463bf895ab5be6 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Thu, 15 Aug 2024 16:26:30 -0700
Subject: [PATCH] [SandboxIR] Implement FreezeInst
This patch implements sandboxir::FreezeInst mirroring llvm::FreezeInst.
---
llvm/include/llvm/SandboxIR/SandboxIR.h | 20 ++++++++++++-
.../llvm/SandboxIR/SandboxIRValues.def | 1 +
llvm/lib/SandboxIR/SandboxIR.cpp | 22 ++++++++++++++
llvm/unittests/SandboxIR/SandboxIRTest.cpp | 29 +++++++++++++++++++
4 files changed, 71 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 06f05f88c9c082..d3bee26c3142c9 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -111,6 +111,7 @@ class ConstantInt;
class Context;
class Function;
class Instruction;
+class FreezeInst;
class FenceInst;
class SelectInst;
class ExtractElementInst;
@@ -251,6 +252,7 @@ class Value {
friend class Context; // For getting `Val`.
friend class User; // For getting `Val`.
friend class Use; // For getting `Val`.
+ friend class FreezeInst; // For getting `Val`.
friend class FenceInst; // For getting `Val`.
friend class SelectInst; // For getting `Val`.
friend class ExtractElementInst; // For getting `Val`.
@@ -682,6 +684,7 @@ class Instruction : public sandboxir::User {
/// A SandboxIR Instruction may map to multiple LLVM IR Instruction. This
/// returns its topmost LLVM IR instruction.
llvm::Instruction *getTopmostLLVMInstruction() const;
+ friend class FreezeInst; // For getTopmostLLVMInstruction().
friend class FenceInst; // For getTopmostLLVMInstruction().
friend class SelectInst; // For getTopmostLLVMInstruction().
friend class ExtractElementInst; // For getTopmostLLVMInstruction().
@@ -1528,13 +1531,26 @@ class UnaryInstruction
public:
static bool classof(const Instruction *I) {
- return isa<LoadInst>(I) || isa<CastInst>(I);
+ return isa<LoadInst>(I) || isa<CastInst>(I) || isa<FreezeInst>(I);
}
static bool classof(const Value *V) {
return isa<Instruction>(V) && classof(cast<Instruction>(V));
}
};
+class FreezeInst : public UnaryInstruction {
+ FreezeInst(llvm::FreezeInst *FI, Context &Ctx)
+ : UnaryInstruction(ClassID::Freeze, Opcode::Freeze, FI, Ctx) {}
+ friend Context; // For constructor;
+
+public:
+ static FreezeInst *create(Value *V, BBIterator WhereIt, BasicBlock *WhereBB,
+ Context &Ctx, const Twine &Name = "");
+ static bool classof(const Value *From) {
+ return From->getSubclassID() == ClassID::Freeze;
+ }
+};
+
class LoadInst final : public UnaryInstruction {
/// Use LoadInst::create() instead of calling the constructor.
LoadInst(llvm::LoadInst *LI, Context &Ctx)
@@ -2930,6 +2946,8 @@ class Context {
IRBuilder<ConstantFolder> LLVMIRBuilder;
auto &getLLVMIRBuilder() { return LLVMIRBuilder; }
+ FreezeInst *createFreezeInst(llvm::FreezeInst *SI);
+ friend FreezeInst; // For createFreezeInst()
FenceInst *createFenceInst(llvm::FenceInst *SI);
friend FenceInst; // For createFenceInst()
SelectInst *createSelectInst(llvm::SelectInst *SI);
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index a74e7adfeab139..8d79523253f233 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -37,6 +37,7 @@ DEF_USER(ConstantInt, ConstantInt)
DEF_INSTR(Opaque, OP(Opaque), OpaqueInst)
DEF_INSTR(ExtractElement, OP(ExtractElement), ExtractElementInst)
DEF_INSTR(InsertElement, OP(InsertElement), InsertElementInst)
+DEF_INSTR(Freeze, OP(Freeze), FreezeInst)
DEF_INSTR(Fence, OP(Fence), FenceInst)
DEF_INSTR(ShuffleVector, OP(ShuffleVector), ShuffleVectorInst)
DEF_INSTR(Select, OP(Select), SelectInst)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 31992b043e9fc6..ccb95b26070446 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -575,6 +575,18 @@ void Instruction::dumpOS(raw_ostream &OS) const {
}
#endif // NDEBUG
+FreezeInst *FreezeInst::create(Value *V, BBIterator WhereIt,
+ BasicBlock *WhereBB, Context &Ctx,
+ const Twine &Name) {
+ auto &Builder = Ctx.getLLVMIRBuilder();
+ if (WhereIt != WhereBB->end())
+ Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
+ else
+ Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
+ auto *LLVMI = cast<llvm::FreezeInst>(Builder.CreateFreeze(V->Val, Name));
+ return Ctx.createFreezeInst(LLVMI);
+}
+
FenceInst *FenceInst::create(AtomicOrdering Ordering, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx,
SyncScope::ID SSID) {
@@ -2209,6 +2221,11 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
assert(isa<llvm::Instruction>(LLVMV) && "Expected Instruction");
switch (cast<llvm::Instruction>(LLVMV)->getOpcode()) {
+ case llvm::Instruction::Freeze: {
+ auto *LLVMFreeze = cast<llvm::FreezeInst>(LLVMV);
+ It->second = std::unique_ptr<FreezeInst>(new FreezeInst(LLVMFreeze, *this));
+ return It->second.get();
+ }
case llvm::Instruction::Fence: {
auto *LLVMFence = cast<llvm::FenceInst>(LLVMV);
It->second = std::unique_ptr<FenceInst>(new FenceInst(LLVMFence, *this));
@@ -2412,6 +2429,11 @@ BasicBlock *Context::createBasicBlock(llvm::BasicBlock *LLVMBB) {
return BB;
}
+FreezeInst *Context::createFreezeInst(llvm::FreezeInst *SI) {
+ auto NewPtr = std::unique_ptr<FreezeInst>(new FreezeInst(SI, *this));
+ return cast<FreezeInst>(registerValue(std::move(NewPtr)));
+}
+
FenceInst *Context::createFenceInst(llvm::FenceInst *SI) {
auto NewPtr = std::unique_ptr<FenceInst>(new FenceInst(SI, *this));
return cast<FenceInst>(registerValue(std::move(NewPtr)));
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index eb405b11be351c..5ab61bd36b9bb3 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -580,6 +580,35 @@ define void @foo(i8 %v1) {
EXPECT_EQ(I0->getNextNode(), Ret);
}
+TEST_F(SandboxIRTest, FreezeInst) {
+ parseIR(C, R"IR(
+define void @foo(i8 %arg) {
+ freeze i8 %arg
+ ret void
+}
+)IR");
+ llvm::Function *LLVMF = &*M->getFunction("foo");
+
+ sandboxir::Context Ctx(C);
+ sandboxir::Function *F = Ctx.createFunction(LLVMF);
+ auto *Arg = F->getArg(0);
+ auto *BB = &*F->begin();
+ auto It = BB->begin();
+ auto *Freeze = cast<sandboxir::FreezeInst>(&*It++);
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+
+ EXPECT_TRUE(isa<sandboxir::UnaryInstruction>(Freeze));
+ EXPECT_EQ(Freeze->getOperand(0), Arg);
+
+ // Check create().
+ auto *NewFreeze = sandboxir::FreezeInst::create(
+ Arg, Ret->getIterator(), Ret->getParent(), Ctx, "NewFreeze");
+ EXPECT_EQ(NewFreeze->getNextNode(), Ret);
+#ifndef NDEBUG
+ EXPECT_EQ(NewFreeze->getName(), "NewFreeze");
+#endif // NDEBUG
+}
+
TEST_F(SandboxIRTest, FenceInst) {
parseIR(C, R"IR(
define void @foo() {
More information about the llvm-commits
mailing list