CIRCT 24.0.0git
Loading...
Searching...
No Matches
CustomDirectiveImpl.cpp
Go to the documentation of this file.
1//===- CustomDirectiveImpl.cpp - Custom TableGen directives ---------------===//
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
10#include "llvm/ADT/SmallString.h"
11
12using namespace circt;
13
14ParseResult circt::parseImplicitSSAName(OpAsmParser &parser, StringAttr &attr) {
15 // Use the explicit name if one is provided as `name "xyz"`.
16 if (!parser.parseOptionalKeyword("name")) {
17 std::string str;
18 if (parser.parseString(&str))
19 return failure();
20 attr = parser.getBuilder().getStringAttr(str);
21 return success();
22 }
23
24 // Infer the name from the SSA name of the operation's first result.
25 auto resultName = parser.getResultName(0).first;
26 if (!resultName.empty() && isdigit(resultName[0]))
27 resultName = "";
28 attr = parser.getBuilder().getStringAttr(resultName);
29 return success();
30}
31
32ParseResult circt::parseImplicitSSANames(OpAsmParser &parser, ArrayAttr &attr) {
33 SmallVector<Attribute> names;
34 unsigned resultIndex = 0;
35 while (resultIndex < parser.getNumResults()) {
36 auto resultName = parser.getResultName(resultIndex++).first;
37 if (!resultName.empty() && isdigit(resultName[0]))
38 resultName = "";
39 names.push_back(parser.getBuilder().getStringAttr(resultName));
40 }
41 attr = parser.getBuilder().getArrayAttr(names);
42 return success();
43}
44
45ParseResult circt::parseImplicitSSAName(OpAsmParser &parser,
46 NamedAttrList &attrs) {
47 if (parser.parseOptionalAttrDict(attrs))
48 return failure();
50 return success();
51}
52
53bool circt::inferImplicitSSAName(OpAsmParser &parser, NamedAttrList &attrs) {
54 // Don't do anything if a `name` attribute is explicitly provided.
55 if (attrs.get("name"))
56 return false;
57
58 // Infer the name from the SSA name of the operation's first result.
59 auto resultName = parser.getResultName(0).first;
60 if (!resultName.empty() && isdigit(resultName[0]))
61 resultName = "";
62 auto nameAttr = parser.getBuilder().getStringAttr(resultName);
63 auto *context = parser.getBuilder().getContext();
64 attrs.push_back({StringAttr::get(context, "name"), nameAttr});
65 return true;
66}
67
68void circt::printImplicitSSANames(OpAsmPrinter &printer, Operation *op,
69 ArrayAttr attr) {
70 // The names are already printed by the standard SSA result syntax.
71}
72
73void circt::printImplicitSSAName(OpAsmPrinter &printer, Operation *op,
74 StringAttr attr) {
75 SmallString<32> resultNameStr;
76 llvm::raw_svector_ostream tmpStream(resultNameStr);
77 printer.printOperand(op->getResult(0), tmpStream);
78 auto actualName = tmpStream.str().drop_front();
79 // Optional names are printed as digits.
80 if (!attr)
81 return;
82 auto expectedName = attr.getValue();
83 // Anonymous names are printed as digits, which is fine.
84 if (actualName == expectedName ||
85 (expectedName.empty() && isdigit(actualName[0])))
86 return;
87
88 printer << " name " << attr;
89}
90
91void circt::printImplicitSSAName(OpAsmPrinter &printer, Operation *op,
92 DictionaryAttr attrs,
93 ArrayRef<StringRef> extraElides) {
94 SmallVector<StringRef, 2> elides(extraElides.begin(), extraElides.end());
95 elideImplicitSSAName(printer, op, attrs, elides);
96 printer.printOptionalAttrDict(attrs.getValue(), elides);
97}
98
99void circt::elideImplicitSSAName(OpAsmPrinter &printer, Operation *op,
100 DictionaryAttr attrs,
101 SmallVectorImpl<StringRef> &elides) {
102 SmallString<32> resultNameStr;
103 llvm::raw_svector_ostream tmpStream(resultNameStr);
104 printer.printOperand(op->getResult(0), tmpStream);
105 auto actualName = tmpStream.str().drop_front();
106 auto expectedName = attrs.getAs<StringAttr>("name").getValue();
107 // Anonymous names are printed as digits, which is fine.
108 if (actualName == expectedName ||
109 (expectedName.empty() && isdigit(actualName[0])))
110 elides.push_back("name");
111}
112
113ParseResult circt::parseOptionalBinaryOpTypes(OpAsmParser &parser, Type &lhs,
114 Type &rhs) {
115 if (parser.parseType(lhs))
116 return failure();
117
118 // Parse an optional rhs type.
119 if (parser.parseOptionalComma()) {
120 rhs = lhs;
121 } else {
122 if (parser.parseType(rhs))
123 return failure();
124 }
125 return success();
126}
127
128void circt::printOptionalBinaryOpTypes(OpAsmPrinter &p, Operation *op, Type lhs,
129 Type rhs) {
130 p << lhs;
131 // If operand types are not same, print a rhs type.
132 if (lhs != rhs)
133 p << ", " << rhs;
134}
135
136ParseResult circt::parseKeywordBool(OpAsmParser &parser, BoolAttr &attr,
137 StringRef trueKeyword,
138 StringRef falseKeyword) {
139 if (succeeded(parser.parseOptionalKeyword(trueKeyword))) {
140 attr = BoolAttr::get(parser.getContext(), true);
141 } else if (succeeded(parser.parseOptionalKeyword(falseKeyword))) {
142 attr = BoolAttr::get(parser.getContext(), false);
143 } else {
144 return parser.emitError(parser.getCurrentLocation())
145 << "expected keyword \"" << trueKeyword << "\" or \"" << falseKeyword
146 << "\"";
147 }
148 return success();
149}
150
151void circt::printKeywordBool(OpAsmPrinter &printer, Operation *op,
152 BoolAttr attr, StringRef trueKeyword,
153 StringRef falseKeyword) {
154 if (attr.getValue())
155 printer << trueKeyword;
156 else
157 printer << falseKeyword;
158}
159
161 OpAsmParser &parser,
162 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands, Type &resultType,
163 mlir::DenseBoolArrayAttr &inverted, NamedAttrList &attrDict) {
164 SmallVector<bool> inverts;
165
166 while (true) {
167 inverts.push_back(succeeded(parser.parseOptionalKeyword("not")));
168 operands.push_back(OpAsmParser::UnresolvedOperand());
169
170 if (parser.parseOperand(operands.back()))
171 return failure();
172 if (parser.parseOptionalComma())
173 break;
174 }
175
176 if (parser.parseOptionalAttrDict(attrDict) || parser.parseColon() ||
177 parser.parseCustomTypeWithFallback(resultType))
178 return failure();
179
180 inverted = parser.getBuilder().getDenseBoolArrayAttr(inverts);
181 return success();
182}
183
184void circt::printVariadicInvertibleOperands(OpAsmPrinter &printer,
185 Operation *op,
186 OperandRange operands,
187 Type resultType,
188 mlir::DenseBoolArrayAttr inverted,
189 DictionaryAttr attrDict) {
190
191 llvm::interleaveComma(llvm::zip(inverted.asArrayRef(), operands), printer,
192 [&](auto &&pair) {
193 auto [invert, input] = pair;
194 if (invert)
195 printer << "not ";
196 printer << input;
197 });
198 printer.printOptionalAttrDict(attrDict.getValue(), {"inverted"});
199 printer << " : ";
200 printer.printType(resultType);
201}
static std::unique_ptr< Context > context
#define isdigit(x)
Definition FIRLexer.cpp:26
false return failure()
Parse an optional keyword or string and set instance into 'result'.
return success()
NamedAttrList attrs
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
ParseResult parseVariadicInvertibleOperands(OpAsmParser &parser, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands, Type &resultType, mlir::DenseBoolArrayAttr &inverted, NamedAttrList &attrDict)
Parse a variadic list of operands that may be prefixed with an optional not keyword.
ParseResult parseKeywordBool(OpAsmParser &parser, BoolAttr &attr, StringRef trueKeyword, StringRef falseKeyword)
Parse a boolean as one of two keywords.
void printOptionalBinaryOpTypes(OpAsmPrinter &p, Operation *op, Type lhs, Type rhs)
Print/parse binary operands type only when types are different.
ParseResult parseImplicitSSANames(OpAsmParser &parser, ArrayAttr &attr)
Parse an array of implicit SSA name string attributes.
void elideImplicitSSAName(OpAsmPrinter &printer, Operation *op, DictionaryAttr attrs, SmallVectorImpl< StringRef > &elides)
Check if the name attribute in attrs matches the SSA name of the operation's first result.
void printVariadicInvertibleOperands(OpAsmPrinter &printer, Operation *op, OperandRange operands, Type resultType, mlir::DenseBoolArrayAttr inverted, DictionaryAttr attrDict)
Print a variadic list of operands that may be prefixed with an optional not keyword.
ParseResult parseImplicitSSAName(OpAsmParser &parser, StringAttr &attr)
Parse an implicit SSA name string attribute.
void printKeywordBool(OpAsmPrinter &printer, Operation *op, BoolAttr attr, StringRef trueKeyword, StringRef falseKeyword)
Print a boolean as one of two keywords.
bool inferImplicitSSAName(OpAsmParser &parser, NamedAttrList &attrs)
Ensure that attrs contains a name attribute by inferring its value from the SSA name of the operation...
void printImplicitSSANames(OpAsmPrinter &p, Operation *op, ArrayAttr attr)
Print an array of implicit SSA name string attributes.
void printImplicitSSAName(OpAsmPrinter &p, Operation *op, StringAttr attr)
Print an implicit SSA name string attribute.
ParseResult parseOptionalBinaryOpTypes(OpAsmParser &parser, Type &lhs, Type &rhs)