1 //===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===//
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
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"
18 int handleError(LLVMErrorRef Err
) {
19 char *ErrMsg
= LLVMGetErrorMessage(Err
);
20 fprintf(stderr
, "Error: %s\n", ErrMsg
);
21 LLVMDisposeErrorMessage(ErrMsg
);
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"
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
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
);
70 int main(int argc
, char *argv
[]) {
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.
86 if ((Err
= LLVMOrcCreateLLJIT(&J
, 0))) {
87 MainResult
= handleError(Err
);
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
);
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
);
110 // Look up the address of our demo entry point.
111 printf("Looking up before removal...\n");
112 LLVMOrcJITTargetAddress SumAddr
;
115 if ((Err
= LLVMOrcLLJITLookup(J
, &SumAddr
, "sum"))) {
116 MainResult
= handleError(Err
);
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);
126 printf("1 + 2 = %i\n", Result
);
131 if ((Err
= LLVMOrcResourceTrackerRemove(RT
))) {
132 MainResult
= handleError(Err
);
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");
144 printf("Received error as expected:\n");
147 printf("Failure: Second lookup should have generated an error.\n");
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");
164 if ((Err
= LLVMOrcDisposeLLJIT(J
))) {
165 int NewFailureResult
= handleError(Err
);
167 MainResult
= NewFailureResult
;