From b28c6906b4885fa3f1183be50b9f92f1aaff5494 Mon Sep 17 00:00:00 2001 From: Gheorghe-Teodor Bercea Date: Wed, 19 Feb 2020 18:15:02 -0500 Subject: [PATCH] Fix building ONNF with latest LLVM/MLIR (#89) * Fix build and link errors. * Fix end to end tests. * Fix indentation. * Fix type conversion. * Use newest LLVM version. * Use newest LLVM version. --- .circleci/config.yml | 4 ++-- MLIR.cmake | 12 +++++++++--- src/conversion/onnx_to_krnl/convert_onnx_to_krnl.cpp | 6 +++++- src/main.cpp | 5 +++++ src/tool/onnf_opt/onnf_opt.cpp | 5 +++++ src/transform/lower_to_llvm.cpp | 12 +++++++++--- test/mlir/krnl/reshape.mlir | 10 +++++++++- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5dc118d..48fda88 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -18,7 +18,7 @@ jobs: git submodule update --init --recursive # Use cached mlir installation if possible. - restore_cache: - key: V4-LLVM-PROJECT-{{ arch }} + key: V6-LLVM-PROJECT-{{ arch }} - run: name: Install MLIR command: | @@ -29,7 +29,7 @@ jobs: source ONNF/utils/install-mlir.sh fi - save_cache: - key: V4-LLVM-PROJECT-{{ arch }} + key: V6-LLVM-PROJECT-{{ arch }} paths: - llvm-project - run: diff --git a/MLIR.cmake b/MLIR.cmake index f7e153d..e330316 100644 --- a/MLIR.cmake +++ b/MLIR.cmake @@ -58,9 +58,11 @@ find_mlir_lib(MLIRAffineOps) find_mlir_lib(MLIRAffineToStandard) find_mlir_lib(MLIRAnalysis) find_mlir_lib(MLIRDialect) +find_mlir_lib(MLIREDSC) find_mlir_lib(MLIRExecutionEngine) find_mlir_lib(MLIRIR) find_mlir_lib(MLIRLLVMIR) +find_mlir_lib(MLIRLoopAnalysis) find_mlir_lib(MLIRLoopToStandard) find_mlir_lib(MLIRLoopOps) find_mlir_lib(MLIRParser) @@ -71,7 +73,8 @@ find_mlir_lib(MLIRTargetLLVMIR) find_mlir_lib(MLIRTransforms) find_mlir_lib(MLIRTransformUtils) find_mlir_lib(MLIRSupport) -find_mlir_lib(MLIROptMain) +find_mlir_lib(MLIRMlirOptMain) +find_mlir_lib(MLIROptLib) find_mlir_lib(MLIRTargetLLVMIRModuleTranslation) find_mlir_lib(MLIRTargetLLVMIR) find_mlir_lib(MLIRTransformUtils) @@ -117,12 +120,15 @@ set(MLIRLibsOnce ${MLIRAffineToStandard} ${MLIRAnalysis} ${MLIRDialect} + ${MLIREDSC} ${MLIRExecutionEngine} ${MLIRIR} ${MLIRLLVMIR} ${MLIRLoopToStandard} ${MLIRLoopOps} - ${MLIROptMain} + ${MLIRLoopAnalysis} + ${MLIRMlirOptMain} + ${MLIROptLib} ${MLIRParser} ${MLIRPass} ${MLIRStandardOps} @@ -226,4 +232,4 @@ function(add_onnf_dialect_doc dialect dialect_tablegen_file) add_dependencies(onnf-doc ${dialect}DocGen) endfunction() -add_custom_target(onnf-doc) \ No newline at end of file +add_custom_target(onnf-doc) diff --git a/src/conversion/onnx_to_krnl/convert_onnx_to_krnl.cpp b/src/conversion/onnx_to_krnl/convert_onnx_to_krnl.cpp index 9c9b826..542a2c1 100644 --- a/src/conversion/onnx_to_krnl/convert_onnx_to_krnl.cpp +++ b/src/conversion/onnx_to_krnl/convert_onnx_to_krnl.cpp @@ -425,7 +425,11 @@ public: struct TensorTypeConverter : public TypeConverter { using TypeConverter::TypeConverter; - LogicalResult convertType(Type t, SmallVectorImpl &results) override { + TensorTypeConverter() { + addConversion(convertType); + } + + static LogicalResult convertType(Type t, SmallVectorImpl &results) { if (auto tensor_type = t.dyn_cast()) { results.push_back(convertTensorToMemRef(tensor_type)); return success(); diff --git a/src/main.cpp b/src/main.cpp index f0de7e9..e3a36c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,7 @@ #include "mlir/Conversion/LoopToStandard/ConvertLoopToStandard.h" #include "mlir/ExecutionEngine/ExecutionEngine.h" #include "mlir/ExecutionEngine/OptUtils.h" +#include "mlir/InitAllDialects.h" #include "mlir/IR/MLIRContext.h" #include "mlir/IR/Module.h" #include "mlir/Parser.h" @@ -69,6 +70,10 @@ void EmitLLVMBitCode(const mlir::OwningModuleRef &module) { } int main(int argc, char *argv[]) { + mlir::registerDialect(); + mlir::registerDialect(); + mlir::registerDialect(); + mlir::registerDialect(); mlir::registerDialect(); mlir::registerDialect(); diff --git a/src/tool/onnf_opt/onnf_opt.cpp b/src/tool/onnf_opt/onnf_opt.cpp index 2311b66..597bfd4 100644 --- a/src/tool/onnf_opt/onnf_opt.cpp +++ b/src/tool/onnf_opt/onnf_opt.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,10 @@ static llvm::cl::opt verify_passes( llvm::cl::init(true)); int main(int argc, char **argv) { + mlir::registerDialect(); + mlir::registerDialect(); + mlir::registerDialect(); + mlir::registerDialect(); llvm::InitLLVM y(argc, argv); mlir::registerDialect(); diff --git a/src/transform/lower_to_llvm.cpp b/src/transform/lower_to_llvm.cpp index 7d01207..b4b46f4 100644 --- a/src/transform/lower_to_llvm.cpp +++ b/src/transform/lower_to_llvm.cpp @@ -224,7 +224,10 @@ public: // Based on the static entry point type signature, unpack dynamic memory // refs to corresponding static memory refs. - auto *staticEntryPointFunc = module.lookupSymbol(staticEntryPointFuncName); + auto wrappedStaticEntryPointFuncName = + "_mlir_ciface_" + staticEntryPointFuncName.lower(); + auto *staticEntryPointFunc = + module.lookupSymbol(wrappedStaticEntryPointFuncName); assert(staticEntryPointFunc && isa(staticEntryPointFunc) && "entry point func must exist and be an llvm func op"); @@ -268,7 +271,8 @@ public: // Call static entry point with the memref ptrs created, and get output. auto outputMemRefs = rewriter.create( loc, staticEntryPointTy.getFunctionResultType(), - rewriter.getSymbolRefAttr(staticEntryPointFuncName), staticInputs); + rewriter.getSymbolRefAttr(wrappedStaticEntryPointFuncName), + staticInputs); // Create wrapped output. auto wrappedOutput = callApi(rewriter, loc, apiRegistry, @@ -563,7 +567,9 @@ void KrnlToLLVMLoweringPass::runOnModule() { OwningRewritePatternList patterns; populateAffineToStdConversionPatterns(patterns, &getContext()); populateLoopToStdConversionPatterns(patterns, &getContext()); - populateStdToLLVMConversionPatterns(typeConverter, patterns); + populateStdToLLVMConversionPatterns(typeConverter, patterns, + /*useAlloca=*/false, + /*emitCWrapper=*/true); // Lower from the `krnl` dialect i.e. the Reshape operation. patterns.insert, %arg1 : tensor<4xi32>) -> tensor<*x "std.return"(%0) : (tensor<*xf32>) -> () // CHECK: llvm.func @llvm.memcpy.p0i8.p0i8.i64(!llvm<"i8*">, !llvm<"i8*">, !llvm.i64, !llvm.i1) + // CHECK: [[TMP:%.+]] = llvm.mlir.undef : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: llvm.insertvalue %arg0, %0[0] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: llvm.insertvalue %arg1, %1[1] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: llvm.insertvalue %arg2, %2[2] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: llvm.insertvalue %arg3, %3[3, 0] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: llvm.insertvalue %arg5, %4[4, 0] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: llvm.insertvalue %arg4, %5[3, 1] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: [[TMP1:%.+]] = llvm.insertvalue %arg6, %6[4, 1] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> // CHECK: [[RES:%.+]] = llvm.insertvalue {{.*}}[4, 3] : !llvm<"{ float*, float*, i64, [4 x i64], [4 x i64] }"> // CHECK: [[EXT_VAL_0:%.+]] = llvm.extractvalue [[RES]][1] : !llvm<"{ float*, float*, i64, [4 x i64], [4 x i64] }"> // CHECK: [[DST:%.+]] = llvm.bitcast [[EXT_VAL_0]] : !llvm<"float*"> to !llvm<"i8*"> - // CHECK: [[EXT_VAL_1:%.+]] = llvm.extractvalue %0[1] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> + // CHECK: [[EXT_VAL_1:%.+]] = llvm.extractvalue [[TMP1]][1] : !llvm<"{ float*, float*, i64, [2 x i64], [2 x i64] }"> // CHECK: [[SRC:%.+]] = llvm.bitcast [[EXT_VAL_1]] : !llvm<"float*"> to !llvm<"i8*"> // CHECK: [[SIZE:%.+]] = llvm.sext %{{.*}} : !llvm.i64 to !llvm.i64 // CHECK: [[VOLATILE:%.+]] = llvm.mlir.constant(0 : i1) : !llvm.i1