From 8e886f8502784d3aafdaf7e9778ce21b8c8f3b93 Mon Sep 17 00:00:00 2001 From: Hafiz Abid Qadeer Date: Fri, 16 Jul 2021 21:00:37 +0100 Subject: [PATCH] [DWARF] Fix hierarchy of debug information for offload kernels. Currently, if we look at the debug information for offload kernel regions, it looks something like this: void foo (void) { { } } DW_TAG_compile_unit DW_AT_name ("") DW_TAG_subprogram // notional parent function (foo) with no code range DW_TAG_subprogram // offload function foo._omp_fn.0 There is an artificial compile unit. It contains a parent subprogram which has the offload function as its child. The parent function makes sense in host code where it actually exists and does have an address range. But in offload code, it does not exist and neither the generated dwarf has an address range for this function. When debugger read the dwarf for offload code, they see a function with no address range and discard it alongwith its children which include offload function. This results in a poor debug experience of offload code. This patch was suggested by Richard and it solves this problem by peeling the parent function from the concrete copies. gcc/ * gcc/dwarf2out.c (maybe_create_die_with_external_ref): Remove function from the context chain. --- gcc/dwarf2out.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index 561f8b23517..e2893bd91ed 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -6121,6 +6121,11 @@ maybe_create_die_with_external_ref (tree decl) /* Peel types in the context stack. */ while (ctx && TYPE_P (ctx)) ctx = TYPE_CONTEXT (ctx); + /* For functions peel the context up to namespace/TU scope. The abstract + copies reveal the true nesting. */ + if (TREE_CODE (decl) == FUNCTION_DECL) + while (ctx && TREE_CODE (ctx) == FUNCTION_DECL) + ctx = DECL_CONTEXT (ctx); /* Likewise namespaces in case we do not want to emit DIEs for them. */ if (debug_info_level <= DINFO_LEVEL_TERSE) while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) -- 2.25.1