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/LLJIT.h"
12 #include "llvm-c/Support.h"
13 #include "llvm-c/Target.h"
14 #include "llvm-c/TargetMachine.h"
15
16 #include <stdio.h>
17
handleError(LLVMErrorRef Err)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
createDemoModule(LLVMContextRef Ctx)25 LLVMModuleRef createDemoModule(LLVMContextRef Ctx) {
26 // Create a new LLVM module.
27 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
28
29 // Add a "sum" function":
30 // - Create the function type and function instance.
31 LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
32 LLVMTypeRef SumFunctionType =
33 LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
34 LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
35
36 // - Add a basic block to the function.
37 LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
38
39 // - Add an IR builder and point it at the end of the basic block.
40 LLVMBuilderRef Builder = LLVMCreateBuilder();
41 LLVMPositionBuilderAtEnd(Builder, EntryBB);
42
43 // - Get the two function arguments and use them co construct an "add"
44 // instruction.
45 LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
46 LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
47 LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
48
49 // - Build the return instruction.
50 LLVMBuildRet(Builder, Result);
51
52 // - Free the builder.
53 LLVMDisposeBuilder(Builder);
54
55 return M;
56 }
57
main(int argc,const char * argv[])58 int main(int argc, const char *argv[]) {
59
60 int MainResult = 0;
61
62 // Parse command line arguments and initialize LLVM Core.
63 LLVMParseCommandLineOptions(argc, argv, "");
64
65 // Initialize native target codegen and asm printer.
66 LLVMInitializeNativeTarget();
67 LLVMInitializeNativeAsmPrinter();
68
69 // Create the JIT instance.
70 LLVMOrcLLJITRef J;
71 {
72 LLVMErrorRef Err;
73 if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
74 MainResult = handleError(Err);
75 goto llvm_shutdown;
76 }
77 }
78
79 // Create our demo object file.
80 LLVMMemoryBufferRef ObjectFileBuffer;
81 {
82 // Create a module.
83 LLVMContextRef Ctx = LLVMContextCreate();
84 LLVMModuleRef M = createDemoModule(Ctx);
85
86 // Get the Target.
87 const char *Triple = LLVMOrcLLJITGetTripleString(J);
88 LLVMTargetRef Target = 0;
89 char *ErrorMsg = 0;
90 if (LLVMGetTargetFromTriple(Triple, &Target, &ErrorMsg)) {
91 fprintf(stderr, "Error getting target for %s: %s\n", Triple, ErrorMsg);
92 LLVMDisposeModule(M);
93 LLVMContextDispose(Ctx);
94 goto jit_cleanup;
95 }
96
97 // Construct a TargetMachine.
98 LLVMTargetMachineRef TM =
99 LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelNone,
100 LLVMRelocDefault, LLVMCodeModelDefault);
101
102 // Run CodeGen to produce the buffer.
103 if (LLVMTargetMachineEmitToMemoryBuffer(TM, M, LLVMObjectFile, &ErrorMsg,
104 &ObjectFileBuffer)) {
105 fprintf(stderr, "Error emitting object: %s\n", ErrorMsg);
106 LLVMDisposeTargetMachine(TM);
107 LLVMDisposeModule(M);
108 LLVMContextDispose(Ctx);
109 goto jit_cleanup;
110 }
111
112 // CodeGen succeeded -- We have our module, so free the Module, LLVMContext,
113 // and TargetMachine.
114 LLVMDisposeModule(M);
115 LLVMContextDispose(Ctx);
116 LLVMDisposeTargetMachine(TM);
117 }
118
119 // Add our object file buffer to the JIT.
120 {
121 LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
122 LLVMErrorRef Err;
123 if ((Err = LLVMOrcLLJITAddObjectFile(J, MainJD, ObjectFileBuffer))) {
124 MainResult = handleError(Err);
125 goto jit_cleanup;
126 }
127 }
128
129 // Look up the address of our demo entry point.
130 LLVMOrcJITTargetAddress SumAddr;
131 {
132 LLVMErrorRef Err;
133 if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
134 MainResult = handleError(Err);
135 goto jit_cleanup;
136 }
137 }
138
139 // If we made it here then everything succeeded. Execute our JIT'd code.
140 int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
141 int32_t Result = Sum(1, 2);
142
143 // Print the result.
144 printf("1 + 2 = %i\n", Result);
145
146 jit_cleanup:
147 // Destroy our JIT instance. This will clean up any memory that the JIT has
148 // taken ownership of. This operation is non-trivial (e.g. it may need to
149 // JIT static destructors) and may also fail. In that case we want to render
150 // the error to stderr, but not overwrite any existing return value.
151 {
152 LLVMErrorRef Err;
153 if ((Err = LLVMOrcDisposeLLJIT(J))) {
154 int NewFailureResult = handleError(Err);
155 if (MainResult == 0)
156 MainResult = NewFailureResult;
157 }
158 }
159
160 llvm_shutdown:
161 // Shut down LLVM.
162 LLVMShutdown();
163
164 return MainResult;
165 }
166