Skip to content

Commit c71956d

Browse files
[MLGO] Add tests for scripts (#78878)
This patch adds integration tests for the script entry points. The tests don't exercise all functionality, as that case is better covered by the unit testing already checked in. This ensures that things like flag parsing work and that the scripts are syntactically valid.
1 parent 86b6dfc commit c71956d

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

llvm/utils/mlgo-utils/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ configure_lit_site_cfg(
55

66
add_lit_testsuite(check-mlgo-utils "Running mlgo-utils tests"
77
${CMAKE_CURRENT_BINARY_DIR}
8-
DEPENDS "FileCheck" "not" "count"
8+
DEPENDS "FileCheck" "not" "count" "split-file" "yaml2obj" "llvm-objcopy"
99
)
1010

1111
set_target_properties(check-mlgo-utils PROPERTIES FOLDER "Tests")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# REQUIRES: python-38, absl, system-linux
2+
3+
## Testing that the combine_trainig_corpus script works as expected when
4+
## invoked.
5+
6+
# RUN: rm -rf %t.dir && mkdir %t.dir
7+
# RUN: split-file %s %t.dir
8+
# RUN: %python %scripts_dir/corpus/combine_training_corpus.py --root_dir=%t.dir
9+
# RUN: cat %t.dir/corpus_description.json | FileCheck %s
10+
11+
## Check that we end up with the same properties as the original corpora
12+
# CHECK: "has_thinlto": false
13+
14+
## Check that the modules end up in the combined corpus. Order does not matter.
15+
# CHECK-DAG: "subcorpus1/test1.o"
16+
# CHECK-DAG: "subcorpus2/test2.o"
17+
18+
#--- subcorpus1/corpus_description.json
19+
{
20+
"has_thinlto": false,
21+
"modules": [
22+
"test1.o"
23+
]
24+
}
25+
#--- subcorpus2/corpus_description.json
26+
{
27+
"has_thinlto": false,
28+
"modules": [
29+
"test2.o"
30+
]
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# REQUIRES: python-38, absl, system-linux
2+
3+
## Test that invoking the extract_ir script work as expected.
4+
5+
# RUN: rm -rf %t.dir && mkdir %t.dir
6+
# RUN: yaml2obj %s -o %t.dir/test1.o
7+
# RUN: yaml2obj %s -o %t.dir/test2.o
8+
# RUN: rm -rf %t.dir.out && mkdir %t.dir.out
9+
10+
# RUN: %python %scripts_dir/corpus/extract_ir.py --input=%t.dir --input_type=directory --output_dir=%t.dir.out --llvm_objcopy_path=llvm-objcopy
11+
# RUN: cat %t.dir.out/corpus_description.json | FileCheck %s
12+
13+
## Check that this is not a thinLTO build
14+
# CHECK: "has_thinlto": false
15+
## Check that the expected modules end up in the corpus description
16+
# CHECK-DAG: "test1.o"
17+
# CHECK-DAG: "test2.o"
18+
19+
# RUN: ls %t.dir.out | FileCheck %s --check-prefix CHECK-DIR
20+
21+
# CHECK-DIR: test1.o.bc
22+
# CHECK-DIR: test1.o.cmd
23+
# CHECK-DIR: test2.o.bc
24+
# CHECK-DIR: test2.o.cmd
25+
26+
--- !ELF
27+
FileHeader:
28+
Class: ELFCLASS64
29+
Data: ELFDATA2LSB
30+
Type: ET_REL
31+
Machine: EM_X86_64
32+
SectionHeaderStringTable: .strtab
33+
Sections:
34+
- Name: .text
35+
Type: SHT_PROGBITS
36+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
37+
AddressAlign: 0x10
38+
Content: 55
39+
- Name: .llvmbc
40+
Type: SHT_PROGBITS
41+
AddressAlign: 0x1
42+
Content: 55
43+
- Name: .llvmcmd
44+
Type: SHT_PROGBITS
45+
AddressAlign: 0x1
46+
Content: ff
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# REQUIRES: python-38, absl, system-linux
2+
3+
## Testing that the make_corpus script works as expected when invoked.
4+
5+
# RUN: rm -rf %t.dir && mkdir %t.dir
6+
# RUN: touch %t.dir/test1.bc
7+
# RUN: touch %t.dir/test2.bc
8+
# RUN: rm -rf %t.out.dir && mkdir %t.out.dir
9+
10+
# RUN: %python %scripts_dir/corpus/make_corpus.py --input_dir=%t.dir --output_dir=%t.out.dir --default_args="-test"
11+
12+
# RUN: cat %t.out.dir/corpus_description.json | FileCheck %s
13+
14+
## Check that we get the expected command in the global command override
15+
# CHECK: "-test"
16+
# CHECK: "has_thinlto": false
17+
## Check that the modules are in the corpus description
18+
# CHECK: "test1"
19+
# CHECK: "test2"
20+
21+
# RUN: ls %t.out.dir | FileCheck %s --check-prefix CHECK-DIR
22+
23+
# CHECK-DIR: test1.bc
24+
# CHECK-DIR: test2.bc

llvm/utils/mlgo-utils/tests/lit.cfg

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
import os
2+
13
import lit.formats
24

35
from lit.llvm import llvm_config
46

57
config.name = "mlgo-utils"
68
config.test_format = lit.formats.ShTest(execute_external=False)
79

8-
config.suffixes = [".py"]
10+
config.suffixes = [".py", ".test"]
911

1012
config.test_source_root = os.path.dirname(__file__)
1113
config.test_exec_root = config.obj_root
1214

1315
config.environment["PYTHONPATH"] = os.path.join(config.src_root, "utils", "mlgo-utils")
1416

1517
llvm_config.use_default_substitutions()
18+
config.substitutions.append(("split-file", llvm_config.use_llvm_tool("split-file")))
19+
config.substitutions.append(("yaml2obj", llvm_config.use_llvm_tool("yaml2obj")))
20+
config.substitutions.append(("llvm-objcopy", llvm_config.use_llvm_tool("llvm-objcopy")))
21+
22+
scripts_dir = os.path.join(config.src_root, "utils/mlgo-utils/mlgo")
23+
config.substitutions.append(("%scripts_dir", scripts_dir))

0 commit comments

Comments
 (0)