Generating strict LLVM IR out of C/C++ headers

Dear colleagues,

Consider there is a need to mix sources and headers that are in two different high-level languages. It should be possible, if both languages are lowered down to LLVM IR and merged. But the problem is that clang is unable to generate strict IR code for headers. For example the code

extern attribute((device)) int var;

attribute((always_inline)) void kernel()
var = 2;


clang -cc1 -emit-llvm -triple ptx64-unknown-unknown -fcuda-is-device -o -

reasonably produces empty module: the only function is inline and not used.

So the question: is there an easy way to generate IR with clang strictly for entire header file, with all inlined functions and unused types?
Probably one way would be to write a plugin to add a fictive call of every function and declare fictive variable of every type, but this is stupid. Any better solution?


  • Dima.

IIRC, LLVM’s IR printer never prints types that aren’t actually used. Types aren’t really contained in a module like that.

attribute((“used”)) should force the emission of a function even if it isn’t called. I don’t believe we have a “emit every function definition you see” mode.


Dear colleagues,

Thanks for replies,
I now see that I over-generalized the problem. Strict LLVM IR for C/C++ would mean conserving statics, template instantiations and more. And it seems I’m currently mostly interested only in keeping inline definitions.

In order to have inline functions kept in the resulting LLVM IR, we can externalize them either with attribute((used)), as John suggests, or by commenting out inner “if” in ASTContext.cpp:

if (!getLangOpts().CPlusPlus || FD->hasAttr()) {
// if (FD->isInlineDefinitionExternallyVisible())
return External;

It works:

$ cat test.c
attribute((used)) attribute((always_inline)) inline int f1() { return 0; }
attribute((always_inline)) inline int f2() { return 0; }
inline int f3() { return 0; }

$ ~/sandbox/bin/clang -cc1 -emit-llvm test.c -o -
; ModuleID = ‘test.c’
target datalayout = “e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128”
target triple = “x86_64-unknown-linux-gnu”

@llvm.used = appending global [1 x i8*] [i8* bitcast (i32 ()* @f1 to i8*)], section “llvm.metadata”

define i32 @f1() nounwind inlinehint alwaysinline {
ret i32 0

define i32 @f2() nounwind inlinehint alwaysinline {
ret i32 0

define i32 @f3() nounwind inlinehint {
ret i32 0

  • D.

2012/6/11 John McCall <>