HowToUseJIT.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
  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. // This small program provides an example of how to quickly build a small
  10. // module with two functions and execute it with the JIT.
  11. //
  12. // Goal:
  13. // The goal of this snippet is to create in the memory
  14. // the LLVM module consisting of two functions as follow:
  15. //
  16. // int add1(int x) {
  17. // return x+1;
  18. // }
  19. //
  20. // int foo() {
  21. // return add1(10);
  22. // }
  23. //
  24. // then compile the module via JIT, then execute the `foo'
  25. // function and return result to a driver, i.e. to a "host program".
  26. //
  27. // Some remarks and questions:
  28. //
  29. // - could we invoke some code using noname functions too?
  30. // e.g. evaluate "foo()+foo()" without fears to introduce
  31. // conflict of temporary function name with some real
  32. // existing function name?
  33. //
  34. //===----------------------------------------------------------------------===//
  35. #include "llvm/ADT/STLExtras.h"
  36. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  37. #include "llvm/ExecutionEngine/GenericValue.h"
  38. #include "llvm/IR/Argument.h"
  39. #include "llvm/IR/BasicBlock.h"
  40. #include "llvm/IR/Constants.h"
  41. #include "llvm/IR/DerivedTypes.h"
  42. #include "llvm/IR/Function.h"
  43. #include "llvm/IR/IRBuilder.h"
  44. #include "llvm/IR/Instructions.h"
  45. #include "llvm/IR/LLVMContext.h"
  46. #include "llvm/IR/Module.h"
  47. #include "llvm/IR/Type.h"
  48. #include "llvm/Support/Casting.h"
  49. #include "llvm/Support/ManagedStatic.h"
  50. #include "llvm/Support/TargetSelect.h"
  51. #include "llvm/Support/raw_ostream.h"
  52. #include <algorithm>
  53. #include <cassert>
  54. #include <memory>
  55. #include <vector>
  56. using namespace llvm;
  57. int main() {
  58. InitializeNativeTarget();
  59. LLVMContext Context;
  60. // Create some module to put our function into it.
  61. std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
  62. Module *M = Owner.get();
  63. // Create the add1 function entry and insert this entry into module M. The
  64. // function will have a return type of "int" and take an argument of "int".
  65. Function *Add1F =
  66. Function::Create(FunctionType::get(Type::getInt32Ty(Context),
  67. {Type::getInt32Ty(Context)}, false),
  68. Function::ExternalLinkage, "add1", M);
  69. // Add a basic block to the function. As before, it automatically inserts
  70. // because of the last argument.
  71. BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
  72. // Create a basic block builder with default parameters. The builder will
  73. // automatically append instructions to the basic block `BB'.
  74. IRBuilder<> builder(BB);
  75. // Get pointers to the constant `1'.
  76. Value *One = builder.getInt32(1);
  77. // Get pointers to the integer argument of the add1 function...
  78. assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
  79. Argument *ArgX = &*Add1F->arg_begin(); // Get the arg
  80. ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
  81. // Create the add instruction, inserting it into the end of BB.
  82. Value *Add = builder.CreateAdd(One, ArgX);
  83. // Create the return instruction and add it to the basic block
  84. builder.CreateRet(Add);
  85. // Now, function add1 is ready.
  86. // Now we're going to create function `foo', which returns an int and takes no
  87. // arguments.
  88. Function *FooF =
  89. Function::Create(FunctionType::get(Type::getInt32Ty(Context), {}, false),
  90. Function::ExternalLinkage, "foo", M);
  91. // Add a basic block to the FooF function.
  92. BB = BasicBlock::Create(Context, "EntryBlock", FooF);
  93. // Tell the basic block builder to attach itself to the new basic block
  94. builder.SetInsertPoint(BB);
  95. // Get pointer to the constant `10'.
  96. Value *Ten = builder.getInt32(10);
  97. // Pass Ten to the call to Add1F
  98. CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
  99. Add1CallRes->setTailCall(true);
  100. // Create the return instruction and add it to the basic block.
  101. builder.CreateRet(Add1CallRes);
  102. // Now we create the JIT.
  103. ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
  104. outs() << "We just constructed this LLVM module:\n\n" << *M;
  105. outs() << "\n\nRunning foo: ";
  106. outs().flush();
  107. // Call the `foo' function with no arguments:
  108. std::vector<GenericValue> noargs;
  109. GenericValue gv = EE->runFunction(FooF, noargs);
  110. // Import result of execution:
  111. outs() << "Result: " << gv.IntVal << "\n";
  112. delete EE;
  113. llvm_shutdown();
  114. return 0;
  115. }