codon-jit 0.5.2__tar.gz → 0.5.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codon-jit
3
- Version: 0.5.2
3
+ Version: 0.5.3
4
4
  Summary: Codon JIT decorator
5
5
  Home-page: https://exaloop.io
6
6
  Author: Exaloop Inc.
@@ -0,0 +1,79 @@
1
+ To install:
2
+
3
+ ```bash
4
+ $ pip install .
5
+ ```
6
+
7
+ If Codon is installed in non-standard directory, please set `CODON_DIR` accordingly.
8
+
9
+ To use:
10
+
11
+ ```python
12
+ import codon
13
+
14
+ @codon.jit
15
+ def ...
16
+ ```
17
+
18
+ ## Compiler options
19
+
20
+ Configure the shared JIT before the first `@codon.jit`, `@codon.convert`, or
21
+ `codon.execute()` call:
22
+
23
+ ```python
24
+ import codon
25
+
26
+ codon.set_options(fastmath=True, pynum=False)
27
+
28
+ @codon.jit
29
+ def divide(value, divisor):
30
+ return value // divisor
31
+
32
+ assert divide(-3, 2) == -1
33
+ ```
34
+
35
+ Option names match the fields in `codon/compiler/options.h`:
36
+
37
+ | Type | Options |
38
+ | --- | --- |
39
+ | `bool` | `debug`, `pmempty`, `capture`, `native`, `pynum`, `noexc`, `fastmath`, `autopy`, `autofree`, `unordereddict` |
40
+ | `str` | `libdevice`, `gpuName`, `gpuFeat`, `gpuOutput`, `log`, `march`, `mcpu` |
41
+ | List of `str` | `plugins`, `defines`, `disabled`, `mattrs` |
42
+
43
+ Defaults match `Options`, except the JIT compiles in release mode (`debug=False`).
44
+ For example, `pynum=False` selects C numerical semantics, `fastmath=True` relaxes
45
+ floating-point semantics, and `unordereddict=True` selects unordered dictionaries.
46
+ `defines=["SIZE=64", "LABEL=str:example"]` supplies compile-time constants using
47
+ the same syntax as the CLI's `-D` flag. Plugins are loaded before initialization.
48
+ Logging controls the compiler's process-wide logger.
49
+
50
+ Repeated `set_options()` calls merge settings; list-valued options are replaced,
51
+ not appended. Values are copied, and invalid names or values leave the previous
52
+ configuration intact. Unknown options and invalid field types raise `ValueError`
53
+ (non-JSON-serializable Python values raise `TypeError`). Initialization failures
54
+ raise `JITError`.
55
+
56
+ Options cannot change after the first JIT use: `set_options()` then raises
57
+ `RuntimeError`. This avoids invalidating compiled functions or changing semantics
58
+ partway through a session. Options are preserved across the JIT's automatic
59
+ error-recovery resets. Internal execution-mode fields (`argv0`, `jit`, `test`,
60
+ `standalone`, `pyext`, and `ctor`) are not configurable through this API.
61
+
62
+ `debug=True` here controls compiler optimization/debug information. The existing
63
+ `@codon.jit(debug=...)` argument and `CODON_JIT_DEBUG` control diagnostic verbosity.
64
+
65
+ Independent low-level wrappers accept the same keyword options and allow updates
66
+ until their first `execute()` or `run_wrapper()` call:
67
+
68
+ ```python
69
+ wrapper = codon.JITWrapper(pynum=False)
70
+ wrapper.set_options(fastmath=True)
71
+ wrapper.execute("assert -3 // 2 == -1", "<example>", 1, False)
72
+ ```
73
+
74
+ The C bridge exposes `jit_validate_options(json)` and
75
+ `jit_init_with_options(name, json)`, using a JSON object with the same field names.
76
+ Both return `CJITResult`; errors are allocated strings that the caller must
77
+ `free()`. Successful initialization returns the JIT handle in `result`, owned by
78
+ the caller and released with `jit_exit()`. The original `jit_init(name)` remains
79
+ available with its existing defaults.
@@ -0,0 +1,9 @@
1
+ # Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
2
+
3
+ __all__ = [
4
+ "jit", "convert", "set_options", "JITError", "JITWrapper", "_jit_register_fn", "_jit"
5
+ ]
6
+
7
+ from .decorator import jit, convert, execute, set_options, JITError, JITWrapper, _jit_register_fn, _jit_callback_fn, _jit
8
+
9
+ __codon__ = False
@@ -1,6 +1,7 @@
1
1
  # Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
