toolchains - clang 3.6 and android and modern gcc …...toolchains currently used by aosp aosp (both...
TRANSCRIPT
![Page 1: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/1.jpg)
Presented by
Date
Android and modern toolchains - clang 3.6 and
gcc 5.0, binutils 2.25
Bernhard "Bero" Rosenkränzer
Android Builders Summit 2015
![Page 2: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/2.jpg)
Toolchains currently used by AOSP
● AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly modified versions of○ gcc 4.8 for ARMv7, x86 and x86_64○ gcc 4.9 for ARMv8 and MIPS
● binutils is 2.23.2 for gcc 4.8, 2.24 for gcc 4.9.
![Page 3: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/3.jpg)
binutils 2.25
● Dropping in binutils 2.25 is painless - only change required: Make sure Nexus 10 blobs don’t try to strip files that aren’t target CPU binaries (should be fixed in the first place)
● Interesting addition in 2.25: gold linker for aarch64
![Page 4: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/4.jpg)
Status of CLANG support
After having submitted 117 related patches, AOSP master can be built with clang unmodified.SetUSE_CLANG_PLATFORM_BUILD=true on the make command line to use it.
![Page 5: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/5.jpg)
Status of CLANG support
The supported version of clang comes with AOSP - it is a slightly modified version of a snapshot taken from svn shortly before the 3.6 release.
![Page 6: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/6.jpg)
Status of CLANG support
AOSP 5.1.0 can almost be built with clang - it is missing 2 patches in Bionic, both oneliners.
Backported patches are available.
![Page 7: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/7.jpg)
Status of CLANG support
Clang based builds currently fall back to using gcc to build some subprojects:● perf
○ compile failure, real fix available
![Page 8: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/8.jpg)
Status of CLANG support
Clang based builds currently fall back to using gcc to build some subprojects:● elfutils
○ compile failures due to heavy use of nested functions, fix available, but will never be accepted upstream
![Page 9: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/9.jpg)
Status of CLANG support
Clang based builds currently fall back to using gcc to build some subprojects:● bionic linker
○ reasoning unknown, it can be built with clang
![Page 10: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/10.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● parts of external/chromium_org
○ probably to reduce the potential for surprise -- Chromium uses clang for desktop OS builds too.gcc can build it.
![Page 11: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/11.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● libpng
○ AOSP’s libpng makes use of clang’s -ftrapv option to catch integer overflow errors
![Page 12: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/12.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● llvm, compiler-rt, libcxx-abi, libcxx,
mclinker○ likely because they’re clang sibling
projects
![Page 13: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/13.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● scrypt● srec● libexif● openssl
![Page 14: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/14.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● vixl● gtest● conscrypt
![Page 15: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/15.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● libnativehelper● libc_cxa in Bionic● libnativebridge● net● dalvikvm● LatinIME
![Page 16: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/16.jpg)
Status of CLANG support
Some other subprojects are always built with clang, even in gcc builds:● slang● libbcc● surfaceflinger● renderscript● libcore
![Page 17: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/17.jpg)
Status of gcc 5.0 support
● AOSP master can be built with gcc 5.0 snapshots with a few problems - most annoyingly, an autogenerated file in Chromium isn’t compatible with it
![Page 18: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/18.jpg)
Status of gcc 5.0 support
● The resulting system boots and works for the most part, but the browser is unstable if built with gcc 5
![Page 19: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/19.jpg)
Stability
Both gcc and clang based builds run well and can pass the CTS (Compatibility Test Suite).
gcc 5.0 snapshot builds generally work fine, but have problems with the browser - this causes CTS failures as well.
Tests with gcc 5.0 snapshots were done in late January, the situation may have improved with newer builds.
![Page 20: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/20.jpg)
Code size
● On ARMv7, clang generates slightly larger binaries than gcc.
● On ARMv8, clang generates slightly smaller binaries than gcc.
● In both cases, the difference in code size is negligible (less than 2.5%)
![Page 21: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/21.jpg)
Code size
● The difference in code size between different current gcc versions (4.8 from AOSP, Linaro 4.9, 5.0 snapshot) is even smaller.
![Page 22: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/22.jpg)
Benchmark results
There is no clear winner. In most benchmarks, gcc is slightly ahead, in some, clang wins. gcc is generally better at multithreaded code.
Detailed results are on the Linaro wiki:https://wiki.linaro.org/Platform/Android/GccClangBenchmark-2014-12 https://wiki.linaro.org/Platform/Android/GccClangBenchmark-2015-01
![Page 23: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/23.jpg)
Benchmark results
Since most common benchmarks are not Open Source and have not been updated with proper Aarch64 builds, some benchmark results from 64bit devices are actually in 32bit code -- not measuring the performance of the code generated by the 64bit compilers appropriately.
![Page 24: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/24.jpg)
Benchmark results
Proper 64 bit benchmarks will likely shift the results a little in Clang’s favor - its 64bit code tends to be better than its 32bit code.
Also, tweaking compiler flags for clang builds may help -- people have had much more time to figure out the best flags for gcc than for clang.
![Page 25: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/25.jpg)
Benchmark results
AOSP currently makes some incorrect assumptions about clang -- e.g. “-mcpu=cortex-a15 has to be stripped from compiler flags” -- this used to be true, but clang added A15 support in 3.6. Clang has been updated, but the Makefiles adjusting compiler flags have not.
![Page 26: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/26.jpg)
Getting a second opinion...
... is not just for medicine anymore -- asking another compiler for its opinion on code can turn up interesting bugs.
![Page 27: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/27.jpg)
Interesting bugs found by clang
void something(char n[30]) {
if(!memcmp(buffer, n, sizeof(n))) {
…
}
}
![Page 28: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/28.jpg)
Interesting bugs found by clang
void something(char n[30]) {
if(!memcmp(buffer, n, sizeof(n))) {
…
}
}size of a pointer - not quite 30
![Page 29: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/29.jpg)
Interesting bugs found by clang
unsigned char a[X];for(int i=0; i<X; i++)
b = a ? tagCpe++ : tagSce++;
![Page 30: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/30.jpg)
Interesting bugs found by clang
unsigned char a[X];for(int i=0; i<X; i++)
b = a ? tagCpe++ : tagSce++;
always true -- address of an array. This should have been a[i]
![Page 31: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/31.jpg)
Things to avoid for compatibility
● Variable length arrays in structs● Variable length arrays of non-POD types● Empty structs● Array subscripts of type “char” (value
[‘0’]=0;)● asm(“add w0, w0, #-1”);
(converted to sub w0, w0, #1 by gas, but not by clang)
![Page 32: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/32.jpg)
Things to avoid for compatibility
● Undefined internal functions and variables -- even if they aren’t used:
static void a();
void b() {
if (false)
a();
}
![Page 33: toolchains - clang 3.6 and Android and modern gcc …...Toolchains currently used by AOSP AOSP (both current master branch and the recent 5.1.0 release) defaults to using slightly](https://reader033.vdocuments.mx/reader033/viewer/2022042305/5ed1545befd7b2537304c59d/html5/thumbnails/33.jpg)
Questions? Comments?