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