Skip to content

[update_cc_test_checks] Use lit's shell to run commands #65333

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Check that shell redirections in the RUN line are handled
// RUN: %clang_cc1 -triple=x86_64-unknown-linux-gnu -emit-llvm < %s 2>/dev/null | FileCheck %s
// RUN: %clang_cc1 -triple=x86_64-unknown-linux-gnu -emit-llvm \
// RUN: -disable-O0-optnone -o - %s 2>&1 | opt -S -passes=mem2reg \
// RUN: | FileCheck %s --check-prefix=MEM2REG

int test(int a, int b) {
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
/// Check that shell redirections in the RUN line are handled
// RUN: %clang_cc1 -triple=x86_64-unknown-linux-gnu -emit-llvm < %s 2>/dev/null | FileCheck %s
// RUN: %clang_cc1 -triple=x86_64-unknown-linux-gnu -emit-llvm \
// RUN: -disable-O0-optnone -o - %s 2>&1 | opt -S -passes=mem2reg \
// RUN: | FileCheck %s --check-prefix=MEM2REG

// CHECK-LABEL: @test(
// CHECK-NEXT: entry:
// CHECK-NEXT: [[A_ADDR:%.*]] = alloca i32, align 4
// CHECK-NEXT: [[B_ADDR:%.*]] = alloca i32, align 4
// CHECK-NEXT: store i32 [[A:%.*]], ptr [[A_ADDR]], align 4
// CHECK-NEXT: store i32 [[B:%.*]], ptr [[B_ADDR]], align 4
// CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A_ADDR]], align 4
// CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[B_ADDR]], align 4
// CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP0]], [[TMP1]]
// CHECK-NEXT: ret i32 [[ADD]]
//
// MEM2REG-LABEL: @test(
// MEM2REG-NEXT: entry:
// MEM2REG-NEXT: [[ADD:%.*]] = add nsw i32 [[A:%.*]], [[B:%.*]]
// MEM2REG-NEXT: ret i32 [[ADD]]
//
int test(int a, int b) {
return a + b;
}
7 changes: 7 additions & 0 deletions clang/test/utils/update_cc_test_checks/shell-redirection.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Check that update_cc_test_checks can parse RUN lines with shell redirection
## This currently fails with errors such as:
## error: error reading '<': No such file or directory
## error: error reading '2>/dev/null': No such file or directory
# RUN: cp -f %S/Inputs/shell-redirection.c %t.c && %update_cc_test_checks %t.c
# RUN: diff -u %t.c %S/Inputs/shell-redirection.c.expected

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;; Check that we can parse RUN lines that have shell redirections
; RUN: llc -mtriple=x86_64 %s -o - 2>/dev/null | FileCheck %s --check-prefix=I32
; RUN: llc < %s -mtriple=x86_64 2>&1 | FileCheck %s --check-prefix=I32
; RUN: sed 's/i32/i64/g' %s | llc -mtriple=x86_64 2>&1 | FileCheck %s --check-prefix=I64

define i32 @add(i32 %X, i32 %Y) {
%Q = add i32 %X, %Y
ret i32 %Q
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
;; Check that we can parse RUN lines that have shell redirections
; RUN: llc -mtriple=x86_64 %s -o - 2>/dev/null | FileCheck %s --check-prefix=I32
; RUN: llc < %s -mtriple=x86_64 2>&1 | FileCheck %s --check-prefix=I32
; RUN: sed 's/i32/i64/g' %s | llc -mtriple=x86_64 2>&1 | FileCheck %s --check-prefix=I64

define i32 @add(i32 %X, i32 %Y) {
; I32-LABEL: add:
; I32: # %bb.0:
; I32-NEXT: # kill: def $esi killed $esi def $rsi
; I32-NEXT: # kill: def $edi killed $edi def $rdi
; I32-NEXT: leal (%rdi,%rsi), %eax
; I32-NEXT: retq
;
; I64-LABEL: add:
; I64: # %bb.0:
; I64-NEXT: leaq (%rdi,%rsi), %rax
; I64-NEXT: retq
%Q = add i32 %X, %Y
ret i32 %Q
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Check that update_llc_test_checks can parse RUN lines with shell redirection
# RUN: cp -f %S/Inputs/shell-redirection.ll %t.ll && %update_llc_test_checks %t.ll
# RUN: diff -u %t.ll %S/Inputs/shell-redirection.ll.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
;; Check that we can parse RUN lines that have shell redirections
; RUN: opt %s -passes=instsimplify -S -o - 2>/dev/null | FileCheck %s --check-prefix=I32
; RUN: opt < %s -passes=instsimplify -S 2>&1 | FileCheck %s --check-prefix=I32
; RUN: sed 's/i32/i64/g' %s | opt -passes=instsimplify -S | FileCheck %s --check-prefix=I64

define i32 @common_sub_operand(i32 %X, i32 %Y) {
%Z = sub i32 %X, %Y
%Q = add i32 %Z, %Y
ret i32 %Q
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
;; Check that we can parse RUN lines that have shell redirections
; RUN: opt %s -passes=instsimplify -S -o - 2>/dev/null | FileCheck %s --check-prefix=I32
; RUN: opt < %s -passes=instsimplify -S 2>&1 | FileCheck %s --check-prefix=I32
; RUN: sed 's/i32/i64/g' %s | opt -passes=instsimplify -S | FileCheck %s --check-prefix=I64

define i32 @common_sub_operand(i32 %X, i32 %Y) {
; I32-LABEL: @common_sub_operand(
; I32-NEXT: ret i32 [[X:%.*]]
;
; I64-LABEL: @common_sub_operand(
; I64-NEXT: ret i64 [[X:%.*]]
;
%Z = sub i32 %X, %Y
%Q = add i32 %Z, %Y
ret i32 %Q
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Check that update_test_checks can parse RUN lines with shell redirection
# RUN: cp -f %S/Inputs/shell-redirection.ll %t.ll && %update_test_checks %t.ll
# RUN: diff -u %t.ll %S/Inputs/shell-redirection.ll.expected
18 changes: 18 additions & 0 deletions llvm/utils/UpdateTestChecks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

from typing import List

sys.path.append(os.path.join(os.path.dirname(__file__), "../lit"))
from lit.TestRunner import ShellEnvironment, executeShCmd, ShellCommandResult
from lit.ShUtil import Pipeline


##### Common utilities for update_*test_checks.py


Expand Down Expand Up @@ -464,6 +469,19 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False):
return stdout.replace("\r\n", "\n")


def execute_pipeline(commands: Pipeline) -> "list[ShellCommandResult]":
shenv = ShellEnvironment(os.getcwd(), os.environ)
results: "list[ShellCommandResult]" = []
exitcode, _ = executeShCmd(commands, shenv, results)
if exitcode != 0:
sys.stderr.write("Failed to run " + str(commands) + "\n")
for result in results:
sys.stderr.write(result.stderr)
sys.stderr.write(result.stdout)
sys.exit(3)
return results


##### LLVM IR parser
RUN_LINE_RE = re.compile(r"^\s*(?://|[;#])\s*RUN:\s*(.*)$")
CHECK_PREFIX_RE = re.compile(r"--?check-prefix(?:es)?[= ](\S+)")
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/lit/lit/ShCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def resolve(self, cwd):


class Pipeline:
def __init__(self, commands, negate=False, pipe_err=False):
def __init__(self, commands: "list[Command]", negate=False, pipe_err=False):
self.commands = commands
self.negate = negate
self.pipe_err = pipe_err
Expand Down
Loading