Skip to content

Commit

Permalink
Do not try to unzip fdo profile in the host configuration
Browse files Browse the repository at this point in the history
    bazelbuild/bazel#6516

    RELNOTES: None.
    PiperOrigin-RevId: 241523004
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent 211dc1c commit 1b07f74
Showing 1 changed file with 6 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static FdoContext getFdoContext(
CppToolchainInfo toolchainInfo)
throws InterruptedException, RuleErrorException {
FdoInputFile fdoInputFile = null;
FdoInputFile csFdoInputFile = null;
FdoInputFile prefetchHints = null;
Artifact protoProfileArtifact = null;
Pair<FdoInputFile, Artifact> fdoInputs = null;
Expand Down Expand Up @@ -88,26 +87,6 @@ public static FdoContext getFdoContext(
!= null) {
fdoInputs = getFdoInputs(ruleContext, attributes.getXFdoProfileProvider());
}

Pair<FdoInputFile, Artifact> csFdoInputs = null;
PathFragment csFdoZip = cppConfiguration.getCSFdoAbsolutePath();
if (csFdoZip != null) {
csFdoInputFile = FdoInputFile.fromAbsolutePath(csFdoZip);
} else if (cppConfiguration.getCSFdoProfileLabel() != null) {
csFdoInputs = getFdoInputs(ruleContext, attributes.getCSFdoProfileProvider());
}
if (csFdoInputs != null) {
csFdoInputFile = csFdoInputs.getFirst();
}
}

if (ruleContext.hasErrors()) {
return null;
}

if (fdoInputs != null) {
fdoInputFile = fdoInputs.getFirst();
protoProfileArtifact = fdoInputs.getSecond();
}

