Skip to content

Commit 697848f

Browse files
ocloc as shared library, ocloc API
Change-Id: Ie866b9ad5ee6de8c8b10f83551e2a3cab9d64a02
1 parent d4d5971 commit 697848f

32 files changed

+1087
-426
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ if(MSVC)
821821
endif()
822822

823823
add_subdirectory_unique(offline_compiler ${NEO_BUILD_DIR}/offline_compiler)
824-
target_compile_definitions(ocloc PRIVATE MOCKABLE_VIRTUAL=)
824+
target_compile_definitions(ocloc_lib PRIVATE MOCKABLE_VIRTUAL=)
825825

826826
if(DONT_CARE_OF_VIRTUALS)
827827
set(NEO_CORE_RELEASE_LIB_NAME "neo_core")

offline_compiler/CMakeLists.txt

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# SPDX-License-Identifier: MIT
55
#
66

7-
project(ocloc)
7+
project(ocloc_lib)
88

9-
set(CLOC_SRCS_LIB
9+
set(CLOC_LIB_SRCS_LIB
1010
${NEO_CORE_DIRECTORY}/device_binary_format/ar/ar.h
1111
${NEO_CORE_DIRECTORY}/device_binary_format/ar/ar_decoder.h
1212
${NEO_CORE_DIRECTORY}/device_binary_format/ar/ar_decoder.cpp
@@ -30,6 +30,10 @@ set(CLOC_SRCS_LIB
3030
${NEO_SOURCE_DIR}/offline_compiler/decoder/helper.h
3131
${NEO_SOURCE_DIR}/offline_compiler/decoder/iga_wrapper.h
3232
${NEO_SOURCE_DIR}/offline_compiler/decoder/translate_platform_base.h
33+
${NEO_SOURCE_DIR}/offline_compiler/ocloc_api.cpp
34+
${NEO_SOURCE_DIR}/offline_compiler/ocloc_api.h
35+
${NEO_SOURCE_DIR}/offline_compiler/ocloc_arg_helper.h
36+
${NEO_SOURCE_DIR}/offline_compiler/ocloc_arg_helper.cpp
3337
${NEO_SOURCE_DIR}/offline_compiler/ocloc_fatbinary.cpp
3438
${NEO_SOURCE_DIR}/offline_compiler/ocloc_fatbinary.h
3539
${NEO_SOURCE_DIR}/offline_compiler/offline_compiler_helper.cpp
@@ -47,24 +51,24 @@ set(CLOC_SRCS_LIB
4751
)
4852

4953
if(${IGA_HEADERS_AVAILABLE})
50-
set(CLOC_SRCS_LIB ${CLOC_SRCS_LIB}
54+
set(CLOC_LIB_SRCS_LIB ${CLOC_LIB_SRCS_LIB}
5155
${NEO_SOURCE_DIR}/offline_compiler/decoder/iga_wrapper.cpp
5256
${NEO_SOURCE_DIR}/offline_compiler/decoder${BRANCH_DIR_SUFFIX}/translate_platform.cpp
5357
)
5458
else()
55-
set(CLOC_SRCS_LIB ${CLOC_SRCS_LIB}
59+
set(CLOC_LIB_SRCS_LIB ${CLOC_LIB_SRCS_LIB}
5660
${NEO_SOURCE_DIR}/offline_compiler/decoder/iga_stubs.cpp
5761
)
5862
endif()
5963

6064
if(WIN32)
61-
list(APPEND CLOC_SRCS_LIB
65+
list(APPEND CLOC_LIB_SRCS_LIB
6266
${NEO_CORE_DIRECTORY}/os_interface/windows/os_library_win.cpp
6367
${NEO_CORE_DIRECTORY}/os_interface/windows/os_library_win.h
6468
${NEO_SOURCE_DIR}/opencl/source/dll/windows/options_windows.cpp
6569
)
6670
else()
67-
list(APPEND CLOC_SRCS_LIB
71+
list(APPEND CLOC_LIB_SRCS_LIB
6872
${NEO_CORE_DIRECTORY}/os_interface/linux/os_library_linux.cpp
6973
${NEO_CORE_DIRECTORY}/os_interface/linux/os_library_linux.h
7074
${NEO_SOURCE_DIR}/opencl/source/dll/linux/options_linux.cpp
@@ -73,7 +77,7 @@ endif()
7377

7478
string(REPLACE ";" "," ALL_SUPPORTED_PRODUCT_FAMILIES "${ALL_SUPPORTED_PRODUCT_FAMILY}")
7579

76-
set(CLOC_LIB_FLAGS_DEFINITIONS
80+
set(CLOC_LIB_LIB_FLAGS_DEFINITIONS
7781
-DCIF_HEADERS_ONLY_BUILD
7882
-DALL_SUPPORTED_PRODUCT_FAMILIES=${ALL_SUPPORTED_PRODUCT_FAMILIES}
7983
)
@@ -83,43 +87,34 @@ set(RUNTIME_GENX_CPP_FILES
8387
)
8488

8589
macro(macro_for_each_platform)
86-
list(APPEND CLOC_SRCS_LIB ${NEO_SOURCE_DIR}/opencl/source/${GEN_TYPE_LOWER}/hw_info_${PLATFORM_IT_LOWER}.inl)
90+
list(APPEND CLOC_LIB_SRCS_LIB ${NEO_SOURCE_DIR}/opencl/source/${GEN_TYPE_LOWER}/hw_info_${PLATFORM_IT_LOWER}.inl)
8791
endmacro()
8892

8993
macro(macro_for_each_gen)
9094
foreach(SRC_IT ${RUNTIME_GENX_CPP_FILES})
9195
set(SRC_FILE ${NEO_SOURCE_DIR}/opencl/source/${GEN_TYPE_LOWER}/${SRC_IT})
9296
if(EXISTS ${SRC_FILE}_${GEN_TYPE_LOWER}.cpp)
93-
list(APPEND CLOC_SRCS_LIB ${SRC_FILE}_${GEN_TYPE_LOWER}.cpp)
97+
list(APPEND CLOC_LIB_SRCS_LIB ${SRC_FILE}_${GEN_TYPE_LOWER}.cpp)
9498
endif()
9599
endforeach()
96100
apply_macro_for_each_platform()
97101

98-
list(APPEND CLOC_SRCS_LIB ${NEO_CORE_DIRECTORY}/${GEN_TYPE_LOWER}/enable_${GEN_TYPE_LOWER}.cpp)
102+
list(APPEND CLOC_LIB_SRCS_LIB ${NEO_CORE_DIRECTORY}/${GEN_TYPE_LOWER}/enable_${GEN_TYPE_LOWER}.cpp)
99103
endmacro()
100104

101105
apply_macro_for_each_gen("SUPPORTED")
102106

103-
set(CLOC_SRCS
104-
${CLOC_SRCS_LIB}
105-
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
107+
set(CLOC_LIB_SRCS
108+
${CLOC_LIB_SRCS_LIB}
106109
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
107110
)
108-
add_executable(ocloc ${CLOC_SRCS})
109-
110-
if(UNIX)
111-
install(FILES $<TARGET_FILE:ocloc>
112-
DESTINATION ${CMAKE_INSTALL_BINDIR}
113-
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
114-
COMPONENT ocloc)
115-
set_property(GLOBAL APPEND PROPERTY NEO_COMPONENTS_LIST "ocloc")
116-
endif()
111+
add_library(ocloc_lib SHARED ${CLOC_LIB_SRCS})
117112

118113
add_subdirectories()
119114

120-
create_project_source_tree(ocloc)
115+
create_project_source_tree(ocloc_lib)
121116

122-
set(CLOC_INCLUDES
117+
set(CLOC_LIB_INCLUDES
123118
${ENGINE_NODE_DIR}
124119
${IGC_OCL_ADAPTOR_DIR}
125120
${CIF_BASE_DIR}
@@ -128,22 +123,75 @@ set(CLOC_INCLUDES
128123
${NEO__IGC_INCLUDE_DIR}
129124
)
130125

131-
target_include_directories(ocloc BEFORE PRIVATE ${CLOC_INCLUDES})
132-
target_include_directories(ocloc BEFORE PRIVATE ${IGA_INCLUDE_DIR})
126+
target_include_directories(ocloc_lib BEFORE PRIVATE ${CLOC_LIB_INCLUDES})
127+
target_include_directories(ocloc_lib BEFORE PRIVATE ${IGA_INCLUDE_DIR})
133128

134-
target_compile_definitions(ocloc PUBLIC ${CLOC_LIB_FLAGS_DEFINITIONS} ${SUPPORTED_GEN_FLAGS_DEFINITONS} DEFAULT_PLATFORM=${DEFAULT_SUPPORTED_PLATFORM}
129+
target_compile_definitions(ocloc_lib PUBLIC ${CLOC_LIB_LIB_FLAGS_DEFINITIONS} ${SUPPORTED_GEN_FLAGS_DEFINITONS} DEFAULT_PLATFORM=${DEFAULT_SUPPORTED_PLATFORM}
135130
IGA_LIBRARY_NAME=${CMAKE_SHARED_LIBRARY_PREFIX}${IGA_LIBRARY_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX})
136-
target_compile_definitions(ocloc PRIVATE ${NEO__IGC_COMPILE_DEFINITIONS})
131+
target_compile_definitions(ocloc_lib PRIVATE ${NEO__IGC_COMPILE_DEFINITIONS})
137132

138133
if(MSVC)
139-
target_link_libraries(ocloc dbghelp)
134+
target_link_libraries(ocloc_lib dbghelp)
135+
endif()
136+
137+
if(UNIX)
138+
target_link_libraries(ocloc_lib dl pthread)
139+
endif()
140+
141+
set(CLOC_LIB_SRCS_LIB ${CLOC_LIB_SRCS_LIB} PARENT_SCOPE)
142+
143+
set(CLOC_SRCS
144+
${NEO_CORE_DIRECTORY}/os_interface/os_library.h
145+
${NEO_SOURCE_DIR}/offline_compiler/ocloc_wrapper.h
146+
${NEO_SOURCE_DIR}/offline_compiler/ocloc_wrapper.cpp
147+
${NEO_SOURCE_DIR}/offline_compiler/utilities/get_path.h
148+
)
149+
if(WIN32)
150+
list(APPEND CLOC_SRCS
151+
${NEO_CORE_DIRECTORY}/os_interface/windows/os_library_win.cpp
152+
${NEO_CORE_DIRECTORY}/os_interface/windows/os_library_win.h
153+
${NEO_SOURCE_DIR}/offline_compiler/utilities/windows/get_path.cpp
154+
155+
)
156+
else()
157+
list(APPEND CLOC_SRCS
158+
${NEO_CORE_DIRECTORY}/os_interface/linux/os_library_linux.cpp
159+
${NEO_CORE_DIRECTORY}/os_interface/linux/os_library_linux.h
160+
${NEO_SOURCE_DIR}/offline_compiler/utilities/linux/get_path.cpp
161+
)
162+
endif()
163+
164+
add_executable(ocloc ${CLOC_SRCS} ${NEO_SOURCE_DIR}/offline_compiler/main.cpp)
165+
add_dependencies(ocloc ocloc_lib)
166+
target_link_libraries(ocloc ocloc_lib)
167+
168+
set(OCLOC_LIB_NAME "ocloc")
169+
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
170+
set(OCLOC_BITNESS_SUFFIX 32)
171+
elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
172+
set(OCLOC_BITNESS_SUFFIX 64)
140173
endif()
141174

142175
if(UNIX)
143-
target_link_libraries(ocloc dl pthread)
176+
install(FILES $<TARGET_FILE:ocloc_lib>
177+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/intel-opencl
178+
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
179+
install(FILES $<TARGET_FILE:ocloc>
180+
DESTINATION ${CMAKE_INSTALL_BINDIR}
181+
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
182+
COMPONENT ocloc)
183+
set_property(GLOBAL APPEND PROPERTY NEO_COMPONENTS_LIST "ocloc")
184+
set_target_properties(ocloc_lib PROPERTIES OUTPUT_NAME ${OCLOC_LIB_NAME})
185+
add_definitions(-DOCLOC_LIB_NAME="lib${OCLOC_LIB_NAME}.so")
186+
else()
187+
set_target_properties(ocloc_lib PROPERTIES OUTPUT_NAME "${OCLOC_LIB_NAME}${OCLOC_BITNESS_SUFFIX}")
188+
add_definitions(-DOCLOC_LIB_NAME="${OCLOC_LIB_NAME}${OCLOC_BITNESS_SUFFIX}.dll")
144189
endif()
145190

191+
create_project_source_tree(ocloc)
146192
set_target_properties(ocloc PROPERTIES FOLDER "offline_compiler")
193+
set_target_properties(ocloc_lib PROPERTIES FOLDER "offline_compiler")
194+
147195

148196
add_custom_target(copy_compiler_files DEPENDS ${NEO__IGC_TARGETS})
149197
set_target_properties(copy_compiler_files PROPERTIES FOLDER "opencl runtime")
@@ -153,10 +201,11 @@ if(WIN32)
153201
add_custom_command(
154202
TARGET copy_compiler_files
155203
PRE_BUILD
204+
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:ocloc_lib>
205+
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${TARGET_tmp}> $<TARGET_FILE_DIR:ocloc_lib>
156206
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:ocloc>
157207
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${TARGET_tmp}> $<TARGET_FILE_DIR:ocloc>
158208
)
159209
endforeach()
160210
endif()
161211

162-
set(CLOC_SRCS_LIB ${CLOC_SRCS_LIB} PARENT_SCOPE)

offline_compiler/api.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (C) 2020 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "ocloc_api.h"
9+
10+
#include <iostream>
11+
12+
void printHelp() {
13+
printf(R"===(ocloc is a tool for managing Intel OpenCL GPU device binary format.
14+
It can be used for generation (as part of 'compile' command) as well as
15+
manipulation (decoding/modifying - as part of 'disasm'/'asm' commands) of such
16+
binary files.
17+
Intel OpenCL GPU device binary is a format used by Intel OpenCL GPU runtime
18+
(aka NEO). Intel OpenCL GPU runtime will return this binary format when queried
19+
using clGetProgramInfo(..., CL_PROGRAM_BINARIES, ...). It will also honor
20+
this format as input to clCreateProgramWithBinary function call.
21+
ocloc does not require Intel GPU device to be present in the system nor does it
22+
depend on Intel OpenCL GPU runtime driver to be installed. It does however rely
23+
on the same set of compilers (IGC, common_clang) as the runtime driver.
24+
25+
Usage: ocloc [--help] <command> [<command_args>]
26+
Available commands are listed below.
27+
Use 'ocloc <command> --help' to get help about specific command.
28+
29+
Commands:
30+
compile Compiles input to Intel OpenCL GPU device binary.
31+
disasm Disassembles Intel OpenCL GPU device binary.
32+
asm Assembles Intel OpenCL GPU device binary.
33+
multi Compiles multiple files using a config file.
34+
35+
Default command (when none provided) is 'compile'.
36+
37+
Examples:
38+
Compile file to Intel OpenCL GPU device binary (out = source_file_Gen9core.bin)
39+
ocloc -file source_file.cl -device skl
40+
41+
Disassemble Intel OpenCL GPU device binary
42+
ocloc disasm -file source_file_Gen9core.bin
43+
44+
Assemble to Intel OpenCL GPU device binary (after above disasm)
45+
ocloc asm -out reassembled.bin
46+
)===");
47+
}
48+
49+
extern "C" {
50+
int oclocInvoke(unsigned int numArgs, const char *argv[],
51+
const uint32_t numSources, const uint8_t **dataSources, const uint64_t *lenSources, const char **nameSources,
52+
const uint32_t numInputHeaders, const uint8_t **dataInputHeaders, const uint64_t *lenInputHeaders, const char **nameInputHeaders,
53+
uint32_t *numOutputs, uint8_t ***dataOutputs, uint64_t **lenOutputs, char ***nameOutputs) {
54+
auto helper = std::make_unique<OclocArgHelper>(
55+
numSources, dataSources, lenSources, nameSources,
56+
numInputHeaders, dataInputHeaders, lenInputHeaders, nameInputHeaders,
57+
numOutputs, dataOutputs, lenOutputs, nameOutputs);
58+
std::vector<std::string> allArgs;
59+
if (numArgs > 1) {
60+
allArgs.assign(argv, argv + numArgs);
61+
}
62+
63+
try {
64+
if (numArgs == 1 || (numArgs > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))) {
65+
printHelp();
66+
} else if (numArgs > 1 && !strcmp(argv[1], "disasm")) {
67+
BinaryDecoder disasm(std::move(helper));
68+
int retVal = disasm.validateInput(allArgs);
69+
if (retVal == 0) {
70+
return disasm.decode();
71+
} else {
72+
return retVal;
73+
}
74+
} else if (numArgs > 1 && !strcmp(argv[1], "asm")) {
75+
BinaryEncoder assembler(std::move(helper));
76+
int retVal = assembler.validateInput(allArgs);
77+
if (retVal == 0) {
78+
return assembler.encode();
79+
} else {
80+
return retVal;
81+
}
82+
} else if (numArgs > 1 && (!strcmp(argv[1], "multi") || !strcmp(argv[1], "-multi"))) {
83+
int retValue = CL_SUCCESS;
84+
auto pMulti = std::unique_ptr<MultiCommand>(MultiCommand::create(allArgs, retValue));
85+
return retValue;
86+
} else {
87+
int retVal = CL_SUCCESS;
88+
std::vector<std::string> allArgs;
89+
if (numArgs > 1) {
90+
allArgs.assign(argv, argv + numArgs);
91+
}
92+
93+
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, allArgs, true, retVal, std::move(helper));
94+
95+
if (retVal == CL_SUCCESS) {
96+
retVal = buildWithSafetyGuard(pCompiler);
97+
98+
std::string buildLog = pCompiler->getBuildLog();
99+
if (buildLog.empty() == false) {
100+
printf("%s\n", buildLog.c_str());
101+
}
102+
103+
if (retVal == CL_SUCCESS) {
104+
if (!pCompiler->isQuiet())
105+
printf("Build succeeded.\n");
106+
} else {
107+
printf("Build failed with error code: %d\n", retVal);
108+
}
109+
}
110+
delete pCompiler;
111+
return retVal;
112+
}
113+
} catch (const std::exception &e) {
114+
printf("%s\n", e.what());
115+
return -1;
116+
}
117+
return -1;
118+
}
119+
void oclocFreeOutput(uint32_t *numOutputs, uint8_t ***dataOutputs, uint64_t **lenOutputs, char ***nameOutputs) {
120+
for (uint32_t i = 0; i < *numOutputs; i++) {
121+
delete[](*dataOutputs)[i];
122+
delete[](*nameOutputs)[i];
123+
}
124+
delete[](*dataOutputs);
125+
delete[](*lenOutputs);
126+
delete[](*nameOutputs);
127+
}
128+
}

0 commit comments

Comments
 (0)