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 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestElementsAttrInterface) 20 21 StringRef getArgument() const final { return "test-elements-attr-interface"; } 22 StringRef getDescription() const final { 23 return "Test ElementsAttr interface support."; 24 } 25 void runOnOperation() override { 26 getOperation().walk([&](Operation *op) { 27 for (NamedAttribute attr : op->getAttrs()) { 28 auto elementsAttr = attr.getValue().dyn_cast<ElementsAttr>(); 29 if (!elementsAttr) 30 continue; 31 testElementsAttrIteration<uint64_t>(op, elementsAttr, "uint64_t"); 32 testElementsAttrIteration<APInt>(op, elementsAttr, "APInt"); 33 testElementsAttrIteration<IntegerAttr>(op, elementsAttr, "IntegerAttr"); 34 } 35 }); 36 } 37 38 template <typename T> 39 void testElementsAttrIteration(Operation *op, ElementsAttr attr, 40 StringRef type) { 41 InFlightDiagnostic diag = op->emitError() 42 << "Test iterating `" << type << "`: "; 43 44 auto values = attr.tryGetValues<T>(); 45 if (!values) { 46 diag << "unable to iterate type"; 47 return; 48 } 49 50 llvm::interleaveComma(*values, diag, [&](T value) { 51 diag << llvm::formatv("{0}", value).str(); 52 }); 53 } 54 }; 55 } // namespace 56 57 namespace mlir { 58 namespace test { 59 void registerTestBuiltinAttributeInterfaces() { 60 PassRegistration<TestElementsAttrInterface>(); 61 } 62 } // namespace test 63 } // namespace mlir 64