llvm-hs / llvm-hs-examples

Examples for Haskell bindings to LLVM
MIT License
70 stars 20 forks source link

Problems running examples in NixOS #13

Open hhefesto opened 4 years ago

hhefesto commented 4 years ago

First of all: greetings, and thank you for such magnifical software.

I am having trouble running some of the examples. I am currently using NixOS (channel release 20.3 stable).

I can run the first example without a problem (i.e. cabal run basic) but not the rest.

To reproduce this problem one should use the shell.nix from the llvm-hs repo (llvm-9 branch) and run:

╭─hhefesto@Olimpo ~/src/llvm-hs ‹llvm-9› 
╰─$ nix-shell shell.nix           
╭─hhefesto@Olimpo ~/src/llvm-hs ‹llvm-9› 
╰─$ cd ../llvm-hs-examples                                                                                                                                                                                            ghc-shell-for-packages
╭─hhefesto@Olimpo ~/src/llvm-hs-examples ‹master› 
╰─$ cabal run basic                                                                                                                                                                                                   ghc-shell-for-packages
Up to date
; ModuleID = 'basic'
source_filename = "<string>"

define i32 @add(i32 %a, i32 %b) {
entry:
  %result = add i32 %a, %b
  ret i32 %result
}

╭─hhefesto@Olimpo ~/src/llvm-hs-examples ‹master› 
╰─$ cabal run orc                                                                                                                                                                                                     ghc-shell-for-packages
Up to date
; ModuleID = 'basic'
source_filename = "<string>"

define i32 @add() {
entry:
  ret i32 42
}

JITSymbolError ""
Eager JIT Result:
()
╭─hhefesto@Olimpo ~/src/llvm-hs-examples ‹master› 
╰─$ cabal run arith                                                                                                                                                                                                   ghc-shell-for-packages
Up to date
*** Expression ***

\x -> sin(3.141592653589793 * x / 2.0) * (1.0 + sqrt(x)) * (1.0 + sqrt(x))

*** LLVM IR ***

; ModuleID = 'arith.ll'
source_filename = "<string>"

declare double @llvm.sin.f64(double)

declare double @llvm.sqrt.f64(double)

define double @f(double %x_0) {
  %x_1 = fmul double 0x400921FB54442D18, %x_0
  %x_2 = fdiv double %x_1, 2.000000e+00
  %x_3 = call double @llvm.sin.f64(double %x_2)
  %x_4 = call double @llvm.sqrt.f64(double %x_0)
  %x_5 = fadd double 1.000000e+00, %x_4
  %x_6 = call double @llvm.sqrt.f64(double %x_0)
  %x_7 = fadd double 1.000000e+00, %x_6
  %x_8 = fmul double %x_5, %x_7
  %x_9 = fmul double %x_3, %x_8
  ret double %x_9
}

arith: zdllvmzmhszm9zi0zi1zm96887f153c504d115c8590f781d2d454ad5394458a40a98d753cc870a382f8c1zdLLVMziInternalziFFIziOrcJITzdllvmzzmhszzm9zzi0zzi1zzm96887f153c504d115c8590f781d2d454ad5394458a40a98d753cc870a382f8c1zuLLVMzziInternalzziFFIzziOrcJITzuwrapGetSymbolResolver: uncaught exception
╭─hhefesto@Olimpo ~/src/llvm-hs-examples ‹master› 
╰─$ cabal run irbuilder                                                                                                                                                                                               ghc-shell-for-packages
Build profile: -w ghc-8.8.1 -O1
In order, the following will be built (use -v for more details):
 - examples-1.0.0.0 (exe:irbuilder) (first run)
