@@ -40,13 +40,29 @@ static void addToPassThroughAttr(NamedAttribute &PassThroughAttr,
40
40
std::vector<mlir::Attribute> Vec =
41
41
PassThroughAttr.getValue ().cast <ArrayAttr>().getValue ().vec ();
42
42
43
- // TODO: find a way to add the attributes only if one does not exist already,
44
- // and keep the list in sorted order.
43
+ // TODO: find a way to add the attributes only if one does not exist already.
45
44
if (Attr.getValue ().isa <UnitAttr>())
46
45
Vec.push_back (Attr.getName ());
47
46
else
48
47
Vec.push_back (ArrayAttr::get (&Ctx, {Attr.getName (), Attr.getValue ()}));
49
48
49
+ auto Comp = [&](const mlir::Attribute &A1, const mlir::Attribute &A2) {
50
+ assert (A1.isa <StringAttr>() || A1.isa <ArrayAttr>());
51
+ assert (A2.isa <StringAttr>() || A2.isa <ArrayAttr>());
52
+
53
+ if (auto StrA1 = A1.dyn_cast <StringAttr>()) {
54
+ if (auto StrA2 = A2.dyn_cast <StringAttr>())
55
+ return StrA1 < StrA2;
56
+ return true ;
57
+ }
58
+
59
+ auto ArrA1 = A1.cast <ArrayAttr>();
60
+ if (auto ArrA2 = A2.dyn_cast <ArrayAttr>())
61
+ return ArrA1[0 ].cast <StringAttr>() < ArrA2[0 ].cast <StringAttr>();
62
+ return false ;
63
+ };
64
+
65
+ llvm::sort (Vec.begin (), Vec.end (), Comp);
50
66
PassThroughAttr.setValue (ArrayAttr::get (&Ctx, Vec));
51
67
52
68
LLVM_DEBUG ({
@@ -81,26 +97,36 @@ static void addToPassThroughAttr(mlir::NamedAttribute &PassThroughAttr,
81
97
// AttributeList Method Implementations
82
98
// ===----------------------------------------------------------------------===//
83
99
100
+ AttributeList::AttributeList (const mlir::NamedAttrList &FnAttrs,
101
+ const mlir::NamedAttrList &RetAttrs,
102
+ llvm::ArrayRef<mlir::NamedAttrList> ParamAttrs)
103
+ : FnAttrs(FnAttrs), RetAttrs(RetAttrs), ParamAttrs(ParamAttrs) {}
104
+
84
105
AttributeList &
85
106
AttributeList::addAttrs (const AttrBuilder &FnAttrB, const AttrBuilder &RetAttrB,
86
107
llvm::ArrayRef<mlir::NamedAttrList> Attrs) {
87
108
return addFnAttrs (FnAttrB).addRetAttrs (RetAttrB).addParamAttrs (Attrs);
88
109
}
89
110
90
111
AttributeList &AttributeList::addFnAttrs (const AttrBuilder &B) {
91
- for (const NamedAttribute &NewNamedAttr : B.getAttrs ()) {
92
- Optional<NamedAttribute> ExistingAttr =
93
- FnAttrs.getNamed (NewNamedAttr.getName ());
94
- if (!ExistingAttr) {
95
- FnAttrs.append (NewNamedAttr);
112
+ return addFnAttrs (B.getAttrs (), B.getContext ());
113
+ }
114
+
115
+ AttributeList &AttributeList::addFnAttrs (const NamedAttrList &Attrs,
116
+ MLIRContext &Ctx) {
117
+ for (const NamedAttribute &NewFnAttr : Attrs) {
118
+ Optional<NamedAttribute> ExistingFnAttr =
119
+ FnAttrs.getNamed (NewFnAttr.getName ());
120
+ if (!ExistingFnAttr) {
121
+ FnAttrs.append (NewFnAttr);
96
122
continue ;
97
123
}
98
124
99
125
// Merge the 'passthrough' attribute lists.
100
- if (ExistingAttr ->getName () == PassThroughAttrName) {
101
- auto Attrs = NewNamedAttr .getValue ().cast <ArrayAttr>();
102
- addToPassThroughAttr (*ExistingAttr , Attrs, B. getContext () );
103
- FnAttrs.set (ExistingAttr ->getName (), ExistingAttr ->getValue ());
126
+ if (ExistingFnAttr ->getName () == PassThroughAttrName) {
127
+ auto Attrs = NewFnAttr .getValue ().cast <ArrayAttr>();
128
+ addToPassThroughAttr (*ExistingFnAttr , Attrs, Ctx );
129
+ FnAttrs.set (ExistingFnAttr ->getName (), ExistingFnAttr ->getValue ());
104
130
continue ;
105
131
}
106
132
@@ -111,7 +137,12 @@ AttributeList &AttributeList::addFnAttrs(const AttrBuilder &B) {
111
137
}
112
138
113
139
AttributeList &AttributeList::addRetAttrs (const AttrBuilder &B) {
114
- for (const NamedAttribute &NewNamedAttr : B.getAttrs ()) {
140
+ return addRetAttrs (B.getAttrs (), B.getContext ());
141
+ }
142
+
143
+ AttributeList &AttributeList::addRetAttrs (const mlir::NamedAttrList &Attrs,
144
+ mlir::MLIRContext &Ctx) {
145
+ for (const NamedAttribute &NewNamedAttr : Attrs) {
115
146
Optional<NamedAttribute> ExistingAttr =
116
147
RetAttrs.getNamed (NewNamedAttr.getName ());
117
148
if (!ExistingAttr) {
0 commit comments