1 //===- TestBuiltinAttributeInterfaces.cpp ---------------------------------===// 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 "TestAttributes.h" 10 #include "mlir/Pass/Pass.h" 11 #include "llvm/Support/FormatVariadic.h" 12 13 using namespace mlir; 14 using namespace test; 15 16 namespace { 17 struct TestElementsAttrInterface 18 : public PassWrapper<TestElementsAttrInterface, OperationPass<ModuleOp>> { 19 StringRef getArgument() const final { return "test-elements-attr-interface"; } 20 StringRef getDescription() const final { 21 return "Test ElementsAttr interface support."; 22 } 23 void runOnOperation() override { 24 getOperation().walk([&](Operation *op) { 25 for (NamedAttribute attr : op->getAttrs()) { 26 auto elementsAttr = attr.second.dyn_cast<ElementsAttr>(); 27 if (!elementsAttr) 28 continue; 29 testElementsAttrIteration<uint64_t>(op, elementsAttr, "uint64_t"); 30 testElementsAttrIteration<APInt>(op, elementsAttr, "APInt"); 31 testElementsAttrIteration<IntegerAttr>(op, elementsAttr, "IntegerAttr"); 32 } 33 }); 34 } 35 36 template <typename T> 37 void testElementsAttrIteration(Operation *op, ElementsAttr attr, 38 StringRef type) { 39 InFlightDiagnostic diag = op->emitError() 40 << "Test iterating `" << type << "`: "; 41 42 auto values = attr.tryGetValues<T>(); 43 if (!values) { 44 diag << "unable to iterate type"; 45 return; 46 } 47 48 llvm::interleaveComma(*values, diag, [&](T value) { 49 diag << llvm::formatv("{0}", value).str(); 50 }); 51 } 52 }; 53 } // end anonymous namespace 54 55 namespace mlir { 56 namespace test { 57 void registerTestBuiltinAttributeInterfaces() { 58 PassRegistration<TestElementsAttrInterface>(); 59 } 60 } // namespace test 61 } // namespace mlir 62