示例—定义一个简单函数
本示例定义了一个函数,用于添加两个双精度浮点数。
"""
This file demonstrates a trivial function "fpadd" returning the sum of
two floating-point numbers.
"""
from llvmlite import ir
# Create some useful types
double = ir.DoubleType()
fnty = ir.FunctionType(double, (double, double))
# Create an empty module...
module = ir.Module(name=__file__)
# and declare a function named "fpadd" inside it
func = ir.Function(module, fnty, name="fpadd")
# Now implement the function
block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
a, b = func.args
result = builder.fadd(a, b, name="res")
builder.ret(result)
# Print the module IR
print(module)
生成的 LLVM 中间表示 会在末尾打印
; 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"
}
要了解如何编译和执行此函数,请参阅 LLVM 绑定层—llvmlite.binding。