Intro to Polyhedral Analysis in MLIR - II
Understanding Memory Access Relations
In Part 1 of this series, we introduced the fundamentals of the polyhedral model and how it fits into the MLIR framework. In this post, we dive into one of the most important components of that model: the memory access relation.
A memory access relation is a mathematical mapping from loop iterations to the memory locations accessed during those iterations.
You can think of it like this:
AccessRel: [loop indices] → [array subscripts]
In the context of the polyhedral model:
- Domain: The iteration space of the loop
- Range: The memory space being accessed (array indices)
This relation is fundamental for detecting data dependences, validating transformations, and enabling optimizations.
Example in MLIR
Let’s take a simple loop written in MLIR:
affine.for %i = 0 to 7
Here’s what’s happening:
- We're reading from
A[%i + 1]
- We’re writing to
A[%i]
There are two memory access relations here: one for the load, and one for the store.
Loop Constraints
These are the constraints on the loop iteration variable %i, extracted from the domain:
( ) )
()
;
; )
This defines the iteration domain: 0 ≤ i < 7
.
Memory Access Relations
Let’s look at the access relations for both load and store.
Load: A[%i + 1]
( ) )
()
;
;
;
1 -1 1 = 0
means:
Memory index m
= loop index i + 1
(Here, m
is represented using a local variable)
Store: A[%i]
( ) )
()
;
;
;
This line:
1 -1 0 = 0
means:
Memory index m
= loop index i
MLIR provides utilities to extract these relations automatically from your code.
mlir::affine::MemRefAccess ;
mlir::affine::MemRefAccess ;
mlir::presburger::PresburgerSpace space =
;
mlir::presburger::IntegerRelation , ;
// Extract memory access relations into IntegerRelation
if
affine::FlatAffineValueConstraints ;
affine::FlatAffineValueConstraints ;
srcDomain.;
dstDomain.;
With this setup, you can analyze, print, or even manipulate memory access relations directly from within an MLIR pass or analysis pipeline.
Coming Next
In the next part of this series, we’ll explore how to use these access relations to perform dependence analysis that enables transformations like loop interchange, tiling, or fusion.
Stay tuned!