[llvm] [Lit] Introduce a shared interface for ongoing process invocations and in-proc builtins (PR #195117)
Benjamin Stott via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 30 09:08:32 PDT 2026
https://github.com/BStott6 created https://github.com/llvm/llvm-project/pull/195117
This PR introduces a shared `CommandInvocation` interface designed to mirror the API of `Popen`, and updates the test runner to use this interface for all process invocations. This has two implementations, `ProcessInvocation` and `InprocBuiltinInvocation` for processes and in-process built-ins respectively. This will make the code much cleaner when we finally enable in-process built-ins to run in command pipelines, as the test runner won't have to care whether past invocations in the pipeline are actual process invocations or in-process builtins. `InprocBuiltinInvocation` is not yet used in this PR.
This PR is blocked on https://github.com/llvm/llvm-project/pull/194664; the first two commits in this PR are from that one, so please review only the third commit on this PR. Once that PR is merged, I will rebase this branch to remove the other commits.
This PR is part of a series of patches upgrading Lit's in-process built-ins to be able to run with piped input/output and full redirection support, and to allow custom in-process builtns to be provided via the Lit config. The remaining patches to Lit's test runner can be found [here](https://github.com/BStott6/llvm-project/compare/lit-inproc-builtins). This is part of the [Lit daemonized testing project](https://discourse.llvm.org/t/rfc-reducing-process-creation-overhead-in-llvm-regression-tests/88612/9).
>From 7939b60f23e46a5a6bf1c605361e568b15a78ddd Mon Sep 17 00:00:00 2001
From: BStott <Benjamin.Stott at sony.com>
Date: Tue, 28 Apr 2026 17:05:04 +0100
Subject: [PATCH 1/3] Introduce a new IO interface for in-process builtins
---
llvm/utils/lit/lit/InprocBuiltins.py | 251 +++++++++++++++++++--------
llvm/utils/lit/lit/TestRunner.py | 54 ++++--
llvm/utils/lit/tests/shtest-glob.py | 3 +-
llvm/utils/lit/tests/shtest-shell.py | 37 +---
4 files changed, 219 insertions(+), 126 deletions(-)
diff --git a/llvm/utils/lit/lit/InprocBuiltins.py b/llvm/utils/lit/lit/InprocBuiltins.py
index 34a4d7eae1e88..9bc43f4d30143 100644
--- a/llvm/utils/lit/lit/InprocBuiltins.py
+++ b/llvm/utils/lit/lit/InprocBuiltins.py
@@ -5,77 +5,158 @@
import shutil
import stat
import subprocess
-from io import BytesIO, StringIO
+from dataclasses import dataclass
+from io import BytesIO
+from typing import Any, Callable, Dict, List, Optional
import lit.util
+from lit.ShCommands import Command
from lit.ShellEnvironment import (
InternalShellError,
- ShellCommandResult,
- expand_glob_expressions,
+ ShellEnvironment,
kIsWindows,
- processRedirects,
updateEnv,
)
-def executeBuiltinCd(cmd, shenv):
+class InprocBuiltinIO:
+ """
+ Holds IO streams for an inproc builtin invocation.
+
+ NB: If stderr is redirected to be the same stream as stdout, then
+ `stder == stdout` is True.
+ """
+
+ stdin: Any
+ stdout: Any
+ stderr: Any
+
+ def __init__(self, stdin, stdout, stderr):
+ """
+ Configure the IO streams for an in-process built-in command. This
+ constructor is designed to mirror how IO streams are configured in
+ `subprocess.Popen`.
+
+ Each of the arguments to this constructor may be:
+ - A file object open in binary mode.
+ - `subprocess.PIPE` or `subprocess.STDOUT` sentinels.
+ - None.
+ """
+
+ # If stderr is redirected to stdout, we make sure to use the same
+ # stream for both so that the order of output is preserved.
+ stderr_redirected_to_stdout = (
+ stdout == subprocess.PIPE and stderr == subprocess.STDOUT
+ )
+
+ # Replace sentinel values with in-memory streams.
+ if stdin == subprocess.PIPE or stdin is None:
+ self.stdin = BytesIO()
+ else:
+ self.stdin = stdin
+
+ if stdout == subprocess.PIPE or stdout is None:
+ self.stdout = BytesIO()
+ else:
+ self.stdout = stdout
+
+ if stderr_redirected_to_stdout:
+ # Make sure stderr and stdout are directed to the same stream.
+ self.stderr = self.stdout
+ elif stderr == subprocess.PIPE or stderr is None:
+ self.stderr = BytesIO()
+ else:
+ self.stderr = stderr
+
+
+InprocBuiltinExecuteFn = Callable[
+ [Command, List[str], ShellEnvironment, InprocBuiltinIO],
+ int,
+]
+"""
+Function called by an in-process built-in command.
+Parameters:
+ - `cmd`: The command itself.
+ - `args`: glob-expanded list of arguments (including argv[0] as the program
+ name).
+ - `shenv`: The shell environment.
+ - `io`: Holds the input and output streams for the invocation. These are
+ binary IO objects (files open in binary mode, BytesIO).
+
+The return value is the exit code.
+"""
+
+
+ at dataclass
+class InprocBuiltin:
+ """
+ Represents a command that is run as an in-process built-in.
+ """
+
+ execute: InprocBuiltinExecuteFn
+ """
+ Function called to execute the in-process built-in.
+ """
+
+ fallback: Optional[str] = None
+
+
+def executeBuiltinCd(
+ cmd: Command, args: List[str], shenv: ShellEnvironment, io: InprocBuiltinIO
+) -> int:
"""executeBuiltinCd - Change the current directory."""
- if len(cmd.args) != 2:
+ if len(args) != 2:
raise InternalShellError(cmd, "'cd' supports only one argument")
# Update the cwd in the parent environment.
- shenv.change_dir(cmd.args[1])
+ shenv.change_dir(args[1])
# The cd builtin always succeeds. If the directory does not exist, the
# following Popen calls will fail instead.
- return ShellCommandResult(cmd, "", "", 0, False)
+ return 0
-def executeBuiltinPushd(cmd, shenv):
+def executeBuiltinPushd(
+ cmd: Command, args: List[str], shenv: ShellEnvironment, io: InprocBuiltinIO
+) -> int:
"""executeBuiltinPushd - Change the current dir and save the old."""
- if len(cmd.args) != 2:
+ if len(args) != 2:
raise InternalShellError(cmd, "'pushd' supports only one argument")
shenv.dirStack.append(shenv.cwd)
- shenv.change_dir(cmd.args[1])
- return ShellCommandResult(cmd, "", "", 0, False)
+ shenv.change_dir(args[1])
+ return 0
-def executeBuiltinPopd(cmd, shenv):
+def executeBuiltinPopd(
+ cmd: Command, args: List[str], shenv: ShellEnvironment, io: InprocBuiltinIO
+) -> int:
"""executeBuiltinPopd - Restore a previously saved working directory."""
- if len(cmd.args) != 1:
+ if len(args) != 1:
raise InternalShellError(cmd, "'popd' does not support arguments")
if not shenv.dirStack:
raise InternalShellError(cmd, "popd: directory stack empty")
shenv.cwd = shenv.dirStack.pop()
- return ShellCommandResult(cmd, "", "", 0, False)
+ return 0
-def executeBuiltinExport(cmd, shenv):
+def executeBuiltinExport(
+ cmd: Command, args: List[str], shenv: ShellEnvironment, io: InprocBuiltinIO
+) -> int:
"""executeBuiltinExport - Set an environment variable."""
- if len(cmd.args) != 2:
+ if len(args) != 2:
raise InternalShellError(cmd, "'export' supports only one argument")
- updateEnv(shenv, cmd.args)
- return ShellCommandResult(cmd, "", "", 0, False)
+ updateEnv(shenv, args)
+ return 0
-def executeBuiltinEcho(cmd, shenv):
+def executeBuiltinEcho(
+ cmd: Command, args: List[str], shenv: ShellEnvironment, io: InprocBuiltinIO
+) -> int:
"""Interpret a redirected echo or @echo command"""
opened_files = []
- stdin, stdout, stderr = processRedirects(cmd, subprocess.PIPE, shenv, opened_files)
- if stdin != subprocess.PIPE or stderr != subprocess.PIPE:
- raise InternalShellError(
- cmd, f"stdin and stderr redirects not supported for {cmd.args[0]}"
- )
-
- # Some tests have un-redirected echo commands to help debug test failures.
- # Buffer our output and return it to the caller.
- is_redirected = True
- if stdout == subprocess.PIPE:
- is_redirected = False
- stdout = BytesIO()
# Implement echo flags. We only support -e and -n, and not yet in
# combination. We have to ignore unknown flags, because `echo "-D FOO"`
# prints the dash.
- args = cmd.args[1:]
+ args = args[1:]
interpret_escapes = False
write_newline = True
while len(args) >= 1 and args[0] in ("-e", "-n"):
@@ -94,30 +175,24 @@ def maybeUnescape(arg):
if args:
for arg in args[:-1]:
- stdout.write(maybeUnescape(arg).encode())
- stdout.write(b" ")
- stdout.write(maybeUnescape(args[-1]).encode())
+ io.stdout.write(maybeUnescape(arg).encode())
+ io.stdout.write(" ".encode())
+ io.stdout.write(maybeUnescape(args[-1]).encode())
if write_newline:
- stdout.write("\n".encode())
+ io.stdout.write("\n".encode())
for name, mode, f, path in opened_files:
f.close()
- output = (
- ""
- if is_redirected
- # TODO(BStott) remove decode once new interface for in-process builtin
- # IO is introduced.
- else stdout.getvalue().decode(encoding="utf8", errors="replace")
- )
- return ShellCommandResult(cmd, output, "", 0, False)
+ return 0
-def executeBuiltinMkdir(cmd, cmd_shenv):
+def executeBuiltinMkdir(
+ cmd: Command, args: List[str], cmd_shenv: ShellEnvironment, io: InprocBuiltinIO
+):
"""executeBuiltinMkdir - Create new directories."""
- args = expand_glob_expressions(cmd.args, cmd_shenv.cwd)[1:]
try:
- opts, args = getopt.gnu_getopt(args, "p")
+ opts, args = getopt.gnu_getopt(args[1:], "p")
except getopt.GetoptError as err:
raise InternalShellError(cmd, "Unsupported: 'mkdir': %s" % str(err))
@@ -131,7 +206,6 @@ def executeBuiltinMkdir(cmd, cmd_shenv):
if len(args) == 0:
raise InternalShellError(cmd, "Error: 'mkdir' is missing an operand")
- stderr = StringIO()
exitCode = 0
for dir in args:
dir = pathlib.Path(dir)
@@ -144,16 +218,19 @@ def executeBuiltinMkdir(cmd, cmd_shenv):
try:
dir.mkdir(exist_ok=True)
except OSError as err:
- stderr.write("Error: 'mkdir' command failed, %s\n" % str(err))
+ io.stderr.write(
+ ("Error: 'mkdir' command failed, %s\n" % str(err)).encode()
+ )
exitCode = 1
- return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False)
+ return exitCode
-def executeBuiltinRm(cmd, cmd_shenv):
+def executeBuiltinRm(
+ cmd: Command, args: List[str], cmd_shenv: ShellEnvironment, io: InprocBuiltinIO
+):
"""executeBuiltinRm - Removes (deletes) files or directories."""
- args = expand_glob_expressions(cmd.args, cmd_shenv.cwd)[1:]
try:
- opts, args = getopt.gnu_getopt(args, "frR", ["--recursive"])
+ opts, args = getopt.gnu_getopt(args[1:], "frR", ["--recursive"])
except getopt.GetoptError as err:
raise InternalShellError(cmd, "Unsupported: 'rm': %s" % str(err))
@@ -176,7 +253,6 @@ def on_rm_error(func, path, exc_info):
os.chmod(path, stat.S_IMODE(os.stat(path).st_mode) | stat.S_IWRITE)
os.remove(path)
- stderr = StringIO()
exitCode = 0
for path in args:
cwd = cmd_shenv.cwd
@@ -189,7 +265,7 @@ def on_rm_error(func, path, exc_info):
os.remove(path)
elif os.path.isdir(path):
if not recursive:
- stderr.write("Error: %s is a directory\n" % path)
+ io.stderr.write(("Error: %s is a directory\n" % path).encode())
exitCode = 1
if platform.system() == "Windows":
# NOTE: use ctypes to access `SHFileOperationsW` on Windows to
@@ -255,26 +331,28 @@ class SHFILEOPSTRUCTW(Structure):
os.chmod(path, stat.S_IMODE(os.stat(path).st_mode) | stat.S_IWRITE)
os.remove(path)
except OSError as err:
- stderr.write("Error: 'rm' command failed, %s" % str(err))
+ io.stderr.write(("Error: 'rm' command failed, %s" % str(err)).encode())
exitCode = 1
- return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False)
+ return exitCode
-def executeBuiltinUmask(cmd, shenv):
+def executeBuiltinUmask(
+ cmd: Command, args: List[str], shenv: ShellEnvironment, io: InprocBuiltinIO
+):
"""executeBuiltinUmask - Change the current umask."""
if os.name != "posix":
raise InternalShellError(cmd, "'umask' not supported on this system")
- if len(cmd.args) != 2:
+ if len(args) != 2:
raise InternalShellError(cmd, "'umask' supports only one argument")
try:
# Update the umask in the parent environment.
- shenv.umask = int(cmd.args[1], 8)
+ shenv.umask = int(args[1], 8)
except ValueError as err:
raise InternalShellError(cmd, "Error: 'umask': %s" % str(err))
- return ShellCommandResult(cmd, "", "", 0, False)
+ return 0
-def executeBuiltinUlimit(cmd, shenv):
+def executeBuiltinUlimit(cmd: Command, args: List[str], shenv, io: InprocBuiltinIO):
"""executeBuiltinUlimit - Change the current limits."""
try:
# Try importing the resource module (available on POSIX systems) and
@@ -282,34 +360,55 @@ def executeBuiltinUlimit(cmd, shenv):
import resource
except ImportError:
raise InternalShellError(cmd, "'ulimit' not supported on this system")
- if len(cmd.args) != 3:
+ if len(args) != 3:
raise InternalShellError(cmd, "'ulimit' requires two arguments")
try:
- if cmd.args[2] == "unlimited":
+ if args[2] == "unlimited":
new_limit = resource.RLIM_INFINITY
else:
- new_limit = int(cmd.args[2])
+ new_limit = int(args[2])
except ValueError as err:
raise InternalShellError(cmd, "Error: 'ulimit': %s" % str(err))
- if cmd.args[1] == "-v":
+ if args[1] == "-v":
if new_limit != resource.RLIM_INFINITY:
new_limit = new_limit * 1024
shenv.ulimit["RLIMIT_AS"] = new_limit
- elif cmd.args[1] == "-n":
+ elif args[1] == "-n":
shenv.ulimit["RLIMIT_NOFILE"] = new_limit
- elif cmd.args[1] == "-s":
+ elif args[1] == "-s":
if new_limit != resource.RLIM_INFINITY:
new_limit = new_limit * 1024
shenv.ulimit["RLIMIT_STACK"] = new_limit
- elif cmd.args[1] == "-f":
+ elif args[1] == "-f":
shenv.ulimit["RLIMIT_FSIZE"] = new_limit
else:
- raise InternalShellError(
- cmd, "'ulimit' does not support option: %s" % cmd.args[1]
- )
- return ShellCommandResult(cmd, "", "", 0, False)
+ raise InternalShellError(cmd, "'ulimit' does not support option: %s" % args[1])
+ return 0
-def executeBuiltinColon(cmd, cmd_shenv):
+def executeBuiltinColon(
+ cmd: Command, args: List[str], cmd_shenv: ShellEnvironment, io: InprocBuiltinIO
+):
"""executeBuiltinColon - Discard arguments and exit with status 0."""
- return ShellCommandResult(cmd, "", "", 0, False)
+ return 0
+
+
+def get_default_inproc_builtins() -> Dict[str, InprocBuiltin]:
+ """
+ get_default_inproc_builtins - Returns the map of command names to Lit's
+ in-process built-in implementations.
+ """
+
+ return {
+ "@echo": InprocBuiltin(executeBuiltinEcho, None),
+ "cd": InprocBuiltin(executeBuiltinCd, None),
+ "export": InprocBuiltin(executeBuiltinExport, None),
+ "echo": InprocBuiltin(executeBuiltinEcho, None),
+ "mkdir": InprocBuiltin(executeBuiltinMkdir, None),
+ "popd": InprocBuiltin(executeBuiltinPopd, None),
+ "pushd": InprocBuiltin(executeBuiltinPushd, None),
+ "rm": InprocBuiltin(executeBuiltinRm, None),
+ "ulimit": InprocBuiltin(executeBuiltinUlimit, None),
+ "umask": InprocBuiltin(executeBuiltinUmask, None),
+ ":": InprocBuiltin(executeBuiltinColon, None),
+ }
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index dbaace6d6c60c..da8f8001196bf 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -11,11 +11,11 @@
import threading
import traceback
-import lit.InprocBuiltins as InprocBuiltins
import lit.ShUtil as ShUtil
import lit.Test as Test
import lit.util
from lit.BooleanExpression import BooleanExpression
+from lit.InprocBuiltins import InprocBuiltinIO, get_default_inproc_builtins
from lit.ShCommands import Command
from lit.ShellEnvironment import (
InternalShellError,
@@ -254,19 +254,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
builtin_commands_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "builtin_commands"
)
- inproc_builtins = {
- "cd": InprocBuiltins.executeBuiltinCd,
- "export": InprocBuiltins.executeBuiltinExport,
- "echo": InprocBuiltins.executeBuiltinEcho,
- "@echo": InprocBuiltins.executeBuiltinEcho,
- "mkdir": InprocBuiltins.executeBuiltinMkdir,
- "popd": InprocBuiltins.executeBuiltinPopd,
- "pushd": InprocBuiltins.executeBuiltinPushd,
- "rm": InprocBuiltins.executeBuiltinRm,
- "ulimit": InprocBuiltins.executeBuiltinUlimit,
- "umask": InprocBuiltins.executeBuiltinUmask,
- ":": InprocBuiltins.executeBuiltinColon,
- }
+ inproc_builtins = get_default_inproc_builtins()
# To avoid deadlock, we use a single stderr stream for piped
# output. This is null until we have seen some output using
# stderr.
@@ -354,9 +342,43 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
j,
"Unsupported: '{}' cannot be part" " of a pipeline".format(args[0]),
)
- result = inproc_builtin(Command(args, j.redirects), cmd_shenv)
+
+ stdin, stdout, stderr = processRedirects(
+ j, subprocess.PIPE, shenv, opened_files
+ )
+
+ builtin_io = InprocBuiltinIO(stdin, stdout, stderr)
+
+ args = expand_glob_expressions(args, cmd_shenv.cwd)
+
+ exit_code = inproc_builtin.execute(
+ Command(args, j.redirects), args, cmd_shenv, builtin_io
+ )
+
+ builtin_io.stdout.flush()
+ builtin_io.stderr.flush()
+
if not_count % 2:
- result.exitCode = int(not result.exitCode)
+ exit_code = int(not exit_code)
+
+ # Gather output from the streams.
+ out = ""
+ if stdout == subprocess.PIPE:
+ builtin_io.stdout.seek(0)
+ out = builtin_io.stdout.read().decode("utf8", "replace")
+
+ err = ""
+ if stderr == subprocess.PIPE:
+ builtin_io.stderr.seek(0)
+ err = builtin_io.stderr.read().decode("utf8", "replace")
+
+ result = ShellCommandResult(
+ j,
+ out,
+ err,
+ exit_code,
+ False,
+ )
result.command.args = j.args
results.append(result)
return result.exitCode
diff --git a/llvm/utils/lit/tests/shtest-glob.py b/llvm/utils/lit/tests/shtest-glob.py
index 95a53e4dd2223..190745f8133d4 100644
--- a/llvm/utils/lit/tests/shtest-glob.py
+++ b/llvm/utils/lit/tests/shtest-glob.py
@@ -4,8 +4,7 @@
# RUN: | FileCheck -dump-input=fail -match-full-lines --implicit-check-not=Error: %s
# END.
-# CHECK: UNRESOLVED: shtest-glob :: glob-echo.txt ({{[^)]*}})
-# CHECK: AttributeError: 'GlobItem' object has no attribute 'encode'
+# CHECK: PASS: shtest-glob :: glob-echo.txt ({{[^)]*}})
# CHECK: FAIL: shtest-glob :: glob-mkdir.txt ({{[^)]*}})
# CHECK: # | Error: 'mkdir' command failed, {{.*}}example_file1.input'
diff --git a/llvm/utils/lit/tests/shtest-shell.py b/llvm/utils/lit/tests/shtest-shell.py
index 498f6bb0adc11..fa5d63b5e9e29 100644
--- a/llvm/utils/lit/tests/shtest-shell.py
+++ b/llvm/utils/lit/tests/shtest-shell.py
@@ -497,37 +497,10 @@
# CHECK-NEXT: # error: command failed with exit status: 1
# CHECK: ***
-# CHECK: FAIL: shtest-shell :: echo-at-redirect-stderr.txt
-# CHECK: *** TEST 'shtest-shell :: echo-at-redirect-stderr.txt' FAILED ***
-# CHECK: @echo 2> {{.*}}
-# CHECK: # executed command: @echo
-# CHECK: # .---command stderr{{-*}}
-# CHECK: # | stdin and stderr redirects not supported for @echo
-# CHECK: error: command failed with exit status:
-
-# CHECK: FAIL: shtest-shell :: echo-at-redirect-stdin.txt
-# CHECK: *** TEST 'shtest-shell :: echo-at-redirect-stdin.txt' FAILED ***
-# CHECK: @echo < {{.*}}
-# CHECK: # executed command: @echo
-# CHECK: # .---command stderr{{-*}}
-# CHECK: # | stdin and stderr redirects not supported for @echo
-# CHECK: error: command failed with exit status:
-
-# CHECK: FAIL: shtest-shell :: echo-redirect-stderr.txt
-# CHECK: *** TEST 'shtest-shell :: echo-redirect-stderr.txt' FAILED ***
-# CHECK: echo 2> {{.*}}
-# CHECK: # executed command: echo
-# CHECK: # .---command stderr{{-*}}
-# CHECK: # | stdin and stderr redirects not supported for echo
-# CHECK: error: command failed with exit status:
-
-# CHECK: FAIL: shtest-shell :: echo-redirect-stdin.txt
-# CHECK: *** TEST 'shtest-shell :: echo-redirect-stdin.txt' FAILED ***
-# CHECK: echo < {{.*}}
-# CHECK: # executed command: echo
-# CHECK: # .---command stderr{{-*}}
-# CHECK: # | stdin and stderr redirects not supported for echo
-# CHECK: error: command failed with exit status:
+# CHECK: PASS: shtest-shell :: echo-at-redirect-stderr.txt
+# CHECK: PASS: shtest-shell :: echo-at-redirect-stdin.txt
+# CHECK: PASS: shtest-shell :: echo-redirect-stderr.txt
+# CHECK: PASS: shtest-shell :: echo-redirect-stdin.txt
# CHECK: FAIL: shtest-shell :: error-0.txt
# CHECK: *** TEST 'shtest-shell :: error-0.txt' FAILED ***
@@ -634,4 +607,4 @@
# CHECK: PASS: shtest-shell :: valid-shell.txt
# CHECK: Unresolved Tests (1)
-# CHECK: Failed Tests (37)
+# CHECK: Failed Tests (33)
>From ebeed6019c9a872b4c5f53e27c6fd0e8a49c7b42 Mon Sep 17 00:00:00 2001
From: BStott <Benjamin.Stott at sony.com>
Date: Wed, 29 Apr 2026 11:16:39 +0100
Subject: [PATCH 2/3] Add typing.BinaryIO type hints for IO objects
---
llvm/utils/lit/lit/InprocBuiltins.py | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/llvm/utils/lit/lit/InprocBuiltins.py b/llvm/utils/lit/lit/InprocBuiltins.py
index 9bc43f4d30143..855694d5c4264 100644
--- a/llvm/utils/lit/lit/InprocBuiltins.py
+++ b/llvm/utils/lit/lit/InprocBuiltins.py
@@ -5,9 +5,10 @@
import shutil
import stat
import subprocess
+import typing
from dataclasses import dataclass
from io import BytesIO
-from typing import Any, Callable, Dict, List, Optional
+from typing import Callable, Dict, List, Optional
import lit.util
from lit.ShCommands import Command
@@ -27,9 +28,9 @@ class InprocBuiltinIO:
`stder == stdout` is True.
"""
- stdin: Any
- stdout: Any
- stderr: Any
+ stdin: typing.BinaryIO
+ stdout: typing.BinaryIO
+ stderr: typing.BinaryIO
def __init__(self, stdin, stdout, stderr):
"""
>From 491a3b1c47141443cc430ab726b67a101c283f01 Mon Sep 17 00:00:00 2001
From: BStott <Benjamin.Stott at sony.com>
Date: Thu, 15 Jan 2026 12:26:35 +0000
Subject: [PATCH 3/3] Introduce a shared interface for ongoing process
invocations and in-process builtin invocations
---
llvm/utils/lit/lit/TestRunner.py | 156 +++++++++++++++++++++++++------
1 file changed, 130 insertions(+), 26 deletions(-)
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index da8f8001196bf..5b1d4e0311e22 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -1,5 +1,6 @@
from __future__ import absolute_import, annotations
+import abc
import os
import pathlib
import re
@@ -10,6 +11,8 @@
import tempfile
import threading
import traceback
+import typing
+from dataclasses import dataclass
import lit.ShUtil as ShUtil
import lit.Test as Test
@@ -185,6 +188,108 @@ def executeShCmd(cmd, shenv, results, timeout=0):
return (finalExitCode, timeoutInfo)
+ at dataclass
+class InprocBuiltinResult:
+ """
+ Result of invoking an in-process builtin command. This stores its exit code
+ and stdout/stderr streams.
+ """
+
+ exit_code: int
+ stdout: typing.BinaryIO
+ stderr: typing.BinaryIO
+
+
+class CommandInvocation(abc.ABC):
+ """
+ Result of invoking a command: implementations hold either a Popen for an
+ out-of-proc command or an InprocCommandResult for an in-process command.
+ This is designed to mirror the functionality of Popen for in-process
+ commands too.
+ """
+
+ @abc.abstractmethod
+ def wait(self) -> int:
+ """
+ Wraps `Popen.wait`. For in-process builtin commands, there is nothing
+ to wait for, so just returns the exit code.
+ """
+
+ raise NotImplemented
+
+ @abc.abstractmethod
+ def communicate(self) -> tuple[bytes, bytes]:
+ """
+ Wraps `Popen.communicate`. For in-process builtin commands, this is
+ the same as `read_output`.
+ """
+
+ raise NotImplemented
+
+ @abc.abstractmethod
+ def stdout(self) -> typing.BinaryIO | None:
+ raise NotImplemented
+
+ @abc.abstractmethod
+ def stderr(self) -> typing.BinaryIO | None:
+ raise NotImplemented
+
+
+ at dataclass
+class ProcessInvocation(CommandInvocation):
+ """
+ CommandInvocation wrapping a `subprocess.Popen`; the result of invoking an
+ out-of-process command.
+ """
+
+ popen: subprocess.Popen
+
+ def wait(self) -> int:
+ return self.popen.wait()
+
+ def communicate(self) -> tuple[bytes, bytes]:
+ return self.popen.communicate()
+
+ def stdout(self) -> typing.BinaryIO | None:
+ return self.popen.stdout
+
+ def stderr(self) -> typing.BinaryIO | None:
+ return self.popen.stderr
+
+
+ at dataclass
+class InprocBuiltinInvocation(CommandInvocation):
+ """
+ CommandInvocation wrapping an `InprocBuiltinResult`; the result of invoking an
+ in-process builtin command.
+ """
+
+ result: InprocBuiltinResult
+
+ def wait(self) -> int:
+ # In-process builtins are not run asynchronously.
+ return self.result.exit_code
+
+ def communicate(self) -> tuple[bytes, bytes]:
+ if self.stdout():
+ stdout = self.stdout().read()
+ else:
+ stdout = b""
+
+ if self.stderr():
+ stderr = self.stderr().read()
+ else:
+ stderr = b""
+
+ return stdout, stderr
+
+ def stdout(self) -> typing.BinaryIO | None:
+ return self.result.stdout
+
+ def stderr(self) -> typing.BinaryIO | None:
+ return self.result.stderr
+
+
def _expandLateSubstitutions(cmd, arguments, cwd, normalize_slashes=False):
for i, arg in enumerate(arguments):
if not isinstance(arg, str):
@@ -243,7 +348,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
raise ValueError("Unknown shell command: %r" % cmd.op)
assert isinstance(cmd, ShUtil.Pipeline)
- procs = []
+ invocations = []
proc_not_counts = []
proc_not_fail_if_crash = []
default_stdin = subprocess.PIPE
@@ -487,19 +592,18 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
old_umask = -1
if cmd_shenv.umask != -1:
old_umask = os.umask(cmd_shenv.umask)
- procs.append(
- subprocess.Popen(
- args,
- cwd=cmd_shenv.cwd,
- executable=executable,
- stdin=stdin,
- stdout=stdout,
- stderr=stderr,
- env=cmd_shenv.env,
- close_fds=kUseCloseFDs,
- text=False,
- )
+ proc = subprocess.Popen(
+ args,
+ cwd=cmd_shenv.cwd,
+ executable=executable,
+ stdin=stdin,
+ stdout=stdout,
+ stderr=stderr,
+ env=cmd_shenv.env,
+ close_fds=kUseCloseFDs,
+ text=False,
)
+ invocations.append(ProcessInvocation(proc))
if old_umask != -1:
os.umask(old_umask)
proc_not_counts.append(not_count)
@@ -508,7 +612,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
else:
proc_not_fail_if_crash.append(False)
# Let the helper know about this process
- timeoutHelper.addProcess(procs[-1])
+ timeoutHelper.addProcess(proc)
except OSError as e:
raise InternalShellError(
j, "Could not create process ({}) due to {}".format(executable, e)
@@ -516,14 +620,14 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
# Immediately close stdin for any process taking stdin from us.
if stdin == subprocess.PIPE:
- procs[-1].stdin.close()
- procs[-1].stdin = None
+ proc.stdin.close()
+ proc.stdin = None
# Update the current stdin source.
if stdout == subprocess.PIPE:
- default_stdin = procs[-1].stdout
+ default_stdin = invocations[-1].stdout()
elif stderrIsStdout:
- default_stdin = procs[-1].stderr
+ default_stdin = invocations[-1].stderr()
else:
default_stdin = subprocess.PIPE
@@ -535,16 +639,16 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
f.close()
# FIXME: There is probably still deadlock potential here. Yawn.
- procData = [None] * len(procs)
- procData[-1] = procs[-1].communicate()
+ procData = [None] * len(invocations)
+ procData[-1] = invocations[-1].communicate()
- for i in range(len(procs) - 1):
- if procs[i].stdout is not None:
- out = procs[i].stdout.read()
+ for i in range(len(invocations) - 1):
+ if invocations[i].stdout() is not None:
+ out = invocations[i].stdout().read()
else:
out = b""
- if procs[i].stderr is not None:
- err = procs[i].stderr.read()
+ if invocations[i].stderr() is not None:
+ err = invocations[i].stderr().read()
else:
err = b""
procData[i] = (out, err)
@@ -557,7 +661,7 @@ def _executeShCmd(cmd, shenv, results, timeoutHelper):
exitCode = None
for i, (out, err) in enumerate(procData):
- res = procs[i].wait()
+ res = invocations[i].wait()
# Detect Ctrl-C in subprocess.
if res == -signal.SIGINT:
raise KeyboardInterrupt
More information about the llvm-commits
mailing list