hquxmu / ai-contest

Automatically exported from code.google.com/p/ai-contest
0 stars 0 forks source link

Suboptimal optimization flags for C/C++ #181

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Quoting from my forum posting: 
http://ai-contest.com/forum/viewtopic.php?f=18&t=700&start=0

It's also worth noting that -funroll-loops is an undesirable optimization for 
two reasons:

* GCC has a wickedly advanced loop optimizer. It will unroll loops when it's 
able to determine that faster or smaller code will be produced; it rarely 
misses a chance since 4.3. Forcing loop unrolling basically guarantees that 
slower code will be generated, probably in the fast inner loops we're all 
counting on to be zippy.

* In the context of C90, without the use of the 'restrict' keyword, GCC will 
not be able to determine that the iterations of loops over pointers to objects 
are distinct, that the loop can be sanely unrolled. GCC will, as a result, 
produce unrolled loops with large amounts of load/store instructions where none 
would be needed if 'restrict' were judiciously used.

More sane optimization flags would be -fwhopr if the contest machine is using 
4.5 and, certainly, -mtune=native. Depending on the contest CPU cache sizes, 
-Os is a better fit than -O3 for object file compilation. The link -O2 combined 
with -fwhopr would be perfectly reasonable.

I will shortly submit a patch to fix this issue.

Original issue reported on code.google.com by brian%tr...@gtempaccount.com on 29 Sep 2010 at 12:19

GoogleCodeExporter commented 8 years ago
Index: compile_anything.py
===================================================================
--- compile_anything.py (revision 380)
+++ compile_anything.py (working copy)
@@ -99,11 +99,11 @@
     for source in sources:
       object_file = \
         source.replace(".cc", "").replace(".cpp", "").replace(".c", "") + ".o"
-      out, err = system(['g++', '-O3', '-funroll-loops', '-c', '-o', \
+      out, err = system(['g++', '-Os', '-mtune=native', '-c', '-o', \
         object_file, source])
       out_message += out
       err_message += err
-    out, err = system(['g++', '-O2', '-o', 'MyBot'] + safeglob('*.o') + 
['-lm'])
+    out, err = system(['g++', '-O2', '-mtune=native', '-fwhopr', '-o', 
'MyBot'] + safeglob('*.o') + ['-lm'])
     out_message += out
     err_message += err
     err_message += check_path('MyBot')
@@ -114,8 +114,8 @@
     sources = safeglob('*.c')
     for source in sources:
       object_file = source.replace(".c", "") + ".o"
-      system(['gcc', '-O3', '-funroll-loops', '-c', '-o', object_file, source])
-    system(['gcc', '-O2', '-o', 'MyBot'] + safeglob('*.o') + ['-lm'])
+      system(['gcc', '-Os', '-mtune=native', '-c', '-o', object_file, source])
+    system(['gcc', '-O2', '-mtune=native', '-fwhopr', '-o', 'MyBot'] + 
safeglob('*.o') + ['-lm'])
     check_path('MyBot')
   if language == "Go":
     nukeglob('*.6')

Original comment by brian%tr...@gtempaccount.com on 29 Sep 2010 at 12:24

GoogleCodeExporter commented 8 years ago

Original comment by janzert on 6 Nov 2010 at 3:49