@@ -122,7 +122,7 @@ class PatternEmitter {
122
122
123
123
// Emits C++ statements for matching the `argIndex`-th argument of the given
124
124
// DAG `tree` as an attribute.
125
- void emitAttributeMatch (DagNode tree, StringRef opName , int argIndex,
125
+ void emitAttributeMatch (DagNode tree, StringRef castedName , int argIndex,
126
126
int depth);
127
127
128
128
// Emits C++ for checking a match with a corresponding match failure
@@ -664,7 +664,7 @@ void PatternEmitter::emitOpMatch(DagNode tree, StringRef opName, int depth) {
664
664
/* variadicSubIndex=*/ std::nullopt);
665
665
++nextOperand;
666
666
} else if (isa<NamedAttribute *>(opArg)) {
667
- emitAttributeMatch (tree, opName , opArgIdx, depth);
667
+ emitAttributeMatch (tree, castedName , opArgIdx, depth);
668
668
} else {
669
669
PrintFatalError (loc, " unhandled case when matching op" );
670
670
}
@@ -864,16 +864,22 @@ void PatternEmitter::emitVariadicOperandMatch(DagNode tree,
864
864
os.unindent () << " }\n " ;
865
865
}
866
866
867
- void PatternEmitter::emitAttributeMatch (DagNode tree, StringRef opName ,
867
+ void PatternEmitter::emitAttributeMatch (DagNode tree, StringRef castedName ,
868
868
int argIndex, int depth) {
869
869
Operator &op = tree.getDialectOp (opMap);
870
870
auto *namedAttr = cast<NamedAttribute *>(op.getArg (argIndex));
871
871
const auto &attr = namedAttr->attr ;
872
872
873
873
os << " {\n " ;
874
- os.indent () << formatv (" auto tblgen_attr = {0}->getAttrOfType<{1}>(\" {2}\" );"
875
- " (void)tblgen_attr;\n " ,
876
- opName, attr.getStorageType (), namedAttr->name );
874
+ if (op.getDialect ().usePropertiesForAttributes ()) {
875
+ os.indent () << formatv (" auto tblgen_attr = {0}.getProperties().{1}();\n " ,
876
+ castedName, op.getGetterName (namedAttr->name ));
877
+ } else {
878
+ os.indent () << formatv (
879
+ " auto tblgen_attr = {0}->getAttrOfType<{1}>(\" {2}\" );"
880
+ " (void)tblgen_attr;\n " ,
881
+ castedName, attr.getStorageType (), namedAttr->name );
882
+ }
877
883
878
884
// TODO: This should use getter method to avoid duplication.
879
885
if (attr.hasDefaultValue ()) {
@@ -887,7 +893,7 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
887
893
// That is precisely what getDiscardableAttr() returns on missing
888
894
// attributes.
889
895
} else {
890
- emitMatchCheck (opName , tgfmt (" tblgen_attr" , &fmtCtx),
896
+ emitMatchCheck (castedName , tgfmt (" tblgen_attr" , &fmtCtx),
891
897
formatv (" \" expected op '{0}' to have attribute '{1}' "
892
898
" of type '{2}'\" " ,
893
899
op.getOperationName (), namedAttr->name ,
@@ -918,7 +924,7 @@ void PatternEmitter::emitAttributeMatch(DagNode tree, StringRef opName,
918
924
}
919
925
}
920
926
emitStaticVerifierCall (
921
- verifier, opName , " tblgen_attr" ,
927
+ verifier, castedName , " tblgen_attr" ,
922
928
formatv (" \" op '{0}' attribute '{1}' failed to satisfy constraint: "
923
929
" '{2}'\" " ,
924
930
op.getOperationName (), namedAttr->name ,
@@ -1532,6 +1538,7 @@ std::string PatternEmitter::handleOpCreation(DagNode tree, int resultIndex,
1532
1538
LLVM_DEBUG (llvm::dbgs () << ' \n ' );
1533
1539
1534
1540
Operator &resultOp = tree.getDialectOp (opMap);
1541
+ bool useProperties = resultOp.getDialect ().usePropertiesForAttributes ();
1535
1542
auto numOpArgs = resultOp.getNumArgs ();
1536
1543
auto numPatArgs = tree.getNumArgs ();
1537
1544
@@ -1623,9 +1630,10 @@ std::string PatternEmitter::handleOpCreation(DagNode tree, int resultIndex,
1623
1630
createAggregateLocalVarsForOpArgs (tree, childNodeNames, depth);
1624
1631
1625
1632
// Then create the op.
1626
- os.scope (" " , " \n }\n " ).os << formatv (
1627
- " {0} = rewriter.create<{1}>({2}, tblgen_values, tblgen_attrs);" ,
1628
- valuePackName, resultOp.getQualCppClassName (), locToUse);
1633
+ os.scope (" " , " \n }\n " ).os
1634
+ << formatv (" {0} = rewriter.create<{1}>({2}, tblgen_values, {3});" ,
1635
+ valuePackName, resultOp.getQualCppClassName (), locToUse,
1636
+ useProperties ? " tblgen_props" : " tblgen_attrs" );
1629
1637
return resultValue;
1630
1638
}
1631
1639
@@ -1682,8 +1690,9 @@ std::string PatternEmitter::handleOpCreation(DagNode tree, int resultIndex,
1682
1690
}
1683
1691
}
1684
1692
os << formatv (" {0} = rewriter.create<{1}>({2}, tblgen_types, "
1685
- " tblgen_values, tblgen_attrs);\n " ,
1686
- valuePackName, resultOp.getQualCppClassName (), locToUse);
1693
+ " tblgen_values, {3});\n " ,
1694
+ valuePackName, resultOp.getQualCppClassName (), locToUse,
1695
+ useProperties ? " tblgen_props" : " tblgen_attrs" );
1687
1696
os.unindent () << " }\n " ;
1688
1697
return resultValue;
1689
1698
}
@@ -1791,16 +1800,27 @@ void PatternEmitter::createAggregateLocalVarsForOpArgs(
1791
1800
DagNode node, const ChildNodeIndexNameMap &childNodeNames, int depth) {
1792
1801
Operator &resultOp = node.getDialectOp (opMap);
1793
1802
1803
+ bool useProperties = resultOp.getDialect ().usePropertiesForAttributes ();
1794
1804
auto scope = os.scope ();
1795
1805
os << formatv (" ::llvm::SmallVector<::mlir::Value, 4> "
1796
1806
" tblgen_values; (void)tblgen_values;\n " );
1797
- os << formatv (" ::llvm::SmallVector<::mlir::NamedAttribute, 4> "
1798
- " tblgen_attrs; (void)tblgen_attrs;\n " );
1807
+ if (useProperties) {
1808
+ os << formatv (" {0}::Properties tblgen_props; (void)tblgen_props;\n " ,
1809
+ resultOp.getQualCppClassName ());
1810
+ } else {
1811
+ os << formatv (" ::llvm::SmallVector<::mlir::NamedAttribute, 4> "
1812
+ " tblgen_attrs; (void)tblgen_attrs;\n " );
1813
+ }
1799
1814
1815
+ const char *setPropCmd =
1816
+ " tblgen_props.{0} = "
1817
+ " ::llvm::dyn_cast_if_present<decltype(tblgen_props.{0})>({1});\n " ;
1800
1818
const char *addAttrCmd =
1801
1819
" if (auto tmpAttr = {1}) {\n "
1802
1820
" tblgen_attrs.emplace_back(rewriter.getStringAttr(\" {0}\" ), "
1803
1821
" tmpAttr);\n }\n " ;
1822
+ const char *setterCmd = (useProperties) ? setPropCmd : addAttrCmd;
1823
+
1804
1824
int numVariadic = 0 ;
1805
1825
bool hasOperandSegmentSizes = false ;
1806
1826
std::vector<std::string> sizes;
@@ -1814,13 +1834,13 @@ void PatternEmitter::createAggregateLocalVarsForOpArgs(
1814
1834
if (!subTree.isNativeCodeCall ())
1815
1835
PrintFatalError (loc, " only NativeCodeCall allowed in nested dag node "
1816
1836
" for creating attribute" );
1817
- os << formatv (addAttrCmd, opArgName, childNodeNames.lookup (argIndex));
1837
+
1838
+ os << formatv (setterCmd, opArgName, childNodeNames.lookup (argIndex));
1818
1839
} else {
1819
1840
auto leaf = node.getArgAsLeaf (argIndex);
1820
1841
// The argument in the result DAG pattern.
1821
1842
auto patArgName = node.getArgName (argIndex);
1822
- os << formatv (addAttrCmd, opArgName,
1823
- handleOpArgument (leaf, patArgName));
1843
+ os << formatv (setterCmd, opArgName, handleOpArgument (leaf, patArgName));
1824
1844
}
1825
1845
continue ;
1826
1846
}
@@ -1876,11 +1896,18 @@ void PatternEmitter::createAggregateLocalVarsForOpArgs(
1876
1896
const auto *sameVariadicSize =
1877
1897
resultOp.getTrait (" ::mlir::OpTrait::SameVariadicOperandSize" );
1878
1898
if (!sameVariadicSize) {
1879
- const char *setSizes = R"(
1880
- tblgen_attrs.emplace_back(rewriter.getStringAttr("operandSegmentSizes"),
1881
- rewriter.getDenseI32ArrayAttr({{ {0} }));
1882
- )" ;
1883
- os.printReindented (formatv (setSizes, llvm::join (sizes, " , " )).str ());
1899
+ if (useProperties) {
1900
+ const char *setSizes = R"(
1901
+ tblgen_props.operandSegmentSizes = {{ {0} };
1902
+ )" ;
1903
+ os.printReindented (formatv (setSizes, llvm::join (sizes, " , " )).str ());
1904
+ } else {
1905
+ const char *setSizes = R"(
1906
+ tblgen_attrs.emplace_back(rewriter.getStringAttr("operandSegmentSizes"),
1907
+ rewriter.getDenseI32ArrayAttr({{ {0} }));
1908
+ )" ;
1909
+ os.printReindented (formatv (setSizes, llvm::join (sizes, " , " )).str ());
1910
+ }
1884
1911
}
1885
1912
}
1886
1913
}
0 commit comments