Skip to content

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

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

Closed
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
21 changes: 19 additions & 2 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,17 @@ class LoadInst final : public Instruction {
unsigned getNumOfIRInstrs() const final { return 1u; }
static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, Context &Ctx,
bool IsVolatile = false, const Twine &Name = "");
const Twine &Name = "");
static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, bool IsVolatile,
Context &Ctx, const Twine &Name = "");
static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, Context &Ctx,
bool IsVolatile = false, const Twine &Name = "");
const Twine &Name = "");
static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, bool IsVolatile,
Context &Ctx, const Twine &Name = "");

/// For isa/dyn_cast.
static bool classof(const Value *From);
Value *getPointerOperand() const;
Expand Down Expand Up @@ -804,14 +811,24 @@ class StoreInst final : public Instruction {
}

public:
/// Return true if this is a load from a volatile memory location.
bool isVolatile() const { return cast<llvm::LoadInst>(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
50 changes: 48 additions & 2 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,19 @@ void BranchInst::dump() const {

LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, Context &Ctx,
bool IsVolatile, const Twine &Name) {
const Twine &Name) {
llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
auto &Builder = Ctx.getLLVMIRBuilder();
Builder.SetInsertPoint(BeforeIR);
auto *NewLI = Builder.CreateAlignedLoad(Ty, Ptr->Val, Align,
/*IsVolatile=*/false, Name);
auto *NewSBI = Ctx.createLoadInst(NewLI);
return NewSBI;
}

LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
Instruction *InsertBefore, bool IsVolatile,
Context &Ctx, const Twine &Name) {
llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
auto &Builder = Ctx.getLLVMIRBuilder();
Builder.SetInsertPoint(BeforeIR);
Expand All @@ -624,7 +636,18 @@ LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,

LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, Context &Ctx,
bool IsVolatile, const Twine &Name) {
const Twine &Name) {
auto &Builder = Ctx.getLLVMIRBuilder();
Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
auto *NewLI = Builder.CreateAlignedLoad(Ty, Ptr->Val, Align,
/*IsVolatile=*/false, Name);
auto *NewSBI = Ctx.createLoadInst(NewLI);
return NewSBI;
}

LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, bool IsVolatile,
Context &Ctx, const Twine &Name) {
auto &Builder = Ctx.getLLVMIRBuilder();
Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
auto *NewLI =
Expand Down Expand Up @@ -662,6 +685,18 @@ StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
auto *NewSBI = Ctx.createStoreInst(NewSI);
return NewSBI;
}

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);
auto *NewSBI = Ctx.createStoreInst(NewSI);
return NewSBI;
}

StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
BasicBlock *InsertAtEnd, Context &Ctx) {
auto *InsertAtEndIR = cast<llvm::BasicBlock>(InsertAtEnd->Val);
Expand All @@ -673,6 +708,17 @@ StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
return NewSBI;
}

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);
auto *NewSBI = Ctx.createStoreInst(NewSI);
return NewSBI;
}

bool StoreInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Store;
}
Expand Down
30 changes: 22 additions & 8 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,22 +750,21 @@ define void @foo(ptr %arg0, ptr %arg1) {
auto *BB = &*F->begin();
auto It = BB->begin();
auto *Ld = cast<sandboxir::LoadInst>(&*It++);
auto *Vld = cast<sandboxir::LoadInst>(&*It++);
auto *VLd = cast<sandboxir::LoadInst>(&*It++);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

// Check isVolatile()
EXPECT_FALSE(Ld->isVolatile());
// Check isVolatile()
EXPECT_TRUE(Vld->isVolatile());
EXPECT_TRUE(VLd->isVolatile());
// Check getPointerOperand()
EXPECT_EQ(Ld->getPointerOperand(), Arg0);
// Check getAlign()
EXPECT_EQ(Ld->getAlign(), 64);
// Check create(InsertBefore)
sandboxir::LoadInst *NewLd =
sandboxir::LoadInst::create(Ld->getType(), Arg1, Align(8),
/*InsertBefore=*/Ret, Ctx,
/*IsVolatile=*/false, "NewLd");
/*InsertBefore=*/Ret, Ctx, "NewLd");
// Checking if create() was volatile
EXPECT_FALSE(NewLd->isVolatile());
EXPECT_EQ(NewLd->getType(), Ld->getType());
Expand All @@ -774,9 +773,9 @@ define void @foo(ptr %arg0, ptr %arg1) {
EXPECT_EQ(NewLd->getName(), "NewLd");

sandboxir::LoadInst *NewVLd =
sandboxir::LoadInst::create(Vld->getType(), Arg1, Align(8),
/*InsertBefore=*/Ret, Ctx,
/*IsVolatile=*/true, "NewVLd");
sandboxir::LoadInst::create(VLd->getType(), Arg1, Align(8),
/*InsertBefore=*/Ret,
/*IsVolatile=*/true, Ctx, "NewVLd");

// Checking if create() was volatile
EXPECT_TRUE(NewVLd->isVolatile());
Expand All @@ -785,8 +784,9 @@ define void @foo(ptr %arg0, ptr %arg1) {

TEST_F(SandboxIRTest, StoreInst) {
parseIR(C, R"IR(
define void @foo(i8 %val, ptr %ptr) {
define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
store i8 %val, ptr %ptr, align 64
store volatile i8 %val, ptr %vptr, align 64
ret void
}
)IR");
Expand All @@ -798,9 +798,13 @@ 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.
// Check isVolatile()
EXPECT_TRUE(VSt->isVolatile());
EXPECT_FALSE(St->isVolatile());
// Check getPointerOperand()
EXPECT_EQ(St->getValueOperand(), Val);
EXPECT_EQ(St->getPointerOperand(), Ptr);
Expand All @@ -810,10 +814,20 @@ define void @foo(i8 %val, ptr %ptr) {
sandboxir::StoreInst *NewSt =
sandboxir::StoreInst::create(Val, Ptr, Align(8),
/*InsertBefore=*/Ret, Ctx);
// Checking if create() was volatile
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);

// Check create(InsertBefore)
sandboxir::StoreInst *NewVSt =
sandboxir::StoreInst::create(Val, Ptr, Align(8),
/*InsertBefore=*/Ret,
/*IsVolatile=*/true, Ctx);
// Checking if create() was volatile
EXPECT_TRUE(NewVSt->isVolatile());
}

TEST_F(SandboxIRTest, ReturnInst) {
Expand Down
Loading