def test_make_benchmark_from_command_line_system_includes(env: LlvmEnv):
    with temporary_working_directory():
        with open("in.c", "w") as f:
            f.write("""
#include <stdio.h>
int main() { return 0; }
""")
        env.make_benchmark_from_command_line("gcc in.c")
def test_make_benchmark_from_command_line_no_system_includes(env: LlvmEnv):
    with temporary_working_directory():
        with open("in.c", "w") as f:
            f.write("""
#include <stdio.h>
int main() { return 0; }
""")
        with pytest.raises(BenchmarkInitError, match="stdio.h"):
            env.make_benchmark_from_command_line("gcc in.c",
                                                 system_includes=False)
示例#3
0
def test_temporary_working_directory_tempdir():
    with temporary_working_directory() as cwdir:
        # Suffix test rather than equality test because on macOS temporary
        # directories can have a /private prefix.
        assert os.getcwd().endswith(str(cwdir))
        assert cwdir.is_dir()
        assert not list(cwdir.iterdir())
        (cwdir / "test").touch()
        assert (cwdir / "test").is_file()

    # Out of scope, the directory is removed.
    assert not cwdir.is_dir()
def test_make_benchmark_from_command_line_only_object_files(env: LlvmEnv):
    with temporary_working_directory():
        with open("a.c", "w") as f:
            f.write("int A() { return 5; }")

        # Compile b.c to object file:
        subprocess.check_call([str(llvm_paths.clang_path()), "a.c", "-c"],
                              timeout=60)
        assert (Path("a.o")).is_file()

        with pytest.raises(
                ValueError,
                match="Input command line has no source file inputs"):
            env.make_benchmark_from_command_line(["gcc", "a.o", "-c"])
def test_make_benchmark_from_command_line_build_cmd(env: LlvmEnv, cmd):
    with temporary_working_directory() as cwd:
        with open("in.c", "w") as f:
            f.write("int main() { return 0; }")

        bm = env.make_benchmark_from_command_line(cmd, system_includes=False)

        assert bm.proto.dynamic_config.build_cmd.argument[:4] == [
            str(llvm_paths.clang_path()),
            "-xir",
            "$IN",
            "-o",
        ]
        assert bm.proto.dynamic_config.build_cmd.argument[-1].endswith(
            f"{cwd}/foo")
示例#6
0
def test_temporary_working_directory():
    with tempfile.TemporaryDirectory() as d:
        path = Path(d)
        with temporary_working_directory(path) as cwdir:
            assert path == cwdir
            # Suffix test rather than equality test because on macOS temporary
            # directories can have a /private prefix.
            assert os.getcwd().endswith(str(path))
            assert cwdir.is_dir()
            assert not list(cwdir.iterdir())
            (cwdir / "test").touch()
            assert (cwdir / "test").is_file()

        # Out of scope, the directory is preserved.
        assert path.is_dir()
def test_make_benchmark_from_command_line(env: LlvmEnv, cmd):
    with temporary_working_directory() as cwd:
        with open("in.c", "w") as f:
            f.write("int main() { return 0; }")

        bm = env.make_benchmark_from_command_line(cmd)
        assert not (cwd / "foo").is_file()

        env.reset(benchmark=bm)
        assert "main()" in env.ir

        assert (cwd / "foo").is_file()

        (cwd / "foo").unlink()
        bm.compile(env)
        assert (cwd / "foo").is_file()
def test_make_benchmark_from_command_line_mixed_source_and_object_files(
        env: LlvmEnv, retcode: int):
    """Test a command line that contains both source files and precompiled
    object files. The object files should be filtered from compilation but
    used for the final link.
    """
    with temporary_working_directory():
        with open("a.c", "w") as f:
            f.write("""
#include "b.h"

int A() {
    return B();
}

int main() {
    return A();
}
""")

        with open("b.c", "w") as f:
            f.write(f"int B() {{ return {retcode}; }}")

        with open("b.h", "w") as f:
            f.write("int B();")

        # Compile b.c to object file:
        subprocess.check_call([str(llvm_paths.clang_path()), "b.c", "-c"],
                              timeout=60)
        assert (Path("b.o")).is_file()

        bm = env.make_benchmark_from_command_line(
            ["gcc", "a.c", "b.o", "-o", "foo"])
        env.reset(benchmark=bm)

        bm.compile(env)
        assert Path("foo").is_file()

        p = subprocess.Popen(["./foo"])
        p.communicate(timeout=60)
        assert p.returncode == retcode
def test_make_benchmark_from_command_line_multiple_input_sources(
        env: LlvmEnv, retcode: int):
    """Test that command lines with multiple source files are linked together."""
    with temporary_working_directory() as cwd:
        with open("a.c", "w") as f:
            f.write("int main() { return B(); }")

        with open("b.c", "w") as f:
            f.write(f"int B() {{ return {retcode}; }}")

        bm = env.make_benchmark_from_command_line(
            ["gcc", "a.c", "b.c", "-o", "foo"])
        assert not (cwd / "foo").is_file()

        env.reset(benchmark=bm)
        assert "main()" in env.ir

        bm.compile(env)
        assert (cwd / "foo").is_file()

        p = subprocess.Popen(["./foo"])
        p.communicate(timeout=60)
        assert p.returncode == retcode
示例#10
0
    def __call__(self, env: CompilerEnv, seed: int = 0xCC) -> CompilerEnvState:
        """Autotune the given environment.

        :param env: The environment to autotune.

        :param seed: The random seed for the autotuner.

        :returns: A CompilerEnvState tuple describing the autotuning result.
        """
        # Run the autotuner in a temporary working directory and capture the
        # stdout/stderr.
        with tempfile.TemporaryDirectory(dir=transient_cache_path("."),
                                         prefix="autotune-") as tmpdir:
            with temporary_working_directory(Path(tmpdir)):
                with capture_output():
                    with Timer() as timer:
                        self.autotune(env, seed=seed, **self.autotune_kwargs)

        return CompilerEnvState(
            benchmark=env.benchmark.uri,
            commandline=env.commandline(),
            walltime=timer.time,
            reward=self.optimization_target.final_reward(env),
        )