gpt4 book ai didi

ocaml - LLVM OCaml 绑定(bind)是否包含内在支持?

转载 作者:行者123 更新时间:2023-12-04 17:12:45 25 4
gpt4 key购买 nike

除了 is_intrinsic 之外,我似乎无法在官方 LLVM OCaml 绑定(bind)中找到对内在函数的引用。功能。

我正在构建一个后端,它需要执行一些特定于目标的代码生成(用于 SSE、AVX 和 NEON),而内在函数是 C++ API 中的标准路径。

最佳答案

OCaml 绑定(bind)以与 C 语言绑定(bind)完全相同的方式支持内在函数:

接口(interface)中没有对它们的特殊支持(就像在完整的 C++ 接口(interface)中一样),但它们可以声明为 extern 并像任何其他函数一样调用它们。

例如。:

open Llvm

let () =
let c = create_context () in

let f32_t = float_type c in
let f32x4_t = vector_type f32_t 4 in

let m = create_module c "test" in

(* declare void @printv(<4 x float>)
* nonce extern which forces preservation of vector results *)
let printv =
declare_function "printv"
(function_type (void_type c) [|f32x4_t|]) m in

(* declare <4 x float> @llvm.x86.sse.sqrt.ps(<4 x float>) nounwind readnone *)
let sqrtps =
declare_function "llvm.x86.sse.sqrt.ps"
(function_type f32x4_t [|f32x4_t|]) m in

(* define i32 @main() { entry: *)
let main = define_function "main" (function_type i32_t [| |]) m in
let at_entry = builder_at_end c (entry_block main) in

(*%sqrtps = call <4 x float> @llvm.x86.sse.sqrt.ps(<4 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00, float 4.000000e+00>)*)
let cv1234 = const_vector [| const_float f32_t 1.0; const_float f32_t 2.0;
const_float f32_t 3.0; const_float f32_t 4.0 |] in
let sqrt = build_call sqrtps [| cv1234 |] "sqrtps" at_entry in

(* call void printv(sqrtps) *)
ignore (build_call printv [| sqrt |] "" at_entry);

(* ret void *)
ignore (build_ret (const_null i32_t) at_entry);

(* Print .ll to stderr *)
dump_module m

产生:
; ModuleID = 'test'

declare void @printv(<4 x float>)

declare <4 x float> @llvm.x86.sse.sqrt.ps(<4 x float>) nounwind readnone

define i32 @main() {
entry:
%sqrtps = call <4 x float> @llvm.x86.sse.sqrt.ps(<4 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00, float 4.000000e+00>)
call void @printv(<4 x float> %sqrtps)
ret i32 0
}

它编译成 x86 正确调用 sqrtpsxmm寄存器。

关于ocaml - LLVM OCaml 绑定(bind)是否包含内在支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5997881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com