Repository navigation
Expand file tree
/
Copy pathgen_coverage.py
More file actions
406 lines (356 loc) · 12.2 KB
/
Copy pathgen_coverage.py
File metadata and controls
406 lines (356 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# *****************************************************************************
# Copyright (c) 2023, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
import argparse
import os
import subprocess
import sys
from _build_helper import (
build_extension,
capture_cmd_output,
clean_build_dir,
err,
get_dpctl_cmake_dir,
install_editable,
log_cmake_args,
make_cmake_args,
resolve_compilers,
run,
)
# in the order they are run, see --stage option
_STAGES = ("build", "dpnp-tests", "tensor-tests", "report")
_DPNP_PROFRAW = "dpnp_pytest.profraw"
_TENSOR_PROFRAW = "dpnp_tensor_pytest.profraw"
# pybind11 modules of dpnp/libtensor holding libtensor functions only, unlike
# _tensor_impl, _tensor_elementwise_impl and _tensor_sorting_impl which hold
# dpnp functions too
_TENSOR_ONLY_OBJS = (
"_tensor_accumulation_impl.",
"_tensor_linalg_impl.",
"_tensor_reductions_impl.",
)
def parse_args():
p = argparse.ArgumentParser(description="Build dpnp and generate coverage")
# compiler and oneAPI relating options
p.add_argument(
"--c-compiler", default=None, help="Path or name of C compiler"
)
p.add_argument(
"--cxx-compiler", default=None, help="Path or name of C++ compiler"
)
p.add_argument(
"--compiler-root",
type=str,
default=None,
help="Path to compiler installation root",
)
p.add_argument(
"--oneapi",
dest="oneapi",
action="store_true",
help="Use default oneAPI compiler layout",
)
p.add_argument(
"--bin-llvm",
type=str,
default=None,
help="Path to folder where llvm-cov/llvm-profdata can be found",
)
# CMake relating options
p.add_argument(
"--generator", type=str, default="Ninja", help="CMake generator"
)
p.add_argument(
"--cmake-executable",
type=str,
default=None,
help="Path to CMake executable used by build",
)
p.add_argument(
"--cmake-opts",
type=str,
default="",
help="Additional options to pass directly to CMake",
)
p.add_argument(
"--verbose",
dest="verbose",
action="store_true",
help="Enable verbose makefile output",
)
# test relating options
p.add_argument(
"--skip-pytest",
dest="run_pytest",
action="store_false",
help="Skip running pytest and coverage generation",
)
p.add_argument(
"--pytest-opts",
help="Channels through additional pytest options",
dest="pytest_opts",
default="",
type=str,
)
p.add_argument(
"--skip-tensor-tests",
dest="run_tensor_tests",
action="store_false",
help="Skip running dpnp.tensor tests (tensor-tests stage)",
)
p.add_argument(
"--tensor-cpp-coverage",
dest="tensor_cpp_coverage",
action="store_true",
help="Build dpnp.tensor pybind11 extensions with coverage "
"instrumentation and include them into C++ coverage report "
"(pass to both build and report stages)",
)
p.add_argument(
"--stage",
dest="stages",
action="append",
choices=_STAGES,
help="Stage to run, can be repeated (default: all stages). "
"Allows to run and retry each stage in a separate CI step",
)
# build relating options
p.add_argument(
"--clean",
action="store_true",
help="Remove build dir before rebuild (default: False)",
)
return p.parse_args()
def find_bin_llvm(compiler):
if os.path.isabs(compiler):
bin_dir = os.path.dirname(compiler)
else:
compiler_path = capture_cmd_output(["which", compiler])
if not compiler_path:
raise RuntimeError(f"Compiler {compiler} not found in PATH")
bin_dir = os.path.dirname(compiler_path)
compiler_dir = os.path.join(bin_dir, "compiler")
if os.path.exists(compiler_dir):
bin_llvm = compiler_dir
else:
bin_dir = os.path.dirname(bin_dir)
bin_llvm = os.path.join(bin_dir, "bin-llvm")
if not os.path.exists(bin_llvm):
raise RuntimeError(
f"Path to folder with llvm-cov/llvm-profdata={bin_llvm} "
"seems to not exist"
)
return bin_llvm
def build_dpnp(args, setup_dir, env, c_compiler, cxx_compiler):
dpctl_cmake_dir = get_dpctl_cmake_dir()
print(f"[gen_coverage] Found DPCTL CMake dir: {dpctl_cmake_dir}")
if args.clean:
clean_build_dir(setup_dir)
cmake_args = make_cmake_args(
c_compiler=c_compiler,
cxx_compiler=cxx_compiler,
dpctl_cmake_dir=dpctl_cmake_dir,
verbose=args.verbose,
)
cmake_args.append("-DDPNP_GENERATE_COVERAGE=ON")
if args.tensor_cpp_coverage:
cmake_args.append(
"-DDPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS=ON"
)
log_cmake_args(cmake_args, "gen_coverage")
build_extension(
setup_dir,
env,
cmake_args,
cmake_executable=args.cmake_executable,
generator=args.generator,
build_type="Coverage",
)
install_editable(setup_dir, env)
def run_pytest(args, setup_dir, env, tensor_tests):
run(
[
"pytest",
"-q",
"-ra",
"--disable-warnings",
"--cov-config",
"pyproject.toml",
"--cov",
"dpnp",
# reported by report stage, on the data of all sessions
"--cov-report=",
# tensor session appends to the data of dpnp one
*(["--cov-append"] if tensor_tests else []),
"--pyargs",
"dpnp.tests.tensor" if tensor_tests else "dpnp",
*args.pytest_opts.split(),
],
env={
**env,
"SKIP_TENSOR_TESTS": "0" if tensor_tests else "1",
"LLVM_PROFILE_FILE": (
_TENSOR_PROFRAW if tensor_tests else _DPNP_PROFRAW
),
},
cwd=setup_dir,
)
def export_coverage(args, setup_dir, env, bin_llvm):
# python coverage of all test sessions
run(
["coverage", "report", "-m", "--rcfile", "pyproject.toml"],
env=env,
cwd=setup_dir,
)
run(
[
"coverage",
"lcov",
"--rcfile",
"pyproject.toml",
"-o",
"coverage-python.lcov",
],
env=env,
cwd=setup_dir,
)
expected_profraw_files = [_DPNP_PROFRAW]
if args.run_tensor_tests:
expected_profraw_files.append(_TENSOR_PROFRAW)
profraw_files = []
for fn in expected_profraw_files:
if os.path.exists(os.path.join(setup_dir, fn)):
profraw_files.append(fn)
else:
# e.g. the stage failed, report on the rest of data
print(f"[gen_coverage] Warning: {fn} not found, skipping it")
if not profraw_files:
err("no LLVM profile data found", "gen_coverage")
def find_objects():
objects = []
search_path = os.path.join(setup_dir, "dpnp")
for root, _, files in os.walk(search_path):
for file in files:
is_impl_obj = (
os.path.basename(root) == "libtensor"
and file.find("_impl.cpython") != -1
)
# modules with libtensor functions only are instrumented with
# --tensor-cpp-coverage only
is_tensor_obj = file.startswith(_TENSOR_ONLY_OBJS)
if file.endswith("_c.so") or (
is_impl_obj
and (args.tensor_cpp_coverage or not is_tensor_obj)
):
objects.extend(["-object", os.path.join(root, file)])
return objects
objects = find_objects()
instr_profile_fn = "dpnp_pytest.profdata"
# generate instrumentation profile data
run(
[
os.path.join(bin_llvm, "llvm-profdata"),
"merge",
"-sparse",
*profraw_files,
"-o",
instr_profile_fn,
],
cwd=setup_dir,
)
# export lcov
with open(os.path.join(setup_dir, "coverage-cpp.lcov"), "w") as fh:
subprocess.check_call(
[
os.path.join(bin_llvm, "llvm-cov"),
"export",
"-format=lcov",
"-ignore-filename-regex=/tmp/icpx*",
r"-ignore-filename-regex=.*/libtensor/(include|extensions/[^/]+)/kernels/.*\.hpp$",
"-instr-profile=" + instr_profile_fn,
]
+ objects
+ ["-sources", "dpnp"],
cwd=setup_dir,
env=env,
stdout=fh,
)
print("[gen_coverage] Coverage export is completed")
def main():
is_linux = "linux" in sys.platform
if not is_linux:
err(f"{sys.platform} not supported", "gen_coverage")
args = parse_args()
setup_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# keep the order of _STAGES, whatever the order of --stage options
stages = [
s
for s in _STAGES
if s in (args.stages or _STAGES)
and (args.run_pytest or s == "build")
and (args.run_tensor_tests or s != "tensor-tests")
]
c_compiler, cxx_compiler = resolve_compilers(
args.oneapi,
args.c_compiler,
args.cxx_compiler,
args.compiler_root,
)
env = os.environ.copy()
if args.bin_llvm:
bin_llvm = args.bin_llvm
else:
bin_llvm = find_bin_llvm(c_compiler)
print(
f"[gen_coverage] Path to folder with llvm-cov/llvm-profdata: {bin_llvm}"
)
if bin_llvm:
env["PATH"] = ":".join((env.get("PATH", ""), bin_llvm))
env["LLVM_TOOLS_HOME"] = bin_llvm
if "build" in stages:
build_dpnp(args, setup_dir, env, c_compiler, cxx_compiler)
if "dpnp-tests" in stages:
# start from scratch, as pytest-cov erases .coverage w/o --cov-append
for fn in (_DPNP_PROFRAW, _TENSOR_PROFRAW):
fn = os.path.join(setup_dir, fn)
if os.path.exists(fn):
os.remove(fn)
# dpnp tests w/o tensor ones, those are run by tensor-tests stage
run_pytest(args, setup_dir, env, tensor_tests=False)
if "tensor-tests" in stages:
# separate session to limit peak memory
run_pytest(args, setup_dir, env, tensor_tests=True)
if "report" in stages:
export_coverage(args, setup_dir, env, bin_llvm)
if not args.run_pytest:
print(
"[gen_coverage] Skipping pytest and coverage collection "
"(--skip-pytest)"
)
print("[gen_coverage] Done")
if __name__ == "__main__":
main()