diff --git a/mlir/tools/mlir-tblgen/PassGen.cpp b/mlir/tools/mlir-tblgen/PassGen.cpp --- a/mlir/tools/mlir-tblgen/PassGen.cpp +++ b/mlir/tools/mlir-tblgen/PassGen.cpp @@ -84,7 +84,6 @@ /// library. MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID({0}Base) -protected: )"; /// Registration for a single dependent dialect, to be inserted for each @@ -110,6 +109,24 @@ } } +/// Emit the declaration of the struct to be used for providing custom options +/// through the C++ APIs. +static void emitPassOptionStructDecl(const Pass &pass, raw_ostream &os) { + os.indent(2) << "struct Options {\n"; + + for (const PassOption &opt : pass.getOptions()) { + os.indent(4) << llvm::formatv("{0} {1}", opt.getType(), + opt.getCppVariableName()); + + if (Optional defaultVal = opt.getDefaultValue()) + os << " = " << defaultVal; + + os << ";\n"; + } + + os.indent(2) << "};\n"; +} + /// Emit the declarations for each of the pass statistics. static void emitPassStatisticDecls(const Pass &pass, raw_ostream &os) { for (const PassStatistic &stat : pass.getStatistics()) { @@ -131,7 +148,24 @@ os << llvm::formatv(passDeclBegin, defName, pass.getBaseClass(), pass.getArgument(), pass.getSummary(), dependentDialectRegistrations); + + // Declare the user-editable structure containing the options. + emitPassOptionStructDecl(pass, os); + + // Create the constructor accepting custom options. + os.indent(2) << llvm::formatv("{0}Base(const Options& options) {{\n", + defName); + + for (const PassOption &opt : pass.getOptions()) + os.indent(4) << llvm::formatv("{0} = options.{0};\n", + opt.getCppVariableName()); + + os.indent(2) << "}\n"; + + // Declare the options. + os << "protected:\n"; emitPassOptionDecls(pass, os); + emitPassStatisticDecls(pass, os); os << "};\n"; }