HowToUseJIT.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This small program provides an example of how to quickly build a small
  11. // module with two functions and execute it with the JIT.
  12. //
  13. // Goal:
  14. // The goal of this snippet is to create in the memory
  15. // the LLVM module consisting of two functions as follow:
  16. //
  17. // int add1(int x) {
  18. // return x+1;
  19. // }
  20. //
  21. // int foo() {
  22. // return add1(10);
  23. // }
  24. //
  25. // then compile the module via JIT, then execute the `foo'
  26. // function and return result to a driver, i.e. to a "host program".
  27. //
  28. // Some remarks and questions:
  29. //
  30. // - could we invoke some code using noname functions too?
  31. // e.g. evaluate "foo()+foo()" without fears to introduce
  32. // conflict of temporary function name with some real
  33. // existing function name?
  34. //
  35. //===----------------------------------------------------------------------===//
  36. #include "llvm/ADT/STLExtras.h"
  37. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  38. #include "llvm/ExecutionEngine/GenericValue.h"
  39. #include "llvm/IR/Argument.h"
  40. #include "llvm/IR/BasicBlock.h"
  41. #include "llvm/IR/Constants.h"
  42. #include "llvm/IR/DerivedTypes.h"
  43. #include "llvm/IR/Function.h"
  44. #include "llvm/IR/IRBuilder.h"
  45. #include "llvm/IR/Instructions.h"
  46. #include "llvm/IR/LLVMContext.h"
  47. #include "llvm/IR/Module.h"
  48. #include "llvm/IR/Type.h"
  49. #include "llvm/Support/Casting.h"
  50. #include "llvm/Support/ManagedStatic.h"
  51. #include "llvm/Support/TargetSelect.h"
  52. #include "llvm/Support/raw_ostream.h"
  53. #include <algorithm>
  54. #include <cassert>
  55. #include <memory>
  56. #include <vector>
  57. using namespace llvm;
  58. int main() {
  59. InitializeNativeTarget();
  60. LLVMContext Context;
  61. // Create some module to put our function into it.
  62. std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
  63. Module *M = Owner.get();
  64. // Create the add1 function entry and insert this entry into module M. The
  65. // function will have a return type of "int" and take an argument of "int".
  66. Function *Add1F =
  67. cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
  68. Type::getInt32Ty(Context)));
  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. cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context)));
  90. // Add a basic block to the FooF function.
  91. BB = BasicBlock::Create(Context, "EntryBlock", FooF);
  92. // Tell the basic block builder to attach itself to the new basic block
  93. builder.SetInsertPoint(BB);
  94. // Get pointer to the constant `10'.
  95. Value *Ten = builder.getInt32(10);
  96. // Pass Ten to the call to Add1F
  97. CallInst *Add1CallRes = builder.CreateCall(Add1F, Ten);
  98. Add1CallRes->setTailCall(true);
  99. // Create the return instruction and add it to the basic block.
  100. builder.CreateRet(Add1CallRes);
  101. // Now we create the JIT.
  102. ExecutionEngine* EE = EngineBuilder(std::move(Owner)).create();
  103. outs() << "We just constructed this LLVM module:\n\n" << *M;
  104. outs() << "\n\nRunning foo: ";
  105. outs().flush();
  106. // Call the `foo' function with no arguments:
  107. std::vector<GenericValue> noargs;
  108. GenericValue gv = EE->runFunction(FooF, noargs);
  109. // Import result of execution:
  110. outs() << "Result: " << gv.IntVal << "\n";
  111. delete EE;
  112. llvm_shutdown();
  113. return 0;
  114. }