From 1d9f13fd9245c66de02478875c9f6c3eea3bc978 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 21 Aug 2013 13:10:23 +0200
Subject: [PATCH] Amend CFLAGS for gcc 4.8 to prevent optimization problems due
 to variable length structs

gcc 4.8 concludes it can terminate some loops prematurely, causing e.g. initdb
to fail in optimized builds, because we embed variable length structs inside
catalog structs.
If those embedded variable length structs are not the trailing field gcc
concludes that they an area of memory has to be of a certain size and uses that
information to infer that some loops can only iterate a limited number of
times.
In reality, the actual struct layout isn't used for any of the elements beyond
the first variable length element, it's just there for the benefit
genbki.pl. Later fields are only accessed indirectly via heap_getattr() and
friends. But the compiler doesn't know that.
Commits 8137f2c3 / 8a3f745f, which are only included in 9.2 and onwards, thus
hide all variable length fields after the first one via #ifdefs.

For the benefit of earlier releases, let configure check for
-fno-aggressive-loop-optimizations, which disables this kind of
optimization. It was concluded that this way of fixing problems arising from
the optimization is less likely to cause problems than backporting the
aforementioned commits.

Per discussion on the hackers mailinglist.
---
 configure.in | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.in b/configure.in
index cf1afeb..76cd5eb 100644
--- a/configure.in
+++ b/configure.in
@@ -438,6 +438,9 @@ if test "$GCC" = yes -a "$ICC" = no; then
   PGAC_PROG_CC_CFLAGS_OPT([-fwrapv])
   # Disable FP optimizations that cause various errors on gcc 4.5+ or maybe 4.6+
   PGAC_PROG_CC_CFLAGS_OPT([-fexcess-precision=standard])
+  # Disable some loop optimizations, causes error due to variable length structs
+  # needed for < 9.2 and gcc 4.8+
+  PGAC_PROG_CC_CFLAGS_OPT([-fno-aggressive-loop-optimizations])
 elif test "$ICC" = yes; then
   # Intel's compiler has a bug/misoptimization in checking for
   # division by NAN (NaN == 0), -mp1 fixes it, so add it to the CFLAGS.
-- 
1.8.2.rc2.4.g7799588.dirty