if (ruleContext.hasErrors()) {
Expand Down Expand Up @@ -136,12 +115,6 @@ public static FdoContext getFdoContext(
ruleContext.ruleError("invalid extension for FDO profile file.");
return null;
}
// Check if this is LLVM_CS_FDO
if (branchFdoMode == BranchFdoMode.LLVM_FDO) {
if (csFdoInputFile != null) {
branchFdoMode = BranchFdoMode.LLVM_CS_FDO;
}
}
if (branchFdoMode != BranchFdoMode.XBINARY_FDO
&& cppConfiguration.getXFdoProfileLabelUnsafeSinceItCanReturnValueFromWrongConfiguration()
!= null) {
Expand All @@ -157,7 +130,7 @@ public static FdoContext getFdoContext(
if (branchFdoMode == BranchFdoMode.LLVM_FDO) {
profileArtifact =
convertLLVMRawProfileToIndexed(
attributes, fdoInputFile, toolchainInfo, ruleContext, cppConfiguration, "fdo");
attributes, fdoInputFile, toolchainInfo, ruleContext, cppConfiguration);
if (ruleContext.hasErrors()) {
return null;
}
Expand All @@ -171,33 +144,6 @@ public static FdoContext getFdoContext(
profileArtifact,
fdoInputFile,
"Symlinking FDO profile " + fdoInputFile.getBasename());
} else if (branchFdoMode == BranchFdoMode.LLVM_CS_FDO) {
Artifact nonCSProfileArtifact =
convertLLVMRawProfileToIndexed(
attributes, fdoInputFile, toolchainInfo, ruleContext, cppConfiguration, "fdo");
if (ruleContext.hasErrors()) {
return null;
}
Artifact csProfileArtifact =
convertLLVMRawProfileToIndexed(
attributes, csFdoInputFile, toolchainInfo, ruleContext, cppConfiguration, "csfdo");
if (ruleContext.hasErrors()) {
return null;
}
if (nonCSProfileArtifact != null && csProfileArtifact != null) {
profileArtifact =
mergeLLVMProfiles(
attributes,
toolchainInfo,
ruleContext,
nonCSProfileArtifact,
csProfileArtifact,
"mergedfdo",
"MergedCS.profdata");
if (ruleContext.hasErrors()) {
return null;
}
}
}
branchFdoProfile =
new FdoContext.BranchFdoProfile(branchFdoMode, profileArtifact, protoProfileArtifact);
Expand Down Expand Up @@ -263,43 +209,6 @@ private static void symlinkTo(
}
}

/** This function merges profile1 and profile2 and generates mergedOutput. */
private static Artifact mergeLLVMProfiles(
CcToolchainAttributesProvider attributes,
CppToolchainInfo toolchainInfo,
RuleContext ruleContext,
Artifact profile1,
Artifact profile2,
String fdoUniqueArtifactName,
String mergedOutput) {
Artifact profileArtifact =
ruleContext.getUniqueDirectoryArtifact(
fdoUniqueArtifactName, mergedOutput, ruleContext.getBinOrGenfilesDirectory());

// Merge LLVM profiles.
ruleContext.registerAction(
new SpawnAction.Builder()
.addInput(profile1)
.addInput(profile2)
.addTransitiveInputs(attributes.getAllFilesMiddleman())
.addOutput(profileArtifact)
.useDefaultShellEnvironment()
.setExecutable(toolchainInfo.getToolPathFragment(Tool.LLVM_PROFDATA))
.setProgressMessage("LLVMProfDataAction: Generating %s", profileArtifact.prettyPrint())
.setMnemonic("LLVMProfDataMergeAction")
.addCommandLine(
CustomCommandLine.builder()
.add("merge")
.add("-o")
.addExecPath(profileArtifact)
.addExecPath(profile1)
.addExecPath(profile2)
.build())
.build(ruleContext));

return profileArtifact;
}

/*
* This function checks the format of the input profile data and converts it to
* the indexed format (.profdata) if necessary.
Expand All @@ -309,15 +218,14 @@ private static Artifact convertLLVMRawProfileToIndexed(
FdoInputFile fdoProfile,
CppToolchainInfo toolchainInfo,
RuleContext ruleContext,
CppConfiguration cppConfiguration,
String fdoUniqueArtifactName) {
CppConfiguration cppConfiguration) {
if (cppConfiguration.isThisHostConfigurationDoNotUseWillBeRemovedFor129045294()) {
return null;
}

Artifact profileArtifact =
ruleContext.getUniqueDirectoryArtifact(
fdoUniqueArtifactName,
"fdo",
getLLVMProfileFileName(fdoProfile, CppFileTypes.LLVM_PROFILE),
ruleContext.getBinOrGenfilesDirectory());

Expand Down Expand Up @@ -355,14 +263,12 @@ private static Artifact convertLLVMRawProfileToIndexed(
}
rawProfileArtifact =
ruleContext.getUniqueDirectoryArtifact(
fdoUniqueArtifactName, rawProfileFileName, ruleContext.getBinOrGenfilesDirectory());
"fdo", rawProfileFileName, ruleContext.getBinOrGenfilesDirectory());

// Symlink to the zipped profile file to extract the contents.
Artifact zipProfileArtifact =
ruleContext.getUniqueDirectoryArtifact(
fdoUniqueArtifactName,
fdoProfile.getBasename(),
ruleContext.getBinOrGenfilesDirectory());
"fdo", fdoProfile.getBasename(), ruleContext.getBinOrGenfilesDirectory());
symlinkTo(
ruleContext,
zipProfileArtifact,
Expand Down Expand Up @@ -391,7 +297,7 @@ private static Artifact convertLLVMRawProfileToIndexed(
} else {
rawProfileArtifact =
ruleContext.getUniqueDirectoryArtifact(
fdoUniqueArtifactName,
"fdo",
getLLVMProfileFileName(fdoProfile, CppFileTypes.LLVM_PROFILE_RAW),
ruleContext.getBinOrGenfilesDirectory());
symlinkTo(
Expand Down

0 comments on commit 1b07f74

Please sign in to comment.