IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Mojo struct

Range

struct Range

A libkineto activity span recorded via the CompilerRT bridge.

Use Range as a context manager: __enter__ opens a libkineto activity span when the profiler is enabled and __exit__ closes it. The span is bound to the with block, so it covers exactly the work inside the block:

from profiling_range import Range

def build_graph() raises:
    with Range("graph_compile"):
        ...  # graph build code runs inside the span

TODO(MXTOOLS-190): once the mo.profile.range MLIR op lands, add a category parameter (mirroring std.runtime.tracing.Trace[level, category]) and a StringSlice name overload for dynamically-built names (e.g. Range("kernel_" + variant)).

Implemented traits​

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDeletable, Movable

Methods​

__init__​

def __init__(out self, name: StringSlice[StaticConstantOrigin], color: UInt32 = UInt32(0))

Records the span name and color; opening is deferred to __enter__.

Args:

  • ​name (StringSlice[StaticConstantOrigin]): The span name. Passed straight through to libkineto in __enter__. StaticString accepts both string literals and runtime StaticString values; this matches the convention used by std.runtime.tracing.Trace.
  • ​color (UInt32): 24-bit RGB color for trace viewers that honor it. Defaults to 0 (libkineto's default color).

__enter__​

def __enter__(mut self)

Begin a libkineto activity span if the profiler is enabled.

Pairs with __exit__. The enabled state is captured into _began here so a disable() racing with the scope cannot leave a span begun without a matching end.

__exit__​

def __exit__(self)

Close the libkineto activity span if __enter__ opened one.

Pairs with the begin call in __enter__. Calling disable() between entry and exit does not unbalance the libkineto bookkeeping because _began was captured at entry time.