{"id":1202,"date":"2013-07-08T11:14:11","date_gmt":"2013-07-08T15:14:11","guid":{"rendered":"http:\/\/yuguangzhang.com\/blog\/?p=1202"},"modified":"2013-12-18T13:15:20","modified_gmt":"2013-12-18T18:15:20","slug":"enabling-gcc-graphite-and-lto-on-gentoo","status":"publish","type":"post","link":"http:\/\/yuguangzhang.com\/blog\/enabling-gcc-graphite-and-lto-on-gentoo\/","title":{"rendered":"Enabling GCC Graphite and LTO on Gentoo"},"content":{"rendered":"<p>In this article, we will be enabling the GCC options marked with &#8220;To use this code transformation, GCC has to be configured with &#8211;with-ppl and &#8211;with-cloog to enable the Graphite loop transformation infrastructure. &#8220;, referred to as graphite. In addition, <a href=\"http:\/\/gcc.gnu.org\/wiki\/LinkTimeOptimization\">Link Time Optimization<\/a>(LTO) refers to the options -flto and -fuse-linker-plugin. -fuse-linker-plugin tells GCC to use an external linker and -flto defers link time optimizations until the final link step so that the optimizer can work across the whole program rather than on individual object files. OpenMP is for parallelization and I save it for another article since I do not have benchmarks to show that it improves performance.<br \/>\nTo enable graphite, LTO, and OpenMP it is recommended to first install the latest version of GCC. More recent versions of GCC contain bug fixes for features we are about to enable. The GCC upgrade process is detailed in the guide <a href=\"http:\/\/www.gentoo.org\/doc\/en\/gcc-upgrading.xml\">http:\/\/www.gentoo.org\/doc\/en\/gcc-upgrading.xml<\/a>.<\/p>\n<p>After upgrading GCC, we start enabling the newer features. The steps follow <a href=\"http:\/\/wiki.gentoo.org\/wiki\/CFLAGS\">the\u00a0guidelines on the Gentoo wiki<\/a> roughly. However, \u00a0it is unnecessary to rebuild everything after upgrading GCC as explained in the official GCC upgrade guide. <em>libtool<\/em>\u00a0allows packages to be built against shared libraries without rebuilding every time the toolchain is upgraded.<br \/>\nBefore adding the graphite USE flag, it is essential to build cloog<br \/>\n<code>emerge dev-libs\/cloog dev-libs\/cloog-ppl<\/code><br \/>\nThen add graphite to CFLAGS and enable graphite USE flags<br \/>\n<code><br \/>\nGRAPHITE=\"-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block\"<br \/>\nCFLAGS=\"-flto=8\u00a0${GRAPHITE} -ftree-vectorize\"<br \/>\nCXXFLAGS=\"${CFLAGS}\"<br \/>\nLDFLAGS=\"${CFLAGS} -fuse-linker-plugin\"<br \/>\n# GCC >= 4.8.2 requires the lto flag<br \/>\nUSE=\"graphite lto\"<br \/>\n<\/code><\/p>\n<p>These flags can be appended to the ones you&#8217;re already using, although certain flags may conflict with the graphite optimizations. Since graphite flags such as\u00a0<em>-ftree-loop-distribution<\/em>\u00a0is intended to enable further\u00a0vectorization, I also enabled the\u00a0<em>-ftree-vectorize<\/em> flag. The\u00a0<em>-fuse-linker-plugin<\/em>\u00a0is a linker flag while\u00a0<em>-flto=n<\/em>\u00a0turns on\u00a0the standard link-time optimizer. I set n to 8 for 8 threads while linking. Another factor that could affect performance is\u00a0<em>-flto-partition<\/em>, which sets the partitioning algorithm.<br \/>\nTo use the linker plugin with LTO, set the linker to Gold:<br \/>\n<code>binutils-config --linker ld.gold<\/code><br \/>\nIn addition, before compiling the system it is necessary to build GCC with graphite:<\/p>\n<p><code>emerge gcc<br \/>\nemerge -e world<br \/>\n<\/code><\/p>\n<h3>Dealing with breakages<\/h3>\n<p>Several packages failed to compile with LTO and graphite enabled. These flags can be disabled on a per package basis. First create the files in <tt>\/etc\/portage\/env\/<\/tt> called <tt>no-lto.conf<\/tt> and <tt>no-graphite.conf<\/tt>. In no-graphite.conf, disable your graphite flags:<br \/>\n<code>CFLAGS=\"${CFLAGS} -fno-loop-interchange -fno-tree-loop-distribution -fno-loop-strip-mine -fno-loop-block\"<br \/>\nCXXFLAGS=\"${CXXFLAGS} -fno-loop-interchange -fno-tree-loop-distribution -fno-loop-strip-mine -fno-loop-block\"<br \/>\nLDFLAGS=\"${LDFLAGS} -fno-loop-interchange -fno-tree-loop-distribution -fno-loop-strip-mine -fno-loop-block\"<\/code><br \/>\nSimilary, disable your LTO flags in no-lto.conf:<br \/>\n<code>CFLAGS=\"${CFLAGS} -fno-lto -fno-use-linker-plugin\"<br \/>\nCXXFLAGS=\"${CXXFLAGS} -fno-lto -fno-use-linker-plugin\"<br \/>\nLDFLAGS=\"${LDFLAGS} -fno-lto -fno-use-linker-plugin\"<br \/>\n<\/code><br \/>\nEvery time a package breaks, add a line to <tt>\/etc\/portage\/package.env<\/tt> that either disables graphite or LTO. For example, I made these adjustments:<br \/>\n<code>net-misc\/curl no-graphite.conf<br \/>\nmedia-libs\/mesa no-graphite.conf<br \/>\nsys-apps\/findutils no-lto.conf<br \/>\nsys-apps\/gawk no-lto.conf<\/code><br \/>\nAfter adding a line you can resume the system rebuild with <tt>emerge --resume<\/tt>.<\/p>\n<h3>Notes<\/h3>\n<p>Before re-compiling your system, setting moving <tt>\/var\/tmp<\/tt> to RAM speeds things up. The Sabayon wiki has <a href=\"https:\/\/wiki.sabayon.org\/index.php?title=How_to_optimize_and_accelerate_your_system#Ram_Drive_using_tmpfs\">an article on performance<\/a>, just make sure you reboot after changing <tt>\/etc\/fstab<\/tt>.<\/p>\n<p>After building the current version of PPL, this message is displayed for upgrades:<br \/>\n<code><br \/>\n* After an upgrade of PPL it is important that you rebuild<br \/>\n* dev-libs\/cloog-ppl.<br \/>\n*<br \/>\n* If you use gcc-config to switch to an older compiler version than<br \/>\n* the one PPL was built with, PPL must be rebuilt with that version.<br \/>\n*<br \/>\n* In both cases failure to do this will get you this error when<br \/>\n* graphite flags are used:<br \/>\n*<br \/>\n* sorry, unimplemented: Graphite loop optimizations cannot be used<br \/>\n<\/code><\/p>\n<ul>\n<li><a href=\"http:\/\/forums.gentoo.org\/viewtopic-t-850087-postdays-0-postorder-asc-start-75.html?sid=8365ed64e18eef6243c362d14a964bdd\">Cloog and PPL must first be built without graphite enabled<\/a><\/li>\n<li><a href=\"http:\/\/forums.gentoo.org\/viewtopic-t-944230-start-0.html\">Packages to exclude graphite flags<\/a><a href=\"http:\/\/forums.gentoo.org\/viewtopic-t-944230-start-0.html\"><br \/>\n<\/a><\/li>\n<li><a href=\"http:\/\/realnc.blogspot.ca\/2012\/06\/building-gentoo-linux-with-gcc-47-and.html\">Another guide on enabling LTO<\/a>\u00a0(read the comment on\u00a0<em>-fuse-linker-plugin)<\/em><\/li>\n<li><a href=\"http:\/\/yuguangzhang.com\/blog\/phoronix-gcc-graphite-and-lto-benchmarks\/\">GCC Graphite and LTO benchmarks with the Phoronix Test Suite<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be enabling the GCC options marked with &#8220;To use this code transformation, GCC has to be configured with &#8211;with-ppl and &#8211;with-cloog to enable the Graphite loop transformation infrastructure. &#8220;, referred to as graphite. In addition, Link Time Optimization(LTO) refers to the options -flto and -fuse-linker-plugin. -fuse-linker-plugin tells GCC to use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[5],"tags":[191,104,192,193,145],"class_list":["post-1202","post","type-post","status-publish","format-standard","hentry","category-linux","tag-gcc","tag-gentoo","tag-graphite","tag-lto","tag-performance"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/comments?post=1202"}],"version-history":[{"count":0,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1202\/revisions"}],"wp:attachment":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/media?parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/categories?post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/tags?post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}