diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst index 2d0d71443dfd..ed6c9e3bc341 100644 --- a/clang/docs/UsersManual.rst +++ b/clang/docs/UsersManual.rst @@ -2172,6 +2172,17 @@ programs using the same instrumentation method as ``-fprofile-generate``. profile file, it reads from that file. If ``pathname`` is a directory name, it reads from ``pathname/default.profdata``. +.. option:: -fprofile-update[=] + + Unless ``-fsanitize=thread`` is specified, the default is ``single``, which + uses non-atomic increments. The counters can be inaccurate under thread + contention. ``atomic`` uses atomic increments which is accurate but has + overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported + by the target, or ``single`` otherwise. + + This option currently works with ``-fprofile-arcs`` and ``-fprofile-instr-generate``, + but not with ``-fprofile-generate``. + Disabling Instrumentation ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index a259218b29c6..062a8c3fe64a 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -185,6 +185,7 @@ CODEGENOPT(ObjCConvertMessagesToRuntimeCalls , 1, 1) VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified. VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified. +CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic /// Choose profile instrumenation kind or no instrumentation. ENUM_CODEGENOPT(ProfileInstr, ProfileInstrKind, 2, ProfileNone) /// Choose profile kind for PGO use compilation. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index a1f3d7a4316f..09fdf50b1cb8 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -853,6 +853,9 @@ def fprofile_filter_files_EQ : Joined<["-"], "fprofile-filter-files=">, def fprofile_exclude_files_EQ : Joined<["-"], "fprofile-exclude-files=">, Group, Flags<[CC1Option, CoreOption]>, HelpText<"Instrument only functions from files where names don't match all the regexes separated by a semi-colon">; +def fprofile_update_EQ : Joined<["-"], "fprofile-update=">, + Group, Flags<[CC1Option, CoreOption]>, Values<"atomic,prefer-atomic,single">, + MetaVarName<"">, HelpText<"Set update method of profile counters (atomic,prefer-atomic,single)">; def forder_file_instrumentation : Flag<["-"], "forder-file-instrumentation">, Group, Flags<[CC1Option, CoreOption]>, HelpText<"Generate instrumented code to collect order file into default.profraw file (overridden by '=' form of option or LLVM_PROFILE_FILE env var)">; diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index f83ec2479652..d77590cc2adf 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -570,7 +570,7 @@ static Optional getGCOVOptions(const CodeGenOptions &CodeGenOpts, Options.NoRedZone = CodeGenOpts.DisableRedZone; Options.Filter = CodeGenOpts.ProfileFilterFiles; Options.Exclude = CodeGenOpts.ProfileExcludeFiles; - Options.Atomic = LangOpts.Sanitize.has(SanitizerKind::Thread); + Options.Atomic = CodeGenOpts.AtomicProfileUpdate; return Options; } @@ -582,10 +582,7 @@ getInstrProfOptions(const CodeGenOptions &CodeGenOpts, InstrProfOptions Options; Options.NoRedZone = CodeGenOpts.DisableRedZone; Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput; - - // TODO: Surface the option to emit atomic profile counter increments at - // the driver level. - Options.Atomic = LangOpts.Sanitize.has(SanitizerKind::Thread); + Options.Atomic = CodeGenOpts.AtomicProfileUpdate; return Options; } diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 12b3c8615e91..272a49899012 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -868,6 +868,17 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, CmdArgs.push_back(Args.MakeArgString(Twine("-fprofile-filter-files=" + v))); } + if (const auto *A = Args.getLastArg(options::OPT_fprofile_update_EQ)) { + StringRef Val = A->getValue(); + if (Val == "atomic" || Val == "prefer-atomic") + CmdArgs.push_back("-fprofile-update=atomic"); + else if (Val != "single") + D.Diag(diag::err_drv_unsupported_option_argument) + << A->getOption().getName() << Val; + } else if (TC.getSanitizerArgs().needsTsanRt()) { + CmdArgs.push_back("-fprofile-update=atomic"); + } + // Leave -fprofile-dir= an unused argument unless .gcda emission is // enabled. To be polite, with '-fprofile-arcs -fno-profile-arcs' consider // the flag used. There is no -fno-profile-dir, so the user has no diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 42224339250d..b402f53cc765 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -884,6 +884,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.DebugRangesBaseAddress = Args.hasArg(OPT_fdebug_ranges_base_address); setPGOInstrumentor(Opts, Args, Diags); + Opts.AtomicProfileUpdate = Args.hasArg(OPT_fprofile_update_EQ); Opts.InstrProfileOutput = std::string(Args.getLastArgValue(OPT_fprofile_instrument_path_EQ)); Opts.ProfileInstrumentUsePath = diff --git a/clang/test/CodeGen/code-coverage-tsan.c b/clang/test/CodeGen/code-coverage-tsan.c index 17f6596aa83d..47eabaa375e5 100644 --- a/clang/test/CodeGen/code-coverage-tsan.c +++ b/clang/test/CodeGen/code-coverage-tsan.c @@ -1,11 +1,12 @@ -/// -fsanitize=thread requires the (potentially concurrent) counter updates to be atomic. -// RUN: %clang_cc1 %s -triple x86_64 -emit-llvm -fsanitize=thread -femit-coverage-notes -femit-coverage-data \ +/// -fprofile-update=atomic (implied by -fsanitize=thread) requires the +/// (potentially concurrent) counter updates to be atomic. +// RUN: %clang_cc1 %s -triple x86_64 -emit-llvm -fprofile-update=atomic -femit-coverage-notes -femit-coverage-data \ // RUN: -coverage-notes-file /dev/null -coverage-data-file /dev/null -o - | FileCheck %s // CHECK-LABEL: void @foo() /// Two counters are incremented by __tsan_atomic64_fetch_add. -// CHECK: call i64 @__tsan_atomic64_fetch_add -// CHECK-NEXT: call i32 @__tsan_atomic32_fetch_sub +// CHECK: atomicrmw add i64* {{.*}} @__llvm_gcov_ctr +// CHECK-NEXT: atomicrmw sub i32* _Atomic(int) cnt; void foo() { cnt--; } diff --git a/clang/test/CodeGen/tsan-instrprof-atomic.c b/clang/test/CodeGen/tsan-instrprof-atomic.c index 9519cb7eb8ed..48d39424e73c 100644 --- a/clang/test/CodeGen/tsan-instrprof-atomic.c +++ b/clang/test/CodeGen/tsan-instrprof-atomic.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 %s -emit-llvm -fprofile-instrument=clang -fsanitize=thread -o - | FileCheck %s +// RUN: %clang_cc1 %s -emit-llvm -fprofile-instrument=clang -fprofile-update=atomic -o - | FileCheck %s // CHECK: define {{.*}}@foo // CHECK-NOT: load {{.*}}foo diff --git a/clang/test/Driver/fprofile-update.c b/clang/test/Driver/fprofile-update.c new file mode 100644 index 000000000000..befbcea03b87 --- /dev/null +++ b/clang/test/Driver/fprofile-update.c @@ -0,0 +1,15 @@ +/// For -fprofile-instr-generate and -fprofile-arcs, increment counters atomically +/// if -fprofile-update={atomic,prefer-atomic} or -fsanitize=thread is specified. +// RUN: %clang -### %s -c -target x86_64-linux -fsanitize=thread %s 2>&1 | FileCheck %s +// RUN: %clang -### %s -c -fprofile-update=atomic 2>&1 | FileCheck %s +// RUN: %clang -### %s -c -fprofile-update=prefer-atomic 2>&1 | FileCheck %s + +// CHECK: "-fprofile-update=atomic" + +// RUN: %clang -### %s -c -fprofile-update=atomic -fprofile-update=single 2>&1 | FileCheck %s --check-prefix=SINGLE + +// SINGLE-NOT: "-fprofile-update=atomic" + +// RUN: not %clang %s -c -fprofile-update=unknown 2>&1 | FileCheck %s --check-prefix=ERROR + +// ERROR: error: unsupported argument 'unknown' to option 'fprofile-update='