Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OMOps.cpp
Go to the documentation of this file.
1//===- OMOps.cpp - Object Model operation definitions ---------------------===//
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 the Object Model operation definitions.
10//
11//===----------------------------------------------------------------------===//
12
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/ImplicitLocOpBuilder.h"
18
19using namespace mlir;
20using namespace circt::om;
21
22//===----------------------------------------------------------------------===//
23// Custom Printers and Parsers
24//===----------------------------------------------------------------------===//
25
26static ParseResult parseBasePathString(OpAsmParser &parser, PathAttr &path) {
27 auto *context = parser.getContext();
28 auto loc = parser.getCurrentLocation();
29 std::string rawPath;
30 if (parser.parseString(&rawPath))
31 return failure();
32 if (parseBasePath(context, rawPath, path))
33 return parser.emitError(loc, "invalid base path");
34 return success();
35}
36
37static void printBasePathString(OpAsmPrinter &p, Operation *op, PathAttr path) {
38 p << '\"';
39 llvm::interleave(
40 path, p,
41 [&](const PathElement &elt) {
42 p << elt.module.getValue() << '/' << elt.instance.getValue();
43 },
44 ":");
45 p << '\"';
46}
47
48static ParseResult parsePathString(OpAsmParser &parser, PathAttr &path,
49 StringAttr &module, StringAttr &ref,
50 StringAttr &field) {
51
52 auto *context = parser.getContext();
53 auto loc = parser.getCurrentLocation();
54 std::string rawPath;
55 if (parser.parseString(&rawPath))
56 return failure();
57 if (parsePath(context, rawPath, path, module, ref, field))
58 return parser.emitError(loc, "invalid path");
59 return success();
60}
61
62static void printPathString(OpAsmPrinter &p, Operation *op, PathAttr path,
63 StringAttr module, StringAttr ref,
64 StringAttr field) {
65 p << '\"';
66 for (const auto &elt : path)
67 p << elt.module.getValue() << '/' << elt.instance.getValue() << ':';
68 if (!module.getValue().empty())
69 p << module.getValue();
70 if (!ref.getValue().empty())
71 p << '>' << ref.getValue();
72 if (!field.getValue().empty())
73 p << field.getValue();
74 p << '\"';
75}
76
77static ParseResult parseFieldLocs(OpAsmParser &parser, ArrayAttr &fieldLocs) {
78 if (parser.parseOptionalKeyword("field_locs"))
79 return success();
80 if (parser.parseLParen() || parser.parseAttribute(fieldLocs) ||
81 parser.parseRParen()) {
82 return failure();
83 }
84 return success();
85}
86
87static void printFieldLocs(OpAsmPrinter &printer, Operation *op,
88 ArrayAttr fieldLocs) {
89 mlir::OpPrintingFlags flags;
90 if (!flags.shouldPrintDebugInfo() || !fieldLocs)
91 return;
92 printer << "field_locs(";
93 printer.printAttribute(fieldLocs);
94 printer << ")";
95}
96
97//===----------------------------------------------------------------------===//
98// Shared definitions
99//===----------------------------------------------------------------------===//
100static ParseResult parseClassFieldsList(OpAsmParser &parser,
101 SmallVectorImpl<Attribute> &fieldNames,
102 SmallVectorImpl<Type> &fieldTypes) {
103
104 llvm::StringMap<SMLoc> nameLocMap;
105 auto parseElt = [&]() -> ParseResult {
106 // Parse the field name.
107 std::string fieldName;
108 if (parser.parseKeywordOrString(&fieldName))
109 return failure();
110 SMLoc currLoc = parser.getCurrentLocation();
111 if (nameLocMap.count(fieldName)) {
112 parser.emitError(currLoc, "field \"")
113 << fieldName << "\" is defined twice";
114 parser.emitError(nameLocMap[fieldName]) << "previous definition is here";
115 return failure();
116 }
117 nameLocMap[fieldName] = currLoc;
118 fieldNames.push_back(StringAttr::get(parser.getContext(), fieldName));
119
120 // Parse the field type.
121 fieldTypes.emplace_back();
122 if (parser.parseColonType(fieldTypes.back()))
123 return failure();
124
125 return success();
126 };
127
128 return parser.parseCommaSeparatedList(OpAsmParser::Delimiter::Paren,
129 parseElt);
130}
131
132static ParseResult parseClassLike(OpAsmParser &parser, OperationState &state) {
133 // Parse the Class symbol name.
134 StringAttr symName;
135 if (parser.parseSymbolName(symName, mlir::SymbolTable::getSymbolAttrName(),
136 state.attributes))
137 return failure();
138
139 // Parse the formal parameters.
140 SmallVector<OpAsmParser::Argument> args;
141 if (parser.parseArgumentList(args, OpAsmParser::Delimiter::Paren,
142 /*allowType=*/true, /*allowAttrs=*/false))
143 return failure();
144
145 SmallVector<Type> fieldTypes;
146 SmallVector<Attribute> fieldNames;
147 if (succeeded(parser.parseOptionalArrow()))
148 if (failed(parseClassFieldsList(parser, fieldNames, fieldTypes)))
149 return failure();
150
151 SmallVector<NamedAttribute> fieldTypesMap;
152 if (!fieldNames.empty()) {
153 for (auto [name, type] : zip(fieldNames, fieldTypes))
154 fieldTypesMap.push_back(
155 NamedAttribute(cast<StringAttr>(name), TypeAttr::get(type)));
156 }
157 auto *ctx = parser.getContext();
158 state.addAttribute("fieldNames", mlir::ArrayAttr::get(ctx, fieldNames));
159 state.addAttribute("fieldTypes",
160 mlir::DictionaryAttr::get(ctx, fieldTypesMap));
161
162 // Parse the optional attribute dictionary.
163 if (failed(parser.parseOptionalAttrDictWithKeyword(state.attributes)))
164 return failure();
165
166 // Parse the body.
167 Region *region = state.addRegion();
168 if (parser.parseRegion(*region, args))
169 return failure();
170
171 // If the region was empty, add an empty block so it's still a SizedRegion<1>.
172 if (region->empty())
173 region->emplaceBlock();
174
175 // Remember the formal parameter names in an attribute.
176 auto argNames = llvm::map_range(args, [&](OpAsmParser::Argument arg) {
177 return StringAttr::get(parser.getContext(), arg.ssaName.name.drop_front());
178 });
179 state.addAttribute(
180 "formalParamNames",
181 ArrayAttr::get(parser.getContext(), SmallVector<Attribute>(argNames)));
182
183 return success();
184}
185
186static void printClassLike(ClassLike classLike, OpAsmPrinter &printer) {
187 // Print the Class symbol name.
188 printer << " @";
189 printer << classLike.getSymName();
190
191 // Retrieve the formal parameter names and values.
192 auto argNames = SmallVector<StringRef>(
193 classLike.getFormalParamNames().getAsValueRange<StringAttr>());
194 ArrayRef<BlockArgument> args = classLike.getBodyBlock()->getArguments();
195
196 // Print the formal parameters.
197 printer << '(';
198 for (size_t i = 0, e = args.size(); i < e; ++i) {
199 printer << '%' << argNames[i] << ": " << args[i].getType();
200 if (i < e - 1)
201 printer << ", ";
202 }
203 printer << ") ";
204
205 ArrayRef<Attribute> fieldNames =
206 cast<ArrayAttr>(classLike->getAttr("fieldNames")).getValue();
207
208 if (!fieldNames.empty()) {
209 printer << " -> (";
210 for (size_t i = 0, e = fieldNames.size(); i < e; ++i) {
211 if (i != 0)
212 printer << ", ";
213 StringAttr name = cast<StringAttr>(fieldNames[i]);
214 printer.printKeywordOrString(name.getValue());
215 printer << ": ";
216 Type type = classLike.getFieldType(name).value();
217 printer.printType(type);
218 }
219 printer << ") ";
220 }
221
222 // Print the optional attribute dictionary.
223 SmallVector<StringRef> elidedAttrs{classLike.getSymNameAttrName(),
224 classLike.getFormalParamNamesAttrName(),
225 "fieldTypes", "fieldNames"};
226 printer.printOptionalAttrDictWithKeyword(classLike.getOperation()->getAttrs(),
227 elidedAttrs);
228
229 // Print the body.
230 printer.printRegion(classLike.getBody(), /*printEntryBlockArgs=*/false,
231 /*printBlockTerminators=*/true);
232}
233
234LogicalResult verifyClassLike(ClassLike classLike) {
235 // Verify the formal parameter names match up with the values.
236 if (classLike.getFormalParamNames().size() !=
237 classLike.getBodyBlock()->getArguments().size()) {
238 auto error = classLike.emitOpError(
239 "formal parameter name list doesn't match formal parameter value list");
240 error.attachNote(classLike.getLoc())
241 << "formal parameter names: " << classLike.getFormalParamNames();
242 error.attachNote(classLike.getLoc())
243 << "formal parameter values: "
244 << classLike.getBodyBlock()->getArguments();
245 return error;
246 }
247
248 return success();
249}
250
251void getClassLikeAsmBlockArgumentNames(ClassLike classLike, Region &region,
252 OpAsmSetValueNameFn setNameFn) {
253 // Retrieve the formal parameter names and values.
254 auto argNames = SmallVector<StringRef>(
255 classLike.getFormalParamNames().getAsValueRange<StringAttr>());
256 ArrayRef<BlockArgument> args = classLike.getBodyBlock()->getArguments();
257
258 // Use the formal parameter names as the SSA value names.
259 for (size_t i = 0, e = args.size(); i < e; ++i)
260 setNameFn(args[i], argNames[i]);
261}
262
263NamedAttribute makeFieldType(StringAttr name, Type type) {
264 return NamedAttribute(name, TypeAttr::get(type));
265}
266
267NamedAttribute makeFieldIdx(MLIRContext *ctx, mlir::StringAttr name,
268 unsigned i) {
269 return NamedAttribute(StringAttr(name),
270 mlir::IntegerAttr::get(mlir::IndexType::get(ctx), i));
271}
272
273std::optional<Type> getClassLikeFieldType(ClassLike classLike,
274 StringAttr name) {
275 DictionaryAttr fieldTypes = mlir::cast<DictionaryAttr>(
276 classLike.getOperation()->getAttr("fieldTypes"));
277 Attribute type = fieldTypes.get(name);
278 if (!type)
279 return std::nullopt;
280 return cast<TypeAttr>(type).getValue();
281}
282
283void replaceClassLikeFieldTypes(ClassLike classLike,
284 AttrTypeReplacer &replacer) {
285 classLike->setAttr("fieldTypes", cast<DictionaryAttr>(replacer.replace(
286 classLike.getFieldTypes())));
287}
288
289//===----------------------------------------------------------------------===//
290// ClassOp
291//===----------------------------------------------------------------------===//
292
293ParseResult circt::om::ClassOp::parse(OpAsmParser &parser,
294 OperationState &state) {
295 return parseClassLike(parser, state);
296}
297
298circt::om::ClassOp circt::om::ClassOp::buildSimpleClassOp(
299 OpBuilder &odsBuilder, Location loc, Twine name,
300 ArrayRef<StringRef> formalParamNames, ArrayRef<StringRef> fieldNames,
301 ArrayRef<Type> fieldTypes) {
302 circt::om::ClassOp classOp = circt::om::ClassOp::create(
303 odsBuilder, loc, odsBuilder.getStringAttr(name),
304 odsBuilder.getStrArrayAttr(formalParamNames),
305 odsBuilder.getStrArrayAttr(fieldNames),
306 odsBuilder.getDictionaryAttr(llvm::map_to_vector(
307 llvm::zip(fieldNames, fieldTypes), [&](auto field) -> NamedAttribute {
308 return NamedAttribute(odsBuilder.getStringAttr(std::get<0>(field)),
309 TypeAttr::get(std::get<1>(field)));
310 })));
311 Block *body = &classOp.getRegion().emplaceBlock();
312 auto prevLoc = odsBuilder.saveInsertionPoint();
313 odsBuilder.setInsertionPointToEnd(body);
314
315 mlir::SmallVector<Attribute> locAttrs(fieldNames.size(), LocationAttr(loc));
316
317 ClassFieldsOp::create(odsBuilder, loc,
318 llvm::map_to_vector(fieldTypes,
319 [&](Type type) -> Value {
320 return body->addArgument(type,
321 loc);
322 }),
323 odsBuilder.getArrayAttr(locAttrs));
324
325 odsBuilder.restoreInsertionPoint(prevLoc);
326
327 return classOp;
328}
329
330void circt::om::ClassOp::print(OpAsmPrinter &printer) {
331 printClassLike(*this, printer);
332}
333
334LogicalResult circt::om::ClassOp::verify() { return verifyClassLike(*this); }
335
336LogicalResult circt::om::ClassOp::verifyRegions() {
337 auto fieldsOp = cast<ClassFieldsOp>(this->getBodyBlock()->getTerminator());
338
339 // The number of results matches the number of terminator operands.
340 if (fieldsOp.getNumOperands() != this->getFieldNames().size()) {
341 auto diag = this->emitOpError()
342 << "returns '" << this->getFieldNames().size()
343 << "' fields, but its terminator returned '"
344 << fieldsOp.getNumOperands() << "' fields";
345 return diag.attachNote(fieldsOp.getLoc()) << "see terminator:";
346 }
347
348 // The type of each result matches the corresponding terminator operand type.
349 auto types = this->getFieldTypes();
350 for (auto [fieldName, terminatorOperandType] :
351 llvm::zip(this->getFieldNames(), fieldsOp.getOperandTypes())) {
352
353 if (terminatorOperandType ==
354 cast<TypeAttr>(types.get(cast<StringAttr>(fieldName))).getValue())
355 continue;
356
357 auto diag = this->emitOpError()
358 << "returns different field types than its terminator";
359 return diag.attachNote(fieldsOp.getLoc()) << "see terminator:";
360 }
361
362 return success();
363}
364
365void circt::om::ClassOp::getAsmBlockArgumentNames(
366 Region &region, OpAsmSetValueNameFn setNameFn) {
367 getClassLikeAsmBlockArgumentNames(*this, region, setNameFn);
368}
369
370std::optional<mlir::Type>
371circt::om::ClassOp::getFieldType(mlir::StringAttr field) {
372 return getClassLikeFieldType(*this, field);
373}
374
375void circt::om::ClassOp::replaceFieldTypes(AttrTypeReplacer replacer) {
376 replaceClassLikeFieldTypes(*this, replacer);
377}
378
379void circt::om::ClassOp::updateFields(
380 mlir::ArrayRef<mlir::Location> newLocations,
381 mlir::ArrayRef<mlir::Value> newValues,
382 mlir::ArrayRef<mlir::Attribute> newNames) {
383
384 auto fieldsOp = getFieldsOp();
385 assert(fieldsOp && "The fields op should exist");
386 // Get field names.
387 SmallVector<Attribute> names(getFieldNamesAttr().getAsRange<StringAttr>());
388 // Get the field types.
389 SmallVector<NamedAttribute> fieldTypes(getFieldTypesAttr().getValue());
390 // Get the field values.
391 SmallVector<Value> fieldVals(fieldsOp.getFields());
392 // Get the field locations.
393 Location fieldOpLoc = fieldsOp->getLoc();
394
395 // Extract the locations per field.
396 SmallVector<Location> locations;
397 if (auto fl = dyn_cast<FusedLoc>(fieldOpLoc)) {
398 auto metadataArr = dyn_cast<ArrayAttr>(fl.getMetadata());
399 assert(metadataArr && "Expected the metadata for the fused location");
400 auto r = metadataArr.getAsRange<LocationAttr>();
401 locations.append(r.begin(), r.end());
402 } else {
403 // Assume same loc for every field.
404 locations.append(names.size(), fieldOpLoc);
405 }
406
407 // Append the new names, locations and values.
408 names.append(newNames.begin(), newNames.end());
409 locations.append(newLocations.begin(), newLocations.end());
410 fieldVals.append(newValues.begin(), newValues.end());
411
412 // Construct the new field types from values and names.
413 for (auto [v, n] : llvm::zip(newValues, newNames))
414 fieldTypes.emplace_back(
415 NamedAttribute(llvm::cast<StringAttr>(n), TypeAttr::get(v.getType())));
416
417 // Keep the locations as array on the metadata.
418 SmallVector<Attribute> locationsAttr;
419 llvm::for_each(locations, [&](Location &l) {
420 locationsAttr.push_back(cast<Attribute>(l));
421 });
422
423 ImplicitLocOpBuilder builder(getLoc(), *this);
424 // Update the field names attribute.
425 setFieldNamesAttr(builder.getArrayAttr(names));
426 // Update the fields type attribute.
427 setFieldTypesAttr(builder.getDictionaryAttr(fieldTypes));
428 fieldsOp.getFieldsMutable().assign(fieldVals);
429 // Update the location.
430 fieldsOp->setLoc(builder.getFusedLoc(
431 locations, ArrayAttr::get(getContext(), locationsAttr)));
432}
433
434void circt::om::ClassOp::addNewFieldsOp(mlir::OpBuilder &builder,
435 mlir::ArrayRef<Location> locs,
436 mlir::ArrayRef<Value> values) {
437 // Store the original locations as a metadata array so that unique locations
438 // are preserved as a mapping from field index to location
439 assert(locs.size() == values.size() && "Expected a location per value");
440 mlir::SmallVector<Attribute> locAttrs;
441 for (auto loc : locs) {
442 locAttrs.push_back(cast<Attribute>(LocationAttr(loc)));
443 }
444 // Also store the locations incase there's some other analysis that might
445 // be able to use the default FusedLoc representation.
446 ClassFieldsOp::create(builder, builder.getFusedLoc(locs), values,
447 builder.getArrayAttr(locAttrs));
448}
449
450mlir::Location circt::om::ClassOp::getFieldLocByIndex(size_t i) {
451 auto fieldsOp = this->getFieldsOp();
452 auto fieldLocs = fieldsOp.getFieldLocs();
453 if (!fieldLocs.has_value())
454 return fieldsOp.getLoc();
455 assert(i < fieldLocs.value().size() &&
456 "field index too large for location array");
457 return cast<LocationAttr>(fieldLocs.value()[i]);
458}
459
460//===----------------------------------------------------------------------===//
461// ClassExternOp
462//===----------------------------------------------------------------------===//
463
464ParseResult circt::om::ClassExternOp::parse(OpAsmParser &parser,
465 OperationState &state) {
466 return parseClassLike(parser, state);
467}
468
469void circt::om::ClassExternOp::print(OpAsmPrinter &printer) {
470 printClassLike(*this, printer);
471}
472
473LogicalResult circt::om::ClassExternOp::verify() {
474 if (failed(verifyClassLike(*this))) {
475 return failure();
476 }
477 // Verify body is empty
478 if (!this->getBodyBlock()->getOperations().empty()) {
479 return this->emitOpError("external class body should be empty");
480 }
481
482 return success();
483}
484
485void circt::om::ClassExternOp::getAsmBlockArgumentNames(
486 Region &region, OpAsmSetValueNameFn setNameFn) {
487 getClassLikeAsmBlockArgumentNames(*this, region, setNameFn);
488}
489
490std::optional<mlir::Type>
491circt::om::ClassExternOp::getFieldType(mlir::StringAttr field) {
492 return getClassLikeFieldType(*this, field);
493}
494
495void circt::om::ClassExternOp::replaceFieldTypes(AttrTypeReplacer replacer) {
496 replaceClassLikeFieldTypes(*this, replacer);
497}
498
499//===----------------------------------------------------------------------===//
500// ClassFieldsOp
501//===----------------------------------------------------------------------===//
502//
503LogicalResult circt::om::ClassFieldsOp::verify() {
504 auto fieldLocs = this->getFieldLocs();
505 if (fieldLocs.has_value()) {
506 auto fieldLocsVal = fieldLocs.value();
507 if (fieldLocsVal.size() != this->getFields().size()) {
508 auto error = this->emitOpError("size of field_locs (")
509 << fieldLocsVal.size()
510 << ") does not match number of fields ("
511 << this->getFields().size() << ")";
512 }
513 }
514 return success();
515}
516
517//===----------------------------------------------------------------------===//
518// ObjectOp
519//===----------------------------------------------------------------------===//
520
521void circt::om::ObjectOp::build(::mlir::OpBuilder &odsBuilder,
522 ::mlir::OperationState &odsState,
523 om::ClassOp classOp,
524 ::mlir::ValueRange actualParams) {
525 return build(odsBuilder, odsState,
526 om::ClassType::get(odsBuilder.getContext(),
527 mlir::FlatSymbolRefAttr::get(classOp)),
528 classOp.getNameAttr(), actualParams);
529}
530
531LogicalResult
532circt::om::ObjectOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
533 // Verify the result type is the same as the referred-to class.
534 StringAttr resultClassName = getResult().getType().getClassName().getAttr();
535 StringAttr className = getClassNameAttr();
536 if (resultClassName != className)
537 return emitOpError("result type (")
538 << resultClassName << ") does not match referred to class ("
539 << className << ')';
540
541 // Verify the referred to ClassOp exists.
542 auto classDef = dyn_cast_or_null<ClassLike>(
543 symbolTable.lookupNearestSymbolFrom(*this, className));
544 if (!classDef)
545 return emitOpError("refers to non-existant class (") << className << ')';
546
547 auto actualTypes = getActualParams().getTypes();
548 auto formalTypes = classDef.getBodyBlock()->getArgumentTypes();
549
550 // Verify the actual parameter list matches the formal parameter list.
551 if (actualTypes.size() != formalTypes.size()) {
552 auto error = emitOpError(
553 "actual parameter list doesn't match formal parameter list");
554 error.attachNote(classDef.getLoc())
555 << "formal parameters: " << classDef.getBodyBlock()->getArguments();
556 error.attachNote(getLoc()) << "actual parameters: " << getActualParams();
557 return error;
558 }
559
560 // Verify the actual parameter types match the formal parameter types.
561 for (size_t i = 0, e = actualTypes.size(); i < e; ++i) {
562 if (actualTypes[i] != formalTypes[i]) {
563 return emitOpError("actual parameter type (")
564 << actualTypes[i] << ") doesn't match formal parameter type ("
565 << formalTypes[i] << ')';
566 }
567 }
568
569 return success();
570}
571
572//===----------------------------------------------------------------------===//
573// ConstantOp
574//===----------------------------------------------------------------------===//
575
576void circt::om::ConstantOp::build(::mlir::OpBuilder &odsBuilder,
577 ::mlir::OperationState &odsState,
578 ::mlir::TypedAttr constVal) {
579 return build(odsBuilder, odsState, constVal.getType(), constVal);
580}
581
582OpFoldResult circt::om::ConstantOp::fold(FoldAdaptor adaptor) {
583 assert(adaptor.getOperands().empty() && "constant has no operands");
584 return getValueAttr();
585}
586
587//===----------------------------------------------------------------------===//
588// ListCreateOp
589//===----------------------------------------------------------------------===//
590
591void circt::om::ListCreateOp::print(OpAsmPrinter &p) {
592 p << " ";
593 p.printOperands(getInputs());
594 p.printOptionalAttrDict((*this)->getAttrs());
595 p << " : " << getType().getElementType();
596}
597
598ParseResult circt::om::ListCreateOp::parse(OpAsmParser &parser,
599 OperationState &result) {
600 llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
601 Type elemType;
602
603 if (parser.parseOperandList(operands) ||
604 parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
605 parser.parseType(elemType))
606 return failure();
607 result.addTypes({circt::om::ListType::get(elemType)});
608
609 for (auto operand : operands)
610 if (parser.resolveOperand(operand, elemType, result.operands))
611 return failure();
612 return success();
613}
614
615//===----------------------------------------------------------------------===//
616// BasePathCreateOp
617//===----------------------------------------------------------------------===//
618
619LogicalResult
620BasePathCreateOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
621 auto hierPath = symbolTable.lookupNearestSymbolFrom<hw::HierPathOp>(
622 *this, getTargetAttr());
623 if (!hierPath)
624 return emitOpError("invalid symbol reference");
625 return success();
626}
627
628//===----------------------------------------------------------------------===//
629// PathCreateOp
630//===----------------------------------------------------------------------===//
631
632LogicalResult
633PathCreateOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
634 auto hierPath = symbolTable.lookupNearestSymbolFrom<hw::HierPathOp>(
635 *this, getTargetAttr());
636 if (!hierPath)
637 return emitOpError("invalid symbol reference");
638 return success();
639}
640
641//===----------------------------------------------------------------------===//
642// IntegerAddOp
643//===----------------------------------------------------------------------===//
644
645FailureOr<llvm::APSInt>
646IntegerAddOp::evaluateIntegerOperation(const llvm::APSInt &lhs,
647 const llvm::APSInt &rhs) {
648 return success(lhs + rhs);
649}
650
651//===----------------------------------------------------------------------===//
652// IntegerMulOp
653//===----------------------------------------------------------------------===//
654
655FailureOr<llvm::APSInt>
656IntegerMulOp::evaluateIntegerOperation(const llvm::APSInt &lhs,
657 const llvm::APSInt &rhs) {
658 return success(lhs * rhs);
659}
660
661//===----------------------------------------------------------------------===//
662// IntegerShrOp
663//===----------------------------------------------------------------------===//
664
665FailureOr<llvm::APSInt>
666IntegerShrOp::evaluateIntegerOperation(const llvm::APSInt &lhs,
667 const llvm::APSInt &rhs) {
668 // Check non-negative constraint from operation semantics.
669 if (!rhs.isNonNegative())
670 return emitOpError("shift amount must be non-negative");
671 // Check size constraint from implementation detail of using getExtValue.
672 if (!rhs.isRepresentableByInt64())
673 return emitOpError("shift amount must be representable in 64 bits");
674 return success(lhs >> rhs.getExtValue());
675}
676
677//===----------------------------------------------------------------------===//
678// IntegerShlOp
679//===----------------------------------------------------------------------===//
680
681FailureOr<llvm::APSInt>
682IntegerShlOp::evaluateIntegerOperation(const llvm::APSInt &lhs,
683 const llvm::APSInt &rhs) {
684 // Check non-negative constraint from operation semantics.
685 if (!rhs.isNonNegative())
686 return emitOpError("shift amount must be non-negative");
687 // Check size constraint from implementation detail of using getExtValue.
688 if (!rhs.isRepresentableByInt64())
689 return emitOpError("shift amount must be representable in 64 bits");
690 return success(lhs << rhs.getExtValue());
691}
692
693//===----------------------------------------------------------------------===//
694// TableGen generated logic.
695//===----------------------------------------------------------------------===//
696
697#define GET_OP_CLASSES
698#include "circt/Dialect/OM/OM.cpp.inc"
assert(baseType &&"element must be base type")
static Location getLoc(DefSlot slot)
Definition Mem2Reg.cpp:216
static ParseResult parseClassLike(OpAsmParser &parser, OperationState &state)
Definition OMOps.cpp:132
LogicalResult verifyClassLike(ClassLike classLike)
Definition OMOps.cpp:234
std::optional< Type > getClassLikeFieldType(ClassLike classLike, StringAttr name)
Definition OMOps.cpp:273
void getClassLikeAsmBlockArgumentNames(ClassLike classLike, Region &region, OpAsmSetValueNameFn setNameFn)
Definition OMOps.cpp:251
static ParseResult parseBasePathString(OpAsmParser &parser, PathAttr &path)
Definition OMOps.cpp:26
static ParseResult parsePathString(OpAsmParser &parser, PathAttr &path, StringAttr &module, StringAttr &ref, StringAttr &field)
Definition OMOps.cpp:48
static void printBasePathString(OpAsmPrinter &p, Operation *op, PathAttr path)
Definition OMOps.cpp:37
static void printFieldLocs(OpAsmPrinter &printer, Operation *op, ArrayAttr fieldLocs)
Definition OMOps.cpp:87
static ParseResult parseFieldLocs(OpAsmParser &parser, ArrayAttr &fieldLocs)
Definition OMOps.cpp:77
static ParseResult parseClassFieldsList(OpAsmParser &parser, SmallVectorImpl< Attribute > &fieldNames, SmallVectorImpl< Type > &fieldTypes)
Definition OMOps.cpp:100
static void printClassLike(ClassLike classLike, OpAsmPrinter &printer)
Definition OMOps.cpp:186
void replaceClassLikeFieldTypes(ClassLike classLike, AttrTypeReplacer &replacer)
Definition OMOps.cpp:283
NamedAttribute makeFieldType(StringAttr name, Type type)
Definition OMOps.cpp:263
NamedAttribute makeFieldIdx(MLIRContext *ctx, mlir::StringAttr name, unsigned i)
Definition OMOps.cpp:267
static void printPathString(OpAsmPrinter &p, Operation *op, PathAttr path, StringAttr module, StringAttr ref, StringAttr field)
Definition OMOps.cpp:62
static Block * getBodyBlock(FModuleLike mod)
static InstancePath empty
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition CalyxOps.cpp:55
void error(Twine message)
Definition LSPUtils.cpp:16
ParseResult parsePath(MLIRContext *context, StringRef spelling, PathAttr &path, StringAttr &module, StringAttr &ref, StringAttr &field)
Parse a target string in to a path.
Definition OMUtils.cpp:182
ParseResult parseBasePath(MLIRContext *context, StringRef spelling, PathAttr &path)
Parse a target string of the form "Foo/bar:Bar/baz" in to a base path.
Definition OMUtils.cpp:177
function_ref< void(Value, StringRef)> OpAsmSetValueNameFn
Definition LLVM.h:183
A module name, and the name of an instance inside that module.
mlir::StringAttr mlir::StringAttr instance