At the moment, the LLVM dialect models access group and alias analysis metadata using operations nested in a global metadata operation:
llvm.metadata @metadata {
llvm.access_group @group
llvm.return
}
llvm.store %0, %ptr { access_groups = [@metadata::@group] }: i32, !llvm.ptr
The disadvantage of this solution is that a module pass is needed to update access group metadata for example during inlining (since adding / removing access groups in parallel is not possible). The LLVM dialect inliner thus currently bails out if a function contains access group metadata.
We propose to replace the access group operations by an attribute based metadata representation similar to the debug metadata representation:
#access_group = #llvm.access_group<id = 0>
llvm.store %0, %ptr { access_groups = [#access_group] }: i32, !llvm.ptr
The advantage of this representation is that the inliner can create new access groups in parallel. Additionally, the structured attribute also simplifies the attribute verification.
There is one problem to circumvent if we want to use attributes to model distinct metadata nodes though. As we want to have multiple instances of the same base attribute, we need to add a unique identifier to the attribute (id = 0 in the example), and more importantly, we need a way to generate these unique identifiers in a thread-safe manner.
We propose to generate the identifiers using a mutable helper attribute that contains a mutable counter. Whenever the inliner or the import need to generate a new distinct attribute, we mutate / increment the counter of the helper attribute to get the next unique identifer. The helper attribute itself also takes a base attribute as a parameter to ensure different attribute kinds use separate counters.
The following code sequence illustrates the generation of a distinct access group attribute:
auto baseAttr = AccessGroupAttr::get(context, 0);
// Instantiate a generator attribute for the access group with id 0.
auto genAttr = GeneratorAttr::get(baseAttr);
// Query the generator for the next id and use it to create a distinct attribute.
auto distinctAttr = AccessGroupAttr::get(context, genAttr.getNextID());
It uses a generator attribute uniqued for the access group attribute with index zero and increments the counter of the generator to get the next unique identifier. âš™ D148106 [mlir][llvm] Use attributes to store access groups. implements the access group refactoring. Follow up patches may update the remaining alias analysis metadata representations.
We will have further use cases once LLVM’s debug metadata changes land that will use an assignment metadata node similar to the access group nodes.