将Haskell程序编译为LLVM IR [英] Compile Haskell programs to LLVM IR
问题描述
根据将此SO帖子的Haskell程序编译为C不再(正式)支持.因此,我想探讨将Haskell程序编译为LLVM IR的选项.我选择了与上述帖子相同的程序:
According to this SO post compilation of Haskell programs to C is no longer (officially) supported. So I wanted to explore the option of compiling Haskell programs to LLVM IR. I chose the same program of the mentioned post:
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
main = print(quicksort([5,2,1,0,8,3]))
,然后尝试使用以下命令将其编译为LLVM IR:
and then tried to compile it to LLVM IR with:
$ ghc -fllvm main.hs
然后我收到有关LLVM版本的此错误:
Then I get this error regarding the LLVM version:
<no location info>: error:
Warning: Couldn't figure out LLVM version!
Make sure you have installed LLVM 3.7
ghc: could not execute: opt-3.7
当我检查我的opt版本时,它是3.8.0,该版本更大:
When I check my opt version it's 3.8.0, which is bigger:
$ opt --version
LLVM (http://llvm.org/):
LLVM version 3.8.0
DEBUG build with assertions.
Built Jun 20 2018 (14:59:34).
Default target: x86_64-unknown-linux-gnu
Host CPU: broadwell
那是怎么回事? ghc是否可以确切预期3.7.0版,仅此而已!!
编辑:
在安装llvm 3.7.0并复制opt
和llc
以具有3.7后缀后:
After installing llvm 3.7.0 and copying opt
and llc
to have 3.7 suffixes:
$ cp opt opt-3.7
$ cp llc llc-3.7
使用以下行,
编译到llvm不会出错:
compilation to llvm goes without errors, using this line:
$ ghc -keep-llvm-files main.hs
并创建一个名为main.ll的文件.
and a file called main.ll is created.
推荐答案
是的,GHC需要LLVM的确切版本. LLVM的内部结构变化非常快,因此GHC(就像许多其他针对或使用LLVM的工具一样)采用了非常保守的方法来对这些工具进行版本控制.
Yes, GHC expects an exact version of LLVM. The LLVM internals change very quickly, and so GHC (like many other tools which target or use LLVM) takes a very conservative approach to versioning of those tools.
这篇关于将Haskell程序编译为LLVM IR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!