//===- TestBuiltinAttributeInterfaces.cpp ---------------------------------===// // // 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 "TestAttributes.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/Pass/Pass.h" #include "llvm/ADT/TypeSwitch.h" #include "llvm/Support/FormatVariadic.h" using namespace mlir; using namespace test; // Helper to print one scalar value, force int8_t to print as integer instead of // char. template static void printOneElement(InFlightDiagnostic &os, T value) { os << llvm::formatv("{0}", value).str(); } namespace { struct TestElementsAttrInterface : public PassWrapper> { MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestElementsAttrInterface) StringRef getArgument() const final { return "test-elements-attr-interface"; } StringRef getDescription() const final { return "Test ElementsAttr interface support."; } void runOnOperation() override { getOperation().walk([&](Operation *op) { for (NamedAttribute attr : op->getAttrs()) { auto elementsAttr = dyn_cast(attr.getValue()); if (!elementsAttr) continue; testElementsAttrIteration(op, elementsAttr, "int64_t"); testElementsAttrIteration(op, elementsAttr, "uint64_t"); testElementsAttrIteration(op, elementsAttr, "APInt"); testElementsAttrIteration(op, elementsAttr, "IntegerAttr"); } }); } template void testElementsAttrIteration(Operation *op, ElementsAttr attr, StringRef type) { InFlightDiagnostic diag = op->emitError() << "Test iterating `" << type << "`: "; if (!attr.getElementType().isa()) { diag << "expected element type to be an integer type"; return; } auto values = attr.tryGetValues(); if (!values) { diag << "unable to iterate type"; return; } llvm::interleaveComma(*values, diag, [&](T value) { printOneElement(diag, value); }); } }; } // namespace namespace mlir { namespace test { void registerTestBuiltinAttributeInterfaces() { PassRegistration(); } } // namespace test } // namespace mlir