Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / examples / OrcV2Examples / OrcV2CBindingsIRTransforms / OrcV2CBindingsIRTransforms.c
blob41ae6e53db1d628a7e337f2d63d056676a19befb
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.
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"
25 #include <stdio.h>
27 int handleError(LLVMErrorRef Err) {
28 char *ErrMsg = LLVMGetErrorMessage(Err);
29 fprintf(stderr, "Error: %s\n", ErrMsg);
30 LLVMDisposeErrorMessage(ErrMsg);
31 return 1;
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);
52 return TSM;
55 LLVMErrorRef myModuleTransform(void *Ctx, LLVMModuleRef Mod) {
56 LLVMPassBuilderOptionsRef Options = LLVMCreatePassBuilderOptions();
57 LLVMErrorRef E = LLVMRunPasses(Mod, "instcombine", NULL, Options);
58 LLVMDisposePassBuilderOptions(Options);
59 return E;
62 LLVMErrorRef transform(void *Ctx, LLVMOrcThreadSafeModuleRef *ModInOut,
63 LLVMOrcMaterializationResponsibilityRef MR) {
64 return LLVMOrcThreadSafeModuleWithModuleDo(*ModInOut, myModuleTransform, Ctx);
67 int main(int argc, const char *argv[]) {
69 int MainResult = 0;
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.
80 LLVMOrcLLJITRef J;
82 LLVMErrorRef Err;
83 if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
84 MainResult = handleError(Err);
85 goto llvm_shutdown;
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);
101 LLVMErrorRef Err;
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);
107 goto jit_cleanup;
111 // Look up the address of our demo entry point.
112 LLVMOrcJITTargetAddress SumAddr;
114 LLVMErrorRef Err;
115 if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
116 MainResult = handleError(Err);
117 goto jit_cleanup;
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);
125 // Print the result.
126 printf("1 + 2 = %i\n", Result);
128 jit_cleanup:
130 // Destroy our JIT instance.
132 LLVMErrorRef Err;
133 if ((Err = LLVMOrcDisposeLLJIT(J))) {
134 int NewFailureResult = handleError(Err);
135 if (MainResult == 0)
136 MainResult = NewFailureResult;
140 llvm_shutdown:
141 // Destroy our DumpObjects instance.
142 LLVMOrcDisposeDumpObjects(DumpObjects);
144 // Shut down LLVM.
145 LLVMShutdown();
147 return MainResult;