Skip to content

[mlir] Clean up prints with llvm::interleaved. NFC. #136468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2025

Conversation

kuhar
Copy link
Member

@kuhar kuhar commented Apr 20, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Apr 20, 2025

@llvm/pr-subscribers-mlir-pdl
@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Jakub Kuderski (kuhar)

Changes

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

8 Files Affected:

  • (modified) mlir/lib/Analysis/FlatLinearValueConstraints.cpp (+4-3)
  • (modified) mlir/lib/Debug/Observers/ActionLogging.cpp (+4-5)
  • (modified) mlir/lib/Dialect/Linalg/TransformOps/GPUHeuristics.cpp (+18-28)
  • (modified) mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp (+6-8)
  • (modified) mlir/lib/Dialect/Linalg/TransformOps/Syntax.cpp (+2-1)
  • (modified) mlir/lib/IR/ExtensibleDialect.cpp (+2-3)
  • (modified) mlir/lib/IR/PDL/PDLPatternMatch.cpp (+3-2)
  • (modified) mlir/lib/Interfaces/InferTypeOpInterface.cpp (+9-10)
diff --git a/mlir/lib/Analysis/FlatLinearValueConstraints.cpp b/mlir/lib/Analysis/FlatLinearValueConstraints.cpp
index 6ad39a3a91293..6cc5ec639f448 100644
--- a/mlir/lib/Analysis/FlatLinearValueConstraints.cpp
+++ b/mlir/lib/Analysis/FlatLinearValueConstraints.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/InterleavedRange.h"
 #include "llvm/Support/raw_ostream.h"
 #include <optional>
 
@@ -1106,9 +1107,9 @@ IntegerSet FlatLinearConstraints::getAsIntegerSet(MLIRContext *context) const {
     }
     if (!noLocalRepVars.empty()) {
       LLVM_DEBUG({
-        llvm::dbgs() << "local variables at position(s) ";
-        llvm::interleaveComma(noLocalRepVars, llvm::dbgs());
-        llvm::dbgs() << " do not have an explicit representation in:\n";
+        llvm::dbgs() << "local variables at position(s) "
+                     << llvm::interleaved(noLocalRepVars)
+                     << " do not have an explicit representation in:\n";
         this->dump();
       });
       return IntegerSet();
diff --git a/mlir/lib/Debug/Observers/ActionLogging.cpp b/mlir/lib/Debug/Observers/ActionLogging.cpp
index add16e84653e5..9c0d041b0d36f 100644
--- a/mlir/lib/Debug/Observers/ActionLogging.cpp
+++ b/mlir/lib/Debug/Observers/ActionLogging.cpp
@@ -9,6 +9,7 @@
 #include "mlir/Debug/Observers/ActionLogging.h"
 #include "mlir/Debug/BreakpointManager.h"
 #include "mlir/IR/Action.h"
+#include "llvm/Support/InterleavedRange.h"
 #include "llvm/Support/Threading.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -55,11 +56,9 @@ void ActionLogger::beforeExecute(const ActionActiveStack *action,
     action->getAction().print(os);
   else
     os << action->getAction().getTag();
-  if (printIRUnits) {
-    os << " (";
-    interleaveComma(action->getAction().getContextIRUnits(), os);
-    os << ")";
-  }
+  if (printIRUnits)
+    os << " (" << llvm::interleaved(action->getAction().getContextIRUnits())
+       << ")";
   os << "`\n";
 }
 
diff --git a/mlir/lib/Dialect/Linalg/TransformOps/GPUHeuristics.cpp b/mlir/lib/Dialect/Linalg/TransformOps/GPUHeuristics.cpp
index 0657a87d1d1ac..5b569e11388b0 100644
--- a/mlir/lib/Dialect/Linalg/TransformOps/GPUHeuristics.cpp
+++ b/mlir/lib/Dialect/Linalg/TransformOps/GPUHeuristics.cpp
@@ -13,6 +13,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/InterleavedRange.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cmath>
@@ -44,8 +45,8 @@ transform::gpu::CopyMappingInfo::CopyMappingInfo(MLIRContext *ctx,
          "only 1,2,3-D copies are supported for now");
 
   LDBG("START CopyMappingInfo, favorPredication: " << favorPredication);
