[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / examples / OrcV2Examples / OrcV2CBindingsRemovableCode / OrcV2CBindingsRemovableCode.c
blob08567071dad8ae9f594e9bb58edb048b35a6e10d
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 //===----------------------------------------------------------------------===//
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"
16 #include <stdio.h>
18 int handleError(LLVMErrorRef Err) {
19 char *ErrMsg = LLVMGetErrorMessage(Err);
20 fprintf(stderr, "Error: %s\n", ErrMsg);
21 LLVMDisposeErrorMessage(ErrMsg);
22 return 1;
25 LLVMOrcThreadSafeModuleRef createDemoModule(void) {
26 // Create a new ThreadSafeContext and underlying LLVMContext.
27 LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
29 // Get a reference to the underlying LLVMContext.
30 LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
32 // Create a new LLVM module.
33 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
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);
42 // - Add a basic block to the function.
43 LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
45 // - Add an IR builder and point it at the end of the basic block.
46 LLVMBuilderRef Builder = LLVMCreateBuilder();
47 LLVMPositionBuilderAtEnd(Builder, EntryBB);
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");
55 // - Build the return instruction.
56 LLVMBuildRet(Builder, Result);
58 // Our demo module is now complete. Wrap it and our ThreadSafeContext in a
59 // ThreadSafeModule.
60 LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
62 // Dispose of our local ThreadSafeContext value. The underlying LLVMContext
63 // will be kept alive by our ThreadSafeModule, TSM.
64 LLVMOrcDisposeThreadSafeContext(TSCtx);
66 // Return the result.
67 return TSM;
70 int main(int argc, char *argv[]) {
72 int MainResult = 0;
74 // Parse command line arguments and initialize LLVM Core.
75 LLVMParseCommandLineOptions(argc, (const char **)argv, "");
76 LLVMInitializeCore(LLVMGetGlobalPassRegistry());
78 // Initialize native target codegen and asm printer.
79 LLVMInitializeNativeTarget();
80 LLVMInitializeNativeAsmPrinter();
82 // Create the JIT instance.
83 LLVMOrcLLJITRef J;
85 LLVMErrorRef Err;
86 if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
87 MainResult = handleError(Err);
88 goto llvm_shutdown;
92 // Create our demo module.
93 LLVMOrcThreadSafeModuleRef TSM = createDemoModule();
94 LLVMOrcResourceTrackerRef RT;
96 // Add our demo module to the JIT.
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;
110 // Look up the address of our demo entry point.
111 printf("Looking up before removal...\n");
112 LLVMOrcJITTargetAddress SumAddr;
114 LLVMErrorRef Err;
115 if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
116 MainResult = handleError(Err);
117 goto jit_cleanup;
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);
125 // Print the result.
126 printf("1 + 2 = %i\n", Result);
128 // Remove the code.
130 LLVMErrorRef Err;
131 if ((Err = LLVMOrcResourceTrackerRemove(RT))) {
132 MainResult = handleError(Err);
133 goto jit_cleanup;
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");
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;
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.
158 printf("Releasing resource tracker...\n");
159 LLVMOrcReleaseResourceTracker(RT);
161 printf("Destroying LLJIT instance and exiting.\n");
163 LLVMErrorRef Err;
164 if ((Err = LLVMOrcDisposeLLJIT(J))) {
165 int NewFailureResult = handleError(Err);
166 if (MainResult == 0)
167 MainResult = NewFailureResult;
171 llvm_shutdown:
172 // Shut down LLVM.
173 LLVMShutdown();
175 return MainResult;