Skip to content

Commit 39321c1

Browse files
author
Mei, Yijie
committed
Merge branch 'main' of https://github.com/intel/graph-compiler into yijie/no-unused-but-set-parameter
2 parents 6f35613 + 432aa52 commit 39321c1

16 files changed

+474
-37
lines changed

.github/workflows/build-llvm.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
cmake -G Ninja llvm -B build -DCMAKE_INSTALL_PREFIX=llvm-install \
2929
-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=true -DLLVM_ENABLE_PROJECTS="mlir" -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_INSTALL_UTILS=true -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
3030
cmake --build build --target install
31+
cp llvm/bin/llvm-lit llvm-install/bin
3132
cd llvm-install
3233
tar -zcf ../llvm.tgz .
3334

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
build
1+
.*/
2+
!.github/
3+
build/
24
compile_commands.json
3-
4-
.vscode

CMakeLists.txt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cmake_minimum_required(VERSION 3.20)
22
project(GraphCompiler LANGUAGES C CXX)
33

4-
include(FetchContent)
54

65
set(CMAKE_CXX_STANDARD 17)
76
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -11,10 +10,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1110
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
1211

1312
option(GC_LEGACY_ENABLE ON)
14-
15-
set(GC_ONEDNN_VERSION "main" CACHE STRING "oneDNN version")
16-
include(onednn)
17-
add_subdirectory(${onednn_SOURCE_DIR} ${onednn_BINARY_DIR})
13+
option(GC_TEST_ENABLE "Build the tests" ON)
1814

1915
if(GC_LEGACY_ENABLE)
2016
add_subdirectory(legacy/core)
@@ -33,17 +29,35 @@ include(AddLLVM)
3329
include(AddMLIR)
3430
include(HandleLLVMOptions)
3531

36-
include_directories(${LLVM_INCLUDE_DIRS})
37-
include_directories(${MLIR_INCLUDE_DIRS})
32+
include_directories(
33+
${LLVM_INCLUDE_DIRS}
34+
${MLIR_INCLUDE_DIRS}
35+
${PROJECT_BINARY_DIR}/include
36+
${PROJECT_SOURCE_DIR}/include
37+
)
3838

39-
include_directories(${PROJECT_SOURCE_DIR}/include)
40-
include_directories(${PROJECT_BINARY_DIR}/include)
39+
# The paths are added in the subfolders using the gc_add_path() function.
40+
# These lists are also used by tests.
41+
set(GC_LIB_SOURCES CACHE INTERNAL "The graph_compiler library source paths")
42+
set(GC_LIB_INCLUDES CACHE INTERNAL "The graph_compiler library include paths")
4143

4244
add_definitions(${LLVM_DEFINITIONS})
4345

46+
set(GC_MLIR_CXX_FLAGS "")
4447
if(NOT MSVC)
45-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-but-set-parameter")
48+
set(GC_MLIR_CXX_FLAGS "-Wno-unused-but-set-parameter")
4649
endif()
4750
add_subdirectory(include)
4851
add_subdirectory(lib)
4952
add_subdirectory(src)
53+
54+
add_library(graph_compiler SHARED ${GC_LIB_SOURCES})
55+
target_include_directories(graph_compiler PUBLIC ${GC_LIB_INCLUDES})
56+
set(GC_LIB_LINKED_LIBS
57+
MLIRLinalgx
58+
MLIRMicrokernel
59+
MLIROnednnGraph
60+
)
61+
target_link_libraries(graph_compiler PRIVATE ${GC_LIB_LINKED_LIBS})
62+
63+
add_subdirectory(test)

