[cfe-dev] Clang and packing/unmacking struts uses as arguments/return values

David Chisnall David.Chisnall at cl.cam.ac.uk
Thu Feb 27 03:04:20 PST 2014


On 17 Feb 2014, at 11:09, Renato Golin <renato.golin at linaro.org> wrote:

> On 17 February 2014 11:04, David Chisnall <David.Chisnall at cl.cam.ac.uk> wrote:
>> We've talked in the past about having some ABI builders, factoring out the code in Clang so that you'd have a generic library that could construct LLVM types from C types, construct calls to LLVM functions from C types, and extract IR values that directly correspond to C types after calls.
> 
> 
> I think his needs are slightly different. What we talked about was to
> have a PCSBuilder or to add PCS-aware function lowering in the
> IRBuilder, but in his case, if I got it right, he has to interact with
> already-built IR from his own functions, which happens to be compiled
> to one target and been lowered a bit too much.

As I understand it, his problem is that he has a set of runtime functions compiled with clang and a JIT compiler that needs to interface with them.  I'd imagine the builder having an API something that would be useable for this case like this:

ABIBuilder B(ABI::x86_64);

PCSCStructType CarryOpResultTy = B.CreateStructTy(B.getUInt32Ty(), B.getUInt32Ty(), B.getUInt32Ty());

ABIFunctionType AddFnTy = B.getFunctionType(CarryOpResult, B.getUInt32Ty(), B.getUInt32Ty(), B.getUInt32Ty());
// Here, a, b, and carryOut are all i32.  If they were structure types you'd need a B.PackStruct() or similar call)
ABIReturn AddResult = B.CreateCall(AddFn, AddFnTy, a, b, carryOut);

Value *value    = B.ExtractValue(AddResult, 0);
Value *carryOut = B.ExtractValue(AddResult, 1);
Value *overflow = B.ExtractValue(AddResult, 2);

The resulting IR would be a bit messy, but we already have passes that can clean it up.  Clang already generates some quite nasty IR in these cases and gets it cleaned up correctly.  The author of the JIT, once it used the ABIBuilder for types that needed to interoperate with the platform ABI, would only need to pass a different initialiser value to the ABIBuilder's constructor.

It would be really great to have this functionality in LLVM, as currently every single front-end author has to do it independently if they want to be able to interoperate with C (and, by C, I mean any language that uses the target platform ABI, which is defined in terms of C types).  I'd be very happy to mentor a GSoC student who wanted to implement this...

Of course, in this specific example, using the overflow-checked arithmetic builtins would make more sense than using runtime library functions, but the concept still stands.

David





More information about the cfe-dev mailing list