Skip to content

Commit cecb98f

Browse files
committed
Speedup test runs by compiling C++ only once rather than once per model
1 parent 6617c55 commit cecb98f

6 files changed

Lines changed: 73 additions & 42 deletions

File tree

‎tests/lit/lit/formats/Cpp2RustTest.py‎

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from dataclasses import dataclass
99
from pathlib import Path
1010
from typing import NamedTuple, Optional
11+
import atexit
1112
import difflib
1213
import functools
1314
import json
@@ -91,12 +92,12 @@ class TestContext:
9192
rust_result: Optional[RunResult] = None
9293

9394
@classmethod
94-
def setup(cls, test):
95+
def setup(cls, test, model):
9596
cc_input = Path(test.getFilePath())
9697
is_multi = is_multi_file_test(cc_input)
97-
model = test.getSourcePath().split("/")[-1]
9898
fname = cc_input.name if is_multi else cc_input.stem
99-
tmp_dir = get_temp_dir() / f"{fname}-{model}"
99+
# Tests in different directories may share a file name.
100+
tmp_dir = get_temp_dir() / f"{cc_input.parent.name}-{fname}-{model}"
100101

101102
shutil.rmtree(tmp_dir, ignore_errors=True)
102103
tmp_dir.mkdir(parents=True)
@@ -191,6 +192,8 @@ def check_expected(self):
191192