Configuring executable 'irbuilder' for examples-1.0.0.0..
Preprocessing executable 'irbuilder' for examples-1.0.0.0..
Building executable 'irbuilder' for examples-1.0.0.0..
[1 of 1] Compiling Main             ( irbuilder/Main.hs, /home/hhefesto/src/llvm-hs-examples/dist-newstyle/build/x86_64-linux/ghc-8.8.1/examples-1.0.0.0/x/irbuilder/build/irbuilder/irbuilder-tmp/Main.o )
Linking /home/hhefesto/src/llvm-hs-examples/dist-newstyle/build/x86_64-linux/ghc-8.8.1/examples-1.0.0.0/x/irbuilder/build/irbuilder/irbuilder ...
Module {moduleName = "exampleModule", moduleSourceFileName = "<string>", moduleDataLayout = Nothing, moduleTargetTriple = Nothing, moduleDefinitions = [GlobalDefinition (Function {linkage = External, visibility = Default, dllStorageClass = Nothing, callingConvention = C, returnAttributes = [], returnType = IntegerType {typeBits = 32}, name = Name "f", parameters = ([Parameter (IntegerType {typeBits = 32}) (Name "a_0") []],False), functionAttributes = [], section = Nothing, comdat = Nothing, alignment = 0, garbageCollectorName = Nothing, prefix = Nothing, basicBlocks = [BasicBlock (Name "entry_0") [UnName 0 := ICmp {iPredicate = EQ, operand0 = LocalReference (IntegerType {typeBits = 32}) (Name "a_0"), operand1 = ConstantOperand (Int {integerBits = 32, integerValue = 0}), metadata = []}] (Do (CondBr {condition = LocalReference (IntegerType {typeBits = 1}) (UnName 0), trueDest = UnName 1, falseDest = Name "if.else_0", metadata' = []})),BasicBlock (UnName 1) [UnName 2 := Add {nsw = False, nuw = False, operand0 = LocalReference (IntegerType {typeBits = 32}) (Name "a_0"), operand1 = ConstantOperand (Int {integerBits = 32, integerValue = 0}), metadata = []}] (Do (Br {dest = Name "if.exit_0", metadata' = []})),BasicBlock (Name "if.else_0") [UnName 3 := Add {nsw = False, nuw = False, operand0 = LocalReference (IntegerType {typeBits = 32}) (Name "a_0"), operand1 = ConstantOperand (Int {integerBits = 32, integerValue = 0}), metadata = []}] (Do (Br {dest = Name "if.exit_0", metadata' = []})),BasicBlock (Name "if.exit_0") [UnName 4 := Phi {type' = IntegerType {typeBits = 32}, incomingValues = [(LocalReference (IntegerType {typeBits = 32}) (UnName 2),UnName 1),(LocalReference (IntegerType {typeBits = 32}) (UnName 3),Name "if.else_0")], metadata = []}] (Do (Ret {returnOperand = Just (LocalReference (IntegerType {typeBits = 32}) (UnName 4)), metadata' = []}))], personalityFunction = Nothing, metadata = []}),GlobalDefinition (Function {linkage = External, visibility = Default, dllStorageClass = Nothing, callingConvention = C, returnAttributes = [], returnType = IntegerType {typeBits = 32}, name = Name "plus", parameters = ([Parameter (IntegerType {typeBits = 32}) (Name "x_0") [],Parameter (IntegerType {typeBits = 32}) (Name "y_0") []],False), functionAttributes = [], section = Nothing, comdat = Nothing, alignment = 0, garbageCollectorName = Nothing, prefix = Nothing, basicBlocks = [BasicBlock (Name "entry2_0") [UnName 0 := Add {nsw = False, nuw = False, operand0 = LocalReference (IntegerType {typeBits = 32}) (Name "x_0"), operand1 = LocalReference (IntegerType {typeBits = 32}) (Name "y_0"), metadata = []}] (Do (Ret {returnOperand = Just (LocalReference (IntegerType {typeBits = 32}) (UnName 0)), metadata' = []}))], personalityFunction = Nothing, metadata = []})]}

Some maybe usefull extra information is that I'm using the master branch from this repo (llvm-hs-examples).

Cheers, Daniel Herrera

levans commented 3 years ago

@hhefesto Did you ever resolve the issue with the JIT not resolving symbols on nixos in the llvm-hs JIT example?

I have an old llvm project that I recently moved onto my relatively new nixos box and I've been trying to bring the code up to llvm 9. In the process, I referenced the llvm-hs example for JIT, but as you point out I get the same effect when trying to resolve an external symbol (in fact, the entry point): JITSymbolError ""

I was pulling my hair out thinking that I'd missed some small detail, until I saw your issue report here. I would be very grateful if you have the solution to getting this example running and/or an explanation of why it wasn't working on nix(os)!

As I'm on nixos, I did add a nix: packages: stanza to my stack.yaml, but only for [ llvm_9 ]. That is clearly sufficient to get the project building OK, but evidently not resolving symbols, per the code in the example anyway.

Thanks.

hhefesto commented 3 years ago

Sadly, still unable to run the examples. I did try to get help on stackoverflow (https://stackoverflow.com/questions/65671417/trouble-running-orc-jit-llvm-hs-examples) and detailed there my approaches a bit more.

If you get the examples running, I would also be very grateful to see your solution.

levans commented 3 years ago

OK, thanks Daniel. I'll continue experimenting as time permits. Not sure if there's any helpful diagnostics that can help me direct efforts, so probably a bit of a random walk. Anyway, I'll report here if I make any progress.