|
| 1 | +//===- TestToLLVMIRTranslation.cpp - Translate Test dialect to LLVM IR ----===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements a translation between the MLIR Test dialect and LLVM IR. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "TestDialect.h" |
| 14 | +#include "mlir/IR/Builders.h" |
| 15 | +#include "mlir/IR/BuiltinAttributes.h" |
| 16 | +#include "mlir/IR/BuiltinOps.h" |
| 17 | +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" |
| 18 | +#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" |
| 19 | +#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" |
| 20 | +#include "mlir/Target/LLVMIR/ModuleTranslation.h" |
| 21 | +#include "mlir/Tools/mlir-translate/Translation.h" |
| 22 | +#include "llvm/ADT/StringSwitch.h" |
| 23 | +#include "llvm/ADT/TypeSwitch.h" |
| 24 | + |
| 25 | +using namespace mlir; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +class TestDialectLLVMIRTranslationInterface |
| 30 | + : public LLVMTranslationDialectInterface { |
| 31 | +public: |
| 32 | + using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface; |
| 33 | + |
| 34 | + LogicalResult |
| 35 | + amendOperation(Operation *op, NamedAttribute attribute, |
| 36 | + LLVM::ModuleTranslation &moduleTranslation) const final; |
| 37 | + |
| 38 | + LogicalResult |
| 39 | + convertOperation(Operation *op, llvm::IRBuilderBase &builder, |
| 40 | + LLVM::ModuleTranslation &moduleTranslation) const final; |
| 41 | +}; |
| 42 | + |
| 43 | +} // namespace |
| 44 | + |
| 45 | +LogicalResult TestDialectLLVMIRTranslationInterface::amendOperation( |
| 46 | + Operation *op, NamedAttribute attribute, |
| 47 | + LLVM::ModuleTranslation &moduleTranslation) const { |
| 48 | + return llvm::StringSwitch<llvm::function_ref<LogicalResult(Attribute)>>( |
| 49 | + attribute.getName()) |
| 50 | + // The `test.discardable_mod_attr` attribute, if present and set to |
| 51 | + // `true`, results in the addition of a `test.symbol` in the module it is |
| 52 | + // attached to with name "sym_from_attr". |
| 53 | + .Case("test.discardable_mod_attr", |
| 54 | + [&](Attribute attr) { |
| 55 | + if (!isa<ModuleOp>(op)) { |
| 56 | + op->emitOpError("attribute 'test.discardable_mod_attr' only " |
| 57 | + "supported in modules"); |
| 58 | + return failure(); |
| 59 | + } |
| 60 | + |
| 61 | + bool createSymbol = false; |
| 62 | + if (auto boolAttr = attr.dyn_cast<BoolAttr>()) |
| 63 | + createSymbol = boolAttr.getValue(); |
| 64 | + |
| 65 | + if (createSymbol) { |
| 66 | + OpBuilder builder(op->getRegion(0)); |
| 67 | + builder.create<test::SymbolOp>( |
| 68 | + op->getLoc(), |
| 69 | + StringAttr::get(op->getContext(), "sym_from_attr"), |
| 70 | + /*sym_visibility=*/nullptr); |
| 71 | + } |
| 72 | + |
| 73 | + return success(); |
| 74 | + }) |
| 75 | + .Default([](Attribute) { |
| 76 | + // Skip other discardable dialect attributes. |
| 77 | + return success(); |
| 78 | + })(attribute.getValue()); |
| 79 | +} |
| 80 | + |
| 81 | +LogicalResult TestDialectLLVMIRTranslationInterface::convertOperation( |
| 82 | + Operation *op, llvm::IRBuilderBase &builder, |
| 83 | + LLVM::ModuleTranslation &moduleTranslation) const { |
| 84 | + return llvm::TypeSwitch<Operation *, LogicalResult>(op) |
| 85 | + // `test.symbol`s are translated into global integers in LLVM IR, with a |
| 86 | + // name equal to the symbol they are translated from. |
| 87 | + .Case([&](test::SymbolOp symOp) { |
| 88 | + llvm::Module *mod = moduleTranslation.getLLVMModule(); |
| 89 | + llvm::IntegerType *i32Type = |
| 90 | + llvm::IntegerType::get(moduleTranslation.getLLVMContext(), 32); |
| 91 | + mod->getOrInsertGlobal(symOp.getSymName(), i32Type); |
| 92 | + return success(); |
| 93 | + }) |
| 94 | + .Default([&](Operation *) { |
| 95 | + return op->emitOpError("unsupported translation of test operation"); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +namespace mlir { |
| 100 | + |
| 101 | +void registerTestToLLVMIR() { |
| 102 | + TranslateFromMLIRRegistration registration( |
| 103 | + "test-to-llvmir", "test dialect to LLVM IR", |
| 104 | + [](Operation *op, raw_ostream &output) { |
| 105 | + llvm::LLVMContext llvmContext; |
| 106 | + auto llvmModule = translateModuleToLLVMIR(op, llvmContext); |
| 107 | + if (!llvmModule) |
| 108 | + return failure(); |
| 109 | + |
| 110 | + llvmModule->print(output, nullptr); |
| 111 | + return success(); |
| 112 | + }, |
| 113 | + [](DialectRegistry ®istry) { |
| 114 | + registry.insert<test::TestDialect>(); |
| 115 | + registerBuiltinDialectTranslation(registry); |
| 116 | + registerLLVMDialectTranslation(registry); |
| 117 | + registry.addExtension( |
| 118 | + +[](MLIRContext *ctx, test::TestDialect *dialect) { |
| 119 | + dialect->addInterfaces<TestDialectLLVMIRTranslationInterface>(); |
| 120 | + }); |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace mlir |
0 commit comments