How to Compile and Run llc-3.4 generated C++ code

2019-07-21 13:16发布

问题:

Note:

Goal of this work is to use some of the c++11 features in non c++11 compiler

Following steps are done,

  1. Generate llvm bit code,

    clang++ -emit-llvm -c test.cc -o test.o
    
  2. Convert llvm bitcode to c++ code,

    llc-3.4 test.o -o test.cpp -march=cpp
    
  3. Getting below error while compiling the llvm generated c++ code using GNU g++

    arunprasadr@geekvm:~/works/myex/llvm$ g++ test.cpp test.cpp:3:23: fatal error: llvm/Pass.h: No such file or directory compilation terminated.

I even tried adding llvm-dev include path, but still fails,

arunprasadr@geekvm:~/works/myex/llvm$ g++ test.cpp -I/usr/include/llvm-3.4 -I /usr/include/llvm-c-3.4
In file included from /usr/include/llvm-3.4/llvm/Support/type_traits.h:20:0,
                 from /usr/include/llvm-3.4/llvm/ADT/StringRef.h:13,
                 from /usr/include/llvm-3.4/llvm/PassRegistry.h:20,
                 from /usr/include/llvm-3.4/llvm/PassSupport.h:26,
                 from /usr/include/llvm-3.4/llvm/Pass.h:366,
                 from test.cpp:3:
/usr/include/llvm-3.4/llvm/Support/DataTypes.h:48:3: error: #error "Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h"
/usr/include/llvm-3.4/llvm/Support/DataTypes.h:52:3: error: #error "Must #define __STDC_CONSTANT_MACROS before " "#including Support/DataTypes.h"
In file included from /usr/include/llvm-3.4/llvm/ADT/SmallVector.h:19:0,
                 from /usr/include/llvm-3.4/llvm/PassAnalysisSupport.h:22,
                 from /usr/include/llvm-3.4/llvm/Pass.h:367,
                 from test.cpp:3:
/usr/include/llvm-3.4/llvm/Support/MathExtras.h: In function ‘bool llvm::isInt(int64_t)’:
/usr/include/llvm-3.4/llvm/Support/MathExtras.h:264:33: error: there are no arguments to ‘INT64_C’ that depend on a template parameter, so a declaration of ‘INT64_C’ must be available [-fpermissive]
/usr/include/llvm-3.4/llvm/Support/MathExtras.h:264:33: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/usr/include/llvm-3.4/llvm/Support/MathExtras.h:264:65: error: there are no arguments to ‘INT64_C’ that depend on a template parameter, so a declaration of ‘INT64_C’ must be available [-fpermissive]
/usr/include/llvm-3.4/llvm/Support/MathExtras.h: In function ‘bool llvm::isUInt(uint64_t)’:
/usr/include/llvm-3.4/llvm/Support/MathExtras.h:290:36: error: there are no arguments to ‘UINT64_C’ that depend on a template parameter, so a declaration of ‘UINT64_C’ must be available [-fpermissive]
/usr/include/llvm-3.4/llvm/Support/MathExtras.h: In function ‘bool llvm::isIntN(unsigned int, int64_t)’:
/usr/include/llvm-3.4/llvm/Support/MathExtras.h:322:33: error: ‘INT64_C’ was not declared in this scope

回答1:

You need to compile against the flags provided by llvm-config. As seen below:

input.cc

#include <iostream>

int main() {
    std::cout << "hello" << std::endl;
}

Command Line

[3:21pm][wlynch@apple /tmp] /opt/llvm/3.4/bin/clang++ -emit-llvm -c input.cc -o input.o
[3:22pm][wlynch@apple /tmp] /opt/llvm/3.4/bin/llc input.o -o llvm.cc -march=cpp
[3:22pm][wlynch@apple /tmp] /opt/llvm/3.4/bin/clang++ -gcc-toolchain /opt/gcc/4.8.2/ llvm.cc `/opt/llvm/3.4/bin/llvm-config --cxxflags --ldflags --libs`

And just to be helpful, here's the output of the llvm-config line on my machine:

-I/opt/llvm/3.4/include -DNDEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3 -fomit-frame-pointer
-std=c++11 -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fPIC
-Woverloaded-virtual -Wcast-qual -L/opt/llvm/3.4/lib -lz -lpthread -ltinfo -lrt
-ldl -lm -lLLVMInstrumentation -lLLVMIRReader -lLLVMAsmParser -lLLVMDebugInfo
-lLLVMOption -lLLVMLTO -lLLVMLinker -lLLVMipo -lLLVMVectorize -lLLVMBitWriter
-lLLVMBitReader -lLLVMTableGen -lLLVMR600CodeGen -lLLVMR600Desc -lLLVMR600Info
-lLLVMR600AsmPrinter -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen
-lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo
-lLLVMSystemZAsmPrinter -lLLVMHexagonCodeGen -lLLVMHexagonAsmPrinter
-lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc
-lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter -lLLVMCppBackendCodeGen
-lLLVMCppBackendInfo -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info
-lLLVMMSP430AsmPrinter -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen
-lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMXCoreAsmPrinter -lLLVMMipsDisassembler
-lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc -lLLVMMipsInfo
-lLLVMMipsAsmPrinter -lLLVMARMDisassembler -lLLVMARMCodeGen -lLLVMARMAsmParser
-lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter -lLLVMAArch64Disassembler
-lLLVMAArch64CodeGen -lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info
-lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMPowerPCCodeGen
-lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo
-lLLVMPowerPCAsmPrinter -lLLVMSparcCodeGen -lLLVMSparcDesc -lLLVMSparcInfo
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG
-lLLVMAsmPrinter -lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils
-lLLVMMCDisassembler -lLLVMMCParser -lLLVMInterpreter -lLLVMMCJIT -lLLVMJIT
-lLLVMCodeGen -lLLVMObjCARCOpts -lLLVMScalarOpts -lLLVMInstCombine
-lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMRuntimeDyld
-lLLVMExecutionEngine -lLLVMTarget -lLLVMMC -lLLVMObject -lLLVMCore
-lLLVMSupport