Skip to content

Commit dfbbb74

Browse files
authored
[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.
1 parent 103461f commit dfbbb74

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

mlir/include/mlir/IR/Operation.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,26 @@ inline raw_ostream &operator<<(raw_ostream &os,
11251125
return os;
11261126
}
11271127

1128+
/// A wrapper class that allows for printing an operation with a custom
1129+
/// AsmState, useful to act as a "stream modifier" to customize printing an
1130+
/// operation with a stream using the operator<< overload, e.g.:
1131+
/// llvm::dbgs() << OpWithState(op, OpPrintingFlags().skipRegions());
1132+
class OpWithState {
1133+
public:
1134+
OpWithState(Operation *op, AsmState &state) : op(op), theState(state) {}
1135+
1136+
private:
1137+
Operation *op;
1138+
AsmState &theState;
1139+
friend raw_ostream &operator<<(raw_ostream &os, const OpWithState &op);
1140+
};
1141+
1142+
inline raw_ostream &operator<<(raw_ostream &os,
1143+
const OpWithState &opWithState) {
1144+
opWithState.op->print(os, const_cast<OpWithState &>(opWithState).theState);
1145+
return os;
1146+
}
1147+
11281148
} // namespace mlir
11291149

11301150
namespace llvm {

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,7 @@ performActions(raw_ostream &os,
501501
<< "bytecode version while not emitting bytecode";
502502
AsmState asmState(op.get(), OpPrintingFlags(), /*locationMap=*/nullptr,
503503
&fallbackResourceMap);
504-
op.get()->print(os, asmState);
505-
os << '\n';
504+
os << OpWithState(op.get(), asmState) << '\n';
506505
return success();
507506
}
508507

0 commit comments

Comments
 (0)