20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/SourceMgr.h"
31 llvm::cl::opt<bool> noImplicitModule{
33 llvm::cl::desc(
"Disable the parsing of an implicit top-level module op"),
34 llvm::cl::init(
false)};
37 static llvm::ManagedStatic<TranslationOptions>
clOptions;
48 static llvm::StringMap<Translation> translationBundle;
49 return translationBundle;
54 std::optional<llvm::Align> inputAlignment,
57 if (registry.count(name))
58 llvm::report_fatal_error(
59 "Attempting to overwrite an existing <file-to-file> function");
61 "Attempting to register an empty translate <file-to-file> function");
62 registry[name] =
Translation(
function, description, inputAlignment);
78 StringRef name, StringRef description,
80 std::optional<llvm::Align> inputAlignment,
82 auto wrappedFn = [
function, dialectRegistration](
83 const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
86 dialectRegistration(registry);
87 context->appendDialectRegistry(registry);
89 if (!op || failed(
verify(*op)))
91 op.get()->
print(output);
98 StringRef name, StringRef description,
101 std::optional<llvm::Align> inputAlignment) {
103 inputAlignment,
function);
106 StringRef name, StringRef description,
109 std::optional<llvm::Align> inputAlignment) {
111 name, description, dialectRegistration, inputAlignment,
112 [
function](
const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
113 MLIRContext *ctx) {
return function(*sourceMgr, ctx); });
118 StringRef name, StringRef description,
121 std::optional<llvm::Align> inputAlignment) {
123 name, description, dialectRegistration, inputAlignment,
124 [
function](
const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
126 const llvm::MemoryBuffer *buffer =
127 sourceMgr->getMemoryBuffer(sourceMgr->getMainFileID());
128 return function(buffer->getBuffer(), ctx);
137 StringRef name, StringRef description,
141 name, description, std::nullopt,
143 dialectRegistration](
const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
146 dialectRegistration(registry);
148 bool implicitModule =
152 if (!op || failed(
verify(*op)))
154 return function(op.get(), output);
165 addLiteralOption(kv.first(), &kv.second, kv.second.getDescription());
169 size_t globalWidth)
const {
171 llvm::array_pod_sort(tp->Values.begin(), tp->Values.end(),
172 [](
const TranslationParser::OptionInfo *lhs,
173 const TranslationParser::OptionInfo *rhs) {
174 return lhs->Name.compare(rhs->Name);
static llvm::ManagedStatic< TranslationOptions > clOptions
static llvm::StringMap< Translation > & getTranslationRegistry()
Get the mutable static map between registered file-to-file MLIR translations.
static void registerTranslateToMLIRFunction(StringRef name, StringRef description, const DialectRegistrationFunction &dialectRegistration, std::optional< llvm::Align > inputAlignment, const TranslateSourceMgrToMLIRFunction &function)
static void registerTranslation(StringRef name, StringRef description, std::optional< llvm::Align > inputAlignment, const TranslateFunction &function)
Register the given translation.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
void appendDialectRegistry(const DialectRegistry ®istry)
Append the contents of the given dialect registry to the registry associated with this context.
void print(raw_ostream &os, const OpPrintingFlags &flags=std::nullopt)
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
This class contains all of the components necessary for performing a translation.
Include the generated interface declarations.
Include the generated interface declarations.
std::function< LogicalResult(const std::shared_ptr< llvm::SourceMgr > &sourceMgr, llvm::raw_ostream &output, MLIRContext *)> TranslateFunction
Interface of the function that performs file-to-file translation involving MLIR.
std::function< OwningOpRef< Operation * >(llvm::SourceMgr &sourceMgr, MLIRContext *)> TranslateRawSourceMgrToMLIRFunction
void registerTranslationCLOptions()
Register command-line options used by the translation registry.
std::function< void(DialectRegistry &)> DialectRegistrationFunction
Interface of the function that adds all dialects and dialect extensions used for the translation to t...
OwningOpRef< Operation * > parseSourceFileForTool(const std::shared_ptr< llvm::SourceMgr > &sourceMgr, const ParserConfig &config, bool insertImplicitModule)
This parses the file specified by the indicated SourceMgr.
std::function< LogicalResult(Operation *, llvm::raw_ostream &output)> TranslateFromMLIRFunction
Interface of the function that translates MLIR to a different format and outputs the result to a stre...
std::function< OwningOpRef< Operation * >(llvm::StringRef, MLIRContext *)> TranslateStringRefToMLIRFunction
Interface of the function that translates the given string to MLIR.
std::function< OwningOpRef< Operation * >(const std::shared_ptr< llvm::SourceMgr > &sourceMgr, MLIRContext *)> TranslateSourceMgrToMLIRFunction
Interface of the function that translates the sources managed by sourceMgr to MLIR.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
TranslateFromMLIRRegistration(llvm::StringRef name, llvm::StringRef description, const TranslateFromMLIRFunction &function, const DialectRegistrationFunction &dialectRegistration=[](DialectRegistry &) {})
TranslateRegistration(llvm::StringRef name, llvm::StringRef description, const TranslateFunction &function)
TranslateToMLIRRegistration(llvm::StringRef name, llvm::StringRef description, const TranslateSourceMgrToMLIRFunction &function, const DialectRegistrationFunction &dialectRegistration=[](DialectRegistry &) {}, std::optional< llvm::Align > inputAlignment=std::nullopt)
A command line parser for translation functions.
void printOptionInfo(const llvm::cl::Option &o, size_t globalWidth) const override
TranslationParser(llvm::cl::Option &opt)