-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathoptions.py
108 lines (87 loc) · 2.93 KB
/
options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Target Options
"""
import operator
from numba.core import config, utils
from numba.core.targetconfig import TargetConfig, Option
class TargetOptions:
"""Target options maps user options from decorators to the
``numba.core.compiler.Flags`` used by lowering and target context.
"""
class Mapping:
def __init__(self, flag_name, apply=lambda x: x):
self.flag_name = flag_name
self.apply = apply
def finalize(self, flags, options):
"""Subclasses can override this method to make target specific
customizations of default flags.
Parameters
----------
flags : Flags
options : dict
"""
pass
@classmethod
def parse_as_flags(cls, flags, options):
"""Parse target options defined in ``options`` and set ``flags``
accordingly.
Parameters
----------
flags : Flags
options : dict
"""
opt = cls()
opt._apply(flags, options)
opt.finalize(flags, options)
return flags
def _apply(self, flags, options):
# Find all Mapping instances in the class
mappings = {}
cls = type(self)
for k in dir(cls):
v = getattr(cls, k)
if isinstance(v, cls.Mapping):
mappings[k] = v
used = set()
for k, mapping in mappings.items():
if k in options:
v = mapping.apply(options[k])
setattr(flags, mapping.flag_name, v)
used.add(k)
unused = set(options) - used
if unused:
# Unread options?
m = (f"Unrecognized options: {unused}. "
f"Known options are {mappings.keys()}")
raise KeyError(m)
_mapping = TargetOptions.Mapping
class DefaultOptions:
"""Defines how user-level target options are mapped to the target flags.
"""
nopython = _mapping("enable_pyobject", operator.not_)
forceobj = _mapping("force_pyobject")
looplift = _mapping("enable_looplift")
_nrt = _mapping("nrt")
debug = _mapping("debuginfo")
boundscheck = _mapping("boundscheck")
nogil = _mapping("release_gil")
writable_args = _mapping("writable_args")
no_rewrites = _mapping("no_rewrites")
no_cpython_wrapper = _mapping("no_cpython_wrapper")
no_cfunc_wrapper = _mapping("no_cfunc_wrapper")
parallel = _mapping("auto_parallel")
fastmath = _mapping("fastmath")
error_model = _mapping("error_model")
inline = _mapping("inline")
forceinline = _mapping("forceinline")
_dbg_extend_lifetimes = _mapping("dbg_extend_lifetimes")
_dbg_optnone = _mapping("dbg_optnone")
def include_default_options(*args):
"""Returns a mixin class with a subset of the options
Parameters
----------
*args : str
Option names to include.
"""
glbs = {k: getattr(DefaultOptions, k) for k in args}
return type("OptionMixins", (), glbs)