[clang] [compiler-rt] [llvm] [InstrProf] Created Thread local counter instrumentation, compiler-rt runtimes (PR #95494)
https://github.com/ajwock created https://github.com/llvm/llvm-project/pull/95494 LLVM can now generate increments to counters in thread local storage. Use a new compiler-rt runtime to atomically add thread local counters to global counters on thread exit. The clang driver will link the new runtime libraries in when the new option -fprofile-thread-local is specified. More details available in the RFC on discourse. >From 44e2159636efd601c90aced44856d17d77728caa Mon Sep 17 00:00:00 2001 From: Andrew Wock Date: Tue, 4 Jun 2024 09:45:31 -0400 Subject: [PATCH] Created Thread local counter instrumentation. LLVM can now generate increments to counters in thread local storage. Use a new compiler-rt runtime to atomically add thread local counters to global counters on thread exit. The clang driver will link the new runtime libraries in when the new option -fprofile-thread-local is specified. Signed-off-by: Andrew Wock --- clang/docs/UsersManual.rst| 8 ++ clang/include/clang/Basic/CodeGenOptions.def | 1 + clang/include/clang/Driver/Options.td | 3 + clang/include/clang/Driver/ToolChain.h| 6 + clang/lib/Driver/ToolChain.cpp| 10 ++ clang/lib/Driver/ToolChains/Clang.cpp | 12 ++ clang/lib/Driver/ToolChains/Linux.cpp | 7 + compiler-rt/include/profile/InstrProfData.inc | 4 + compiler-rt/lib/profile/CMakeLists.txt| 35 + .../lib/profile/InstrProfilingDyLibLinux.cpp | 63 + compiler-rt/lib/profile/InstrProfilingFile.c | 6 + .../lib/profile/InstrProfilingPlatformLinux.c | 1 + .../profile/InstrProfilingStaticTLSLinux.cpp | 123 ++ compiler-rt/lib/profile/InstrProfilingTLS.c | 29 + compiler-rt/lib/profile/InstrProfilingTLS.h | 39 ++ .../lib/profile/InstrProfilingTLSDyLib.c | 100 ++ .../lib/profile/InstrProfilingTLSDyLib.h | 4 + compiler-rt/lib/tsan/rtl/CMakeLists.txt | 2 +- .../Inputs/instrprof-tls-dlclose-lib.c| 7 + .../Inputs/instrprof-tls-dlclose-main.c | 93 + .../Inputs/instrprof-tls-dlopen-func.c| 9 ++ .../Inputs/instrprof-tls-dlopen-func2.c | 9 ++ .../Inputs/instrprof-tls-dlopen-main.c| 105 +++ .../test/profile/Inputs/instrprof-tls-exit.c | 37 ++ .../Linux/instrprof-tls-dlclose-memfault.test | 27 .../instrprof-tls-dlclose-mix-subset.test | 41 ++ .../Linux/instrprof-tls-dlclose-mix.test | 48 +++ .../Linux/instrprof-tls-dlclose-nodelete.test | 24 .../profile/Linux/instrprof-tls-dlopen.test | 32 + .../profile/Linux/instrprof-tls-exit.test | 17 +++ .../Linux/instrprof-tls-noclose-mix.test | 51 .../instrprof-tls-shared-mix-subset.test | 35 + .../Linux/instrprof-tls-shared-mix.test | 48 +++ llvm/include/llvm/ProfileData/InstrProf.h | 3 + .../llvm/ProfileData/InstrProfData.inc| 4 + .../Instrumentation/InstrProfiling.cpp| 71 +- 36 files changed, 1110 insertions(+), 4 deletions(-) create mode 100644 compiler-rt/lib/profile/InstrProfilingDyLibLinux.cpp create mode 100644 compiler-rt/lib/profile/InstrProfilingStaticTLSLinux.cpp create mode 100644 compiler-rt/lib/profile/InstrProfilingTLS.c create mode 100644 compiler-rt/lib/profile/InstrProfilingTLS.h create mode 100644 compiler-rt/lib/profile/InstrProfilingTLSDyLib.c create mode 100644 compiler-rt/lib/profile/InstrProfilingTLSDyLib.h create mode 100644 compiler-rt/test/profile/Inputs/instrprof-tls-dlclose-lib.c create mode 100644 compiler-rt/test/profile/Inputs/instrprof-tls-dlclose-main.c create mode 100644 compiler-rt/test/profile/Inputs/instrprof-tls-dlopen-func.c create mode 100644 compiler-rt/test/profile/Inputs/instrprof-tls-dlopen-func2.c create mode 100644 compiler-rt/test/profile/Inputs/instrprof-tls-dlopen-main.c create mode 100644 compiler-rt/test/profile/Inputs/instrprof-tls-exit.c create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-dlclose-memfault.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-dlclose-mix-subset.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-dlclose-mix.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-dlclose-nodelete.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-dlopen.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-exit.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-noclose-mix.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-shared-mix-subset.test create mode 100644 compiler-rt/test/profile/Linux/instrprof-tls-shared-mix.test diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst index f954857b0235a..f7db513b92909 100644 --- a/clang/docs/UsersManual.rst +++ b/clang/docs/UsersManual.rst @@ -2932,6 +2932,14 @@ indexed format, regardeless whether it i
[clang] [compiler-rt] [llvm] [InstrProf] Created Thread local counter instrumentation, compiler-rt runtimes (PR #95494)
ajwock wrote: https://discourse.llvm.org/t/rfc-profiling-counters-in-thread-local-storage/79596 https://github.com/llvm/llvm-project/pull/95494 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add option to avoid generating coverage mappings for unused functions (PR #92582)
https://github.com/ajwock created https://github.com/llvm/llvm-project/pull/92582 This change adds an option which prevents generating coverage mapping data for functions which aren't used. This reduces binary size in cases where many unused static inline functions are automatically generated. >From da030dd074ad253c7f0b0376384c441ceaf3c42c Mon Sep 17 00:00:00 2001 From: Andrew Wock Date: Fri, 17 May 2024 11:52:16 -0400 Subject: [PATCH] Add option to avoid generating coverage mappings for unused functions This change adds an option which prevents generating coverage mapping data for functions which aren't used. This reduces binary size in cases where many unused static inline functions are automatically generated. Signed-off-by: Andrew Wock --- clang/include/clang/Basic/CodeGenOptions.def | 3 +++ clang/include/clang/Driver/Options.td | 3 +++ clang/lib/CodeGen/CodeGenModule.cpp| 4 +++- clang/test/CoverageMapping/deferred_unused_names.c | 11 +++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 clang/test/CoverageMapping/deferred_unused_names.c diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index 07b0ca1691a67..8e63d28ffdb83 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -222,6 +222,9 @@ CODEGENOPT(CoverageMapping , 1, 0) ///< Generate coverage mapping regions to ///< enable code coverage analysis. CODEGENOPT(DumpCoverageMapping , 1, 0) ///< Dump the generated coverage mapping ///< regions. +CODEGENOPT(NoUnusedCoverage , 1, 0) ///< Turn off coverage mapping for code that +///< is not emitted. + CODEGENOPT(MCDCCoverage , 1, 0) ///< Enable MC/DC code coverage criteria. /// If -fpcc-struct-return or -freg-struct-return is specified. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 7bb781667e926..5ca8425e4dd71 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -7065,6 +7065,9 @@ def coverage_version_EQ : Joined<["-"], "coverage-version=">, def dump_coverage_mapping : Flag<["-"], "dump-coverage-mapping">, HelpText<"Dump the coverage mapping records, for testing">, MarshallingInfoFlag>; +def no_unused_coverage : Flag<["-"], "no-unused-coverage">, + HelpText<"Turn off coverage mapping for code that is not emitted">, + MarshallingInfoFlag>; def fuse_register_sized_bitfield_access: Flag<["-"], "fuse-register-sized-bitfield-access">, HelpText<"Use register sized accesses to bit-fields, when possible.">, MarshallingInfoFlag>; diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 489c08a4d4819..a037c258bfa27 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -891,7 +891,9 @@ void CodeGenModule::Release() { EmitGlobalAnnotations(); EmitStaticExternCAliases(); checkAliases(); - EmitDeferredUnusedCoverageMappings(); + if (!CodeGenOpts.NoUnusedCoverage) { +EmitDeferredUnusedCoverageMappings(); + } CodeGenPGO(*this).setValueProfilingFlag(getModule()); CodeGenPGO(*this).setProfileVersion(getModule()); if (CoverageMapping) diff --git a/clang/test/CoverageMapping/deferred_unused_names.c b/clang/test/CoverageMapping/deferred_unused_names.c new file mode 100644 index 0..488bba527631b --- /dev/null +++ b/clang/test/CoverageMapping/deferred_unused_names.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -mllvm -enable-name-compression=false -no-unused-coverage -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -main-file-name unused_names.c -o - %s > %t +// RUN: FileCheck -input-file %t %s + +// There should only be a prf_names entry for bar, as the other two functions are +// unused. +// +// CHECK-DAG: @__llvm_prf_nm = private constant [5 x i8] c"\03\00bar", section "{{.*__llvm_prf_names|\.lprfn\$M}}" + +int bar(void) { return 0; } +inline int baz(void) { return 0; } +static int qux(void) { return 42; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add option to avoid generating coverage mappings for unused functions (PR #92582)
ajwock wrote: cc @MaskRay @hyp @zygoloid as possible reviewers https://github.com/llvm/llvm-project/pull/92582 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add option to avoid generating coverage mappings for unused functions (PR #92582)
ajwock wrote: cc @ZequanWu @ellishg for review https://github.com/llvm/llvm-project/pull/92582 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add option to avoid generating coverage mappings for unused functions (PR #92582)
ajwock wrote: The problem that this solves is related to binary size. In situations where a large C API is in generated code but a small part of the API is used, the size of __llvm_prf_names and __llvm_cov_fun sections can be prohibitively large. This flag offers a way to reduce binary size in this case assuming you aren't concerned with coverage of non-emitted code. Would this flag make more sense if it were named differently? For example, `-fcoverage-mapping-only-emitted-code`? https://github.com/llvm/llvm-project/pull/92582 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits