From: Dehao Chen Date: Thu, 24 Aug 2017 21:37:33 +0000 (+0000) Subject: Expose -mllvm -accurate-sample-profile to clang. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9619ec9a5eb42ebc8f71c933a919df08dfd90f86;p=clang Expose -mllvm -accurate-sample-profile to clang. Summary: With accurate sample profile, we can do more aggressive size optimization. For some size-critical application, this can reduce the text size by 20% Reviewers: davidxl, rsmith Reviewed By: davidxl, rsmith Subscribers: mehdi_amini, eraman, sanjoy, cfe-commits Differential Revision: https://reviews.llvm.org/D37091 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311707 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 6f40bb03e3..2c58e27363 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -637,12 +637,25 @@ def fno_profile_sample_use : Flag<["-"], "fno-profile-sample-use">, Group; +def fprofile_sample_accurate : Flag<["-"], "fprofile-sample-accurate">, + Group, Flags<[DriverOption, CC1Option]>, + HelpText<"Specifies that the sample profile is accurate">, + DocBrief<[{Specifies that the sample profile is accurate. If the sample + profile is accurate, callsites without profile samples are marked + as cold. Otherwise, treat callsites without profile samples as if + we have no profile}]>; +def fno_profile_sample_accurate : Flag<["-"], "fno-profile-sample-accurate">, + Group, Flags<[DriverOption]>; def fauto_profile : Flag<["-"], "fauto-profile">, Group, Alias; def fno_auto_profile : Flag<["-"], "fno-auto-profile">, Group, Alias; def fauto_profile_EQ : Joined<["-"], "fauto-profile=">, Alias; +def fauto_profile_accurate : Flag<["-"], "fauto-profile-accurate">, + Group, Alias; +def fno_auto_profile_accurate : Flag<["-"], "fno-auto-profile-accurate">, + Group, Alias; def fdebug_info_for_profiling : Flag<["-"], "fdebug-info-for-profiling">, Group, Flags<[CC1Option]>, HelpText<"Emit extra debug info to make sample profile more accurate.">; diff --git a/include/clang/Frontend/CodeGenOptions.def b/include/clang/Frontend/CodeGenOptions.def index 6a866fe507..c9040e7e40 100644 --- a/include/clang/Frontend/CodeGenOptions.def +++ b/include/clang/Frontend/CodeGenOptions.def @@ -183,6 +183,7 @@ CODEGENOPT(UnsafeFPMath , 1, 0) ///< Allow unsafe floating point optzns. CODEGENOPT(UnwindTables , 1, 0) ///< Emit unwind tables. CODEGENOPT(VectorizeLoop , 1, 0) ///< Run loop vectorizer. CODEGENOPT(VectorizeSLP , 1, 0) ///< Run SLP vectorizer. +CODEGENOPT(ProfileSampleAccurate, 1, 0) ///< Sample profile is accurate. /// Attempt to use register sized accesses to bit-fields in structures, when /// possible. diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 17295ae2be..a9a1a53be2 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -838,6 +838,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, Fn->addFnAttr("no-jump-tables", llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables)); + // Add profile-sample-accurate value. + if (CGM.getCodeGenOpts().ProfileSampleAccurate) + Fn->addFnAttr("profile-sample-accurate"); + if (getLangOpts().OpenCL) { // Add metadata for a kernel function. if (const FunctionDecl *FD = dyn_cast_or_null(D)) diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp index 35dcf206ab..fa768b70c5 100644 --- a/lib/Driver/ToolChains/Clang.cpp +++ b/lib/Driver/ToolChains/Clang.cpp @@ -2340,6 +2340,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, true)) CmdArgs.push_back("-fno-jump-tables"); + if (Args.hasFlag(options::OPT_fprofile_sample_accurate, + options::OPT_fno_profile_sample_accurate, false)) + CmdArgs.push_back("-fprofile-sample-accurate"); + if (!Args.hasFlag(options::OPT_fpreserve_as_comments, options::OPT_fno_preserve_as_comments, true)) CmdArgs.push_back("-fno-preserve-as-comments"); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 24a08e84dc..aea3af9828 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -652,6 +652,8 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.NoUseJumpTables = Args.hasArg(OPT_fno_jump_tables); + Opts.ProfileSampleAccurate = Args.hasArg(OPT_fprofile_sample_accurate); + Opts.PrepareForLTO = Args.hasArg(OPT_flto, OPT_flto_EQ); Opts.EmitSummaryIndex = false; if (Arg *A = Args.getLastArg(OPT_flto_EQ)) { diff --git a/test/CodeGen/profile-sample-accurate.c b/test/CodeGen/profile-sample-accurate.c new file mode 100644 index 0000000000..cf628d55cf --- /dev/null +++ b/test/CodeGen/profile-sample-accurate.c @@ -0,0 +1,7 @@ +// Test to ensure -emit-llvm profile-sample-accurate is honored by clang. +// RUN: %clang -S -emit-llvm %s -fprofile-sample-accurate -o - | FileCheck %s + +// CHECK: define void @foo() +// CHECK: attributes {{.*}} "profile-sample-accurate" +void foo() { +} diff --git a/test/Driver/clang_f_opts.c b/test/Driver/clang_f_opts.c index c17cec6eba..bb058a00cd 100644 --- a/test/Driver/clang_f_opts.c +++ b/test/Driver/clang_f_opts.c @@ -53,6 +53,9 @@ // CHECK-REROLL-LOOPS: "-freroll-loops" // CHECK-NO-REROLL-LOOPS-NOT: "-freroll-loops" +// RUN: %clang -### -S -fprofile-sample-accurate %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-SAMPLE-ACCURATE %s +// CHECK-PROFILE-SAMPLE-ACCURATE: "-fprofile-sample-accurate" + // RUN: %clang -### -S -fprofile-sample-use=%S/Inputs/file.prof %s 2>&1 | FileCheck -check-prefix=CHECK-SAMPLE-PROFILE %s // CHECK-SAMPLE-PROFILE: "-fprofile-sample-use={{.*}}/file.prof" diff --git a/test/Integration/thinlto_profile_sample_accurate.c b/test/Integration/thinlto_profile_sample_accurate.c new file mode 100644 index 0000000000..d7966c215c --- /dev/null +++ b/test/Integration/thinlto_profile_sample_accurate.c @@ -0,0 +1,9 @@ +// Test to ensure -emit-llvm profile-sample-accurate is honored in ThinLTO. +// RUN: %clang -O2 %s -flto=thin -fprofile-sample-accurate -c -o %t.o +// RUN: llvm-lto -thinlto -o %t %t.o +// RUN: %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -emit-llvm -o - | FileCheck %s + +// CHECK: define void @foo() +// CHECK: attributes {{.*}} "profile-sample-accurate" +void foo() { +}