-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generated kernel name header instead of relying on kernel name qu…
…ery.
- Loading branch information
1 parent
16138ee
commit aef17a2
Showing
15 changed files
with
250 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
""" | ||
Copyright (C) 2022 Intel Corporation | ||
SPDX-License-Identifier: MIT | ||
""" | ||
import argparse | ||
import os | ||
import re | ||
import subprocess | ||
import sys | ||
|
||
from mako.template import Template | ||
|
||
HEADER_TEMPLATE = Template("""/* | ||
* | ||
* Copyright (C) 2023 Intel Corporation | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* @file ${file_name}.h | ||
* | ||
*/ | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
namespace uur { | ||
namespace device_binaries { | ||
std::map<std::string, std::vector<std::string>> program_kernel_map = { | ||
% for program, entry_points in kernel_name_dict.items(): | ||
{"${program}", { | ||
% for entry_point in entry_points: | ||
"${entry_point}", | ||
% endfor | ||
}}, | ||
% endfor | ||
}; | ||
} | ||
} | ||
""") | ||
|
||
|
||
def generate_header(output_file, kernel_name_dict): | ||
"""Render the template and write it to the output file.""" | ||
file_name = os.path.basename(output_file) | ||
rendered = HEADER_TEMPLATE.render(file_name=file_name, | ||
kernel_name_dict=kernel_name_dict) | ||
rendered = re.sub(r"\r\n", r"\n", rendered) | ||
|
||
with open(output_file, "w") as fout: | ||
fout.write(rendered) | ||
|
||
|
||
def get_mangled_names(dpcxx_path, source_file, output_header): | ||
"""Return a list of all the entry point names from a given sycl source file. | ||
Filters out wrapper and offset handler entry points. | ||
""" | ||
output_dir = os.path.dirname(output_header) | ||
il_file = os.path.join(output_dir, os.path.basename(source_file) + ".ll") | ||
generate_il_command = f"""\ | ||
{dpcxx_path} -S -fsycl -fsycl-device-code-split=off \ | ||
-fsycl-device-only -o {il_file} {source_file}""" | ||
subprocess.run(generate_il_command, shell=True) | ||
kernel_line_regex = re.compile("define.*spir_kernel") | ||
definition_lines = [] | ||
with open(il_file) as f: | ||
lines = f.readlines() | ||
for line in lines: | ||
if kernel_line_regex.search(line) is not None: | ||
definition_lines.append(line) | ||
|
||
entry_point_names = [] | ||
kernel_name_regex = re.compile(r"@(.*?)\(") | ||
for line in definition_lines: | ||
if kernel_name_regex.search(line) is None: | ||
continue | ||
kernel_name = kernel_name_regex.search(line).group(1) | ||
if "kernel_wrapper" not in kernel_name and "with_offset" not in kernel_name: | ||
entry_point_names.append(kernel_name) | ||
|
||
os.remove(il_file) | ||
return entry_point_names | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--dpcxx_path", | ||
help="Full path to dpc++ compiler executable.") | ||
parser.add_argument( | ||
"-o", | ||
"--output", | ||
help="Full path to header file that will be generated.") | ||
parser.add_argument("source_files", nargs="+") | ||
args = parser.parse_args() | ||
|
||
mangled_names = {} | ||
|
||
for source_file in args.source_files: | ||
program_name = os.path.splitext(os.path.basename(source_file))[0] | ||
mangled_names[program_name] = get_mangled_names( | ||
args.dpcxx_path, source_file, args.output) | ||
generate_header(args.output, mangled_names) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,42 @@ | ||
# Copyright (C) 2023 Intel Corporation | ||
# SPDX-License-Identifier: MIT | ||
|
||
function(add_device_binary SOURCE_FILE) | ||
macro(add_device_binary SOURCE_FILE) | ||
get_filename_component(KERNEL_NAME ${SOURCE_FILE} NAME_WE) | ||
set(DEVICE_BINARY_DIR "${UR_CONFORMANCE_DEVICE_BINARIES_DIR}/${KERNEL_NAME}") | ||
file(MAKE_DIRECTORY ${DEVICE_BINARY_DIR}) | ||
foreach(TRIPLE ${TARGET_TRIPLES}) | ||
add_custom_target(${KERNEL_NAME}_${TRIPLE}_device_binary | ||
${UR_DPCXX} -fsycl -fsycl-targets=${TRIPLE} -fsycl-device-code-split=off | ||
${SOURCE_FILE} -o "${DEVICE_BINARY_DIR}/${KERNEL_NAME}_${TRIPLE}" | ||
set(EXE_PATH "${DEVICE_BINARY_DIR}/${KERNEL_NAME}_${TRIPLE}") | ||
add_custom_command(OUTPUT ${EXE_PATH} | ||
COMMAND ${UR_DPCXX} -fsycl -fsycl-targets=${TRIPLE} -fsycl-device-code-split=off | ||
${SOURCE_FILE} -o ${EXE_PATH} | ||
COMMAND ${CMAKE_COMMAND} -E env SYCL_DUMP_IMAGES=true | ||
"${DEVICE_BINARY_DIR}/${KERNEL_NAME}_${TRIPLE}" || (exit 0) | ||
${EXE_PATH} || (exit 0) | ||
WORKING_DIRECTORY "${DEVICE_BINARY_DIR}" | ||
COMMAND ${CMAKE_COMMAND} -E remove | ||
"${DEVICE_BINARY_DIR}/${KERNEL_NAME}_${TRIPLE}" | ||
DEPENDS ${SOURCE_FILE} | ||
) | ||
add_dependencies(generate_device_binaries ${KERNEL_NAME}_${TRIPLE}_device_binary) | ||
add_custom_target(generate_${KERNEL_NAME}_${TRIPLE} DEPENDS ${EXE_PATH}) | ||
add_dependencies(generate_device_binaries generate_${KERNEL_NAME}_${TRIPLE}) | ||
endforeach() | ||
endfunction() | ||
list(APPEND DEVICE_CODE_SOURCES ${SOURCE_FILE}) | ||
endmacro() | ||
|
||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/bar.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/fill.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/fill_2d.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/fill_3d.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/fill_usm.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/image_copy.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/mean.cpp) | ||
add_device_binary(${CMAKE_CURRENT_SOURCE_DIR}/spec_constant.cpp) | ||
|
||
set(KERNEL_HEADER ${UR_CONFORMANCE_DEVICE_BINARIES_DIR}/kernel_entry_points.h) | ||
add_custom_command(OUTPUT ${KERNEL_HEADER} | ||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/scripts | ||
COMMAND ${Python3_EXECUTABLE} generate_kernel_header.py | ||
--dpcxx_path ${UR_DPCXX} -o ${KERNEL_HEADER} ${DEVICE_CODE_SOURCES} | ||
DEPENDS ${PROJECT_SOURCE_DIR}/scripts/generate_kernel_header.py | ||
${DEVICE_CODE_SOURCES}) | ||
add_custom_target(kernel_names_header DEPENDS ${KERNEL_HEADER}) | ||
add_dependencies(generate_device_binaries kernel_names_header) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (C) 2023 Intel Corporation | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <CL/sycl.hpp> | ||
|
||
int main() { | ||
size_t array_size = 16; | ||
std::vector<uint32_t> A(array_size, 1); | ||
uint32_t val = 42; | ||
cl::sycl::queue sycl_queue; | ||
uint32_t *data = cl::sycl::malloc_shared<uint32_t>(array_size, sycl_queue); | ||
sycl_queue.submit([&](cl::sycl::handler &cgh) { | ||
cgh.parallel_for<class fill_usm>(cl::sycl::range<1>{array_size}, | ||
[data, val](cl::sycl::item<1> itemId) { | ||
auto id = itemId.get_id(0); | ||
data[id] = val; | ||
}); | ||
}); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.