[RISCV] Match vcompress during shuffle lowering (#117748)
[llvm-project.git] / llvm / examples / OrcV2Examples / OrcV2CBindingsAddObjectFile / OrcV2CBindingsAddObjectFile.c
blobb51fc930c4318e77e3b736326b2c7bbf1f6a9770
1 //===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm-c/Core.h"
10 #include "llvm-c/Error.h"
11 #include "llvm-c/LLJIT.h"
12 #include "llvm-c/Support.h"
13 #include "llvm-c/Target.h"
14 #include "llvm-c/TargetMachine.h"
16 #include <stdio.h>
18 int handleError(LLVMErrorRef Err) {
19 char *ErrMsg = LLVMGetErrorMessage(Err);
20 fprintf(stderr, "Error: %s\n", ErrMsg);
21 LLVMDisposeErrorMessage(ErrMsg);
22 return 1;
25 LLVMModuleRef createDemoModule(LLVMContextRef Ctx) {
26 // Create a new LLVM module.
27 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
29 // Add a "sum" function":
30 // - Create the function type and function instance.
31 LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
32 LLVMTypeRef SumFunctionType =
33 LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
34 LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
36 // - Add a basic block to the function.
37 LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
39 // - Add an IR builder and point it at the end of the basic block.
40 LLVMBuilderRef Builder = LLVMCreateBuilder();
41 LLVMPositionBuilderAtEnd(Builder, EntryBB);
43 // - Get the two function arguments and use them co construct an "add"
44 // instruction.
45 LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
46 LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
47 LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
49 // - Build the return instruction.
50 LLVMBuildRet(Builder, Result);
52 // - Free the builder.
53 LLVMDisposeBuilder(Builder);
55 return M;
58 int main(int argc, const char *argv[]) {
60 int MainResult = 0;
62 // Parse command line arguments and initialize LLVM Core.
63 LLVMParseCommandLineOptions(argc, argv, "");
65 // Initialize native target codegen and asm printer.
66 LLVMInitializeNativeTarget();
67 LLVMInitializeNativeAsmPrinter();
69 // Create the JIT instance.
70 LLVMOrcLLJITRef J;
72 LLVMErrorRef Err;
73 if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
74 MainResult = handleError(Err);
75 goto llvm_shutdown;
79 // Create our demo object file.
80 LLVMMemoryBufferRef ObjectFileBuffer;
82 // Create a module.
83 LLVMContextRef Ctx = LLVMContextCreate();
84 LLVMModuleRef M = createDemoModule(Ctx);
86 // Get the Target.
87 const char *Triple = LLVMOrcLLJITGetTripleString(J);
88 LLVMTargetRef Target = 0;
89 char *ErrorMsg = 0;
90 if (LLVMGetTargetFromTriple(Triple, &Target, &ErrorMsg)) {
91 fprintf(stderr, "Error getting target for %s: %s\n", Triple, ErrorMsg);
92 LLVMDisposeModule(M);
93 LLVMContextDispose(Ctx);
94 goto jit_cleanup;
97 // Construct a TargetMachine.
98 LLVMTargetMachineRef TM =
99 LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelNone,
100 LLVMRelocDefault, LLVMCodeModelDefault);
102 // Run CodeGen to produce the buffer.
103 if (LLVMTargetMachineEmitToMemoryBuffer(TM, M, LLVMObjectFile, &ErrorMsg,
104 &ObjectFileBuffer)) {
105 fprintf(stderr, "Error emitting object: %s\n", ErrorMsg);
106 LLVMDisposeTargetMachine(TM);
107 LLVMDisposeModule(M);
108 LLVMContextDispose(Ctx);
109 goto jit_cleanup;
112 // CodeGen succeeded -- We have our module, so free the Module, LLVMContext,
113 // and TargetMachine.
114 LLVMDisposeModule(M);
115 LLVMContextDispose(Ctx);
116 LLVMDisposeTargetMachine(TM);
119 // Add our object file buffer to the JIT.
121 LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
122 LLVMErrorRef Err;
123 if ((Err = LLVMOrcLLJITAddObjectFile(J, MainJD, ObjectFileBuffer))) {
124 MainResult = handleError(Err);
125 goto jit_cleanup;
129 // Look up the address of our demo entry point.
130 LLVMOrcJITTargetAddress SumAddr;
132 LLVMErrorRef Err;
133 if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
134 MainResult = handleError(Err);
135 goto jit_cleanup;
139 // If we made it here then everything succeeded. Execute our JIT'd code.
140 int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
141 int32_t Result = Sum(1, 2);
143 // Print the result.
144 printf("1 + 2 = %i\n", Result);
146 jit_cleanup:
147 // Destroy our JIT instance. This will clean up any memory that the JIT has
148 // taken ownership of. This operation is non-trivial (e.g. it may need to
149 // JIT static destructors) and may also fail. In that case we want to render
150 // the error to stderr, but not overwrite any existing return value.
152 LLVMErrorRef Err;
153 if ((Err = LLVMOrcDisposeLLJIT(J))) {
154 int NewFailureResult = handleError(Err);
155 if (MainResult == 0)
156 MainResult = NewFailureResult;
160 llvm_shutdown:
161 // Shut down LLVM.
162 LLVMShutdown();
164 return MainResult;