cmake/functions.cmake

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
include_guard()
2+
include(FetchContent)
3+
4+
# A wrapper around FetchContent that could either fetch content from
5+
# a git repository, use a local directory or find_package().
6+
function(gc_fetch_content
7+
# The content name. Also used as the package name for find_package().
8+
name
9+
# Git tag, branch name or hash. If the value has the format
10+
# [v]major[.minor[.patch[.tweak]]] and the SKIP_FIND option is not
11+
# specified, use find_package() first.
12+
# The variable GC_<NAME>_VERSION could be used to override this argument.
13+
git_tag_or_version
14+
# Git repository URL. If the variable GC_<NAME>_SRC_DIR is defined,
15+
# the local directory is used instead.
16+
git_repository
17+
#
18+
# Optional arguments:
19+
# SKIP_ADD: Populate but do not add the content to the project.
20+
# SKIP_FIND: Do not use find_package().
21+
# CMAKE_ARGS: Passed to FetchContent_Declare.
22+
)
23+
string(TOUPPER ${name} uname)
24+
cmake_parse_arguments(GC_${uname} "SKIP_ADD;SKIP_FIND" "" "CMAKE_ARGS" ${ARGN})
25+
26+
if (DEFINED GC_${uname}_CMAKE_ARGS)
27+
message(STATUS "${name}_CMAKE_ARGS: ${GC_${uname}_CMAKE_ARGS}")
28+
endif ()
29+
30+
if (DEFINED GC_${uname}_SRC_DIR)
31+
FetchContent_Declare(
32+
${name}
33+
SOURCE_DIR ${GC_${uname}_SRC_DIR}
34+
CMAKE_ARGS ${${uname}_CMAKE_ARGS}
35+
)
36+
else ()
37+
if (DEFINED GC_${uname}_VERSION)
38+
set(git_tag_or_version ${GC_${uname}_VERSION})
39+
endif ()
40+
message(STATUS "${name}_VERSION: ${git_tag_or_version}")
41+
42+
if (NOT ${uname}_SKIP_FIND AND git_tag_or_version
43+
MATCHES "^v?([0-9]+(\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?)?)$")
44+
string(REPLACE "v" "" version ${git_tag_or_version})
45+
find_package(${name} ${version} EXACT)
46+
endif ()
47+
48+
if (NOT DEFINED ${name}_VERSION_COUNT)
49+
message(STATUS "Fetching ${name} from ${git_repository}")
50+
set(FETCHCONTENT_QUIET FALSE)
51+
FetchContent_Declare(
52+
${name}
53+
GIT_REPOSITORY ${git_repository}
54+
GIT_TAG ${git_tag_or_version}
55+
GIT_PROGRESS TRUE
56+
CMAKE_ARGS ${GC_${uname}_CMAKE_ARGS}
57+
FIND_PACKAGE_ARGS ${FIND_PACKAGE_ARGS}
58+
)
59+
endif ()
60+
endif ()
61+
62+
if (NOT DEFINED ${name}_VERSION_COUNT)
63+
if (GC_${uname}_SKIP_ADD)
64+
if (NOT DEFINED ${name}_POPULATED)
65+
FetchContent_Populate(${name})
66+
FetchContent_GetProperties(${name})
67+
set(${name}_POPULATED TRUE PARENT_SCOPE)
68+
set(${name}_SOURCE_DIR ${${name}_SOURCE_DIR} PARENT_SCOPE)
69+
set(${name}_BINARY_DIR ${${name}_BINARY_DIR} PARENT_SCOPE)
70+
endif ()
71+
else ()
72+
FetchContent_MakeAvailable(${name})
73+
endif ()
74+
endif ()
75+
endfunction()
76+
77+
# Add one or multiple paths to the specified list.
78+
# The paths could be specified as a list of files or a GLOB pattern:
79+
# gc_add_path(SOURCES GLOB "src/*.cpp")
80+
# gc_add_path(INCLUDES include1 include2 include3)
81+
function(gc_add_path list_name paths)
82+
if (paths STREQUAL "GLOB")
83+
file(GLOB paths ${ARGN})
84+
list(APPEND ${list_name} ${paths})
85+
else ()
86+
get_filename_component(path ${paths} ABSOLUTE)
87+
list(APPEND ${list_name} ${path})
88+
foreach (path ${ARGN})
89+
get_filename_component(path ${path} ABSOLUTE)
90+
list(APPEND ${list_name} ${path})
91+
endforeach ()
92+
endif ()
93+
set(${list_name} ${${list_name}}
94+
CACHE INTERNAL "${list_name} paths"
95+
)
96+
endfunction()

cmake/gtest.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include_guard()
2+
include(functions)
3+
4+
gc_fetch_content(
5+
GTest
6+
v1.14.0
7+
https://github.com/google/googletest.git
8+
)

cmake/onednn.cmake

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
include_guard()
22

3-
set(ONEDNN_CMAKE_ARGS
4-
-DDNNL_IS_MAIN_PROJECT=FALSE
5-
-DDNNL_BUILD_TESTS=FALSE
6-
-DDNNL_BUILD_EXAMPLES=FALSE
7-
)
3+
get_property(DNNL_INCLUDES GLOBAL PROPERTY DNNL_INCLUDES)
4+
if (NOT DEFINED DNNL_INCLUDES)
5+
include(functions)
86

