﻿# IREE VM modules and contexts: sharing code does not mean sharing mutable module state

Understand IREE module reuse, context-local mutable state and high-level runtime sessions before choosing resource ownership for several instances of the same compiled program.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-iree-vm-instance-module-context-state-ownership/
Language: en

## Two callers need the same code but different program state

An application wants several independent instances of a compiled program. Copying the module bytes for every caller may be unnecessary, while sharing a single mutable execution state may be incorrect. Distinguish the code object from the context that instantiates it. This is a runtime ownership decision rather than a question of whether a model file can be loaded twice. Identify which state must remain independent and which expensive read-only resources may legitimately be shared.

## Modules and contexts have different responsibilities

The [invocation execution model](https://iree.dev/developers/design-docs/invocation-execution-model/) describes read-only modules reused across contexts and contexts carrying mutable module state. A context links and instantiates its modules. Reusing a module object therefore does not automatically imply that every context observes the same mutable globals. Conversely, context-local module state does not establish complete physical isolation of all resources reachable through external native code or shared device objects. Keep those claims separate.

## VM instances provide a broader hosting boundary

The [virtual machine design](https://iree.dev/developers/design-docs/vm/) explains the VM and its modules, including runtime subsystems. A compiled user program is not the only module involved: device access and parameter access can be supplied through other modules. Record which modules participate in each context and which resources they reference. The [hybrid architecture guide](/journal/cpu-gpu-hybrid-architecture/) provides background on host and device responsibilities, but does not determine how this application should partition mutable state.

## High-level sessions arrange the runtime objects

The [C API guide](https://iree.dev/reference/bindings/c-api/) describes instances, sessions, contexts and modules in the embedding workflow. Follow ownership and release rules of the API actually used instead of translating product-level terminology into assumed object lifetimes. A session may organize calls and state without becoming an application-wide isolation policy. Validate separate stateful invocations and resource cleanup under the intended hosting arrangement, including any native modules that hold resources outside the compiled program’s ordinary globals.

## Review sharing by resource category

Create an inventory covering module code, context state, device allocations, parameters and application-native resources. State who creates, retains and releases each category. The [allocation lifetime guide](/journal/learn-stream-ordered-allocation-lifetime/) helps explain why shared references need explicit lifetime relationships. Test that changes intended to remain local do not leak through an accidentally shared external object. This yields a concrete reuse design, rather than either duplicating everything indiscriminately or assuming that a shared compiled module forces all callers into one mutable state space.
