[MLIR] Make printing Value to a stream thread-safe (#185762)

Adjust `mlir::Value`'s `operator<<` and `dump()` function so that it can
be safely used with multithreading enabled.

Similar to `mlir::Operation` which uses `OpPrintingFlags` with
`localScope` enabled in printing methods.

Using `Value::print(raw_ostream &os)` creates default `OpPrintingFlags`
and with these flags print method is called on `getDefiningOp()`. With
`localScope` disabled by default, `findParent()` could go all the way up
to the `ModuleOp` which can be outside of the pass scope.

We had an instance of it in our project when multiple OperationPasses
were ran concurrently. Trying to print or dump `mlir::Value` in one of
the passes resulted in crash due to failed verifiers that got triggered
on parent op before the print.
This commit is contained in:
Jacenty Andruszkiewicz 2026-03-12 10:37:56 +01:00 committed by GitHub
parent fd225e296f
commit 0c4eaba2f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 5 deletions

View File

@ -243,10 +243,7 @@ protected:
detail::ValueImpl *impl;
};
inline raw_ostream &operator<<(raw_ostream &os, Value value) {
value.print(os);
return os;
}
raw_ostream &operator<<(raw_ostream &os, Value value);
//===----------------------------------------------------------------------===//
// OpOperand

View File

@ -4142,8 +4142,13 @@ void Value::print(raw_ostream &os, AsmState &state) const {
<< "' at index: " << arg.getArgNumber();
}
raw_ostream &mlir::operator<<(raw_ostream &os, Value value) {
value.print(os, OpPrintingFlags().useLocalScope());
return os;
}
void Value::dump() const {
print(llvm::errs());
print(llvm::errs(), OpPrintingFlags().useLocalScope());
llvm::errs() << "\n";
}