Skip to content

registry

Classes:

Functions:

RuntimeFeatureRegistry

RuntimeFeatureRegistry()

Methods:

Source code in packages/irx/src/irx/builder/runtime/registry.py
42
43
44
45
46
def __init__(self) -> None:
    """
    title: Initialize RuntimeFeatureRegistry.
    """
    self._features: dict[str, RuntimeFeature] = {}

get

get(name: str) -> RuntimeFeature
Source code in packages/irx/src/irx/builder/runtime/registry.py
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
def get(self, name: str) -> RuntimeFeature:
    """
    title: Return a runtime feature by name.
    parameters:
      name:
        type: str
    returns:
      type: RuntimeFeature
    """
    try:
        return self._features[name]
    except KeyError as exc:
        available = self.names()
        raise RuntimeFeatureError(
            Diagnostic(
                message=f"runtime feature '{name}' is not registered",
                code=DiagnosticCodes.RUNTIME_FEATURE_UNKNOWN,
                phase="runtime",
                notes=(
                    f"available runtime features: {', '.join(available)}",
                )
                if available
                else (),
                cause=KeyError(name),
            )
        ) from exc

names

names() -> tuple[str, ...]
Source code in packages/irx/src/irx/builder/runtime/registry.py
88
89
90
91
92
93
94
def names(self) -> tuple[str, ...]:
    """
    title: Return the registered runtime feature names.
    returns:
      type: tuple[str, Ellipsis]
    """
    return tuple(sorted(self._features))

register

register(feature: RuntimeFeature) -> None
Source code in packages/irx/src/irx/builder/runtime/registry.py
48
49
50
51
52
53
54
55
56
57
58
59
def register(self, feature: RuntimeFeature) -> None:
    """
    title: Register a runtime feature by name.
    parameters:
      feature:
        type: RuntimeFeature
    """
    if feature.name in self._features:
        raise ValueError(
            f"Runtime feature '{feature.name}' already exists"
        )
    self._features[feature.name] = feature

RuntimeFeatureState

RuntimeFeatureState(
    owner: "VisitorProtocol",
    registry: RuntimeFeatureRegistry,
    active_features: Iterable[str] | None = None,
)

Methods:

Source code in packages/irx/src/irx/builder/runtime/registry.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def __init__(
    self,
    owner: "VisitorProtocol",
    registry: RuntimeFeatureRegistry,
    active_features: Iterable[str] | None = None,
) -> None:
    """
    title: Initialize RuntimeFeatureState.
    parameters:
      owner:
        type: VisitorProtocol
      registry:
        type: RuntimeFeatureRegistry
      active_features:
        type: Iterable[str] | None
    """
    self._owner = owner
    self._registry = registry
    self._active_features: set[str] = set()
    self._declared_symbols: dict[tuple[str, str], ir.Function] = {}

    if active_features is None:
        return

    for feature_name in active_features:
        self.activate(feature_name)

activate

activate(feature_name: str) -> None
Source code in packages/irx/src/irx/builder/runtime/registry.py
144
145
146
147
148
149
150
151
152
def activate(self, feature_name: str) -> None:
    """
    title: Activate a runtime feature for the current module.
    parameters:
      feature_name:
        type: str
    """
    self._registry.get(feature_name)
    self._active_features.add(feature_name)

active_feature_names

active_feature_names() -> tuple[str, ...]
Source code in packages/irx/src/irx/builder/runtime/registry.py
165
166
167
168
169
170
171
def active_feature_names(self) -> tuple[str, ...]:
    """
    title: Return the active feature names.
    returns:
      type: tuple[str, Ellipsis]
    """
    return tuple(sorted(self._active_features))

feature

feature(feature_name: str) -> RuntimeFeature
Source code in packages/irx/src/irx/builder/runtime/registry.py
173
174
175
176
177
178
179
180
181
182
def feature(self, feature_name: str) -> RuntimeFeature:
    """
    title: Return a registered feature by name.
    parameters:
      feature_name:
        type: str
    returns:
      type: RuntimeFeature
    """
    return self._registry.get(feature_name)

feature_declares_symbol

