[cfe-dev] Generating strict LLVM IR out of C/C++ headers

Dmitry N. Mikushin maemarcus at gmail.com
Wed Jun 20 09:45:39 PDT 2012


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<GNUInlineAttr>()) {
//    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 {
entry:
  ret i32 0
}

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

define i32 @f3() nounwind inlinehint {
entry:
  ret i32 0
}

- D.

2012/6/11 John McCall <rjmccall at apple.com>

> On Jun 9, 2012, at 2:43 PM, Dmitry N. Mikushin wrote:
>
> 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;
> }
>
> with
>
> clang -cc1 test.cu -emit-llvm -triple ptx64-unknown-unknown
> -fcuda-is-device test.cu -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?
>
>
> 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.
>
> John.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120620/93cdca6f/attachment.html>


More information about the cfe-dev mailing list