diff --git a/mlir/include/mlir/TableGen/Format.h b/mlir/include/mlir/TableGen/Format.h
--- a/mlir/include/mlir/TableGen/Format.h
+++ b/mlir/include/mlir/TableGen/Format.h
@@ -121,7 +121,7 @@
// std::vector.
struct CreateAdapters {
template
- std::vector operator()(Ts &... items) {
+ std::vector operator()(Ts &...items) {
return std::vector{&items...};
}
};
@@ -151,19 +151,24 @@
return s.str();
}
- template SmallString sstr() const {
+ template
+ SmallString sstr() const {
SmallString result;
llvm::raw_svector_ostream s(result);
format(s);
return result;
}
- template operator SmallString() const { return sstr(); }
+ template
+ operator SmallString() const {
+ return sstr();
+ }
operator std::string() const { return str(); }
};
-template class FmtObject : public FmtObjectBase {
+template
+class FmtObject : public FmtObjectBase {
// Storage for the parameter adapters. Since the base class erases the type
// of the parameters, we have to own the storage for the parameters here, and
// have the base class store type-erased pointers into this tuple.
@@ -186,6 +191,20 @@
}
};
+class FmtStrVecObject : public FmtObjectBase {
+public:
+ using StrFormatAdapter =
+ decltype(llvm::detail::build_format_adapter(std::declval()));
+
+ FmtStrVecObject(StringRef fmt, const FmtContext *ctx,
+ ArrayRef params);
+ FmtStrVecObject(FmtStrVecObject const &that) = delete;
+ FmtStrVecObject(FmtStrVecObject &&that);
+
+private:
+ SmallVector parameters;
+};
+
/// Formats text by substituting placeholders in format string with replacement
/// parameters.
///
@@ -223,7 +242,7 @@
/// 2. This utility does not support format layout because it is rarely needed
/// in C++ code generation.
template
-inline auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&... vals)
+inline auto tgfmt(StringRef fmt, const FmtContext *ctx, Ts &&...vals)
-> FmtObject(vals))...))> {
using ParamTuple = decltype(std::make_tuple(
@@ -234,6 +253,11 @@
llvm::detail::build_format_adapter(std::forward(vals))...));
}
+inline FmtStrVecObject tgfmt(StringRef fmt, const FmtContext *ctx,
+ ArrayRef params) {
+ return FmtStrVecObject(fmt, ctx, params);
+}
+
} // end namespace tblgen
} // end namespace mlir
diff --git a/mlir/lib/TableGen/Format.cpp b/mlir/lib/TableGen/Format.cpp
--- a/mlir/lib/TableGen/Format.cpp
+++ b/mlir/lib/TableGen/Format.cpp
@@ -173,3 +173,22 @@
adapters[repl.index]->format(s, /*Options=*/"");
}
}
+
+FmtStrVecObject::FmtStrVecObject(StringRef fmt, const FmtContext *ctx,
+ ArrayRef params)
+ : FmtObjectBase(fmt, ctx, params.size()) {
+ parameters.reserve(params.size());
+ for (std::string p : params)
+ parameters.push_back(llvm::detail::build_format_adapter(std::move(p)));
+
+ adapters.reserve(parameters.size());
+ for (auto &p : parameters)
+ adapters.push_back(&p);
+}
+
+FmtStrVecObject::FmtStrVecObject(FmtStrVecObject &&that)
+ : FmtObjectBase(std::move(that)), parameters(std::move(that.parameters)) {
+ adapters.reserve(parameters.size());
+ for (auto &p : parameters)
+ adapters.push_back(&p);
+}
diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/tools/mlir-tblgen/RewriterGen.cpp
--- a/mlir/tools/mlir-tblgen/RewriterGen.cpp
+++ b/mlir/tools/mlir-tblgen/RewriterGen.cpp
@@ -249,14 +249,8 @@
LLVM_DEBUG(tree.print(llvm::dbgs()));
LLVM_DEBUG(llvm::dbgs() << '\n');
- // TODO(suderman): iterate through arguments, determine their types, output
- // names.
- SmallVector capture(8);
- if (tree.getNumArgs() > 8) {
- PrintFatalError(loc,
- "unsupported NativeCodeCall matcher argument numbers: " +
- Twine(tree.getNumArgs()));
- }
+ SmallVector capture;
+ capture.push_back(opName.str());
raw_indented_ostream::DelimitedScope scope(os);
@@ -274,7 +268,7 @@
}
}
- capture[i] = std::move(argName);
+ capture.push_back(std::move(argName));
}
bool hasLocationDirective;
@@ -282,21 +276,20 @@
std::tie(hasLocationDirective, locToUse) = getLocation(tree);
auto fmt = tree.getNativeCodeTemplate();
- auto nativeCodeCall = std::string(tgfmt(
- fmt, &fmtCtx.addSubst("_loc", locToUse), opName, capture[0], capture[1],
- capture[2], capture[3], capture[4], capture[5], capture[6], capture[7]));
+ auto nativeCodeCall =
+ std::string(tgfmt(fmt, &fmtCtx.addSubst("_loc", locToUse), capture));
os << "if (failed(" << nativeCodeCall << ")) return ::mlir::failure();\n";
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
auto name = tree.getArgName(i);
if (!name.empty() && name != "_") {
- os << formatv("{0} = {1};\n", name, capture[i]);
+ os << formatv("{0} = {1};\n", name, capture[i + 1]);
}
}
for (int i = 0, e = tree.getNumArgs(); i != e; ++i) {
- std::string argName = capture[i];
+ std::string argName = capture[i + 1];
// Handle nested DAG construct first
if (DagNode argTree = tree.getArgAsNestedDag(i)) {
@@ -915,29 +908,26 @@
LLVM_DEBUG(llvm::dbgs() << '\n');
auto fmt = tree.getNativeCodeTemplate();
- // TODO: replace formatv arguments with the exact specified args.
- SmallVector attrs(8);
- if (tree.getNumArgs() > 8) {
- PrintFatalError(loc,
- "unsupported NativeCodeCall replace argument numbers: " +
- Twine(tree.getNumArgs()));
- }
+
+ SmallVector attrs;
+
bool hasLocationDirective;
std::string locToUse;
std::tie(hasLocationDirective, locToUse) = getLocation(tree);
for (int i = 0, e = tree.getNumArgs() - hasLocationDirective; i != e; ++i) {
if (tree.isNestedDagArg(i)) {
- attrs[i] = handleResultPattern(tree.getArgAsNestedDag(i), i, depth + 1);
+ attrs.push_back(
+ handleResultPattern(tree.getArgAsNestedDag(i), i, depth + 1));
} else {
- attrs[i] = handleOpArgument(tree.getArgAsLeaf(i), tree.getArgName(i));
+ attrs.push_back(
+ handleOpArgument(tree.getArgAsLeaf(i), tree.getArgName(i)));
}
LLVM_DEBUG(llvm::dbgs() << "NativeCodeCall argument #" << i
<< " replacement: " << attrs[i] << "\n");
}
- return std::string(tgfmt(fmt, &fmtCtx.addSubst("_loc", locToUse), attrs[0],
- attrs[1], attrs[2], attrs[3], attrs[4], attrs[5],
- attrs[6], attrs[7]));
+
+ return std::string(tgfmt(fmt, &fmtCtx.addSubst("_loc", locToUse), attrs));
}
int PatternEmitter::getNodeValueCount(DagNode node) {