|
| 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/Orc.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 | + LLVMOrcResourceTrackerRef RT; |
| 95 | + |
| 96 | + // Add our demo module to the JIT. |
| 97 | + { |
| 98 | + LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J); |
| 99 | + RT = LLVMOrcJITDylibCreateResourceTracker(MainJD); |
| 100 | + LLVMErrorRef Err; |
| 101 | + if ((Err = LLVMOrcLLJITAddLLVMIRModuleWithRT(J, RT, TSM))) { |
| 102 | + // If adding the ThreadSafeModule fails then we need to clean it up |
| 103 | + // 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 | + printf("Looking up before removal...\n"); |
| 112 | + LLVMOrcJITTargetAddress SumAddr; |
| 113 | + { |
| 114 | + LLVMErrorRef Err; |
| 115 | + if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) { |
| 116 | + MainResult = handleError(Err); |
| 117 | + goto jit_cleanup; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // If we made it here then everything succeeded. Execute our JIT'd code. |
| 122 | + int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr; |
| 123 | + int32_t Result = Sum(1, 2); |
| 124 | + |
| 125 | + // Print the result. |
| 126 | + printf("1 + 2 = %i\n", Result); |
| 127 | + |
| 128 | + // Remove the code. |
| 129 | + { |
| 130 | + LLVMErrorRef Err; |
| 131 | + if ((Err = LLVMOrcResourceTrackerRemove(RT))) { |
| 132 | + MainResult = handleError(Err); |
| 133 | + goto jit_cleanup; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // Attempt a second lookup. Here we expect an error as the code and symbols |
| 138 | + // should have been removed. |
| 139 | + printf("Attempting to remove code / symbols...\n"); |
| 140 | + { |
| 141 | + LLVMOrcJITTargetAddress ThrowAwayAddress; |
| 142 | + LLVMErrorRef Err = LLVMOrcLLJITLookup(J, &ThrowAwayAddress, "sum"); |
| 143 | + if (Err) { |
| 144 | + printf("Received error as expected:\n"); |
| 145 | + handleError(Err); |
| 146 | + } else { |
| 147 | + printf("Failure: Second lookup should have generated an error.\n"); |
| 148 | + MainResult = 1; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | +jit_cleanup: |
| 153 | + // Destroy our JIT instance. This will clean up any memory that the JIT has |
| 154 | + // taken ownership of. This operation is non-trivial (e.g. it may need to |
| 155 | + // JIT static destructors) and may also fail. In that case we want to render |
| 156 | + // the error to stderr, but not overwrite any existing return value. |
| 157 | + |
| 158 | + printf("Releasing resource tracker...\n"); |
| 159 | + LLVMOrcReleaseResourceTracker(RT); |
| 160 | + |
| 161 | + printf("Destroying LLJIT instance and exiting.\n"); |
| 162 | + { |
| 163 | + LLVMErrorRef Err; |
| 164 | + if ((Err = LLVMOrcDisposeLLJIT(J))) { |
| 165 | + int NewFailureResult = handleError(Err); |
| 166 | + if (MainResult == 0) |
| 167 | + MainResult = NewFailureResult; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | +llvm_shutdown: |
| 172 | + // Shut down LLVM. |
| 173 | + LLVMShutdown(); |
| 174 | + |
| 175 | + return MainResult; |
| 176 | +} |
0 commit comments