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

Julius Alexandre via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 12:43:29 PDT 2024


https://github.com/medievalghoul updated https://github.com/llvm/llvm-project/pull/100961

>From 3a9606103dbebed35d04abbf737167b6d6535c82 Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Sun, 28 Jul 2024 22:31:13 -0400
Subject: [PATCH 1/7] [SandboxIR] Added new StoreInst::create() functions with
 isVolatile arg

---
 llvm/include/llvm/SandboxIR/SandboxIR.h    |  9 +++++++++
 llvm/lib/SandboxIR/SandboxIR.cpp           | 23 ++++++++++++++++++++++
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 15 +++++++++++++-
 3 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 667aeba1bda1f..0028f57b777c0 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -804,14 +804,23 @@ 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;
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index d2b4fb207ba3a..1125b5faa1dfd 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -662,6 +662,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);
@@ -673,6 +685,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;
 }
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 122508d15194f..831a9be85a053 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -785,8 +785,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");
@@ -798,9 +799,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_FALSE(St->isVolatile());
+  EXPECT_TRUE(VSt->isVolatile());
   // Check getPointerOperand()
   EXPECT_EQ(St->getValueOperand(), Val);
   EXPECT_EQ(St->getPointerOperand(), Ptr);
@@ -810,10 +815,18 @@ define void @foo(i8 %val, ptr %ptr) {
   sandboxir::StoreInst *NewSt =
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertBefore=*/Ret, Ctx);
+  // Check 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);
+  EXPECT_TRUE(NewVSt->isVolatile());
 }
 
 TEST_F(SandboxIRTest, ReturnInst) {

>From fd4398495251b7275f874ef82d145f88b5cbf18d Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Sun, 28 Jul 2024 22:40:57 -0400
Subject: [PATCH 2/7] fixed formatting

---
 llvm/include/llvm/SandboxIR/SandboxIR.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 0028f57b777c0..324759acd5727 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -804,7 +804,7 @@ class StoreInst final : public Instruction {
   }
 
 public:
-   /// Return true if this is a load from a volatile memory location.
+  /// 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 {

>From 6231816b7e4e8c5161189d39549232b53cc22d73 Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Mon, 29 Jul 2024 03:09:18 -0400
Subject: [PATCH 3/7] simple code refactoring

---
 llvm/lib/SandboxIR/SandboxIR.cpp           | 16 ++--------------
 llvm/unittests/SandboxIR/SandboxIRTest.cpp |  1 +
 2 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 1125b5faa1dfd..efcb1b74a5320 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -654,13 +654,7 @@ void LoadInst::dump() const {
 #endif // NDEBUG
 StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
                              Instruction *InsertBefore, 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 *NewSBI = Ctx.createStoreInst(NewSI);
-  return NewSBI;
+  return create(V, Ptr, Align, InsertBefore, /*IsVolatile=*/false, Ctx);
 }
 
 StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
@@ -676,13 +670,7 @@ StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
 
 StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
                              BasicBlock *InsertAtEnd, 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 *NewSBI = Ctx.createStoreInst(NewSI);
-  return NewSBI;
+  return create(V, Ptr, Align, InsertAtEnd, /*IsVolatile=*/false, Ctx);
 }
 
 StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 831a9be85a053..a9b4dda7b22d3 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -826,6 +826,7 @@ define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertBefore=*/Ret,
                                    /*IsVolatile=*/true, Ctx);
+  // Check if create was volatile
   EXPECT_TRUE(NewVSt->isVolatile());
 }
 

>From a958fd2d3bfce45fa3f9bbf71ba2e6bacf89e93b Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Mon, 29 Jul 2024 14:04:29 -0400
Subject: [PATCH 4/7] minor change: fixed StoreInst

---
 llvm/include/llvm/SandboxIR/SandboxIR.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 324759acd5727..aad2383aa0dbb 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -805,7 +805,7 @@ 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(); }
+  bool isVolatile() const { return cast<llvm::StoreInst>(Val)->isVolatile(); }
 
   unsigned getUseOperandNo(const Use &Use) const final {
     return getUseOperandNoDefault(Use);

>From 8e52ce3ab1996f5a2433119150c1030e7a5e03f2 Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Mon, 29 Jul 2024 14:23:38 -0400
Subject: [PATCH 5/7] added more functional tests for StoreInst

---
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 28 +++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index a9b4dda7b22d3..ef2953b5d213e 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -821,13 +821,39 @@ define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
   EXPECT_EQ(NewSt->getValueOperand(), Val);
   EXPECT_EQ(NewSt->getPointerOperand(), Ptr);
   EXPECT_EQ(NewSt->getAlign(), 8);
-  // Check create(InsertBefore)
+  // Check create(InsertBefore, IsVolatile=true)
   sandboxir::StoreInst *NewVSt =
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertBefore=*/Ret,
                                    /*IsVolatile=*/true, Ctx);
   // Check if create was volatile
   EXPECT_TRUE(NewVSt->isVolatile());
+
+  // Check create(InsertAtEnd)
+  sandboxir::StoreInst *NewStEnd =
+      sandboxir::StoreInst::create(Val, Ptr, Align(8),
+                                   /*InsertAtEnd=*/BB, Ctx);
+  // Check if create was volatile
+  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);
+  // Check if create was volatile
+  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) {

>From ffda4434803706148c036cf754cf204456641802 Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Mon, 29 Jul 2024 15:39:16 -0400
Subject: [PATCH 6/7] fixed some comments

---
 llvm/include/llvm/SandboxIR/SandboxIR.h    | 2 +-
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 9 ++++-----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index aad2383aa0dbb..c36f5c27e27e5 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -804,7 +804,7 @@ class StoreInst final : public Instruction {
   }
 
 public:
-  /// Return true if this is a load from a volatile memory location.
+  /// 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 {
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index ef2953b5d213e..066e3a1560a73 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -815,7 +815,6 @@ define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
   sandboxir::StoreInst *NewSt =
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertBefore=*/Ret, Ctx);
-  // Check if create was volatile
   EXPECT_FALSE(NewSt->isVolatile());
   EXPECT_EQ(NewSt->getType(), St->getType());
   EXPECT_EQ(NewSt->getValueOperand(), Val);
@@ -826,14 +825,15 @@ define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertBefore=*/Ret,
                                    /*IsVolatile=*/true, Ctx);
-  // Check if create was volatile
   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);
   // Check create(InsertAtEnd)
   sandboxir::StoreInst *NewStEnd =
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertAtEnd=*/BB, Ctx);
-  // Check if create was volatile
   EXPECT_FALSE(NewStEnd->isVolatile());
   EXPECT_EQ(NewStEnd->getType(), St->getType());
   EXPECT_EQ(NewStEnd->getValueOperand(), Val);
@@ -846,7 +846,6 @@ define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
       sandboxir::StoreInst::create(Val, Ptr, Align(8),
                                    /*InsertAtEnd=*/BB,
                                    /*IsVolatile=*/true, Ctx);
-  // Check if create was volatile
   EXPECT_TRUE(NewVStEnd->isVolatile());
   EXPECT_EQ(NewVStEnd->getType(), VSt->getType());
   EXPECT_EQ(NewVStEnd->getValueOperand(), Val);

>From a7c893e1152d0cbe6d3df9ba427f45de18aa5c37 Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Mon, 29 Jul 2024 15:43:12 -0400
Subject: [PATCH 7/7] added check the instruction position

---
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 066e3a1560a73..c1e181f039b30 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -830,6 +830,7 @@ define void @foo(i8 %val, ptr %ptr, ptr %vptr) {
   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),



More information about the llvm-commits mailing list