1//===- TestBuiltinDistinctAttributes.cpp - Test DistinctAttributes --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TestDialect.h"
10#include "mlir/IR/BuiltinAttributes.h"
11#include "mlir/Pass/Pass.h"
12
13using namespace mlir;
14
15namespace {
16/// This is a distinct attribute test pass that tests if distinct attributes can
17/// be created in parallel in a deterministic way.
18struct DistinctAttributesPass
19 : public PassWrapper<DistinctAttributesPass, OperationPass<func::FuncOp>> {
20 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(DistinctAttributesPass)
21
22 StringRef getArgument() const final { return "test-distinct-attrs"; }
23 StringRef getDescription() const final {
24 return "Test parallel creation of distinct attributes";
25 }
26
27 void runOnOperation() override {
28 auto funcOp = getOperation();
29
30 /// Walk all operations and create a distinct output attribute given a
31 /// distinct input attribute.
32 funcOp->walk(callback: [](Operation *op) {
33 auto distinctAttr =
34 op->getDiscardableAttrOfType<DistinctAttr>(name: "distinct.input");
35 if (!distinctAttr)
36 return;
37 op->setDiscardableAttr(
38 name: "distinct.output",
39 value: DistinctAttr::create(referencedAttr: distinctAttr.getReferencedAttr()));
40 });
41 }
42};
43} // namespace
44
45namespace mlir {
46namespace test {
47void registerTestBuiltinDistinctAttributes() {
48 PassRegistration<DistinctAttributesPass>();
49}
50} // namespace test
51} // namespace mlir
52