Skip to content

Commit bd163cb

Browse files
committed
implement gen sharding
1 parent 1d20ce7 commit bd163cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+313
-729
lines changed

projects/eudsl-py/CMakeLists.txt

Lines changed: 94 additions & 110 deletions
Large diffs are not rendered by default.

projects/eudsl-py/cmake/EUDSLPYConfig.cmake

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function(add_public_eudslpygen_target target eudslpygen_output)
3131
set_target_properties(${target} PROPERTIES FOLDER "${subproject_title}/Eudslpygenning")
3232
endfunction()
3333

34-
function(eudslpygen target inputFile outputFileName)
34+
function(eudslpygen target inputFile)
3535
set(EUDSLPYGEN_TARGET_DEFINITIONS ${inputFile})
3636
# Get the current set of include paths for this source file.
3737
cmake_parse_arguments(ARG "" "" "DEPENDS;EXTRA_INCLUDES;NAMESPACES" ${ARGN})
@@ -52,35 +52,41 @@ function(eudslpygen target inputFile outputFileName)
5252
# Filter out empty items before prepending each entry with -I
5353
list(REMOVE_ITEM eudslpygen_includes "")
5454
list(TRANSFORM eudslpygen_includes PREPEND -I)
55+
56+
set(_gen_target_dir "${CMAKE_CURRENT_BINARY_DIR}/generated/${target}")
57+
file(MAKE_DIRECTORY ${_gen_target_dir})
58+
set(fullGenFile "${_gen_target_dir}/${target}.cpp.gen")
59+
file(RELATIVE_PATH fullGenFile_rel "${CMAKE_BINARY_DIR}" "${fullGenFile}")
60+
set(_depfile ${fullGenFile}.d)
5561

5662
# hack but most of the time we're loading headers that are downstream of tds anyway
5763
# this could be smarter by asking people to list td targets or something but that's too onerous
5864
file(GLOB_RECURSE global_tds "${MLIR_INCLUDE_DIR}/mlir/*.td")
5965
# use cc -MM to collect all transitive headers
60-
set(_depfile ${CMAKE_CURRENT_BINARY_DIR}/${outputFileName}.d)
61-
file(RELATIVE_PATH outputFileName_rel
62-
"${CMAKE_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/${outputFileName}")
63-
set(clang_command ${CMAKE_CXX_COMPILER} -v -xc++ "-std=c++${CMAKE_CXX_STANDARD}" -MM ${EUDSLPYGEN_TARGET_DEFINITIONS_ABSOLUTE}
64-
${eudslpygen_includes} -MT ${outputFileName_rel} -o ${_depfile})
66+
set(clang_command ${CMAKE_CXX_COMPILER} -v -xc++ "-std=c++${CMAKE_CXX_STANDARD}"
67+
-MM ${EUDSLPYGEN_TARGET_DEFINITIONS_ABSOLUTE}
68+
-MT ${fullGenFile_rel}
69+
${eudslpygen_includes}
70+
-o ${_depfile})
6571
execute_process(COMMAND ${clang_command} RESULT_VARIABLE had_error COMMAND_ECHO STDERR)
6672
if(had_error OR NOT EXISTS "${_depfile}")
67-
set(additional_cmdline -o "${outputFileName_rel}")
73+
set(additional_cmdline -o "${fullGenFile_rel}")
6874
else()
6975
# Use depfile instead of globbing arbitrary *.td(s) for Ninja.
7076
if(CMAKE_GENERATOR MATCHES "Ninja")
7177
# Make output path relative to build.ninja, assuming located on ${CMAKE_BINARY_DIR}.
7278
# CMake emits build targets as relative paths but Ninja doesn't identify
7379
# absolute path (in *.d) as relative path (in build.ninja)
7480
# Note that eudslpygen is executed on ${CMAKE_BINARY_DIR} as working directory.
75-
set(additional_cmdline -o "${outputFileName_rel}" DEPFILE "${_depfile}")
81+
set(additional_cmdline -o "${fullGenFile_rel}" DEPFILE "${_depfile}")
7682
else()
7783
# the length of the first line in the depfile...
78-
string(LENGTH "${outputFileName_rel}: \\" depfile_offset)
84+
string(LENGTH "${fullGenFile_rel}: \\" depfile_offset)
7985
file(READ ${_depfile} local_headers OFFSET ${depfile_offset})
8086
string(REPLACE "\\" ";" local_headers "${local_headers}")
8187
string(REGEX REPLACE "[ \t\r\n]" "" local_headers "${local_headers}")
8288
list(REMOVE_ITEM local_headers "")
83-
set(additional_cmdline -o "${outputFileName_rel}")
89+
set(additional_cmdline -o "${fullGenFile_rel}")
8490
endif()
8591
endif()
8692

@@ -106,24 +112,39 @@ function(eudslpygen target inputFile outputFileName)
106112
string(REPLACE " " ";" eudslpygen_defines "${LLVM_DEFINITIONS}")
107113
list(JOIN ARG_NAMESPACES "," namespaces)
108114

