-
Notifications
You must be signed in to change notification settings - Fork 38
Common Mistakes
String globals are values of type [N x i8]*
. What you usually want is a value of type i8*
. Use a constant GetElementPtr
with indexes [C.Int 32 0, C.Int 32 0]
to get the pointer to the first i8.
LLVM asserts the correctness of IR that is fed into it. If you've made an error, this will often lead to your program aborting with an unhelpful message during code generation. To get more information, install a debug build of LLVM and run your program inside a debugger. Now when an assert happens, you'll get a detailed backtrace. Most LLVM objects have a void dump()
method which you can call from inside the debugger to inspect them in a convenient human-readable form. Use this to work out just what and where your IR was incorrect.
If your generated code is calling traps when you don't expect it to, it's probably because LLVM often compiles undefined behavior as a trap. Many things are undefined behavior (see What Every C Programmer Should Know About Undefined Behavior and, more specifically, the LLVM Language Reference), but one particularly common cause is the calling convention of your call instructions not matching the one declared for your functions. Some basic errors such as this will be caught by running your IR through opt -lint
. Others won't.
If you're using the Haskell Platform and have a standalone MinGW installed, it's important to note that the GHC uses gcc and libstdc++ from the Platform's MinGW, which are not likely to be binary-compatible with the libstdc++ your LLVM is built against. To make GHC use the standalone MinGW's gcc and libstdc++, pass --ghc-options=-pgml=gcc
to your cabal commands (or pass -pgml=gcc
to ghc directly if you're not using cabal).