-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[mlir][IR] Mark getParentBlock()
and getParentRegion()
as const (NFC)
#151915
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
Conversation
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.
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir Author: Longsheng Mou (CoTinker) ChangesThis PR marks Full diff: https://github.com/llvm/llvm-project/pull/151915.diff 2 Files Affected:
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
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();
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure how to interpret this document: https://mlir.llvm.org/docs/Rationale/UsageOfConst/
It suggests that functions like these should not be const
. E.g.: you should never see a const method on Operation
. Presumably that also extends to Value
?
However, we have many const functions in Value.h
today. So for consistency reasons, it makes sense to me to add the const
keyword here.
Thanks for the explanation. I just read the document — it looks like they removed Edit: |
The reason behind this change is that llvm-project/mlir/lib/Dialect/Bufferization/IR/BufferDeallocationOpInterface.cpp Line 222 in 19803d8
|
Yes, the same logic for removing const from Operation applies to Value as well. I'd be +1 on removing the const qualifiers from it. |
I would think that ValueComparator can just const_cast the values in its implementation? |
Okay, thanks, I'll open a PR to remove the const. |
Fine, I'll fix it. |
This PR marks
Value::getParentBlock()
andValue::getParentRegion()
asconst
member functions. These methods do not modify the internal state ofValue
and are often used in read-only contexts. Marking them asconst
improves API clarity, allows greater flexibility in calling code.