9-
if (${GC_ONEDNN_VERSION} STREQUAL "main")
10-
set(ONEDNN_TAG ${GC_ONEDNN_VERSION})
11-
else ()
12-
set(ONEDNN_TAG v${GC_ONEDNN_VERSION})
13-
endif ()
14-
message(STATUS "oneDNN version: ${GC_ONEDNN_VERSION}")
15-
set(FETCHCONTENT_QUIET FALSE)
16-
FetchContent_Declare(
17-
oneDNN
18-
GIT_REPOSITORY https://github.com/intel-ai/oneDNN.git
19-
GIT_TAG ${ONEDNN_TAG}
20-
CMAKE_ARGS ${ONEDNN_CMAKE_ARGS}
21-
GIT_PROGRESS TRUE
22-
)
7+
gc_fetch_content(dnnl main https://github.com/intel-ai/oneDNN.git
8+
SKIP_ADD
9+
CMAKE_ARGS -DDNNL_IS_MAIN_PROJECT=FALSE -DDNNL_BUILD_TESTS=FALSE -DDNNL_BUILD_EXAMPLES=FALSE
10+
)
11+
12+
set(DNNL_INCLUDES
13+
${dnnl_BINARY_DIR}/include
14+
${dnnl_SOURCE_DIR}/include
15+
${dnnl_SOURCE_DIR}/src/graph/backend/elyzor/include
16+
)
17+
set_property(GLOBAL PROPERTY DNNL_INCLUDES ${DNNL_INCLUDES})
2318

24-
FetchContent_Populate(oneDNN)
25-
FetchContent_GetProperties(oneDNN)
26-
message(STATUS "oneDNN source dir: ${onednn_SOURCE_DIR}")
27-
FetchContent_MakeAvailable(oneDNN)
19+
# This allows to generate headers from *.in without adding the library to the build.
20+
# If the build is required, remove this and the SKIP_ADD option above.
21+
if (DEFINED CMAKE_GENERATOR)
22+
set(GENERATOR_FLAG "-G ${CMAKE_GENERATOR}")
23+
endif ()
24+
execute_process(COMMAND ${CMAKE_COMMAND} ${GENERATOR_FLAG}
25+
-Wno-dev
26+
-S ${dnnl_SOURCE_DIR}
27+
-B ${dnnl_BINARY_DIR}
28+
${GC_DNNL_CMAKE_ARGS}
29+
)
30+
endif ()

lib/gc-dialects/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GC_MLIR_CXX_FLAGS}")
12
add_subdirectory(Linalgx)
23
add_subdirectory(Microkernel)
34
add_subdirectory(OnednnGraph)

src/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
22
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
33
set(gc_opt_libs ${dialect_libs} ${conversion_libs} MLIROptLib)
44
add_llvm_executable(gc-opt gc-opt.cpp)
5-
target_link_libraries(gc-opt ${gc_opt_libs})
5+
if(GC_MLIR_CXX_FLAGS)
6+
target_compile_options(gc-opt PRIVATE ${GC_MLIR_CXX_FLAGS})
7+
endif()
8+
target_link_libraries(gc-opt PRIVATE ${gc_opt_libs})
9+
10+
add_subdirectory(dnnl)

src/dnnl/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include(onednn)
2+
3+
gc_add_path(GC_LIB_SOURCES GLOB "*.*pp")
4+
gc_add_path(GC_LIB_INCLUDES ${DNNL_INCLUDES})

