//===- Pass.cpp - MLIR pass registration generator ------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // PassGen uses the description of passes to generate base classes for passes // and command line registration. // //===----------------------------------------------------------------------===// #include "mlir/TableGen/GenInfo.h" #include "mlir/TableGen/Pass.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/TableGen/Error.h" #include "llvm/TableGen/Record.h" usingnamespacemlir; usingnamespacemlir::tblgen; formatv; RecordKeeper; static llvm::cl::OptionCategory passGenCat("Options for -gen-pass-decls"); static llvm::cl::opt<std::string> groupName("name", llvm::cl::desc("The name of this group of passes"), llvm::cl::cat(passGenCat)); /// Extract the list of passes from the TableGen records. static std::vector<Pass> getPasses(const RecordKeeper &records) { … } const char *const passHeader = …; //===----------------------------------------------------------------------===// // GEN: Pass registration generation //===----------------------------------------------------------------------===// /// The code snippet used to generate a pass registration. /// /// {0}: The def name of the pass record. /// {1}: The pass constructor call. const char *const passRegistrationCode = …; /// The code snippet used to generate a function to register all passes in a /// group. /// /// {0}: The name of the pass group. const char *const passGroupRegistrationCode = …; /// Emits the definition of the struct to be used to control the pass options. static void emitPassOptionsStruct(const Pass &pass, raw_ostream &os) { … } static std::string getPassDeclVarName(const Pass &pass) { … } /// Emit the code to be included in the public header of the pass. static void emitPassDecls(const Pass &pass, raw_ostream &os) { … } /// Emit the code for registering each of the given passes with the global /// PassRegistry. static void emitRegistrations(llvm::ArrayRef<Pass> passes, raw_ostream &os) { … } //===----------------------------------------------------------------------===// // GEN: Pass base class generation //===----------------------------------------------------------------------===// /// The code snippet used to generate the start of a pass base class. /// /// {0}: The def name of the pass record. /// {1}: The base class for the pass. /// {2): The command line argument for the pass. /// {3}: The summary for the pass. /// {4}: The dependent dialects registration. const char *const baseClassBegin = …; /// Registration for a single dependent dialect, to be inserted for each /// dependent dialect in the `getDependentDialects` above. const char *const dialectRegistrationTemplate = …; const char *const friendDefaultConstructorDeclTemplate = …; const char *const friendDefaultConstructorWithOptionsDeclTemplate = …; const char *const friendDefaultConstructorDefTemplate = …; const char *const friendDefaultConstructorWithOptionsDefTemplate = …; const char *const defaultConstructorDefTemplate = …; const char *const defaultConstructorWithOptionsDefTemplate = …; /// Emit the declarations for each of the pass options. static void emitPassOptionDecls(const Pass &pass, raw_ostream &os) { … } /// Emit the declarations for each of the pass statistics. static void emitPassStatisticDecls(const Pass &pass, raw_ostream &os) { … } /// Emit the code to be used in the implementation of the pass. static void emitPassDefs(const Pass &pass, raw_ostream &os) { … } static void emitPass(const Pass &pass, raw_ostream &os) { … } // TODO: Drop old pass declarations. // The old pass base class is being kept until all the passes have switched to // the new decls/defs design. const char *const oldPassDeclBegin = …; // TODO: Drop old pass declarations. /// Emit a backward-compatible declaration of the pass base class. static void emitOldPassDecl(const Pass &pass, raw_ostream &os) { … } static void emitPasses(const RecordKeeper &records, raw_ostream &os) { … } static mlir::GenRegistration genPassDecls("gen-pass-decls", "Generate pass declarations", [](const RecordKeeper &records, raw_ostream &os) { … });