From 0e9523efda8a4ad95ecb1d5b5e65e10bcc3711f5 Mon Sep 17 00:00:00 2001 From: Sergio Afonso Date: Mon, 13 Mar 2023 12:51:18 +0000 Subject: [PATCH] [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 --- flang/lib/Frontend/CMakeLists.txt | 1 + flang/lib/Optimizer/CodeGen/CMakeLists.txt | 1 + flang/lib/Optimizer/Dialect/CMakeLists.txt | 1 + flang/lib/Optimizer/Support/CMakeLists.txt | 1 + flang/lib/Optimizer/Support/InitFIR.cpp | 5 ++- flang/tools/tco/CMakeLists.txt | 1 + mlir/examples/toy/Ch6/CMakeLists.txt | 1 + mlir/examples/toy/Ch6/toyc.cpp | 3 ++ mlir/examples/toy/Ch7/CMakeLists.txt | 1 + mlir/examples/toy/Ch7/toyc.cpp | 3 ++ mlir/include/mlir/Target/LLVMIR/Dialect/All.h | 4 ++ .../Dialect/Builtin/BuiltinToLLVMIRTranslation.h | 31 +++++++++++++++ .../LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h | 31 +++++++++++++++ mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt | 1 + mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp | 5 ++- mlir/lib/CAPI/RegisterEverything/CMakeLists.txt | 1 + .../CAPI/RegisterEverything/RegisterEverything.cpp | 5 ++- mlir/lib/Dialect/GPU/CMakeLists.txt | 2 + .../lib/Dialect/GPU/Transforms/SerializeToBlob.cpp | 2 + mlir/lib/ExecutionEngine/CMakeLists.txt | 1 + mlir/lib/Target/LLVMIR/CMakeLists.txt | 2 + .../Dialect/Builtin/BuiltinToLLVMIRTranslation.cpp | 46 ++++++++++++++++++++++ .../Target/LLVMIR/Dialect/Builtin/CMakeLists.txt | 12 ++++++ mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt | 2 + mlir/lib/Target/LLVMIR/Dialect/GPU/CMakeLists.txt | 13 ++++++ .../LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.cpp | 45 +++++++++++++++++++++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp | 4 ++ mlir/tools/mlir-cpu-runner/CMakeLists.txt | 1 + mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt | 1 + mlir/tools/mlir-vulkan-runner/CMakeLists.txt | 1 + mlir/unittests/ExecutionEngine/Invoke.cpp | 7 ++++ 31 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 mlir/include/mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h create mode 100644 mlir/include/mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h create mode 100644 mlir/lib/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.cpp create mode 100644 mlir/lib/Target/LLVMIR/Dialect/Builtin/CMakeLists.txt create mode 100644 mlir/lib/Target/LLVMIR/Dialect/GPU/CMakeLists.txt create mode 100644 mlir/lib/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.cpp diff --git a/flang/lib/Frontend/CMakeLists.txt b/flang/lib/Frontend/CMakeLists.txt index 7a04a2e..c23a395 100644 --- a/flang/lib/Frontend/CMakeLists.txt +++ b/flang/lib/Frontend/CMakeLists.txt @@ -34,6 +34,7 @@ add_flang_library(flangFrontend HLFIRDialect HLFIRTransforms MLIRTransforms + MLIRBuiltinToLLVMIRTranslation MLIRLLVMToLLVMIRTranslation MLIRSCFToControlFlow MLIRTargetLLVMIRImport diff --git a/flang/lib/Optimizer/CodeGen/CMakeLists.txt b/flang/lib/Optimizer/CodeGen/CMakeLists.txt index 1b8fe47..711bb40 100644 --- a/flang/lib/Optimizer/CodeGen/CMakeLists.txt +++ b/flang/lib/Optimizer/CodeGen/CMakeLists.txt @@ -23,6 +23,7 @@ add_flang_library(FIRCodeGen MLIRMathToLLVM MLIRMathToLibm MLIROpenMPToLLVM + MLIRBuiltinToLLVMIRTranslation MLIRLLVMToLLVMIRTranslation MLIRTargetLLVMIRExport diff --git a/flang/lib/Optimizer/Dialect/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CMakeLists.txt index 342a9d141..fe5edb5 100644 --- a/flang/lib/Optimizer/Dialect/CMakeLists.txt +++ b/flang/lib/Optimizer/Dialect/CMakeLists.txt @@ -17,6 +17,7 @@ add_flang_library(FIRDialect LINK_LIBS FIRDialectSupport MLIRArithDialect + MLIRBuiltinToLLVMIRTranslation MLIROpenMPToLLVM MLIRLLVMToLLVMIRTranslation MLIRTargetLLVMIRExport diff --git a/flang/lib/Optimizer/Support/CMakeLists.txt b/flang/lib/Optimizer/Support/CMakeLists.txt index 33a9ed4..2d0e4dd 100644 --- a/flang/lib/Optimizer/Support/CMakeLists.txt +++ b/flang/lib/Optimizer/Support/CMakeLists.txt @@ -12,6 +12,7 @@ add_flang_library(FIRSupport LINK_LIBS ${dialect_libs} + MLIRBuiltinToLLVMIRTranslation MLIROpenMPToLLVMIRTranslation MLIRLLVMToLLVMIRTranslation MLIRTargetLLVMIRExport diff --git a/flang/lib/Optimizer/Support/InitFIR.cpp b/flang/lib/Optimizer/Support/InitFIR.cpp index baa1336..09852b1 100644 --- a/flang/lib/Optimizer/Support/InitFIR.cpp +++ b/flang/lib/Optimizer/Support/InitFIR.cpp @@ -7,14 +7,17 @@ //===----------------------------------------------------------------------===// #include "flang/Optimizer/Support/InitFIR.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h" void fir::support::registerLLVMTranslation(mlir::MLIRContext &context) { mlir::DialectRegistry registry; // Register OpenMP dialect interface here as well. - mlir::registerOpenMPDialectTranslation(registry); + registerOpenMPDialectTranslation(registry); // Register LLVM-IR dialect interface. registerLLVMDialectTranslation(registry); + // Register builtin dialect interface. + registerBuiltinDialectTranslation(registry); context.appendDialectRegistry(registry); } diff --git a/flang/tools/tco/CMakeLists.txt b/flang/tools/tco/CMakeLists.txt index 207e722..cb9b438 100644 --- a/flang/tools/tco/CMakeLists.txt +++ b/flang/tools/tco/CMakeLists.txt @@ -17,6 +17,7 @@ target_link_libraries(tco PRIVATE ${dialect_libs} MLIRIR MLIRLLVMDialect + MLIRBuiltinToLLVMIRTranslation MLIRLLVMToLLVMIRTranslation MLIRTargetLLVMIRExport MLIRPass diff --git a/mlir/examples/toy/Ch6/CMakeLists.txt b/mlir/examples/toy/Ch6/CMakeLists.txt index 13ab002..2a2b796 100644 --- a/mlir/examples/toy/Ch6/CMakeLists.txt +++ b/mlir/examples/toy/Ch6/CMakeLists.txt @@ -44,6 +44,7 @@ target_link_libraries(toyc-ch6 ${dialect_libs} ${conversion_libs} MLIRAnalysis + MLIRBuiltinToLLVMIRTranslation MLIRCallInterfaces MLIRCastInterfaces MLIRExecutionEngine diff --git a/mlir/examples/toy/Ch6/toyc.cpp b/mlir/examples/toy/Ch6/toyc.cpp index 9397874..830df1c 100644 --- a/mlir/examples/toy/Ch6/toyc.cpp +++ b/mlir/examples/toy/Ch6/toyc.cpp @@ -27,6 +27,7 @@ #include "mlir/Parser/Parser.h" #include "mlir/Pass/Pass.h" #include "mlir/Pass/PassManager.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Export.h" #include "mlir/Transforms/Passes.h" @@ -200,6 +201,7 @@ int dumpAST() { int dumpLLVMIR(mlir::ModuleOp module) { // Register the translation to LLVM IR with the MLIR context. + mlir::registerBuiltinDialectTranslation(*module->getContext()); mlir::registerLLVMDialectTranslation(*module->getContext()); // Convert the module to LLVM IR in a new LLVM IR context. @@ -234,6 +236,7 @@ int runJit(mlir::ModuleOp module) { // Register the translation from MLIR to LLVM IR, which must happen before we // can JIT-compile. + mlir::registerBuiltinDialectTranslation(*module->getContext()); mlir::registerLLVMDialectTranslation(*module->getContext()); // An optimization pipeline to use within the execution engine. diff --git a/mlir/examples/toy/Ch7/CMakeLists.txt b/mlir/examples/toy/Ch7/CMakeLists.txt index 8c4c3ee..05a56dc 100644 --- a/mlir/examples/toy/Ch7/CMakeLists.txt +++ b/mlir/examples/toy/Ch7/CMakeLists.txt @@ -43,6 +43,7 @@ target_link_libraries(toyc-ch7 ${dialect_libs} ${conversion_libs} MLIRAnalysis + MLIRBuiltinToLLVMIRTranslation MLIRCallInterfaces MLIRCastInterfaces MLIRExecutionEngine diff --git a/mlir/examples/toy/Ch7/toyc.cpp b/mlir/examples/toy/Ch7/toyc.cpp index 78dcc1a..056f2a8 100644 --- a/mlir/examples/toy/Ch7/toyc.cpp +++ b/mlir/examples/toy/Ch7/toyc.cpp @@ -27,6 +27,7 @@ #include "mlir/Parser/Parser.h" #include "mlir/Pass/Pass.h" #include "mlir/Pass/PassManager.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Export.h" #include "mlir/Transforms/Passes.h" @@ -201,6 +202,7 @@ int dumpAST() { int dumpLLVMIR(mlir::ModuleOp module) { // Register the translation to LLVM IR with the MLIR context. + mlir::registerBuiltinDialectTranslation(*module->getContext()); mlir::registerLLVMDialectTranslation(*module->getContext()); // Convert the module to LLVM IR in a new LLVM IR context. @@ -235,6 +237,7 @@ int runJit(mlir::ModuleOp module) { // Register the translation from MLIR to LLVM IR, which must happen before we // can JIT-compile. + mlir::registerBuiltinDialectTranslation(*module->getContext()); mlir::registerLLVMDialectTranslation(*module->getContext()); // An optimization pipeline to use within the execution engine. diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/All.h b/mlir/include/mlir/Target/LLVMIR/Dialect/All.h index b9e5297..cd7f76f 100644 --- a/mlir/include/mlir/Target/LLVMIR/Dialect/All.h +++ b/mlir/include/mlir/Target/LLVMIR/Dialect/All.h @@ -17,6 +17,8 @@ #include "mlir/Target/LLVMIR/Dialect/AMX/AMXToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/ArmNeon/ArmNeonToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/ArmSVE/ArmSVEToLLVMIRTranslation.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" +#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/NVVM/NVVMToLLVMIRTranslation.h" @@ -34,6 +36,8 @@ static inline void registerAllToLLVMIRTranslations(DialectRegistry ®istry) { registerArmNeonDialectTranslation(registry); registerAMXDialectTranslation(registry); registerArmSVEDialectTranslation(registry); + registerBuiltinDialectTranslation(registry); + registerGPUDialectTranslation(registry); registerLLVMDialectTranslation(registry); registerNVVMDialectTranslation(registry); registerOpenACCDialectTranslation(registry); diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h b/mlir/include/mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h new file mode 100644 index 0000000..cfb2629 --- /dev/null +++ b/mlir/include/mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h @@ -0,0 +1,31 @@ +//==- BuiltinToLLVMIRTranslation.h - Builtin Dialect to LLVM IR -*- C++ -*-==// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This provides registration calls for builtin dialect to LLVM IR translation. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_TARGET_LLVMIR_DIALECT_BUILTIN_BUILTINTOLLVMIRTRANSLATION_H +#define MLIR_TARGET_LLVMIR_DIALECT_BUILTIN_BUILTINTOLLVMIRTRANSLATION_H + +namespace mlir { + +class DialectRegistry; +class MLIRContext; + +/// Register the translation from the builtin dialect to the LLVM IR in the +/// given registry. +void registerBuiltinDialectTranslation(DialectRegistry ®istry); + +/// Register the translation from the builtin dialect in the registry associated +/// with the given context. +void registerBuiltinDialectTranslation(MLIRContext &context); + +} // namespace mlir + +#endif // MLIR_TARGET_LLVMIR_DIALECT_BUILTIN_BUILTINTOLLVMIRTRANSLATION_H diff --git a/mlir/include/mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h b/mlir/include/mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h new file mode 100644 index 0000000..3ba7a18 --- /dev/null +++ b/mlir/include/mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h @@ -0,0 +1,31 @@ +//===- GPUToLLVMIRTranslation.h - GPU Dialect to LLVM IR --------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This provides registration calls for GPU dialect to LLVM IR translation. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_TARGET_LLVMIR_DIALECT_GPU_GPUTOLLVMIRTRANSLATION_H +#define MLIR_TARGET_LLVMIR_DIALECT_GPU_GPUTOLLVMIRTRANSLATION_H + +namespace mlir { + +class DialectRegistry; +class MLIRContext; + +/// Register the GPU dialect and the translation from it to the LLVM IR in +/// the given registry; +void registerGPUDialectTranslation(DialectRegistry ®istry); + +/// Register the GPU dialect and the translation from it in the registry +/// associated with the given context. +void registerGPUDialectTranslation(MLIRContext &context); + +} // namespace mlir + +#endif // MLIR_TARGET_LLVMIR_DIALECT_GPU_GPUTOLLVMIRTRANSLATION_H diff --git a/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt b/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt index 105ce24..0be8f2a 100644 --- a/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt +++ b/mlir/lib/CAPI/ExecutionEngine/CMakeLists.txt @@ -8,6 +8,7 @@ add_mlir_upstream_c_api_library(MLIRCAPIExecutionEngine ExecutionEngine.cpp LINK_LIBS PUBLIC + MLIRBuiltinToLLVMIRTranslation MLIRExecutionEngine MLIRLLVMToLLVMIRTranslation ) diff --git a/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp b/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp index a832119..a0ea7f4 100644 --- a/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp +++ b/mlir/lib/CAPI/ExecutionEngine/ExecutionEngine.cpp @@ -11,6 +11,7 @@ #include "mlir/CAPI/IR.h" #include "mlir/CAPI/Support.h" #include "mlir/ExecutionEngine/OptUtils.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "llvm/ExecutionEngine/Orc/Mangling.h" #include "llvm/Support/TargetSelect.h" @@ -29,7 +30,9 @@ mlirExecutionEngineCreate(MlirModule op, int optLevel, int numPaths, }(); (void)initOnce; - mlir::registerLLVMDialectTranslation(*unwrap(op)->getContext()); + auto &ctx = *unwrap(op)->getContext(); + mlir::registerBuiltinDialectTranslation(ctx); + mlir::registerLLVMDialectTranslation(ctx); auto tmBuilderOrError = llvm::orc::JITTargetMachineBuilder::detectHost(); if (!tmBuilderOrError) { diff --git a/mlir/lib/CAPI/RegisterEverything/CMakeLists.txt b/mlir/lib/CAPI/RegisterEverything/CMakeLists.txt index 942bba8..55fe49b 100644 --- a/mlir/lib/CAPI/RegisterEverything/CMakeLists.txt +++ b/mlir/lib/CAPI/RegisterEverything/CMakeLists.txt @@ -10,6 +10,7 @@ add_mlir_upstream_c_api_library(MLIRCAPIRegisterEverything ${translation_libs} ${conversion_libs} + MLIRBuiltinToLLVMIRTranslation MLIRCAPIIR MLIRLLVMToLLVMIRTranslation MLIRCAPITransforms diff --git a/mlir/lib/CAPI/RegisterEverything/RegisterEverything.cpp b/mlir/lib/CAPI/RegisterEverything/RegisterEverything.cpp index 25a1a21..e4a7516 100644 --- a/mlir/lib/CAPI/RegisterEverything/RegisterEverything.cpp +++ b/mlir/lib/CAPI/RegisterEverything/RegisterEverything.cpp @@ -11,6 +11,7 @@ #include "mlir/CAPI/IR.h" #include "mlir/InitAllDialects.h" #include "mlir/InitAllPasses.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" void mlirRegisterAllDialects(MlirDialectRegistry registry) { @@ -18,7 +19,9 @@ void mlirRegisterAllDialects(MlirDialectRegistry registry) { } void mlirRegisterAllLLVMTranslations(MlirContext context) { - mlir::registerLLVMDialectTranslation(*unwrap(context)); + auto &ctx = *unwrap(context); + mlir::registerBuiltinDialectTranslation(ctx); + mlir::registerLLVMDialectTranslation(ctx); } void mlirRegisterAllPasses() { mlir::registerAllPasses(); } diff --git a/mlir/lib/Dialect/GPU/CMakeLists.txt b/mlir/lib/Dialect/GPU/CMakeLists.txt index 94f3ab5..50e4982 100644 --- a/mlir/lib/Dialect/GPU/CMakeLists.txt +++ b/mlir/lib/Dialect/GPU/CMakeLists.txt @@ -70,11 +70,13 @@ add_mlir_dialect_library(MLIRGPUTransforms MLIRAffineUtils MLIRArithDialect MLIRAsyncDialect + MLIRBuiltinToLLVMIRTranslation MLIRDataLayoutInterfaces MLIRExecutionEngineUtils MLIRGPUOps MLIRIR MLIRLLVMDialect + MLIRGPUToLLVMIRTranslation MLIRLLVMToLLVMIRTranslation MLIRMemRefDialect MLIRPass diff --git a/mlir/lib/Dialect/GPU/Transforms/SerializeToBlob.cpp b/mlir/lib/Dialect/GPU/Transforms/SerializeToBlob.cpp index 3c4f005..d82e6ca 100644 --- a/mlir/lib/Dialect/GPU/Transforms/SerializeToBlob.cpp +++ b/mlir/lib/Dialect/GPU/Transforms/SerializeToBlob.cpp @@ -14,6 +14,7 @@ #include "mlir/Dialect/GPU/Transforms/Passes.h" #include "mlir/Pass/Pass.h" +#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Export.h" #include "llvm/IR/LegacyPassManager.h" @@ -108,6 +109,7 @@ gpu::SerializeToBlobPass::optimizeLlvm(llvm::Module &llvmModule, void gpu::SerializeToBlobPass::getDependentDialects( DialectRegistry ®istry) const { + registerGPUDialectTranslation(registry); registerLLVMDialectTranslation(registry); OperationPass::getDependentDialects(registry); } diff --git a/mlir/lib/ExecutionEngine/CMakeLists.txt b/mlir/lib/ExecutionEngine/CMakeLists.txt index a212e1a..5ed7af6 100644 --- a/mlir/lib/ExecutionEngine/CMakeLists.txt +++ b/mlir/lib/ExecutionEngine/CMakeLists.txt @@ -86,6 +86,7 @@ add_mlir_library(MLIRExecutionEngine ${LLVM_JIT_LISTENER_LIB} LINK_LIBS PUBLIC + MLIRBuiltinToLLVMIRTranslation MLIRExecutionEngineUtils MLIRLLVMDialect MLIRLLVMToLLVMIRTranslation diff --git a/mlir/lib/Target/LLVMIR/CMakeLists.txt b/mlir/lib/Target/LLVMIR/CMakeLists.txt index 7411573..da28c36 100644 --- a/mlir/lib/Target/LLVMIR/CMakeLists.txt +++ b/mlir/lib/Target/LLVMIR/CMakeLists.txt @@ -47,6 +47,8 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration MLIRArmNeonToLLVMIRTranslation MLIRArmSVEToLLVMIRTranslation MLIRAMXToLLVMIRTranslation + MLIRBuiltinToLLVMIRTranslation + MLIRGPUToLLVMIRTranslation MLIRX86VectorToLLVMIRTranslation MLIRLLVMToLLVMIRTranslation MLIRNVVMToLLVMIRTranslation diff --git a/mlir/lib/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.cpp new file mode 100644 index 0000000..51c304c --- /dev/null +++ b/mlir/lib/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.cpp @@ -0,0 +1,46 @@ +//===- BuiltinToLLVMIRTranslation.cpp - Translate builtin to LLVM IR ------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file implements a translation between the MLIR builtin dialect and LLVM +// IR. +// +//===----------------------------------------------------------------------===// +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" +#include "mlir/IR/BuiltinDialect.h" +#include "mlir/IR/BuiltinOps.h" +#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" + +using namespace mlir; + +namespace { + +class BuiltinDialectLLVMIRTranslationInterface + : public LLVMTranslationDialectInterface { +public: + using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface; + + LogicalResult + convertOperation(Operation *op, llvm::IRBuilderBase &builder, + LLVM::ModuleTranslation &moduleTranslation) const override { + return success(isa(op)); + } +}; + +} // namespace + +void mlir::registerBuiltinDialectTranslation(DialectRegistry ®istry) { + registry.addExtension(+[](MLIRContext *ctx, BuiltinDialect *dialect) { + dialect->addInterfaces(); + }); +} + +void mlir::registerBuiltinDialectTranslation(MLIRContext &context) { + DialectRegistry registry; + registerBuiltinDialectTranslation(registry); + context.appendDialectRegistry(registry); +} diff --git a/mlir/lib/Target/LLVMIR/Dialect/Builtin/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/Builtin/CMakeLists.txt new file mode 100644 index 0000000..e463969 --- /dev/null +++ b/mlir/lib/Target/LLVMIR/Dialect/Builtin/CMakeLists.txt @@ -0,0 +1,12 @@ +add_mlir_translation_library(MLIRBuiltinToLLVMIRTranslation + BuiltinToLLVMIRTranslation.cpp + + LINK_COMPONENTS + Core + + LINK_LIBS PUBLIC + MLIRIR + MLIRLLVMDialect + MLIRSupport + MLIRTargetLLVMIRExport + ) diff --git a/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt index 8df5176..f27810f 100644 --- a/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt +++ b/mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt @@ -1,6 +1,8 @@ add_subdirectory(ArmNeon) add_subdirectory(ArmSVE) add_subdirectory(AMX) +add_subdirectory(Builtin) +add_subdirectory(GPU) add_subdirectory(LLVMIR) add_subdirectory(NVVM) add_subdirectory(OpenACC) diff --git a/mlir/lib/Target/LLVMIR/Dialect/GPU/CMakeLists.txt b/mlir/lib/Target/LLVMIR/Dialect/GPU/CMakeLists.txt new file mode 100644 index 0000000..88e9990 --- /dev/null +++ b/mlir/lib/Target/LLVMIR/Dialect/GPU/CMakeLists.txt @@ -0,0 +1,13 @@ +add_mlir_translation_library(MLIRGPUToLLVMIRTranslation + GPUToLLVMIRTranslation.cpp + + LINK_COMPONENTS + Core + + LINK_LIBS PUBLIC + MLIRIR + MLIRGPUOps + MLIRLLVMDialect + MLIRSupport + MLIRTargetLLVMIRExport + ) diff --git a/mlir/lib/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.cpp new file mode 100644 index 0000000..a123161 --- /dev/null +++ b/mlir/lib/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.cpp @@ -0,0 +1,45 @@ +//===- GPUToLLVMIRTranslation.cpp - Translate GPU dialect to LLVM IR ------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file implements a translation between the MLIR GPU dialect and LLVM IR. +// +//===----------------------------------------------------------------------===// +#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h" +#include "mlir/Dialect/GPU/IR/GPUDialect.h" +#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" + +using namespace mlir; + +namespace { + +class GPUDialectLLVMIRTranslationInterface + : public LLVMTranslationDialectInterface { +public: + using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface; + + LogicalResult + convertOperation(Operation *op, llvm::IRBuilderBase &builder, + LLVM::ModuleTranslation &moduleTranslation) const override { + return isa(op) ? success() : failure(); + } +}; + +} // namespace + +void mlir::registerGPUDialectTranslation(DialectRegistry ®istry) { + registry.insert(); + registry.addExtension(+[](MLIRContext *ctx, gpu::GPUDialect *dialect) { + dialect->addInterfaces(); + }); +} + +void mlir::registerGPUDialectTranslation(MLIRContext &context) { + DialectRegistry registry; + registerGPUDialectTranslation(registry); + context.appendDialectRegistry(registry); +} diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp index 0ce7d4f..9b0bfa2 100644 --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -1320,6 +1320,10 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, } } + // Convert module itself. + if (failed(translator.convertOperation(*module, llvmBuilder))) + return nullptr; + if (llvm::verifyModule(*translator.llvmModule, &llvm::errs())) return nullptr; diff --git a/mlir/tools/mlir-cpu-runner/CMakeLists.txt b/mlir/tools/mlir-cpu-runner/CMakeLists.txt index 092ef1b..c2ab62e 100644 --- a/mlir/tools/mlir-cpu-runner/CMakeLists.txt +++ b/mlir/tools/mlir-cpu-runner/CMakeLists.txt @@ -11,6 +11,7 @@ add_mlir_tool(mlir-cpu-runner llvm_update_compile_flags(mlir-cpu-runner) target_link_libraries(mlir-cpu-runner PRIVATE MLIRAnalysis + MLIRBuiltinToLLVMIRTranslation MLIRExecutionEngine MLIRIR MLIRJitRunner diff --git a/mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt b/mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt index 60951ad..789c94b 100644 --- a/mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt +++ b/mlir/tools/mlir-spirv-cpu-runner/CMakeLists.txt @@ -17,6 +17,7 @@ if (MLIR_ENABLE_SPIRV_CPU_RUNNER) ${conversion_libs} MLIRAnalysis MLIRArithDialect + MLIRBuiltinToLLVMIRTranslation MLIRExecutionEngine MLIRFuncDialect MLIRGPUOps diff --git a/mlir/tools/mlir-vulkan-runner/CMakeLists.txt b/mlir/tools/mlir-vulkan-runner/CMakeLists.txt index 4f64117..a24ac08 100644 --- a/mlir/tools/mlir-vulkan-runner/CMakeLists.txt +++ b/mlir/tools/mlir-vulkan-runner/CMakeLists.txt @@ -57,6 +57,7 @@ if (MLIR_ENABLE_VULKAN_RUNNER) ${conversion_libs} MLIRAnalysis MLIRArithDialect + MLIRBuiltinToLLVMIRTranslation MLIRExecutionEngine MLIRFuncDialect MLIRGPUOps diff --git a/mlir/unittests/ExecutionEngine/Invoke.cpp b/mlir/unittests/ExecutionEngine/Invoke.cpp index 0f9212d..e726d93 100644 --- a/mlir/unittests/ExecutionEngine/Invoke.cpp +++ b/mlir/unittests/ExecutionEngine/Invoke.cpp @@ -23,6 +23,7 @@ #include "mlir/InitAllDialects.h" #include "mlir/Parser/Parser.h" #include "mlir/Pass/PassManager.h" +#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" #include "mlir/Target/LLVMIR/Export.h" #include "llvm/Support/TargetSelect.h" @@ -69,6 +70,7 @@ TEST(MLIRExecutionEngine, SKIP_WITHOUT_JIT(AddInteger)) { )mlir"; DialectRegistry registry; registerAllDialects(registry); + registerBuiltinDialectTranslation(registry); registerLLVMDialectTranslation(registry); MLIRContext context(registry); OwningOpRef module = @@ -95,6 +97,7 @@ TEST(MLIRExecutionEngine, SKIP_WITHOUT_JIT(SubtractFloat)) { )mlir"; DialectRegistry registry; registerAllDialects(registry); + registerBuiltinDialectTranslation(registry); registerLLVMDialectTranslation(registry); MLIRContext context(registry); OwningOpRef module = @@ -126,6 +129,7 @@ TEST(NativeMemRefJit, SKIP_WITHOUT_JIT(ZeroRankMemref)) { )mlir"; DialectRegistry registry; registerAllDialects(registry); + registerBuiltinDialectTranslation(registry); registerLLVMDialectTranslation(registry); MLIRContext context(registry); auto module = parseSourceString(moduleStr, &context); @@ -161,6 +165,7 @@ TEST(NativeMemRefJit, SKIP_WITHOUT_JIT(RankOneMemref)) { )mlir"; DialectRegistry registry; registerAllDialects(registry); + registerBuiltinDialectTranslation(registry); registerLLVMDialectTranslation(registry); MLIRContext context(registry); auto module = parseSourceString(moduleStr, &context); @@ -215,6 +220,7 @@ TEST(NativeMemRefJit, SKIP_WITHOUT_JIT(BasicMemref)) { )mlir"; DialectRegistry registry; registerAllDialects(registry); + registerBuiltinDialectTranslation(registry); registerLLVMDialectTranslation(registry); MLIRContext context(registry); OwningOpRef module = @@ -264,6 +270,7 @@ TEST(NativeMemRefJit, MAYBE_JITCallback) { )mlir"; DialectRegistry registry; registerAllDialects(registry); + registerBuiltinDialectTranslation(registry); registerLLVMDialectTranslation(registry); MLIRContext context(registry); auto module = parseSourceString(moduleStr, &context); -- 2.7.4