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