Skip to content

Commit 42326c7

Browse files
[SandboxIR] Added new StoreInst::create() functions with isVolatile arg (#100961)
This patch implements new create() functions with an `IsVolatile` argument for sandboxir::StoreInst.
1 parent 76605f5 commit 42326c7

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,22 @@ class StoreInst final : public Instruction {
817817
}
818818

819819
public:
820+
/// Return true if this is a store from a volatile memory location.
821+
bool isVolatile() const { return cast<llvm::StoreInst>(Val)->isVolatile(); }
820822
unsigned getUseOperandNo(const Use &Use) const final {
821823
return getUseOperandNoDefault(Use);
822824
}
823825
unsigned getNumOfIRInstrs() const final { return 1u; }
824826
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
825827
Instruction *InsertBefore, Context &Ctx);
828+
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
829+
Instruction *InsertBefore, bool IsVolatile,
830+
Context &Ctx);
826831
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
827832
BasicBlock *InsertAtEnd, Context &Ctx);
833+
static StoreInst *create(Value *V, Value *Ptr, MaybeAlign Align,
834+
BasicBlock *InsertAtEnd, bool IsVolatile,
835+
Context &Ctx);
828836
/// For isa/dyn_cast.
829837
static bool classof(const Value *From);
830838
Value *getValueOperand() const;

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,21 +667,32 @@ void LoadInst::dump() const {
667667
#endif // NDEBUG
668668
StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
669669
Instruction *InsertBefore, Context &Ctx) {
670+
return create(V, Ptr, Align, InsertBefore, /*IsVolatile=*/false, Ctx);
671+
}
672+
673+
StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
674+
Instruction *InsertBefore, bool IsVolatile,
675+
Context &Ctx) {
670676
llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
671677
auto &Builder = Ctx.getLLVMIRBuilder();
672678
Builder.SetInsertPoint(BeforeIR);
673-
auto *NewSI =
674-
Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, /*isVolatile=*/false);
679+
auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
675680
auto *NewSBI = Ctx.createStoreInst(NewSI);
676681
return NewSBI;
677682
}
683+
678684
StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
679685
BasicBlock *InsertAtEnd, Context &Ctx) {
686+
return create(V, Ptr, Align, InsertAtEnd, /*IsVolatile=*/false, Ctx);
687+
}
688+
689+
StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
690+
BasicBlock *InsertAtEnd, bool IsVolatile,
691+
Context &Ctx) {
680692
auto *InsertAtEndIR = cast<llvm::BasicBlock>(InsertAtEnd->Val);
681693
auto &Builder = Ctx.getLLVMIRBuilder();
682694
Builder.SetInsertPoint(InsertAtEndIR);
683-
auto *NewSI =
684-
Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, /*isVolatile=*/false);
695+
auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
685696
auto *NewSBI = Ctx.createStoreInst(NewSI);
686697
return NewSBI;
687698
}

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ TEST_F(SandboxIRTest, StoreInst) {
810810
parseIR(C, R"IR(
811811
define void @foo(i8 %val, ptr %ptr) {
812812
store i8 %val, ptr %ptr, align 64
813+
store volatile i8 %val, ptr %ptr, align 64
813814
ret void
814815
}
815816
)IR");
@@ -821,9 +822,12 @@ define void @foo(i8 %val, ptr %ptr) {
821822
auto *BB = &*F->begin();
822823
auto It = BB->begin();
823824
auto *St = cast<sandboxir::StoreInst>(&*It++);
825+
auto *VSt = cast<sandboxir::StoreInst>(&*It++);
824826
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
825827

826828
// Check that the StoreInst has been created correctly.
829+
EXPECT_FALSE(St->isVolatile());
830+
EXPECT_TRUE(VSt->isVolatile());
827831
// Check getPointerOperand()
828832
EXPECT_EQ(St->getValueOperand(), Val);
829833
EXPECT_EQ(St->getPointerOperand(), Ptr);
@@ -833,10 +837,46 @@ define void @foo(i8 %val, ptr %ptr) {
833837
sandboxir::StoreInst *NewSt =
834838
sandboxir::StoreInst::create(Val, Ptr, Align(8),
835839
/*InsertBefore=*/Ret, Ctx);
840+
EXPECT_FALSE(NewSt->isVolatile());
836841
EXPECT_EQ(NewSt->getType(), St->getType());
837842
EXPECT_EQ(NewSt->getValueOperand(), Val);
838843
EXPECT_EQ(NewSt->getPointerOperand(), Ptr);
839844
EXPECT_EQ(NewSt->getAlign(), 8);
845+
EXPECT_EQ(NewSt->getNextNode(), Ret);
846+
// Check create(InsertBefore, IsVolatile=true)
847+
sandboxir::StoreInst *NewVSt =
848+
sandboxir::StoreInst::create(Val, Ptr, Align(8),
849+
/*InsertBefore=*/Ret,
850+
/*IsVolatile=*/true, Ctx);
851+
EXPECT_TRUE(NewVSt->isVolatile());
852+
EXPECT_EQ(NewVSt->getType(), VSt->getType());
853+
EXPECT_EQ(NewVSt->getValueOperand(), Val);
854+
EXPECT_EQ(NewVSt->getPointerOperand(), Ptr);
855+
EXPECT_EQ(NewVSt->getAlign(), 8);
856+
EXPECT_EQ(NewVSt->getNextNode(), Ret);
857+
// Check create(InsertAtEnd)
858+
sandboxir::StoreInst *NewStEnd =
859+
sandboxir::StoreInst::create(Val, Ptr, Align(8),
860+
/*InsertAtEnd=*/BB, Ctx);
861+
EXPECT_FALSE(NewStEnd->isVolatile());
862+
EXPECT_EQ(NewStEnd->getType(), St->getType());
863+
EXPECT_EQ(NewStEnd->getValueOperand(), Val);
864+
EXPECT_EQ(NewStEnd->getPointerOperand(), Ptr);
865+
EXPECT_EQ(NewStEnd->getAlign(), 8);
866+
EXPECT_EQ(NewStEnd->getParent(), BB);
867+
EXPECT_EQ(NewStEnd->getNextNode(), nullptr);
868+
// Check create(InsertAtEnd, IsVolatile=true)
869+
sandboxir::StoreInst *NewVStEnd =
870+
sandboxir::StoreInst::create(Val, Ptr, Align(8),
871+
/*InsertAtEnd=*/BB,
872+
/*IsVolatile=*/true, Ctx);
873+
EXPECT_TRUE(NewVStEnd->isVolatile());
874+
EXPECT_EQ(NewVStEnd->getType(), VSt->getType());
875+
EXPECT_EQ(NewVStEnd->getValueOperand(), Val);
876+
EXPECT_EQ(NewVStEnd->getPointerOperand(), Ptr);
877+
EXPECT_EQ(NewVStEnd->getAlign(), 8);
878+
EXPECT_EQ(NewVStEnd->getParent(), BB);
879+
EXPECT_EQ(NewVStEnd->getNextNode(), nullptr);
840880
}
841881

842882
TEST_F(SandboxIRTest, ReturnInst) {

0 commit comments

Comments
 (0)