brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · 30806dc Raw
147 lines · c
1//===------ OrcV2CBindingsBasicUsage.c - Basic OrcV2 C Bindings Demo ------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm-c/Core.h"10#include "llvm-c/Error.h"11#include "llvm-c/LLJIT.h"12#include "llvm-c/Support.h"13#include "llvm-c/Target.h"14 15#include <stdio.h>16 17int handleError(LLVMErrorRef Err) {18  char *ErrMsg = LLVMGetErrorMessage(Err);19  fprintf(stderr, "Error: %s\n", ErrMsg);20  LLVMDisposeErrorMessage(ErrMsg);21  return 1;22}23 24LLVMOrcThreadSafeModuleRef createDemoModule(void) {25  // Create an LLVMContext.26  LLVMContextRef Ctx = LLVMContextCreate();27 28  // Create a new LLVM module.29  LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);30 31  // Add a "sum" function":32  //  - Create the function type and function instance.33  LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};34  LLVMTypeRef SumFunctionType =35      LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);36  LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);37 38  //  - Add a basic block to the function.39  LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");40 41  //  - Add an IR builder and point it at the end of the basic block.42  LLVMBuilderRef Builder = LLVMCreateBuilder();43  LLVMPositionBuilderAtEnd(Builder, EntryBB);44 45  //  - Get the two function arguments and use them co construct an "add"46  //    instruction.47  LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);48  LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);49  LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");50 51  //  - Build the return instruction.52  LLVMBuildRet(Builder, Result);53 54  //  - Free the builder.55  LLVMDisposeBuilder(Builder);56 57  // Create a new ThreadSafeContext to hold the context.58  LLVMOrcThreadSafeContextRef TSCtx =59      LLVMOrcCreateNewThreadSafeContextFromLLVMContext(Ctx);60 61  // Our demo module is now complete. Wrap it and our ThreadSafeContext in a62  // ThreadSafeModule.63  LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);64 65  // Dispose of our local ThreadSafeContext value. The underlying LLVMContext66  // will be kept alive by our ThreadSafeModule, TSM.67  LLVMOrcDisposeThreadSafeContext(TSCtx);68 69  // Return the result.70  return TSM;71}72 73int main(int argc, const char *argv[]) {74 75  int MainResult = 0;76 77  // Parse command line arguments and initialize LLVM Core.78  LLVMParseCommandLineOptions(argc, argv, "");79 80  // Initialize native target codegen and asm printer.81  LLVMInitializeNativeTarget();82  LLVMInitializeNativeAsmPrinter();83 84  // Create the JIT instance.85  LLVMOrcLLJITRef J;86  {87    LLVMErrorRef Err;88    if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {89      MainResult = handleError(Err);90      goto llvm_shutdown;91    }92  }93 94  // Create our demo module.95  LLVMOrcThreadSafeModuleRef TSM = createDemoModule();96 97  // Add our demo module to the JIT.98  {99    LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);100    LLVMErrorRef Err;101    if ((Err = LLVMOrcLLJITAddLLVMIRModule(J, MainJD, TSM))) {102      // If adding the ThreadSafeModule fails then we need to clean it up103      // ourselves. If adding it succeeds the JIT will manage the memory.104      LLVMOrcDisposeThreadSafeModule(TSM);105      MainResult = handleError(Err);106      goto jit_cleanup;107    }108  }109 110  // Look up the address of our demo entry point.111  LLVMOrcJITTargetAddress SumAddr;112  {113    LLVMErrorRef Err;114    if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {115      MainResult = handleError(Err);116      goto jit_cleanup;117    }118  }119 120  // If we made it here then everything succeeded. Execute our JIT'd code.121  int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;122  int32_t Result = Sum(1, 2);123 124  // Print the result.125  printf("1 + 2 = %i\n", Result);126 127jit_cleanup:128  // Destroy our JIT instance. This will clean up any memory that the JIT has129  // taken ownership of. This operation is non-trivial (e.g. it may need to130  // JIT static destructors) and may also fail. In that case we want to render131  // the error to stderr, but not overwrite any existing return value.132  {133    LLVMErrorRef Err;134    if ((Err = LLVMOrcDisposeLLJIT(J))) {135      int NewFailureResult = handleError(Err);136      if (MainResult == 0)137        MainResult = NewFailureResult;138    }139  }140 141llvm_shutdown:142  // Shut down LLVM.143  LLVMShutdown();144 145  return MainResult;146}147