-  LLVM_DEBUG(llvm::interleaveComma(copySizes, DBGS() << "--copy shape: ");
-             llvm::dbgs() << "\n";);
+  LLVM_DEBUG(DBGS() << "--copy shape: " << llvm::interleaved(copySizes)
+                    << "\n");
 
   // Greedily find the largest vector size that can be used to copy the most
   // minor dimension: we are in the business of filling kMaxVectorLoadBitWidth
@@ -63,11 +64,10 @@ transform::gpu::CopyMappingInfo::CopyMappingInfo(MLIRContext *ctx,
   if (status == Status::Invalid)
     return;
 
-  LLVM_DEBUG(llvm::interleaveComma(copySizes, DBGS() << "--copy: ");
-             llvm::dbgs() << "\n"; llvm::interleaveComma(
-                 this->numThreads, DBGS() << "--numThreads: ");
-             llvm::dbgs() << "\n";);
-  LDBG("--vectorSize: " << this->vectorSize);
+  LLVM_DEBUG(DBGS() << "--copy: " << llvm::interleaved(copySizes) << "\n"
+                    << "--numThreads: " << llvm::interleaved(this->numThreads)
+                    << "\n"
+                    << "--vectorSize: " << this->vectorSize << "\n");
   assert(this->numThreads.size() == copySizes.size() &&
          "compute copy mapping expected same number of threads and copy sizes");
 
@@ -158,10 +158,8 @@ static SmallVector<int64_t> maximizeNumThreads(ArrayRef<int64_t> sizes,
     int64_t localBest = factor * product(nestedThreadsPerDim);
     if (localBest > best && localBest <= maxNumThreads) {
       LDBG(indent << "new localBest: " << localBest);
-      LLVM_DEBUG(
-          llvm::interleaveComma(nestedThreadsPerDim,
-                                DBGS() << indent << "nestedThreadsPerDim: ");
-          llvm::dbgs() << "\n";);
+      LDBG(indent << "nestedThreadsPerDim: "
+                  << llvm::interleaved(nestedThreadsPerDim));
       localThreadsPerDim.clear();
       localThreadsPerDim.push_back(factor);
       llvm::append_range(localThreadsPerDim, nestedThreadsPerDim);
@@ -170,10 +168,7 @@ static SmallVector<int64_t> maximizeNumThreads(ArrayRef<int64_t> sizes,
   }
 
   LDBG(indent << "found globalBest: " << best);
-  LLVM_DEBUG(llvm::interleaveComma(localThreadsPerDim,
-                                   DBGS() << indent << "numThreads: ");
-             llvm::dbgs() << "\n";);
-
+  LDBG(indent << "numThreads: " << llvm::interleaved(localThreadsPerDim));
   return localThreadsPerDim;
 }
 
@@ -231,10 +226,8 @@ transform::gpu::CopyMappingInfo::inferNumThreadsImpl(
   SmallVector<int64_t> inferredNumThreads =
       maximizeNumThreads(scaledSizes, 0, totalNumThreads);
 
-  LLVM_DEBUG(llvm::interleaveComma(inferredNumThreads,
-                                   DBGS() << "inferred numThreads: ");
-             llvm::dbgs() << "\n";
-             LDBG("computed actualVectorSize: " << desiredVectorSize););
+  LDBG("inferred numThreads: " << llvm::interleaved(inferredNumThreads));
+  LDBG("computed actualVectorSize: " << desiredVectorSize);
 
   // Corner case: we cannot use more threads than available. If the dimension of
   // the copy is so bad it is because higher-level tiling did not do its job, we
@@ -255,13 +248,10 @@ transform::gpu::CopyMappingInfo::inferNumThreadsImpl(
 }
 
 void transform::gpu::CopyMappingInfo::print(llvm::raw_ostream &os) const {
-  os << "MappingInfo{";
-  os << "CopyMappingInfo: ";
-  os << "valid: " << (status != Status::Invalid) << ", ";
-  os << "vectorSize: " << vectorSize << ", ";
-  llvm::interleaveComma(numThreads, os << ", numThreads: {");
-  llvm::interleaveComma(smallestBoundingTileSizes,
-                        os << "}, smallestBoundingTileSizes: {");
-  llvm::interleaveComma(threadMapping, os << "}, threadMapping: {");
-  os << "}}";
+  os << "MappingInfo{"
+     << "CopyMappingInfo: " << "valid: " << (status != Status::Invalid) << ", "
+     << "vectorSize: " << vectorSize << ", numThreads: {"
+     << llvm::interleaved(numThreads) << "}, smallestBoundingTileSizes: {"
+     << llvm::interleaved(smallestBoundingTileSizes) << "}, threadMapping: {"
+     << llvm::interleaved(threadMapping) << "}}";
 }
diff --git a/mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp b/mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp
index 5460f50328a45..67bb1de59dd43 100644
--- a/mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp
+++ b/mlir/lib/Dialect/Linalg/TransformOps/LinalgMatchOps.cpp
@@ -18,6 +18,7 @@
 #include "mlir/Interfaces/FunctionImplementation.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/InterleavedRange.h"
 
 using namespace mlir;
 
@@ -219,14 +220,11 @@ LogicalResult transform::MatchStructuredBodyOp::verify() {
                        getElementwise() + getContraction().has_value();
 
   if (numOptions > 1) {
-    std::string attributeNames;
-    llvm::raw_string_ostream os(attributeNames);
-    llvm::interleaveComma(ArrayRef<StringAttr>{getReductionPositionAttrName(),
-                                               getPassthroughAttrName(),
-                                               getElementwiseAttrName(),
-                                               getContractionAttrName()},
-                          os);
-    return emitOpError() << "only one of {" << attributeNames << "} is allowed";
+    StringAttr attributeNames[] = {
+        getReductionPositionAttrName(), getPassthroughAttrName(),
+        getElementwiseAttrName(), getContractionAttrName()};
+    return emitOpError() << "only one of {" << llvm::interleaved(attributeNames)
+                         << "} is allowed";
   }
 
   if (std::optional<ArrayAttr> contractionAttr = getContraction()) {
diff --git a/mlir/lib/Dialect/Linalg/TransformOps/Syntax.cpp b/mlir/lib/Dialect/Linalg/TransformOps/Syntax.cpp
index 266c9ad3314a3..e968c73d8e075 100644
--- a/mlir/lib/Dialect/Linalg/TransformOps/Syntax.cpp
+++ b/mlir/lib/Dialect/Linalg/TransformOps/Syntax.cpp
@@ -8,6 +8,7 @@
 
 #include "mlir/Dialect/Linalg/TransformOps/Syntax.h"
 #include "mlir/IR/OpImplementation.h"
+#include "llvm/Support/InterleavedRange.h"
 
 using namespace mlir;
 
@@ -67,7 +68,7 @@ void mlir::printSemiFunctionType(OpAsmPrinter &printer, Operation *op,
 
   if (resultType.size() > 1)
     printer << "(";
-  llvm::interleaveComma(resultType, printer.getStream());
+  printer << llvm::interleaved(resultType);
   if (resultType.size() > 1)
     printer << ")";
 }
diff --git a/mlir/lib/IR/ExtensibleDialect.cpp b/mlir/lib/IR/ExtensibleDialect.cpp
index 8a7d74700006c..3312549ddfcd4 100644
--- a/mlir/lib/IR/ExtensibleDialect.cpp
+++ b/mlir/lib/IR/ExtensibleDialect.cpp
@@ -11,6 +11,7 @@
 #include "mlir/IR/DialectImplementation.h"
 #include "mlir/IR/OperationSupport.h"
 #include "mlir/IR/StorageUniquerSupport.h"
+#include "llvm/Support/InterleavedRange.h"
 
 using namespace mlir;
 
@@ -47,9 +48,7 @@ static void typeOrAttrPrinter(AsmPrinter &printer, ArrayRef<Attribute> params) {
   if (params.empty())
     return;
 
-  printer << "<";
-  interleaveComma(params, printer.getStream());
-  printer << ">";
+  printer << "<" << llvm::interleaved(params) << ">";
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/IR/PDL/PDLPatternMatch.cpp b/mlir/lib/IR/PDL/PDLPatternMatch.cpp
index 0d05153bf7dab..28b39dd75e26c 100644
--- a/mlir/lib/IR/PDL/PDLPatternMatch.cpp
+++ b/mlir/lib/IR/PDL/PDLPatternMatch.cpp
@@ -11,6 +11,7 @@
 #include "mlir/IR/Iterators.h"
 #include "mlir/IR/PatternMatch.h"
 #include "mlir/IR/RegionKindInterface.h"
+#include "llvm/Support/InterleavedRange.h"
 
 using namespace mlir;
 
@@ -34,13 +35,13 @@ void PDLValue::print(raw_ostream &os) const {
     os << cast<Type>();
     break;
   case Kind::TypeRange:
-    llvm::interleaveComma(cast<TypeRange>(), os);
+    os << llvm::interleaved(cast<TypeRange>());
     break;
   case Kind::Value:
     os << cast<Value>();
     break;
   case Kind::ValueRange:
-    llvm::interleaveComma(cast<ValueRange>(), os);
+    os << llvm::interleaved(cast<ValueRange>());
     break;
   }
 }
diff --git a/mlir/lib/Interfaces/InferTypeOpInterface.cpp b/mlir/lib/Interfaces/InferTypeOpInterface.cpp
index 6b5e103cd36c2..9f4f672fb9f4d 100644
--- a/mlir/lib/Interfaces/InferTypeOpInterface.cpp
+++ b/mlir/lib/Interfaces/InferTypeOpInterface.cpp
@@ -15,6 +15,7 @@
 #include "mlir/IR/BuiltinTypes.h"
 #include "mlir/IR/Matchers.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/InterleavedRange.h"
 
 using namespace mlir;
 
@@ -176,9 +177,8 @@ void ShapeAdaptor::dump() const {
       return "?";
     return llvm::formatv("{0}", dim).str();
   });
-  llvm::errs() << "rank = " << getRank() << " dims = [";
-  llvm::interleave(mapped, llvm::errs(), "x");
-  llvm::errs() << "]\n";
+  llvm::errs() << "rank = " << getRank()
+               << " dims = " << llvm::interleaved_array(mapped, "x") << "\n";
 }
 
 ShapeAdaptor ValueShapeRange::getValueAsShape(int index) {
@@ -243,13 +243,12 @@ LogicalResult mlir::detail::verifyInferredResultTypes(Operation *op) {
 void mlir::detail::reportFatalInferReturnTypesError(OperationState &state) {
   std::string buffer;
   llvm::raw_string_ostream os(buffer);
-  os << "Failed to infer result type(s):\n";
-  os << "\"" << state.name << "\"(...) ";
-  os << state.attributes.getDictionary(state.location.getContext());
-  os << " : (";
-  llvm::interleaveComma(state.operands, os,
-                        [&](Value val) { os << val.getType(); });
-  os << ") -> ( ??? )";
+  os << "Failed to infer result type(s):\n"
+     << "\"" << state.name << "\"(...) "
+     << state.attributes.getDictionary(state.location.getContext()) << " : ("
+     << llvm::interleaved(llvm::map_range(
+            state.operands, [](Value val) { return val.getType(); }))
+     << ") -> ( ??? )";
   emitRemark(state.location, "location of op");
   llvm::report_fatal_error(llvm::StringRef(buffer));
 }

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@kuhar kuhar merged commit eb78b9b into llvm:main Apr 20, 2025
16 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants