Skip to content

Commit 241d4e9

Browse files
tlepley-cadenceopti-mix
authored andcommitted
[Build] Simplify the integration of external backends
1 parent da1e9f6 commit 241d4e9

File tree

10 files changed

+157
-0
lines changed

10 files changed

+157
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ compile_commands.json
1717
build/
1818
external/googletest/
1919
external/llvm/
20+
externalbackends/*/
2021
.vscode/
2122
.vim/
2223
.idea/

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3030

3131
include(GlowDefaults)
3232
include(GlowTestSupport)
33+
include(GlowExternalBackends)
3334
include(SanitizerSupport)
3435
include(CoverageSupport)
3536
include(DoxygenSupport)
@@ -51,6 +52,7 @@ include_directories(BEFORE
5152

5253
include_directories(${GLOW_BINARY_DIR})
5354
include_directories(${CMAKE_CURRENT_BINARY_DIR})
55+
include_directories(${GLOW_SOURCE_DIR})
5456

5557
file(GLOB_RECURSE header_files include/*.h tools/*.h lib/*.h)
5658
add_custom_target(CollectHeaders SOURCES ${header_files})
@@ -83,6 +85,9 @@ if (GLOW_WITH_HABANA)
8385
add_definitions(-DGLOW_WITH_HABANA=1)
8486
endif ()
8587

88+
# Top level setup for external backends
89+
ExternalBackendsInit()
90+
8691
# Prefer LLVM 7.
8792
find_package(LLVM 7 CONFIG)
8893

cmake/modules/GlowDefaults.cmake

+12
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ function(make_whole_archive DST SRC)
7676
target_link_libraries(${DST} INTERFACE ${SRC})
7777
endif()
7878
endfunction()
79+
80+
# Return in result the name of curdir sub-directories
81+
MACRO(getSubDirList result curdir)
82+
file(GLOB children RELATIVE ${curdir} ${curdir}/[^\.]*)
83+
SET(dirlist "")
84+
FOREACH(child ${children})
85+
IF(IS_DIRECTORY ${curdir}/${child})
86+
LIST(APPEND dirlist ${child})
87+
ENDIF()
88+
ENDFOREACH()
89+
SET(${result} ${dirlist})
90+
ENDMACRO()
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Copyright (c) 2019-present, Facebook, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# A function to add a test to be driven through the 'check' target.
16+
# Unlike the 'test' target, the 'check' target rebuilds the executables
17+
# before invoking the tests.
18+
19+
# Returns the name of the backend enabling.
20+
MACRO(getBackendEnableVariable result backend_dir_name)
21+
string(TOUPPER "GLOW_WITH_${backend_dir_name}" ${result})
22+
ENDMACRO()
23+
24+
25+
# Macro to be called at top level.
26+
MACRO(ExternalBackendsInit)
27+
# External backends
28+
set(EXTERNAL_BACKENDS_DIR ${GLOW_SOURCE_DIR}/externalbackends)
29+
include_directories(${EXTERNAL_BACKENDS_DIR})
30+
getSubDirList(SUBDIRS ${EXTERNAL_BACKENDS_DIR})
31+
32+
FOREACH(child ${SUBDIRS})
33+
# Add an option for the backend. The backend is enabled by default.
34+
# The user can disable it by setting the right variable to OFF.
35+
getBackendEnableVariable(backend_enable_variable ${child})
36+
option(${backend_enable_variable} "Build the ${child} backend" ON)
37+
# define BACKEND_ROOT_<upper case backend name>
38+
string(TOUPPER "BACKEND_ROOT_${child}" BACKEND_ROOT_NAME)
39+
set(${BACKEND_ROOT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/externalbackends/${child})
40+
41+
# Verbosing
42+
message(STATUS "Detected external backend '${child}'")
43+
message(STATUS " -> Backend '${child}' can be disabled by setting ${backend_enable_variable}=OFF")
44+
message(STATUS " -> Backend '${child}' specific variables:")
45+
message(STATUS " - ${backend_enable_variable} = ${${backend_enable_variable}}")
46+
message(STATUS " - ${BACKEND_ROOT_NAME} = ${${BACKEND_ROOT_NAME}}")
47+
if (${backend_enable_variable})
48+
message(STATUS " -> Backend '${child}' ENABLED")
49+
add_definitions(-D${backend_enable_variable}=1)
50+
else()
51+
message(STATUS " -> Backend '${child}' DISABLED")
52+
endif()
53+
54+
# Handle the backend only when activated
55+
if (${backend_enable_variable})
56+
# If the backend has a global CMakefile, include it.
57+
if(EXISTS "${EXTERNAL_BACKENDS_DIR}/${child}/CMakeLists.txt")
58+
include("${EXTERNAL_BACKENDS_DIR}/${child}/CMakeLists.txt")
59+
else()
60+
message(STATUS "External backend '${child}' has no global CMakeLists.txt")
61+
endif()
62+
endif()
63+
ENDFOREACH()
64+
ENDMACRO()
65+
66+
# Macro to register external backends.
67+
MACRO(ExternalBackendsRegister)
68+
getSubDirList(SUBDIRS ${GLOW_SOURCE_DIR}/externalbackends)
69+
FOREACH(child ${SUBDIRS})
70+
getBackendEnableVariable(backend_enable_variable ${child})
71+
# Handle the backend only when activated
72+
if (${backend_enable_variable})
73+
# If the backend has a 'Backends' sub-directory, add it.
74+
if(EXISTS ${EXTERNAL_BACKENDS_DIR}/${child}/Backends)
75+
message("Adding external ${child} backend.")
76+
set(EXTERNAL_BACKEND_NAME ${child}Backend)
77+
add_subdirectory(${EXTERNAL_BACKENDS_DIR}/${child}/Backends EXT_${EXTERNAL_BACKEND_NAME})
78+
else()
79+
message(FATAL_ERROR "External backend '${child}' has no 'Backends' sub-directory (${EXTERNAL_BACKENDS_DIR}/${child}/Backends)")
80+
endif()
81+
endif()
82+
ENDFOREACH()
83+
ENDMACRO()
84+
85+
# Macro to register backend specific nodes and instructions.
86+
MACRO(ExternalBackendsClassGen)
87+
getSubDirList(SUBDIRS ${GLOW_SOURCE_DIR}/externalbackends)
88+
FOREACH(child ${SUBDIRS})
89+
getBackendEnableVariable(backend_enable_variable ${child})
90+
# Handle the backend only when activated
91+
if (${backend_enable_variable})
92+
# If the backend has a 'ClassGen' sub-directory, add it.
93+
if(EXISTS ${EXTERNAL_BACKENDS_DIR}/${child}/ClassGen)
94+
add_subdirectory(${EXTERNAL_BACKENDS_DIR}/${child}/ClassGen EXT_${child})
95+
else()
96+
message(STATUS "External backend '${child}' has no 'ClassGen' sub-directory")
97+
endif()
98+
endif()
99+
ENDFOREACH()
100+
ENDMACRO()
101+
102+
# Macro to add external backends tests.
103+
MACRO(ExternalBackendsTest)
104+
getSubDirList(SUBDIRS ${GLOW_SOURCE_DIR}/externalbackends)
105+
FOREACH(child ${SUBDIRS})
106+
getBackendEnableVariable(backend_enable_variable ${child})
107+
# Handle the backend only when activated
108+
if (${backend_enable_variable})
109+
# If the backend has a 'Tests' sub-directory, add it.
110+
if(EXISTS "${EXTERNAL_BACKENDS_DIR}/${child}/Tests")
111+
add_subdirectory("${EXTERNAL_BACKENDS_DIR}/${child}/Tests" EXT_${child})
112+
else()
113+
message(STATUS "External backend '${child}' has no 'Tests' sub-directory")
114+
endif()
115+
endif()
116+
ENDFOREACH()
117+
ENDMACRO()
118+

externalbackends/README.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory is intended to contain external backends.

lib/Backends/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ foreach(object ${subdirs})
1616
endif()
1717
endforeach()
1818

19+
# External backends
20+
ExternalBackendsRegister()
21+
1922
add_library(ExecutionContext
2023
TraceEvents.cpp)
2124
target_link_libraries(ExecutionContext
@@ -38,6 +41,7 @@ target_link_libraries(Backend
3841
Optimizer)
3942
add_library(Backends
4043
Backends.cpp)
44+
4145
target_link_libraries(Backends
4246
PRIVATE
4347
Base

tests/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ add_subdirectory(images)
55
add_subdirectory(benchmark)
66
add_subdirectory(models)
77

8+
# External backends
9+
ExternalBackendsTest()
10+
811
# Function which creates a new OutputCheck test that runs test_script.
912
function(add_output_check_test)
1013
set(oneValueArgs NAME)

tools/ClassGen/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ endif()
3030
if(GLOW_WITH_HABANA)
3131
add_subdirectory(Backends/Habana)
3232
endif()
33+
34+
# External backends
35+
ExternalBackendsClassGen()

tools/ClassGen/InstrGen.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,11 @@ int main(int argc, char **argv) {
625625
#include "Backends/CPU/CPUSpecificInstrs.h"
626626
#include "Backends/Habana/HabanaSpecificInstrs.h"
627627
#include "Backends/OpenCL/OpenCLSpecificInstrs.h"
628+
// Add here external backend specific instructions headers.
629+
// Example:
630+
// #ifdef GLOW_WITH_<NAME>
631+
// #include "<Name>/ClassGen/<Name>SpecificInstrs.h"
632+
// #endif
628633

629634
return 0;
630635
}

tools/ClassGen/NodeGen.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,11 @@ int main(int argc, char **argv) {
716716
#include "Backends/CPU/CPUSpecificNodes.h"
717717
#include "Backends/OpenCL/OpenCLSpecificNodes.h"
718718
#include "Backends/Habana/HabanaSpecificNodes.h"
719+
// Add here external backend specific node headers.
720+
// Example:
721+
// #ifdef GLOW_WITH_<NAME>
722+
// #include "<Name>/ClassGen/<Name>SpecificNodes.h"
723+
// #endif
719724

720725
return 0;
721726
}

0 commit comments

Comments
 (0)