From d47cf332cc45201702c9cc7ae87b23198c5ed32d Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Fri, 25 Sep 2020 15:40:05 +0800 Subject: [PATCH] build(third_party): update llvm-project and adapt to mlir interface changes GitOrigin-RevId: dd45984ccaeef39094f9a209abe490d10e54a77e --- cmake/llvm-project.cmake | 14 +++++++++----- src/jit/impl/mlir/compiler.cpp | 5 +++-- src/jit/impl/mlir/ir/dialect.cpp | 3 ++- src/jit/impl/mlir/ir/lower_to_affine_pass.cpp | 5 +++++ src/jit/impl/mlir/ir/lower_to_gpu_pass.cpp | 5 +++++ src/jit/impl/mlir/ir/lower_to_llvm_pass.cpp | 8 ++++++++ src/jit/impl/mlir/ir/types.h | 2 +- src/jit/impl/mlir/ir/utils.cpp | 13 ++++++------- src/jit/include/megbrain/jit/mlir/ir/dialect.h | 6 +++--- 9 files changed, 42 insertions(+), 19 deletions(-) diff --git a/cmake/llvm-project.cmake b/cmake/llvm-project.cmake index 561435c5..94df3879 100644 --- a/cmake/llvm-project.cmake +++ b/cmake/llvm-project.cmake @@ -49,6 +49,14 @@ function(external_tablegen_library) install(TARGETS ${_NAME} EXPORT ${MGE_EXPORT_TARGETS}) endfunction() +set(LLVM_LIBS LLVMCore LLVMSupport LLVMX86CodeGen LLVMOrcJIT LLVMNVPTXCodeGen LLVMNVPTXDesc LLVMNVPTXInfo) +set(MLIR_CORE_LIBS MLIRAnalysis MLIRExecutionEngine MLIRIR MLIRParser MLIRPass MLIRSideEffectInterfaces MLIRTransforms) +set(MLIR_DIALECT_LIBS MLIRAsync MLIRAVX512 MLIRGPU MLIRLLVMAVX512 MLIRNVVMIR MLIROpenACC MLIRPDL MLIRPDLInterp MLIRQuant MLIRROCDLIR MLIRSDBM MLIRShape MLIRSPIRV MLIRStandardOpsTransforms) +set(MLIR_CONVERSION_LIBS MLIRAffineToStandard MLIRAVX512ToLLVM MLIRGPUToGPURuntimeTransforms MLIRGPUToNVVMTransforms MLIRSCFToStandard) +set(MLIR_TRANSLATION_LIBS MLIRTargetLLVMIR MLIRTargetNVVMIR) +set(MLIR_LIBS ${MLIR_CORE_LIBS} ${MLIR_DIALECT_LIBS} ${MLIR_CONVERSION_LIBS} ${MLIR_TRANSLATION_LIBS}) +set(MLIR_LLVM_LIBS ${LLVM_LIBS} ${MLIR_LIBS}) + if (MGE_USE_SYSTEM_LIB) find_package(ZLIB) find_package(MLIR REQUIRED CONFIG) @@ -77,9 +85,7 @@ if (MGE_USE_SYSTEM_LIB) endif() endfunction(find_mlir_llvm_lib) - set(MLIR_COMPONENTS MLIRAnalysis;MLIRExecutionEngine;MLIRIR;MLIRParser;MLIRPass;MLIRSideEffectInterfaces;MLIRTargetLLVMIR;MLIRTransforms;MLIRAffineToStandard;MLIRSCFToStandard;MLIRAVX512ToLLVM;MLIRAVX512;MLIRLLVMAVX512;MLIRSDBM;MLIRROCDLIR;MLIRGPU;MLIRQuant;MLIRSPIRV;MLIRNVVMIR;MLIRShape;MLIRGPUToNVVMTransforms;MLIRTargetNVVMIR;MLIRGPUToGPURuntimeTransforms;MLIRStandardOpsTransforms) - - foreach(c ${MLIR_COMPONENTS}) + foreach(c ${MLIR_LIBS}) find_mlir_llvm_lib(${c}) endforeach() return() @@ -119,5 +125,3 @@ set(MLIR_LLVM_INCLUDE_DIR ${PROJECT_BINARY_DIR}/third_party/llvm-project/llvm/tools/mlir/include ) set(MLIR_TABLEGEN_EXE mlir-tblgen) - -set(MLIR_LLVM_LIBS LLVMCore;LLVMSupport;LLVMX86CodeGen;LLVMOrcJIT;LLVMNVPTXCodeGen;LLVMNVPTXDesc;LLVMNVPTXInfo;MLIRAnalysis;MLIRExecutionEngine;MLIRIR;MLIRParser;MLIRPass;MLIRSideEffectInterfaces;MLIRTargetLLVMIR;MLIRTransforms;MLIRAffineToStandard;MLIRSCFToStandard;MLIRAVX512ToLLVM;MLIRAVX512;MLIRLLVMAVX512;MLIRSDBM;MLIRROCDLIR;MLIRGPU;MLIRQuant;MLIRSPIRV;MLIRNVVMIR;MLIRGPUToNVVMTransforms;MLIRShape;MLIRTargetNVVMIR;MLIRGPUToGPURuntimeTransforms;MLIRStandardOpsTransforms) diff --git a/src/jit/impl/mlir/compiler.cpp b/src/jit/impl/mlir/compiler.cpp index 1be70411..6f4588d1 100644 --- a/src/jit/impl/mlir/compiler.cpp +++ b/src/jit/impl/mlir/compiler.cpp @@ -67,8 +67,8 @@ mlir::OwnedBlob compile_ptx_to_cubin(const std::string ptx, mlir::Location, } std::unique_ptr translate_module_to_nvvm_ir_and_link_device( - Operation* m) { - std::unique_ptr module = mlir::translateModuleToNVVMIR(m); + Operation* m, llvm::LLVMContext& llvmContext, llvm::StringRef name) { + std::unique_ptr module = mlir::translateModuleToNVVMIR(m, llvmContext); auto get_device_path = []() -> std::string { auto cuda_path = getenv("CUDA_BIN_PATH"); std::string device_dir; @@ -223,6 +223,7 @@ void MLIRCompiler::run_lowering_pass(mlir::OwningModuleRef& module, std::unique_ptr MLIRCompiler::do_compile( const InternalGraph& graph, const JITExecutor::Args& args) { mlir::MLIRContext ctx; + ctx.getOrLoadDialect(); ctx.printStackTraceOnDiagnostic(true); ctx.printOpOnDiagnostic(true); diff --git a/src/jit/impl/mlir/ir/dialect.cpp b/src/jit/impl/mlir/ir/dialect.cpp index e877b5a6..c4a82bfd 100644 --- a/src/jit/impl/mlir/ir/dialect.cpp +++ b/src/jit/impl/mlir/ir/dialect.cpp @@ -24,7 +24,8 @@ using namespace mgb; using namespace jit; -MgbDialect::MgbDialect(mlir::MLIRContext* ctx) : mlir::Dialect("mgb", ctx) { +MgbDialect::MgbDialect(mlir::MLIRContext* ctx) + : mlir::Dialect("mgb", ctx, mlir::TypeID::get()) { addOperations< #define GET_OP_LIST #include "megbrain/jit/mlir/ir/ops.cpp.inc" diff --git a/src/jit/impl/mlir/ir/lower_to_affine_pass.cpp b/src/jit/impl/mlir/ir/lower_to_affine_pass.cpp index a63a556e..32c7bcff 100644 --- a/src/jit/impl/mlir/ir/lower_to_affine_pass.cpp +++ b/src/jit/impl/mlir/ir/lower_to_affine_pass.cpp @@ -209,6 +209,11 @@ struct ConstantScalarOpLowering class MgbToAffineLoweringPass : public PassWrapper { public: + void getDependentDialects(mlir::DialectRegistry& registry) const override { + registry.insert(); + registry.insert(); + } + void runOnFunction() override final { ConversionTarget target(getContext()); target.addLegalDialect(); diff --git a/src/jit/impl/mlir/ir/lower_to_gpu_pass.cpp b/src/jit/impl/mlir/ir/lower_to_gpu_pass.cpp index 525f256e..e0b35b66 100644 --- a/src/jit/impl/mlir/ir/lower_to_gpu_pass.cpp +++ b/src/jit/impl/mlir/ir/lower_to_gpu_pass.cpp @@ -259,6 +259,11 @@ private: class MgbToGpuLoweringPass : public PassWrapper { public: + void getDependentDialects(mlir::DialectRegistry& registry) const override { + registry.insert(); + registry.insert(); + } + void runOnFunction() override final { auto func_op = getFunction(); Location loc = func_op.getLoc(); diff --git a/src/jit/impl/mlir/ir/lower_to_llvm_pass.cpp b/src/jit/impl/mlir/ir/lower_to_llvm_pass.cpp index 9e17e0c1..edacdbcb 100644 --- a/src/jit/impl/mlir/ir/lower_to_llvm_pass.cpp +++ b/src/jit/impl/mlir/ir/lower_to_llvm_pass.cpp @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include using namespace mgb; @@ -30,6 +32,12 @@ namespace { class AffineToLLVMLoweringPass : public PassWrapper> { +public: + void getDependentDialects(mlir::DialectRegistry& registry) const override { + registry.insert(); + registry.insert(); + } + void runOnOperation() final { LLVMConversionTarget target(getContext()); target.addLegalOp(); diff --git a/src/jit/impl/mlir/ir/types.h b/src/jit/impl/mlir/ir/types.h index 548b5db4..ee8a1f61 100644 --- a/src/jit/impl/mlir/ir/types.h +++ b/src/jit/impl/mlir/ir/types.h @@ -21,7 +21,7 @@ namespace jit { inline bool is_elemwise_float(const mlir::Type& dt) { if (auto cast = dt.dyn_cast_or_null()) { - if (cast.getElementType().getKind() == mlir::StandardTypes::F32) { + if (cast.getElementType().isF32()) { return true; } } diff --git a/src/jit/impl/mlir/ir/utils.cpp b/src/jit/impl/mlir/ir/utils.cpp index f3553b98..afdb25e8 100644 --- a/src/jit/impl/mlir/ir/utils.cpp +++ b/src/jit/impl/mlir/ir/utils.cpp @@ -82,13 +82,12 @@ megdnn::DType jit::mlir_type_to_dtype(mlir::Type type) { if (auto cast = type.dyn_cast_or_null()) { element_type = cast.getElementType(); } - switch (element_type.getKind()) { - case mlir::StandardTypes::F32: - return megdnn::dtype::Float32{}; - default: - mgb_throw(InternalError, - "Unsupport mlir type for MemRefType, got: %s\n", - mlir_type_to_string(type).c_str()); + if (element_type.isF32()) { + return megdnn::dtype::Float32{}; + } else { + mgb_throw(InternalError, + "Unsupport mlir type for MemRefType, got: %s\n", + mlir_type_to_string(type).c_str()); } return {}; } diff --git a/src/jit/include/megbrain/jit/mlir/ir/dialect.h b/src/jit/include/megbrain/jit/mlir/ir/dialect.h index f0ee5fe8..d1c93e9b 100644 --- a/src/jit/include/megbrain/jit/mlir/ir/dialect.h +++ b/src/jit/include/megbrain/jit/mlir/ir/dialect.h @@ -34,13 +34,13 @@ public: static llvm::StringRef getDialectNamespace() { return "mgb::jit"; } }; +} // namespace jit +} // namespace mgb + #define GET_OP_CLASSES using namespace mlir; #include "megbrain/jit/mlir/ir/ops.h.inc" -} // namespace jit -} // namespace mgb - #endif // MGB_JIT && MGB_JIT_MLIR // vim: syntax=cpp.doxygen -- GitLab