Add flags to disable profile generation.

Justin Bogner justin at justinbogner.com
Wed Aug 5 13:04:47 PDT 2015


Diego Novillo <dnovillo at google.com> writes:
> This patch adds flags -fno-profile-instr-generate and
> -fno-profile-instr-use, and the GCC aliases -fno-profile-generate and
> -fno-profile-use.
>
> These flags are used in situations where users need to disable profile
> generation or use for specific files in a build, without affecting other
> files.
>
> OK to commit?

Sure, but we should probably add a -fno-coverage-mapping as well, since
`-fprofile-generate -fcoverage-mapping -fno-profile-generate` will error
with this change. Do you mind adding that in a followup?

> Thanks.  Diego.
>
> From 016d91cdf4540497f338a3ad4e2da612e29c7502 Mon Sep 17 00:00:00 2001
> From: Diego Novillo <dnovillo at google.com>
> Date: Wed, 5 Aug 2015 15:02:16 -0400
> Subject: [PATCH] Add flags to disable profile generation.
>
> This patch adds flags -fno-profile-instr-generate and
> -fno-profile-instr-use, and the GCC aliases -fno-profile-generate and
> -fno-profile-use.
>
> These flags are used in situations where users need to disable profile
> generation or use for specific files in a build, without affecting other
> files.
> ---
>  docs/UsersManual.rst            | 13 ++++++++
>  include/clang/Driver/Options.td | 10 ++++++
>  lib/Driver/Tools.cpp            | 67 ++++++++++++++++++++++++-----------------
>  test/Driver/clang_f_opts.c      | 10 ++++++
>  4 files changed, 72 insertions(+), 28 deletions(-)
>
> diff --git a/docs/UsersManual.rst b/docs/UsersManual.rst
> index 842538c..9a70d63 100644
> --- a/docs/UsersManual.rst
> +++ b/docs/UsersManual.rst
> @@ -1534,6 +1534,19 @@ with respect to profile creation and use.
>    profile file, it reads from that file. If ``pathname`` is a directory name,
>    it reads from ``pathname/default.profdata``.
>  
> +Disabling Instrumentation
> +^^^^^^^^^^^^^^^^^^^^^^^^^
> +
> +In certain situations, it may be useful to disable profile generation or use
> +for specific files in a build, without affecting the main compilation flags
> +used for the other files in the project.
> +
> +In these cases, you can use the flag ``-fno-profile-instr-generate`` (or
> +``-fno-profile-generate``) to disable profile generation, and
> +``-fno-profile-instr-use`` (or ``-fno-profile-use``) to disable profile use.
> +
> +Note that these flags should appear after the corresponding profile
> +flags to have an effect.
>  
>  Controlling Size of Debug Information
>  -------------------------------------
> diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
> index 63e880d..d84e03e 100644
> --- a/include/clang/Driver/Options.td
> +++ b/include/clang/Driver/Options.td
> @@ -446,6 +446,16 @@ def fprofile_use : Flag<["-"], "fprofile-use">, Group<f_Group>,
>  def fprofile_use_EQ : Joined<["-"], "fprofile-use=">,
>      Group<f_Group>, Flags<[DriverOption]>, MetaVarName<"<pathname>">,
>      HelpText<"Use instrumentation data for profile-guided optimization. If pathname is a directory, it reads from <pathname>/default.profdata. Otherwise, it reads from file <pathname>.">;
> +def fno_profile_instr_generate : Flag<["-"], "fno-profile-instr-generate">,
> +    Group<f_Group>, Flags<[DriverOption]>,
> +    HelpText<"Disable generation of profile instrumentation.">;
> +def fno_profile_generate : Flag<["-"], "fno-profile-generate">,
> +    Alias<fno_profile_instr_generate>;
> +def fno_profile_instr_use : Flag<["-"], "fno-profile-instr-use">,
> +    Group<f_Group>, Flags<[DriverOption]>,
> +    HelpText<"Disable using instrumentation data for profile-guided optimization">;
> +def fno_profile_use : Flag<["-"], "fno-profile-use">,
> +    Alias<fno_profile_instr_use>;
>  
>  def fblocks : Flag<["-"], "fblocks">, Group<f_Group>, Flags<[CC1Option]>,
>    HelpText<"Enable the 'blocks' language feature">;
> diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
> index 2135b77..22863dc 100644
> --- a/lib/Driver/Tools.cpp
> +++ b/lib/Driver/Tools.cpp
> @@ -2848,42 +2848,53 @@ static void addPGOAndCoverageFlags(Compilation &C, const Driver &D,
>    auto *ProfileGenerateArg = Args.getLastArg(
>        options::OPT_fprofile_instr_generate,
>        options::OPT_fprofile_instr_generate_EQ, options::OPT_fprofile_generate,
> -      options::OPT_fprofile_generate_EQ);
> +      options::OPT_fprofile_generate_EQ,
> +      options::OPT_fno_profile_instr_generate);
> +  if (ProfileGenerateArg &&
> +      ProfileGenerateArg->getOption().matches(
> +          options::OPT_fno_profile_instr_generate))
> +    ProfileGenerateArg = nullptr;
>  
>    auto *ProfileUseArg = Args.getLastArg(
>        options::OPT_fprofile_instr_use, options::OPT_fprofile_instr_use_EQ,
> -      options::OPT_fprofile_use, options::OPT_fprofile_use_EQ);
> +      options::OPT_fprofile_use, options::OPT_fprofile_use_EQ,
> +      options::OPT_fno_profile_instr_use);
> +  if (ProfileUseArg &&
> +      ProfileUseArg->getOption().matches(options::OPT_fno_profile_instr_use))
> +    ProfileUseArg = nullptr;
>  
>    if (ProfileGenerateArg && ProfileUseArg)
>      D.Diag(diag::err_drv_argument_not_allowed_with)
>          << ProfileGenerateArg->getSpelling() << ProfileUseArg->getSpelling();
>  
> -  if (ProfileGenerateArg &&
> -      ProfileGenerateArg->getOption().matches(
> -          options::OPT_fprofile_instr_generate_EQ))
> -    ProfileGenerateArg->render(Args, CmdArgs);
> -  else if (ProfileGenerateArg &&
> -           ProfileGenerateArg->getOption().matches(
> -               options::OPT_fprofile_generate_EQ)) {
> -    SmallString<128> Path(ProfileGenerateArg->getValue());
> -    llvm::sys::path::append(Path, "default.profraw");
> -    CmdArgs.push_back(
> -        Args.MakeArgString(Twine("-fprofile-instr-generate=") + Path));
> -  } else
> -    Args.AddAllArgs(CmdArgs, options::OPT_fprofile_instr_generate);
> -
> -  if (ProfileUseArg &&
> -      ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
> -    ProfileUseArg->render(Args, CmdArgs);
> -  else if (ProfileUseArg &&
> -           (ProfileUseArg->getOption().matches(options::OPT_fprofile_use_EQ) ||
> -            ProfileUseArg->getOption().matches(
> -                options::OPT_fprofile_instr_use))) {
> -    SmallString<128> Path(
> -        ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
> -    if (Path.empty() || llvm::sys::fs::is_directory(Path))
> -      llvm::sys::path::append(Path, "default.profdata");
> -    CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-instr-use=") + Path));
> +  if (ProfileGenerateArg) {
> +    if (ProfileGenerateArg->getOption().matches(
> +            options::OPT_fprofile_instr_generate_EQ))
> +      ProfileGenerateArg->render(Args, CmdArgs);
> +    else if (ProfileGenerateArg->getOption().matches(
> +                 options::OPT_fprofile_generate_EQ)) {
> +      SmallString<128> Path(ProfileGenerateArg->getValue());
> +      llvm::sys::path::append(Path, "default.profraw");
> +      CmdArgs.push_back(
> +          Args.MakeArgString(Twine("-fprofile-instr-generate=") + Path));
> +    } else
> +      Args.AddAllArgs(CmdArgs, options::OPT_fprofile_instr_generate);
> +  }
> +
> +  if (ProfileUseArg) {
> +    if (ProfileUseArg->getOption().matches(options::OPT_fprofile_instr_use_EQ))
> +      ProfileUseArg->render(Args, CmdArgs);
> +    else if ((ProfileUseArg->getOption().matches(
> +                  options::OPT_fprofile_use_EQ) ||
> +              ProfileUseArg->getOption().matches(
> +                  options::OPT_fprofile_instr_use))) {
> +      SmallString<128> Path(
> +          ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
> +      if (Path.empty() || llvm::sys::fs::is_directory(Path))
> +        llvm::sys::path::append(Path, "default.profdata");
> +      CmdArgs.push_back(
> +          Args.MakeArgString(Twine("-fprofile-instr-use=") + Path));
> +    }
>    }
>  
>    if (Args.hasArg(options::OPT_ftest_coverage) ||
> diff --git a/test/Driver/clang_f_opts.c b/test/Driver/clang_f_opts.c
> index 0034301..fd1f854 100644
> --- a/test/Driver/clang_f_opts.c
> +++ b/test/Driver/clang_f_opts.c
> @@ -86,10 +86,20 @@
>  // RUN: %clang -### -S -fprofile-generate=dir -fprofile-use=dir %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MIX-GEN-USE %s
>  // RUN: %clang -### -S -fprofile-generate=dir -fprofile-instr-use %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MIX-GEN-USE %s
>  // RUN: %clang -### -S -fprofile-generate=dir -fprofile-instr-use=file %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MIX-GEN-USE %s
> +// RUN: %clang -### -S -fprofile-instr-generate=file -fno-profile-instr-generate %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-GEN %s
> +// RUN: %clang -### -S -fprofile-instr-generate=file -fno-profile-generate %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-GEN %s
> +// RUN: %clang -### -S -fprofile-generate=dir -fno-profile-generate %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-GEN %s
> +// RUN: %clang -### -S -fprofile-generate=dir -fno-profile-instr-generate %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-GEN %s
> +// RUN: %clang -### -S -fprofile-instr-use=file -fno-profile-instr-use %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-USE %s
> +// RUN: %clang -### -S -fprofile-instr-use=file -fno-profile-use %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-USE %s
> +// RUN: %clang -### -S -fprofile-use=file -fno-profile-use %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-USE %s
> +// RUN: %clang -### -S -fprofile-use=file -fno-profile-instr-use %s 2>&1 | FileCheck -check-prefix=CHECK-DISABLE-USE %s
>  // CHECK-PROFILE-GENERATE: "-fprofile-instr-generate"
>  // CHECK-PROFILE-GENERATE-DIR: "-fprofile-instr-generate=/some/dir{{/|\\\\}}default.profraw"
>  // CHECK-PROFILE-GENERATE-FILE: "-fprofile-instr-generate=/tmp/somefile.profraw"
>  // CHECK-NO-MIX-GEN-USE: '{{[a-z=-]*}}' not allowed with '{{[a-z=-]*}}'
> +// CHECK-DISABLE-GEN-NOT: "-fprofile-instr-generate"
> +// CHECK-DISABLE-USE-NOT: "-fprofile-instr-use"
>  
>  // RUN: %clang -### -S -fprofile-use %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-USE %s
>  // RUN: %clang -### -S -fprofile-instr-use %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-USE %s


More information about the cfe-commits mailing list