|
| 1 | +/*===-- passbuilder_ocaml.c - LLVM OCaml Glue -------------------*- C++ -*-===*\ |
| 2 | +|* *| |
| 3 | +|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| |
| 4 | +|* Exceptions. *| |
| 5 | +|* See https://llvm.org/LICENSE.txt for license information. *| |
| 6 | +|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| |
| 7 | +|* *| |
| 8 | +|*===----------------------------------------------------------------------===*| |
| 9 | +|* *| |
| 10 | +|* This file glues LLVM's OCaml interface to its C interface. These functions *| |
| 11 | +|* are by and large transparent wrappers to the corresponding C functions. *| |
| 12 | +|* *| |
| 13 | +|* Note that these functions intentionally take liberties with the CAMLparamX *| |
| 14 | +|* macros, since most of the parameters are not GC heap objects. *| |
| 15 | +|* *| |
| 16 | +\*===----------------------------------------------------------------------===*/ |
| 17 | + |
| 18 | +#include "llvm_ocaml.h" |
| 19 | +#include "target_ocaml.h" |
| 20 | +#include "llvm-c/Error.h" |
| 21 | +#include "llvm-c/Transforms/PassBuilder.h" |
| 22 | +#include <caml/memory.h> |
| 23 | + |
| 24 | +#define PassBuilderOptions_val(v) ((LLVMPassBuilderOptionsRef)from_val(v)) |
| 25 | + |
| 26 | +value llvm_run_passes(value M, value Passes, value TM, value Options) { |
| 27 | + LLVMErrorRef Err = |
| 28 | + LLVMRunPasses(Module_val(M), String_val(Passes), TargetMachine_val(TM), |
| 29 | + PassBuilderOptions_val(Options)); |
| 30 | + if (Err == LLVMErrorSuccess) { |
| 31 | + value result = caml_alloc(1, 0); |
| 32 | + Store_field(result, 0, Val_unit); |
| 33 | + return result; |
| 34 | + } else { |
| 35 | + char *str = LLVMGetErrorMessage(Err); |
| 36 | + value v = caml_copy_string(str); |
| 37 | + LLVMDisposeErrorMessage(str); |
| 38 | + value result = caml_alloc(1, 1); |
| 39 | + Store_field(result, 0, v); |
| 40 | + return result; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +value llvm_create_passbuilder_options(value Unit) { |
| 45 | + LLVMPassBuilderOptionsRef PBO = LLVMCreatePassBuilderOptions(); |
| 46 | + return to_val(PBO); |
| 47 | +} |
| 48 | + |
| 49 | +value llvm_passbuilder_options_set_verify_each(value PBO, value VerifyEach) { |
| 50 | + LLVMPassBuilderOptionsSetVerifyEach(PassBuilderOptions_val(PBO), |
| 51 | + Bool_val(VerifyEach)); |
| 52 | + return Val_unit; |
| 53 | +} |
| 54 | + |
| 55 | +value llvm_passbuilder_options_set_debug_logging(value PBO, |
| 56 | + value DebugLogging) { |
| 57 | + LLVMPassBuilderOptionsSetDebugLogging(PassBuilderOptions_val(PBO), |
| 58 | + Bool_val(DebugLogging)); |
| 59 | + return Val_unit; |
| 60 | +} |
| 61 | + |
| 62 | +value llvm_passbuilder_options_set_loop_interleaving(value PBO, |
| 63 | + value LoopInterleaving) { |
| 64 | + LLVMPassBuilderOptionsSetLoopInterleaving(PassBuilderOptions_val(PBO), |
| 65 | + Bool_val(LoopInterleaving)); |
| 66 | + return Val_unit; |
| 67 | +} |
| 68 | + |
| 69 | +value llvm_passbuilder_options_set_loop_vectorization(value PBO, |
| 70 | + value LoopVectorization) { |
| 71 | + LLVMPassBuilderOptionsSetLoopVectorization(PassBuilderOptions_val(PBO), |
| 72 | + Bool_val(LoopVectorization)); |
| 73 | + return Val_unit; |
| 74 | +} |
| 75 | + |
| 76 | +value llvm_passbuilder_options_set_slp_vectorization(value PBO, |
| 77 | + value SLPVectorization) { |
| 78 | + LLVMPassBuilderOptionsSetSLPVectorization(PassBuilderOptions_val(PBO), |
| 79 | + Bool_val(SLPVectorization)); |
| 80 | + return Val_unit; |
| 81 | +} |
| 82 | + |
| 83 | +value llvm_passbuilder_options_set_loop_unrolling(value PBO, |
| 84 | + value LoopUnrolling) { |
| 85 | + LLVMPassBuilderOptionsSetLoopUnrolling(PassBuilderOptions_val(PBO), |
| 86 | + Bool_val(LoopUnrolling)); |
| 87 | + return Val_unit; |
| 88 | +} |
| 89 | + |
| 90 | +value llvm_passbuilder_options_set_forget_all_scev_in_loop_unroll( |
| 91 | + value PBO, value ForgetAllSCEVInLoopUnroll) { |
| 92 | + LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll( |
| 93 | + PassBuilderOptions_val(PBO), Bool_val(ForgetAllSCEVInLoopUnroll)); |
| 94 | + return Val_unit; |
| 95 | +} |
| 96 | + |
| 97 | +value llvm_passbuilder_options_set_licm_mssa_opt_cap(value PBO, |
| 98 | + value LicmMssaOptCap) { |
| 99 | + LLVMPassBuilderOptionsSetLicmMssaOptCap(PassBuilderOptions_val(PBO), |
| 100 | + Int_val(LicmMssaOptCap)); |
| 101 | + return Val_unit; |
| 102 | +} |
| 103 | + |
| 104 | +value llvm_passbuilder_options_set_licm_mssa_no_acc_for_promotion_cap( |
| 105 | + value PBO, value LicmMssaNoAccForPromotionCap) { |
| 106 | + LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap( |
| 107 | + PassBuilderOptions_val(PBO), Int_val(LicmMssaNoAccForPromotionCap)); |
| 108 | + return Val_unit; |
| 109 | +} |
| 110 | + |
| 111 | +value llvm_passbuilder_options_set_call_graph_profile(value PBO, |
| 112 | + value CallGraphProfile) { |
| 113 | + LLVMPassBuilderOptionsSetCallGraphProfile(PassBuilderOptions_val(PBO), |
| 114 | + Bool_val(CallGraphProfile)); |
| 115 | + return Val_unit; |
| 116 | +} |
| 117 | + |
| 118 | +value llvm_passbuilder_options_set_merge_functions(value PBO, |
| 119 | + value MergeFunctions) { |
| 120 | + LLVMPassBuilderOptionsSetMergeFunctions(PassBuilderOptions_val(PBO), |
| 121 | + Bool_val(MergeFunctions)); |
| 122 | + return Val_unit; |
| 123 | +} |
| 124 | + |
| 125 | +value llvm_passbuilder_options_set_inliner_threshold(value PBO, |
| 126 | + value InlinerThreshold) { |
| 127 | + LLVMPassBuilderOptionsSetInlinerThreshold(PassBuilderOptions_val(PBO), |
| 128 | + Int_val(InlinerThreshold)); |
| 129 | + return Val_unit; |
| 130 | +} |
| 131 | + |
| 132 | +value llvm_dispose_passbuilder_options(value PBO) { |
| 133 | + LLVMDisposePassBuilderOptions(PassBuilderOptions_val(PBO)); |
| 134 | + return Val_unit; |
| 135 | +} |
0 commit comments