15#include "mlir/IR/Builders.h"
16#include "mlir/IR/DialectImplementation.h"
17#include "llvm/ADT/SmallString.h"
27LogicalResult SequenceOp::verifyRegions() {
28 if (TypeRange(getSequenceType().getElementTypes()) !=
29 getBody()->getArgumentTypes())
30 return emitOpError(
"sequence type does not match block argument types");
35ParseResult SequenceOp::parse(OpAsmParser &parser, OperationState &result) {
37 if (parser.parseSymbolName(
38 result.getOrAddProperties<SequenceOp::Properties>().sym_name))
42 SmallVector<OpAsmParser::Argument> arguments;
43 if (parser.parseArgumentList(arguments, OpAsmParser::Delimiter::Paren,
47 SmallVector<Type> argTypes;
48 SmallVector<Location> argLocs;
49 argTypes.reserve(arguments.size());
50 argLocs.reserve(arguments.size());
51 for (
auto &arg : arguments) {
52 argTypes.push_back(arg.type);
53 argLocs.push_back(arg.sourceLoc ? *arg.sourceLoc : result.location);
55 Type type = SequenceType::get(result.getContext(), argTypes);
56 result.getOrAddProperties<SequenceOp::Properties>().sequenceType =
59 auto loc = parser.getCurrentLocation();
60 if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
62 if (failed(verifyInherentAttrs(result.name, result.attributes, [&]() {
63 return parser.emitError(loc)
64 <<
"'" << result.name.getStringRef() <<
"' op ";
68 std::unique_ptr<Region> bodyRegionRegion = std::make_unique<Region>();
69 if (parser.parseRegion(*bodyRegionRegion, arguments))
72 if (bodyRegionRegion->empty()) {
73 bodyRegionRegion->emplaceBlock();
74 bodyRegionRegion->addArguments(argTypes, argLocs);
76 result.addRegion(std::move(bodyRegionRegion));
81void SequenceOp::print(OpAsmPrinter &p) {
83 p.printSymbolName(getSymNameAttr().getValue());
85 llvm::interleaveComma(getBody()->getArguments(), p,
86 [&](
auto arg) { p.printRegionArgument(arg); });
88 p.printOptionalAttrDictWithKeyword(
89 (*this)->getAttrs(), {getSymNameAttrName(), getSequenceTypeAttrName()});
91 p.printRegion(getBodyRegion(),
false);
99GetSequenceOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
101 symbolTable.lookupNearestSymbolFrom<SequenceOp>(*
this, getSequenceAttr());
104 <<
"'" << getSequence()
105 <<
"' does not reference a valid 'rtg.sequence' operation";
107 if (
seq.getSequenceType() != getType())
108 return emitOpError(
"referenced 'rtg.sequence' op's type does not match");
117LogicalResult SubstituteSequenceOp::verify() {
118 if (getReplacements().
empty())
119 return emitOpError(
"must at least have one replacement value");
121 if (getReplacements().size() >
122 getSequence().getType().getElementTypes().size())
124 "must not have more replacement values than sequence arguments");
126 if (getReplacements().getTypes() !=
127 getSequence().getType().getElementTypes().take_front(
128 getReplacements().size()))
129 return emitOpError(
"replacement types must match the same number of "
130 "sequence argument types from the front");
135LogicalResult SubstituteSequenceOp::inferReturnTypes(
136 MLIRContext *context, std::optional<Location> loc, ValueRange operands,
137 DictionaryAttr attributes, OpaqueProperties properties, RegionRange regions,
138 SmallVectorImpl<Type> &inferredReturnTypes) {
139 ArrayRef<Type> argTypes =
140 cast<SequenceType>(operands[0].getType()).getElementTypes();
142 SequenceType::get(context, argTypes.drop_front(operands.size() - 1));
143 inferredReturnTypes.push_back(seqType);
147ParseResult SubstituteSequenceOp::parse(::mlir::OpAsmParser &parser,
148 ::mlir::OperationState &result) {
149 OpAsmParser::UnresolvedOperand sequenceRawOperand;
150 SmallVector<OpAsmParser::UnresolvedOperand, 4> replacementsOperands;
151 Type sequenceRawType;
153 if (parser.parseOperand(sequenceRawOperand) || parser.parseLParen())
156 auto replacementsOperandsLoc = parser.getCurrentLocation();
157 if (parser.parseOperandList(replacementsOperands) || parser.parseRParen() ||
158 parser.parseColon() || parser.parseType(sequenceRawType) ||
159 parser.parseOptionalAttrDict(result.attributes))
162 if (!isa<SequenceType>(sequenceRawType))
163 return parser.emitError(parser.getNameLoc())
164 <<
"'sequence' must be handle to a sequence or sequence family, but "
168 if (parser.resolveOperand(sequenceRawOperand, sequenceRawType,
172 if (parser.resolveOperands(replacementsOperands,
173 cast<SequenceType>(sequenceRawType)
175 .take_front(replacementsOperands.size()),
176 replacementsOperandsLoc, result.operands))
179 SmallVector<Type> inferredReturnTypes;
180 if (failed(inferReturnTypes(
181 parser.getContext(), result.location, result.operands,
182 result.attributes.getDictionary(parser.getContext()),
183 result.getRawProperties(), result.regions, inferredReturnTypes)))
186 result.addTypes(inferredReturnTypes);
190void SubstituteSequenceOp::print(OpAsmPrinter &p) {
191 p <<
' ' << getSequence() <<
"(" << getReplacements()
192 <<
") : " << getSequence().getType();
193 p.printOptionalAttrDict((*this)->getAttrs(), {});
200LogicalResult InterleaveSequencesOp::verify() {
201 if (getSequences().
empty())
202 return emitOpError(
"must have at least one sequence in the list");
207OpFoldResult InterleaveSequencesOp::fold(FoldAdaptor adaptor) {
208 if (getSequences().size() == 1)
209 return getSequences()[0];
218ParseResult SetCreateOp::parse(OpAsmParser &parser, OperationState &result) {
219 llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
222 if (parser.parseOperandList(operands) ||
223 parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
224 parser.parseType(elemType))
227 result.addTypes({SetType::get(result.getContext(), elemType)});
229 for (
auto operand : operands)
230 if (parser.resolveOperand(operand, elemType, result.operands))
236void SetCreateOp::print(OpAsmPrinter &p) {
238 p.printOperands(getElements());
239 p.printOptionalAttrDict((*this)->getAttrs());
240 p <<
" : " << getSet().getType().getElementType();
243LogicalResult SetCreateOp::verify() {
244 if (getElements().size() > 0) {
247 if (getElements()[0].getType() != getSet().getType().getElementType())
248 return emitOpError() <<
"operand types must match set element type";
258ParseResult BagCreateOp::parse(OpAsmParser &parser, OperationState &result) {
259 llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> elementOperands,
263 if (!parser.parseOptionalLParen()) {
265 OpAsmParser::UnresolvedOperand elementOperand, multipleOperand;
266 if (parser.parseOperand(multipleOperand) || parser.parseKeyword(
"x") ||
267 parser.parseOperand(elementOperand))
270 elementOperands.push_back(elementOperand);
271 multipleOperands.push_back(multipleOperand);
273 if (parser.parseOptionalComma()) {
274 if (parser.parseRParen())
281 if (parser.parseColon() || parser.parseType(elemType) ||
282 parser.parseOptionalAttrDict(result.attributes))
285 result.addTypes({BagType::get(result.getContext(), elemType)});
287 for (
auto operand : elementOperands)
288 if (parser.resolveOperand(operand, elemType, result.operands))
291 for (
auto operand : multipleOperands)
292 if (parser.resolveOperand(operand, IndexType::
get(result.getContext()),
299void BagCreateOp::print(OpAsmPrinter &p) {
301 if (!getElements().
empty())
303 llvm::interleaveComma(llvm::zip(getElements(), getMultiples()), p,
304 [&](
auto elAndMultiple) {
305 auto [el, multiple] = elAndMultiple;
306 p << multiple <<
" x " << el;
308 if (!getElements().
empty())
311 p <<
" : " << getBag().getType().getElementType();
312 p.printOptionalAttrDict((*this)->getAttrs());
315LogicalResult BagCreateOp::verify() {
316 if (!llvm::all_equal(getElements().getTypes()))
317 return emitOpError() <<
"types of all elements must match";
319 if (getElements().size() > 0)
320 if (getElements()[0].getType() != getBag().getType().getElementType())
321 return emitOpError() <<
"operand types must match bag element type";
330LogicalResult FixedRegisterOp::inferReturnTypes(
331 MLIRContext *context, std::optional<Location> loc, ValueRange operands,
332 DictionaryAttr attributes, OpaqueProperties properties, RegionRange regions,
333 SmallVectorImpl<Type> &inferredReturnTypes) {
334 inferredReturnTypes.push_back(
335 properties.as<Properties *>()->getReg().getType());
339OpFoldResult FixedRegisterOp::fold(FoldAdaptor adaptor) {
return getRegAttr(); }
345LogicalResult VirtualRegisterOp::verify() {
346 if (getAllowedRegs().
empty())
347 return emitOpError(
"must have at least one allowed register");
349 if (llvm::any_of(getAllowedRegs(), [](Attribute attr) {
350 return !isa<RegisterAttrInterface>(attr);
352 return emitOpError(
"all elements must be of RegisterAttrInterface");
354 if (!llvm::all_equal(
355 llvm::map_range(getAllowedRegs().getAsRange<RegisterAttrInterface>(),
356 [](
auto attr) {
return attr.getType(); })))
357 return emitOpError(
"all allowed registers must be of the same type");
362LogicalResult VirtualRegisterOp::inferReturnTypes(
363 MLIRContext *context, std::optional<Location> loc, ValueRange operands,
364 DictionaryAttr attributes, OpaqueProperties properties, RegionRange regions,
365 SmallVectorImpl<Type> &inferredReturnTypes) {
366 auto allowedRegs = properties.as<Properties *>()->getAllowedRegs();
367 if (allowedRegs.empty()) {
369 return mlir::emitError(*loc,
"must have at least one allowed register");
374 auto regAttr = dyn_cast<RegisterAttrInterface>(allowedRegs[0]);
377 return mlir::emitError(
378 *loc,
"allowed register attributes must be of RegisterAttrInterface");
382 inferredReturnTypes.push_back(regAttr.getType());
390LogicalResult ContextSwitchOp::verify() {
391 auto elementTypes = getSequence().getType().getElementTypes();
392 if (elementTypes.size() != 3)
393 return emitOpError(
"sequence type must have exactly 3 element types");
395 if (getFrom().getType() != elementTypes[0])
397 "first sequence element type must match 'from' attribute type");
399 if (getTo().getType() != elementTypes[1])
401 "second sequence element type must match 'to' attribute type");
403 auto seqTy = dyn_cast<SequenceType>(elementTypes[2]);
404 if (!seqTy || !seqTy.getElementTypes().empty())
406 "third sequence element type must be a fully substituted sequence");
415LogicalResult TestOp::verifyRegions() {
416 if (!getTarget().entryTypesMatch(getBody()->getArgumentTypes()))
417 return emitOpError(
"argument types must match dict entry types");
422ParseResult TestOp::parse(OpAsmParser &parser, OperationState &result) {
424 if (parser.parseSymbolName(
425 result.getOrAddProperties<TestOp::Properties>().sym_name))
429 SmallVector<OpAsmParser::Argument> arguments;
430 SmallVector<StringAttr> names;
432 auto parseOneArgument = [&]() -> ParseResult {
434 if (parser.parseKeywordOrString(&name) || parser.parseEqual() ||
435 parser.parseArgument(arguments.emplace_back(),
true,
439 names.push_back(StringAttr::get(result.getContext(), name));
442 if (parser.parseCommaSeparatedList(OpAsmParser::Delimiter::Paren,
443 parseOneArgument,
" in argument list"))
446 SmallVector<Type> argTypes;
447 SmallVector<DictEntry> entries;
448 SmallVector<Location> argLocs;
449 argTypes.reserve(arguments.size());
450 argLocs.reserve(arguments.size());
451 for (
auto [name, arg] :
llvm::zip(names, arguments)) {
452 argTypes.push_back(arg.type);
453 argLocs.push_back(arg.sourceLoc ? *arg.sourceLoc : result.location);
454 entries.push_back({name, arg.type});
456 auto emitError = [&]() -> InFlightDiagnostic {
457 return parser.emitError(parser.getCurrentLocation());
459 Type type = DictType::getChecked(emitError, result.getContext(),
460 ArrayRef<DictEntry>(entries));
463 result.getOrAddProperties<TestOp::Properties>().target = TypeAttr::get(type);
465 auto loc = parser.getCurrentLocation();
466 if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
468 if (failed(verifyInherentAttrs(result.name, result.attributes, [&]() {
469 return parser.emitError(loc)
470 <<
"'" << result.name.getStringRef() <<
"' op ";
474 std::unique_ptr<Region> bodyRegionRegion = std::make_unique<Region>();
475 if (parser.parseRegion(*bodyRegionRegion, arguments))
478 if (bodyRegionRegion->empty()) {
479 bodyRegionRegion->emplaceBlock();
480 bodyRegionRegion->addArguments(argTypes, argLocs);
482 result.addRegion(std::move(bodyRegionRegion));
487void TestOp::print(OpAsmPrinter &p) {
489 p.printSymbolName(getSymNameAttr().getValue());
491 SmallString<32> resultNameStr;
492 llvm::interleaveComma(
493 llvm::zip(getTarget().getEntries(), getBody()->getArguments()), p,
494 [&](
auto entryAndArg) {
495 auto [entry, arg] = entryAndArg;
496 p << entry.name.getValue() <<
" = ";
497 p.printRegionArgument(arg);
500 p.printOptionalAttrDictWithKeyword(
501 (*this)->getAttrs(), {getSymNameAttrName(), getTargetAttrName()});
503 p.printRegion(getBodyRegion(),
false);
506void TestOp::getAsmBlockArgumentNames(Region ®ion,
508 for (
auto [entry, arg] :
509 llvm::zip(getTarget().getEntries(), region.getArguments()))
510 setNameFn(arg, entry.name.getValue());
517LogicalResult TargetOp::verifyRegions() {
518 if (!getTarget().entryTypesMatch(
519 getBody()->getTerminator()->getOperandTypes()))
520 return emitOpError(
"terminator operand types must match dict entry types");
529#define GET_OP_CLASSES
530#include "circt/Dialect/RTG/IR/RTG.cpp.inc"
static InstancePath empty
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
function_ref< void(Value, StringRef)> OpAsmSetValueNameFn