18
18
import urllib .request
19
19
from io import StringIO
20
20
from pathlib import Path
21
+ from enum import Enum , unique
21
22
from typing import Callable , ContextManager , Dict , Iterable , Iterator , List , Optional , \
22
23
Tuple , Union
23
24
48
49
49
50
LLVM_BOLT_CRATES = LLVM_PGO_CRATES
50
51
52
+ @unique
53
+ class Type (Enum ):
54
+ RUST_CI = 1
55
+ CUSTOM_BUILD = 2
51
56
52
57
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
+
53
63
# Paths
54
64
def checkout_path (self ) -> Path :
55
65
"""
@@ -81,6 +91,15 @@ def cargo_stage_0(self) -> Path:
81
91
def rustc_stage_2 (self ) -> Path :
82
92
return self .build_artifacts () / "stage2" / "bin" / "rustc"
83
93
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
+
84
103
def opt_artifacts (self ) -> Path :
85
104
raise NotImplementedError
86
105
@@ -451,6 +470,20 @@ def cmd(
451
470
)
452
471
return subprocess .run (args , env = environment , check = True )
453
472
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
+ ))
454
487
455
488
def run_compiler_benchmarks (
456
489
pipeline : Pipeline ,
@@ -582,12 +615,18 @@ def create_pipeline() -> Pipeline:
582
615
583
616
def gather_llvm_profiles (pipeline : Pipeline ):
584
617
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
+ )
591
630
592
631
profile_path = pipeline .llvm_profile_merged_file ()
593
632
LOGGER .info (f"Merging LLVM PGO profiles to { profile_path } " )
@@ -614,15 +653,20 @@ def gather_rustc_profiles(pipeline: Pipeline):
614
653
615
654
# Here we're profiling the `rustc` frontend, so we also include `Check`.
616
655
# 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
+ )
624
669
)
625
- )
626
670
627
671
profile_path = pipeline .rustc_profile_merged_file ()
628
672
LOGGER .info (f"Merging Rustc PGO profiles to { profile_path } " )
@@ -646,12 +690,18 @@ def gather_rustc_profiles(pipeline: Pipeline):
646
690
647
691
def gather_llvm_bolt_profiles (pipeline : Pipeline ):
648
692
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
+ )
655
705
656
706
merged_profile_path = pipeline .llvm_bolt_profile_merged_file ()
657
707
profile_files_path = Path ("/tmp/prof.fdata" )
0 commit comments