3

I vaguely remember from conversations with colleagues that changing compiler flags can break ABI compatibility. I'm not sure if this is always the case, or if it applies to all flags.

Specifically, I'm wondering if optimization flags (e.g. gcc -O2, gcc-O3) or to be more precise, flags to do with SIMD vectorization (e.g. gcc -mavx2, or gcc -msse3) can cause ABI compatibility issues.

I'm writing a vision library that uses a 3rd party closed-source shared library (really, dlls, because we're on windows).

I'm thinking about doing proper aligned memory allocations and enabling vectorization through compiler flags (/ARCH:AVX2 e.g. for MSVC).

I'm wondering if this will break compatibility with the dlls supplied by the 3rd party vendor.

If it may, then there is a lot less to think about, at the design level. If it doesn't then I'll have to think about implementing my own memory allocators etc.

My expectation is that optimization flags only affect code generation and not memory layout, and my understanding is that ABI compatibility has to do with memory layout.

Aidin
  • 31
  • 1
  • 1
    Code librarian Titus Winters would say "compile everything — ABSOLUTELY EVERYTHING — with the same flags, or fear the wrath of the ODR gods." – Eljay Sep 19 '19 at 23:16
  • ABI is not just about memory layout, but for example also about how atomics are implemented, and about how names are mangled. – Kerrek SB Sep 19 '19 at 23:17
  • https://stackoverflow.com/questions/4489012/does-c-have-a-standard-abi and https://stackoverflow.com/questions/2083060/what-could-c-c-lose-if-they-defined-a-standard-abi and https://stackoverflow.com/questions/2801938/gcc-abi-compatibility – Richard Chambers Sep 19 '19 at 23:18
  • Thanks @HansPassant. I do have a C API for the 3rd party library, so I guess I can always fallback on that if things break. I'll move forward with the soft assumption that optimizer (-o3 and the like) doesn't break ABI. – Aidin Sep 20 '19 at 00:04
  • From the gcc flags page: `Certain ABI-changing flags are required to match in all compilation units, and trying to override this at link time with a conflicting value is ignored. ` https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html – Martin York Sep 20 '19 at 00:14
  • I know compilers where some optimizations will compress padding out of structures to optimize for size (rather than speed). Using a TU with such an optimization that passes an object to a TU that does not have the same optimization will mean a member will be written the wrong place in the object. – Martin York Sep 20 '19 at 00:18
  • Thanks a lot @MartinYork. I wish compilers were more transparent when it comes to ABI compatibility. Like, "certain ABI-changing flags".. why isn't there a well defined list of which flags are ABI changing and which aren't. This would certainly be helpful information for build system too. I'm sure for GCC there is probably a way to figure this out but I'm dealing with MSVC. I'll probably have to write some test code and use an ABI compliance checker tool, or just see if things seem to break or not. – Aidin Sep 20 '19 at 18:49
  • @Aidin This is not an issue really. All your code should be built with the same flags. Its all optimized or its all debug.If you change flags you need to recompile everything. MSVC if you change anything it will force a re-compile. – Martin York Sep 20 '19 at 20:36
  • @MartinYork There is a usecase for that. I work in barebone embedded. I wanted to debug. Rolling back the optimization for all files, results in program not fitting on target. So I wanted only my problem child file to be optimized. – aiao Nov 13 '19 at 10:30
  • @aiao You mean you only want the problem child to be debug? I did not say it would not work only that it's not guaranteed to work. You just need to check the compiler documentation to make sure that optimized code is compatible with debug code. Also compilers for embedded systems (in my experience (I have also seen systems with full gcc backends)) are usually simpler, so less likely to have flags that generate incompatible code. – Martin York Nov 13 '19 at 15:28

0 Answers0