[Mlir-commits] [mlir] 8e4887a - [mlir] use a thread-local alternative to llvm::nulls

Alex Zinenko llvmlistbot at llvm.org
Mon Jul 31 01:21:29 PDT 2023


Author: Alex Zinenko
Date: 2023-07-31T08:21:21Z
New Revision: 8e4887a12e863015c5492b5d5a703f7dfa37e784

URL: https://github.com/llvm/llvm-project/commit/8e4887a12e863015c5492b5d5a703f7dfa37e784
DIFF: https://github.com/llvm/llvm-project/commit/8e4887a12e863015c5492b5d5a703f7dfa37e784.diff

LOG: [mlir] use a thread-local alternative to llvm::nulls

LLVM is not set up in a thread-safe way, which seems to be leading to
race conditions when sending stuff to llvm::nulls in opt builds. Try a
thread-local alternative.

Reviewed By: pzread

Differential Revision: https://reviews.llvm.org/D156421

Added: 
    mlir/include/mlir/Support/RawOstreamExtras.h
    mlir/lib/Support/RawOstreamExtras.cpp

Modified: 
    mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h
    mlir/lib/Support/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h
index a330d9cf9fc60d..b321a376309976 100644
--- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h
+++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h
@@ -22,6 +22,7 @@
 #include "mlir/Interfaces/DestinationStyleOpInterface.h"
 #include "mlir/Interfaces/InferTypeOpInterface.h"
 #include "mlir/Interfaces/ViewLikeInterface.h"
+#include "mlir/Support/RawOstreamExtras.h"
 
 namespace mlir {
 namespace linalg {
@@ -90,7 +91,7 @@ namespace detail {
 /// the failed precondition is send to the `errs` stream, if provided.
 bool isContractionBody(Block &block,
                        function_ref<bool(Operation *, Operation *)> isaPair,
-                       llvm::raw_ostream &errs = llvm::nulls());
+                       llvm::raw_ostream &errs = mlir::thread_safe_nulls());
 
 /// Result of matching a Linalg generic against the predicates of it being a
 /// contraction.

diff  --git a/mlir/include/mlir/Support/RawOstreamExtras.h b/mlir/include/mlir/Support/RawOstreamExtras.h
new file mode 100644
index 00000000000000..a3d8d25dac6085
--- /dev/null
+++ b/mlir/include/mlir/Support/RawOstreamExtras.h
@@ -0,0 +1,17 @@
+//===- RawOstreamExtras.h - Extensions to LLVM's raw_ostream.h --*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+namespace llvm {
+class raw_ostream;
+} // namespace llvm
+
+namespace mlir {
+/// Returns a raw output stream that simply discards the output, but in a
+/// thread-safe manner. Similar to llvm::nulls.
+llvm::raw_ostream &thread_safe_nulls();
+} // namespace mlir

diff  --git a/mlir/lib/Support/CMakeLists.txt b/mlir/lib/Support/CMakeLists.txt
index 5288441ca13ba8..488decd52ae647 100644
--- a/mlir/lib/Support/CMakeLists.txt
+++ b/mlir/lib/Support/CMakeLists.txt
@@ -10,6 +10,7 @@ set(LLVM_OPTIONAL_SOURCES
 add_mlir_library(MLIRSupport
   FileUtilities.cpp
   InterfaceSupport.cpp
+  RawOstreamExtras.cpp
   StorageUniquer.cpp
   Timing.cpp
   ToolUtilities.cpp

diff  --git a/mlir/lib/Support/RawOstreamExtras.cpp b/mlir/lib/Support/RawOstreamExtras.cpp
new file mode 100644
index 00000000000000..80d1595d7f55ce
--- /dev/null
+++ b/mlir/lib/Support/RawOstreamExtras.cpp
@@ -0,0 +1,15 @@
+//===- RawOstreamExtras.cpp - Extensions to LLVM's raw_ostream ------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Support/RawOstreamExtras.h"
+#include "llvm/Support/raw_ostream.h"
+
+llvm::raw_ostream &mlir::thread_safe_nulls() {
+  static thread_local llvm::raw_null_ostream stream;
+  return stream;
+}


        


More information about the Mlir-commits mailing list