Files
clang-p2996/mlir/test/lib/IR/TestBuiltinDistinctAttributes.cpp
Tobias Gysi 728a8d5a81 [mlir] Add a builtin distinct attribute
A distinct attribute associates a referenced attribute with a unique
identifier. Every call to its create function allocates a new
distinct attribute instance. The address of the attribute instance
temporarily serves as its unique identifier. Similar to the names
of SSA values, the final unique identifiers are generated during
pretty printing.

Examples:
 #distinct = distinct[0]<42.0 : f32>
 #distinct1 = distinct[1]<42.0 : f32>
 #distinct2 = distinct[2]<array<i32: 10, 42>>

This mechanism is meant to generate attributes with a unique
identifier, which can be used to mark groups of operations
that share a common properties such as if they are aliasing.

The design of the distinct attribute ensures minimal memory
footprint per distinct attribute since it only contains a reference
to another attribute. All distinct attributes are stored outside of
the storage uniquer in a thread local store that is part of the
context. It uses one bump pointer allocator per thread to ensure
distinct attributes can be created in-parallel.

Reviewed By: rriddle, Dinistro, zero9178

Differential Revision: https://reviews.llvm.org/D153360
2023-07-11 07:33:16 +00:00

50 lines
1.6 KiB
C++

//===- TestBuiltinDistinctAttributes.cpp - Test DistinctAttributes --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TestDialect.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
namespace {
/// This is a distinct attribute test pass that tests if distinct attributes can
/// be created in parallel in a deterministic way.
struct DistinctAttributesPass
: public PassWrapper<DistinctAttributesPass, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(DistinctAttributesPass)
StringRef getArgument() const final { return "test-distinct-attrs"; }
StringRef getDescription() const final {
return "Test parallel creation of distinct attributes";
}
void runOnOperation() override {
auto funcOp = getOperation();
/// Walk all operations and create a distinct output attribute given a
/// distinct input attribute.
funcOp->walk([](Operation *op) {
auto distinctAttr = op->getAttrOfType<DistinctAttr>("distinct.input");
if (!distinctAttr)
return;
op->setAttr("distinct.output",
DistinctAttr::create(distinctAttr.getReferencedAttr()));
});
}
};
} // namespace
namespace mlir {
namespace test {
void registerTestBuiltinDistinctAttributes() {
PassRegistration<DistinctAttributesPass>();
}
} // namespace test
} // namespace mlir