diff mbox series

[RFC,debug] Fix handling of vlas in lto

Message ID 20180817100430.GA29582@delia
State New
Headers show
Series [RFC,debug] Fix handling of vlas in lto | expand

Commit Message

Tom de Vries Aug. 17, 2018, 10:04 a.m. UTC
Hi,

Atm, when running vla-1.c with -O0 -flto, we have:
...
FAIL: gcc.dg/guality/vla-1.c -O0 -flto -fuse-linker-plugin \
  -fno-fat-lto-objects line 17 sizeof (a) == 6
...

The vla a[i + 1] in f1 is gimplified into:
...
f1 (int i)
{
  char a[0:D.1922] [value-expr: *a.0];
  char[0:D.1922] * a.0;

  D.1921 = i + 1;
  D.1926 = (sizetype) D.1921;
  a.0 = __builtin_alloca_with_align (D.1926, 8);
...

The early debug info for the upper bound of the type of vla a that we stream
out is:
...
  DIE    0: DW_TAG_subrange_type (0x7f85029a90f0)
    DW_AT_upper_bound: location descriptor:
      (0x7f85029a9230) DW_OP_GNU_variable_value die -> 0 (0x7f85029a94b0), 0
  DIE    0: DW_TAG_variable (0x7f85029a94b0)
    DW_AT_name: "D.1922"
    DW_AT_type: die -> 0 (0x7f85029a3d70)
    DW_AT_artificial: 1
...

and in ltrans we have for that same upper bound:
...
  DIE    0: DW_TAG_subrange_type (0x7f5183b57d70)
    DW_AT_upper_bound: die -> 0 (0x7f5183b576e0)
  DIE    0: DW_TAG_variable (0x7f5183b576e0)
    DW_AT_name: "D.4278"
    DW_AT_abstract_origin: die -> label: vla_1.c.6719312a + 193 (0x7f5183b57730)
...
where D.4278 has abstract origin D.1922.

The D.4278 die has no DW_AT_location, so when evaluting "sizeof (a)" in the
debugger, we can't find the information to get the value of D.4278, and the
debugger prints "<optimized out>".

This patch fixes that by either:
- adding DW_AT_location to the referenced variable die, or
- instead of using a ref for the upper bound, using an exprloc.

When changing gcc.dg/guality/guality.exp to run the usual flto flavours
"-fno-use-linker-plugin -flto-partition=none" and "-fuse-linker-plugin
-fno-fat-lto-objects" in combination with O0, Og, O1, O2, O3 and Os, this patch
fixes all (20) failures in vla-1.c, leaving only:
...
No symbol "i" in current context.
UNSUPPORTED: gcc.dg/guality/vla-1.c  -O3 -flto -fno-use-linker-plugin \
  -flto-partition=none line 17 i == 5
'a' has unknown type; cast it to its declared type
UNSUPPORTED: gcc.dg/guality/vla-1.c  -O3 -flto -fno-use-linker-plugin \
  -flto-partition=none line 17 sizeof (a) == 6
...

Bootstrapped and reg-tested on x86_64.

Any comments?

Is the approach in this patch ok, given that this approach works without
requiring a gdb with DW_OP_GNU_variable_value support?

Thanks,
- Tom

[debug] Fix handling of vlas in lto

2018-08-17  Tom de Vries  <tdevries@suse.de>

	* dwarf2out.c (dw_loc_list_1): Improve handling of want_address == 1.
	(add_scalar_info): Try harder to add an exprloc for DW_AT_upper_bound.
	Otherwise, try to add a DW_AT_location to the referenced die.

---
 gcc/dwarf2out.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 8c6b4372874..408c3fda8cd 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -16870,7 +16870,7 @@  dw_loc_list_1 (tree loc, rtx varloc, int want_address,
   if (!descr)
     return 0;
 
-  if (want_address == 2 && !have_address
+  if (want_address != 0 && !have_address
       && (dwarf_version >= 4 || !dwarf_strict))
     {
       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
@@ -20702,6 +20702,25 @@  add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
 	     later parameter.  */
 	  if (decl_die != NULL)
 	    {
+	      if (attr == DW_AT_upper_bound)
+		{
+		  list = loc_list_from_tree (decl, 1, context);
+		  if (list != NULL)
+		    {
+		      if (single_element_loc_list_p (list)
+			  && (forms & dw_scalar_form_exprloc) != 0)
+			{
+			  list = loc_list_from_tree (decl, 0, context);
+			  add_AT_loc (die, attr, list->expr);
+			  return;
+			}
+
+		      if (!get_AT (decl_die, DW_AT_location))
+			add_AT_location_description (decl_die, DW_AT_location,
+						     list);
+		    }
+		}
+
 	      add_AT_die_ref (die, attr, decl_die);
 	      return;
 	    }