1414"""FDO context describes how C++ FDO compilation should be done."""
1515
1616load ("@bazel_skylib//lib:paths.bzl" , "paths" )
17+ load ("//cc:action_names.bzl" , "ACTION_NAMES" )
1718load ("//cc/common:cc_common.bzl" , "cc_common" )
1819load ("//cc/private/rules_impl/fdo:fdo_prefetch_hints.bzl" , "FdoPrefetchHintsInfo" )
1920load ("//cc/private/rules_impl/fdo:fdo_profile.bzl" , "FdoProfileInfo" )
@@ -28,6 +29,7 @@ def _create_fdo_context(
2829 zipper ,
2930 cc_toolchain_config_info ,
3031 coverage_enabled ,
32+ feature_configuration ,
3133 _fdo_prefetch_hints ,
3234 _propeller_optimize ,
3335 _memprof_profile ,
@@ -48,6 +50,7 @@ def _create_fdo_context(
4850 zipper: (File) zip tool, used to unpact the profiles
4951 cc_toolchain_config_info: (CcToolchainConfigInfo) Used to check CPU value, should be removed
5052 coverage_enabled: (bool) Is code coverage enabled
53+ feature_configuration: (FeatureConfiguration) Used for llvm-profdata action
5154 _fdo_prefetch_hints: (Target) Pointed to by --fdo_prefetch_hints
5255 _propeller_optimize: (Target) Pointed to by --propeller_optimize
5356 _memprof_profile: (Target) Pointed to by --memprof_profile
@@ -70,6 +73,21 @@ def _create_fdo_context(
7073 if cpp_config .compilation_mode () != "opt" :
7174 return struct ()
7275
76+ llvm_profdata_env = {}
77+ if cc_common .action_is_enabled (
78+ feature_configuration = feature_configuration ,
79+ action_name = ACTION_NAMES .llvm_profdata ,
80+ ):
81+ llvm_profdata = cc_common .get_tool_for_action (
82+ feature_configuration = feature_configuration ,
83+ action_name = ACTION_NAMES .llvm_profdata ,
84+ )
85+ llvm_profdata_env = cc_common .get_environment_variables (
86+ feature_configuration = feature_configuration ,
87+ action_name = ACTION_NAMES .llvm_profdata ,
88+ variables = cc_common .empty_variables (),
89+ )
90+
7391 # Propeller optimize cc and ld profiles
7492 cc_profile = _symlink_to (
7593 ctx ,
@@ -186,6 +204,7 @@ def _create_fdo_context(
186204 all_files ,
187205 zipper ,
188206 cc_toolchain_config_info ,
207+ llvm_profdata_env ,
189208 )
190209 elif branch_fdo_mode in ["auto_fdo" , "xbinary_fdo" ]:
191210 profile_artifact = _symlink_input (
@@ -203,6 +222,7 @@ def _create_fdo_context(
203222 all_files ,
204223 zipper ,
205224 cc_toolchain_config_info ,
225+ llvm_profdata_env ,
206226 )
207227 cs_profile_artifact = _convert_llvm_raw_profile_to_indexed (
208228 ctx ,
@@ -212,6 +232,7 @@ def _create_fdo_context(
212232 all_files ,
213233 zipper ,
214234 cc_toolchain_config_info ,
235+ llvm_profdata_env ,
215236 )
216237 profile_artifact = _merge_llvm_profiles (
217238 ctx ,
@@ -221,6 +242,7 @@ def _create_fdo_context(
221242 non_cs_profile_artifact ,
222243 cs_profile_artifact ,
223244 "MergedCS.profdata" ,
245+ llvm_profdata_env ,
224246 )
225247
226248 branch_fdo_profile = struct (
@@ -254,7 +276,8 @@ def _convert_llvm_raw_profile_to_indexed(
254276 llvm_profdata ,
255277 all_files ,
256278 zipper ,
257- cc_toolchain_config_info ):
279+ cc_toolchain_config_info ,
280+ env ):
258281 """This function checks the input profile format and converts it to the indexed format (.profdata) if necessary."""
259282 basename = _basename (fdo_inputs )
260283 if basename .endswith (".profdata" ):
@@ -310,6 +333,7 @@ def _convert_llvm_raw_profile_to_indexed(
310333 arguments = [ctx .actions .args ().add ("merge" ).add ("-o" ).add (profile_artifact ).add (raw_profile_artifact )],
311334 inputs = [raw_profile_artifact ],
312335 outputs = [profile_artifact ],
336+ env = env ,
313337 use_default_shell_env = True ,
314338 progress_message = "LLVMProfDataAction: Generating %{output}" ,
315339 )
@@ -323,7 +347,8 @@ def _merge_llvm_profiles(
323347 all_files ,
324348 profile1 ,
325349 profile2 ,
326- merged_output_name ):
350+ merged_output_name ,
351+ env ):
327352 """This function merges profile1 and profile2 and generates merged_output."""
328353 profile_artifact = ctx .actions .declare_file (name_prefix + "/" + ctx .label .name + "/" + merged_output_name )
329354
@@ -335,6 +360,7 @@ def _merge_llvm_profiles(
335360 arguments = [ctx .actions .args ().add ("merge" ).add ("-o" ).add (profile_artifact ).add (profile1 ).add (profile2 )],
336361 inputs = [profile1 , profile2 ],
337362 outputs = [profile_artifact ],
363+ env = env ,
338364 use_default_shell_env = True ,
339365 progress_message = "LLVMProfDataAction: Generating %{output}" ,
340366 )
0 commit comments