[llvm] [SanbdoxIR] Implement BBIterator::getNodeParent() (PR #109039)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 17 13:21:26 PDT 2024


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/109039

This patch implements sandboxir::BasicBlock::iterator::getNodeParent() which returns the parent basic block of an iterator.

>From 8534324616922cbd79ba5ce5ed59c4ee527ee508 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Tue, 17 Sep 2024 13:18:33 -0700
Subject: [PATCH] [SanbdoxIR] Implement BBIterator::getNodeParent()

This patch implements sandboxir::BasicBlock::iterator::getNodeParent()
which returns the parent basic block of an iterator.
---
 llvm/include/llvm/SandboxIR/SandboxIR.h    | 2 ++
 llvm/lib/SandboxIR/SandboxIR.cpp           | 5 +++++
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 4 ++++
 3 files changed, 11 insertions(+)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 22b46bd8d7da14..6a31a4b23234c7 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -1659,6 +1659,8 @@ class BBIterator {
   /// \Returns the SBInstruction that corresponds to this iterator, or null if
   /// the instruction is not found in the IR-to-SandboxIR tables.
   pointer get() const { return getInstr(It); }
+  /// \Returns the parent BB.
+  BasicBlock *getNodeParent() const;
 };
 
 /// Contains a list of sandboxir::Instruction's.
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index c26ba1983db91e..20f46046cd2ad0 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -306,6 +306,11 @@ BBIterator &BBIterator::operator--() {
   return *this;
 }
 
+BasicBlock *BBIterator::getNodeParent() const {
+  llvm::BasicBlock *Parent = const_cast<BBIterator *>(this)->It.getNodeParent();
+  return cast<BasicBlock>(Ctx->getValue(Parent));
+}
+
 const char *Instruction::getOpcodeName(Opcode Opc) {
   switch (Opc) {
 #define OP(OPC)                                                                \
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 5ded063ef6f735..1b90c9ad5850b6 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -1509,12 +1509,16 @@ define void @foo(i32 %v1) {
   for (sandboxir::Instruction &I : BB0) {
     EXPECT_EQ(&I, Ctx.getValue(LLVMI));
     LLVMI = LLVMI->getNextNode();
+    // Check getNodeParent().
+    EXPECT_EQ(I.getIterator().getNodeParent(), &BB0);
   }
   LLVMI = &*LLVMBB1->begin();
   for (sandboxir::Instruction &I : BB1) {
     EXPECT_EQ(&I, Ctx.getValue(LLVMI));
     LLVMI = LLVMI->getNextNode();
   }
+  // Check NodeParent() for BB::end().
+  EXPECT_EQ(BB0.end().getNodeParent(), &BB0);
 
   // Check BB.getTerminator()
   EXPECT_EQ(BB0.getTerminator(), Ctx.getValue(LLVMBB0->getTerminator()));



More information about the llvm-commits mailing list