[Mlir-commits] [mlir] [mlir][vector] Fix round-trip parsing of operand-less vector.print (PR #231851)
purnima shrivastava
llvmlistbot at llvm.org
Wed Oct 14 03:28:53 PDT 2026
https://github.com/purnima-nlp created https://github.com/llvm/llvm-project/pull/231851
The declarative assembly format for `vector.print` allows an optional source operand while omitting the default newline punctuation. As a result, an operand-less `vector.print` followed by an SSA definition can produce IR that fails to parse, because the parser interprets the following SSA result as the optional source operand.
This patch introduces a focused custom assembly directive that explicitly prints `punctuation <newline>` for operand-less newline prints while preserving the existing syntax for source operands, string literals, and nondefault punctuation.
Changes
- Replace the declarative `vector.print` body with a custom parsing/printing directive.
- Preserve existing assembly syntax and accept legacy bare `vector.print` forms.
- Add round-trip regression tests, including a newline-only print followed by an SSA definition.
- Update VectorToSCF FileCheck expectations to reflect the unambiguous printed syntax.
Testing
- Passed the `ops.mlir` two-pass round-trip test.
- Passed the three VectorToSCF FileCheck pipelines.
- Passed `invalid.mlir` diagnostic verification.
- Passed `git diff --check`.
The relevant test pipelines were executed directly using a rebuilt `mlir-opt`. The lit harness could not run because the build lacked the `count` utility.
Fixes #231427.
>From 49e221bc15e97810bec1aa9a7c0fd2a57873cd7f Mon Sep 17 00:00:00 2001
From: Purnima Shrivastava <purnimashrivastava05 at .com>
Date: Wed, 14 Oct 2026 15:54:30 +0530
Subject: [PATCH] [mlir][vector] Fix round-trip parsing of operand-less
vector.print
---
.../mlir/Dialect/Vector/IR/VectorOps.td | 5 +-
mlir/lib/Dialect/Vector/IR/VectorOps.cpp | 54 +++++++++++++++++++
.../Conversion/VectorToSCF/vector-to-scf.mlir | 6 +--
mlir/test/Dialect/Vector/ops.mlir | 24 +++++++++
4 files changed, 82 insertions(+), 7 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
index 83fe3165b88540..517f8541cb4818 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
@@ -2977,10 +2977,7 @@ def Vector_PrintOp :
];
let assemblyFormat = [{
- ($source^ `:` type($source))?
- oilist(
- `str` $stringLiteral
- | `punctuation` $punctuation)
+ custom<PrintBody>($source, type($source), $stringLiteral, $punctuation)
attr-dict
}];
}
diff --git a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
index 72bab1a37c5008..73193c0fdcbcef 100644
--- a/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/IR/VectorOps.cpp
@@ -8684,6 +8684,60 @@ std::optional<SmallVector<int64_t, 4>> DeinterleaveOp::getShapeForUnroll() {
return llvm::to_vector<4>(getResultVectorType().getShape());
}
+//===----------------------------------------------------------------------===//
+// PrintOp
+//===----------------------------------------------------------------------===//
+
+static ParseResult
+parsePrintBody(OpAsmParser &parser,
+ std::optional<OpAsmParser::UnresolvedOperand> &source,
+ Type &sourceType, StringAttr &stringLiteral,
+ PrintPunctuationAttr &punctuation) {
+ OpAsmParser::UnresolvedOperand operand;
+ if (auto parseResult = parser.parseOptionalOperand(operand);
+ parseResult.has_value()) {
+ if (failed(*parseResult) || parser.parseColonType(sourceType))
+ return failure();
+ source = operand;
+ }
+ while (true) {
+ if (succeeded(parser.parseOptionalKeyword("str"))) {
+ if (stringLiteral)
+ return parser.emitError(parser.getNameLoc(), "duplicate `str` clause");
+ if (parser.parseCustomAttributeWithFallback(stringLiteral))
+ return failure();
+ } else if (succeeded(parser.parseOptionalKeyword("punctuation"))) {
+ if (punctuation)
+ return parser.emitError(parser.getNameLoc(),
+ "duplicate `punctuation` clause");
+ if (parser.parseCustomAttributeWithFallback(punctuation))
+ return failure();
+ } else {
+ return success();
+ }
+ }
+}
+
+static void printPrintBody(OpAsmPrinter &printer, Operation *, Value source,
+ Type sourceType, StringAttr stringLiteral,
+ PrintPunctuationAttr punctuation) {
+ if (source)
+ printer << source << " : " << sourceType;
+ if (stringLiteral) {
+ if (source)
+ printer << ' ';
+ printer << "str ";
+ printer.printStrippedAttrOrType(stringLiteral);
+ }
+ if (punctuation && (punctuation.getValue() != PrintPunctuation::NewLine ||
+ (!source && !stringLiteral))) {
+ if (source || stringLiteral)
+ printer << ' ';
+ printer << "punctuation ";
+ printer.printStrippedAttrOrType(punctuation);
+ }
+}
+
//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Conversion/VectorToSCF/vector-to-scf.mlir b/mlir/test/Conversion/VectorToSCF/vector-to-scf.mlir
index d715ef0fef263b..65ee2dc0e557a3 100644
--- a/mlir/test/Conversion/VectorToSCF/vector-to-scf.mlir
+++ b/mlir/test/Conversion/VectorToSCF/vector-to-scf.mlir
@@ -628,7 +628,7 @@ func.func @vector_print_vector_0d(%arg0: vector<f32>) {
// CHECK: }
// CHECK: }
// CHECK: vector.print punctuation <close>
-// CHECK: vector.print
+// CHECK: vector.print punctuation <newline>
// CHECK: return
// CHECK: }
@@ -664,7 +664,7 @@ func.func @vector_print_vector(%arg0: vector<2x2xf32>) {
// CHECK: }
// CHECK: }
// CHECK: vector.print punctuation <close>
-// CHECK: vector.print
+// CHECK: vector.print punctuation <newline>
// CHECK: return
// CHECK: }
@@ -692,7 +692,7 @@ func.func @vector_print_scalable_vector(%arg0: vector<[4]xi32>) {
// CHECK: }
// CHECK: }
// CHECK: vector.print punctuation <close>
-// CHECK: vector.print
+// CHECK: vector.print punctuation <newline>
// CHECK: return
// CHECK: }
diff --git a/mlir/test/Dialect/Vector/ops.mlir b/mlir/test/Dialect/Vector/ops.mlir
index bd123bd0a6bd83..ed69a3ca98bc32 100644
--- a/mlir/test/Dialect/Vector/ops.mlir
+++ b/mlir/test/Dialect/Vector/ops.mlir
@@ -513,6 +513,30 @@ func.func @vector_print_on_scalar(%arg0: i64) {
return
}
+// CHECK-LABEL: @vector_print_clauses
+func.func @vector_print_clauses() {
+ // CHECK: vector.print punctuation <newline>
+ // CHECK-NEXT: %[[VALUE:.*]] = arith.constant 1 : i64
+ vector.print punctuation <newline>
+ %value = arith.constant 1 : i64
+ // CHECK-NEXT: vector.print %[[VALUE]] : i64
+ vector.print %value : i64
+ // CHECK-NEXT: vector.print %[[VALUE]] : i64 punctuation <comma>
+ vector.print %value : i64 punctuation <comma>
+ // CHECK-NEXT: vector.print str "value"
+ vector.print str "value"
+ // CHECK-NEXT: vector.print punctuation <comma>
+ vector.print punctuation <comma>
+ return
+}
+
+// CHECK-LABEL: @vector_print_bare
+func.func @vector_print_bare() {
+ // CHECK: vector.print punctuation <newline>
+ vector.print
+ return
+}
+
// CHECK-LABEL: @shape_cast
func.func @shape_cast(%arg0 : vector<5x1x3x2xf32>,
%arg1 : vector<8x1xf32>,
More information about the Mlir-commits
mailing list