feature_declares_symbol(
    feature_name: str, symbol_name: str
) -> bool
Source code in packages/irx/src/irx/builder/runtime/registry.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def feature_declares_symbol(
    self,
    feature_name: str,
    symbol_name: str,
) -> bool:
    """
    title: Return whether one feature declares one symbol.
    parameters:
      feature_name:
        type: str
      symbol_name:
        type: str
    returns:
      type: bool
    """
    return symbol_name in self.feature(feature_name).symbols

is_active

is_active(feature_name: str) -> bool
Source code in packages/irx/src/irx/builder/runtime/registry.py
154
155
156
157
158
159
160
161
162
163
def is_active(self, feature_name: str) -> bool:
    """
    title: Return whether a feature is active for the current module.
    parameters:
      feature_name:
        type: str
    returns:
      type: bool
    """
    return feature_name in self._active_features

linker_flags

linker_flags() -> tuple[str, ...]
Source code in packages/irx/src/irx/builder/runtime/registry.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
def linker_flags(self) -> tuple[str, ...]:
    """
    title: Return the linker flags required by active features.
    returns:
      type: tuple[str, Ellipsis]
    """
    flags: list[str] = []
    seen: set[str] = set()

    for feature_name in self.active_feature_names():
        feature = self.feature(feature_name)
        for flag in feature.linker_flags:
            if flag in seen:
                continue
            seen.add(flag)
            flags.append(flag)

    return tuple(flags)

native_artifacts

native_artifacts() -> tuple[NativeArtifact, ...]
Source code in packages/irx/src/irx/builder/runtime/registry.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def native_artifacts(self) -> tuple[NativeArtifact, ...]:
    """
    title: Return the native artifacts required by active features.
    returns:
      type: tuple[NativeArtifact, Ellipsis]
    """
    artifacts: list[NativeArtifact] = []
    seen: set[tuple[str, str]] = set()

    for feature_name in self.active_feature_names():
        feature = self.feature(feature_name)
        for artifact in feature.artifacts:
            dedupe_key = (artifact.kind, str(artifact.path))
            if dedupe_key in seen:
                continue
            seen.add(dedupe_key)
            artifacts.append(artifact)

    return tuple(artifacts)

require_symbol

require_symbol(
    feature_name: str, symbol_name: str
) -> Function
Source code in packages/irx/src/irx/builder/runtime/registry.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def require_symbol(
    self,
    feature_name: str,
    symbol_name: str,
) -> ir.Function:
    """
    title: Activate a feature and declare one of its external symbols.
    parameters:
      feature_name:
        type: str
      symbol_name:
        type: str
    returns:
      type: ir.Function
    """
    self.activate(feature_name)
    cache_key = (feature_name, symbol_name)
    cached = self._declared_symbols.get(cache_key)
    if cached is not None:
        return cached

    feature = self.feature(feature_name)
    try:
        symbol_spec = feature.symbols[symbol_name]
    except KeyError as exc:
        raise RuntimeFeatureError(
            Diagnostic(
                message=(
                    f"runtime feature '{feature_name}' does not declare "
                    f"symbol '{symbol_name}'"
                ),
                code=DiagnosticCodes.RUNTIME_FEATURE_SYMBOL_MISSING,
                phase="runtime",
                notes=(
                    "declared symbols: "
                    f"{', '.join(sorted(feature.symbols))}",
                )
                if feature.symbols
                else (),
                cause=KeyError(symbol_name),
            )
        ) from exc

    declared = symbol_spec.declare(self._owner)
    self._declared_symbols[cache_key] = declared
    return declared

get_default_runtime_feature_registry cached

get_default_runtime_feature_registry() -> (
    RuntimeFeatureRegistry
)
Source code in packages/irx/src/irx/builder/runtime/registry.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
@lru_cache(maxsize=1)
@typechecked
def get_default_runtime_feature_registry() -> RuntimeFeatureRegistry:
    """
    title: Build the default runtime feature registry.
    returns:
      type: RuntimeFeatureRegistry
    """
    registry = RuntimeFeatureRegistry()
    registry.register(build_libc_runtime_feature())
    registry.register(build_assertions_runtime_feature())
    registry.register(build_libm_runtime_feature())
    registry.register(build_buffer_runtime_feature())
    registry.register(build_array_runtime_feature())
    registry.register(build_tensor_runtime_feature())
    registry.register(build_list_runtime_feature())
    return registry