Skip to main content

revmc_llvm/
cpp.rs

1//! Custom bindings to LLVM.
2
3use inkwell::{
4    attributes::Attribute,
5    context::Context,
6    llvm_sys::{
7        error::LLVMErrorRef,
8        orc2::{
9            LLVMOrcExecutionSessionRef, LLVMOrcExecutorAddress, LLVMOrcJITDylibRef,
10            lljit::{LLVMOrcLLJITBuilderRef, LLVMOrcLLJITRef},
11        },
12        prelude::{LLVMAttributeRef, LLVMContextRef, LLVMValueRef},
13        target_machine::{LLVMCodeGenOptLevel, LLVMTargetMachineRef},
14    },
15    values::AsValueRef,
16};
17use std::{ffi::c_char, sync::atomic::AtomicUsize};
18
19#[link(name = "revmc_llvm_cpp", kind = "static")]
20unsafe extern "C" {
21    fn revmc_llvm_create_initializes_attr(
22        ctx: LLVMContextRef,
23        lower: i64,
24        upper: i64,
25    ) -> LLVMAttributeRef;
26
27    pub(crate) fn revmc_llvm_jit_dylib_add_to_link_order(
28        jd: LLVMOrcJITDylibRef,
29        other: LLVMOrcJITDylibRef,
30    );
31
32    pub(crate) fn revmc_llvm_lljit_builder_set_concurrent_compiler(builder: LLVMOrcLLJITBuilderRef);
33
34    pub(crate) fn revmc_llvm_execution_session_remove_jit_dylib(
35        es: LLVMOrcExecutionSessionRef,
36        jd: LLVMOrcJITDylibRef,
37    ) -> LLVMErrorRef;
38
39    pub(crate) fn revmc_llvm_lljit_lookup_in(
40        jit: LLVMOrcLLJITRef,
41        jd: LLVMOrcJITDylibRef,
42        result: *mut LLVMOrcExecutorAddress,
43        name: *const c_char,
44    ) -> LLVMErrorRef;
45
46    pub(crate) fn revmc_llvm_lljit_enable_debug_support(jit: LLVMOrcLLJITRef) -> LLVMErrorRef;
47
48    pub(crate) fn revmc_llvm_lljit_enable_perf_support(jit: LLVMOrcLLJITRef) -> LLVMErrorRef;
49
50    pub(crate) fn revmc_llvm_lljit_enable_simple_perf(jit: LLVMOrcLLJITRef) -> LLVMErrorRef;
51
52    pub(crate) fn revmc_llvm_lljit_enable_memory_usage(
53        jit: LLVMOrcLLJITRef,
54        code_bytes: *const AtomicUsize,
55        data_bytes: *const AtomicUsize,
56    ) -> LLVMErrorRef;
57
58    pub(crate) fn revmc_llvm_target_machine_set_opt_level(
59        tm: LLVMTargetMachineRef,
60        level: LLVMCodeGenOptLevel,
61    );
62
63    fn revmc_llvm_set_dso_local(val: LLVMValueRef, local: bool);
64}
65
66pub(crate) fn set_dso_local(f: inkwell::values::FunctionValue<'_>) {
67    unsafe { revmc_llvm_set_dso_local(f.as_value_ref(), true) };
68}
69
70pub(crate) fn create_initializes_attr(cx: &Context, lower: i64, upper: i64) -> Attribute {
71    unsafe {
72        let raw = revmc_llvm_create_initializes_attr(cx.raw(), lower, upper);
73        Attribute::new(raw)
74    }
75}