From 877b9236d1e7c50192bd6d1e747382e25a2b2776 Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Sat, 11 May 2024 16:38:23 +0800 Subject: [PATCH 1/8] add cpu runner --- CMakeLists.txt | 4 ++ src/CMakeLists.txt | 13 +----- src/gc-cpu-runner/CMakeLists.txt | 16 ++++++++ src/gc-cpu-runner/gc-cpu-runner.cpp | 40 +++++++++++++++++++ src/gc-opt/CMakeLists.txt | 11 +++++ src/{ => gc-opt}/gc-opt.cpp | 0 test/CMakeLists.txt | 1 + .../Dialect}/Linlagx/linalg-generalize.mlir | 0 test/gc/cpu-runner/run.mlir | 5 +++ test/lit.cfg.py | 4 +- 10 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 src/gc-cpu-runner/CMakeLists.txt create mode 100644 src/gc-cpu-runner/gc-cpu-runner.cpp create mode 100644 src/gc-opt/CMakeLists.txt rename src/{ => gc-opt}/gc-opt.cpp (100%) rename test/{gc-dialects => gc/Dialect}/Linlagx/linalg-generalize.mlir (100%) create mode 100644 test/gc/cpu-runner/run.mlir diff --git a/CMakeLists.txt b/CMakeLists.txt index 50839aa4b..8101d66ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,10 @@ find_package(MLIR REQUIRED CONFIG) message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") +set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin) +set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib) +set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR}) + list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42b2d03f7..f6298c270 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,15 +1,6 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) -set(gc_opt_libs - ${dialect_libs} - ${conversion_libs} - MLIROptLib - GCPasses) -add_llvm_executable(gc-opt gc-opt.cpp) - -target_link_libraries(gc-opt PRIVATE ${gc_opt_libs}) -llvm_update_compile_flags(gc-opt) -mlir_check_all_link_libraries(gc-opt) - add_subdirectory(dnnl) +add_subdirectory(gc-cpu-runner) +add_subdirectory(gc-opt) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt new file mode 100644 index 000000000..cee5b726c --- /dev/null +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -0,0 +1,16 @@ +set(LLVM_LINK_COMPONENTS + LLVM + ) +set(gc_cpu_runner_libs + ${dialect_libs} + ${conversion_libs} + ${LLVM_LINK_COMPONENTS} + MLIR + MLIRExecutionEngine + MLIRJitRunner + GCPasses) +add_llvm_executable(gc-cpu-runner gc-cpu-runner.cpp) + +target_link_libraries(gc-cpu-runner PRIVATE ${gc_cpu_runner_libs}) +llvm_update_compile_flags(gc-cpu-runner) +mlir_check_all_link_libraries(gc-cpu-runner) diff --git a/src/gc-cpu-runner/gc-cpu-runner.cpp b/src/gc-cpu-runner/gc-cpu-runner.cpp new file mode 100644 index 000000000..3ece8f2ff --- /dev/null +++ b/src/gc-cpu-runner/gc-cpu-runner.cpp @@ -0,0 +1,40 @@ + +/* + * Copyright (C) 2024 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/ExecutionEngine/JitRunner.h" +#include "mlir/ExecutionEngine/OptUtils.h" +#include "mlir/IR/Dialect.h" +#include "mlir/Target/LLVMIR/Dialect/All.h" + +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/TargetSelect.h" +#include + +int main(int argc, char **argv) { + llvm::InitLLVM y(argc, argv); + llvm::InitializeNativeTarget(); + llvm::InitializeNativeTargetAsmPrinter(); + llvm::InitializeNativeTargetAsmParser(); + + mlir::DialectRegistry registry; + mlir::registerAllToLLVMIRTranslations(registry); + + return mlir::JitRunnerMain(argc, argv, registry); +} \ No newline at end of file diff --git a/src/gc-opt/CMakeLists.txt b/src/gc-opt/CMakeLists.txt new file mode 100644 index 000000000..7093bfd48 --- /dev/null +++ b/src/gc-opt/CMakeLists.txt @@ -0,0 +1,11 @@ +set(gc_opt_libs + ${dialect_libs} + ${conversion_libs} + MLIROptLib + GCPasses) +add_llvm_executable(gc-opt gc-opt.cpp) + +target_link_libraries(gc-opt PRIVATE ${gc_opt_libs}) +llvm_update_compile_flags(gc-opt) +mlir_check_all_link_libraries(gc-opt) + diff --git a/src/gc-opt.cpp b/src/gc-opt/gc-opt.cpp similarity index 100% rename from src/gc-opt.cpp rename to src/gc-opt/gc-opt.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 98c7c8c44..b133f9b86 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,6 +22,7 @@ set(GC_OPT_TEST_DEPENDS FileCheck count not # mlir-gen gc-opt + gc-cpu-runner ) add_lit_testsuite(gc-check "Running the regression tests" diff --git a/test/gc-dialects/Linlagx/linalg-generalize.mlir b/test/gc/Dialect/Linlagx/linalg-generalize.mlir similarity index 100% rename from test/gc-dialects/Linlagx/linalg-generalize.mlir rename to test/gc/Dialect/Linlagx/linalg-generalize.mlir diff --git a/test/gc/cpu-runner/run.mlir b/test/gc/cpu-runner/run.mlir new file mode 100644 index 000000000..406bf68d9 --- /dev/null +++ b/test/gc/cpu-runner/run.mlir @@ -0,0 +1,5 @@ +// RUN: gc-opt %s --convert-func-to-llvm --convert-cf-to-llvm | gc-cpu-runner -e main -entry-point-result=void | FileCheck --allow-empty %s +func.func @main() { + return +} +// CHECK-NOT: any \ No newline at end of file diff --git a/test/lit.cfg.py b/test/lit.cfg.py index c6867ec2a..0f67b5115 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -41,12 +41,12 @@ # test_exec_root: The root path where tests should be run. config.test_exec_root = os.path.join(config.gc_obj_root, "test") -config.gc_tools_dir = os.path.join(config.gc_obj_root, "src") +config.gc_tools_dir = os.path.join(config.gc_obj_root, "bin") # Tweak the PATH to include the tools dir. llvm_config.with_environment("PATH", config.llvm_tools_dir, append_path=True) tool_dirs = [config.gc_tools_dir, config.llvm_tools_dir] -tools = ["gc-opt"] +tools = ["gc-opt", "gc-cpu-runner"] llvm_config.add_tool_substitutions(tools, tool_dirs) From 096a88c56008bd7528b918e7e0c587bf65340bdc Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 11:42:34 +0800 Subject: [PATCH 2/8] dyn link --- CMakeLists.txt | 1 + src/gc-cpu-runner/CMakeLists.txt | 38 ++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f114f4c9..9be05eeab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") option(GC_LEGACY_ENABLE ON) option(GC_TEST_ENABLE "Build the tests" ON) +option(GC_LINK_DYNAMIC_LLVM "Link dynamic libraries of LLVM and MLIR" OFF) if(GC_LEGACY_ENABLE) add_subdirectory(legacy/core) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index 6a4410092..1b210d41d 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -1,18 +1,38 @@ -set(LLVM_LINK_COMPONENTS - LLVM +if(GC_LINK_DYNAMIC_LLVM) + set(LLVM_LINK_COMPONENTS + LLVM + ) + set(MLIR_LINK_COMPONENTS + MLIR + MLIRExecutionEngine + MLIRJitRunner ) +else() + set(LLVM_LINK_COMPONENTS + "" + ) + set(MLIR_LINK_COMPONENTS + MLIRAnalysis + MLIRBuiltinToLLVMIRTranslation + MLIRExecutionEngine + MLIRIR + MLIRJitRunner + MLIRLLVMDialect + MLIRLLVMToLLVMIRTranslation + MLIRToLLVMIRTranslationRegistration + MLIRParser + MLIRTargetLLVMIRExport + MLIRSupport + ) +endif() + if(GC_MLIR_CXX_FLAGS) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GC_MLIR_CXX_FLAGS}") endif() -set(gc_cpu_runner_libs - ${dialect_libs} - ${conversion_libs} +set(gc_cpu_runner_libs ${LLVM_LINK_COMPONENTS} - MLIR - MLIRExecutionEngine - MLIRJitRunner - GCPasses) + ${MLIR_LINK_COMPONENTS}) add_llvm_executable(gc-cpu-runner gc-cpu-runner.cpp) target_link_libraries(gc-cpu-runner PRIVATE ${gc_cpu_runner_libs}) From e752dd60536ce14ff71045974a7d1fa8369f6066 Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 13:33:58 +0800 Subject: [PATCH 3/8] fix --- src/gc-cpu-runner/CMakeLists.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index 1b210d41d..d1b3fbfd4 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -8,9 +8,12 @@ if(GC_LINK_DYNAMIC_LLVM) MLIRJitRunner ) else() - set(LLVM_LINK_COMPONENTS - "" - ) + if(NOT LLVM_LINK_LLVM_DYLIB) + llvm_map_components_to_libnames(llvm_libs nativecodegen) + set(LLVM_LINK_COMPONENTS + ${llvm_libs} + ) + endif() set(MLIR_LINK_COMPONENTS MLIRAnalysis MLIRBuiltinToLLVMIRTranslation From dd39dc0f5d3e1eaef869836e3ae27b9d616224de Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 14:09:19 +0800 Subject: [PATCH 4/8] fix --- src/gc-cpu-runner/CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index d1b3fbfd4..3ee8a5ee6 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -9,9 +9,11 @@ if(GC_LINK_DYNAMIC_LLVM) ) else() if(NOT LLVM_LINK_LLVM_DYLIB) - llvm_map_components_to_libnames(llvm_libs nativecodegen) set(LLVM_LINK_COMPONENTS - ${llvm_libs} + Core + Support + nativecodegen + native ) endif() set(MLIR_LINK_COMPONENTS From e96b0403decf15f7f0c9987c3195567052aafe57 Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 14:29:38 +0800 Subject: [PATCH 5/8] fix --- src/gc-cpu-runner/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index 3ee8a5ee6..9f886fa11 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -35,8 +35,8 @@ if(GC_MLIR_CXX_FLAGS) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GC_MLIR_CXX_FLAGS}") endif() +#LLVM_LINK_COMPONENTS is processed by LLVM cmake in add_llvm_executable set(gc_cpu_runner_libs - ${LLVM_LINK_COMPONENTS} ${MLIR_LINK_COMPONENTS}) add_llvm_executable(gc-cpu-runner gc-cpu-runner.cpp) From b975989569de88b60c562d21f76fad90af1e88ea Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 15:08:41 +0800 Subject: [PATCH 6/8] fix --- CMakeLists.txt | 2 +- src/gc-cpu-runner/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9be05eeab..4c2d08972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") option(GC_LEGACY_ENABLE ON) option(GC_TEST_ENABLE "Build the tests" ON) -option(GC_LINK_DYNAMIC_LLVM "Link dynamic libraries of LLVM and MLIR" OFF) +option(GC_DEV_LINK_DYN_LLVM "Link dynamic libraries of LLVM and MLIR. For developers only. Do not use it in packing the library." OFF) if(GC_LEGACY_ENABLE) add_subdirectory(legacy/core) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index 9f886fa11..b05058371 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -1,4 +1,4 @@ -if(GC_LINK_DYNAMIC_LLVM) +if(GC_DEV_LINK_DYN_LLVM) set(LLVM_LINK_COMPONENTS LLVM ) From 0332c274337c897f18b8eaef2fc759919c39540c Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 15:33:07 +0800 Subject: [PATCH 7/8] add link --- CMakeLists.txt | 1 - src/gc-cpu-runner/CMakeLists.txt | 48 +++++++++++++------------------- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c2d08972..5f114f4c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,6 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") option(GC_LEGACY_ENABLE ON) option(GC_TEST_ENABLE "Build the tests" ON) -option(GC_DEV_LINK_DYN_LLVM "Link dynamic libraries of LLVM and MLIR. For developers only. Do not use it in packing the library." OFF) if(GC_LEGACY_ENABLE) add_subdirectory(legacy/core) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index b05058371..b4cc13b78 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -1,35 +1,25 @@ -if(GC_DEV_LINK_DYN_LLVM) +# the dependency list copied from mlir/tools/mlir-cpu-runner/CMakeLists.txt of upstream +if(NOT LLVM_LINK_LLVM_DYLIB) set(LLVM_LINK_COMPONENTS - LLVM - ) - set(MLIR_LINK_COMPONENTS - MLIR - MLIRExecutionEngine - MLIRJitRunner - ) -else() - if(NOT LLVM_LINK_LLVM_DYLIB) - set(LLVM_LINK_COMPONENTS - Core - Support - nativecodegen - native - ) - endif() - set(MLIR_LINK_COMPONENTS - MLIRAnalysis - MLIRBuiltinToLLVMIRTranslation - MLIRExecutionEngine - MLIRIR - MLIRJitRunner - MLIRLLVMDialect - MLIRLLVMToLLVMIRTranslation - MLIRToLLVMIRTranslationRegistration - MLIRParser - MLIRTargetLLVMIRExport - MLIRSupport + Core + Support + nativecodegen + native ) endif() +set(MLIR_LINK_COMPONENTS + MLIRAnalysis + MLIRBuiltinToLLVMIRTranslation + MLIRExecutionEngine + MLIRIR + MLIRJitRunner + MLIRLLVMDialect + MLIRLLVMToLLVMIRTranslation + MLIRToLLVMIRTranslationRegistration + MLIRParser + MLIRTargetLLVMIRExport + MLIRSupport +) if(GC_MLIR_CXX_FLAGS) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GC_MLIR_CXX_FLAGS}") From 93a62100c85327ca39f30ae1007766ef9f2452c6 Mon Sep 17 00:00:00 2001 From: "Mei, Yijie" Date: Mon, 13 May 2024 16:30:28 +0800 Subject: [PATCH 8/8] remove unnecessary if --- src/gc-cpu-runner/CMakeLists.txt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/gc-cpu-runner/CMakeLists.txt b/src/gc-cpu-runner/CMakeLists.txt index b4cc13b78..eb45c93b0 100644 --- a/src/gc-cpu-runner/CMakeLists.txt +++ b/src/gc-cpu-runner/CMakeLists.txt @@ -1,12 +1,10 @@ # the dependency list copied from mlir/tools/mlir-cpu-runner/CMakeLists.txt of upstream -if(NOT LLVM_LINK_LLVM_DYLIB) - set(LLVM_LINK_COMPONENTS - Core - Support - nativecodegen - native - ) -endif() +set(LLVM_LINK_COMPONENTS + Core + Support + nativecodegen + native +) set(MLIR_LINK_COMPONENTS MLIRAnalysis MLIRBuiltinToLLVMIRTranslation