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/IRReader.h"
12 #include "llvm-c/Initialization.h"
13 #include "llvm-c/LLJIT.h"
14 #include "llvm-c/Support.h"
15 #include "llvm-c/Target.h"
19 int handleError(LLVMErrorRef Err
) {
20 char *ErrMsg
= LLVMGetErrorMessage(Err
);
21 fprintf(stderr
, "Error: %s\n", ErrMsg
);
22 LLVMDisposeErrorMessage(ErrMsg
);
26 // Example IR modules.
28 // Note that in the conditionally compiled modules, FooMod and BarMod, functions
29 // have been given an _body suffix. This is to ensure that their names do not
30 // clash with their lazy-reexports.
31 // For clients who do not wish to rename function bodies (e.g. because they want
32 // to re-use cached objects between static and JIT compiles) techniques exist to
33 // avoid renaming. See the lazy-reexports section of the ORCv2 design doc.
35 const char FooMod
[] = " define i32 @foo_body() { \n"
40 const char BarMod
[] = " define i32 @bar_body() { \n"
45 const char MainMod
[] =
46 " define i32 @entry(i32 %argc) { \n"
48 " %and = and i32 %argc, 1 \n"
49 " %tobool = icmp eq i32 %and, 0 \n"
50 " br i1 %tobool, label %if.end, label %if.then \n"
53 " %call = tail call i32 @foo() \n"
54 " br label %return \n"
57 " %call1 = tail call i32 @bar() \n"
58 " br label %return \n"
61 " %retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.end ] \n"
62 " ret i32 %retval.0 \n"
65 " declare i32 @foo() \n"
66 " declare i32 @bar() \n";
68 LLVMErrorRef
parseExampleModule(const char *Source
, size_t Len
,
70 LLVMOrcThreadSafeModuleRef
*TSM
) {
71 // Create a new ThreadSafeContext and underlying LLVMContext.
72 LLVMOrcThreadSafeContextRef TSCtx
= LLVMOrcCreateNewThreadSafeContext();
74 // Get a reference to the underlying LLVMContext.
75 LLVMContextRef Ctx
= LLVMOrcThreadSafeContextGetContext(TSCtx
);
77 // Wrap Source in a MemoryBuffer
78 LLVMMemoryBufferRef MB
=
79 LLVMCreateMemoryBufferWithMemoryRange(Source
, Len
, Name
, 0);
81 // Parse the LLVM module.
84 if (LLVMParseIRInContext(Ctx
, MB
, &M
, &ErrMsg
)) {
85 return LLVMCreateStringError(ErrMsg
);
86 // TODO: LLVMDisposeMessage(ErrMsg);
89 // Our module is now complete. Wrap it and our ThreadSafeContext in a
91 *TSM
= LLVMOrcCreateNewThreadSafeModule(M
, TSCtx
);
93 // Dispose of our local ThreadSafeContext value. The underlying LLVMContext
94 // will be kept alive by our ThreadSafeModule, TSM.
95 LLVMOrcDisposeThreadSafeContext(TSCtx
);
97 return LLVMErrorSuccess
;
100 int main(int argc
, char *argv
[]) {
104 // Parse command line arguments and initialize LLVM Core.
105 LLVMParseCommandLineOptions(argc
, (const char **)argv
, "");
106 LLVMInitializeCore(LLVMGetGlobalPassRegistry());
108 // Initialize native target codegen and asm printer.
109 LLVMInitializeNativeTarget();
110 LLVMInitializeNativeAsmPrinter();
112 // Set up a JIT instance.
114 const char *TargetTriple
;
117 if ((Err
= LLVMOrcCreateLLJIT(&J
, 0))) {
118 MainResult
= handleError(Err
);
121 TargetTriple
= LLVMOrcLLJITGetTripleString(J
);
124 // Add our demo modules to the JIT.
126 LLVMOrcJITDylibRef MainJD
= LLVMOrcLLJITGetMainJITDylib(J
);
129 LLVMOrcThreadSafeModuleRef FooTSM
;
131 parseExampleModule(FooMod
, sizeof(FooMod
), "foo-mod", &FooTSM
))) {
132 MainResult
= handleError(Err
);
136 if ((Err
= LLVMOrcLLJITAddLLVMIRModule(J
, MainJD
, FooTSM
))) {
137 // If adding the ThreadSafeModule fails then we need to clean it up
138 // ourselves. If adding it succeeds the JIT will manage the memory.
139 LLVMOrcDisposeThreadSafeModule(FooTSM
);
140 MainResult
= handleError(Err
);
144 LLVMOrcThreadSafeModuleRef BarTSM
;
146 parseExampleModule(BarMod
, sizeof(BarMod
), "bar-mod", &BarTSM
))) {
147 MainResult
= handleError(Err
);
151 if ((Err
= LLVMOrcLLJITAddLLVMIRModule(J
, MainJD
, BarTSM
))) {
152 LLVMOrcDisposeThreadSafeModule(BarTSM
);
153 MainResult
= handleError(Err
);
157 LLVMOrcThreadSafeModuleRef MainTSM
;
158 if ((Err
= parseExampleModule(MainMod
, sizeof(MainMod
), "main-mod",
160 MainResult
= handleError(Err
);
164 if ((Err
= LLVMOrcLLJITAddLLVMIRModule(J
, MainJD
, MainTSM
))) {
165 LLVMOrcDisposeThreadSafeModule(MainTSM
);
166 MainResult
= handleError(Err
);
171 // add lazy reexports
172 LLVMOrcIndirectStubsManagerRef ISM
=
173 LLVMOrcCreateLocalIndirectStubsManager(TargetTriple
);
175 LLVMOrcLazyCallThroughManagerRef LCTM
;
178 LLVMOrcExecutionSessionRef ES
= LLVMOrcLLJITGetExecutionSession(J
);
179 if ((Err
= LLVMOrcCreateLocalLazyCallThroughManager(TargetTriple
, ES
, 0,
181 LLVMOrcDisposeIndirectStubsManager(ISM
);
182 MainResult
= handleError(Err
);
187 LLVMJITSymbolFlags flag
= {
188 LLVMJITSymbolGenericFlagsExported
| LLVMJITSymbolGenericFlagsCallable
, 0};
189 LLVMOrcCSymbolAliasMapPair ReExports
[2] = {
190 {LLVMOrcLLJITMangleAndIntern(J
, "foo"),
191 {LLVMOrcLLJITMangleAndIntern(J
, "foo_body"), flag
}},
192 {LLVMOrcLLJITMangleAndIntern(J
, "bar"),
193 {LLVMOrcLLJITMangleAndIntern(J
, "bar_body"), flag
}},
197 LLVMOrcJITDylibRef MainJD
= LLVMOrcLLJITGetMainJITDylib(J
);
198 LLVMOrcMaterializationUnitRef MU
=
199 LLVMOrcLazyReexports(LCTM
, ISM
, MainJD
, ReExports
, 2);
200 LLVMOrcJITDylibDefine(MainJD
, MU
);
203 // Look up the address of our demo entry point.
204 LLVMOrcJITTargetAddress EntryAddr
;
207 if ((Err
= LLVMOrcLLJITLookup(J
, &EntryAddr
, "entry"))) {
208 MainResult
= handleError(Err
);
213 // If we made it here then everything succeeded. Execute our JIT'd code.
214 int32_t (*Entry
)(int32_t) = (int32_t(*)(int32_t))EntryAddr
;
215 int32_t Result
= Entry(argc
);
217 printf("--- Result ---\n");
218 printf("entry(%i) = %i\n", argc
, Result
);
221 LLVMOrcDisposeIndirectStubsManager(ISM
);
222 LLVMOrcDisposeLazyCallThroughManager(LCTM
);
226 // Destroy our JIT instance. This will clean up any memory that the JIT has
227 // taken ownership of. This operation is non-trivial (e.g. it may need to
228 // JIT static destructors) and may also fail. In that case we want to render
229 // the error to stderr, but not overwrite any existing return value.
232 if ((Err
= LLVMOrcDisposeLLJIT(J
))) {
233 int NewFailureResult
= handleError(Err
);
235 MainResult
= NewFailureResult
;