Skip to content

Conversation

@joker-eph
Copy link
Collaborator

On the model of OpWithFlags, this modifier allows to stream an operation using a custom AsmPrinter.

@joker-eph joker-eph requested review from Copilot and jpienaar July 31, 2025 15:58
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jul 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-mlir-core

Author: Mehdi Amini (joker-eph)

Changes

On the model of OpWithFlags, this modifier allows to stream an operation using a custom AsmPrinter.


Full diff: https://github.com/llvm/llvm-project/pull/151547.diff

2 Files Affected:

  • (modified) mlir/include/mlir/IR/Operation.h (+21)
  • (modified) mlir/lib/Tools/mlir-opt/MlirOptMain.cpp (+1-2)
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index edc8ab489e12d..f4bbc8e6c984a 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -1125,6 +1125,27 @@ inline raw_ostream &operator<<(raw_ostream &os,
   return os;
 }
 
+/// A wrapper class that allows for printing an operation with a custom
+/// AsmState, useful to act as a "stream modifier" to customize printing an
+/// operation with a stream using the operator<< overload, e.g.:
+///   llvm::dbgs() << OpWithState(op, OpPrintingFlags().skipRegions());
+class OpWithState {
+public:
+  OpWithState(Operation *op, AsmState &state) : op(op), theState(state) {}
+  AsmState &state() { return theState; }
+  const AsmState &state() const { return theState; }
+
+private:
+  Operation *op;
+  AsmState &theState;
+  friend raw_ostream &operator<<(raw_ostream &os, OpWithState &op);
+};
+
+inline raw_ostream &operator<<(raw_ostream &os, OpWithState &opWithState) {
+  opWithState.op->print(os, opWithState.state());
+  return os;
+}
+
 } // namespace mlir
 
 namespace llvm {
diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index bdcdaa407e616..de714d8b740af 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -501,8 +501,7 @@ performActions(raw_ostream &os,
            << "bytecode version while not emitting bytecode";
   AsmState asmState(op.get(), OpPrintingFlags(), /*locationMap=*/nullptr,
                     &fallbackResourceMap);
-  op.get()->print(os, asmState);
-  os << '\n';
+  os << OpWithState(op.get(), asmState) << '\n';
   return success();
 }
 

@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-mlir

Author: Mehdi Amini (joker-eph)

Changes

On the model of OpWithFlags, this modifier allows to stream an operation using a custom AsmPrinter.


Full diff: https://github.com/llvm/llvm-project/pull/151547.diff

2 Files Affected:

  • (modified) mlir/include/mlir/IR/Operation.h (+21)
  • (modified) mlir/lib/Tools/mlir-opt/MlirOptMain.cpp (+1-2)
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index edc8ab489e12d..f4bbc8e6c984a 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -1125,6 +1125,27 @@ inline raw_ostream &operator<<(raw_ostream &os,
   return os;
 }
 
+/// A wrapper class that allows for printing an operation with a custom
+/// AsmState, useful to act as a "stream modifier" to customize printing an
+/// operation with a stream using the operator<< overload, e.g.:
+///   llvm::dbgs() << OpWithState(op, OpPrintingFlags().skipRegions());
+class OpWithState {
+public:
+  OpWithState(Operation *op, AsmState &state) : op(op), theState(state) {}
+  AsmState &state() { return theState; }
+  const AsmState &state() const { return theState; }
+
+private:
+  Operation *op;
+  AsmState &theState;
+  friend raw_ostream &operator<<(raw_ostream &os, OpWithState &op);
+};
+
+inline raw_ostream &operator<<(raw_ostream &os, OpWithState &opWithState) {
+  opWithState.op->print(os, opWithState.state());
+  return os;
+}
+
 } // namespace mlir
 
 namespace llvm {
diff --git a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
index bdcdaa407e616..de714d8b740af 100644
--- a/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
+++ b/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
@@ -501,8 +501,7 @@ performActions(raw_ostream &os,
            << "bytecode version while not emitting bytecode";
   AsmState asmState(op.get(), OpPrintingFlags(), /*locationMap=*/nullptr,
                     &fallbackResourceMap);
-  op.get()->print(os, asmState);
-  os << '\n';
+  os << OpWithState(op.get(), asmState) << '\n';
   return success();
 }
 

This comment was marked as outdated.

…Operations (NFC)

On the model of OpWithFlags, this modifier allows to stream an operation
using a custom AsmPrinter.
@joker-eph joker-eph closed this Jul 31, 2025
@joker-eph joker-eph reopened this Jul 31, 2025
@joker-eph joker-eph requested a review from Copilot July 31, 2025 16:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new OpWithState class that serves as a stream modifier for printing MLIR operations with custom AsmState configurations. The class follows the pattern established by OpWithFlags to enable more flexible operation printing.

Key changes:

  • Introduces OpWithState wrapper class in Operation.h for custom AsmState printing
  • Updates MlirOptMain.cpp to use the new streaming interface instead of direct print calls

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
mlir/include/mlir/IR/Operation.h Adds OpWithState class definition with streaming operator overload
mlir/lib/Tools/mlir-opt/MlirOptMain.cpp Replaces direct print call with OpWithState streaming usage
Comments suppressed due to low confidence (1)

mlir/include/mlir/IR/Operation.h:1139

  • The parameter name 'op' in the friend declaration is misleading since it refers to an OpWithState object, not an Operation. Consider renaming to 'opWithState' for clarity.
  friend raw_ostream &operator<<(raw_ostream &os, const OpWithState &op);

@joker-eph joker-eph merged commit dfbbb74 into llvm:main Aug 1, 2025
12 of 15 checks passed
joker-eph added a commit to joker-eph/llvm-project that referenced this pull request Aug 20, 2025
…Operations (NFC) (llvm#151547)

On the model of OpWithFlags, this modifier allows to stream an operation
using a custom AsmPrinter.
tru pushed a commit that referenced this pull request Aug 26, 2025
…Operations (NFC) (#151547)

On the model of OpWithFlags, this modifier allows to stream an operation
using a custom AsmPrinter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants