17#include "mlir/IR/Builders.h"
18#include "mlir/IR/ImplicitLocOpBuilder.h"
19#include "mlir/IR/SymbolTable.h"
20#include "llvm/ADT/STLExtras.h"
30 auto *
context = parser.getContext();
31 auto loc = parser.getCurrentLocation();
33 if (parser.parseString(&rawPath))
36 return parser.emitError(loc,
"invalid base path");
45 p << elt.module.getValue() <<
'/' << elt.
instance.getValue();
52 StringAttr &module, StringAttr &ref,
55 auto *
context = parser.getContext();
56 auto loc = parser.getCurrentLocation();
58 if (parser.parseString(&rawPath))
61 return parser.emitError(loc,
"invalid path");
66 StringAttr module, StringAttr ref,
69 for (
const auto &elt : path)
70 p << elt.module.getValue() <<
'/' << elt.instance.getValue() <<
':';
71 if (!module.getValue().empty())
72 p << module.getValue();
73 if (!ref.getValue().empty())
74 p <<
'>' << ref.getValue();
75 if (!field.getValue().empty())
76 p << field.getValue();
80static ParseResult
parseFieldLocs(OpAsmParser &parser, ArrayAttr &fieldLocs) {
81 if (parser.parseOptionalKeyword(
"field_locs"))
83 if (parser.parseLParen() || parser.parseAttribute(fieldLocs) ||
84 parser.parseRParen()) {
91 ArrayAttr fieldLocs) {
92 mlir::OpPrintingFlags flags;
93 if (!flags.shouldPrintDebugInfo() || !fieldLocs)
95 printer <<
"field_locs(";
96 printer.printAttribute(fieldLocs);
104 SmallVectorImpl<Attribute> &fieldNames,
105 SmallVectorImpl<Type> &fieldTypes) {
107 llvm::StringMap<SMLoc> nameLocMap;
108 auto parseElt = [&]() -> ParseResult {
110 std::string fieldName;
111 if (parser.parseKeywordOrString(&fieldName))
113 SMLoc currLoc = parser.getCurrentLocation();
114 if (nameLocMap.count(fieldName)) {
115 parser.emitError(currLoc,
"field \"")
116 << fieldName <<
"\" is defined twice";
117 parser.emitError(nameLocMap[fieldName]) <<
"previous definition is here";
120 nameLocMap[fieldName] = currLoc;
121 fieldNames.push_back(StringAttr::get(parser.getContext(), fieldName));
124 fieldTypes.emplace_back();
125 if (parser.parseColonType(fieldTypes.back()))
131 return parser.parseCommaSeparatedList(OpAsmParser::Delimiter::Paren,
137 (void)mlir::impl::parseOptionalVisibilityKeyword(parser, state.attributes);
141 if (parser.parseSymbolName(symName, mlir::SymbolTable::getSymbolAttrName(),
146 SmallVector<OpAsmParser::Argument> args;
147 if (parser.parseArgumentList(args, OpAsmParser::Delimiter::Paren,
151 SmallVector<Type> fieldTypes;
152 SmallVector<Attribute> fieldNames;
153 if (succeeded(parser.parseOptionalArrow()))
157 SmallVector<NamedAttribute> fieldTypesMap;
158 if (!fieldNames.empty()) {
159 for (
auto [name, type] : zip(fieldNames, fieldTypes))
160 fieldTypesMap.push_back(
161 NamedAttribute(cast<StringAttr>(name), TypeAttr::get(type)));
163 auto *ctx = parser.getContext();
164 state.addAttribute(
"fieldNames", mlir::ArrayAttr::get(ctx, fieldNames));
165 state.addAttribute(
"fieldTypes",
166 mlir::DictionaryAttr::get(ctx, fieldTypesMap));
169 if (failed(parser.parseOptionalAttrDictWithKeyword(state.attributes)))
173 Region *region = state.addRegion();
174 if (parser.parseRegion(*region, args))
179 region->emplaceBlock();
182 auto argNames = llvm::map_range(args, [&](OpAsmParser::Argument arg) {
183 return StringAttr::get(parser.getContext(), arg.ssaName.name.drop_front());
187 ArrayAttr::get(parser.getContext(), SmallVector<Attribute>(argNames)));
196 StringRef visibilityAttrName = SymbolTable::getVisibilityAttrName();
197 if (
auto visibility =
198 classLike->getAttrOfType<StringAttr>(visibilityAttrName))
199 printer << visibility.getValue() <<
' ';
202 printer.printSymbolName(classLike.getSymName());
205 auto argNames = SmallVector<StringRef>(
206 classLike.getFormalParamNames().getAsValueRange<StringAttr>());
207 ArrayRef<BlockArgument> args = classLike.getBodyBlock()->getArguments();
211 for (
size_t i = 0, e = args.size(); i < e; ++i) {
212 printer <<
'%' << argNames[i] <<
": " << args[i].getType();
218 ArrayRef<Attribute> fieldNames =
219 cast<ArrayAttr>(classLike->getAttr(
"fieldNames")).getValue();
221 if (!fieldNames.empty()) {
223 for (
size_t i = 0, e = fieldNames.size(); i < e; ++i) {
226 StringAttr name = cast<StringAttr>(fieldNames[i]);
227 printer.printKeywordOrString(name.getValue());
229 Type type = classLike.getFieldType(name).value();
230 printer.printType(type);
236 SmallVector<StringRef> elidedAttrs{
237 classLike.getSymNameAttrName(), classLike.getFormalParamNamesAttrName(),
238 visibilityAttrName,
"fieldTypes",
"fieldNames"};
239 printer.printOptionalAttrDictWithKeyword(classLike.getOperation()->getAttrs(),
243 printer.printRegion(classLike.getBody(),
false,
249 if (classLike.getFormalParamNames().size() !=
250 classLike.getBodyBlock()->getArguments().size()) {
251 auto error = classLike.emitOpError(
252 "formal parameter name list doesn't match formal parameter value list");
253 error.attachNote(classLike.getLoc())
254 <<
"formal parameter names: " << classLike.getFormalParamNames();
255 error.attachNote(classLike.getLoc())
256 <<
"formal parameter values: "
257 << classLike.getBodyBlock()->getArguments();
267 auto argNames = SmallVector<StringRef>(
268 classLike.getFormalParamNames().getAsValueRange<StringAttr>());
269 ArrayRef<BlockArgument> args = classLike.getBodyBlock()->getArguments();
272 for (
size_t i = 0, e = args.size(); i < e; ++i)
273 setNameFn(args[i], argNames[i]);
277 return NamedAttribute(name, TypeAttr::get(type));
282 return NamedAttribute(StringAttr(name),
283 mlir::IntegerAttr::get(mlir::IndexType::get(ctx), i));
288 DictionaryAttr fieldTypes = mlir::cast<DictionaryAttr>(
289 classLike.getOperation()->getAttr(
"fieldTypes"));
290 Attribute type = fieldTypes.get(name);
291 if (
auto field = dyn_cast_or_null<TypeAttr>(type))
292 return field.getValue();
297 AttrTypeReplacer &replacer) {
298 classLike->setAttr(
"fieldTypes", cast<DictionaryAttr>(replacer.replace(
299 classLike.getFieldTypes())));
306ParseResult circt::om::ClassOp::parse(OpAsmParser &parser,
307 OperationState &state) {
311circt::om::ClassOp circt::om::ClassOp::buildSimpleClassOp(
312 OpBuilder &odsBuilder, Location loc, Twine name,
313 ArrayRef<StringRef> formalParamNames, ArrayRef<StringRef> fieldNames,
314 ArrayRef<Type> fieldTypes) {
315 circt::om::ClassOp classOp = circt::om::ClassOp::create(
316 odsBuilder, loc, odsBuilder.getStringAttr(name),
317 odsBuilder.getStrArrayAttr(formalParamNames),
318 odsBuilder.getStrArrayAttr(fieldNames),
319 odsBuilder.getDictionaryAttr(llvm::map_to_vector(
320 llvm::zip(fieldNames, fieldTypes), [&](
auto field) -> NamedAttribute {
321 return NamedAttribute(odsBuilder.getStringAttr(std::get<0>(field)),
322 TypeAttr::get(std::get<1>(field)));
324 Block *body = &classOp.getRegion().emplaceBlock();
325 auto prevLoc = odsBuilder.saveInsertionPoint();
326 odsBuilder.setInsertionPointToEnd(body);
328 mlir::SmallVector<Attribute> locAttrs(fieldNames.size(), LocationAttr(loc));
330 ClassFieldsOp::create(odsBuilder, loc,
331 llvm::map_to_vector(fieldTypes,
332 [&](Type type) -> Value {
333 return body->addArgument(type,
336 odsBuilder.getArrayAttr(locAttrs));
338 odsBuilder.restoreInsertionPoint(prevLoc);
343void circt::om::ClassOp::print(OpAsmPrinter &printer) {
347LogicalResult circt::om::ClassOp::verify() {
return verifyClassLike(*
this); }
349LogicalResult circt::om::ClassOp::verifyRegions() {
351 dyn_cast_or_null<ClassFieldsOp>(this->
getBodyBlock()->getTerminator());
353 return this->emitOpError(
"expected terminator to be ClassFieldsOp");
356 if (fieldsOp.getNumOperands() != this->getFieldNames().size()) {
357 auto diag = this->emitOpError()
358 <<
"returns '" << this->getFieldNames().size()
359 <<
"' fields, but its terminator returned '"
360 << fieldsOp.getNumOperands() <<
"' fields";
361 return diag.attachNote(fieldsOp.getLoc()) <<
"see terminator:";
365 auto types = this->getFieldTypes();
366 for (
auto [fieldName, terminatorOperandType] :
367 llvm::zip(this->getFieldNames(), fieldsOp.getOperandTypes())) {
369 auto fieldNameAttr = dyn_cast_or_null<StringAttr>(fieldName);
371 return this->emitOpError(
"field name is not a StringAttr");
373 if (
auto fieldType = types.get(fieldNameAttr))
374 if (
auto typeAttr = dyn_cast<TypeAttr>(fieldType))
375 if (typeAttr.getValue() == terminatorOperandType)
378 auto diag = this->emitOpError()
379 <<
"returns different field types than its terminator";
380 return diag.attachNote(fieldsOp.getLoc()) <<
"see terminator:";
386void circt::om::ClassOp::getAsmBlockArgumentNames(
391std::optional<mlir::Type>
392circt::om::ClassOp::getFieldType(mlir::StringAttr field) {
396void circt::om::ClassOp::replaceFieldTypes(AttrTypeReplacer replacer) {
400void circt::om::ClassOp::updateFields(
401 mlir::ArrayRef<mlir::Location> newLocations,
402 mlir::ArrayRef<mlir::Value> newValues,
403 mlir::ArrayRef<mlir::Attribute> newNames) {
405 auto fieldsOp = getFieldsOp();
406 assert(fieldsOp &&
"The fields op should exist");
408 SmallVector<Attribute> names(getFieldNamesAttr().getAsRange<StringAttr>());
410 SmallVector<NamedAttribute> fieldTypes(getFieldTypesAttr().getValue());
412 SmallVector<Value> fieldVals(fieldsOp.getFields());
414 Location fieldOpLoc = fieldsOp->getLoc();
417 SmallVector<Location> locations;
418 if (
auto fl = dyn_cast<FusedLoc>(fieldOpLoc)) {
419 auto metadataArr = dyn_cast<ArrayAttr>(fl.getMetadata());
420 assert(metadataArr &&
"Expected the metadata for the fused location");
421 auto r = metadataArr.getAsRange<LocationAttr>();
422 locations.append(r.begin(), r.end());
425 locations.append(names.size(), fieldOpLoc);
429 names.append(newNames.begin(), newNames.end());
430 locations.append(newLocations.begin(), newLocations.end());
431 fieldVals.append(newValues.begin(), newValues.end());
434 for (
auto [v, n] :
llvm::zip(newValues, newNames))
435 fieldTypes.emplace_back(
436 NamedAttribute(
llvm::cast<StringAttr>(n), TypeAttr::
get(v.getType())));
439 SmallVector<Attribute> locationsAttr;
440 llvm::for_each(locations, [&](Location &l) {
441 locationsAttr.push_back(cast<Attribute>(l));
444 ImplicitLocOpBuilder builder(
getLoc(), *
this);
446 setFieldNamesAttr(builder.getArrayAttr(names));
448 setFieldTypesAttr(builder.getDictionaryAttr(fieldTypes));
449 fieldsOp.getFieldsMutable().assign(fieldVals);
451 fieldsOp->setLoc(builder.getFusedLoc(
452 locations, ArrayAttr::get(getContext(), locationsAttr)));
455void circt::om::ClassOp::addNewFieldsOp(mlir::OpBuilder &builder,
456 mlir::ArrayRef<Location> locs,
457 mlir::ArrayRef<Value> values) {
460 assert(locs.size() == values.size() &&
"Expected a location per value");
461 mlir::SmallVector<Attribute> locAttrs;
462 for (
auto loc : locs) {
463 locAttrs.push_back(cast<Attribute>(LocationAttr(loc)));
467 ClassFieldsOp::create(builder, builder.getFusedLoc(locs), values,
468 builder.getArrayAttr(locAttrs));
471mlir::Location circt::om::ClassOp::getFieldLocByIndex(
size_t i) {
472 auto fieldsOp = this->getFieldsOp();
473 auto fieldLocs = fieldsOp.getFieldLocs();
474 if (!fieldLocs.has_value())
475 return fieldsOp.getLoc();
476 assert(i < fieldLocs.value().size() &&
477 "field index too large for location array");
478 return cast<LocationAttr>(fieldLocs.value()[i]);
485ParseResult circt::om::ClassExternOp::parse(OpAsmParser &parser,
486 OperationState &state) {
490void circt::om::ClassExternOp::print(OpAsmPrinter &printer) {
494LogicalResult circt::om::ClassExternOp::verify() {
500 return this->emitOpError(
"external class body should be empty");
506void circt::om::ClassExternOp::getAsmBlockArgumentNames(
511std::optional<mlir::Type>
512circt::om::ClassExternOp::getFieldType(mlir::StringAttr field) {
516void circt::om::ClassExternOp::replaceFieldTypes(AttrTypeReplacer replacer) {
524LogicalResult circt::om::ClassFieldsOp::verify() {
525 auto fieldLocs = this->getFieldLocs();
526 if (fieldLocs.has_value()) {
527 auto fieldLocsVal = fieldLocs.value();
528 if (fieldLocsVal.size() != this->getFields().size()) {
529 auto error = this->emitOpError(
"size of field_locs (")
530 << fieldLocsVal.size()
531 <<
") does not match number of fields ("
532 << this->getFields().size() <<
")";
542void circt::om::ObjectOp::build(::mlir::OpBuilder &odsBuilder,
543 ::mlir::OperationState &odsState,
545 ::mlir::ValueRange actualParams) {
546 return build(odsBuilder, odsState,
547 om::ClassType::get(odsBuilder.getContext(),
548 mlir::FlatSymbolRefAttr::get(classOp)),
549 mlir::FlatSymbolRefAttr::get(classOp.getNameAttr()),
553static FailureOr<ClassLike>
555 ClassType resultType, StringAttr className) {
556 StringAttr resultClassName = resultType.getClassName().getAttr();
557 if (resultClassName != className)
558 return op->emitOpError(
"result type (")
559 << resultClassName <<
") does not match referred to class ("
562 auto classDef = dyn_cast_or_null<ClassLike>(
563 symbolTable.lookupNearestSymbolFrom(op, className));
565 return op->emitOpError(
"refers to non-existant class (")
571circt::om::ObjectOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
574 getClassNameAttr().getAttr());
575 if (failed(classDef))
578 auto actualTypes = getActualParams().getTypes();
579 auto formalTypes = classDef->getBodyBlock()->getArgumentTypes();
582 if (actualTypes.size() != formalTypes.size()) {
583 auto error = emitOpError(
584 "actual parameter list doesn't match formal parameter list");
585 error.attachNote(classDef->getLoc())
586 <<
"formal parameters: " << classDef->getBodyBlock()->getArguments();
587 error.attachNote(
getLoc()) <<
"actual parameters: " << getActualParams();
592 for (
size_t i = 0, e = actualTypes.size(); i < e; ++i) {
593 if (actualTypes[i] != formalTypes[i]) {
594 return emitOpError(
"actual parameter type (")
595 << actualTypes[i] <<
") doesn't match formal parameter type ("
596 << formalTypes[i] <<
')';
608circt::om::ObjectFieldOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
609 auto classType = getObject().getType();
610 auto className = classType.getClassName().getAttr();
613 auto classDef = dyn_cast_or_null<ClassLike>(
614 symbolTable.lookupNearestSymbolFrom(*
this, className));
616 return emitOpError(
"class ") << className <<
" was not found";
619 auto fieldName = getFieldAttr();
620 std::optional<Type> fieldType = classDef.getFieldType(fieldName);
622 auto diag = emitOpError(
"referenced non-existent field ") << fieldName;
623 diag.attachNote(classDef.getLoc()) <<
"class defined here";
628 if (getResult().getType() != fieldType.value())
629 return emitOpError(
"expected type ")
630 << getResult().getType() <<
", but accessed field has type "
631 << fieldType.value();
639void circt::om::ElaboratedObjectOp::build(OpBuilder &odsBuilder,
640 OperationState &odsState,
641 om::ClassLike classOp,
642 ValueRange fieldValues) {
643 return build(odsBuilder, odsState,
645 odsBuilder.getContext(),
646 mlir::FlatSymbolRefAttr::get(classOp.getSymNameAttr())),
647 mlir::FlatSymbolRefAttr::get(classOp.getSymNameAttr()),
651LogicalResult circt::om::ElaboratedObjectOp::verifySymbolUses(
652 SymbolTableCollection &symbolTable) {
655 getClassNameAttr().getAttr());
656 if (failed(classDef))
659 auto fieldNames = classDef->getFieldNames();
660 auto fieldValues = getFieldValues();
661 if (fieldValues.size() != fieldNames.size())
662 return emitOpError(
"field value list doesn't match class field list, "
664 << fieldNames.size() <<
" values but got " << fieldValues.size();
666 for (
auto [fieldName, fieldValue] :
llvm::zip(fieldNames, fieldValues)) {
668 classDef->getFieldType(cast<StringAttr>(fieldName)).value();
669 if (fieldValue.getType() != expectedType)
670 return emitOpError(
"field value type for ")
671 << cast<StringAttr>(fieldName) <<
" (" << fieldValue.getType()
672 <<
") doesn't match class field type (" << expectedType <<
')';
682void circt::om::ConstantOp::build(::mlir::OpBuilder &odsBuilder,
683 ::mlir::OperationState &odsState,
684 ::mlir::TypedAttr constVal) {
685 return build(odsBuilder, odsState, constVal.getType(), constVal);
688OpFoldResult circt::om::ConstantOp::fold(FoldAdaptor adaptor) {
689 assert(adaptor.getOperands().empty() &&
"constant has no operands");
690 return getValueAttr();
697void circt::om::ListCreateOp::print(OpAsmPrinter &p) {
699 p.printOperands(getInputs());
700 p.printOptionalAttrDict((*this)->getAttrs());
701 p <<
" : " << getType().getElementType();
704ParseResult circt::om::ListCreateOp::parse(OpAsmParser &parser,
705 OperationState &result) {
706 llvm::SmallVector<OpAsmParser::UnresolvedOperand, 16> operands;
709 if (parser.parseOperandList(operands) ||
710 parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
711 parser.parseType(elemType))
713 result.addTypes({circt::om::ListType::get(elemType)});
715 for (
auto operand : operands)
716 if (parser.resolveOperand(operand, elemType, result.operands))
726BasePathCreateOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
727 auto hierPath = symbolTable.lookupNearestSymbolFrom<hw::HierPathOp>(
728 *
this, getTargetAttr());
730 return emitOpError(
"invalid symbol reference");
739PathCreateOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
740 auto hierPath = symbolTable.lookupNearestSymbolFrom<hw::HierPathOp>(
741 *
this, getTargetAttr());
743 return emitOpError(
"invalid symbol reference");
754 auto lhs = dyn_cast_or_null<circt::om::IntegerAttr>(lhsAttr);
755 auto rhs = dyn_cast_or_null<circt::om::IntegerAttr>(rhsAttr);
762 APSInt lhsVal = lhs.getValue().getAPSInt();
763 APSInt rhsVal = rhs.getValue().getAPSInt();
764 if (lhsVal.getBitWidth() > rhsVal.getBitWidth())
765 rhsVal = rhsVal.extend(lhsVal.getBitWidth());
766 else if (rhsVal.getBitWidth() > lhsVal.getBitWidth())
767 lhsVal = lhsVal.extend(rhsVal.getBitWidth());
770 auto result = op.evaluateIntegerOperation(lhsVal, rhsVal);
774 auto *ctx = op.getContext();
776 return circt::om::IntegerAttr::get(
777 ctx, mlir::IntegerAttr::get(ctx, result.value()));
784FailureOr<llvm::APSInt>
785IntegerAddOp::evaluateIntegerOperation(
const llvm::APSInt &lhs,
786 const llvm::APSInt &rhs) {
787 return success(lhs + rhs);
790OpFoldResult IntegerAddOp::fold(FoldAdaptor adaptor) {
798FailureOr<llvm::APSInt>
799IntegerMulOp::evaluateIntegerOperation(
const llvm::APSInt &lhs,
800 const llvm::APSInt &rhs) {
801 return success(lhs * rhs);
804OpFoldResult IntegerMulOp::fold(FoldAdaptor adaptor) {
812FailureOr<llvm::APSInt>
813IntegerShrOp::evaluateIntegerOperation(
const llvm::APSInt &lhs,
814 const llvm::APSInt &rhs) {
816 if (!rhs.isNonNegative())
817 return emitOpError(
"shift amount must be non-negative");
819 if (!rhs.isRepresentableByInt64())
820 return emitOpError(
"shift amount must be representable in 64 bits");
821 return success(lhs >> rhs.getExtValue());
824OpFoldResult IntegerShrOp::fold(FoldAdaptor adaptor) {
832FailureOr<llvm::APSInt>
833IntegerShlOp::evaluateIntegerOperation(
const llvm::APSInt &lhs,
834 const llvm::APSInt &rhs) {
836 if (!rhs.isNonNegative())
837 return emitOpError(
"shift amount must be non-negative");
839 if (!rhs.isRepresentableByInt64())
840 return emitOpError(
"shift amount must be representable in 64 bits");
842 int64_t shiftAmt = rhs.getExtValue();
844 return success(lhs.extend(lhs.getBitWidth() + shiftAmt) << shiftAmt);
847OpFoldResult IntegerShlOp::fold(FoldAdaptor adaptor) {
855OpFoldResult StringConcatOp::fold(FoldAdaptor adaptor) {
857 if (getStrings().size() == 1) {
858 if (
auto strAttr = adaptor.getStrings()[0])
861 return getStrings()[0];
865 if (!llvm::all_of(adaptor.getStrings(), [](Attribute operand) {
866 return isa_and_nonnull<StringAttr>(operand);
871 SmallString<64> result;
872 for (
auto operand : adaptor.getStrings())
873 result += cast<StringAttr>(operand).getValue();
875 return StringAttr::get(result, getResult().getType());
883 using OpRewritePattern::OpRewritePattern;
886 matchAndRewrite(StringConcatOp concat,
887 mlir::PatternRewriter &rewriter)
const override {
891 bool hasNestedConcat = llvm::any_of(concat.getStrings(), [](Value operand) {
892 auto nestedConcat = operand.getDefiningOp<StringConcatOp>();
893 return nestedConcat && operand.hasOneUse();
896 if (!hasNestedConcat)
900 SmallVector<Value> flatOperands;
901 for (
auto input : concat.getStrings()) {
902 if (
auto nestedConcat = input.getDefiningOp<StringConcatOp>();
903 nestedConcat && input.hasOneUse())
904 llvm::append_range(flatOperands, nestedConcat.getStrings());
906 flatOperands.push_back(input);
909 rewriter.modifyOpInPlace(concat,
910 [&]() { concat->setOperands(flatOperands); });
917class MergeAdjacentOMStringConstants
920 using OpRewritePattern::OpRewritePattern;
923 matchAndRewrite(StringConcatOp concat,
924 mlir::PatternRewriter &rewriter)
const override {
926 SmallVector<Value> newOperands;
927 SmallString<64> accumulatedLit;
928 SmallVector<ConstantOp> accumulatedOps;
929 bool changed =
false;
931 auto flushLiterals = [&]() {
932 if (accumulatedOps.empty())
936 if (accumulatedOps.size() == 1) {
937 newOperands.push_back(accumulatedOps[0]);
940 auto newLit = rewriter.createOrFold<ConstantOp>(
942 StringAttr::get(accumulatedLit, concat.getResult().getType()));
943 newOperands.push_back(newLit);
946 accumulatedLit.clear();
947 accumulatedOps.clear();
950 for (
auto operand : concat.getStrings()) {
951 if (
auto litOp = operand.getDefiningOp<ConstantOp>()) {
952 if (
auto strAttr = dyn_cast<StringAttr>(litOp.getValue())) {
954 if (strAttr.getValue().empty()) {
958 accumulatedLit += strAttr.getValue();
959 accumulatedOps.push_back(litOp);
965 newOperands.push_back(operand);
975 if (newOperands.empty())
976 return rewriter.replaceOpWithNewOp<ConstantOp>(
977 concat, StringAttr::get(
"", concat.getResult().getType())),
981 rewriter.modifyOpInPlace(concat,
982 [&]() { concat->setOperands(newOperands); });
989void StringConcatOp::getCanonicalizationPatterns(RewritePatternSet &results,
991 results.insert<FlattenOMStringConcat, MergeAdjacentOMStringConstants>(
999FailureOr<mlir::Attribute>
1000PropEqOp::evaluateBinaryEquality(mlir::Attribute lhsAttr,
1001 mlir::Attribute rhsAttr) {
1002 auto resultType = mlir::IntegerType::get(getContext(), 1);
1005 if (
auto lhs = dyn_cast<mlir::StringAttr>(lhsAttr))
1006 if (
auto rhs = dyn_cast<mlir::StringAttr>(rhsAttr))
1007 return mlir::Attribute(
1008 mlir::IntegerAttr::get(resultType, lhs == rhs ? 1 : 0));
1011 if (
auto lhs = dyn_cast<om::IntegerAttr>(lhsAttr))
1012 if (
auto rhs = dyn_cast<om::IntegerAttr>(rhsAttr)) {
1013 APSInt lhsVal = lhs.getValue().getAPSInt();
1014 APSInt rhsVal = rhs.getValue().getAPSInt();
1015 if (lhsVal.getBitWidth() > rhsVal.getBitWidth())
1016 rhsVal = rhsVal.extend(lhsVal.getBitWidth());
1017 else if (rhsVal.getBitWidth() > lhsVal.getBitWidth())
1018 lhsVal = lhsVal.extend(rhsVal.getBitWidth());
1019 return mlir::Attribute(
1020 mlir::IntegerAttr::get(resultType, lhsVal == rhsVal ? 1 : 0));
1024 if (
auto lhs = dyn_cast<mlir::IntegerAttr>(lhsAttr))
1025 if (
auto rhs = dyn_cast<mlir::IntegerAttr>(rhsAttr))
1026 return mlir::Attribute(
1027 mlir::IntegerAttr::get(resultType, lhs == rhs ? 1 : 0));
1032OpFoldResult PropEqOp::fold(FoldAdaptor adaptor) {
1033 auto lhsAttr = adaptor.getLhs();
1034 auto rhsAttr = adaptor.getRhs();
1035 if (!lhsAttr || !rhsAttr)
1038 auto result = evaluateBinaryEquality(lhsAttr, rhsAttr);
1050 Attribute rhsAttr) {
1051 auto lhsInt = dyn_cast_or_null<mlir::IntegerAttr>(lhsAttr);
1052 auto rhsInt = dyn_cast_or_null<mlir::IntegerAttr>(rhsAttr);
1053 if (!lhsInt || !rhsInt)
1055 APSInt lhsVal(lhsInt.getValue());
1056 APSInt rhsVal(rhsInt.getValue());
1057 auto result = op.evaluateIntegerOperation(lhsVal, rhsVal);
1060 return mlir::IntegerAttr::get(
1061 lhsInt.getType(), result->extOrTrunc(lhsInt.getValue().getBitWidth()));
1066 auto i = dyn_cast_or_null<mlir::IntegerAttr>(a);
1067 return i && i.getValue().isZero();
1072 auto i = dyn_cast_or_null<mlir::IntegerAttr>(a);
1073 return i && i.getValue().isAllOnes();
1076FailureOr<APSInt> IntegerAndOp::evaluateIntegerOperation(
const APSInt &lhs,
1077 const APSInt &rhs) {
1078 return success(APSInt(lhs & rhs,
false));
1081OpFoldResult IntegerAndOp::fold(FoldAdaptor adaptor) {
1087 return mlir::IntegerAttr::get(getResult().getType(),
1088 APInt::getZero(getType().
getWidth()));
1097FailureOr<APSInt> IntegerOrOp::evaluateIntegerOperation(
const APSInt &lhs,
1098 const APSInt &rhs) {
1099 return success(APSInt(lhs | rhs,
false));
1102OpFoldResult IntegerOrOp::fold(FoldAdaptor adaptor) {
1108 return mlir::IntegerAttr::get(getResult().getType(),
1109 APInt::getAllOnes(getType().
getWidth()));
1118FailureOr<APSInt> IntegerXorOp::evaluateIntegerOperation(
const APSInt &lhs,
1119 const APSInt &rhs) {
1120 return success(APSInt(lhs ^ rhs,
false));
1123OpFoldResult IntegerXorOp::fold(FoldAdaptor adaptor) {
1139LogicalResult circt::om::UnknownValueOp::verifySymbolUses(
1140 SymbolTableCollection &symbolTable) {
1143 auto classType = dyn_cast<ClassType>(getType());
1148 auto className = classType.getClassName();
1149 if (symbolTable.lookupNearestSymbolFrom<ClassLike>(*
this, className))
1152 return emitOpError() <<
"refers to non-existant class (\""
1153 << className.getValue() <<
"\")";
1160#define GET_OP_CLASSES
1161#include "circt/Dialect/OM/OM.cpp.inc"
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static Location getLoc(DefSlot slot)
static ParseResult parseClassLike(OpAsmParser &parser, OperationState &state)
LogicalResult verifyClassLike(ClassLike classLike)
std::optional< Type > getClassLikeFieldType(ClassLike classLike, StringAttr name)
void getClassLikeAsmBlockArgumentNames(ClassLike classLike, Region ®ion, OpAsmSetValueNameFn setNameFn)
static ParseResult parseBasePathString(OpAsmParser &parser, PathAttr &path)
static ParseResult parsePathString(OpAsmParser &parser, PathAttr &path, StringAttr &module, StringAttr &ref, StringAttr &field)
static void printBasePathString(OpAsmPrinter &p, Operation *op, PathAttr path)
static OpFoldResult foldIntegerBitwise(IntegerBinaryOp op, Attribute lhsAttr, Attribute rhsAttr)
static FailureOr< ClassLike > verifyClassLikeSymbolUser(Operation *op, SymbolTableCollection &symbolTable, ClassType resultType, StringAttr className)
static void printFieldLocs(OpAsmPrinter &printer, Operation *op, ArrayAttr fieldLocs)
static OpFoldResult foldIntegerBinaryArithmetic(IntegerBinaryOp op, Attribute lhsAttr, Attribute rhsAttr)
static bool isZeroInt(Attribute a)
static ParseResult parseFieldLocs(OpAsmParser &parser, ArrayAttr &fieldLocs)
static ParseResult parseClassFieldsList(OpAsmParser &parser, SmallVectorImpl< Attribute > &fieldNames, SmallVectorImpl< Type > &fieldTypes)
static void printClassLike(ClassLike classLike, OpAsmPrinter &printer)
void replaceClassLikeFieldTypes(ClassLike classLike, AttrTypeReplacer &replacer)
NamedAttribute makeFieldType(StringAttr name, Type type)
NamedAttribute makeFieldIdx(MLIRContext *ctx, mlir::StringAttr name, unsigned i)
static void printPathString(OpAsmPrinter &p, Operation *op, PathAttr path, StringAttr module, StringAttr ref, StringAttr field)
static bool isAllOnesInt(Attribute a)
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.
uint64_t getWidth(Type t)
void error(Twine message)
ParseResult parsePath(MLIRContext *context, StringRef spelling, PathAttr &path, StringAttr &module, StringAttr &ref, StringAttr &field)
Parse a target string in to a path.
ParseResult parseBasePath(MLIRContext *context, StringRef spelling, PathAttr &path)
Parse a target string of the form "Foo/bar:Bar/baz" in to a base path.
function_ref< void(Value, StringRef)> OpAsmSetValueNameFn
A module name, and the name of an instance inside that module.
mlir::StringAttr mlir::StringAttr instance