-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[utils] Add script to generate elaborated IR and assembly tests #89026
Merged
MaskRay
merged 8 commits into
main
from
users/MaskRay/spr/utils-add-script-to-generate-elaborated-assembly-tests
May 9, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
30967cf
[𝘀𝗽𝗿] initial version
MaskRay 105063b
add discourse link
MaskRay b9c98dc
add --shell for debugging. set euo pipefail
MaskRay dceadc3
make `ruff check` happy. properly set return code
MaskRay a75a340
add split-file-fail.test (forgot in the last commit). we cannot test …
MaskRay c9e4841
remove -o pipefail due to dash
MaskRay 23affca
don't require .ifdef/.endif
MaskRay ad7b4cc
don't need ``-fdebug-compilation-dir=`` since its default value is ``…
MaskRay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
13 changes: 13 additions & 0 deletions
13
llvm/test/tools/UpdateTestChecks/update_test_body/Inputs/basic-asm.test.expected
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,13 @@ | ||
# RUN: cp %s %t && %update_test_body %t 2>&1 | count 0 | ||
# RUN: diff -u %S/Inputs/basic-asm.test.expected %t | ||
|
||
.ifdef GEN | ||
#--- a.txt | ||
.long 0 | ||
#--- b.txt | ||
.long 1 | ||
#--- gen | ||
cat a.txt b.txt | ||
.endif | ||
.long 0 | ||
.long 1 |
16 changes: 16 additions & 0 deletions
16
llvm/test/tools/UpdateTestChecks/update_test_body/Inputs/basic.test.expected
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,16 @@ | ||
; RUN: cp %s %t && %update_test_body %t 2>&1 | count 0 | ||
; RUN: diff -u %S/Inputs/basic.test.expected %t | ||
|
||
;--- a.txt | ||
@a = global i32 0 | ||
;--- b.txt | ||
@b = global i32 0 | ||
;--- gen | ||
cat a.txt | ||
echo ';--- b.ll' | ||
cat b.txt | ||
|
||
;--- a.ll | ||
@a = global i32 0 | ||
;--- b.ll | ||
@b = global i32 0 |
11 changes: 11 additions & 0 deletions
11
llvm/test/tools/UpdateTestChecks/update_test_body/basic-asm.test
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,11 @@ | ||
# RUN: cp %s %t && %update_test_body %t 2>&1 | count 0 | ||
# RUN: diff -u %S/Inputs/basic-asm.test.expected %t | ||
|
||
.ifdef GEN | ||
#--- a.txt | ||
.long 0 | ||
#--- b.txt | ||
.long 1 | ||
#--- gen | ||
cat a.txt b.txt | ||
.endif |
13 changes: 13 additions & 0 deletions
13
llvm/test/tools/UpdateTestChecks/update_test_body/basic.test
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,13 @@ | ||
; RUN: cp %s %t && %update_test_body %t 2>&1 | count 0 | ||
; RUN: diff -u %S/Inputs/basic.test.expected %t | ||
|
||
;--- a.txt | ||
@a = global i32 0 | ||
;--- b.txt | ||
@b = global i32 0 | ||
;--- gen | ||
cat a.txt | ||
echo ';--- b.ll' | ||
cat b.txt | ||
|
||
;--- a.ll |
13 changes: 13 additions & 0 deletions
13
llvm/test/tools/UpdateTestChecks/update_test_body/empty-stdout.test
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,13 @@ | ||
# RUN: cp %s %t && not %update_test_body %t 2>&1 | FileCheck %s | ||
# RUN: diff -u %t %s | ||
|
||
# CHECK: stdout is empty; forgot -o - ? | ||
|
||
.ifdef GEN | ||
#--- a.txt | ||
.long 0 | ||
#--- b.txt | ||
.long 1 | ||
#--- gen | ||
true | ||
.endif |
7 changes: 7 additions & 0 deletions
7
llvm/test/tools/UpdateTestChecks/update_test_body/gen-absent.test
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,7 @@ | ||
# RUN: cp %s %t && not %update_test_body %t 2>&1 | FileCheck %s | ||
|
||
# CHECK: 'gen' does not exist | ||
|
||
.ifdef GEN | ||
#--- a.txt | ||
.endif |
11 changes: 11 additions & 0 deletions
11
llvm/test/tools/UpdateTestChecks/update_test_body/gen-fail.test
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,11 @@ | ||
# RUN: cp %s %t && not %update_test_body %t 2>&1 | FileCheck %s | ||
|
||
# CHECK: log | ||
# CHECK-NEXT: 'gen' failed | ||
|
||
.ifdef GEN | ||
#--- gen | ||
echo log >&2 | ||
false # gen fails due to sh -e | ||
true | ||
.endif |
8 changes: 8 additions & 0 deletions
8
llvm/test/tools/UpdateTestChecks/update_test_body/gen-unterminated.test
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,8 @@ | ||
# RUN: cp %s %t && not %update_test_body %t 2>&1 | FileCheck %s | ||
|
||
# CHECK: 'gen' should be followed by another part (---) or .endif | ||
|
||
#--- a.txt | ||
.long 0 | ||
#--- gen | ||
cat a.txt |
4 changes: 4 additions & 0 deletions
4
llvm/test/tools/UpdateTestChecks/update_test_body/lit.local.cfg
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,4 @@ | ||
import platform | ||
|
||
if platform.system() == "Windows": | ||
config.unsupported = True |
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
14 changes: 7 additions & 7 deletions
14
llvm/test/tools/llvm-dwarfdump/X86/prettyprint_type_units_split_v5.s
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,110 @@ | ||
#!/usr/bin/env python3 | ||
"""Generate test body using split-file and a custom script. | ||
The script will prepare extra files with `split-file`, invoke `gen`, and then | ||
rewrite the part after `gen` with its stdout. | ||
https://llvm.org/docs/TestingGuide.html#elaborated-tests | ||
Example: | ||
PATH=/path/to/clang_build/bin:$PATH llvm/utils/update_test_body.py path/to/test.s | ||
""" | ||
import argparse | ||
import contextlib | ||
import os | ||
import re | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
|
||
@contextlib.contextmanager | ||
def cd(directory): | ||
cwd = os.getcwd() | ||
os.chdir(directory) | ||
try: | ||
yield | ||
finally: | ||
os.chdir(cwd) | ||
|
||
|
||
def process(args, path): | ||
prolog = [] | ||
seen_gen = False | ||
with open(path) as f: | ||
for line in f.readlines(): | ||
line = line.rstrip() | ||
prolog.append(line) | ||
if (seen_gen and re.match(r"(.|//)---", line)) or line.startswith(".endif"): | ||
break | ||
if re.match(r"(.|//)--- gen", line): | ||
seen_gen = True | ||
else: | ||
print( | ||
"'gen' should be followed by another part (---) or .endif", | ||
file=sys.stderr, | ||
) | ||
return 1 | ||
|
||
if not seen_gen: | ||
print("'gen' does not exist", file=sys.stderr) | ||
return 1 | ||
with tempfile.TemporaryDirectory(prefix="update_test_body_") as dir: | ||
try: | ||
# If the last line starts with ".endif", remove it. | ||
sub = subprocess.run( | ||
["split-file", "-", dir], | ||
input="\n".join( | ||
prolog[:-1] if prolog[-1].startswith(".endif") else prolog | ||
).encode(), | ||
capture_output=True, | ||
check=True, | ||
) | ||
except subprocess.CalledProcessError as ex: | ||
sys.stderr.write(ex.stderr.decode()) | ||
return 1 | ||
with cd(dir): | ||
if args.shell: | ||
print(f"invoke shell in the temporary directory '{dir}'") | ||
subprocess.run([os.environ.get("SHELL", "sh")]) | ||
return 0 | ||
|
||
sub = subprocess.run( | ||
["sh", "-eu", "gen"], | ||
capture_output=True, | ||
# Don't encode the directory information to the Clang output. | ||
# Remove unneeded details (.ident) as well. | ||
env=dict( | ||
os.environ, | ||
CCC_OVERRIDE_OPTIONS="#^-fno-ident", | ||
PWD="/proc/self/cwd", | ||
), | ||
) | ||
sys.stderr.write(sub.stderr.decode()) | ||
if sub.returncode != 0: | ||
print("'gen' failed", file=sys.stderr) | ||
return sub.returncode | ||
if not sub.stdout: | ||
print("stdout is empty; forgot -o - ?", file=sys.stderr) | ||
return 1 | ||
content = sub.stdout.decode() | ||
|
||
with open(path, "w") as f: | ||
# Print lines up to '.endif'. | ||
print("\n".join(prolog), file=f) | ||
# Then print the stdout of 'gen'. | ||
f.write(content) | ||
|
||
|
||
parser = argparse.ArgumentParser( | ||
description="Generate test body using split-file and a custom script" | ||
) | ||
parser.add_argument("files", nargs="+") | ||
parser.add_argument( | ||
"--shell", action="store_true", help="invoke shell instead of 'gen'" | ||
) | ||
args = parser.parse_args() | ||
for path in args.files: | ||
retcode = process(args, path) | ||
if retcode != 0: | ||
sys.exit(retcode) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a weird mixture of
print(..., file=sys.stderr)
andsys.stderr.write
in this file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sys.stderr.write
does not append a newline whileprint
does by default (unlessend=''
)... I feel that sys.stderr.write is quite common as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must admit that I rarely, if ever use
print
forstderr
writing, but I don't know what the norm is in the LLVM scripts.