Skip to content

[SandboxIR] Added new StoreInst::create() functions with isVolatile arg #100961

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
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
8 changes: 8 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,22 @@ class StoreInst final : public Instruction {
}

public:
/// Return true if this is a store from a volatile memory location.
bool isVolatile() const { return cast<llvm::StoreInst>(Val)->isVolatile(); }
unsigned getUseOperandNo(const Use &Use) const final {
return getUseOperandNoDefault(Use);
}
unsigned getNumOfIRInstrs() const final { return 1u; }
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, Context &Ctx);
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, bool IsVolatile,
Context &Ctx);
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, Context &Ctx);
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, bool IsVolatile,
Context &Ctx);
/// For isa/dyn_cast.
static bool classof(const Value *From);
Value *getValueOperand() const;
Expand Down
19 changes: 15 additions & 4 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,21 +654,32 @@ void LoadInst::dump() const {
#endif // NDEBUG
StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, Context &Ctx) {
return create(V, Ptr, Align, InsertBefore, /*IsVolatile=*/false, Ctx);
}

StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, bool IsVolatile,
Context &Ctx) {
llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
auto &Builder = Ctx.getLLVMIRBuilder();
Builder.SetInsertPoint(BeforeIR);
auto *NewSI =
Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, /*isVolatile=*/false);
auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
auto *NewSBI = Ctx.createStoreInst(NewSI);
return NewSBI;
}

StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, Context &Ctx) {
return create(V, Ptr, Align, InsertAtEnd, /*IsVolatile=*/false, Ctx);
}

StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, bool IsVolatile,
Context &Ctx) {
auto *InsertAtEndIR = cast<llvm::BasicBlock>(InsertAtEnd->Val);
auto &Builder = Ctx.getLLVMIRBuilder();
Builder.SetInsertPoint(InsertAtEndIR);
auto *NewSI =
Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, /*isVolatile=*/false);
auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
auto *NewSBI = Ctx.createStoreInst(NewSI);
return NewSBI;
}
Expand Down
40 changes: 40 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ TEST_F(SandboxIRTest, StoreInst) {
parseIR(C, R"IR(
define void @foo(i8 %val, ptr %ptr) {
store i8 %val, ptr %ptr, align 64
store volatile i8 %val, ptr %ptr, align 64
ret void
}
)IR");
Expand All @@ -798,9 +799,12 @@ define void @foo(i8 %val, ptr %ptr) {
auto *BB = &*F->begin();
auto It = BB->begin();
auto *St = cast<sandboxir::StoreInst>(&*It++);
auto *VSt = cast<sandboxir::StoreInst>(&*It++);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

// Check that the StoreInst has been created correctly.
EXPECT_FALSE(St->isVolatile());
EXPECT_TRUE(VSt->isVolatile());
// Check getPointerOperand()
EXPECT_EQ(St->getValueOperand(), Val);
EXPECT_EQ(St->getPointerOperand(), Ptr);
Expand All @@ -810,10 +814,46 @@ define void @foo(i8 %val, ptr %ptr) {
sandboxir::StoreInst *NewSt =
sandboxir::StoreInst::create(Val, Ptr, Align(8),
/*InsertBefore=*/Ret, Ctx);
EXPECT_FALSE(NewSt->isVolatile());
EXPECT_EQ(NewSt->getType(), St->getType());
EXPECT_EQ(NewSt->getValueOperand(), Val);
EXPECT_EQ(NewSt->getPointerOperand(), Ptr);
EXPECT_EQ(NewSt->getAlign(), 8);
EXPECT_EQ(NewSt->getNextNode(), Ret);
// Check create(InsertBefore, IsVolatile=true)
sandboxir::StoreInst *NewVSt =
sandboxir::StoreInst::create(Val, Ptr, Align(8),
/*InsertBefore=*/Ret,
/*IsVolatile=*/true, Ctx);
EXPECT_TRUE(NewVSt->isVolatile());
EXPECT_EQ(NewVSt->getType(), VSt->getType());
EXPECT_EQ(NewVSt->getValueOperand(), Val);
EXPECT_EQ(NewVSt->getPointerOperand(), Ptr);
EXPECT_EQ(NewVSt->getAlign(), 8);
EXPECT_EQ(NewVSt->getNextNode(), Ret);
// Check create(InsertAtEnd)
sandboxir::StoreInst *NewStEnd =
sandboxir::StoreInst::create(Val, Ptr, Align(8),
/*InsertAtEnd=*/BB, Ctx);
EXPECT_FALSE(NewStEnd->isVolatile());
EXPECT_EQ(NewStEnd->getType(), St->getType());
EXPECT_EQ(NewStEnd->getValueOperand(), Val);
EXPECT_EQ(NewStEnd->getPointerOperand(), Ptr);
EXPECT_EQ(NewStEnd->getAlign(), 8);
EXPECT_EQ(NewStEnd->getParent(), BB);
EXPECT_EQ(NewStEnd->getNextNode(), nullptr);
// Check create(InsertAtEnd, IsVolatile=true)
sandboxir::StoreInst *NewVStEnd =
sandboxir::StoreInst::create(Val, Ptr, Align(8),
/*InsertAtEnd=*/BB,
/*IsVolatile=*/true, Ctx);
EXPECT_TRUE(NewVStEnd->isVolatile());
EXPECT_EQ(NewVStEnd->getType(), VSt->getType());
EXPECT_EQ(NewVStEnd->getValueOperand(), Val);
EXPECT_EQ(NewVStEnd->getPointerOperand(), Ptr);
EXPECT_EQ(NewVStEnd->getAlign(), 8);
EXPECT_EQ(NewVStEnd->getParent(), BB);
EXPECT_EQ(NewVStEnd->getNextNode(), nullptr);
}

TEST_F(SandboxIRTest, ReturnInst) {
Expand Down
Loading