34  Memory Effects

Memory effects describe how an operation interacts with a resource, usually memory: read, write, allocate, free, or an unknown effect. They are the facts that make dependence analysis, dead-code elimination, scheduling, and bufferization possible across dialects.

34.1 Read And Write Have Different Consequences

A read observes a memory state; moving it can change results if an intervening write may alias. A write changes memory; removing or duplicating it is observable. Allocation introduces a resource and free ends its lifetime. These are not interchangeable labels, and an operation may have several effects on different resources.

An arithmetic add normally has no memory effects. A memref load reads, a store writes, and a call may have unknown effects unless summarized. A compiler must assume the conservative case when it cannot prove which resources are touched.

34.2 Resource Identity And Aliasing

Effects alone do not establish independence. Two operations may both write memory but be reorderable if analysis proves they use disjoint resources. Two different memref values may alias the same allocation. Alias analysis and memory-effect interfaces work together: effects say what kind of access occurs; aliasing says whether the accesses may refer to the same location.

34.3 Implementations Must Be Exact

An incomplete memory-effect implementation is a correctness bug because generic passes may remove or move operations based on it. A conservative implementation costs optimization but remains safe. When adding a custom dialect operation, document all reads, writes, allocations, frees, and unknown calls, and test it under transformations that perform code motion or dead-code elimination.