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"
23 #include "llvm-c/Transforms/PassBuilder.h"
27 int handleError(LLVMErrorRef Err
) {
28 char *ErrMsg
= LLVMGetErrorMessage(Err
);
29 fprintf(stderr
, "Error: %s\n", ErrMsg
);
30 LLVMDisposeErrorMessage(ErrMsg
);
34 LLVMOrcThreadSafeModuleRef
createDemoModule(void) {
35 LLVMOrcThreadSafeContextRef TSCtx
= LLVMOrcCreateNewThreadSafeContext();
36 LLVMContextRef Ctx
= LLVMOrcThreadSafeContextGetContext(TSCtx
);
37 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext("demo", Ctx
);
38 LLVMTypeRef ParamTypes
[] = {LLVMInt32Type(), LLVMInt32Type()};
39 LLVMTypeRef SumFunctionType
=
40 LLVMFunctionType(LLVMInt32Type(), ParamTypes
, 2, 0);
41 LLVMValueRef SumFunction
= LLVMAddFunction(M
, "sum", SumFunctionType
);
42 LLVMBasicBlockRef EntryBB
= LLVMAppendBasicBlock(SumFunction
, "entry");
43 LLVMBuilderRef Builder
= LLVMCreateBuilder();
44 LLVMPositionBuilderAtEnd(Builder
, EntryBB
);
45 LLVMValueRef SumArg0
= LLVMGetParam(SumFunction
, 0);
46 LLVMValueRef SumArg1
= LLVMGetParam(SumFunction
, 1);
47 LLVMValueRef Result
= LLVMBuildAdd(Builder
, SumArg0
, SumArg1
, "result");
48 LLVMBuildRet(Builder
, Result
);
49 LLVMDisposeBuilder(Builder
);
50 LLVMOrcThreadSafeModuleRef TSM
= LLVMOrcCreateNewThreadSafeModule(M
, TSCtx
);
51 LLVMOrcDisposeThreadSafeContext(TSCtx
);
55 LLVMErrorRef
myModuleTransform(void *Ctx
, LLVMModuleRef Mod
) {
56 LLVMPassBuilderOptionsRef Options
= LLVMCreatePassBuilderOptions();
57 LLVMErrorRef E
= LLVMRunPasses(Mod
, "instcombine", NULL
, Options
);
58 LLVMDisposePassBuilderOptions(Options
);
62 LLVMErrorRef
transform(void *Ctx
, LLVMOrcThreadSafeModuleRef
*ModInOut
,
63 LLVMOrcMaterializationResponsibilityRef MR
) {
64 return LLVMOrcThreadSafeModuleWithModuleDo(*ModInOut
, myModuleTransform
, Ctx
);
67 int main(int argc
, const char *argv
[]) {
71 LLVMParseCommandLineOptions(argc
, argv
, "");
73 LLVMInitializeNativeTarget();
74 LLVMInitializeNativeAsmPrinter();
76 // Create a DumpObjects instance to use when dumping objects to disk.
77 LLVMOrcDumpObjectsRef DumpObjects
= LLVMOrcCreateDumpObjects("", "");
79 // Create the JIT instance.
83 if ((Err
= LLVMOrcCreateLLJIT(&J
, 0))) {
84 MainResult
= handleError(Err
);
89 // Use TransformLayer to set IR transform.
91 LLVMOrcIRTransformLayerRef TL
= LLVMOrcLLJITGetIRTransformLayer(J
);
92 LLVMOrcIRTransformLayerSetTransform(TL
, *transform
, NULL
);
95 // Create our demo module.
96 LLVMOrcThreadSafeModuleRef TSM
= createDemoModule();
98 // Add our demo module to the JIT.
100 LLVMOrcJITDylibRef MainJD
= LLVMOrcLLJITGetMainJITDylib(J
);
102 if ((Err
= LLVMOrcLLJITAddLLVMIRModule(J
, MainJD
, TSM
))) {
103 // If adding the ThreadSafeModule fails then we need to clean it up
104 // ourselves. If adding it succeeds the JIT will manage the memory.
105 LLVMOrcDisposeThreadSafeModule(TSM
);
106 MainResult
= handleError(Err
);
111 // Look up the address of our demo entry point.
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
);
130 // Destroy our JIT instance.
133 if ((Err
= LLVMOrcDisposeLLJIT(J
))) {
134 int NewFailureResult
= handleError(Err
);
136 MainResult
= NewFailureResult
;
141 // Destroy our DumpObjects instance.
142 LLVMOrcDisposeDumpObjects(DumpObjects
);