Skip to content

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

Merged
joker-eph merged 3 commits into
llvm:mainfrom
Jacenty-And-Intel:value_local_scope
Mar 12, 2026
Merged

joker-eph merged 3 commits into
llvm:mainfrom
Jacenty-And-Intel:value_local_scope

Conversation

@Jacenty-And-Intel

@Jacenty-And-Intel Jacenty-And-Intel commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

Adjust Value's operator<< and dump() function so that it can be safely used with multithreading enabled.
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Mar 10, 2026
@llvmbot

llvmbot commented Mar 10, 2026 •

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Jacenty Andruszkiewicz (Jacenty-And-Intel)

Changes

Adjust mlir::Value's operator&lt;&lt; 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.


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

2 Files Affected:

  • (modified) mlir/include/mlir/IR/Value.h (+1-4)
  • (modified) mlir/lib/IR/AsmPrinter.cpp (+6-1)
diff --git a/mlir/include/mlir/IR/Value.h b/mlir/include/mlir/IR/Value.h
index af58778a0a13e..e72ace0920eb1 100644
--- a/mlir/include/mlir/IR/Value.h
+++ b/mlir/include/mlir/IR/Value.h
@@ -243,10 +243,7 @@ class Value {
   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
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index b3242f838fc1d..5e9f6e87dfe08 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -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";
 }
 

void Value::dump() const {
print(llvm::errs());
print(llvm::errs(), OpPrintingFlags().useLocalScope());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would think we want dump() to print just the same as the default output.

When is dump() useful in a threaded context?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When is dump() useful in a threaded context?

In manual debug of concurrently ran OperationPasses could be useful.

I've updated the description with more context, please take a look. @joker-eph

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In manual debug of concurrently ran OperationPasses could be useful.

I'm not so sure: the output would be completely mangled from the concurrent writes to stderr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Without any mutex or logger adjusted to concurrent passes sure, but it wouldn't fail the compilation as it could now

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree that crashing is bad.

My concern is that dump() is what we call from the debugger, and it is useful to do op->dump() and then op->getParentOp()->dump() and finding your op in the larger output, which is hard with the local scope...

That said since Operation::dump() has this problem, aligning Value should be fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could you please hit the merge button then? Thanks!

@nikic nikic changed the title Make printing Value to a stream thread-safe [MLIR] Make printing Value to a stream thread-safe Mar 11, 2026
@joker-eph
joker-eph merged commit 0c4eaba into llvm:main Mar 12, 2026
8 checks passed
ggladilo pushed a commit to ggladilo/npu-compiler-llvm that referenced this pull request Apr 21, 2026
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.
ggladilo added a commit to intel-staging/npu-compiler-llvm that referenced this pull request Jul 1, 2026
* [mlir][RFC] Allow to skip operands type specification in ASM form
* Add FlatSymbolRefAttr and ArrayAttr for memory space representation (llvm#44/llvm#45)
* [mlir] Relax ASM parser requirements about operands Type presence
* [mlir] Remove LIT tests affected by ASM simplification
* Add method getInlineBlockAndPoint for DialectInlinerInterface
* Initialize pass options using pipeline options
* Fix compilation issue with MSVC
* Port Ubunu-24 warning fixes to npu/release/18.x (llvm#45)
* Extend Quant dialect with Quantile Quantization type (llvm#53)
* Extending QuantileQuantizedType with quantileType mlir::Type member (llvm#60)
* Relax negative scales conditions in Quant Dialect (llvm#61)
* Adding missing check in QuantileQuantizedPerAxisType::verify (llvm#66)
* Adding new quantileType types to mlir test (llvm#85)
* Add Value back to SideEffectInterfaces
* EISW-130780: remove all wd flag to enable warnings when LLVM_ENABLE_WARNINGS is ON (llvm#99)
* EISW-176770 Fix argument parsing issue in mlir-src-sharder after command-line parser reset
* Enable quantization for FP4 type
* [MLIR] Make printing Value to a stream thread-safe (llvm#185762)
* [MLIR] Fix mlir-doc build failures by adding -dialect to add_mlir_doc calls
* [MLIR] Add getter for the action handler of MLIR context (llvm#197230)
* Silence deprecation warning for OpBuilder::create
* [MLIR] make One-Shot and SCF bufferization TensorLikeType-aware (llvm#189073)
* [NFC][mlir][bufferization] Remove getMemRefType() helper (llvm#199034)
* [mlir][bufferization] Drop TensorLikeType::getBufferType() (llvm#201350)

---------

Signed-off-by: Gladilov, Gleb <[email protected]>
Signed-off-by: Zhu, Shaojie <[email protected]>
Signed-off-by: Dmitrii Makarenko <[email protected]>
Co-authored-by: Vladislav Vinogradov <[email protected]>
Co-authored-by: Nikita Kudriavtsev <[email protected]>
Co-authored-by: Schondube, Jorn <[email protected]>
Co-authored-by: Daria Mityagina <[email protected]>
Co-authored-by: Luca Sarti <[email protected]>
Co-authored-by: Zoran Zomborat <[email protected]>
Co-authored-by: Ngun, Raymond <[email protected]>
Co-authored-by: Shaojie Zhu <[email protected]>
Co-authored-by: Surendar Rama Sitaraman <[email protected]>
Co-authored-by: Andruszkiewicz, Jacenty <[email protected]>
Co-authored-by: Mehdi Amini <[email protected]>
Co-authored-by: Kigyosi Alexandru <[email protected]>
Co-authored-by: Dmitrii Makarenko <[email protected]>
Co-authored-by: Andrei Golubev <[email protected]>
andrey-golubev pushed a commit to intel-staging/npu-compiler-llvm that referenced this pull request Jul 29, 2026
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.
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