[m-rev.] for review: add a workaround for Mantis bug #492

Julien Fischer jfischer at opturion.com
Thu Jan 16 18:58:55 AEDT 2020


For review by anyone.

Branches: master, 20.01

--------------------------

Add a workaround for Mantis bug #492.

In debug grades that use global register variables, the generated C code is
triggering an internal error in GCC 9 on x86_64 machines. Force GCC to compile
at -O0 in this case as that seems to be the only workaround for the issue.

scripts/mgnuc.in:
     Force GCC to use -O0 in the above case.

compiler/compile_target_code.m:
     Do the same when GCC is invoked directly by the Mercury compiler.

     Re-arrange some of the code that applies C compiler bug workarounds
     to make this possible.

     Add an XXX about an overly broad bug workaround on darwin; I'll look
     into that separately.

Julien.

diff --git a/compiler/compile_target_code.m b/compiler/compile_target_code.m
index b257b6845..bd31a3995 100644
--- a/compiler/compile_target_code.m
+++ b/compiler/compile_target_code.m
@@ -349,6 +349,7 @@ gather_c_compiler_flags(Globals, PIC, AllCFlags) :-
      ),
      globals.lookup_string_option(Globals, cflags_for_sanitizers,
          SanitizerOpts),
+    globals.get_c_compiler_type(Globals, C_CompilerType),
      globals.lookup_bool_option(Globals, use_trail, UseTrail),
      (
          UseTrail = yes,
@@ -362,7 +363,6 @@ gather_c_compiler_flags(Globals, PIC, AllCFlags) :-
          % Note that this will also affect the untagged version of the trail,
          % but that shouldn't matter.
          %
-        globals.get_c_compiler_type(Globals, C_CompilerType),
          (
              C_CompilerType = cc_gcc(_, _, _),
              globals.lookup_int_option(Globals, bytes_per_word, BytesPerWord),
@@ -432,9 +432,10 @@ gather_c_compiler_flags(Globals, PIC, AllCFlags) :-
      % program which fails with this optimization.

      globals.lookup_string_option(Globals, target_arch, TargetArch),
+    globals.lookup_bool_option(Globals, gcc_global_registers, GlobalRegisters),
      ( if
          globals.lookup_bool_option(Globals, highlevel_code, no),
-        globals.lookup_bool_option(Globals, gcc_global_registers, yes),
+        GlobalRegisters = yes,
          string.prefix(TargetArch, "powerpc-apple-darwin")
      then
          AppleGCCRegWorkaroundOpt = "-fno-loop-optimize "
@@ -442,12 +443,33 @@ gather_c_compiler_flags(Globals, PIC, AllCFlags) :-
          AppleGCCRegWorkaroundOpt = ""
      ),

-    % Workaround performance problem(s) with gcc that causes the C files
-    % generated in debugging grades to compile very slowly at -O1 and above.
-    % (Changes here need to be reflected in scripts/mgnuc.in.)
+    % Last resort workarounds for C compiler bugs.
+    % Changes here need to be reflected in scripts/mgnuc.in.
+    %
+    globals.lookup_bool_option(Globals, exec_trace, ExecTrace),
      ( if
-        globals.lookup_bool_option(Globals, exec_trace, yes),
-        arch_is_apple_darwin(TargetArch)
+        % We need to disable C compiler optimizations in debugging grades
+        % in either of the two situations described below.
+        ExecTrace = yes,
+        (
+            % 1. On Apple Darwin systems there are performance problems with
+            % GCC that cause it to compile the C files generated in debugging
+            % grades very slowly at -O1 or greater.
+            %
+            % XXX we are also enabling this for clang; does it have the
+            % same performance problems?
+            %
+            arch_is_apple_darwin(TargetArch)
+        ;
+            % 2. There is a bug in GCC 9.[12] that results in an internal error
+            % in the LRA pass when compiling generated C files in debugging
+            % grades that also use global registers on x86_64 machines.
+            % See: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91430>
+            %
+            GlobalRegisters = yes,
+            C_CompilerType = cc_gcc(yes(9), _, _),
+            string.prefix(TargetArch, "x86_64")
+        )
      then
          OverrideOpts = "-O0"
      else
diff --git a/scripts/mgnuc.in b/scripts/mgnuc.in
index fda5a685e..4d7034ac0 100755
--- a/scripts/mgnuc.in
+++ b/scripts/mgnuc.in
@@ -594,6 +594,21 @@ case "$FULLARCH" in
          ;;
  esac

+# Using global register variables triggers an internal error in the LRA pass of
+# GCC 9.1 and 9.2 on x86_64 systems in debug grades unless we compile at -O0.
+# See: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91430>
+#
+# Changes to this need to be reflected in the predicate
+# gather_c_compiler_flags/3 in compiler/compile_target_code.m.
+
+case "$FULLARCH" in x86_64*)
+    case $global_regs,$debug in true,true)
+        case "$C_COMPILER_TYPE" in gcc_9_*)
+            ARCH_OPTS="$ARCH_OPTS -O0" ;;
+        esac ;;
+    esac ;;
+esac
+
  # The -floop-optimize option is incompatible with the global register code
  # we generated on Darwin PowerPC. See the hard_coded/ppc_bug test case
  # for an example program which fails with this optimization.



More information about the reviews mailing list