What: introducing set of aliasing metadata to memref dialect, similar to LLVM ones LLVM Language Reference Manual — LLVM 22.0.0git documentation
Motivation: LLVM provides a set of aliasing metadata for fine-grained control which read/write operations can (not) alias, which is important to optimizations. Unfortunately, currently there is no way to generate it going from memref dialect.
We propose to introduce similar constructs to the memref dialect.
Example:
```
#alias_scope1 = #memref.alias_scope<id = distinct[0]<>, description = "scope">
#alias_scope2 = #memref.alias_scope<id = distinct[1]<>>
func.func @memref_alias_scope(%arg1 : memref<?xf32>, %arg2 : memref<?xf32>, %arg3: index) -> f32 {
%0 = memref.alias_domain_scope "The Domain" -> f32 {
%val = memref.load %arg1[%arg3] { alias = #memref.aliasing<alias_scopes=[#alias_scope1], noalias=[#alias_scope2]> } : memref<?xf32>
memref.store %val, %arg2[%arg3] { alias = #memref.aliasing<alias_scopes=[#alias_scope2], noalias=[#alias_scope1]> } : memref<?xf32>
memref.alias_domain_scope.return %val: f32
}
return %0 : f32
}
```
Any meref load/store op can have AliasingAttr attached, consisting of alias_scopes and noaliaslists. The semantics is (from LLVM doc):
When evaluating an aliasing query, if for some domain, the set of scopes with that domain in one instruction’s alias.scope list is a subset of (or equal to) the set of scopes for that domain in another instruction’s noalias list, then the two memory accesses are assumed not to alias.
The notable difference is that aliasing domain is modeled using region op instead of attribute. Modeling domain using attributes (as it currently done in LLVM dialect) have issues with inlining, as you have to create new domain and update all attributes each time you inline the function and it doesn’t compose well with MLIR inlining infra. Associating domain with the region op solves it naturally as domain op is cloned during the inlining. Using regions for domain also limits the API somewhat as unlike LLVM you can only have a single domain attached to the op.
The change is backward compatible as aliasing attributes are optional and omitting them corresponds with current behavior (load/store op may alias with any other load/store op). Dropping alias attributes during transformation is safe and can only result in suboptimal codegen but not in miscompilations. We are planning to update existing transformations incrementally to support new attributes.
While mostly modeled against LLVM, we believe the concept is generic enough to be introduced into middle-level dialect like memref. Lowering to LLVM is straightforward and if other backend doesn’t support such constructs (e.g. SPIR-V doesn’t have such fine-grained control) it’s always safe to ignore/drop the metadata during lowering.
The initial PR [mlir][memref] Alias attributes for memref dialect by Hardcode84 · Pull Request #154991 · llvm/llvm-project · GitHub