[Mlir-commits] [mlir] [mlir][IR] Mark `getParentBlock()` and `getParentRegion()` as const (NFC) (PR #151915)

Longsheng Mou llvmlistbot at llvm.org
Mon Aug 4 01:11:14 PDT 2025


https://github.com/CoTinker created https://github.com/llvm/llvm-project/pull/151915

This PR marks `Value::getParentBlock()` and `Value::getParentRegion()` as `const` member functions. These methods do not modify the internal state of `Value` and are often used in read-only contexts. Marking them as `const` improves API clarity, allows greater flexibility in calling code.

>From f774962fe7775a55e96c2b5ad399f78cd4f3910a Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 4 Aug 2025 16:06:09 +0800
Subject: [PATCH 1/2] [mlir][IR] Mark getParentBlock() and getParentRegion() as
 const

This PR marks `Value::getParentBlock()` and `Value::getParentRegion()` as `const` member functions. These methods do not modify the internal state of `Value` and are often used in read-only contexts. Marking them as `const` improves API clarity, allows greater flexibility in calling code.
---
 mlir/include/mlir/IR/Value.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/include/mlir/IR/Value.h b/mlir/include/mlir/IR/Value.h
index 4d6d89fa69a07..e773d199c3c23 100644
--- a/mlir/include/mlir/IR/Value.h
+++ b/mlir/include/mlir/IR/Value.h
@@ -131,10 +131,10 @@ class Value {
   void setLoc(Location loc);
 
   /// Return the Region in which this Value is defined.
-  Region *getParentRegion();
+  Region *getParentRegion() const;
 
   /// Return the Block in which this Value is defined.
-  Block *getParentBlock();
+  Block *getParentBlock() const;
 
   //===--------------------------------------------------------------------===//
   // UseLists

>From 870104d12221f1b0d4328b602505f7d2da51d1ae Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 4 Aug 2025 16:08:51 +0800
Subject: [PATCH 2/2] Update Value.cpp

---
 mlir/lib/IR/Value.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/IR/Value.cpp b/mlir/lib/IR/Value.cpp
index fa550e4d5d5d0..2cffb962f8543 100644
--- a/mlir/lib/IR/Value.cpp
+++ b/mlir/lib/IR/Value.cpp
@@ -36,14 +36,14 @@ void Value::setLoc(Location loc) {
 }
 
 /// Return the Region in which this Value is defined.
-Region *Value::getParentRegion() {
+Region *Value::getParentRegion() const {
   if (auto *op = getDefiningOp())
     return op->getParentRegion();
   return llvm::cast<BlockArgument>(*this).getOwner()->getParent();
 }
 
 /// Return the Block in which this Value is defined.
-Block *Value::getParentBlock() {
+Block *Value::getParentBlock() const {
   if (Operation *op = getDefiningOp())
     return op->getBlock();
   return llvm::cast<BlockArgument>(*this).getOwner();



More information about the Mlir-commits mailing list