Skip to content

Commit 0e9523e

Browse files
committed
[mlir] Support lowering of dialect attributes attached to top-level modules
This patch supports the processing of dialect attributes attached to top-level module-type operations during MLIR-to-LLVMIR lowering. This approach modifies the `mlir::translateModuleToLLVMIR()` function to call `ModuleTranslation::convertOperation()` on the top-level operation, after its body has been lowered. This, in turn, will get the `LLVMTranslationDialectInterface` object associated to that operation's dialect before trying to use it for lowering prior to processing dialect attributes attached to the operation. Since there are no `LLVMTranslationDialectInterface`s for the builtin and GPU dialects, which define their own module-type operations, this patch also adds and registers them. The requirement for always calling `mlir::registerBuiltinDialectTranslation()` before any translation of MLIR to LLVM IR where builtin module operations are present is introduced. The purpose of these new translation interfaces is to succeed when processing module-type operations, allowing the lowering process to continue and to prevent the introduction of failures related to not finding such interfaces. Differential Revision: https://reviews.llvm.org/D145932
1 parent cdccea8 commit 0e9523e

File tree

31 files changed

+232
-3
lines changed

31 files changed

+232
-3
lines changed

flang/lib/Frontend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_flang_library(flangFrontend
3434
HLFIRDialect
3535
HLFIRTransforms
3636
MLIRTransforms
37+
MLIRBuiltinToLLVMIRTranslation
3738
MLIRLLVMToLLVMIRTranslation
3839
MLIRSCFToControlFlow
3940
MLIRTargetLLVMIRImport

flang/lib/Optimizer/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_flang_library(FIRCodeGen
2323
MLIRMathToLLVM
2424
MLIRMathToLibm
2525
MLIROpenMPToLLVM
26+
MLIRBuiltinToLLVMIRTranslation
2627
MLIRLLVMToLLVMIRTranslation
2728
MLIRTargetLLVMIRExport
2829

flang/lib/Optimizer/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_flang_library(FIRDialect
1717
LINK_LIBS
1818
FIRDialectSupport
1919
MLIRArithDialect
20+
MLIRBuiltinToLLVMIRTranslation
2021
MLIROpenMPToLLVM
2122
MLIRLLVMToLLVMIRTranslation
2223
MLIRTargetLLVMIRExport

flang/lib/Optimizer/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_flang_library(FIRSupport
1212

1313
LINK_LIBS
1414
${dialect_libs}
15+
MLIRBuiltinToLLVMIRTranslation
1516
MLIROpenMPToLLVMIRTranslation
1617
MLIRLLVMToLLVMIRTranslation
1718
MLIRTargetLLVMIRExport

flang/lib/Optimizer/Support/InitFIR.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "flang/Optimizer/Support/InitFIR.h"
10+
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
1011
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
1112
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
1213

1314
void fir::support::registerLLVMTranslation(mlir::MLIRContext &context) {
1415
mlir::DialectRegistry registry;
1516
// Register OpenMP dialect interface here as well.
16-
mlir::registerOpenMPDialectTranslation(registry);
17+
registerOpenMPDialectTranslation(registry);
1718
// Register LLVM-IR dialect interface.
1819
registerLLVMDialectTranslation(registry);
20+
// Register builtin dialect interface.
21+
registerBuiltinDialectTranslation(registry);
1922
context.appendDialectRegistry(registry);
2023
}

flang/tools/tco/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ target_link_libraries(tco PRIVATE
1717
${dialect_libs}
1818
MLIRIR
1919
MLIRLLVMDialect
20+
MLIRBuiltinToLLVMIRTranslation
2021
MLIRLLVMToLLVMIRTranslation
2122
MLIRTargetLLVMIRExport
2223
MLIRPass

mlir/examples/toy/Ch6/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ target_link_libraries(toyc-ch6
4444
${dialect_libs}
4545
${conversion_libs}
4646
MLIRAnalysis
47+
MLIRBuiltinToLLVMIRTranslation
4748
MLIRCallInterfaces
4849
MLIRCastInterfaces
4950
MLIRExecutionEngine

mlir/examples/toy/Ch6/toyc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mlir/Parser/Parser.h"
2828
#include "mlir/Pass/Pass.h"
2929
#include "mlir/Pass/PassManager.h"
30+
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
3031
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
3132
#include "mlir/Target/LLVMIR/Export.h"
3233
#include "mlir/Transforms/Passes.h"
@@ -200,6 +201,7 @@ int dumpAST() {
200201

201202
int dumpLLVMIR(mlir::ModuleOp module) {
202203
// Register the translation to LLVM IR with the MLIR context.
204+
mlir::registerBuiltinDialectTranslation(*module->getContext());
203205
mlir::registerLLVMDialectTranslation(*module->getContext());
204206

205207
// Convert the module to LLVM IR in a new LLVM IR context.
@@ -234,6 +236,7 @@ int runJit(mlir::ModuleOp module) {
234236

235237
// Register the translation from MLIR to LLVM IR, which must happen before we
236238
// can JIT-compile.
239+
mlir::registerBuiltinDialectTranslation(*module->getContext());
237240
mlir::registerLLVMDialectTranslation(*module->getContext());
238241

239242
// An optimization pipeline to use within the execution engine.

mlir/examples/toy/Ch7/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ target_link_libraries(toyc-ch7
4343
${dialect_libs}
4444
${conversion_libs}
4545
MLIRAnalysis
46+
MLIRBuiltinToLLVMIRTranslation
4647
MLIRCallInterfaces
4748
MLIRCastInterfaces
4849
MLIRExecutionEngine

mlir/examples/toy/Ch7/toyc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mlir/Parser/Parser.h"
2828
#include "mlir/Pass/Pass.h"
2929
#include "mlir/Pass/PassManager.h"
30+
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
3031
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
3132
#include "mlir/Target/LLVMIR/Export.h"
3233
#include "mlir/Transforms/Passes.h"
@@ -201,6 +202,7 @@ int dumpAST() {
201202

202203
int dumpLLVMIR(mlir::ModuleOp module) {
203204
// Register the translation to LLVM IR with the MLIR context.
205+
mlir::registerBuiltinDialectTranslation(*module->getContext());
204206
mlir::registerLLVMDialectTranslation(*module->getContext());
205207

206208
// Convert the module to LLVM IR in a new LLVM IR context.
@@ -235,6 +237,7 @@ int runJit(mlir::ModuleOp module) {
235237

236238
// Register the translation from MLIR to LLVM IR, which must happen before we
237239
// can JIT-compile.
240+
mlir::registerBuiltinDialectTranslation(*module->getContext());
238241
mlir::registerLLVMDialectTranslation(*module->getContext());
239242

240243
// An optimization pipeline to use within the execution engine.

0 commit comments

Comments
 (0)