gcov: add Clang support
v2 cover gcov: add Clang support
From: Tri Vo <[email protected]>
This patch series adds Clang supoprt for gcov.
Patch 1 refactors existing code in preparation for Clang support. Patches 2-3 implement necessary LLVM runtime hooks and gcov kernel interfaces. Patch 4 simplifies config selection.
Greg Hackmann (2):
gcov: clang: move common gcc code into gcc_base.c
gcov: clang support
Nick Desaulniers (1):
gcov: clang: link/unlink profiling data set.
Tri Vo (1):
gcov: clang: pick GCC vs Clang format depending on compiler
kernel/gcov/Kconfig | 3 +- kernel/gcov/Makefile | 5 +- kernel/gcov/base.c | 78 +----- kernel/gcov/clang.c | 554 +++++++++++++++++++++++++++++++++++++++++ kernel/gcov/gcc_base.c | 79 ++++++ kernel/gcov/gcov.h | 3 + 6 files changed, 643 insertions(+), 79 deletions(-) create mode 100644 kernel/gcov/clang.c create mode 100644 kernel/gcov/gcc_base.c
v2:
- Reorganized config dependencies, as per Masahiro.
--
2.20.1.97.g81188d93c3-goog
PATCH v2 1/4 gcov: clang: move common gcc code into gcc_base.c
From: Greg Hackmann <[email protected]>
base.c contains a few callbacks specific to GCC's gcov implementation. Move these into their own module in preparation for clang support.
--- kernel/gcov/Makefile | 4 +-- kernel/gcov/base.c | 78 ++--------------------------------------- kernel/gcov/gcc_base.c | 79 ++++++++++++++++++++++++++++++++++++++++++ kernel/gcov/gcov.h | 3 ++ 4 files changed, 86 insertions(+), 78 deletions(-) create mode 100644 kernel/gcov/gcc_base.c
kernel/gcov/Makefile +2 -2
@@ -2,5 +2,5 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' obj-y := base.o fs.o -obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_3_4.o -obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_4_7.o +obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o +obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o
kernel/gcov/base.c +2 -76
84 lines not on this page · raw message
kernel/gcov/gcc_base.c new file +79 -0
79 lines not on this page · raw message
kernel/gcov/gcov.h +3 -0
@@ -83,4 +83,7 @@ struct gcov_link { };extern const struct gcov_link gcov_link[]; +extern int gcov_events_enabled; +extern struct mutex gcov_lock; +#endif /* GCOV_H */
Signature · 1 line
2.20.1.97.g81188d93c3-goog
PATCH v2 2/4 gcov: clang support
In reply to Tri VoFrom: Greg Hackmann <[email protected]>
LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.
We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:
llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()
This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.
See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().
--- kernel/gcov/Kconfig | 5 + kernel/gcov/Makefile | 1 + kernel/gcov/clang.c | 531 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 537 insertions(+) create mode 100644 kernel/gcov/clang.c
kernel/gcov/Kconfig +5 -0
@@ -71,6 +71,11 @@ config GCOV_FORMAT_4_7 ---help--- Select this option to use the format defined by GCC 4.7. +config GCOV_FORMAT_CLANG + bool "Clang format" + ---help--- + Select this option to use the format defined by Clang. + endchoice endmenu
kernel/gcov/Makefile +1 -0
@@ -4,3 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' obj-y := base.o fs.o obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o +obj-$(CONFIG_GCOV_FORMAT_CLANG) += clang.o
kernel/gcov/clang.c new file +531 -0
531 lines not on this page · raw message
Signature · 1 line
2.20.1.97.g81188d93c3-goog
PATCH v2 3/4 gcov: clang: link/unlink profiling data set.
In reply to Tri VoFrom: Nick Desaulniers <[email protected]>
gcov.h defines an interface to access gcov_info data.
Add Clang implementation of gcov_info_link/gcov_info_unlink interfaces.
--- kernel/gcov/clang.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
kernel/gcov/clang.c +28 -5
@@ -77,6 +77,7 @@ struct gcov_fn_info {u32 num_counters;u64 *counters; +const char *function_name; };static struct gcov_info *current_info;
@@ -124,7 +125,7 @@ void llvm_gcda_emit_function(u32 ident, const char *function_name,if (!info) {pr_warn_ratelimited ("failed to allocate gcov function info for %s\n" , - function_name); + function_name ?:"UNKNOWN" );return ; }
@@ -133,6 +134,8 @@ void llvm_gcda_emit_function(u32 ident, const char *function_name, info->checksum = func_checksum; info->use_extra_checksum = use_extra_checksum; info->cfg_checksum = cfg_checksum; +if (function_name) + info->function_name =kstrdup (function_name,GFP_KERNEL );list_add_tail (&info->head, ¤t_info->functions); }
@@ -193,6 +196,26 @@ struct gcov_info *gcov_info_next(struct gcov_info *info)return list_next_entry (info, head); } +/** +* gcov_info_link - link/add profiling data set to the list +* @info: profiling data set +*/ +void gcov_info_link (struct gcov_info *info) +{ +list_add_tail (&info->head, &clang_gcov_list); +} + +/** +* gcov_info_unlink - unlink/remove profiling data set from the list +* @prev: previous profiling data set +* @info: profiling data set +*/ +void gcov_info_unlink (struct gcov_info *prev,struct gcov_info *info) +{ +if (prev) +list_del (&prev->head); +} +/* Symbolic links to be created for each profiling data file. */ const struct gcov_link gcov_link[] = { {OBJ_TREE ,"gcno" },/* Link to .gcno file in $(objtree). */
@@ -256,8 +279,8 @@ static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)return NULL ;INIT_LIST_HEAD (&fn_dup->head); - fn_dup->name =kstrdup (fn->name,GFP_KERNEL ); -if (!fn_dup->name) + fn_dup->function_name =kstrdup (fn->function_name,GFP_KERNEL ); +if (!fn_dup->function_name) goto err_name; fn_dup->counters = kmemdup(fn->counters,
@@ -269,7 +292,7 @@ static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)return fn_dup; err_counters: -kfree (fn_dup->name); +kfree (fn_dup->function_name); err_name:kfree (fn_dup);return NULL ;
@@ -317,7 +340,7 @@ void gcov_info_free(struct gcov_info *info)struct gcov_fn_info *fn, *tmp;list_for_each_entry_safe (fn, tmp, &info->functions, head) { -kfree (fn->name); +kfree (fn->function_name);kfree (fn->counters);list_del (&fn->head);kfree (fn);
Signature · 1 line
2.20.1.97.g81188d93c3-goog
PATCH v2 4/4 gcov: clang: pick GCC vs Clang format depending on compiler
In reply to Tri VoFrom: Tri Vo <[email protected]>
Clang gcov format is only supported by Clang compiler, and Clang compiler only supports Clang format.
We set gcov format to depend on which compiler (GCC or Clang) is used.
Automatic format detection behavior is preserved because:
If GCC is used, one of the GCC gcov formats is selected. If Clang is used, Clang gcov format is selected.
--- kernel/gcov/Kconfig | 8 ++------ kernel/gcov/Makefile | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-)
kernel/gcov/Kconfig +2 -6
@@ -53,6 +53,7 @@ config GCOV_PROFILE_ALL choice prompt "Specify GCOV format" depends on GCOV_KERNEL + depends on CC_IS_GCC ---help--- The gcov format is usually determined by the GCC version, and the default is chosen according to your GCC version. However, there are
@@ -62,7 +63,7 @@ choice config GCOV_FORMAT_3_4 bool "GCC 3.4 format" - depends on CC_IS_GCC && GCC_VERSION < 40700 + depends on GCC_VERSION < 40700 ---help--- Select this option to use the format defined by GCC 3.4.
@@ -71,11 +72,6 @@ config GCOV_FORMAT_4_7 ---help--- Select this option to use the format defined by GCC 4.7. -config GCOV_FORMAT_CLANG - bool "Clang format" - ---help--- - Select this option to use the format defined by Clang. - endchoice endmenu
kernel/gcov/Makefile +1 -1
@@ -4,4 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' obj-y := base.o fs.o obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o -obj-$(CONFIG_GCOV_FORMAT_CLANG) += clang.o +obj-$(CONFIG_CC_IS_CLANG) += clang.o
Signature · 1 line
2.20.1.97.g81188d93c3-goog
Re: [PATCH v2 4/4] gcov: clang: pick GCC vs Clang format depending on compiler
+ Masahiro
Quoted 56 lines · Tri Vo
From: Tri Vo <[email protected]>
Clang gcov format is only supported by Clang compiler, and Clang compiler only supports Clang format.
We set gcov format to depend on which compiler (GCC or Clang) is used.
Automatic format detection behavior is preserved because:
If GCC is used, one of the GCC gcov formats is selected. If Clang is used, Clang gcov format is selected.Signed-off-by Tri Vo <[email protected]>--- kernel/gcov/Kconfig | 8 ++------ kernel/gcov/Makefile | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-)kernel/gcov/Kconfig +2
-6@@ -53,6 +53,7 @@ config GCOV_PROFILE_ALL choice prompt "Specify GCOV format" depends on GCOV_KERNEL + depends on CC_IS_GCC ---help--- The gcov format is usually determined by the GCC version, and the default is chosen according to your GCC version. However, there are@@ -62,7 +63,7 @@ choice config GCOV_FORMAT_3_4 bool "GCC 3.4 format" - depends on CC_IS_GCC && GCC_VERSION < 40700 + depends on GCC_VERSION < 40700 ---help--- Select this option to use the format defined by GCC 3.4.@@ -71,11 +72,6 @@ config GCOV_FORMAT_4_7 ---help--- Select this option to use the format defined by GCC 4.7. -config GCOV_FORMAT_CLANG - bool "Clang format" - ---help--- - Select this option to use the format defined by Clang. - endchoice endmenukernel/gcov/Makefile +0
-0
kernel/gcov/Makefile ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' · line 7
Quoted 5 lines
kernel/gcov/Makefile +1
-1@@ -4,4 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' obj-y := base.o fs.o obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o -obj-$(CONFIG_GCOV_FORMAT_CLANG) += clang.o +obj-$(CONFIG_CC_IS_CLANG) += clang.o
I think this was the suggestion from Masahiro from v1?
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCH v2 4/4] gcov: clang: pick GCC vs Clang format depending on compiler
Quoted 54 lines · Nick Desaulniers
+ Masahiro
Quoted 50 lines · Tri Vo
From: Tri Vo <[email protected]>
Clang gcov format is only supported by Clang compiler, and Clang compiler only supports Clang format.
We set gcov format to depend on which compiler (GCC or Clang) is used.
Automatic format detection behavior is preserved because:
If GCC is used, one of the GCC gcov formats is selected. If Clang is used, Clang gcov format is selected.Signed-off-by Tri Vo <[email protected]>--- kernel/gcov/Kconfig | 8 ++------ kernel/gcov/Makefile | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-)kernel/gcov/Kconfig +2
-6@@ -53,6 +53,7 @@ config GCOV_PROFILE_ALL choice prompt "Specify GCOV format" depends on GCOV_KERNEL + depends on CC_IS_GCC ---help--- The gcov format is usually determined by the GCC version, and the default is chosen according to your GCC version. However, there are@@ -62,7 +63,7 @@ choice config GCOV_FORMAT_3_4 bool "GCC 3.4 format" - depends on CC_IS_GCC && GCC_VERSION < 40700 + depends on GCC_VERSION < 40700 ---help--- Select this option to use the format defined by GCC 3.4.@@ -71,11 +72,6 @@ config GCOV_FORMAT_4_7 ---help--- Select this option to use the format defined by GCC 4.7. -config GCOV_FORMAT_CLANG - bool "Clang format" - ---help--- - Select this option to use the format defined by Clang. - endchoice
Quoted 13 lines
kernel/gcov/Makefile ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' · line 7
Quoted 11 lines
kernel/gcov/Kconfig +0
-0endmenukernel/gcov/Makefile +1
-1@@ -4,4 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' obj-y := base.o fs.o obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o -obj-$(CONFIG_GCOV_FORMAT_CLANG) += clang.o +obj-$(CONFIG_CC_IS_CLANG) += clang.oI think this was the suggestion from Masahiro from v1?
Yes.
What is strange is, 2/4 added GCOV_FORMAT_CLANG,
then 4/4 deletes it.
Probably, changing the patch order,
or squashing 4/4 into 2/4 would be cleaner.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v2 4/4] gcov: clang: pick GCC vs Clang format depending on compiler
Quoted 11 lines · Masahiro Yamada
Quoted 1 line
I think this was the suggestion from Masahiro from v1?
Yes.
What is strange is, 2/4 added
GCOV_FORMAT_CLANG,
then 4/4 deletes it.Probably, changing the patch order,
or squashing 4/4 into 2/4 would be cleaner.
Sure; Tri, I think it's ok for you to become author of 2/4 and put a "Co-authored-by:" tag for Greg.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Peter Oberparleiter
maintainerQuoted 12 lines · Tri Vo
From: Tri Vo <[email protected]>
This patch series adds Clang supoprt for gcov.
Patch 1 refactors existing code in preparation for Clang support. Patches 2-3 implement necessary LLVM runtime hooks and gcov kernel interfaces. Patch 4 simplifies config selection.
Greg Hackmann (2):
gcov: clang: move common gcc code intogcc_base.c
gcov: clang support
Quoted 5 lines
Nick Desaulniers (1):
gcov: clang: link/unlink profiling data set.Tri Vo (1):
gcov: clang: pick GCC vs Clang format depending on compiler
Overall this patch set looks good to me. It introduces a clean way for GCC and Clang specific implementation details to coexist.
A quick test compile using GCC on s390 with the patch set applied was successful, so it seems like the existing support is unaffected.
There are some detail suggestions on patch improvements that I'll post in separate e-mails.
Signature · 2 lines
Peter Oberparleiter Linux on Z Development - IBM Germany
Quoted 20 lines · Peter Oberparleiter
Quoted 12 lines · Tri Vo
From: Tri Vo <[email protected]>
This patch series adds Clang supoprt for gcov.
Patch 1 refactors existing code in preparation for Clang support. Patches 2-3 implement necessary LLVM runtime hooks and gcov kernel interfaces. Patch 4 simplifies config selection.
Greg Hackmann (2):
gcov: clang: move common gcc code intogcc_base.c
gcov: clang supportQuoted 5 lines
Nick Desaulniers (1):
gcov: clang: link/unlink profiling data set.Tri Vo (1):
gcov: clang: pick GCC vs Clang format depending on compiler
Quoted 8 lines
Overall this patch set looks good to me. It introduces a clean way for GCC and Clang specific implementation details to coexist.
A quick test compile using GCC on s390 with the patch set applied was successful, so it seems like the existing support is unaffected.
There are some detail suggestions on patch improvements that I'll post in separate e-mails.
Peter,
Thank you so much for the valuable code review and feedback. We'll prep a v3.
Tri,
I think you should change the authorship for the patches to yourself, and add a "Co-authored-by: " tag to the commit messages. Can probably squash this series into 2 patches; the one that separates out the shared code; and the new llvm additions. WDYT?
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 32 lines · Nick Desaulniers
Quoted 20 lines · Peter Oberparleiter
Quoted 12 lines · Tri Vo
From: Tri Vo <[email protected]>
This patch series adds Clang supoprt for gcov.
Patch 1 refactors existing code in preparation for Clang support. Patches 2-3 implement necessary LLVM runtime hooks and gcov kernel interfaces. Patch 4 simplifies config selection.
Greg Hackmann (2):
gcov: clang: move common gcc code intogcc_base.c
gcov: clang supportQuoted 5 lines
Nick Desaulniers (1):
gcov: clang: link/unlink profiling data set.Tri Vo (1):
gcov: clang: pick GCC vs Clang format depending on compilerQuoted 8 lines
Overall this patch set looks good to me. It introduces a clean way for GCC and Clang specific implementation details to coexist.
A quick test compile using GCC on s390 with the patch set applied was successful, so it seems like the existing support is unaffected.
There are some detail suggestions on patch improvements that I'll post in separate e-mails.
Quoted 8 lines
Peter,
Thank you so much for the valuable code review and feedback. We'll prep a v3.Tri,
I think you should change the authorship for the patches to yourself, and add a "Co-authored-by: " tag to the commit messages. Can probably squash this series into 2 patches; the one that separates out the shared code; and the new llvm additions. WDYT?
Squashing into 2 patches sounds good. I'll leave Greg's authorship in there though.