109-
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${outputFileName}
110-
COMMAND ${eudslpygen_exe} ${EUDSLPYGEN_TARGET_DEFINITIONS_ABSOLUTE} -I${CMAKE_CURRENT_SOURCE_DIR}
111-
-namespaces=${namespaces}
112-
${eudslpygen_includes}
113-
${eudslpygen_defines}
114-
${additional_cmdline}
115+
add_custom_command(OUTPUT ${fullGenFile}
116+
COMMAND ${eudslpygen_exe} ${EUDSLPYGEN_TARGET_DEFINITIONS_ABSOLUTE}
117+
-I${CMAKE_CURRENT_SOURCE_DIR}
118+
-namespaces=${namespaces}
119+
${eudslpygen_includes}
120+
${eudslpygen_defines}
121+
${additional_cmdline}
115122
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
116123
# The file in EUDSLPYGEN_TARGET_DEFINITIONS may be not in the current
117124
# directory and local_headers may not contain it, so we must
118125
# explicitly list it here:
119126
DEPENDS ${ARG_DEPENDS} ${eudslpygen_depends} ${local_headers} ${global_tds}
120-
COMMENT "EUDSLPY: Generating ${outputFileName}..."
127+
COMMENT "EUDSLPY: Generating ${fullGenFile}..."
128+
)
129+
130+
add_custom_command(OUTPUT "${fullGenFile}.sharded.cpp"
131+
COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/make_generated_registration.py
132+
${fullGenFile} -t ${target} -I ${ARG_EXTRA_INCLUDES} ${EUDSLPYGEN_TARGET_DEFINITIONS_ABSOLUTE}
133+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
134+
DEPENDS ${fullGenFile}
135+
COMMENT "EUDSLPY: Generating ${fullGenFile}.sharded.cpp..."
121136
)
122137

138+
# this is the specific thing connected the dependencies...
139+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${fullGenFile}.sharded.cpp")
140+
file(GLOB _generated_shards "${_gen_target_dir}/*.shard.*")
141+
list(APPEND _generated_shards "${fullGenFile}.sharded.cpp")
142+
set(${target}_GENERATED_SHARDS ${_generated_shards} PARENT_SCOPE)
143+
123144
# `make clean' must remove all those generated files:
124145
# TODO(max): clean up dep files
125-
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${outputFileName})
126-
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${outputFileName} PROPERTIES
146+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${fullGenFile})
147+
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${fullGenFile} PROPERTIES
127148
GENERATED 1)
128149

129150
# Append the includes used for this file to the pdll_compilation_commands
@@ -134,7 +155,7 @@ function(eudslpygen target inputFile outputFileName)
134155
" includes: \"${CMAKE_CURRENT_SOURCE_DIR};${eudslpygen_includes}\"\n"
135156
)
136157

137-
add_public_eudslpygen_target(${target} "${CMAKE_CURRENT_BINARY_DIR}/${outputFileName}")
158+
add_public_eudslpygen_target(${target} "${fullGenFile}.sharded.cpp;${_generated_shards}")
138159
endfunction()
139160

140161
macro(add_eudslpygen target project)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import argparse
2+
import re
3+
from pathlib import Path
4+
from textwrap import dedent
5+
6+
7+
def make_source_shards(filename: Path, target, extra_includes):
8+
assert filename.name.endswith("cpp.gen"), "expected .cpp.gen file"
9+
with open(filename) as f:
10+
source = f.read()
11+
shards = re.split(r"// eudslpy-gen-shard \d+", source)
12+
if target is None:
13+
target = filename.stem.split(".")[0]
14+
for i, shar in enumerate(shards):
15+
with open(f"{filename}.shard.{i}.cpp", "w") as f:
16+
if extra_includes is not None:
17+
for inc in extra_includes:
18+
print(f'#include "{inc}"', file=f)
19+
print(
20+
dedent(
21+
f"""\
22+
#include <nanobind/nanobind.h>
23+
namespace nb = nanobind;
24+
using namespace nb::literals;
25+
using namespace mlir;
26+
using namespace llvm;
27+
#include "type_casters.h"
28+
29+
void populate{target}{i}Module(nb::module_ &m) {{"""
30+
),
31+
file=f,
32+
)
33+
print(shar, file=f)
34+
print("}", file=f)
35+
36+
with open(f"{filename}.sharded.cpp", "w") as f:
37+
print(
38+
dedent(
39+
f"""\
40+
#include <nanobind/nanobind.h>
41+
namespace nb = nanobind;
42+
using namespace nb::literals;
43+
void populate{target}Module(nb::module_ &m) {{"""
44+
),
45+
file=f,
46+
)
47+
for i in range(len(shards)):
48+
print(dedent(f"extern void populate{target}{i}Module(nb::module_ &m);"), file=f)
49+
for i in range(len(shards)):
50+
print(dedent(f"populate{target}{i}Module(m);"), file=f)
51+
52+
print("}", file=f)
53+
54+
55+
if __name__ == "__main__":
56+
parser = argparse.ArgumentParser()
57+
parser.add_argument("filename")
58+
parser.add_argument("-t", "--target")
59+
parser.add_argument("-I", "--extra_includes", nargs="*")
60+
args = parser.parse_args()
61+
make_source_shards(Path(args.filename), args.target, args.extra_includes)

projects/eudsl-py/src/EUDSLGenDLTIDialect.inc.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

projects/eudsl-py/src/EUDSLGenLLVM.inc.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

projects/eudsl-py/src/EUDSLGenNVVM.inc.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

projects/eudsl-py/src/EUDSLGenROCDL.inc.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

projects/eudsl-py/src/EUDSLGenacc.inc.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

projects/eudsl-py/src/EUDSLGenaffine.inc.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.

projects/eudsl-py/src/EUDSLGenamdgpu.inc.cpp

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)