src/dnnl/dnnl_graph_compiler.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "dnnl_graph_compiler.hpp"
2+
#include <new>
3+
4+
// dnnl_graph_compiler.h interface implementation.
5+
// TODO: Implement
6+
7+
dnnl_status_t
8+
dnnl_graph_compiler_create(const struct dnnl_graph_compiler_context *ctx,
9+
const struct dnnl_graph_compiler **gc) {
10+
try {
11+
*gc = new dnnl_graph_compiler(ctx);
12+
return dnnl_success;
13+
} catch (const std::bad_alloc &e) {
14+
return dnnl_out_of_memory;
15+
} catch (...) {
16+
// TODO: Add error handling
17+
return dnnl_runtime_error;
18+
}
19+
}
20+
21+
void dnnl_graph_compiler_destroy(const struct dnnl_graph_compiler *gc) {
22+
delete gc;
23+
}
24+
25+
dnnl_status_t
26+
dnnl_graph_compiler_compile(const dnnl_graph_compiler *gc,
27+
const char *graph_json,
28+
const struct dnnl_graph_compiler_executable **exe) {
29+
try {
30+
auto ptr = gc->compile(std::string_view(graph_json));
31+
*exe = ptr.release();
32+
return dnnl_success;
33+
} catch (const std::bad_alloc &e) {
34+
return dnnl_out_of_memory;
35+
} catch (...) {
36+
// TODO: Add error handling
37+
return dnnl_runtime_error;
38+
}
39+
}
40+
41+
void dnnl_graph_compiler_destroy_executable(
42+
const struct dnnl_graph_compiler *gc,
43+
const struct dnnl_graph_compiler_executable *exe) {
44+
delete exe;
45+
}
46+
47+
dnnl_status_t
48+
dnnl_graph_compiler_execute(const struct dnnl_graph_compiler *gc,
49+
const struct dnnl_graph_compiler_executable *exe,
50+
dnnl_graph_compiler_tensor *inputs,
51+
dnnl_graph_compiler_tensor *outputs) {
52+
try {
53+
exe->execute(inputs, outputs);
54+
return dnnl_success;
55+
} catch (const std::bad_alloc &e) {
56+
return dnnl_out_of_memory;
57+
} catch (...) {
58+
// TODO: Add error handling
59+
return dnnl_runtime_error;
60+
}
61+
}
62+
63+
std::unique_ptr<const dnnl_graph_compiler_executable>
64+
dnnl_graph_compiler::compile(const std::string_view &graph_json) const {
65+
// TODO: Implement
66+
return std::unique_ptr<const dnnl_graph_compiler_executable>(
67+
new dnnl_graph_compiler_executable());
68+
}
69+
70+
void dnnl_graph_compiler_executable::execute(
71+
dnnl_graph_compiler_tensor *inputs,
72+
dnnl_graph_compiler_tensor *outputs) const {
73+
// TODO: Implement
74+
}

src/dnnl/dnnl_graph_compiler.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef GRAPH_COMPILER_HPP
2+
#define GRAPH_COMPILER_HPP
3+
4+
#include "dnnl_graph_compiler.h"
5+
#include <memory>
6+
#include <string_view>
7+
8+
struct dnnl_graph_compiler_executable {
9+
// TODO: Implement
10+
11+
void execute(dnnl_graph_compiler_tensor *inputs,
12+
dnnl_graph_compiler_tensor *outputs) const;
13+
};
14+
15+
struct dnnl_graph_compiler {
16+
const dnnl_graph_compiler_context ctx;
17+
18+
explicit dnnl_graph_compiler(const dnnl_graph_compiler_context *context)
19+
// TODO: Initialize ctx with context or defaults if context is nullptr
20+
: ctx() {}
21+
22+
std::unique_ptr<const dnnl_graph_compiler_executable>
23+
compile(const std::string_view &graph_json) const;
24+
};
25+
26+
#endif // GRAPH_COMPILER_HPP

test/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if (NOT GC_TEST_ENABLE)
2+
message(STATUS "The tests are not enabled.")
3+
return()
4+
endif ()
5+
6+
include(gtest)
7+
8+
# Static library to be used in tests
9+
add_library(graph_compiler_static STATIC ${GC_LIB_SOURCES})
10+
target_include_directories(graph_compiler_static PUBLIC ${GC_LIB_INCLUDES})
11+
12+
add_subdirectory(dnnl)

test/dnnl/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copy resources to the build directory
2+
file(GLOB_RECURSE TEST_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} resources/*)
3+
foreach (TEST_RESOURCE ${TEST_RESOURCES})
4+
configure_file(${TEST_RESOURCE} ${TEST_RESOURCE} COPYONLY)
5+
endforeach ()
6+
7+
file(GLOB TEST_SOURCES test_*.cpp)
8+
9+
foreach (TEST_SOURCE ${TEST_SOURCES})
10+
get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE)
11+
add_executable(${TEST_NAME} ${TEST_SOURCE})
12+
target_include_directories(${TEST_NAME} PUBLIC ${GC_LIB_INCLUDES})
13+
target_link_libraries(${TEST_NAME} graph_compiler_static gtest gtest_main)
14+
add_test(NAME ${TEST_NAME}
15+
COMMAND ${TEST_NAME}
16+
)
17+
endforeach ()

0 commit comments

Comments
 (0)