[llvm-branch-commits] [mlir] [mlir][NFC] Move OpFoldResult to mlir/IR/OpFoldResult.h (PR #228740)
Victor Perez via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Oct 3 09:45:49 PDT 2026
https://github.com/victor-eds created https://github.com/llvm/llvm-project/pull/228740
`OpFoldResult` is defined in `OpDefinition.h`. A later patch adds a class that holds the fold results of any op, and `OperationSupport.h` must use that class in the signature of the fold hook. `OperationSupport.h` cannot include `OpDefinition.h`.
Move `OpFoldResult` and its `CastInfo` specializations to the new header `mlir/IR/OpFoldResult.h`. `OpDefinition.h` includes the new header, so the users of `OpFoldResult` do not change. Move the definitions of `OpFoldResult::dump` and `operator<<` to the new file `mlir/lib/IR/OpFoldResult.cpp`.
Assisted by: Claude Code.
>From 4a0b2c11393005a8d12d72b44c536eb790fe4eec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADctor=20P=C3=A9rez=20Carrasco?=
<victor.pc.upm at gmail.com>
Date: Fri, 2 Oct 2026 14:12:32 -0700
Subject: [PATCH] [mlir][NFC] Move OpFoldResult to mlir/IR/OpFoldResult.h
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
OpFoldResult is defined in OpDefinition.h. A later patch adds a class
that holds the fold results of any op, and OperationSupport.h must use
that class in the signature of the fold hook. OperationSupport.h
cannot include OpDefinition.h.
Move OpFoldResult and its CastInfo specializations to the new header
`mlir/IR/OpFoldResult.h`. OpDefinition.h includes the new header, so
the users of OpFoldResult do not change. Move the definitions of
`OpFoldResult::dump` and `operator<<` to the new file
`mlir/lib/IR/OpFoldResult.cpp`.
Signed-off-by: Víctor Pérez Carrasco <victor.pc.upm at gmail.com>
---
mlir/include/mlir/IR/OpDefinition.h | 46 +-------------------
mlir/include/mlir/IR/OpFoldResult.h | 65 +++++++++++++++++++++++++++++
mlir/lib/IR/CMakeLists.txt | 1 +
mlir/lib/IR/OpFoldResult.cpp | 26 ++++++++++++
4 files changed, 93 insertions(+), 45 deletions(-)
create mode 100644 mlir/include/mlir/IR/OpFoldResult.h
create mode 100644 mlir/lib/IR/OpFoldResult.cpp
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 0ab6679187c8eb5..cb35ba7c8eaa26f 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -21,6 +21,7 @@
#include "mlir/IR/Dialect.h"
#include "mlir/IR/ODSSupport.h"
+#include "mlir/IR/OpFoldResult.h"
#include "mlir/IR/Operation.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
@@ -267,51 +268,6 @@ inline bool operator!=(OpState lhs, OpState rhs) {
return lhs.getOperation() != rhs.getOperation();
}
-raw_ostream &operator<<(raw_ostream &os, OpFoldResult ofr);
-
-/// This class represents a single result from folding an operation.
-class OpFoldResult : public PointerUnion<Attribute, Value> {
- using PointerUnion<Attribute, Value>::PointerUnion;
-
-public:
- LLVM_DUMP_METHOD void dump() const { llvm::errs() << *this << "\n"; }
-
- MLIRContext *getContext() const {
- PointerUnion pu = *this;
- return isa<Attribute>(pu) ? cast<Attribute>(pu).getContext()
- : cast<Value>(pu).getContext();
- }
-};
-
-// Temporarily exit the MLIR namespace to add casting support as later code in
-// this uses it. The CastInfo must come after the OpFoldResult definition and
-// before any cast function calls depending on CastInfo.
-
-} // namespace mlir
-
-namespace llvm {
-
-// Allow llvm::cast style functions.
-template <typename To>
-struct CastInfo<To, mlir::OpFoldResult>
- : public CastInfo<To, mlir::OpFoldResult::PointerUnion> {};
-
-template <typename To>
-struct CastInfo<To, const mlir::OpFoldResult>
- : public CastInfo<To, const mlir::OpFoldResult::PointerUnion> {};
-
-} // namespace llvm
-
-namespace mlir {
-
-/// Allow printing to a stream.
-inline raw_ostream &operator<<(raw_ostream &os, OpFoldResult ofr) {
- if (Value value = llvm::dyn_cast_if_present<Value>(ofr))
- value.print(os);
- else
- llvm::dyn_cast_if_present<Attribute>(ofr).print(os);
- return os;
-}
/// Allow printing to a stream.
inline raw_ostream &operator<<(raw_ostream &os, OpState op) {
op.print(os, OpPrintingFlags().useLocalScope());
diff --git a/mlir/include/mlir/IR/OpFoldResult.h b/mlir/include/mlir/IR/OpFoldResult.h
new file mode 100644
index 000000000000000..0bda0e75403e253
--- /dev/null
+++ b/mlir/include/mlir/IR/OpFoldResult.h
@@ -0,0 +1,65 @@
+//===- OpFoldResult.h - Results of an operation fold ------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines OpFoldResult, the result of a fold of an operation with
+// exactly one result.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_IR_OPFOLDRESULT_H
+#define MLIR_IR_OPFOLDRESULT_H
+
+#include "mlir/IR/Attributes.h"
+#include "mlir/IR/Value.h"
+#include "mlir/Support/LLVM.h"
+#include "llvm/ADT/PointerUnion.h"
+#include "llvm/Support/Compiler.h"
+
+namespace mlir {
+
+/// This class represents a single result from folding an operation.
+class OpFoldResult : public PointerUnion<Attribute, Value> {
+ using PointerUnion<Attribute, Value>::PointerUnion;
+
+public:
+ LLVM_DUMP_METHOD void dump() const;
+
+ MLIRContext *getContext() const {
+ PointerUnion pu = *this;
+ return isa<Attribute>(pu) ? cast<Attribute>(pu).getContext()
+ : cast<Value>(pu).getContext();
+ }
+};
+
+// Temporarily exit the MLIR namespace to add casting support as later code in
+// this uses it. The CastInfo must come after the OpFoldResult definition and
+// before any cast function calls depending on CastInfo.
+
+} // namespace mlir
+
+namespace llvm {
+
+// Allow llvm::cast style functions.
+template <typename To>
+struct CastInfo<To, mlir::OpFoldResult>
+ : public CastInfo<To, mlir::OpFoldResult::PointerUnion> {};
+
+template <typename To>
+struct CastInfo<To, const mlir::OpFoldResult>
+ : public CastInfo<To, const mlir::OpFoldResult::PointerUnion> {};
+
+} // namespace llvm
+
+namespace mlir {
+
+/// Allow printing to a stream.
+raw_ostream &operator<<(raw_ostream &os, OpFoldResult ofr);
+
+} // namespace mlir
+
+#endif // MLIR_IR_OPFOLDRESULT_H
diff --git a/mlir/lib/IR/CMakeLists.txt b/mlir/lib/IR/CMakeLists.txt
index 01c80dafcd21a78..2578cc086c3c664 100644
--- a/mlir/lib/IR/CMakeLists.txt
+++ b/mlir/lib/IR/CMakeLists.txt
@@ -27,6 +27,7 @@ add_mlir_library(MLIRIR
Location.cpp
MLIRContext.cpp
ODSSupport.cpp
+ OpFoldResult.cpp
Operation.cpp
OperationSupport.cpp
PatternLoggingListener.cpp
diff --git a/mlir/lib/IR/OpFoldResult.cpp b/mlir/lib/IR/OpFoldResult.cpp
new file mode 100644
index 000000000000000..3a448d4b456edf5
--- /dev/null
+++ b/mlir/lib/IR/OpFoldResult.cpp
@@ -0,0 +1,26 @@
+//===- OpFoldResult.cpp - Results of an operation fold --------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/IR/OpFoldResult.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace mlir;
+
+//===----------------------------------------------------------------------===//
+// OpFoldResult
+//===----------------------------------------------------------------------===//
+
+void OpFoldResult::dump() const { llvm::errs() << *this << "\n"; }
+
+raw_ostream &mlir::operator<<(raw_ostream &os, OpFoldResult ofr) {
+ if (Value value = llvm::dyn_cast_if_present<Value>(ofr))
+ value.print(os);
+ else
+ llvm::dyn_cast_if_present<Attribute>(ofr).print(os);
+ return os;
+}
More information about the llvm-branch-commits
mailing list