Skip to content

Commit ba6f5e3

Browse files
committed
Auto merge of rust-lang#110605 - csmoe:open-cgo, r=Kobzol
support PGO on custom project make PGO easier for custom toolchain distribution. r? `@Kobzol`
2 parents 9052ca9 + 27beb46 commit ba6f5e3

File tree

1 file changed

+59
-33
lines changed

1 file changed

+59
-33
lines changed

src/ci/stage-build.py

+59-33
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
LLVM_BOLT_CRATES = LLVM_PGO_CRATES
5050

51-
5251
class Pipeline:
5352
# Paths
5453
def checkout_path(self) -> Path:
@@ -451,6 +450,44 @@ def cmd(
451450
)
452451
return subprocess.run(args, env=environment, check=True)
453452

453+
class BenchmarkRunner:
454+
def run_rustc(self, pipeline: Pipeline):
455+
raise NotImplementedError
456+
457+
def run_llvm(self, pipeline: Pipeline):
458+
raise NotImplementedError
459+
460+
def run_bolt(self, pipeline: Pipeline):
461+
raise NotImplementedError
462+
463+
class DefaultBenchmarkRunner(BenchmarkRunner):
464+
def run_rustc(self, pipeline: Pipeline):
465+
# Here we're profiling the `rustc` frontend, so we also include `Check`.
466+
# The benchmark set includes various stress tests that put the frontend under pressure.
467+
run_compiler_benchmarks(
468+
pipeline,
469+
profiles=["Check", "Debug", "Opt"],
470+
scenarios=["All"],
471+
crates=RUSTC_PGO_CRATES,
472+
env=dict(
473+
LLVM_PROFILE_FILE=str(pipeline.rustc_profile_template_path())
474+
)
475+
)
476+
def run_llvm(self, pipeline: Pipeline):
477+
run_compiler_benchmarks(
478+
pipeline,
479+
profiles=["Debug", "Opt"],
480+
scenarios=["Full"],
481+
crates=LLVM_PGO_CRATES
482+
)
483+
484+
def run_bolt(self, pipeline: Pipeline):
485+
run_compiler_benchmarks(
486+
pipeline,
487+
profiles=["Check", "Debug", "Opt"],
488+
scenarios=["Full"],
489+
crates=LLVM_BOLT_CRATES
490+
)
454491

455492
def run_compiler_benchmarks(
456493
pipeline: Pipeline,
@@ -580,14 +617,10 @@ def create_pipeline() -> Pipeline:
580617
raise Exception(f"Optimized build is not supported for platform {sys.platform}")
581618

582619

583-
def gather_llvm_profiles(pipeline: Pipeline):
620+
def gather_llvm_profiles(pipeline: Pipeline, runner: BenchmarkRunner):
584621
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-
)
622+
623+
runner.run_llvm(pipeline)
591624

592625
profile_path = pipeline.llvm_profile_merged_file()
593626
LOGGER.info(f"Merging LLVM PGO profiles to {profile_path}")
@@ -609,20 +642,12 @@ def gather_llvm_profiles(pipeline: Pipeline):
609642
delete_directory(pipeline.llvm_profile_dir_root())
610643

611644

612-
def gather_rustc_profiles(pipeline: Pipeline):
645+
def gather_rustc_profiles(pipeline: Pipeline, runner: BenchmarkRunner):
613646
LOGGER.info("Running benchmarks with PGO instrumented rustc")
614647

615-
# Here we're profiling the `rustc` frontend, so we also include `Check`.
616-
# 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())
624-
)
625-
)
648+
649+
runner.run_rustc(pipeline)
650+
626651

627652
profile_path = pipeline.rustc_profile_merged_file()
628653
LOGGER.info(f"Merging Rustc PGO profiles to {profile_path}")
@@ -644,14 +669,10 @@ def gather_rustc_profiles(pipeline: Pipeline):
644669
delete_directory(pipeline.rustc_profile_dir_root())
645670

646671

647-
def gather_llvm_bolt_profiles(pipeline: Pipeline):
672+
def gather_llvm_bolt_profiles(pipeline: Pipeline, runner: BenchmarkRunner):
648673
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-
)
674+
675+
runner.run_bolt(pipeline)
655676

656677
merged_profile_path = pipeline.llvm_bolt_profile_merged_file()
657678
profile_files_path = Path("/tmp/prof.fdata")
@@ -744,7 +765,7 @@ def record_metrics(pipeline: Pipeline, timer: Timer):
744765
log_metrics(metrics)
745766

746767

747-
def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: List[str]):
768+
def execute_build_pipeline(timer: Timer, pipeline: Pipeline, runner: BenchmarkRunner, final_build_args: List[str]):
748769
# Clear and prepare tmp directory
749770
shutil.rmtree(pipeline.opt_artifacts(), ignore_errors=True)
750771
os.makedirs(pipeline.opt_artifacts(), exist_ok=True)
@@ -762,7 +783,7 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
762783
record_metrics(pipeline, rustc_build)
763784

764785
with stage1.section("Gather profiles"):
765-
gather_llvm_profiles(pipeline)
786+
gather_llvm_profiles(pipeline, runner)
766787
print_free_disk_space(pipeline)
767788

768789
clear_llvm_files(pipeline)
@@ -781,7 +802,7 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
781802
record_metrics(pipeline, rustc_build)
782803

783804
with stage2.section("Gather profiles"):
784-
gather_rustc_profiles(pipeline)
805+
gather_rustc_profiles(pipeline, runner)
785806
print_free_disk_space(pipeline)
786807

787808
clear_llvm_files(pipeline)
@@ -804,7 +825,7 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
804825
record_metrics(pipeline, rustc_build)
805826

806827
with stage3.section("Gather profiles"):
807-
gather_llvm_bolt_profiles(pipeline)
828+
gather_llvm_bolt_profiles(pipeline, runner)
808829

809830
# LLVM is not being cleared here, we want to reuse the previous build
810831
print_free_disk_space(pipeline)
@@ -819,7 +840,7 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
819840
record_metrics(pipeline, stage4)
820841

821842

822-
if __name__ == "__main__":
843+
def run(runner: BenchmarkRunner):
823844
logging.basicConfig(
824845
level=logging.DEBUG,
825846
format="%(name)s %(levelname)-4s: %(message)s",
@@ -832,8 +853,9 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
832853

833854
timer = Timer()
834855
pipeline = create_pipeline()
856+
835857
try:
836-
execute_build_pipeline(timer, pipeline, build_args)
858+
execute_build_pipeline(timer, pipeline, runner, build_args)
837859
except BaseException as e:
838860
LOGGER.error("The multi-stage build has failed")
839861
raise e
@@ -842,3 +864,7 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
842864
print_free_disk_space(pipeline)
843865

844866
print_binary_sizes(pipeline)
867+
868+
if __name__ == "__main__":
869+
runner = DefaultBenchmarkRunner()
870+
run(runner)

0 commit comments

Comments
 (0)