文章目录

  • 1.过时的llvmpy
  • 2.llvmlite

1.过时的llvmpy

llvmpy是llvm C ++库的Python包装器,允许简单访问编译器工具。
但是这个库已经不再更新了,只支持LLVM 3.3,不支持更新的版本。
有一篇好文章可以阅读:Let’s Write an LLVM Specializer for Python! (Stephen Diehl)

2.llvmlite

项目的GitHub地址:numba/llvmlite: A lightweight LLVM python binding for writing JIT compilers
对应版本如下:

llvmlite versions compatible LLVM versions
0.29.0 - … 7.0.x, 7.1.x, 8.0.x
0.27.0 - 0.28.0 7.0.x
0.23.0 - 0.26.0 6.0.x
0.21.0 - 0.22.0 5.0.x
0.17.0 - 0.20.0 4.0.x
0.16.0 - 0.17.0 3.9.x
0.13.0 - 0.15.0 3.8.x
0.9.0 - 0.12.1 3.7.x
0.6.0 - 0.8.0 3.6.x
0.1.0 - 0.5.1 3.5.x

我使用的LLVM版本是3.9,所以我下载0.16.0版本的llvmlite :

apt install libedit-dev
alias llvm-config="llvm-config-3.9"
export LLVM_CONFIG="/usr/bin/llvm-config-3.9"
pip3 install llvmlite==0.16.0
python3 -m llvmlite.tests

现在就可以使用了,示例代码如下:

from __future__ import print_functionfrom ctypes import CFUNCTYPE, c_doubleimport llvmlite.binding as llvm# All these initializations are required for code generation!
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()  # yes, even this onellvm_ir = """; ModuleID = "examples/ir_fpadd.py"target triple = "unknown-unknown-unknown"target datalayout = ""define double @"fpadd"(double %".1", double %".2"){entry:%"res" = fadd double %".1", %".2"ret double %"res"}"""def create_execution_engine():"""Create an ExecutionEngine suitable for JIT code generation onthe host CPU.  The engine is reusable for an arbitrary number ofmodules."""# Create a target machine representing the hosttarget = llvm.Target.from_default_triple()target_machine = target.create_target_machine()# And an execution engine with an empty backing modulebacking_mod = llvm.parse_assembly("")engine = llvm.create_mcjit_compiler(backing_mod, target_machine)return enginedef compile_ir(engine, llvm_ir):"""Compile the LLVM IR string with the given engine.The compiled module object is returned."""# Create a LLVM module object from the IRmod = llvm.parse_assembly(llvm_ir)mod.verify()# Now add the module and make sure it is ready for executionengine.add_module(mod)engine.finalize_object()return modengine = create_execution_engine()
mod = compile_ir(engine, llvm_ir)# Look up the function pointer (a Python int)
func_ptr = engine.get_function_address("fpadd")# Run the function via ctypes
cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
res = cfunc(1.0, 3.5)
print("fpadd(...) =", res)

在Python中使用LLVM接口:llvmpy和llvmlite相关推荐

  1. linux上创建loopback接口,在python中的特定接口的linux loopback接口

    我试图发送一些数据到本地地址,然后使用特定的接口ppp0'转发'到外部地址.我认为我的问题与分组路由/套接字绑定问题有关,但我太多了解它的新手.在python中的特定接口的linux loopback ...

  2. Python中抽象类和接口的区别

    Python中的抽象类和接口有什么区别? #1楼 用更基本的方式来解释:界面有点像空的松饼盘. 它是一个包含一组没有代码的方法定义的类文件. 抽象类是相同的,但并非所有函数都需要为空. 有些人可以有代 ...

  3. Python中如何编写接口,以及如何请求外部接口

    您好,我是码农飞哥,感谢您阅读本文!本文主要介绍Python中如何请求外部接口以及如何通过Flask框架定义接口. 文章目录 引入requests库 Get请求 Post请求 以form表单提交的方式 ...

  4. python股票接口_在Python中使用股票接口

    之前已经分享了一些货币基金的接口,而近期分享1个股票的接口. 关于股票的接口主要分为2种类型: 实时的 历史的 下面我们分别来说明. 实时的 对于实时的股票,我们可以使用 http://hq.sina ...

  5. 如何在Python中调用RNAfold接口,即RNA扩展包

    最近在做RNA蛋白质位点结合方向的研究,复现大佬代码的过程中,发现其用到了RNAfold的python接口包,其中主要用到了根据RNA序列生成二级结构的功能. RNAfold官网 链接: https: ...

  6. stat在python中_stat模块接口

    Python的stat模块定义了一组解释函数,这些函数专门用来解释 os.stat(), os.lstat(), os.fstat() 这三个函数返回的模式信息(st_mode)(这三个stat函数的 ...

  7. linux 增加环回接口,linux环回接口到python中的特定接口

    我试图将一些数据发送到本地地址,然后使用特定接口ppp0将其"转发"到外部地址.我认为我的问题与包路由/套接字绑定问题有关,但我太过新手,无法理解它.在 我的网络设置:Kernel ...

  8. 在C++中使用LLVM的JIT功能进行代码优化:Optimization passes--PassManagerBuilder

    今天找了一天的在LLVM中如何进行代码优化的方法. 一开始在谷歌搜索LLVM JIT,看到了LLVM的官方教程: 1. Building a JIT: Starting out with Kaleid ...

  9. python中append函数合并列表且列表内数字从高到低_35个高级Python知识点总结

    No.1 一切皆对象 众所周知,Java中强调"一切皆对象",但是Python中的面向对象比Java更加彻底,因为Python中的类(class)也是对象,函数(function) ...

最新文章

  1. Linux面试最常见的5个基本问题
  2. 技术系列课|“主动降噪”到底有多厉害?
  3. Spring基础篇——Spring容器和应用上下文理解
  4. Web前端面试题集锦
  5. 详解string容器(应用+模拟实现,string练习题)
  6. 【蓝桥杯官网试题 - 历届试题】小朋友排队(逆序数,树状数组)
  7. Flutter搜索框SearchBar
  8. RTT学习笔记2-线程
  9. 在matlab中安装命令窗口,安装完后发现命令窗口有这个?怎么回事?
  10. MongoDB教程——第3天(性能——索引)
  11. 为什么都开始流行将洗手台装在厕所外?
  12. PhoneGap在Android上的插件开发方法介绍
  13. Raphael的set使用
  14. read()/write()的生命旅程之二——第二章:read()
  15. 1到20的阶乘和是多少 php,20的阶乘(1到20的阶乘和结果)
  16. 当区块链遇上财政电子票据
  17. C语言的printf输出格式
  18. Oracle 函数编写
  19. checksum命令 linux_Linux命令大全完整版
  20. 弘辽科技:如何做好淘宝店铺推广?有什么技巧吗?

热门文章

  1. R语言基础练习与入门实践
  2. Ubuntu16下编译安装Open Babel2.4.1和python绑定
  3. APK 签名中应该注意的一些点 (未完待续)
  4. sangerbox平台使用(四)气泡图的绘制
  5. Linux 中多终端同步 history 记录
  6. MPB:南农成艳芬组-瘤胃微生物体外发酵过程与注意事项
  7. QIIME 2教程. 01简介和安装 Introduction Install(2020.11开始更新)
  8. 西湖大学鞠峰:环境微生物宏基因组学(报告视频+PPT,11月23日)
  9. R语言绘制带聚类树的堆叠柱形图
  10. 植保口的面上项目共153项,系统总结