Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suboptimal optimization flags for C/C++ #181

Open
GoogleCodeExporter opened this issue Aug 3, 2015 · 2 comments
Open

Suboptimal optimization flags for C/C++ #181

GoogleCodeExporter opened this issue Aug 3, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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
Copy link
Author

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

  • Added labels: Priority-Low

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant