Skip to content

Commit ab9fab0

Browse files
committed
Optional attributes
1 parent e984d11 commit ab9fab0

File tree

6 files changed

+119
-36
lines changed

6 files changed

+119
-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
@@ -321,9 +321,10 @@ def IRDL_AttributesOp : IRDL_Op<"attributes", [HasParent<"OperationOp">]> {
321321
}];
322322

323323
let arguments = (ins Variadic<IRDL_AttributeType>:$attributeValues,
324-
StrArrayAttr:$attributeValueNames);
324+
StrArrayAttr:$attributeValueNames,
325+
VariadicityArrayAttr:$variadicity);
325326
let assemblyFormat = [{
326-
custom<AttributesOp>($attributeValues, $attributeValueNames) attr-dict
327+
custom<AttributesOp>($attributeValues, $attributeValueNames, $variadicity) attr-dict
327328
}];
328329

329330
let hasVerifier = true;

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,18 @@ LogicalResult ResultsOp::verify() {
107107
LogicalResult AttributesOp::verify() {
108108
size_t namesSize = getAttributeValueNames().size();
109109
size_t valuesSize = getAttributeValues().size();
110+
size_t numVariadicities = getVariadicity().size();
111+
112+
if (numVariadicities != valuesSize)
113+
return emitOpError()
114+
<< "the number of attributes and their variadicities must be "
115+
"the same, but got "
116+
<< valuesSize << " and " << numVariadicities << " respectively";
110117

111118
if (namesSize != valuesSize)
112119
return emitOpError()
113120
<< "the number of attribute names and their constraints must be "
114-
"the same but got "
121+
"the same, but got "
115122
<< namesSize << " and " << valuesSize << " respectively";
116123

117124
return success();
@@ -249,30 +256,42 @@ static void printValuesWithVariadicity(OpAsmPrinter &p, Operation *op,
249256
static ParseResult
250257
parseAttributesOp(OpAsmParser &p,
251258
SmallVectorImpl<OpAsmParser::UnresolvedOperand> &attrOperands,
252-
ArrayAttr &attrNamesAttr) {
259+
ArrayAttr &attrNamesAttr,
260+
VariadicityArrayAttr &variadicityAttr) {
253261
Builder &builder = p.getBuilder();
262+
MLIRContext *ctx = builder.getContext();
254263
SmallVector<Attribute> attrNames;
264+
SmallVector<VariadicityAttr> variadicities;
265+
255266
if (succeeded(p.parseOptionalLBrace())) {
256267
auto parseOperands = [&]() {
257268
if (p.parseAttribute(attrNames.emplace_back()) || p.parseEqual() ||
258-
p.parseOperand(attrOperands.emplace_back()))
269+
parseValueWithVariadicity(p, attrOperands.emplace_back(),
270+
variadicities.emplace_back()))
259271
return failure();
260272
return success();
261273
};
262274
if (p.parseCommaSeparatedList(parseOperands) || p.parseRBrace())
263275
return failure();
264276
}
265277
attrNamesAttr = builder.getArrayAttr(attrNames);
278+
variadicityAttr = VariadicityArrayAttr::get(ctx, variadicities);
266279
return success();
267280
}
268281

269282
static void printAttributesOp(OpAsmPrinter &p, AttributesOp op,
270-
OperandRange attrArgs, ArrayAttr attrNames) {
283+
OperandRange attrArgs, ArrayAttr attrNames,
284+
VariadicityArrayAttr variadicityAttr) {
271285
if (attrNames.empty())
272286
return;
273287
p << "{";
274-
interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
275-
[&](int i) { p << attrNames[i] << " = " << attrArgs[i]; });
288+
interleaveComma(llvm::seq<int>(0, attrNames.size()), p, [&](int i) {
289+
Variadicity variadicity = variadicityAttr[i].getValue();
290+
p << attrNames[i] << " = ";
291+
if (variadicity != Variadicity::single)
292+
p << stringifyVariadicity(variadicity) << " ";
293+
p << attrArgs[i];
294+
});
276295
p << '}';
277296
}
278297

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: 39 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) <{variadicity = #irdl<variadicity_array[single, single]>}> : (!irdl.attribute) -> ()
2020
}
@@ -36,8 +36,45 @@ 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 operands and their variadicities must be the same, but got 1 and 2 respectively}}
4141
"irdl.results"(%0) <{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+

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(%[[v0]], variadic %[[v1]], %[[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(%[[v0]], optional %[[v1]], %[[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(variadic %[[v0]], optional %[[v1]], %[[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(%[[v0]], variadic %[[v1]], %[[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(%[[v0]], optional %[[v1]], %[[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(variadic %[[v0]], optional %[[v1]], %[[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(variadic %0, optional %1, %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
@@ -422,11 +422,18 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
422422

423423
SmallVector<Value> attributes;
424424
SmallVector<Attribute> attrNames;
425+
SmallVector<irdl::VariadicityAttr> attrVariadicity;
425426
for (auto namedAttr : tblgenOp.getAttributes()) {
427+
irdl::VariadicityAttr var;
426428
if (namedAttr.attr.isOptional())
427-
continue;
429+
var = consBuilder.getAttr<irdl::VariadicityAttr>(
430+
irdl::Variadicity::optional);
431+
else
432+
var =
433+
consBuilder.getAttr<irdl::VariadicityAttr>(irdl::Variadicity::single);
428434
attributes.push_back(createAttrConstraint(consBuilder, namedAttr.attr));
429435
attrNames.push_back(StringAttr::get(ctx, namedAttr.name));
436+
attrVariadicity.push_back(var);
430437
}
431438

432439
SmallVector<Value> regions;
@@ -443,8 +450,9 @@ irdl::OperationOp createIRDLOperation(OpBuilder &builder,
443450
consBuilder.create<irdl::ResultsOp>(UnknownLoc::get(ctx), results,
444451
resultVariadicity);
445452
if (!attributes.empty())
446-
consBuilder.create<irdl::AttributesOp>(UnknownLoc::get(ctx), attributes,
447-
ArrayAttr::get(ctx, attrNames));
453+
consBuilder.create<irdl::AttributesOp>(
454+
UnknownLoc::get(ctx), attributes, ArrayAttr::get(ctx, attrNames),
455+
irdl::VariadicityArrayAttr::get(ctx, attrVariadicity));
448456
if (!regions.empty())
449457
consBuilder.create<irdl::RegionsOp>(UnknownLoc::get(ctx), regions);
450458

0 commit comments

Comments
 (0)