using TranslateFunction = std::function<LogicalResult(
llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)>;
+/// This class contains all of the components necessary for performing a
+/// translation.
+class Translation {
+public:
+ Translation() = default;
+ Translation(TranslateFunction function, StringRef description,
+ Optional<llvm::Align> inputAlignment)
+ : function(std::move(function)), description(description),
+ inputAlignment(inputAlignment) {}
+
+ /// Return the description of this translation.
+ StringRef getDescription() const { return description; }
+
+ /// Return the optional alignment desired for the input of the translation.
+ Optional<llvm::Align> getInputAlignment() const { return inputAlignment; }
+
+ /// Invoke the translation function with the given input and output streams.
+ LogicalResult operator()(llvm::SourceMgr &sourceMgr,
+ llvm::raw_ostream &output,
+ MLIRContext *context) const {
+ return function(sourceMgr, output, context);
+ }
+
+private:
+ /// The underlying translation function.
+ TranslateFunction function;
+
+ /// The description of the translation.
+ StringRef description;
+
+ /// An optional alignment desired for the input of the translation.
+ Optional<llvm::Align> inputAlignment;
+};
+
/// Use Translate[ToMLIR|FromMLIR]Registration as an initializer that
/// registers a function and associates it with name. This requires that a
-/// translation has not been registered to a given name.
+/// translation has not been registered to a given name. `inputAlign` is an
+/// optional expected alignment for the input data.
///
/// Usage:
///
///
/// \{
struct TranslateToMLIRRegistration {
- TranslateToMLIRRegistration(llvm::StringRef name, llvm::StringRef description,
- const TranslateSourceMgrToMLIRFunction &function);
- TranslateToMLIRRegistration(llvm::StringRef name, llvm::StringRef description,
- const TranslateStringRefToMLIRFunction &function);
+ TranslateToMLIRRegistration(
+ llvm::StringRef name, llvm::StringRef description,
+ const TranslateSourceMgrToMLIRFunction &function,
+ Optional<llvm::Align> inputAlignment = llvm::None);
+ TranslateToMLIRRegistration(
+ llvm::StringRef name, llvm::StringRef description,
+ const TranslateStringRefToMLIRFunction &function,
+ Optional<llvm::Align> inputAlignment = llvm::None);
};
struct TranslateFromMLIRRegistration {
/// \}
/// A command line parser for translation functions.
-struct TranslationParser : public llvm::cl::parser<const TranslateFunction *> {
+struct TranslationParser : public llvm::cl::parser<const Translation *> {
TranslationParser(llvm::cl::Option &opt);
void printOptionInfo(const llvm::cl::Option &o,
// Translation Registry
//===----------------------------------------------------------------------===//
-struct TranslationBundle {
- TranslateFunction translateFunction;
- StringRef translateDescription;
-};
-
-/// Get the mutable static map between registered file-to-file MLIR translations
-/// and TranslateFunctions with its description that perform those translations.
-static llvm::StringMap<TranslationBundle> &getTranslationRegistry() {
- static llvm::StringMap<TranslationBundle> translationBundle;
+/// Get the mutable static map between registered file-to-file MLIR
+/// translations.
+static llvm::StringMap<Translation> &getTranslationRegistry() {
+ static llvm::StringMap<Translation> translationBundle;
return translationBundle;
}
/// Register the given translation.
static void registerTranslation(StringRef name, StringRef description,
+ Optional<llvm::Align> inputAlignment,
const TranslateFunction &function) {
- auto &translationRegistry = getTranslationRegistry();
- if (translationRegistry.find(name) != translationRegistry.end())
+ auto ®istry = getTranslationRegistry();
+ if (registry.count(name))
llvm::report_fatal_error(
"Attempting to overwrite an existing <file-to-file> function");
assert(function &&
"Attempting to register an empty translate <file-to-file> function");
- translationRegistry[name].translateFunction = function;
- translationRegistry[name].translateDescription = description;
+ registry[name] = Translation(function, description, inputAlignment);
}
TranslateRegistration::TranslateRegistration(
StringRef name, StringRef description, const TranslateFunction &function) {
- registerTranslation(name, description, function);
+ registerTranslation(name, description, /*inputAlignment=*/llvm::None,
+ function);
}
//===----------------------------------------------------------------------===//
// Puts `function` into the to-MLIR translation registry unless there is already
// a function registered for the same name.
static void registerTranslateToMLIRFunction(
- StringRef name, StringRef description,
+ StringRef name, StringRef description, Optional<llvm::Align> inputAlignment,
const TranslateSourceMgrToMLIRFunction &function) {
auto wrappedFn = [function](llvm::SourceMgr &sourceMgr, raw_ostream &output,
MLIRContext *context) {
op.get()->print(output);
return success();
};
- registerTranslation(name, description, wrappedFn);
+ registerTranslation(name, description, inputAlignment, wrappedFn);
}
TranslateToMLIRRegistration::TranslateToMLIRRegistration(
StringRef name, StringRef description,
- const TranslateSourceMgrToMLIRFunction &function) {
- registerTranslateToMLIRFunction(name, description, function);
+ const TranslateSourceMgrToMLIRFunction &function,
+ Optional<llvm::Align> inputAlignment) {
+ registerTranslateToMLIRFunction(name, description, inputAlignment, function);
}
/// Wraps `function` with a lambda that extracts a StringRef from a source
/// manager and registers the wrapper lambda as a to-MLIR conversion.
TranslateToMLIRRegistration::TranslateToMLIRRegistration(
StringRef name, StringRef description,
- const TranslateStringRefToMLIRFunction &function) {
+ const TranslateStringRefToMLIRFunction &function,
+ Optional<llvm::Align> inputAlignment) {
registerTranslateToMLIRFunction(
- name, description,
+ name, description, inputAlignment,
[function](llvm::SourceMgr &sourceMgr, MLIRContext *ctx) {
const llvm::MemoryBuffer *buffer =
sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
StringRef name, StringRef description,
const TranslateFromMLIRFunction &function,
const std::function<void(DialectRegistry &)> &dialectRegistration) {
-
registerTranslation(
- name, description,
+ name, description, /*inputAlignment=*/llvm::None,
[function, dialectRegistration](llvm::SourceMgr &sourceMgr,
raw_ostream &output,
MLIRContext *context) {
//===----------------------------------------------------------------------===//
TranslationParser::TranslationParser(llvm::cl::Option &opt)
- : llvm::cl::parser<const TranslateFunction *>(opt) {
- for (const auto &kv : getTranslationRegistry()) {
- addLiteralOption(kv.first(), &kv.second.translateFunction,
- kv.second.translateDescription);
- }
+ : llvm::cl::parser<const Translation *>(opt) {
+ for (const auto &kv : getTranslationRegistry())
+ addLiteralOption(kv.first(), &kv.second, kv.second.getDescription());
}
void TranslationParser::printOptionInfo(const llvm::cl::Option &o,
const TranslationParser::OptionInfo *rhs) {
return lhs->Name.compare(rhs->Name);
});
- llvm::cl::parser<const TranslateFunction *>::printOptionInfo(o, globalWidth);
+ llvm::cl::parser<const Translation *>::printOptionInfo(o, globalWidth);
}