Skip to content

[mlir][drr] Fix variadic destination emission #95855

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
Jun 17, 2024
Merged

Conversation

jpienaar
Copy link
Member

Its possible for handleResultPattern to emit helpers, these helpers
cannot be interleaved with pushing into the array. Emit into a separate
string to enable helpers to be emitted before the population of vector.

Signed-off-by: Jacques Pienaar jpienaar@google.com

Its possible for handleResultPattern to emit helpers, these helpers
cannot be interleaved with pushing into the array. Emit into a separate
string to enable helpers to be emitted before the population of vector.

Signed-off-by: Jacques Pienaar <jpienaar@google.com>
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jun 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 17, 2024

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Jacques Pienaar (jpienaar)

Changes

Its possible for handleResultPattern to emit helpers, these helpers
cannot be interleaved with pushing into the array. Emit into a separate
string to enable helpers to be emitted before the population of vector.

Signed-off-by: Jacques Pienaar <jpienaar@google.com>


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

2 Files Affected:

  • (modified) mlir/test/mlir-tblgen/rewriter-static-matcher.td (+15)
  • (modified) mlir/tools/mlir-tblgen/RewriterGen.cpp (+9-6)
diff --git a/mlir/test/mlir-tblgen/rewriter-static-matcher.td b/mlir/test/mlir-tblgen/rewriter-static-matcher.td
index 2907923cb28e4..7a84dfd5706b1 100644
--- a/mlir/test/mlir-tblgen/rewriter-static-matcher.td
+++ b/mlir/test/mlir-tblgen/rewriter-static-matcher.td
@@ -35,6 +35,16 @@ def COp : NS_Op<"c_op", []> {
   let results = (outs AnyInteger);
 }
 
+def DOp : NS_Op<"d_op", []> {
+  let arguments = (ins
+    Variadic<AnyInteger>:$any_integer
+  );
+
+  let results = (outs AnyInteger);
+}
+
+def Foo : NativeCodeCall<"foo($_builder, $0)">;
+
 // Test static matcher for duplicate DagNode
 // ---
 
@@ -53,3 +63,8 @@ def : Pat<(AOp (BOp I32Attr:$attr, I32:$int)),
 // CHECK: if(::mlir::failed([[$DAG_MATCHER]](rewriter, op1, tblgen_ops
 def : Pat<(COp $_, (BOp I32Attr:$attr, I32:$int)),
           (COp $attr, $int)>;
+
+// CHECK: auto [[$VAR:.*]] = foo(
+// CHECK: ::llvm::SmallVector<::mlir::Value, 4> [[$ARR:tblgen_variadic_values_.*]];
+// CHECK: [[$ARR]].push_back([[$VAR]]);
+def : Pat<(AOp $x), (DOp (variadic (Foo $x)))>;
diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp
index d8e16d98fd756..b8a3d5b49938e 100644
--- a/mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -1261,20 +1261,23 @@ std::string PatternEmitter::handleResultPattern(DagNode resultTree,
 std::string PatternEmitter::handleVariadic(DagNode tree, int depth) {
   assert(tree.isVariadic());
 
+  std::string output;
+  llvm::raw_string_ostream oss(output);
   auto name = std::string(formatv("tblgen_variadic_values_{0}", nextValueId++));
   symbolInfoMap.bindValue(name);
-  os << "::llvm::SmallVector<::mlir::Value, 4> " << name << ";\n";
+  oss << "::llvm::SmallVector<::mlir::Value, 4> " << name << ";\n";
   for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
     if (auto child = tree.getArgAsNestedDag(i)) {
-      os << name << ".push_back(" << handleResultPattern(child, i, depth + 1)
-         << ");\n";
+      oss << name << ".push_back(" << handleResultPattern(child, i, depth + 1)
+          << ");\n";
     } else {
-      os << name << ".push_back("
-         << handleOpArgument(tree.getArgAsLeaf(i), tree.getArgName(i))
-         << ");\n";
+      oss << name << ".push_back("
+          << handleOpArgument(tree.getArgAsLeaf(i), tree.getArgName(i))
+          << ");\n";
     }
   }
 
+  os << oss.str();
   return name;
 }
 

@jpienaar jpienaar merged commit ca1a963 into llvm:main Jun 17, 2024
7 of 9 checks passed
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.

2 participants