[llvm-dev] Generating C headers from LLVM

Cranmer, Joshua via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 15 06:58:35 PDT 2019

> -----Original Message-----
> From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Peter
> Lammich via llvm-dev
> Sent: Friday, April 12, 2019 17:35
> To: Doerfert, Johannes <jdoerfert at anl.gov>; Michael Spencer
> <bigcheesegs at gmail.com>
> Cc: llvm-dev <llvm-dev at lists.llvm.org>
> Subject: Re: [llvm-dev] Generating C headers from LLVM
> Given the LLVM-IR definition (say in file test.ll)
>   {i64,i64,i64} @create_test() { ... }

The LLVM IR ABI is not, in general, the same as the C ABI, since there's generally more trickery that gets involved. There have been suggestions in the past to build an ABI lowering library that's not so tightly integrated to Clang, but nothing has come of that yet. In particular, the ABI you get from passing structs around is completely different: a function returning a struct generally maps each element of the struct into a different register (i.e., returning a struct in LLVM IR is essentially declaring that a function returns multiple values--which C doesn't support), whereas the ABI for most targets is to have the caller allocate a struct on the stack and pass the address as the first argument (generally marked sret).

> I hoped to keep my frontend simple, and that's why it just generates
> functions that return structure types. However, I need to call these
> functions from clang, ideally without clobbering my frontend with
> target-specific calling-convention implementations (Conceptually, those
> things belong to the backend!). Note that it is irrelevant for me
> whether my functions adhere to some specific ABI, as long as I can call
> them from C code compiled with clang.

Returning a struct in LLVM IR is pretty much the one thing you can do to a function to make it completely uncallable from a C code no matter how many compiler-specific annotations you put on the C code.

More information about the llvm-dev mailing list