Skip to content

Commit 6eade7b

Browse files
committed
support PGO on custom project
1 parent 74864fa commit 6eade7b

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

src/ci/stage-build.py

+70-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import urllib.request
1919
from io import StringIO
2020
from pathlib import Path
21+
from enum import Enum, unique
2122
from typing import Callable, ContextManager, Dict, Iterable, Iterator, List, Optional, \
2223
Tuple, Union
2324

@@ -48,8 +49,17 @@
4849

4950
LLVM_BOLT_CRATES = LLVM_PGO_CRATES
5051

52+
@unique
53+
class Type(Enum):
54+
RUST_CI = 1
55+
CUSTOM_BUILD = 2
5156

5257
class Pipeline:
58+
def __init__(self):
59+
self.type = Type.RUST_CI
60+
if "CUSTOM_BUILD_DIR" in os.environ and "CUSTOM_BUILD_CMD" in os.environ:
61+
self.type = Type.CUSTOM_BUILD
62+
5363
# Paths
5464
def checkout_path(self) -> Path:
5565
"""
@@ -81,6 +91,15 @@ def cargo_stage_0(self) -> Path:
8191
def rustc_stage_2(self) -> Path:
8292
return self.build_artifacts() / "stage2" / "bin" / "rustc"
8393

94+
def custom_build(self) -> Tuple[Path, List[str]]:
95+
try:
96+
build_path = os.environ["CUSTOM_BUILD_DIR"],
97+
build_cmd = os.environ["CUSTOM_BUILD_CMD"].split(" ")
98+
self.type = Type.CUSTOM_BUILD
99+
return (build_path, build_cmd)
100+
except:
101+
raise Exception(f"CUSTOM_BULID_DIR and CUSTOM_BUILD_CMD not setted for custom build")
102+
84103
def opt_artifacts(self) -> Path:
85104
raise NotImplementedError
86105

@@ -451,6 +470,20 @@ def cmd(
451470
)
452471
return subprocess.run(args, env=environment, check=True)
453472

473+
def run_custom_build(
474+
pipeline: Pipeline,
475+
env: Optional[Dict[str, str]] = None
476+
):
477+
env = env if env is not None else {}
478+
(build_path, build_cmd) = pipeline.custom_build()
479+
with change_cwd(build_path):
480+
cmd(build_cmd, env=dict(
481+
CARGO=str(pipeline.cargo_stage_2()),
482+
RUST_LOG="collector=debug",
483+
RUSTC=str(pipeline.rustc_stage_2()),
484+
RUSTC_BOOTSTRAP="1",
485+
**env
486+
))
454487

455488
def run_compiler_benchmarks(
456489
pipeline: Pipeline,
@@ -582,12 +615,18 @@ def create_pipeline() -> Pipeline:
582615

583616
def gather_llvm_profiles(pipeline: Pipeline):
584617
LOGGER.info("Running benchmarks with PGO instrumented LLVM")
585-
run_compiler_benchmarks(
586-
pipeline,
587-
profiles=["Debug", "Opt"],
588-
scenarios=["Full"],
589-
crates=LLVM_PGO_CRATES
590-
)
618+
619+
if pipeline.type is Type.CUSTOM_BUILD:
620+
run_custom_build(
621+
pipeline,
622+
)
623+
else:
624+
run_compiler_benchmarks(
625+
pipeline,
626+
profiles=["Debug", "Opt"],
627+
scenarios=["Full"],
628+
crates=LLVM_PGO_CRATES
629+
)
591630

592631
profile_path = pipeline.llvm_profile_merged_file()
593632
LOGGER.info(f"Merging LLVM PGO profiles to {profile_path}")
@@ -614,15 +653,20 @@ def gather_rustc_profiles(pipeline: Pipeline):
614653

615654
# Here we're profiling the `rustc` frontend, so we also include `Check`.
616655
# The benchmark set includes various stress tests that put the frontend under pressure.
617-
run_compiler_benchmarks(
618-
pipeline,
619-
profiles=["Check", "Debug", "Opt"],
620-
scenarios=["All"],
621-
crates=RUSTC_PGO_CRATES,
622-
env=dict(
623-
LLVM_PROFILE_FILE=str(pipeline.rustc_profile_template_path())
656+
if pipeline.type is Type.CUSTOM_BUILD:
657+
run_custom_build(
658+
pipeline,
659+
)
660+
else:
661+
run_compiler_benchmarks(
662+
pipeline,
663+
profiles=["Check", "Debug", "Opt"],
664+
scenarios=["All"],
665+
crates=RUSTC_PGO_CRATES,
666+
env=dict(
667+
LLVM_PROFILE_FILE=str(pipeline.rustc_profile_template_path())
668+
)
624669
)
625-
)
626670

627671
profile_path = pipeline.rustc_profile_merged_file()
628672
LOGGER.info(f"Merging Rustc PGO profiles to {profile_path}")
@@ -646,12 +690,18 @@ def gather_rustc_profiles(pipeline: Pipeline):
646690

647691
def gather_llvm_bolt_profiles(pipeline: Pipeline):
648692
LOGGER.info("Running benchmarks with BOLT instrumented LLVM")
649-
run_compiler_benchmarks(
650-
pipeline,
651-
profiles=["Check", "Debug", "Opt"],
652-
scenarios=["Full"],
653-
crates=LLVM_BOLT_CRATES
654-
)
693+
694+
if pipeline.type is Type.CUSTOM_BUILD:
695+
run_custom_build(
696+
pipeline,
697+
)
698+
else:
699+
run_compiler_benchmarks(
700+
pipeline,
701+
profiles=["Check", "Debug", "Opt"],
702+
scenarios=["Full"],
703+
crates=LLVM_BOLT_CRATES
704+
)
655705

656706
merged_profile_path = pipeline.llvm_bolt_profile_merged_file()
657707
profile_files_path = Path("/tmp/prof.fdata")

0 commit comments

Comments
 (0)