From fe81b49e727efdcc90a270520af193af75f4e31d Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 3 Jun 2019 09:58:11 -0700 Subject: [PATCH] Support of using the absolute path profile for LLVM's Context Sensitive FDO This CL adds the support of using the absolute path profile in LLVM's Context Sensitive FDO. The newly added option is --cs_fdo_absolute_path= The profile can be of the zip file containing the profile file, a raw or an indexed LLVM profile file. Unlike --fdo_optimize, it will not accept a profile label. Profile label should should use option --cs_fdo_profile. RELNOTES: Add new options --cs_fdo_absolute_path= to support the absolute path profile for LLVM's context-sensitive FDO. PiperOrigin-RevId: 251252510 --- .../build/lib/rules/cpp/CppConfiguration.java | 25 +++++++++++++++++++ .../build/lib/rules/cpp/CppOptions.java | 11 ++++++++ .../build/lib/rules/cpp/FdoHelper.java | 5 +++- .../build/lib/rules/cpp/CcToolchainTest.java | 18 +++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java index d72b76b3a6159c..6d7798ef1a3a46 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java @@ -147,6 +147,8 @@ public String toString() { private final PathFragment fdoPath; private final Label fdoOptimizeLabel; + private final PathFragment csFdoAbsolutePath; + private final ImmutableList conlyopts; private final ImmutableList copts; @@ -197,6 +199,22 @@ static CppConfiguration create(CpuTransformer cpuTransformer, BuildOptions optio } } + PathFragment csFdoAbsolutePath = null; + if (cppOptions.csFdoAbsolutePathForBuild != null) { + csFdoAbsolutePath = PathFragment.create(cppOptions.csFdoAbsolutePathForBuild); + if (!csFdoAbsolutePath.isAbsolute()) { + throw new InvalidConfigurationException( + "Path of '" + + csFdoAbsolutePath.getPathString() + + "' in --cs_fdo_absolute_path is not an absolute path."); + } + try { + FileSystemUtils.checkBaseName(csFdoAbsolutePath.getBaseName()); + } catch (IllegalArgumentException e) { + throw new InvalidConfigurationException(e); + } + } + return new CppConfiguration( cppOptions.doNotUseCpuTransformer ? commonOptions.cpu @@ -204,6 +222,7 @@ static CppConfiguration create(CpuTransformer cpuTransformer, BuildOptions optio Preconditions.checkNotNull(commonOptions.cpu), fdoPath, fdoProfileLabel, + csFdoAbsolutePath, ImmutableList.copyOf(cppOptions.conlyoptList), ImmutableList.copyOf(cppOptions.coptList), ImmutableList.copyOf(cppOptions.cxxoptList), @@ -224,6 +243,7 @@ private CppConfiguration( String desiredCpu, PathFragment fdoPath, Label fdoOptimizeLabel, + PathFragment csFdoAbsolutePath, ImmutableList conlyopts, ImmutableList copts, ImmutableList cxxopts, @@ -239,6 +259,7 @@ private CppConfiguration( this.desiredCpu = desiredCpu; this.fdoPath = fdoPath; this.fdoOptimizeLabel = fdoOptimizeLabel; + this.csFdoAbsolutePath = csFdoAbsolutePath; this.conlyopts = conlyopts; this.copts = copts; this.cxxopts = cxxopts; @@ -548,6 +569,10 @@ public String getCSFdoInstrument() { return cppOptions.csFdoInstrumentForBuild; } + public PathFragment getCSFdoAbsolutePath() { + return csFdoAbsolutePath; + } + Label getFdoPrefetchHintsLabel() { if (isThisHostConfigurationDoNotUseWillBeRemovedFor129045294()) { // We don't want FDO in the host configuration diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java index 52a0619c789ddd..91641f628c8422 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppOptions.java @@ -431,6 +431,17 @@ public String getFdoOptimize() { + "dumped at runtime.") public String csFdoInstrumentForBuild; + @Option( + name = "cs_fdo_absolute_path", + defaultValue = "null", + documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS, + effectTags = {OptionEffectTag.AFFECTS_OUTPUTS}, + help = + "Use CSFDO profile information to optimize compilation. Specify the absolute path name " + + "of the zip file containing the profile file, a raw or an indexed " + + "LLVM profile file.") + public String csFdoAbsolutePathForBuild; + @Option( name = "xbinary_fdo", defaultValue = "null", diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoHelper.java index 687373a6bbf044..aab809c0a9ebf9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoHelper.java @@ -90,7 +90,10 @@ public static FdoContext getFdoContext( } Pair csFdoInputs = null; - if (cppConfiguration.getCSFdoProfileLabel() != 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) { diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java index eb32956d7c8135..dc6120c7ce529d 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainTest.java @@ -454,6 +454,24 @@ public void testFdoOptimizeNotCompatibleWithCoverage() throws Exception { assertContainsEvent("coverage mode is not compatible with FDO optimization"); } + @Test + public void testCSFdoRejectRelativePath() throws Exception { + reporter.removeHandler(failFastHandler); + scratch.file("a/BUILD", "cc_toolchain_alias(name = 'b')"); + scratch.file("a/profile.profdata", ""); + scratch.file("a/csprofile.profdata", ""); + Exception e = + assertThrows( + Exception.class, + () -> + useConfiguration( + "-c", + "opt", + "--fdo_optimize=a/profile.profdata", + "--cs_fdo_absolute_path=a/csprofile.profdata")); + assertThat(e).hasMessageThat().contains("in --cs_fdo_absolute_path is not an absolute path"); + } + @Test public void testXFdoOptimizeNotProvider() throws Exception { reporter.removeHandler(failFastHandler);