Files
clang-p2996/mlir/unittests/IR/SubElementInterfaceTest.cpp
River Riddle 38c219b4a8 [mlir] Infer SubElementInterface implementations using the storage KeyTy
The KeyTy of attribute/type storage classes provide enough information for
automatically implementing the necessary sub element interface methods. This
removes the need for derived classes to do it themselves, which is both much
nicer and easier to handle certain invariants (e.g. null handling). In cases where
explicitly handling for parameter types is necessary, they can provide an implementation
of `AttrTypeSubElementHandler` to opt-in to support.

This tickles a few things alias wise, which annoyingly messes with tests that hard
code specific affine map numbers.

Differential Revision: https://reviews.llvm.org/D137374
2022-11-04 18:15:03 -07:00

37 lines
1.2 KiB
C++

//===- SubElementInterfaceTest.cpp - SubElementInterface unit tests -------===//
//
// 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 "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/SubElementInterfaces.h"
#include "gtest/gtest.h"
#include <cstdint>
using namespace mlir;
using namespace mlir::detail;
namespace {
TEST(SubElementInterfaceTest, Nested) {
MLIRContext context;
Builder builder(&context);
BoolAttr trueAttr = builder.getBoolAttr(true);
BoolAttr falseAttr = builder.getBoolAttr(false);
ArrayAttr boolArrayAttr = builder.getArrayAttr({trueAttr, falseAttr});
StringAttr strAttr = builder.getStringAttr("array");
DictionaryAttr dictAttr =
builder.getDictionaryAttr(builder.getNamedAttr(strAttr, boolArrayAttr));
SmallVector<Attribute> subAttrs;
dictAttr.walkSubAttrs([&](Attribute attr) { subAttrs.push_back(attr); });
EXPECT_EQ(llvm::makeArrayRef(subAttrs),
ArrayRef<Attribute>({strAttr, trueAttr, falseAttr, boolArrayAttr}));
}
} // namespace