[Bug 1685385] Re: GCC generates bad code with zero length array and -O3

Bug Watch Updater 1685385 at bugs.launchpad.net
Sat Feb 15 08:34:58 UTC 2020


Launchpad has imported 5 comments from the remote bug at
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80533.

If you reply to an imported comment from within Launchpad, your comment
will be sent to the remote bug automatically. Read more about
Launchpad's inter-bugtracker facilities at
https://help.launchpad.net/InterBugTracking.

------------------------------------------------------------------------
On 2017-04-26T19:07:20+00:00 3-sje wrote:

Created attachment 41274
Test case

GCC should treat zero length arrays like C99 flexible arrays when they are
at the end of a structure.  I.e. recognize that accesses to that array may
go beyond the end of the structure.   GCC has been treating zero length
arrays and flexible arrays differently since at least 5.0.

This was found on aarch64 but the bug is not platform specific.  A test case
is attached, it cannot be run but if compiled for aarch64 with
-O2 -fno-strict-aliasing and either -UFLEX or -DFLEX you can see the
different code.  In the -UFLEX case it generates a load/load/store/store
sequence and in the -DFLEX case it generates load/store/load/store for
the code in the main loop.

See also:

https://gcc.gnu.org/ml/gcc/2017-04/msg00118.html
https://gcc.gnu.org/ml/gcc-patches/2017-04/msg01257.html

Reply at:
https://bugs.launchpad.net/ubuntu/+source/gcc-6/+bug/1685385/comments/2

------------------------------------------------------------------------
On 2017-04-27T07:55:09+00:00 Rguenth wrote:

Mine.

Reply at:
https://bugs.launchpad.net/ubuntu/+source/gcc-6/+bug/1685385/comments/3

------------------------------------------------------------------------
On 2017-04-27T12:35:16+00:00 Rguenth wrote:

Author: rguenth
Date: Thu Apr 27 12:34:45 2017
New Revision: 247327

URL: https://gcc.gnu.org/viewcvs?rev=247327&root=gcc&view=rev
Log:
2017-04-27  Richard Biener  <rguenther at suse.de>

	PR middle-end/80533
	* emit-rtl.c (set_mem_attributes_minus_bitpos): When
	stripping ARRAY_REFs from MEM_EXPR make sure we're not
	keeping a reference to a trailing array.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/emit-rtl.c

Reply at:
https://bugs.launchpad.net/ubuntu/+source/gcc-6/+bug/1685385/comments/4

------------------------------------------------------------------------
On 2017-04-27T12:59:11+00:00 Rguenth wrote:

Fixed on trunk sofar.

Reply at:
https://bugs.launchpad.net/ubuntu/+source/gcc-6/+bug/1685385/comments/5

------------------------------------------------------------------------
On 2020-02-14T09:48:33+00:00 Rguenth wrote:

Fixed on all still maintained branches.

Reply at:
https://bugs.launchpad.net/ubuntu/+source/gcc-6/+bug/1685385/comments/8


** Changed in: gcc
       Status: In Progress => Fix Released

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to gcc-5 in Ubuntu.
https://bugs.launchpad.net/bugs/1685385

Title:
  GCC generates bad code with zero length array and -O3

Status in gcc:
  Fix Released
Status in gcc-5 package in Ubuntu:
  New
Status in gcc-6 package in Ubuntu:
  Fix Released

Bug description:
  With the program below, if I compile it with '-O3 -DFLEX' I get good code but if I compile it with '-O3 -UFLEX' then I get bad code.  The program is (unfortunately) not runnable but if you generate
  assembly language you can see that the good version has a loop with ldr/str/ldr/str and the bad
  version has a loop with ldr/ldr/str/str.  I.e. the second load has been moved to be before the first store and if they are referencing the same memory that will result in incorrect behavior.

  It is unclear to me why declaring the array 'o' at the end of struct 's' as a zero length array
  instead of a C99 flexible array would affect the code generation but it does.  The version with
  the flexible array works, the version with a zero length array does not work (i.e. it moves the
  second load up to before the first store).

  Test case (compile with -O3 and either -DFLEX or -UFLEX):

  struct q {
  	int b;
  };
  struct r {
     int n;
     struct q slot[0];
  };
  struct s {
     int n;
  #ifdef FLEX
   long int o[];
  #else
   long int o[0];
  #endif
  };
  extern int x, y, m;
  extern struct s *a;
  extern struct r *b;
  extern void bar();
  int foo() {
     int i,j;
     for (i = 0; i < m; i++) {
     	a->o[i] = sizeof(*a);
     	b = ((struct r *)(((char *)a) + a->o[a->n]));
  	for (j = 0; j < 10; j++) {
  		b->slot[j].b = 0;
     	}
          bar();
    }
  }

  ProblemType: Bug
  DistroRelease: Ubuntu 16.04
  Package: gcc 4:5.3.1-1ubuntu1
  Uname: Linux 4.10.0-rc2-00045-g2748079 aarch64
  ApportVersion: 2.20.1-0ubuntu2.5
  Architecture: arm64
  Date: Fri Apr 21 16:07:53 2017
  JournalErrors:
   Error: command ['journalctl', '-b', '--priority=warning', '--lines=1000'] failed with exit code 1: Failed to search journal ACL: Operation not supported
   No journal files were opened due to insufficient permissions.
  ProcEnviron:
   TERM=xterm-256color
   PATH=(custom, no user)
   XDG_RUNTIME_DIR=<set>
   LANG=en_US.UTF-8
   SHELL=/bin/bash
  SourcePackage: gcc-defaults
  UpgradeStatus: No upgrade log present (probably fresh install)

To manage notifications about this bug go to:
https://bugs.launchpad.net/gcc/+bug/1685385/+subscriptions



More information about the foundations-bugs mailing list