﻿# StableHLO custom calls: declare observable effects and aliasing beyond tensor results

Review StableHLO custom calls for effects and output-operand aliasing instead of assuming a registered implementation’s hidden behavior is visible to compiler optimizations.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-stablehlo-custom-call-side-effects-alias-contract/
Language: en

## The tensor result does not describe every consequence

A custom implementation computes an output tensor and also updates an external resource. The graph exposes only the tensor dependency, so an optimization review cannot infer the external update from the function’s source code elsewhere. The implementation may be registered correctly and still have an incomplete compiler-facing contract. Examine what is observable outside the returned values, including resource mutation and storage relationships. A successful numerical comparison on one invocation does not establish that transformations may safely reorder or eliminate the call.

## StableHLO carries explicit custom-call metadata

The [StableHLO specification](https://openxla.org/stablehlo/spec) describes `custom_call` as implementation-defined and includes effect and output-operand alias metadata. Those declarations are part of the interface, not decorative annotations. Keep them consistent with the actual implementation and the consuming compiler’s documented interpretation. A target name identifies a call destination but does not describe every consequence of executing it. Treat ABI compatibility, tensor semantics and effects as distinct review questions.

## Backend call conventions still need agreement

The [XLA custom call guide](https://openxla.org/xla/custom_call) illustrates custom-call construction with effect and aliasing information. Verify the convention used by the chosen integration rather than transferring assumptions from another API version. Explicitly identify whether an output shares storage with an operand or owns separate storage. The [tensor descriptor guide](/journal/learn-tensor-memory-descriptor/) explains why equal shapes do not establish shared allocation: logical dimensions and physical ownership are different properties of a value.

## Effect declarations are not universal ordering barriers

The [MLIR side-effects rationale](https://mlir.llvm.org/docs/Rationale/SideEffectsAndSpeculation/) explains why optimization reasoning depends on modeled effects and speculation constraints. Do not assume an effect flag automatically imposes every application-required order across unrelated calls or devices. Express required dependencies through the mechanisms supported by the actual compiler path. The [stream dependency guide](/journal/learn-stream-ordering-dependencies/) provides related background, while host-visible effects and compiler effect modeling have their own interfaces beyond stream scheduling.

## Test observable behavior under transformation

Compare more than the returned tensor: check relevant external state, repeated calls and the alias behavior promised to consumers. Include a case where the tensor result is unused if that is legal for the operation’s role. Review the optimized graph to confirm that its treatment matches the declared contract. This distinguishes a correct external kernel from a correctly integrated external operation, which must remain meaningful when ordinary compiler transformations change the surrounding graph.
