[llvm] [SandboxIR] Added isVolatile args to existing LoadInst::create function (PR #100850)

Julius Alexandre via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 27 15:13:49 PDT 2024


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

>From f66ee5e7c901bda1a2cb78a8554f7621dc96020b Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Fri, 26 Jul 2024 21:48:23 -0400
Subject: [PATCH 1/2] Added isVolatile args in LoadInst::create function

---
 llvm/include/llvm/SandboxIR/SandboxIR.h    |  4 ++--
 llvm/lib/SandboxIR/SandboxIR.cpp           |  8 ++++----
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 13 ++++++++++++-
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 5c89f165c9647..d13e547bc97cc 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -769,10 +769,10 @@ class LoadInst final : public Instruction {
   unsigned getNumOfIRInstrs() const final { return 1u; }
   static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
                           Instruction *InsertBefore, Context &Ctx,
-                          const Twine &Name = "");
+                          bool isVolatile = false, const Twine &Name = "");
   static LoadInst *create(Type *Ty, Value *Ptr, MaybeAlign Align,
                           BasicBlock *InsertAtEnd, Context &Ctx,
-                          const Twine &Name = "");
+                          bool isVolatile = false, const Twine &Name = "");
   /// For isa/dyn_cast.
   static bool classof(const Value *From);
   Value *getPointerOperand() const;
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index da482765c7d11..9e41bacf1a8dd 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -612,23 +612,23 @@ void BranchInst::dump() const {
 
 LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
                            Instruction *InsertBefore, Context &Ctx,
-                           const Twine &Name) {
+                           bool isVolatile, 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);
+                                          isVolatile, Name);
   auto *NewSBI = Ctx.createLoadInst(NewLI);
   return NewSBI;
 }
 
 LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
                            BasicBlock *InsertAtEnd, Context &Ctx,
-                           const Twine &Name) {
+                           bool isVolatile, 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);
+                                          isVolatile, Name);
   auto *NewSBI = Ctx.createLoadInst(NewLI);
   return NewSBI;
 }
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 0565e87ce9517..d1d326dab9cbb 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -764,11 +764,22 @@ define void @foo(ptr %arg0, ptr %arg1) {
   // Check create(InsertBefore)
   sandboxir::LoadInst *NewLd =
       sandboxir::LoadInst::create(Ld->getType(), Arg1, Align(8),
-                                  /*InsertBefore=*/Ret, Ctx, "NewLd");
+                                  /*InsertBefore=*/Ret, Ctx, 
+                                  Ld->isVolatile(), "NewLd");
   EXPECT_EQ(NewLd->getType(), Ld->getType());
   EXPECT_EQ(NewLd->getPointerOperand(), Arg1);
   EXPECT_EQ(NewLd->getAlign(), 8);
   EXPECT_EQ(NewLd->getName(), "NewLd");
+  
+  sandboxir::LoadInst *NewVld =
+      sandboxir::LoadInst::create(Vld->getType(), Arg1, Align(8),
+                                  /*InsertBefore=*/Ret, Ctx, 
+                                  Vld->isVolatile(), "NewVld");
+  EXPECT_EQ(NewVld->getType(), Vld->getType());
+  EXPECT_EQ(NewVld->getPointerOperand(), Arg1);
+  EXPECT_EQ(NewVld->isVolatile(), Vld->isVolatile());
+  EXPECT_EQ(NewVld->getAlign(), 8);
+  EXPECT_EQ(NewVld->getName(), "NewVld");
 }
 
 TEST_F(SandboxIRTest, StoreInst) {

>From 785544161e7c40349a1c910b0159041f8ce1344f Mon Sep 17 00:00:00 2001
From: medievalghoul <61852278+medievalghoul at users.noreply.github.com>
Date: Sat, 27 Jul 2024 18:13:30 -0400
Subject: [PATCH 2/2] removed some unneeded code and clang format

---
 llvm/include/llvm/SandboxIR/SandboxIR.h    |  2 +-
 llvm/lib/SandboxIR/SandboxIR.cpp           |  8 ++++----
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 15 +++++++--------
 3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index d13e547bc97cc..9b102b8348ae5 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -65,8 +65,8 @@
 #define LLVM_SANDBOXIR_SANDBOXIR_H
 
 #include "llvm/IR/Function.h"
-#include "llvm/IR/Instruction.h"
 #include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instruction.h"
 #include "llvm/IR/User.h"
 #include "llvm/IR/Value.h"
 #include "llvm/SandboxIR/Tracker.h"
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 9e41bacf1a8dd..fb780ed87a3c0 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -616,8 +616,8 @@ LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
   llvm::Instruction *BeforeIR = InsertBefore->getTopmostLLVMInstruction();
   auto &Builder = Ctx.getLLVMIRBuilder();
   Builder.SetInsertPoint(BeforeIR);
-  auto *NewLI = Builder.CreateAlignedLoad(Ty, Ptr->Val, Align,
-                                          isVolatile, Name);
+  auto *NewLI =
+      Builder.CreateAlignedLoad(Ty, Ptr->Val, Align, isVolatile, Name);
   auto *NewSBI = Ctx.createLoadInst(NewLI);
   return NewSBI;
 }
@@ -627,8 +627,8 @@ LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
                            bool isVolatile, const Twine &Name) {
   auto &Builder = Ctx.getLLVMIRBuilder();
   Builder.SetInsertPoint(cast<llvm::BasicBlock>(InsertAtEnd->Val));
-  auto *NewLI = Builder.CreateAlignedLoad(Ty, Ptr->Val, Align,
-                                          isVolatile, Name);
+  auto *NewLI =
+      Builder.CreateAlignedLoad(Ty, Ptr->Val, Align, isVolatile, Name);
   auto *NewSBI = Ctx.createLoadInst(NewLI);
   return NewSBI;
 }
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index d1d326dab9cbb..266e5d3302518 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -764,21 +764,20 @@ define void @foo(ptr %arg0, ptr %arg1) {
   // Check create(InsertBefore)
   sandboxir::LoadInst *NewLd =
       sandboxir::LoadInst::create(Ld->getType(), Arg1, Align(8),
-                                  /*InsertBefore=*/Ret, Ctx, 
-                                  Ld->isVolatile(), "NewLd");
+                                  /*InsertBefore=*/Ret, Ctx,
+                                  /*IsVolatile=*/false, "NewLd");
   EXPECT_EQ(NewLd->getType(), Ld->getType());
   EXPECT_EQ(NewLd->getPointerOperand(), Arg1);
   EXPECT_EQ(NewLd->getAlign(), 8);
   EXPECT_EQ(NewLd->getName(), "NewLd");
-  
+
   sandboxir::LoadInst *NewVld =
       sandboxir::LoadInst::create(Vld->getType(), Arg1, Align(8),
-                                  /*InsertBefore=*/Ret, Ctx, 
-                                  Vld->isVolatile(), "NewVld");
-  EXPECT_EQ(NewVld->getType(), Vld->getType());
-  EXPECT_EQ(NewVld->getPointerOperand(), Arg1);
+                                  /*InsertBefore=*/Ret, Ctx,
+                                  /*IsVolatile=*/true, "NewVld");
+
+  // Checking if create() was volatile
   EXPECT_EQ(NewVld->isVolatile(), Vld->isVolatile());
-  EXPECT_EQ(NewVld->getAlign(), 8);
   EXPECT_EQ(NewVld->getName(), "NewVld");
 }
 



More information about the llvm-commits mailing list