Skip to content

Commit 936e69f

Browse files
committed
Optional attributes
1 parent 1e1bf79 commit 936e69f

File tree

6 files changed

+141
-36
lines changed

6 files changed

+141
-36
lines changed

mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,10 @@ def IRDL_AttributesOp : IRDL_Op<"attributes", [HasParent<"OperationOp">]> {
328328
}];
329329

330330
let arguments = (ins Variadic<IRDL_AttributeType>:$attributeValues,
331-
StrArrayAttr:$attributeValueNames);
331+
StrArrayAttr:$attributeValueNames,
332+
VariadicityArrayAttr:$variadicity);
332333
let assemblyFormat = [{
333-
custom<AttributesOp>($attributeValues, $attributeValueNames) attr-dict
334+
custom<AttributesOp>($attributeValues, $attributeValueNames, $variadicity) attr-dict
334335
}];
335336

336337
let hasVerifier = true;

mlir/lib/Dialect/IRDL/IR/IRDL.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,29 @@ LogicalResult ResultsOp::verify() {
186186
LogicalResult AttributesOp::verify() {
187187
size_t namesSize = getAttributeValueNames().size();
188188
size_t valuesSize = getAttributeValues().size();
189+
auto variadicities = getVariadicity();
190+
size_t numVariadicities = variadicities.size();
191+
192+
if (numVariadicities != valuesSize)
193+
return emitOpError()
194+
<< "the number of attributes and their variadicities must be "
195+
"the same, but got "
196+
<< valuesSize << " and " << numVariadicities << " respectively";
197+
198+
for (size_t i = 0; i < numVariadicities; ++i) {
199+
if (variadicities[i].getValue() == Variadicity::variadic) {
200+
// auto test =
201+
return emitOpError() << "requires attributes to have single or optional variadicity, but " << getAttributeValueNames()[i]
202+
<< " was specified as variadic.";
203+
204+
205+
}
206+
}
189207

190208
if (namesSize != valuesSize)
191209
return emitOpError()
192210
<< "the number of attribute names and their constraints must be "
193-
"the same but got "
211+
"the same, but got "
194212
<< namesSize << " and " << valuesSize << " respectively";
195213

196214
return success();
@@ -383,30 +401,42 @@ static void printNamedValueListWithVariadicity(
383401
static ParseResult
384402
parseAttributesOp(OpAsmParser &p,
385403
SmallVectorImpl<OpAsmParser::UnresolvedOperand> &attrOperands,
386-
ArrayAttr &attrNamesAttr) {
404+
ArrayAttr &attrNamesAttr,
405+
VariadicityArrayAttr &variadicityAttr) {
387406
Builder &builder = p.getBuilder();
407+
MLIRContext *ctx = builder.getContext();
388408
SmallVector<Attribute> attrNames;
409+
SmallVector<VariadicityAttr> variadicities;
410+
389411
if (succeeded(p.parseOptionalLBrace())) {
390412
auto parseOperands = [&]() {
391413
if (p.parseAttribute(attrNames.emplace_back()) || p.parseEqual() ||
392-
p.parseOperand(attrOperands.emplace_back()))
414+
parseValueWithVariadicity(p, attrOperands.emplace_back(),
415+
variadicities.emplace_back()))
393416
return failure();
394417
return success();
395418
};
396419
if (p.parseCommaSeparatedList(parseOperands) || p.parseRBrace())
397420
return failure();
398421
}
399422
attrNamesAttr = builder.getArrayAttr(attrNames);
423+
variadicityAttr = VariadicityArrayAttr::get(ctx, variadicities);
400424
return success();
401425
}
402426

403427
static void printAttributesOp(OpAsmPrinter &p, AttributesOp op,
404-
OperandRange attrArgs, ArrayAttr attrNames) {
428+
OperandRange attrArgs, ArrayAttr attrNames,
429+
VariadicityArrayAttr variadicityAttr) {
405430
if (attrNames.empty())
406431
return;
407432
p << "{";
408-
interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
409-
[&](int i) { p << attrNames[i] << " = " << attrArgs[i]; });
433+
interleaveComma(llvm::seq<int>(0, attrNames.size()), p, [&](int i) {
434+
Variadicity variadicity = variadicityAttr[i].getValue();
435+
p << attrNames[i] << " = ";
436+
if (variadicity != Variadicity::single)
437+
p << stringifyVariadicity(variadicity) << " ";
438+
p << attrArgs[i];
439+
});
410440
p << '}';
411441
}
412442

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// RUN: mlir-opt %s -verify-diagnostics -split-input-file
2+
23
irdl.dialect @errors {
34
irdl.operation @attrs1 {
45
%0 = irdl.is i32
56
%1 = irdl.is i64
67

7-
// expected-error@+1 {{'irdl.attributes' op the number of attribute names and their constraints must be the same but got 1 and 2 respectively}}
8-
"irdl.attributes"(%0, %1) <{attributeValueNames = ["attr1"]}> : (!irdl.attribute, !irdl.attribute) -> ()
8+
// expected-error@+1 {{'irdl.attributes' op the number of attribute names and their constraints must be the same, but got 1 and 2 respectively}}
9+
"irdl.attributes"(%0, %1) <{attributeValueNames = ["attr1"], variadicity = #irdl<variadicity_array[ single, single]>}> : (!irdl.attribute, !irdl.attribute) -> ()
910
}
1011
}
1112

@@ -15,8 +16,8 @@ irdl.dialect @errors {
1516
irdl.operation @attrs2 {
1617
%0 = irdl.is i32
1718
%1 = irdl.is i64
18-
19-
// expected-error@+1 {{'irdl.attributes' op the number of attribute names and their constraints must be the same but got 2 and 1 respectively}}
20-
"irdl.attributes"(%0) <{attributeValueNames = ["attr1", "attr2"]}> : (!irdl.attribute) -> ()
19+
20+
// expected-error@+1 {{'irdl.attributes' op the number of attribute names and their constraints must be the same, but got 2 and 1 respectively}}
21+
"irdl.attributes"(%0) <{attributeValueNames = ["attr1", "attr2"], variadicity = #irdl<variadicity_array[ single]>}> : (!irdl.attribute) -> ()
2122
}
2223
}

mlir/test/Dialect/IRDL/variadics-error.irdl.mlir

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ irdl.dialect @errors {
1414
irdl.dialect @errors {
1515
irdl.operation @operands2 {
1616
%0 = irdl.is i32
17-
17+
1818
// expected-error@+1 {{'irdl.operands' op the number of operands and their variadicities must be the same, but got 1 and 2 respectively}}
1919
"irdl.operands"(%0) <{names = ["foo"], variadicity = #irdl<variadicity_array[single, single]>}> : (!irdl.attribute) -> ()
2020
}
@@ -36,8 +36,56 @@ irdl.dialect @errors {
3636
irdl.dialect @errors {
3737
irdl.operation @results2 {
3838
%0 = irdl.is i32
39-
39+
4040
// expected-error@+1 {{'irdl.results' op the number of results and their variadicities must be the same, but got 1 and 2 respectively}}
4141
"irdl.results"(%0) <{names = ["foo"], variadicity = #irdl<variadicity_array[single, single]>}> : (!irdl.attribute) -> ()
4242
}
4343
}
44+
45+
// -----
46+
47+
irdl.dialect @errors {
48+
irdl.operation @no_var {
49+
%0 = irdl.is i32
50+
%1 = irdl.is i64
51+
52+
// expected-error@+1 {{'irdl.attributes' op requires attribute 'variadicity'}}
53+
"irdl.attributes"(%0) <{attributeValueNames = ["attr1"]}> : (!irdl.attribute) -> ()
54+
}
55+
}
56+
57+
// -----
58+
59+
irdl.dialect @errors {
60+
irdl.operation @attrs1 {
61+
%0 = irdl.is i32
62+
%1 = irdl.is i64
63+
64+
// expected-error@+1 {{'irdl.attributes' op the number of attributes and their variadicities must be the same, but got 2 and 1 respectively}}
65+
"irdl.attributes"(%0, %1) <{attributeValueNames = ["attr1", "attr2"], variadicity = #irdl<variadicity_array[ single]>}> : (!irdl.attribute, !irdl.attribute) -> ()
66+
}
67+
}
68+
69+
// -----
70+
71+
irdl.dialect @errors {
72+
irdl.operation @attrs2 {
73+
%0 = irdl.is i32
74+
%1 = irdl.is i64
75+
76+
// expected-error@+1 {{'irdl.attributes' op the number of attributes and their variadicities must be the same, but got 1 and 2 respectively}}
77+
"irdl.attributes"(%0) <{attributeValueNames = ["attr1"], variadicity = #irdl<variadicity_array[ single, single]>}> : (!irdl.attribute) -> ()
78+
}
79+
}
80+
81+
// -----
82+
83+
irdl.dialect @errors {
84+
irdl.operation @attrs_variadic {
85+
%0 = irdl.is i32
86+
87+
// expected-error@+1 {{'irdl.attributes' op requires attributes to have single or optional variadicity, but "attr1" was specified as variadic}}
88+
"irdl.attributes"(%0) <{attributeValueNames = ["attr1"], variadicity = #irdl<variadicity_array[ variadic]>}> : (!irdl.attribute) -> ()
89+
}
90+
}
91+

mlir/test/Dialect/IRDL/variadics.irdl.mlir

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ irdl.dialect @testvar {
1313
}
1414

1515
// CHECK-LABEL: irdl.operation @var_operand {
16-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
17-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
18-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
16+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
17+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
18+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
1919
// CHECK-NEXT: irdl.operands(foo: %[[v0]], bar: variadic %[[v1]], baz: %[[v2]])
2020
// CHECK-NEXT: }
2121
irdl.operation @var_operand {
@@ -26,9 +26,9 @@ irdl.dialect @testvar {
2626
}
2727

2828
// CHECK-LABEL: irdl.operation @opt_operand {
29-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
30-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
31-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
29+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
30+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
31+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
3232
// CHECK-NEXT: irdl.operands(foo: %[[v0]], bar: optional %[[v1]], baz: %[[v2]])
3333
// CHECK-NEXT: }
3434
irdl.operation @opt_operand {
@@ -39,9 +39,9 @@ irdl.dialect @testvar {
3939
}
4040

4141
// CHECK-LABEL: irdl.operation @var_and_opt_operand {
42-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
43-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
44-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
42+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
43+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
44+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
4545
// CHECK-NEXT: irdl.operands(foo: variadic %[[v0]], bar: optional %[[v1]], baz: %[[v2]])
4646
// CHECK-NEXT: }
4747
irdl.operation @var_and_opt_operand {
@@ -62,9 +62,9 @@ irdl.dialect @testvar {
6262
}
6363

6464
// CHECK-LABEL: irdl.operation @var_result {
65-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
66-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
67-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
65+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
66+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
67+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
6868
// CHECK-NEXT: irdl.results(foo: %[[v0]], bar: variadic %[[v1]], baz: %[[v2]])
6969
// CHECK-NEXT: }
7070
irdl.operation @var_result {
@@ -75,9 +75,9 @@ irdl.dialect @testvar {
7575
}
7676

7777
// CHECK-LABEL: irdl.operation @opt_result {
78-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
79-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
80-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
78+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
79+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
80+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
8181
// CHECK-NEXT: irdl.results(foo: %[[v0]], bar: optional %[[v1]], baz: %[[v2]])
8282
// CHECK-NEXT: }
8383
irdl.operation @opt_result {
@@ -88,9 +88,9 @@ irdl.dialect @testvar {
8888
}
8989

9090
// CHECK-LABEL: irdl.operation @var_and_opt_result {
91-
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
92-
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
93-
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
91+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
92+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
93+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
9494
// CHECK-NEXT: irdl.results(foo: variadic %[[v0]], bar: optional %[[v1]], baz: %[[v2]])
9595
// CHECK-NEXT: }
9696
irdl.operation @var_and_opt_result {
@@ -99,4 +99,21 @@ irdl.dialect @testvar {
9999
%2 = irdl.is i64
100100
irdl.results(foo: variadic %0, bar: optional %1, baz: %2)
101101
}
102+
103+
// CHECK-LABEL: irdl.operation @var_attr {
104+
// CHECK-NEXT: %[[v0:[^ ]*]] = irdl.is i16
105+
// CHECK-NEXT: %[[v1:[^ ]*]] = irdl.is i32
106+
// CHECK-NEXT: %[[v2:[^ ]*]] = irdl.is i64
107+
// CHECK-NEXT: irdl.attributes {"optional" = optional %[[v0]], "single" = %[[v1]], "single_no_word" = %[[v2]]}
108+
// CHECK-NEXT: }
109+
irdl.operation @var_attr {
110+
%0 = irdl.is i16
111+
%1 = irdl.is i32
112+
%2 = irdl.is i64
113+
irdl.attributes {
114+
"optional" = optional %0,
115+
"single" = single %1,
116+
"single_no_word" = %2
117+
}
118+
}
102119
}

mlir/tools/tblgen-to-irdl/OpDefinitionsGen.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,18 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
454454

455455
SmallVector<Value> attributes;
456456
SmallVector<Attribute> attrNames;
457+
SmallVector<irdl::VariadicityAttr> attrVariadicity;
457458
for (auto namedAttr : tblgenOp.getAttributes()) {
459+
irdl::VariadicityAttr var;
458460
if (namedAttr.attr.isOptional())
459-
continue;
461+
var = consBuilder.getAttr<irdl::VariadicityAttr>(
462+
irdl::Variadicity::optional);
463+
else
464+
var =
465+
consBuilder.getAttr<irdl::VariadicityAttr>(irdl::Variadicity::single);
460466
attributes.push_back(createAttrConstraint(consBuilder, namedAttr.attr));
461467
attrNames.push_back(StringAttr::get(ctx, namedAttr.name));
468+
attrVariadicity.push_back(var);
462469
}
463470

464471
SmallVector<Value> regions;
@@ -479,8 +486,9 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
479486
ArrayAttr::get(ctx, resultNames),
480487
resultVariadicity);
481488
if (!attributes.empty())
482-
consBuilder.create<irdl::AttributesOp>(UnknownLoc::get(ctx), attributes,
483-
ArrayAttr::get(ctx, attrNames));
489+
consBuilder.create<irdl::AttributesOp>(
490+
UnknownLoc::get(ctx), attributes, ArrayAttr::get(ctx, attrNames),
491+
irdl::VariadicityArrayAttr::get(ctx, attrVariadicity));
484492
if (!regions.empty())
485493
consBuilder.create<irdl::RegionsOp>(UnknownLoc::get(ctx), regions,
486494
ArrayAttr::get(ctx, regionNames));

0 commit comments

Comments
 (0)