192193
def build_cpp(self):
193194
exp = self.expectations
195+
if self.cpp_bin is not None:
196+
return None
194197
if self.build_dir is not None:
195198
cmd = ["cmake", "--build", str(self.build_dir)]
196199
_, _, rc = lit.util.executeCommand(cmd)
@@ -203,7 +206,6 @@ def build_cpp(self):
203206
self.cpp_bin = self.tmp_dir / "cpp"
204207
cmd = [
205208
cc,
206-
"-O3",
207209
"-o",
208210
str(self.cpp_bin),
209211
str(self.cc_input),
@@ -229,7 +231,9 @@ def build_rust(self):
229231
"-A",
230232
"warnings",
231233
"-C",
232-
"opt-level=3", # Equivalent to --release
234+
"opt-level=0",
235+
"-C",
236+
"debug-assertions=off", # On by default at opt-level=0
233237
"-C",
234238
"strip=symbols",
235239
"-C",
@@ -255,9 +259,10 @@ def build_rust(self):
255259
def run_cpp(self):
256260
if self.skip_run:
257261
return None
258-
self.cpp_result = RunResult(
259-
*lit.util.executeCommand(str(self.cpp_bin), str(self.tmp_dir))
260-
)
262+
if self.cpp_result is None:
263+
self.cpp_result = RunResult(
264+
*lit.util.executeCommand(str(self.cpp_bin), str(self.tmp_dir))
265+
)
261266

262267
if (
263268
not self.expectations.should_panic_ub
@@ -325,50 +330,69 @@ def success_result(self):
325330
return (lit.Test.FAIL, "did not fail as expected")
326331
return (lit.Test.PASS, "")
327332

328-
def finalize(self, result):
333+
def finalize(self):
329334
shutil.rmtree(self.tmp_dir, ignore_errors=True)
330-
return result
331335

332336

333337
class Cpp2RustTest(TestFormat):
334338
def __init__(self):
335339
os.environ["RUSTFLAGS"] = "-Awarnings"
340+
# Runs once, in the main lit process. Each test removes its own dirs,
341+
# so this only removes the top-level dir.
342+
atexit.register(shutil.rmtree, get_temp_dir(), ignore_errors=True)
336343

337344
def execute(self, test, litConfig):
338-
ctx = TestContext.setup(test)
339-
result = (
340-
ctx.translate()
341-
or ctx.check_expected()
342-
or ctx.build_cpp()
343-
or ctx.build_rust()
344-
or ctx.run_cpp()
345-
or ctx.run_rust()
346-
or ctx.compare()
347-
or ctx.success_result()
348-
)
349-
return ctx.finalize(result)
350-
351-
def getTestsForPath(self, testSuite, path_in_suite, litConfig, localConfig):
352-
source_path = testSuite.getSourcePath(path_in_suite)
345+
contexts = []
346+
results = []
353347
for model in MODELS:
354-
yield lit.Test.Test(
355-
testSuite, path_in_suite + (model,), localConfig, source_path
348+
ctx = TestContext.setup(test, model)
349+
if contexts:
350+
# Reuse the C++ binary and its output from the previous model.
351+
ctx.cpp_bin = contexts[-1].cpp_bin
352+
ctx.cpp_result = contexts[-1].cpp_result
353+
contexts.append(ctx)
354+
result = (
355+
ctx.translate()
356+
or ctx.check_expected()
357+
or ctx.build_cpp()
358+
or ctx.build_rust()
359+
or ctx.run_cpp()
360+
or ctx.run_rust()
361+
or ctx.compare()
362+
or ctx.success_result()
356363
)
364+
results.append((model, result))
365+
# The C++ binary lives in the first model's dir, so clean up at the end.
366+
for ctx in contexts:
367+
ctx.finalize()
368+
return combine_results(results)
357369

358370
def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
359371
source_path = Path(testSuite.getSourcePath(path_in_suite))
360372
if is_multi_file_test(source_path):
361-
for t in self.getTestsForPath(
362-
testSuite, path_in_suite, litConfig, localConfig
363-
):
364-
yield t
373+
yield lit.Test.Test(testSuite, path_in_suite, localConfig)
365374
return
366375
for entry in source_path.iterdir():
367376
if entry.is_file() and entry.suffix in (".cpp", ".c"):
368-
for t in self.getTestsForPath(
369-
testSuite, path_in_suite + (entry.name,), litConfig, localConfig
370-
):
371-
yield t
377+
yield lit.Test.Test(
378+
testSuite, path_in_suite + (entry.name,), localConfig
379+
)
380+
381+
382+
def combine_results(results):
383+
"""Reports the first model that failed; otherwise XFAIL if some model failed
384+
as expected, and PASS if none did."""
385+
codes = [code for _, (code, _) in results]
386+
code = next(
387+
(c for c in codes if c.isFailure),
388+
lit.Test.XFAIL if lit.Test.XFAIL in codes else lit.Test.PASS,
389+
)
390+
output = "".join(
391+
f"{model}: {c.name}\n{out}\n"
392+
for model, (c, out) in results
393+
if c != lit.Test.PASS
394+
)
395+
return (code, output)
372396

373397

374398
def read_rust_version():

‎tests/ub/ctor_ref_member.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// panic-ub: refcount
2+
// nondet-result: unsafe
23

34
#include <cassert>
45

‎tests/unit/libc_struct_without_default.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ int main() {
4242
assert(st.st_size == 1024);
4343

4444
UserDefined ud;
45-
assert(ud.a[0] == 0);
45+
ud.a[0] = 3;
46+
assert(ud.a[0] == 3);
4647
assert(ud.v.size() == 0);
4748

4849
FieldIsLibcType filt;
49-
assert(filt.addr.sa_family == 0);
50+
filt.addr.sa_family = 2;
51+
assert(filt.addr.sa_family == 2);
5052
return 0;
5153
}

‎tests/unit/out/refcount/libc_struct_without_default.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ fn main_0() -> i32 {
7878
(*st.borrow_mut()).st_size = (1024 as ::libc::off_t);
7979
assert!(({ ({ (*st.borrow()).st_size } == (1024 as ::libc::off_t)) }));
8080
let ud: Value<UserDefined> = Rc::new(RefCell::new(<UserDefined>::default()));
81-
assert!(((elem!(field_ref!(ud.as_pointer(), a).decay(), 0_usize).read()) == 0));
81+
elem!(field_ref!(ud.as_pointer(), a).decay(), 0_usize).write(3);
82+
assert!(((elem!(field_ref!(ud.as_pointer(), a).decay(), 0_usize).read()) == 3));
8283
assert!(({ ((*ud.borrow()).v.len() == 0_usize) }));
8384
let mut filt: FieldIsLibcType = <FieldIsLibcType>::default();
84-
assert!(((filt.addr.sa_family as i32) == 0));
85+
filt.addr.sa_family = (2 as libc::sa_family_t);
86+
assert!(((filt.addr.sa_family as i32) == 2));
8587
return 0;
8688
}
8789
pub trait UserDefinedImpl {

‎tests/unit/out/unsafe/libc_struct_without_default.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ unsafe fn main_0() -> i32 {
8080
st.st_size = (1024 as ::libc::off_t);
8181
assert!(((st.st_size) == (1024 as ::libc::off_t)));
8282
let mut ud: UserDefined = <UserDefined>::default();
83-
assert!(((ud.a[(0_usize)]) == (0)));
83+
ud.a[(0_usize)] = 3;
84+
assert!(((ud.a[(0_usize)]) == (3)));
8485
assert!(((ud.v.len()) == (0_usize)));
8586
let mut filt: FieldIsLibcType = <FieldIsLibcType>::default();
86-
assert!(((filt.addr.sa_family as i32) == (0)));
87+
filt.addr.sa_family = (2 as libc::sa_family_t);
88+
assert!(((filt.addr.sa_family as i32) == (2)));
8789
return 0;
8890
}
8991
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/this.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct S {
1414
}
1515
}
1616

17-
S(int a, const S *other) : a_(a) {
17+
S(int a, const S *other) : a_(a), self_(nullptr) {
1818
if (this == other) {
1919
self_ = nullptr;
2020
}

0 commit comments

Comments
 (0)