Search code examples
clangllvmcross-compilingtargetllvm-ir

how to make target independent IR with llvm


I want to make target independent IR with LLVM.

clang -emit-llvm -S source.c -o source.ll

in source.ll

target datalayout = "e-m:e-i64:..."
target triple = "x86_64-pc-linux-gnu"
...

LLVM IR is said to be Target-independent, but the properties of the target are specified in the actual IR file.

How can I create an LLVM IR without this Target property?


Solution

  • Short answer: you cannot

    Long answer: target-neutrality is the property of input language, not the LLVM IR. While in theory it is possible to make more or less target-neutral LLVM IR for some inputs it is not possible for C/C++ inputs. I will only mention few things that prevents us from having such LLVM IR:

    1. Preprocessor. Target-specific #ifdef clauses obviously make resulting IR target-specific
    2. Pointer sizes. Think about expressions like sizeof(void*). These are target-dependent compile-time constants (yes, there are ways to defer calculation of these constants later on, but this is not something frontends are prepared to deal with, this also hinders many optimizations)
    3. Struct layout. Partly it depends on 2. (think about struct { int foo; void* bar; }
    4. Various ABI-related things like necessary support steps for argument / result passing, etc.
    5. I will not mention target-specific things like vectors, builtins for target-specific instructions sets, etc.