diff options
| author | Mehdi Amini <joker.eph@gmail.com> | 2025-08-01 13:11:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-01 13:11:19 +0200 |
| commit | dfbbb744384167bc874985c340149e39c107e37f (patch) | |
| tree | 790e3fa8c1ad23433102c0a455f0f0a59637c7e1 | |
| parent | 103461f1190e90b141830bc50734874ba954dfe6 (diff) | |
| download | llvm-dfbbb744384167bc874985c340149e39c107e37f.zip llvm-dfbbb744384167bc874985c340149e39c107e37f.tar.gz llvm-dfbbb744384167bc874985c340149e39c107e37f.tar.bz2 | |
[MLIR] Introduce a OpWithState class to act as a stream modifier for Operations (NFC) (#151547)
On the model of OpWithFlags, this modifier allows to stream an operation
using a custom AsmPrinter.
| -rw-r--r-- | mlir/include/mlir/IR/Operation.h | 20 | ||||
| -rw-r--r-- | mlir/lib/Tools/mlir-opt/MlirOptMain.cpp | 3 |
2 files changed, 21 insertions, 2 deletions
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h index edc8ab4..4f89f8b 100644 --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -1125,6 +1125,26 @@ 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) {} + +private: + Operation *op; + AsmState &theState; + friend raw_ostream &operator<<(raw_ostream &os, const OpWithState &op); +}; + +inline raw_ostream &operator<<(raw_ostream &os, + const OpWithState &opWithState) { + opWithState.op->print(os, const_cast<OpWithState &>(opWithState).theState); + 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 bdcdaa4..de714d8 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(); } |
