1 //===- OrcV2CBindingsDumpObjects.c - Dump JIT'd objects to disk via C API -===//
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 // To run the demo build 'OrcV2CBindingsDumpObjects', then run the built
10 // program. It will execute as for OrcV2CBindingsBasicUsage, but will write
11 // a single JIT'd object out to the working directory.
13 // Try experimenting with the DumpDir and IdentifierOverride arguments to
14 // LLVMOrcCreateDumpObjects.
16 //===----------------------------------------------------------------------===//
18 #include "llvm-c/Core.h"
19 #include "llvm-c/Error.h"
20 #include "llvm-c/LLJIT.h"
21 #include "llvm-c/Support.h"
22 #include "llvm-c/Target.h"
26 int handleError(LLVMErrorRef Err
) {
27 char *ErrMsg
= LLVMGetErrorMessage(Err
);
28 fprintf(stderr
, "Error: %s\n", ErrMsg
);
29 LLVMDisposeErrorMessage(ErrMsg
);
33 LLVMOrcThreadSafeModuleRef
createDemoModule(void) {
34 LLVMOrcThreadSafeContextRef TSCtx
= LLVMOrcCreateNewThreadSafeContext();
35 LLVMContextRef Ctx
= LLVMOrcThreadSafeContextGetContext(TSCtx
);
36 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext("demo", Ctx
);
37 LLVMTypeRef ParamTypes
[] = {LLVMInt32Type(), LLVMInt32Type()};
38 LLVMTypeRef SumFunctionType
=
39 LLVMFunctionType(LLVMInt32Type(), ParamTypes
, 2, 0);
40 LLVMValueRef SumFunction
= LLVMAddFunction(M
, "sum", SumFunctionType
);
41 LLVMBasicBlockRef EntryBB
= LLVMAppendBasicBlock(SumFunction
, "entry");
42 LLVMBuilderRef Builder
= LLVMCreateBuilder();
43 LLVMPositionBuilderAtEnd(Builder
, EntryBB
);
44 LLVMValueRef SumArg0
= LLVMGetParam(SumFunction
, 0);
45 LLVMValueRef SumArg1
= LLVMGetParam(SumFunction
, 1);
46 LLVMValueRef Result
= LLVMBuildAdd(Builder
, SumArg0
, SumArg1
, "result");
47 LLVMBuildRet(Builder
, Result
);
48 LLVMOrcThreadSafeModuleRef TSM
= LLVMOrcCreateNewThreadSafeModule(M
, TSCtx
);
49 LLVMOrcDisposeThreadSafeContext(TSCtx
);
53 LLVMErrorRef
dumpObjectsTransform(void *Ctx
, LLVMMemoryBufferRef
*ObjInOut
) {
54 LLVMOrcDumpObjectsRef DumpObjects
= *(LLVMOrcDumpObjectsRef
*)Ctx
;
55 return LLVMOrcDumpObjects_CallOperator(DumpObjects
, ObjInOut
);
58 int main(int argc
, const char *argv
[]) {
62 LLVMParseCommandLineOptions(argc
, argv
, "");
64 LLVMInitializeNativeTarget();
65 LLVMInitializeNativeAsmPrinter();
67 // Create a DumpObjects instance to use when dumping objects to disk.
68 LLVMOrcDumpObjectsRef DumpObjects
= LLVMOrcCreateDumpObjects("", "");
70 // Create the JIT instance.
74 if ((Err
= LLVMOrcCreateLLJIT(&J
, 0))) {
75 MainResult
= handleError(Err
);
80 // Set an object transform to call our DumpObjects instance for every
82 LLVMOrcObjectTransformLayerSetTransform(LLVMOrcLLJITGetObjTransformLayer(J
),
83 dumpObjectsTransform
, &DumpObjects
);
85 // Create our demo module.
86 LLVMOrcThreadSafeModuleRef TSM
= createDemoModule();
88 // Add our demo module to the JIT.
90 LLVMOrcJITDylibRef MainJD
= LLVMOrcLLJITGetMainJITDylib(J
);
92 if ((Err
= LLVMOrcLLJITAddLLVMIRModule(J
, MainJD
, TSM
))) {
93 // If adding the ThreadSafeModule fails then we need to clean it up
94 // ourselves. If adding it succeeds the JIT will manage the memory.
95 LLVMOrcDisposeThreadSafeModule(TSM
);
96 MainResult
= handleError(Err
);
101 // Look up the address of our demo entry point.
102 LLVMOrcJITTargetAddress SumAddr
;
105 if ((Err
= LLVMOrcLLJITLookup(J
, &SumAddr
, "sum"))) {
106 MainResult
= handleError(Err
);
111 // If we made it here then everything succeeded. Execute our JIT'd code.
112 int32_t (*Sum
)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr
;
113 int32_t Result
= Sum(1, 2);
116 printf("1 + 2 = %i\n", Result
);
120 // Destroy our JIT instance.
123 if ((Err
= LLVMOrcDisposeLLJIT(J
))) {
124 int NewFailureResult
= handleError(Err
);
126 MainResult
= NewFailureResult
;
131 // Destroy our DumpObjects instance.
132 LLVMOrcDisposeDumpObjects(DumpObjects
);