Sunday, 15 May 2011

Segfault in Haskell LLVM-General code generation -



Segfault in Haskell LLVM-General code generation -

i'm trying follow along llvm bindings tutorial here, , running segfault. next code works in sense prints module header output.ll, segfaults somewhere.

module main import control.monad.error import llvm.general.module import llvm.general.context import qualified llvm.general.ast ast --create , write out empty llvm module main :: io () main = writemodule (ast.defaultmodule { ast.modulename = "mymodule" }) outputfile :: file outputfile = file "output.ll" writemodule :: ast.module -> io () writemodule mod = withcontext $ (\context -> lifterror $ withmodulefromast context mod (\m -> lifterror $ writellvmassemblytofile outputfile m)) --perform action, or fail on error lifterror :: errort string io -> io lifterror = runerrort >=> either fail homecoming

i suspect related next hint linked tutorial:

it of import remember not pass or effort utilize resources outside of bracket lead undefined behavior and/or segfaults.

i think in context "bracket" implemented withcontext function, makes seem should handled.

if alter definition of writemodule to

writemodule mod = assembly <- (withcontext $ (\context -> lifterror $ withmodulefromast context mod modulellvmassembly)) putstrln assembly

that is, instead of writing file print out string representation of llvm assembly, no segfault thrown.

does have experience these bindings? i'm interested know failure cases warning quoted. is, how 1 "forget" not utilize resources outside bracket? of functions seem require context, well, require one. isn't kind of resource scoping issue haskell @ handling you?

version information:

llvm-general-3.4.3.0 llvm version 3.4 default target: x86_64-apple-darwin13.2.0

it help if shared llvm , cabal environment, llvm notorious beingness backwards incompatible there might issue using latest versions of bindings.

behind scenes writellvmassemblytofile using c++ phone call file io operation , speculate it's holding reference llvm module result of finalizing file resource.

try rendering module string using modulestring , lifting io monad phone call writefile haskell instead of going through c++ write.

import llvm.general.context import llvm.general.module mod import qualified llvm.general.ast ast import control.monad.error main :: io () main = writemodule (ast.defaultmodule { ast.modulename = "mymodule" }) homecoming () writemodule :: ast.module -> io (either string ()) writemodule ast = withcontext $ \ctx -> runerrort $ withmodulefromast ctx ast $ \m -> asm <- modulestring m liftio $ writefile "output.ll" asm

the bindings can still rather brittle in experience, should inquire on issue tracker if problem persists.

edit: workaround old version has been subsequently fixed. see: https://github.com/bscarlet/llvm-general/issues/109

haskell llvm

No comments:

Post a Comment