/Modules/_llvm.c

http://unladen-swallow.googlecode.com/ · C · 207 lines · 171 code · 27 blank · 9 comment · 28 complexity · d971897c3e0df1b18e5a3d3d4c2a6e3f MD5 · raw file

  1. /* _llvm module */
  2. /*
  3. This module provides a way to get at the minimal LLVM wrapper
  4. types. It's not intended to be a full LLVM interface. For that, use
  5. LLVM-py.
  6. */
  7. #include "Python.h"
  8. #include "_llvmfunctionobject.h"
  9. #include "JIT/global_llvm_data_fwd.h"
  10. #include "JIT/llvm_compile.h"
  11. #include "JIT/RuntimeFeedback_fwd.h"
  12. PyDoc_STRVAR(llvm_module_doc,
  13. "Defines thin wrappers around fundamental LLVM types.");
  14. PyDoc_STRVAR(setdebug_doc,
  15. "set_debug(bool). Sets LLVM debug output on or off.");
  16. static PyObject *
  17. llvm_setdebug(PyObject *self, PyObject *on_obj)
  18. {
  19. int on = PyObject_IsTrue(on_obj);
  20. if (on == -1) /* Error. */
  21. return NULL;
  22. if (!PyLlvm_SetDebug(on)) {
  23. PyErr_SetString(PyExc_ValueError, "llvm debugging not available");
  24. return NULL;
  25. }
  26. Py_RETURN_NONE;
  27. }
  28. PyDoc_STRVAR(llvm_compile_doc,
  29. "compile(code, optimization_level) -> llvm_function\n\
  30. \n\
  31. Compile a code object to an llvm_function object at the given\n\
  32. optimization level.");
  33. static PyObject *
  34. llvm_compile(PyObject *self, PyObject *args)
  35. {
  36. PyObject *obj;
  37. PyCodeObject *code;
  38. long opt_level;
  39. if (!PyArg_ParseTuple(args, "O!l:compile",
  40. &PyCode_Type, &obj, &opt_level))
  41. return NULL;
  42. if (opt_level < -1 || opt_level > Py_MAX_LLVM_OPT_LEVEL) {
  43. PyErr_SetString(PyExc_ValueError, "invalid optimization level");
  44. return NULL;
  45. }
  46. code = (PyCodeObject *)obj;
  47. if (code->co_llvm_function)
  48. _LlvmFunction_Dealloc(code->co_llvm_function);
  49. code->co_llvm_function = _PyCode_ToLlvmIr(code);
  50. if (code->co_llvm_function == NULL)
  51. return NULL;
  52. if (code->co_optimization < opt_level &&
  53. PyGlobalLlvmData_Optimize(PyGlobalLlvmData_GET(),
  54. code->co_llvm_function, opt_level) < 0) {
  55. PyErr_Format(PyExc_ValueError,
  56. "Failed to optimize to level %ld", opt_level);
  57. _LlvmFunction_Dealloc(code->co_llvm_function);
  58. return NULL;
  59. }
  60. return _PyLlvmFunction_FromCodeObject((PyObject *)code);
  61. }
  62. PyDoc_STRVAR(llvm_clear_feedback_doc,
  63. "clear_feedback(func)\n\
  64. \n\
  65. Clear the runtime feedback collected for the given function.");
  66. static PyObject *
  67. llvm_clear_feedback(PyObject *self, PyObject *obj)
  68. {
  69. PyCodeObject *code;
  70. if (PyFunction_Check(obj)) {
  71. PyFunctionObject *func = (PyFunctionObject *)obj;
  72. code = (PyCodeObject *)func->func_code;
  73. }
  74. else if (PyMethod_Check(obj)) {
  75. /* Methods contain other callable objects, including, potentially other
  76. methods. */
  77. return llvm_clear_feedback(self, ((PyMethodObject *)obj)->im_func);
  78. }
  79. else if (PyCode_Check(obj)) {
  80. code = (PyCodeObject *)obj;
  81. }
  82. else if (PyCFunction_Check(obj)) { /* No feedback; this is a no-op. */
  83. Py_RETURN_NONE;
  84. }
  85. else {
  86. PyErr_Format(PyExc_TypeError,
  87. "cannot clear feedback for %.100s objects",
  88. Py_TYPE(obj)->tp_name);
  89. return NULL;
  90. }
  91. if (code->co_runtime_feedback)
  92. PyFeedbackMap_Clear(code->co_runtime_feedback);
  93. Py_RETURN_NONE;
  94. }
  95. PyDoc_STRVAR(llvm_set_jit_control_doc,
  96. "set_jit_control(string)\n\
  97. \n\
  98. Set the JIT control mode. Valid values are 'always', 'never', and 'whenhot'.");
  99. static PyObject *
  100. llvm_set_jit_control(PyObject *self, PyObject *flag_obj)
  101. {
  102. const char *flag_str;
  103. if (!PyString_Check(flag_obj)) {
  104. PyErr_Format(PyExc_TypeError,
  105. "expected str, not %.100s object",
  106. Py_TYPE(flag_obj)->tp_name);
  107. return NULL;
  108. }
  109. flag_str = PyString_AsString(flag_obj);
  110. if (flag_str == NULL)
  111. return NULL;
  112. if (Py_JitControlStrToEnum(flag_str, &Py_JitControl) < 0) {
  113. PyErr_Format(PyExc_ValueError,
  114. "invalid jit control value: \"%s\"", flag_str);
  115. return NULL;
  116. }
  117. Py_RETURN_NONE;
  118. }
  119. PyDoc_STRVAR(llvm_get_jit_control_doc,
  120. "get_jit_control() -> string\n\
  121. \n\
  122. Returns the current value for the Py_JitControl flag. This may disagree\n\
  123. with sys.flags.jit_control because the sys.flags structure is immutable and\n\
  124. it is initialized at load time, so it reflects the value passed to the\n\
  125. program on the command line.");
  126. static PyObject *
  127. llvm_get_jit_control(PyObject *self)
  128. {
  129. const char *flag_str = Py_JitControlEnumToStr(Py_JitControl);
  130. assert(flag_str != NULL && "Current JIT control value was invalid!");
  131. return PyString_FromString(flag_str);
  132. }
  133. PyDoc_STRVAR(llvm_get_hotness_threshold_doc,
  134. "get_hotness_threshold() -> long\n\
  135. \n\
  136. Return the threshold for co_hotness before the code is 'hot'.");
  137. static PyObject *
  138. llvm_get_hotness_threshold(PyObject *self)
  139. {
  140. return PyInt_FromLong(PY_HOTNESS_THRESHOLD);
  141. }
  142. PyDoc_STRVAR(llvm_collect_unused_globals_doc,
  143. "collect_unused_globals()\n\
  144. \n\
  145. Collect any unused LLVM global variables that may be holding references to\n\
  146. Python objects.");
  147. static PyObject *
  148. llvm_collect_unused_globals(PyObject *self)
  149. {
  150. PyGlobalLlvmData_CollectUnusedGlobals(PyGlobalLlvmData_GET());
  151. Py_RETURN_NONE;
  152. }
  153. static struct PyMethodDef llvm_methods[] = {
  154. {"set_debug", (PyCFunction)llvm_setdebug, METH_O, setdebug_doc},
  155. {"compile", llvm_compile, METH_VARARGS, llvm_compile_doc},
  156. {"clear_feedback", (PyCFunction)llvm_clear_feedback, METH_O,
  157. llvm_clear_feedback_doc},
  158. {"get_jit_control", (PyCFunction)llvm_get_jit_control, METH_NOARGS,
  159. llvm_get_jit_control_doc},
  160. {"set_jit_control", (PyCFunction)llvm_set_jit_control, METH_O,
  161. llvm_set_jit_control_doc},
  162. {"get_hotness_threshold", (PyCFunction)llvm_get_hotness_threshold,
  163. METH_NOARGS, llvm_get_hotness_threshold_doc},
  164. {"collect_unused_globals", (PyCFunction)llvm_collect_unused_globals,
  165. METH_NOARGS, llvm_collect_unused_globals_doc},
  166. { NULL, NULL }
  167. };
  168. PyMODINIT_FUNC
  169. init_llvm(void)
  170. {
  171. PyObject *module;
  172. /* Create the module and add the functions */
  173. module = Py_InitModule3("_llvm", llvm_methods, llvm_module_doc);
  174. if (module == NULL)
  175. return;
  176. Py_INCREF(&PyLlvmFunction_Type);
  177. if (PyModule_AddObject(module, "_function",
  178. (PyObject *)&PyLlvmFunction_Type))
  179. return;
  180. }