/Objects/_llvmfunctionobject.cc

http://unladen-swallow.googlecode.com/ · C++ · 354 lines · 274 code · 39 blank · 41 comment · 28 complexity · b462ede15959cbea5b7b82259526466e MD5 · raw file

  1. // Definition of _llvmfunction, the llvm::Function wrapper.
  2. #include "Python.h"
  3. #include "_llvmfunctionobject.h"
  4. #include "frameobject.h"
  5. #include "structmember.h"
  6. #include "JIT/global_llvm_data.h"
  7. #include "Util/Stats.h"
  8. #include "llvm/BasicBlock.h"
  9. #include "llvm/Constants.h"
  10. #include "llvm/Function.h"
  11. #include "llvm/Instructions.h"
  12. #include "llvm/Module.h"
  13. #include "llvm/ADT/SmallPtrSet.h"
  14. #include "llvm/CodeGen/MachineCodeInfo.h"
  15. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  16. #include "llvm/Support/Casting.h"
  17. #include "llvm/Support/ManagedStatic.h"
  18. #include "llvm/Support/ValueHandle.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <sstream>
  21. #include <string>
  22. #include <vector>
  23. using llvm::BasicBlock;
  24. using llvm::Function;
  25. using llvm::Type;
  26. using llvm::Value;
  27. struct _LlvmFunction {
  28. // This should be an AssertingVH, but that runs into problems on
  29. // shutdown, where the Module is destroyed without destroying all
  30. // code objects first.
  31. Function *lf_function;
  32. };
  33. #ifdef Py_WITH_INSTRUMENTATION
  34. // Collect statistics about the number of lines of LLVM IR we're writing,
  35. // and the amount of native code that translates to. Even if we're not changing
  36. // the amount of generated native code, reducing the number of LLVM IR lines
  37. // helps compilation time.
  38. class NativeSizeStats : public DataVectorStats<size_t> {
  39. public:
  40. NativeSizeStats() : DataVectorStats<size_t>("Native code size in bytes") {}
  41. };
  42. class LlvmIrSizeStats : public DataVectorStats<size_t> {
  43. public:
  44. LlvmIrSizeStats() : DataVectorStats<size_t>("LLVM IR size in lines") {}
  45. };
  46. // Count the number of non-blank lines of LLVM IR for the given function.
  47. static size_t
  48. count_ir_lines(llvm::Function *const function)
  49. {
  50. size_t result = 1; // Function's 'define' line.
  51. for (llvm::Function::iterator bb = function->begin(),
  52. bb_end = function->end(); bb != bb_end; ++bb) {
  53. ++result; // 'bb_name:' line.
  54. for (llvm::BasicBlock::iterator inst = bb->begin(),
  55. inst_end = bb->end(); inst != inst_end; ++inst) {
  56. ++result;
  57. }
  58. }
  59. return result;
  60. }
  61. static llvm::ManagedStatic<NativeSizeStats> native_size_stats;
  62. static llvm::ManagedStatic<LlvmIrSizeStats> llvm_ir_size_stats;
  63. #endif // Py_WITH_INSTRUMENTATION
  64. _LlvmFunction *
  65. _LlvmFunction_New(void *llvm_function)
  66. {
  67. llvm::Function *typed_function = (llvm::Function*)llvm_function;
  68. _LlvmFunction *wrapper = new _LlvmFunction();
  69. wrapper->lf_function = typed_function;
  70. return wrapper;
  71. }
  72. void
  73. _LlvmFunction_Dealloc(_LlvmFunction *functionobj)
  74. {
  75. llvm::Function *function = functionobj->lf_function;
  76. // Clear the AssertingVH to avoid crashing when we delete the function.
  77. functionobj->lf_function = NULL;
  78. // Allow global optimizations to destroy the function.
  79. function->setLinkage(llvm::GlobalValue::InternalLinkage);
  80. if (function->use_empty()) {
  81. // Delete the function if it's already unused.
  82. function->eraseFromParent();
  83. }
  84. delete functionobj;
  85. }
  86. // Deletes most of the contents of function but keeps all references
  87. // to global variables so they don't get destroyed by globaldce.
  88. static void
  89. clear_body(llvm::Function *function)
  90. {
  91. // Gather all the pieces of *function that could be or use globals.
  92. llvm::SmallPtrSet<Value*, 8> globals;
  93. // For all basic blocks...
  94. for (Function::iterator BB = function->begin(), E = function->end();
  95. BB != E; ++BB)
  96. // For all instructions...
  97. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  98. // For all operands...
  99. for (llvm::User::op_iterator U = I->op_begin(), E = I->op_end();
  100. U != E; ++U) {
  101. // We could recursively iterate through the Constants
  102. // too to find their component GlobalValues, but since
  103. // Constants are immortal anyway that doesn't seem
  104. // worth it. GlobalValues are Constants, so this
  105. // picks them up too.
  106. if (llvm::isa<llvm::Constant>(*U))
  107. globals.insert(*U);
  108. }
  109. const std::vector<Value*> globals_vect(globals.begin(), globals.end());
  110. // Clear out the function now that we know what it uses.
  111. function->deleteBody();
  112. BasicBlock *entry =
  113. BasicBlock::Create(function->getContext(), "", function);
  114. // Build a single "call void undef(all_constants)" instruction to
  115. // keep a use of all the constants.
  116. std::vector<const Type*> types;
  117. types.reserve(globals_vect.size());
  118. for (std::vector<Value*>::const_iterator C = globals_vect.begin(),
  119. E = globals_vect.end(); C != E; ++C)
  120. types.push_back((*C)->getType());
  121. const llvm::FunctionType *user_t = llvm::FunctionType::get(
  122. Type::getVoidTy(function->getContext()), types, false);
  123. llvm::CallInst::Create(
  124. llvm::UndefValue::get(llvm::PointerType::getUnqual(user_t)),
  125. globals_vect.begin(), globals_vect.end(), "", entry);
  126. }
  127. PyEvalFrameFunction
  128. _LlvmFunction_Jit(_LlvmFunction *function_obj)
  129. {
  130. llvm::Function *function = (llvm::Function *)function_obj->lf_function;
  131. PyGlobalLlvmData *global_llvm_data =
  132. PyThreadState_GET()->interp->global_llvm_data;
  133. llvm::ExecutionEngine *engine = global_llvm_data->getExecutionEngine();
  134. PyEvalFrameFunction native_func;
  135. #ifdef Py_WITH_INSTRUMENTATION
  136. llvm::MachineCodeInfo code_info;
  137. engine->runJITOnFunction(function, &code_info);
  138. native_size_stats->RecordDataPoint(code_info.size());
  139. size_t llvm_ir_lines = count_ir_lines(function);
  140. llvm_ir_size_stats->RecordDataPoint(llvm_ir_lines);
  141. // TODO(jyasskin): code_info.address() doesn't work for some reason.
  142. void *func = engine->getPointerToGlobalIfAvailable(function);
  143. assert(func && "function not installed in the globals");
  144. native_func = (PyEvalFrameFunction)func;
  145. #else
  146. native_func = (PyEvalFrameFunction)engine->getPointerToFunction(function);
  147. #endif
  148. // Clear the function body to reduce memory usage. This means we'll
  149. // need to re-compile the bytecode to IR and reoptimize it again, if we
  150. // need it again.
  151. clear_body(function);
  152. return native_func;
  153. }
  154. int
  155. _LlvmFunction_Optimize(PyGlobalLlvmData *global_data,
  156. _LlvmFunction *llvm_function,
  157. int level)
  158. {
  159. return global_data->Optimize(*llvm_function->lf_function, level);
  160. }
  161. // Python-level wrapper.
  162. struct PyLlvmFunctionObject {
  163. public:
  164. PyObject_HEAD
  165. PyCodeObject *code_object; // Hold a reference to the PyCodeObject.
  166. };
  167. PyObject *
  168. _PyLlvmFunction_FromCodeObject(PyObject *co)
  169. {
  170. PyLlvmFunctionObject *result =
  171. PyObject_NEW(PyLlvmFunctionObject, &PyLlvmFunction_Type);
  172. if (result == NULL) {
  173. return NULL;
  174. }
  175. Py_INCREF(co);
  176. result->code_object = (PyCodeObject *)co;
  177. return (PyObject *)result;
  178. }
  179. static llvm::Function *
  180. _PyLlvmFunction_GetFunction(PyLlvmFunctionObject *llvm_function)
  181. {
  182. PyCodeObject *code = llvm_function->code_object;
  183. return (llvm::Function *)code->co_llvm_function->lf_function;
  184. }
  185. PyDoc_STRVAR(llvmfunction_doc,
  186. "_llvmfunction()\n\
  187. \n\
  188. A wrapper around an llvm::Function object. Can only be created from\n\
  189. existing _llvmmodule objects.");
  190. static void
  191. llvmfunction_dealloc(PyLlvmFunctionObject *functionobj)
  192. {
  193. Py_DECREF(functionobj->code_object);
  194. }
  195. static _LlvmFunction *
  196. recompile(PyLlvmFunctionObject *functionobj)
  197. {
  198. // We clear out all llvm::Functions after emitting machine code for
  199. // them. Compile the code object back to IR, then throw that IR away. We
  200. // assume that people aren't printing out code objects in tight loops.
  201. PyCodeObject *code = functionobj->code_object;
  202. _LlvmFunction *cur_function = code->co_llvm_function;
  203. int cur_opt_level = code->co_optimization;
  204. // Null these out to trick _PyCode_ToOptimizedLlvmIr() into recompiling
  205. // this function, then restore the original values when we're done.
  206. // TODO(collinwinter): this approach is suboptimal.
  207. code->co_llvm_function = NULL;
  208. code->co_optimization = 0;
  209. int ret = _PyCode_ToOptimizedLlvmIr(code, cur_opt_level);
  210. _LlvmFunction *new_function = code->co_llvm_function;
  211. code->co_llvm_function = cur_function;
  212. code->co_optimization = cur_opt_level;
  213. // The only way we could have rejected compilation is if the code
  214. // object changed. I don't know how this could happen, but Python has
  215. // surprised me before.
  216. if (ret == 1) { // Compilation rejected.
  217. PyErr_BadInternalCall();
  218. return NULL;
  219. }
  220. else if (ret == -1) // Error during compilation.
  221. return NULL;
  222. return new_function;
  223. }
  224. static PyObject *
  225. llvmfunction_str(PyLlvmFunctionObject *function_obj)
  226. {
  227. std::string result;
  228. llvm::raw_string_ostream wrapper(result);
  229. _LlvmFunction *new_function = recompile(function_obj);
  230. if (new_function == NULL) {
  231. return NULL;
  232. }
  233. llvm::Function *func = (llvm::Function *)new_function->lf_function;
  234. func->print(wrapper);
  235. _LlvmFunction_Dealloc(new_function);
  236. wrapper.flush();
  237. return PyString_FromStringAndSize(result.data(), result.size());
  238. }
  239. static PyObject *
  240. func_view_cfg(PyLlvmFunctionObject *function_obj)
  241. {
  242. _LlvmFunction *new_function = recompile(function_obj);
  243. if (new_function == NULL) {
  244. return NULL;
  245. }
  246. llvm::Function *func = (llvm::Function *)new_function->lf_function;
  247. func->viewCFG();
  248. _LlvmFunction_Dealloc(new_function);
  249. Py_RETURN_NONE;
  250. }
  251. static PyObject *
  252. func_get_module(PyLlvmFunctionObject *op)
  253. {
  254. llvm::Module *module = _PyLlvmFunction_GetFunction(op)->getParent();
  255. if (module == NULL) {
  256. PyErr_BadInternalCall();
  257. return NULL;
  258. }
  259. std::string result;
  260. llvm::raw_string_ostream wrapper(result);
  261. module->print(wrapper, NULL /* No extra annotations in the output */);
  262. wrapper.flush();
  263. return PyString_FromStringAndSize(result.data(),
  264. result.size());
  265. }
  266. static PyMethodDef llvmfunction_methods[] = {
  267. {"view_cfg", (PyCFunction)func_view_cfg, METH_NOARGS,
  268. "View the CFG for this function."},
  269. {NULL, NULL, 0, NULL} /* Sentinel */
  270. };
  271. static PyGetSetDef llvmfunction_getsetlist[] = {
  272. {"module", (getter)func_get_module, NULL},
  273. {NULL, NULL, NULL},
  274. };
  275. // PyType_Ready is called on this in global_llvm_data.cc:_PyLlvm_Init().
  276. PyTypeObject PyLlvmFunction_Type = {
  277. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  278. "_llvmfunction",
  279. sizeof(PyLlvmFunctionObject),
  280. 0,
  281. (destructor)llvmfunction_dealloc, /* tp_dealloc */
  282. 0, /* tp_print */
  283. 0, /* tp_getattr */
  284. 0, /* tp_setattr */
  285. 0, /* tp_compare */
  286. 0, /* tp_repr */
  287. 0, /* tp_as_number */
  288. 0, /* tp_as_sequence */
  289. 0, /* tp_as_mapping */
  290. 0, /* tp_hash */
  291. 0, /* tp_call */
  292. (reprfunc)llvmfunction_str, /* tp_str */
  293. PyObject_GenericGetAttr, /* tp_getattro */
  294. 0, /* tp_setattro */
  295. 0, /* tp_as_buffer */
  296. Py_TPFLAGS_DEFAULT, /* tp_flags */
  297. llvmfunction_doc, /* tp_doc */
  298. 0, /* tp_traverse */
  299. 0, /* tp_clear */
  300. 0, /* tp_richcompare */
  301. 0, /* tp_weaklistoffset */
  302. 0, /* tp_iter */
  303. 0, /* tp_iternext */
  304. llvmfunction_methods, /* tp_methods */
  305. 0, /* tp_members */
  306. llvmfunction_getsetlist, /* tp_getset */
  307. 0, /* tp_base */
  308. 0, /* tp_dict */
  309. 0, /* tp_descr_get */
  310. 0, /* tp_descr_set */
  311. 0, /* tp_dictoffset */
  312. 0, /* tp_init */
  313. 0, /* tp_alloc */
  314. 0, /* tp_new */
  315. };