2
2
 
3
3
  from argparse import ArgumentError
4
+ from copy import deepcopy
4
5
  import ctypes
5
6
  import inspect
6
7
  import sys
@@ -146,9 +147,36 @@ def _codon_type(arg, **kwargs):
146
147
  def _codon_types(args, **kwargs):
147
148
  return tuple(_codon_type(arg, **kwargs) for arg in args)
148
149
 
150
+ _jit_options = {}
151
+ _jit_initialized = False
152
+ _jit_started = False
153
+
154
+
155
+ def set_options(**options):
156
+ """Configure the shared JIT before using jit, convert, or execute.
157
+
158
+ Keys match compiler Options fields, for example fastmath=True or pynum=False.
159
+ Calls merge settings. Invalid options leave the current configuration intact.
160
+ """
161
+ if _jit_started:
162
+ raise RuntimeError("JIT options must be set before the first JIT use")
163
+ options = deepcopy(options)
164
+ _jit.set_options(**options)
165
+ _jit_options.update(options)
166
+
167
+
149
168
  def _reset_jit():
150
- global _jit
151
- _jit = JITWrapper()
169
+ global _jit, _jit_initialized
170
+ _jit = JITWrapper(**_jit_options)
171
+ _jit_initialized = False
172
+ return _jit
173
+
174
+
175
+ def _get_jit():
176
+ global _jit_initialized, _jit_started
177
+ if _jit_initialized:
178
+ return _jit
179
+ _jit_started = True
152
180
  init_code = (
153
181
  "from internal.python import "
154
182
  "setup_decorator, PyTuple_GetItem, PyObject_GetAttrString\n"
@@ -159,6 +187,7 @@ def _reset_jit():
159
187
  if debug_override == 2:
160
188
  print(f"[jit_debug] execute:\n{init_code}", file=sys.stderr)
161
189
  _jit.execute(init_code, "", 0, int(debug_override > 0))
190
+ _jit_initialized = True
162
191
  return _jit
163
192
 
164
193
  _jit = _reset_jit()
@@ -232,7 +261,7 @@ def convert(t):
232
261
 
233
262
  if debug_override == 2:
234
263
  print(f"[jit_debug] execute:\n{code}", file=sys.stderr)
235
- _jit.execute(code, "", 0, int(debug_override > 0))
264
+ _get_jit().execute(code, "", 0, int(debug_override > 0))
236
265
  custom_conversions[t] = name
237
266
  return t
238
267
 
@@ -244,7 +273,7 @@ def _jit_register_fn(f, pyvars, debug):
244
273
  fn, fl = f.__code__.co_filename, f.__code__.co_firstlineno
245
274
  if debug == 2:
246
275
  print(f"[jit_debug] execute:\n{obj_str}", file=sys.stderr)
247
- _jit.execute(obj_str, fn, fl, int(debug > 0))
276
+ _get_jit().execute(obj_str, fn, fl, int(debug > 0))
248
277
  return obj_name
249
278
  except JITError:
250
279
  _reset_jit()
@@ -270,7 +299,7 @@ def _jit_callback_fn(fn,
270
299
  types = _codon_types(args, debug=debug, sample_size=sample_size)
271
300
  if debug > 0:
272
301
  print("[python] {}({})".format(obj_name, list(types)), file=sys.stderr)
273
- return _jit.run_wrapper(
302
+ return _get_jit().run_wrapper(
274
303
  obj_name, list(types), module, list(pyvars), args, int(debug > 0)
275
304
  )
276
305
  except JITError:
@@ -323,7 +352,7 @@ def execute(code, debug=0):
323
352
  try:
324
353
  if debug == 2:
325
354
  print(f"[jit_debug] execute:\n{code}", file=sys.stderr)
326
- _jit.execute(code, "<internal>", 0, int(debug))
355
+ _get_jit().execute(code, "<internal>", 0, int(debug))
327
356
  except JITError:
328
357
  _reset_jit()
329
358
  raise
@@ -8,6 +8,8 @@ cdef extern from "codon/compiler/jit_extern.h":
8
8
  char *error
9
9
 
10
10
  void *jit_init(char *name)
11
+ CJITResult jit_init_with_options(const char *name, const char *options)
12
+ CJITResult jit_validate_options(const char *options)
11
13
  void jit_exit(void *jit)
12
14
 
13
15
  cdef char *get_jit_library()
@@ -6,6 +6,7 @@
6
6
  # cython: c_string_encoding=utf8
7
7
 
8
8
  cimport codon.jit
9
+ import json
9
10
  from libc.stdlib cimport malloc, calloc, free
10
11
  from libc.string cimport strcpy
11
12
  from libc.stdint cimport int32_t, uint8_t
@@ -26,14 +27,37 @@ cdef str get_free_str(char *s):
26
27
 
27
28
  cdef class JITWrapper:
28
29
  cdef void* jit
30
+ cdef bytes options
29
31
 
30
- def __cinit__(self):
31
- self.jit = codon.jit.jit_init(b"codon jit")
32
+ def __cinit__(self, **options):
33
+ self.jit = NULL
34
+ self.options = b"{}"
35
+ self.set_options(**options)
32
36
 
33
37
  def __dealloc__(self):
34
38
  codon.jit.jit_exit(self.jit)
35
39
 
40
+ def set_options(self, **options):
41
+ """Set compiler options before this JIT's first execution."""
42
+ if self.jit is not NULL:
43
+ raise RuntimeError("JIT options must be set before the first execution")
44
+ settings = json.loads(self.options)
45
+ settings.update(options)
46
+ cdef bytes encoded = json.dumps(settings).encode('utf-8')
47
+ result = codon.jit.jit_validate_options(encoded)
48
+ if result.error is not NULL:
49
+ raise ValueError(get_free_str(result.error))
50
+ self.options = encoded
51
+
52
+ cdef initialize(self):
53
+ if self.jit is NULL:
54
+ result = codon.jit.jit_init_with_options(b"codon jit", self.options)
55
+ if result.error is not NULL:
56
+ raise JITError(get_free_str(result.error))
57
+ self.jit = result.result
58
+
36
59
  def execute(self, code: str, filename: str, fileno: int, debug) -> str:
60
+ self.initialize()
37
61
  result = codon.jit.jit_execute_safe(
38
62
  self.jit, code.encode('utf-8'), filename.encode('utf-8'), fileno, <uint8_t>debug
39
63
  )
@@ -44,6 +68,7 @@ cdef class JITWrapper:
44
68
  raise JITError(msg)
45
69
 
46
70
  def run_wrapper(self, name: str, types: list[str], module: str, pyvars: list[str], args, debug) -> object:
71
+ self.initialize()
47
72
  cdef char** c_types = <char**>calloc(len(types), sizeof(char*))
48
73
  cdef char** c_pyvars = <char**>calloc(len(pyvars), sizeof(char*))
49
74
  if not c_types or not c_pyvars:
@@ -0,0 +1,5 @@
1
+ __version__ = "0.5.3"
2
+ CODON_VERSION = "0.20.3"
3
+ CODON_VERSION_MAJOR = 0
4
+ CODON_VERSION_MINOR = 20
5
+ CODON_VERSION_PATCH = 3
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codon-jit
3
- Version: 0.5.2
3
+ Version: 0.5.3
4
4
  Summary: Codon JIT decorator
5
5
  Home-page: https://exaloop.io
6
6
  Author: Exaloop Inc.
codon_jit-0.5.2/README.md DELETED
@@ -1,16 +0,0 @@
1
- To install:
2
-
3
- ```bash
4
- $ pip install .
5
- ```
6
-
7
- If Codon is installed in non-standard directory, please set `CODON_DIR` accordingly.
8
-
9
- To use:
10
-
11
- ```python
12
- import codon
13
-
14
- @codon.jit
15
- def ...
16
- ```
@@ -1,9 +0,0 @@
1
- # Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
2
-
3
- __all__ = [
4
- "jit", "convert", "JITError", "JITWrapper", "_jit_register_fn", "_jit"
5
- ]
6
-
7
- from .decorator import jit, convert, execute, JITError, JITWrapper, _jit_register_fn, _jit_callback_fn, _jit
8
-
9
- __codon__ = False
@@ -1,5 +0,0 @@
1
- __version__ = "0.5.2"
2
- CODON_VERSION = "0.20.2"
3
- CODON_VERSION_MAJOR = 0
4
- CODON_VERSION_MINOR = 20
5
- CODON_VERSION_PATCH = 2
File without changes
File without changes
File without changes
File without changes