1 //===- TestAttributes.cpp - MLIR Test Dialect Attributes --------*- C++ -*-===// 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 // This file contains attributes defined by the TestDialect for testing various 10 // features of MLIR. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "TestAttributes.h" 15 #include "TestDialect.h" 16 #include "mlir/IR/Builders.h" 17 #include "mlir/IR/DialectImplementation.h" 18 #include "mlir/IR/Types.h" 19 #include "llvm/ADT/Hashing.h" 20 #include "llvm/ADT/SetVector.h" 21 #include "llvm/ADT/TypeSwitch.h" 22 23 using namespace mlir; 24 using namespace mlir::test; 25 26 //===----------------------------------------------------------------------===// 27 // AttrWithSelfTypeParamAttr 28 //===----------------------------------------------------------------------===// 29 30 Attribute AttrWithSelfTypeParamAttr::parse(MLIRContext *context, 31 DialectAsmParser &parser, 32 Type type) { 33 Type selfType; 34 if (parser.parseType(selfType)) 35 return Attribute(); 36 return get(context, selfType); 37 } 38 39 void AttrWithSelfTypeParamAttr::print(DialectAsmPrinter &printer) const { 40 printer << "attr_with_self_type_param " << getType(); 41 } 42 43 //===----------------------------------------------------------------------===// 44 // AttrWithTypeBuilderAttr 45 //===----------------------------------------------------------------------===// 46 47 Attribute AttrWithTypeBuilderAttr::parse(MLIRContext *context, 48 DialectAsmParser &parser, Type type) { 49 IntegerAttr element; 50 if (parser.parseAttribute(element)) 51 return Attribute(); 52 return get(context, element); 53 } 54 55 void AttrWithTypeBuilderAttr::print(DialectAsmPrinter &printer) const { 56 printer << "attr_with_type_builder " << getAttr(); 57 } 58 59 //===----------------------------------------------------------------------===// 60 // CompoundAAttr 61 //===----------------------------------------------------------------------===// 62 63 Attribute CompoundAAttr::parse(MLIRContext *context, DialectAsmParser &parser, 64 Type type) { 65 int widthOfSomething; 66 Type oneType; 67 SmallVector<int, 4> arrayOfInts; 68 if (parser.parseLess() || parser.parseInteger(widthOfSomething) || 69 parser.parseComma() || parser.parseType(oneType) || parser.parseComma() || 70 parser.parseLSquare()) 71 return Attribute(); 72 73 int intVal; 74 while (!*parser.parseOptionalInteger(intVal)) { 75 arrayOfInts.push_back(intVal); 76 if (parser.parseOptionalComma()) 77 break; 78 } 79 80 if (parser.parseRSquare() || parser.parseGreater()) 81 return Attribute(); 82 return get(context, widthOfSomething, oneType, arrayOfInts); 83 } 84 85 void CompoundAAttr::print(DialectAsmPrinter &printer) const { 86 printer << "cmpnd_a<" << getWidthOfSomething() << ", " << getOneType() 87 << ", ["; 88 llvm::interleaveComma(getArrayOfInts(), printer); 89 printer << "]>"; 90 } 91 92 //===----------------------------------------------------------------------===// 93 // Tablegen Generated Definitions 94 //===----------------------------------------------------------------------===// 95 96 #define GET_ATTRDEF_CLASSES 97 #include "TestAttrDefs.cpp.inc" 98 99 //===----------------------------------------------------------------------===// 100 // TestDialect 101 //===----------------------------------------------------------------------===// 102 103 Attribute TestDialect::parseAttribute(DialectAsmParser &parser, 104 Type type) const { 105 StringRef attrTag; 106 if (failed(parser.parseKeyword(&attrTag))) 107 return Attribute(); 108 { 109 Attribute attr; 110 auto parseResult = 111 generatedAttributeParser(getContext(), parser, attrTag, type, attr); 112 if (parseResult.hasValue()) 113 return attr; 114 } 115 parser.emitError(parser.getNameLoc(), "unknown test attribute"); 116 return Attribute(); 117 } 118 119 void TestDialect::printAttribute(Attribute attr, 120 DialectAsmPrinter &printer) const { 121 if (succeeded(generatedAttributePrinter(attr, printer))) 122 return; 123 } 124