I am trying to use SSE4.2 intrinsics with clang/llvm but its not compiling, as I get cannot select intrinsic
error from LLVM. On the other hand, the same code compiles flawlessly in gcc. So I thought, maybe I can compile that function with gcc, so as to have an object or library file, and then call that library function in my code, which is compiled by clang/llvm. Would that work?

- 29,191
- 52
- 200
- 356
-
3Probably. It will depend on your system. Mac? Linux? Windows? In general, you can mix object files built with clang and gcc. Make sure that you use the same standard library (don't mix `libc++` and `libstdc++`), though. – Marshall Clow May 03 '13 at 17:01
-
5For plain C, it's pretty much guaranteed; all compilers there use a common _ABI_ (_Application Binary Interface_, i.e. the same mechanisms for execution transfer / calling functions, passing and returning parameters, laying out data structures / aligning variables). For C++, this is not generally true (you can't call Microsoft Visual C++ code from gcc/MinGW and vice versa, for example), but in the gcc / clang case, you're lucky - they use the same C and C++ ABI, and therefore binary code is compatible: http://stackoverflow.com/questions/11682748/is-clang-abi-same-as-g – FrankH. May 06 '13 at 08:46
-
1@FrankH., you CAN call GCC code from MSVC (see my answer). In fact you can call MSVC from GCC (though I don't know why you would want to since GCC is alyways better at optimization) but this is more difficult since MSVC does not provide a way to produce Unix function calling conventions so you have to do it after the object is created and it's more restrictive (e.g. only for functions with four parameters). – Z boson Jan 27 '14 at 10:52
-
1@Zboson I'm well aware of ABIs :) and where the respective (in)compatibilities are. I agree with you that _if you're very careful_ you can create code - thunking layers - between the _C++_ calling conventions used by GCC/MSVC. It's just _not automatic_ / not at all guaranteed. – FrankH. Feb 04 '14 at 10:16
-
@FrankH., yeah, I read some of your answers later and realized you're quite the expert on ABIs. I'm quite new to them. What are thunking layers? – Z boson Feb 04 '14 at 12:53
-
1@Zboson stackoverflow's search to the rescue, re "thunking" :) http://stackoverflow.com/search?q=thunking – FrankH. Feb 05 '14 at 14:32
3 Answers
It's possible to compile an object file with GCC in Linux and convert it to work in Visual Studio. I did this recently running Linux in Virtual Box on Windows converting-c-object-file-from-linux-o-to-windows-obj so this should be possible with Clang on Linux or Windows as well.
So not only can this be done cross compiler it can be done cross platform.
You need to get the calling conventions and the object file format correct (and for C++ the name mangling as well) . With GCC when you compile you can tell it which calling convention/API to use with mabi
. Then, if going from Linux to Windows, you need an object file converter to convert from e.g. ELF on Linux to COFF on Windows. Of course, there are cases this probably won't work (e.g. if the module relies on a system call that is only in one platform). See the link above for more details.
For any more-or-less complicated c++ code, e.g., one that compiles to vtable - the answer is a resounding NO. The two are NOT compatible.
To illustrate the above point, try to compile the Crypto++ library with g++ (gains about 40% speedup for AES/GCM) and then link your clang++-compiled code with it.

- 542
- 6
- 9
-
5Correcting my own answer: yes you CAN. Compile your code with "clang++ -stdlib=libstdc++". Then it will link correctly to g++-compiled library (including Crypto++). Unfortunately, the reverse does not work: g++-compiled code won't work with clang++-compiled library, no matter what flags I tried. :-( – Mouse Jan 28 '14 at 12:39
It may or it may not work. Some elements of the ABI can be expected to be the same. For example, I believe both g++ and clang use the Itanium ABI name mangling scheme. Others elements can not. So it depends on how complex the code you're compiling is.
Also, I would suggest opening an LLVM bug for the intrinsic that could not be selected. Clang and LLVM have a very active community, and it's possible someone will pick the bug up quickly.

- 1
- 1
-
1The question is tagged C, so the entire ABI can be expected to be the same. – Dietrich Epp Jan 24 '14 at 17:25
-
You are right, I misread the question. The part about the bug stands, though. – Marvin Jan 24 '14 at 18:19