10d5f15f7SLang Hames //===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===//
20d5f15f7SLang Hames //
30d5f15f7SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40d5f15f7SLang Hames // See https://llvm.org/LICENSE.txt for license information.
50d5f15f7SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60d5f15f7SLang Hames //
70d5f15f7SLang Hames //===----------------------------------------------------------------------===//
80d5f15f7SLang Hames
90d5f15f7SLang Hames #include "llvm-c/Core.h"
100d5f15f7SLang Hames #include "llvm-c/Error.h"
11f3570704SLang Hames #include "llvm-c/LLJIT.h"
120d5f15f7SLang Hames #include "llvm-c/Support.h"
130d5f15f7SLang Hames #include "llvm-c/Target.h"
140d5f15f7SLang Hames #include "llvm-c/TargetMachine.h"
150d5f15f7SLang Hames
160d5f15f7SLang Hames #include <stdio.h>
170d5f15f7SLang Hames
handleError(LLVMErrorRef Err)180d5f15f7SLang Hames int handleError(LLVMErrorRef Err) {
190d5f15f7SLang Hames char *ErrMsg = LLVMGetErrorMessage(Err);
200d5f15f7SLang Hames fprintf(stderr, "Error: %s\n", ErrMsg);
210d5f15f7SLang Hames LLVMDisposeErrorMessage(ErrMsg);
220d5f15f7SLang Hames return 1;
230d5f15f7SLang Hames }
240d5f15f7SLang Hames
createDemoModule(LLVMContextRef Ctx)250d5f15f7SLang Hames LLVMModuleRef createDemoModule(LLVMContextRef Ctx) {
260d5f15f7SLang Hames // Create a new LLVM module.
270d5f15f7SLang Hames LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
280d5f15f7SLang Hames
290d5f15f7SLang Hames // Add a "sum" function":
300d5f15f7SLang Hames // - Create the function type and function instance.
310d5f15f7SLang Hames LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
320d5f15f7SLang Hames LLVMTypeRef SumFunctionType =
330d5f15f7SLang Hames LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
340d5f15f7SLang Hames LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
350d5f15f7SLang Hames
360d5f15f7SLang Hames // - Add a basic block to the function.
370d5f15f7SLang Hames LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
380d5f15f7SLang Hames
390d5f15f7SLang Hames // - Add an IR builder and point it at the end of the basic block.
400d5f15f7SLang Hames LLVMBuilderRef Builder = LLVMCreateBuilder();
410d5f15f7SLang Hames LLVMPositionBuilderAtEnd(Builder, EntryBB);
420d5f15f7SLang Hames
430d5f15f7SLang Hames // - Get the two function arguments and use them co construct an "add"
440d5f15f7SLang Hames // instruction.
450d5f15f7SLang Hames LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
460d5f15f7SLang Hames LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
470d5f15f7SLang Hames LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
480d5f15f7SLang Hames
490d5f15f7SLang Hames // - Build the return instruction.
500d5f15f7SLang Hames LLVMBuildRet(Builder, Result);
510d5f15f7SLang Hames
5223c1822dSLang Hames // - Free the builder.
5323c1822dSLang Hames LLVMDisposeBuilder(Builder);
5423c1822dSLang Hames
550d5f15f7SLang Hames return M;
560d5f15f7SLang Hames }
570d5f15f7SLang Hames
main(int argc,const char * argv[])58*c2cabe47SJie Fu int main(int argc, const char *argv[]) {
590d5f15f7SLang Hames
600d5f15f7SLang Hames int MainResult = 0;
610d5f15f7SLang Hames
620d5f15f7SLang Hames // Parse command line arguments and initialize LLVM Core.
63*c2cabe47SJie Fu LLVMParseCommandLineOptions(argc, argv, "");
640d5f15f7SLang Hames
650d5f15f7SLang Hames // Initialize native target codegen and asm printer.
660d5f15f7SLang Hames LLVMInitializeNativeTarget();
670d5f15f7SLang Hames LLVMInitializeNativeAsmPrinter();
680d5f15f7SLang Hames
690d5f15f7SLang Hames // Create the JIT instance.
700d5f15f7SLang Hames LLVMOrcLLJITRef J;
710d5f15f7SLang Hames {
720d5f15f7SLang Hames LLVMErrorRef Err;
730d5f15f7SLang Hames if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
740d5f15f7SLang Hames MainResult = handleError(Err);
750d5f15f7SLang Hames goto llvm_shutdown;
760d5f15f7SLang Hames }
770d5f15f7SLang Hames }
780d5f15f7SLang Hames
790d5f15f7SLang Hames // Create our demo object file.
800d5f15f7SLang Hames LLVMMemoryBufferRef ObjectFileBuffer;
810d5f15f7SLang Hames {
820d5f15f7SLang Hames // Create a module.
830d5f15f7SLang Hames LLVMContextRef Ctx = LLVMContextCreate();
840d5f15f7SLang Hames LLVMModuleRef M = createDemoModule(Ctx);
850d5f15f7SLang Hames
860d5f15f7SLang Hames // Get the Target.
870d5f15f7SLang Hames const char *Triple = LLVMOrcLLJITGetTripleString(J);
880d5f15f7SLang Hames LLVMTargetRef Target = 0;
890d5f15f7SLang Hames char *ErrorMsg = 0;
900d5f15f7SLang Hames if (LLVMGetTargetFromTriple(Triple, &Target, &ErrorMsg)) {
910d5f15f7SLang Hames fprintf(stderr, "Error getting target for %s: %s\n", Triple, ErrorMsg);
920d5f15f7SLang Hames LLVMDisposeModule(M);
930d5f15f7SLang Hames LLVMContextDispose(Ctx);
940d5f15f7SLang Hames goto jit_cleanup;
950d5f15f7SLang Hames }
960d5f15f7SLang Hames
970d5f15f7SLang Hames // Construct a TargetMachine.
980d5f15f7SLang Hames LLVMTargetMachineRef TM =
990d5f15f7SLang Hames LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelNone,
1000d5f15f7SLang Hames LLVMRelocDefault, LLVMCodeModelDefault);
1010d5f15f7SLang Hames
1020d5f15f7SLang Hames // Run CodeGen to produce the buffer.
1030d5f15f7SLang Hames if (LLVMTargetMachineEmitToMemoryBuffer(TM, M, LLVMObjectFile, &ErrorMsg,
1040d5f15f7SLang Hames &ObjectFileBuffer)) {
1050d5f15f7SLang Hames fprintf(stderr, "Error emitting object: %s\n", ErrorMsg);
1060d5f15f7SLang Hames LLVMDisposeTargetMachine(TM);
1070d5f15f7SLang Hames LLVMDisposeModule(M);
1080d5f15f7SLang Hames LLVMContextDispose(Ctx);
1090d5f15f7SLang Hames goto jit_cleanup;
1100d5f15f7SLang Hames }
11123c1822dSLang Hames
11223c1822dSLang Hames // CodeGen succeeded -- We have our module, so free the Module, LLVMContext,
11323c1822dSLang Hames // and TargetMachine.
11423c1822dSLang Hames LLVMDisposeModule(M);
11523c1822dSLang Hames LLVMContextDispose(Ctx);
11623c1822dSLang Hames LLVMDisposeTargetMachine(TM);
1170d5f15f7SLang Hames }
1180d5f15f7SLang Hames
1190d5f15f7SLang Hames // Add our object file buffer to the JIT.
1200d5f15f7SLang Hames {
12137bcf2dfSLang Hames LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
1220d5f15f7SLang Hames LLVMErrorRef Err;
12337bcf2dfSLang Hames if ((Err = LLVMOrcLLJITAddObjectFile(J, MainJD, ObjectFileBuffer))) {
1240d5f15f7SLang Hames MainResult = handleError(Err);
1250d5f15f7SLang Hames goto jit_cleanup;
1260d5f15f7SLang Hames }
1270d5f15f7SLang Hames }
1280d5f15f7SLang Hames
1290d5f15f7SLang Hames // Look up the address of our demo entry point.
1300d5f15f7SLang Hames LLVMOrcJITTargetAddress SumAddr;
1310d5f15f7SLang Hames {
1320d5f15f7SLang Hames LLVMErrorRef Err;
1330d5f15f7SLang Hames if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
1340d5f15f7SLang Hames MainResult = handleError(Err);
1350d5f15f7SLang Hames goto jit_cleanup;
1360d5f15f7SLang Hames }
1370d5f15f7SLang Hames }
1380d5f15f7SLang Hames
1390d5f15f7SLang Hames // If we made it here then everything succeeded. Execute our JIT'd code.
1400d5f15f7SLang Hames int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
1410d5f15f7SLang Hames int32_t Result = Sum(1, 2);
1420d5f15f7SLang Hames
1430d5f15f7SLang Hames // Print the result.
1440d5f15f7SLang Hames printf("1 + 2 = %i\n", Result);
1450d5f15f7SLang Hames
1460d5f15f7SLang Hames jit_cleanup:
1470d5f15f7SLang Hames // Destroy our JIT instance. This will clean up any memory that the JIT has
1480d5f15f7SLang Hames // taken ownership of. This operation is non-trivial (e.g. it may need to
1490d5f15f7SLang Hames // JIT static destructors) and may also fail. In that case we want to render
1500d5f15f7SLang Hames // the error to stderr, but not overwrite any existing return value.
1510d5f15f7SLang Hames {
1520d5f15f7SLang Hames LLVMErrorRef Err;
1530d5f15f7SLang Hames if ((Err = LLVMOrcDisposeLLJIT(J))) {
1540d5f15f7SLang Hames int NewFailureResult = handleError(Err);
1550d5f15f7SLang Hames if (MainResult == 0)
1560d5f15f7SLang Hames MainResult = NewFailureResult;
1570d5f15f7SLang Hames }
1580d5f15f7SLang Hames }
1590d5f15f7SLang Hames
1600d5f15f7SLang Hames llvm_shutdown:
1610d5f15f7SLang Hames // Shut down LLVM.
1620d5f15f7SLang Hames LLVMShutdown();
1630d5f15f7SLang Hames
1640d5f15f7SLang Hames return MainResult;
1650d5f15f7SLang Hames }
166