1 //===- OrcV2CBindingsDumpObjects.c - Dump JIT'd objects to disk via C API -===//
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 // 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.
12 //
13 // Try experimenting with the DumpDir and IdentifierOverride arguments to
14 // LLVMOrcCreateDumpObjects.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm-c/Core.h"
19 #include "llvm-c/Error.h"
20 #include "llvm-c/Initialization.h"
21 #include "llvm-c/LLJIT.h"
22 #include "llvm-c/Support.h"
23 #include "llvm-c/Target.h"
24 #include "llvm-c/Transforms/Scalar.h"
25 
26 #include <stdio.h>
27 
handleError(LLVMErrorRef Err)28 int handleError(LLVMErrorRef Err) {
29   char *ErrMsg = LLVMGetErrorMessage(Err);
30   fprintf(stderr, "Error: %s\n", ErrMsg);
31   LLVMDisposeErrorMessage(ErrMsg);
32   return 1;
33 }
34 
createDemoModule(void)35 LLVMOrcThreadSafeModuleRef createDemoModule(void) {
36   LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
37   LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
38   LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
39   LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
40   LLVMTypeRef SumFunctionType =
41       LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
42   LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
43   LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
44   LLVMBuilderRef Builder = LLVMCreateBuilder();
45   LLVMPositionBuilderAtEnd(Builder, EntryBB);
46   LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
47   LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
48   LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
49   LLVMBuildRet(Builder, Result);
50   LLVMDisposeBuilder(Builder);
51   LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
52   LLVMOrcDisposeThreadSafeContext(TSCtx);
53   return TSM;
54 }
55 
myModuleTransform(void * Ctx,LLVMModuleRef Mod)56 LLVMErrorRef myModuleTransform(void *Ctx, LLVMModuleRef Mod) {
57   LLVMPassManagerRef PM = LLVMCreatePassManager();
58   LLVMAddInstructionCombiningPass(PM);
59   LLVMRunPassManager(PM, Mod);
60   LLVMDisposePassManager(PM);
61   return LLVMErrorSuccess;
62 }
63 
transform(void * Ctx,LLVMOrcThreadSafeModuleRef * ModInOut,LLVMOrcMaterializationResponsibilityRef MR)64 LLVMErrorRef transform(void *Ctx, LLVMOrcThreadSafeModuleRef *ModInOut,
65                        LLVMOrcMaterializationResponsibilityRef MR) {
66   return LLVMOrcThreadSafeModuleWithModuleDo(*ModInOut, myModuleTransform, Ctx);
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[]) {
70 
71   int MainResult = 0;
72 
73   LLVMParseCommandLineOptions(argc, (const char **)argv, "");
74   LLVMInitializeCore(LLVMGetGlobalPassRegistry());
75 
76   LLVMInitializeNativeTarget();
77   LLVMInitializeNativeAsmPrinter();
78 
79   // Create a DumpObjects instance to use when dumping objects to disk.
80   LLVMOrcDumpObjectsRef DumpObjects = LLVMOrcCreateDumpObjects("", "");
81 
82   // Create the JIT instance.
83   LLVMOrcLLJITRef J;
84   {
85     LLVMErrorRef Err;
86     if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
87       MainResult = handleError(Err);
88       goto llvm_shutdown;
89     }
90   }
91 
92   // Use TransformLayer to set IR transform.
93   {
94     LLVMOrcIRTransformLayerRef TL = LLVMOrcLLJITGetIRTransformLayer(J);
95     LLVMOrcIRTransformLayerSetTransform(TL, *transform, NULL);
96   }
97 
98   // Create our demo module.
99   LLVMOrcThreadSafeModuleRef TSM = createDemoModule();
100 
101   // Add our demo module to the JIT.
102   {
103     LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
104     LLVMErrorRef Err;
105     if ((Err = LLVMOrcLLJITAddLLVMIRModule(J, MainJD, TSM))) {
106       // If adding the ThreadSafeModule fails then we need to clean it up
107       // ourselves. If adding it succeeds the JIT will manage the memory.
108       LLVMOrcDisposeThreadSafeModule(TSM);
109       MainResult = handleError(Err);
110       goto jit_cleanup;
111     }
112   }
113 
114   // Look up the address of our demo entry point.
115   LLVMOrcJITTargetAddress SumAddr;
116   {
117     LLVMErrorRef Err;
118     if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
119       MainResult = handleError(Err);
120       goto jit_cleanup;
121     }
122   }
123 
124   // If we made it here then everything succeeded. Execute our JIT'd code.
125   int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
126   int32_t Result = Sum(1, 2);
127 
128   // Print the result.
129   printf("1 + 2 = %i\n", Result);
130 
131 jit_cleanup:
132 
133   // Destroy our JIT instance.
134   {
135     LLVMErrorRef Err;
136     if ((Err = LLVMOrcDisposeLLJIT(J))) {
137       int NewFailureResult = handleError(Err);
138       if (MainResult == 0)
139         MainResult = NewFailureResult;
140     }
141   }
142 
143 llvm_shutdown:
144   // Destroy our DumpObjects instance.
145   LLVMOrcDisposeDumpObjects(DumpObjects);
146 
147   // Shut down LLVM.
148   LLVMShutdown();
149 
150   return MainResult;
151 }
152