/JIT/llvm_fbuilder.h

http://unladen-swallow.googlecode.com/ · C Header · 425 lines · 194 code · 71 blank · 160 comment · 0 complexity · 675ffc2e55e1f790e2d63e1a4b3a3b5a MD5 · raw file

  1. // -*- C++ -*-
  2. #ifndef PYTHON_LLVM_FBUILDER_H
  3. #define PYTHON_LLVM_FBUILDER_H
  4. #ifndef __cplusplus
  5. #error This header expects to be included only in C++ source
  6. #endif
  7. #include "JIT/llvm_state.h"
  8. #include "JIT/PyTypeBuilder.h"
  9. #include "JIT/RuntimeFeedback.h"
  10. #include "Util/EventTimer.h"
  11. #include "llvm/ADT/SmallPtrSet.h"
  12. #include "llvm/ADT/SparseBitVector.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/Analysis/DebugInfo.h"
  15. #include "llvm/Constants.h"
  16. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  17. #include "llvm/GlobalVariable.h"
  18. #include "llvm/Type.h"
  19. #include "llvm/Support/IRBuilder.h"
  20. #include "llvm/Support/TargetFolder.h"
  21. #include <bitset>
  22. #include <string>
  23. struct PyCodeObject;
  24. struct PyGlobalLlvmData;
  25. namespace py {
  26. /// Helps the compiler build LLVM functions corresponding to Python
  27. /// functions. This class maintains all Value*s which depend on a
  28. /// single frame/code object. It also contains all functions that
  29. /// depend on these Values.
  30. class LlvmFunctionBuilder {
  31. LlvmFunctionBuilder(const LlvmFunctionBuilder &); // Not implemented.
  32. void operator=(const LlvmFunctionBuilder &); // Not implemented.
  33. public:
  34. LlvmFunctionBuilder(LlvmFunctionState *state, PyCodeObject *code);
  35. llvm::Function *function() { return function_; }
  36. typedef llvm::IRBuilder<true, llvm::TargetFolder> BuilderT;
  37. BuilderT& builder() { return builder_; }
  38. LlvmFunctionState *state() const { return this->state_; }
  39. PyCodeObject *code_object() const { return this->code_object_; }
  40. PyGlobalLlvmData *llvm_data() const { return this->llvm_data_; }
  41. llvm::LLVMContext& context() { return this->context_; }
  42. bool &uses_delete_fast() { return this->uses_delete_fast_; }
  43. std::vector<bool> &loads_optimized() { return this->loads_optimized_; }
  44. llvm::BasicBlock *unreachable_block() const
  45. {
  46. return this->unreachable_block_;
  47. }
  48. llvm::BasicBlock *unwind_block() const { return this->unwind_block_; }
  49. llvm::BasicBlock *do_return_block() const { return this->do_return_block_; }
  50. llvm::Value *unwind_reason_addr() const
  51. {
  52. return this->unwind_reason_addr_;
  53. }
  54. llvm::Value *retval_addr() const
  55. {
  56. return this->retval_addr_;
  57. }
  58. llvm::Value *num_blocks_addr() const
  59. {
  60. return this->num_blocks_addr_;
  61. }
  62. llvm::Value *blockstack_addr() const
  63. {
  64. return this->blockstack_addr_;
  65. }
  66. llvm::Value *stack_pointer_addr() const
  67. {
  68. return this->stack_pointer_addr_;
  69. }
  70. llvm::Value *f_lasti_addr() const { return this->f_lasti_addr_; }
  71. llvm::Value *stack_bottom() const { return this->stack_bottom_; }
  72. int stack_top() const { return this->stack_top_; }
  73. llvm::Value *freevars() const { return this->freevars_; }
  74. llvm::Value *frame() const { return this->frame_; }
  75. llvm::Value *globals() const { return this->globals_; }
  76. llvm::Value *builtins() const { return this->builtins_; }
  77. llvm::Value *fastlocals() const { return this->fastlocals_; }
  78. bool is_generator() const { return this->is_generator_; }
  79. llvm::Value *GetLocal(int i) const { return this->locals_[i]; }
  80. void UpdateStackInfo();
  81. bool Error() { return this->error_; }
  82. /// Sets the current instruction index. This is only put into the
  83. /// frame object when tracing.
  84. void SetLasti(int current_instruction_index);
  85. int GetLasti() const { return this->f_lasti_; }
  86. /// Sets the current line number being executed. This is used to
  87. /// make tracebacks correct and to get tracing to fire in the
  88. /// right places.
  89. void SetLineNumber(int line);
  90. /// Inserts a call to llvm.dbg.stoppoint.
  91. void SetDebugStopPoint(int line_number);
  92. /// This function fills the block that handles a backedge. Each
  93. /// backedge needs to check if it needs to handle signals or
  94. /// switch threads. If the backedge doesn't land at the start of
  95. /// a line, it also needs to update the line number and check
  96. /// whether line tracing has been turned on. This function leaves
  97. /// the insert point in a block with a terminator already added,
  98. /// so the caller should re-set the insert point.
  99. void FillBackedgeLanding(llvm::BasicBlock *backedge_landing,
  100. llvm::BasicBlock *target,
  101. bool to_start_of_line,
  102. int line_number);
  103. /// Sets the insert point to next_block, inserting an
  104. /// unconditional branch to there if the current block doesn't yet
  105. /// have a terminator instruction.
  106. void FallThroughTo(llvm::BasicBlock *next_block);
  107. /// Register callbacks that might invalidate native code based on the
  108. /// optimizations performed in the generated code.
  109. int FinishFunction();
  110. /// These two push or pop a value onto or off of the stack. The
  111. /// behavior is undefined if the Value's type isn't PyObject* or a
  112. /// subclass. These do no refcount operations, which means that
  113. /// Push() consumes a reference and gives ownership of it to the
  114. /// new value on the stack, and Pop() returns a pointer that owns
  115. /// a reference (which it got from the stack).
  116. /// Push/Pop are legacy methods to provide compatibility to existing
  117. /// opcode implementations. They should not be used any more.
  118. /// You must call Push only once per argument and Pop only once
  119. /// per result.
  120. void Push(llvm::Value *value);
  121. llvm::Value *Pop();
  122. /// Set the number of arguments the next opcode uses.
  123. /// This serves as a baseline for reading/writing to the stack.
  124. /// It also resets the stack pointer to it's base value.
  125. void SetOpcodeArguments(int amount);
  126. /// Sets the number of arguments the next opcode uses.
  127. /// Use this when the opcode needs to implement guards.
  128. /// GetOpcodeArg can be used as usual. Call BeginOpcodeImpl
  129. /// if the guard was successfully passed.
  130. void SetOpcodeArgsWithGuard(int amount);
  131. /// Changes the stack pointer after a successfully passed guard.
  132. void BeginOpcodeImpl();
  133. /// Retrieve an opcode argument. Must be called after SetOpcodeArguments.
  134. /// Can be called unlimited.
  135. /// Increasing the argument reads in the direction of stack growth.
  136. llvm::Value *GetOpcodeArg(int i);
  137. /// Sets the result of an opcode. Must be called after SetOpcodeArguments.
  138. /// Must only be called once per result per IR-codepath.
  139. void SetOpcodeResult(int i, llvm::Value *value);
  140. /// Normally it is not needed to specify the number of result values an
  141. /// opcode produces, as the stack top will be set to the correct value
  142. /// at the beginning of each opcode
  143. /// If you want to use SetOpcodeArguments/SetOpocodeArgsWithGuard twice
  144. /// in one opcode (e.g. to split the opcode in to sub-implementations)
  145. /// call FinishOpcodeImpl with the number of results from the previous
  146. /// opcode.
  147. void FinishOpcodeImpl(int amount);
  148. /// Takes a target stack pointer and pops values off the stack
  149. /// until it gets there, decref'ing as it goes.
  150. void PopAndDecrefTo(llvm::Value *target_stack_pointer);
  151. /// The PyFrameObject holds several values, like the block stack
  152. /// and stack pointer, that we store in allocas inside this
  153. /// function. When we suspend or resume a generator, or bail out
  154. /// to the interpreter, we need to transfer those values between
  155. /// the frame and the allocas.
  156. void CopyToFrameObject();
  157. void CopyFromFrameObject();
  158. /// We copy the function's locals into an LLVM alloca so that LLVM can
  159. /// better reason about them.
  160. void CopyLocalsFromFrameObject();
  161. /// Returns the difference between the current stack pointer and
  162. /// the base of the stack.
  163. llvm::Value *GetStackLevel();
  164. /// Get the runtime feedback for the current opcode (as set by SetLasti()).
  165. /// Opcodes with multiple feedback units should use the arg_index version
  166. /// to access individual units.
  167. const PyRuntimeFeedback *GetFeedback() const {
  168. return GetFeedback(0);
  169. }
  170. const PyRuntimeFeedback *GetFeedback(unsigned arg_index) const;
  171. const PyTypeObject *GetTypeFeedback(unsigned arg_index) const;
  172. // Look up a name in the function's names list, returning the
  173. // PyStringObject for the name_index.
  174. llvm::Value *LookupName(int name_index);
  175. /// Inserts a call that will print opcode_name and abort the
  176. /// program when it's reached.
  177. void DieForUndefinedOpcode(const char *opcode_name);
  178. /// How many parameters does the currently-compiling function have?
  179. int GetParamCount() const;
  180. // Emits code to decrement _Py_Ticker and handle signals and
  181. // thread-switching when it expires. Falls through to next_block (or a
  182. // new block if it's NULL) and leaves the insertion point there.
  183. void CheckPyTicker(llvm::BasicBlock *next_block = NULL);
  184. /// Marks the end of the function and inserts a return instruction.
  185. llvm::ReturnInst *CreateRet(llvm::Value *retval);
  186. // Returns an i1, true if value is a PyObject considered true.
  187. // Steals the reference to value.
  188. llvm::Value *IsPythonTrue(llvm::Value *value);
  189. /// During stack unwinding it may be necessary to jump back into
  190. /// the function to handle a finally or except block. Since LLVM
  191. /// doesn't allow us to directly store labels as data, we instead
  192. /// add the index->label mapping to a switch instruction and
  193. /// return the i32 for the index.
  194. llvm::ConstantInt *AddUnwindTarget(llvm::BasicBlock *target,
  195. int target_opindex);
  196. // Inserts a jump to the return block, returning retval. You
  197. // should _never_ call CreateRet directly from one of the opcode
  198. // handlers, since doing so would fail to unwind the stack.
  199. void Return(llvm::Value *retval);
  200. // Propagates an exception by jumping to the unwind block with an
  201. // appropriate unwind reason set.
  202. void PropagateException();
  203. // Set up a block preceding the bail-to-interpreter block.
  204. void CreateBailPoint(unsigned bail_idx, char reason);
  205. void CreateBailPoint(char reason) {
  206. CreateBailPoint(this->f_lasti_, reason);
  207. }
  208. // Set up a block preceding the bail-to-interpreter block.
  209. void CreateGuardBailPoint(unsigned bail_idx, char reason);
  210. void CreateGuardBailPoint(char reason) {
  211. CreateGuardBailPoint(this->f_lasti_, reason);
  212. }
  213. // Only for use in the constructor: Fills in the block that
  214. // handles bailing out of JITted code back to the interpreter
  215. // loop. Code jumping to this block must first:
  216. // 1) Set frame->f_lasti to the current opcode index.
  217. // 2) Set frame->f_bailed_from_llvm to a reason.
  218. void FillBailToInterpreterBlock();
  219. // Only for use in the constructor: Fills in the block that starts
  220. // propagating an exception. Jump to this block when you want to
  221. // add a traceback entry for the current line. Don't jump to this
  222. // block (and just set retval_addr_ and unwind_reason_addr_
  223. // directly) when you're re-raising an exception and you want to
  224. // use its traceback.
  225. void FillPropagateExceptionBlock();
  226. // Only for use in the constructor: Fills in the unwind block.
  227. void FillUnwindBlock();
  228. // Only for use in the constructor: Fills in the block that
  229. // actually handles returning from the function.
  230. void FillDoReturnBlock();
  231. // If 'value' represents NULL, propagates the exception.
  232. // Otherwise, falls through.
  233. void PropagateExceptionOnNull(llvm::Value *value);
  234. // If 'value' represents a negative integer, propagates the exception.
  235. // Otherwise, falls through.
  236. void PropagateExceptionOnNegative(llvm::Value *value);
  237. // If 'value' represents a non-zero integer, propagates the exception.
  238. // Otherwise, falls through.
  239. void PropagateExceptionOnNonZero(llvm::Value *value);
  240. /// Emits code to conditionally bail out to the interpreter loop
  241. /// if a line tracing function is installed. If the line tracing
  242. /// function is not installed, execution will continue at
  243. /// fallthrough_block. direction should be _PYFRAME_LINE_TRACE or
  244. /// _PYFRAME_BACKEDGE_TRACE.
  245. void MaybeCallLineTrace(llvm::BasicBlock *fallthrough_block,
  246. char direction);
  247. /// Emits code to conditionally bail out to the interpreter loop if a
  248. /// profiling function is installed. If a profiling function is not
  249. /// installed, execution will continue at fallthrough_block.
  250. void BailIfProfiling(llvm::BasicBlock *fallthrough_block);
  251. /// Return the BasicBlock we should jump to in order to bail to the
  252. /// interpreter.
  253. llvm::BasicBlock *GetBailBlock() const;
  254. /// Return the BasicBlock we should jump to in order to handle a Python
  255. /// exception.
  256. llvm::BasicBlock *GetExceptionBlock() const;
  257. void PushException();
  258. // Add a Type to the watch list.
  259. void WatchType(PyTypeObject *type);
  260. void WatchDict(int reason);
  261. // Return a i1 which is true when the use_jit field is set in the
  262. // code object
  263. llvm::Value *GetUseJitCond();
  264. void AddYieldResumeBB(llvm::ConstantInt *number, llvm::BasicBlock *block);
  265. private:
  266. // Stack pointer relative push and pop methods are for internal
  267. // use only.
  268. llvm::Value *PopRel();
  269. LlvmFunctionState *state_;
  270. PyGlobalLlvmData *const llvm_data_;
  271. // The code object is used for looking up peripheral information
  272. // about the function. It's not used to examine the bytecode
  273. // string.
  274. PyCodeObject *const code_object_;
  275. llvm::LLVMContext &context_;
  276. llvm::Module *const module_;
  277. llvm::Function *const function_;
  278. BuilderT &builder_;
  279. llvm::DIFactory &debug_info_;
  280. const llvm::DICompileUnit debug_compile_unit_;
  281. const llvm::DISubprogram debug_subprogram_;
  282. // The most recent index we've started emitting an instruction for.
  283. int f_lasti_;
  284. // Flags to indicate whether the code object is watching any of the
  285. // watchable dicts.
  286. std::bitset<NUM_WATCHING_REASONS> uses_watched_dicts_;
  287. // The following pointers hold values created in the function's
  288. // entry block. They're constant after construction.
  289. llvm::Value *frame_;
  290. // Address of code_object_->co_use_jit, used for guards.
  291. llvm::Value *use_jit_addr_;
  292. llvm::Value *tstate_;
  293. llvm::Value *stack_bottom_;
  294. llvm::Value *stack_pointer_addr_;
  295. // The tmp_stack_pointer is used when we need to have another
  296. // function update the stack pointer. Passing the stack pointer
  297. // directly would prevent mem2reg from working on it, so we copy
  298. // it to and from the tmp_stack_pointer around the call.
  299. llvm::Value *tmp_stack_pointer_addr_;
  300. llvm::Value *varnames_;
  301. llvm::Value *names_;
  302. llvm::Value *globals_;
  303. llvm::Value *builtins_;
  304. llvm::Value *fastlocals_;
  305. llvm::Value *freevars_;
  306. llvm::Value *f_lineno_addr_;
  307. llvm::Value *f_lasti_addr_;
  308. // These two fields correspond to the f_blockstack and f_iblock
  309. // fields in the frame object. They get explicitly copied back
  310. // and forth when the frame escapes.
  311. llvm::Value *blockstack_addr_;
  312. llvm::Value *num_blocks_addr_;
  313. // Expose the frame's locals to LLVM. We copy them in on function-entry,
  314. // copy them out on write. We use a separate alloca for each local
  315. // because LLVM's scalar replacement of aggregates pass doesn't handle
  316. // array allocas.
  317. std::vector<llvm::Value*> locals_;
  318. llvm::BasicBlock *unreachable_block_;
  319. // In generators, we use this switch to jump back to the most
  320. // recently executed yield instruction.
  321. llvm::SwitchInst *yield_resume_switch_;
  322. llvm::BasicBlock *bail_to_interpreter_block_;
  323. llvm::BasicBlock *propagate_exception_block_;
  324. llvm::BasicBlock *unwind_block_;
  325. llvm::Value *unwind_target_index_addr_;
  326. llvm::SparseBitVector<> existing_unwind_targets_;
  327. llvm::SwitchInst *unwind_target_switch_;
  328. // Stores one of the UNWIND_XXX constants defined at the top of
  329. // llvm_fbuilder.cc
  330. llvm::Value *unwind_reason_addr_;
  331. llvm::Value *exception_tb_;
  332. llvm::Value *exception_val_;
  333. llvm::Value *exception_exc_;
  334. llvm::BasicBlock *do_return_block_;
  335. llvm::Value *retval_addr_;
  336. llvm::SmallPtrSet<PyTypeObject*, 5> types_used_;
  337. // A stack that corresponds to LOAD_METHOD/CALL_METHOD pairs. For every
  338. // load, we push on a boolean for whether or not the load was optimized.
  339. // The call uses this value to decide whether to expect an extra "self"
  340. // argument. The stack is necessary if the user wrote code with nested
  341. // method calls, like this: f.foo(b.bar()).
  342. std::vector<bool> loads_optimized_;
  343. // Stores information about the stack top for every opcode
  344. std::vector<int> stack_info_;
  345. int stack_top_;
  346. // True if something went wrong and we need to stop compilation without
  347. // aborting the process. If this is true, a Python error has already
  348. // been set.
  349. bool error_;
  350. const bool is_generator_;
  351. bool uses_delete_fast_;
  352. };
  353. } // namespace py
  354. #endif // PYTHON_LLVM_FBUILDER_H