Skip to content

Commit f54dc7b

Browse files
authored
[mlir][ODS] Omit printing default-valued attributes in oilists (#68880)
This makes these match the behaviour of optional attributes (which are omitted when they are their default value of none). This allows for concise assembly formats without a custom printer. An extra print of " " is also removed, this does change any existing uses of oilists, but if the parameter before the oilist is optional, that would previously add an extra space. This #68694 + some fixes for the MLIR Python tests, unfortunately GitHub does not allow re-opening PRs 😕
1 parent 92e751d commit f54dc7b

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

flang/test/Lower/OpenMP/FIR/atomic-read.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
!CHECK: %[[VAR_X:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFEx"}
1515
!CHECK: %[[VAR_Y:.*]] = fir.alloca i32 {bindc_name = "y", uniq_name = "_QFEy"}
1616
!CHECK: omp.atomic.read %[[VAR_X]] = %[[VAR_Y]] memory_order(acquire) hint(uncontended) : !fir.ref<i32>, i32
17-
!CHECK: omp.atomic.read %[[VAR_A]] = %[[VAR_B]] memory_order(relaxed) hint(none) : !fir.ref<!fir.char<1>>, !fir.char<1>
17+
!CHECK: omp.atomic.read %[[VAR_A]] = %[[VAR_B]] memory_order(relaxed) : !fir.ref<!fir.char<1>>, !fir.char<1>
1818
!CHECK: omp.atomic.read %[[VAR_C]] = %[[VAR_D]] memory_order(seq_cst) hint(contended) : !fir.ref<!fir.logical<4>>, !fir.logical<4>
1919
!CHECK: omp.atomic.read %[[VAR_E]] = %[[VAR_F]] hint(speculative) : !fir.ref<!fir.char<1,8>>, !fir.char<1,8>
2020
!CHECK: omp.atomic.read %[[VAR_G]] = %[[VAR_H]] hint(nonspeculative) : !fir.ref<f32>, f32

flang/test/Lower/OpenMP/FIR/critical.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | fir-opt --fir-to-llvm-ir | FileCheck %s --check-prefix="OMPDialect"
33
!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | fir-opt --fir-to-llvm-ir | tco | FileCheck %s --check-prefix="LLVMIR"
44

5-
!OMPDialect: omp.critical.declare @help2 hint(none)
5+
!OMPDialect: omp.critical.declare @help2
66
!OMPDialect: omp.critical.declare @help1 hint(contended)
77

88
subroutine omp_critical()

flang/test/Lower/OpenMP/critical.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
22

3-
!CHECK: omp.critical.declare @help2 hint(none)
3+
!CHECK: omp.critical.declare @help2
44
!CHECK: omp.critical.declare @help1 hint(contended)
55

66
subroutine omp_critical()

mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func.func @wsloop(%arg0: index, %arg1: index, %arg2: index, %arg3: index, %arg4:
9090
// CHECK-LABEL: @atomic_write
9191
// CHECK: (%[[ARG0:.*]]: !llvm.ptr<i32>)
9292
// CHECK: %[[VAL0:.*]] = llvm.mlir.constant(1 : i32) : i32
93-
// CHECK: omp.atomic.write %[[ARG0]] = %[[VAL0]] hint(none) memory_order(relaxed) : !llvm.ptr<i32>, i32
93+
// CHECK: omp.atomic.write %[[ARG0]] = %[[VAL0]] memory_order(relaxed) : !llvm.ptr<i32>, i32
9494
func.func @atomic_write(%a: !llvm.ptr<i32>) -> () {
9595
%1 = arith.constant 1 : i32
9696
omp.atomic.write %a = %1 hint(none) memory_order(relaxed) : !llvm.ptr<i32>, i32
@@ -474,4 +474,4 @@ llvm.func @_QPtarget_map_with_bounds(%arg0: !llvm.ptr<i32>, %arg1: !llvm.ptr<arr
474474
omp.terminator
475475
}
476476
llvm.return
477-
}
477+
}

mlir/test/Dialect/OpenMP/ops.mlir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ omp.critical.declare @mutex6 hint(contended, nonspeculative)
831831
omp.critical.declare @mutex7 hint(uncontended, speculative)
832832
// CHECK: omp.critical.declare @mutex8 hint(contended, speculative)
833833
omp.critical.declare @mutex8 hint(contended, speculative)
834-
// CHECK: omp.critical.declare @mutex9 hint(none)
834+
// CHECK: omp.critical.declare @mutex9
835835
omp.critical.declare @mutex9 hint(none)
836836
// CHECK: omp.critical.declare @mutex10
837837
omp.critical.declare @mutex10
@@ -909,7 +909,7 @@ func.func @omp_atomic_read(%v: memref<i32>, %x: memref<i32>) {
909909
omp.atomic.read %v = %x hint(nonspeculative, contended) : memref<i32>, i32
910910
// CHECK: omp.atomic.read %[[v]] = %[[x]] memory_order(seq_cst) hint(contended, speculative) : memref<i32>, i32
911911
omp.atomic.read %v = %x hint(speculative, contended) memory_order(seq_cst) : memref<i32>, i32
912-
// CHECK: omp.atomic.read %[[v]] = %[[x]] memory_order(seq_cst) hint(none) : memref<i32>, i32
912+
// CHECK: omp.atomic.read %[[v]] = %[[x]] memory_order(seq_cst) : memref<i32>, i32
913913
omp.atomic.read %v = %x hint(none) memory_order(seq_cst) : memref<i32>, i32
914914
return
915915
}
@@ -927,7 +927,7 @@ func.func @omp_atomic_write(%addr : memref<i32>, %val : i32) {
927927
omp.atomic.write %addr = %val memory_order(relaxed) : memref<i32>, i32
928928
// CHECK: omp.atomic.write %[[ADDR]] = %[[VAL]] hint(uncontended, speculative) : memref<i32>, i32
929929
omp.atomic.write %addr = %val hint(speculative, uncontended) : memref<i32>, i32
930-
// CHECK: omp.atomic.write %[[ADDR]] = %[[VAL]] hint(none) : memref<i32>, i32
930+
// CHECK: omp.atomic.write %[[ADDR]] = %[[VAL]] : memref<i32>, i32
931931
omp.atomic.write %addr = %val hint(none) : memref<i32>, i32
932932
return
933933
}
@@ -1004,7 +1004,7 @@ func.func @omp_atomic_update(%x : memref<i32>, %expr : i32, %xBool : memref<i1>,
10041004
omp.yield(%const:i32)
10051005
}
10061006

1007-
// CHECK: omp.atomic.update hint(none) %[[X]] : memref<i32>
1007+
// CHECK: omp.atomic.update %[[X]] : memref<i32>
10081008
// CHECK-NEXT: (%[[XVAL:.*]]: i32):
10091009
// CHECK-NEXT: %[[NEWVAL:.*]] = llvm.add %[[XVAL]], %[[EXPR]] : i32
10101010
// CHECK-NEXT: omp.yield(%[[NEWVAL]] : i32)
@@ -1181,7 +1181,7 @@ func.func @omp_atomic_capture(%v: memref<i32>, %x: memref<i32>, %expr: i32) {
11811181
omp.atomic.write %x = %expr : memref<i32>, i32
11821182
}
11831183

1184-
// CHECK: omp.atomic.capture hint(none) {
1184+
// CHECK: omp.atomic.capture {
11851185
// CHECK-NEXT: omp.atomic.update %[[x]] : memref<i32>
11861186
// CHECK-NEXT: (%[[xval:.*]]: i32):
11871187
// CHECK-NEXT: %[[newval:.*]] = llvm.add %[[xval]], %[[expr]] : i32

mlir/test/python/dialects/transform_structured_ext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def testTileToForallCompact(target):
439439
structured.TileUsingForallOp(matmul, num_threads=[2, 3, 4])
440440
# CHECK-LABEL: TEST: testTileToForallCompact
441441
# CHECK: = transform.structured.tile_using_forall
442-
# CHECK-SAME: num_threads [2, 3, 4] tile_sizes []
442+
# CHECK-SAME: num_threads [2, 3, 4]
443443
# CHECK-SAME: (!transform.op<"linalg.matmul">) -> (!transform.any_op, !transform.any_op)
444444

445445

@@ -454,7 +454,7 @@ def testTileToForallLoopsAndTileOpTypes(target):
454454
)
455455
# CHECK-LABEL: TEST: testTileToForallLoopsAndTileOpTypes
456456
# CHECK: = transform.structured.tile_using_forall
457-
# CHECK-SAME: num_threads [2, 3, 4] tile_sizes []
457+
# CHECK-SAME: num_threads [2, 3, 4]
458458
# CHECK-SAME: (!transform.any_op) -> (!transform.op<"scf.forall">, !transform.op<"linalg.matmul">)
459459

460460

@@ -464,7 +464,7 @@ def testTileToForallTileSizes(target):
464464
structured.TileUsingForallOp(target, tile_sizes=[2, 3, 4])
465465
# CHECK-LABEL: TEST: testTileToForallTileSizes
466466
# CHECK: = transform.structured.tile_using_forall
467-
# CHECK-SAME: num_threads [] tile_sizes [2, 3, 4]
467+
# CHECK-SAME: tile_sizes [2, 3, 4]
468468

469469

470470
@run

mlir/test/python/dialects/transform_vector_ext.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,24 @@ def enum_configurable_patterns():
9494
)
9595

9696
# CHECK: transform.apply_patterns.vector.lower_transpose
97-
# CHECK-SAME: lowering_strategy = eltwise
98-
# CHECK-SAME: avx2_lowering_strategy = false
9997
vector.ApplyLowerTransposePatternsOp()
10098
# CHECK: transform.apply_patterns.vector.lower_transpose
101-
# CHECK-SAME: lowering_strategy = eltwise
102-
# CHECK-SAME: avx2_lowering_strategy = false
99+
# This is the default strategy, not printed.
103100
vector.ApplyLowerTransposePatternsOp(
104101
lowering_strategy=vector.VectorTransposeLowering.EltWise
105102
)
106103
# CHECK: transform.apply_patterns.vector.lower_transpose
107104
# CHECK-SAME: lowering_strategy = flat_transpose
108-
# CHECK-SAME: avx2_lowering_strategy = false
109105
vector.ApplyLowerTransposePatternsOp(
110106
lowering_strategy=vector.VectorTransposeLowering.Flat
111107
)
112108
# CHECK: transform.apply_patterns.vector.lower_transpose
113109
# CHECK-SAME: lowering_strategy = shuffle_1d
114-
# CHECK-SAME: avx2_lowering_strategy = false
115110
vector.ApplyLowerTransposePatternsOp(
116111
lowering_strategy=vector.VectorTransposeLowering.Shuffle1D
117112
)
118113
# CHECK: transform.apply_patterns.vector.lower_transpose
119114
# CHECK-SAME: lowering_strategy = shuffle_16x16
120-
# CHECK-SAME: avx2_lowering_strategy = false
121115
vector.ApplyLowerTransposePatternsOp(
122116
lowering_strategy=vector.VectorTransposeLowering.Shuffle16x16
123117
)

mlir/tools/mlir-tblgen/OpFormatGen.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,16 @@ static void genEnumAttrPrinter(const NamedAttribute *var, const Operator &op,
20092009
" }\n";
20102010
}
20112011

2012+
/// Generate a check that a DefaultValuedAttr has a value that is non-default.
2013+
static void genNonDefaultValueCheck(MethodBody &body, const Operator &op,
2014+
AttributeVariable &attrElement) {
2015+
FmtContext fctx;
2016+
Attribute attr = attrElement.getVar()->attr;
2017+
fctx.withBuilder("::mlir::OpBuilder((*this)->getContext())");
2018+
body << " && " << op.getGetterName(attrElement.getVar()->name) << "Attr() != "
2019+
<< tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue());
2020+
}
2021+
20122022
/// Generate the check for the anchor of an optional group.
20132023
static void genOptionalGroupPrinterAnchor(FormatElement *anchor,
20142024
const Operator &op,
@@ -2042,12 +2052,7 @@ static void genOptionalGroupPrinterAnchor(FormatElement *anchor,
20422052
if (attr.hasDefaultValue()) {
20432053
// Consider a default-valued attribute as present if it's not the
20442054
// default value.
2045-
FmtContext fctx;
2046-
fctx.withBuilder("::mlir::OpBuilder((*this)->getContext())");
2047-
body << " && " << op.getGetterName(element->getVar()->name)
2048-
<< "Attr() != "
2049-
<< tgfmt(attr.getConstBuilderTemplate(), &fctx,
2050-
attr.getDefaultValue());
2055+
genNonDefaultValueCheck(body, op, *element);
20512056
return;
20522057
}
20532058
llvm_unreachable("attribute must be optional or default-valued");
@@ -2158,7 +2163,6 @@ void OperationFormat::genElementPrinter(FormatElement *element,
21582163

21592164
// Emit the OIList
21602165
if (auto *oilist = dyn_cast<OIListElement>(element)) {
2161-
genLiteralPrinter(" ", body, shouldEmitSpace, lastWasPunctuation);
21622166
for (auto clause : oilist->getClauses()) {
21632167
LiteralElement *lelement = std::get<0>(clause);
21642168
ArrayRef<FormatElement *> pelement = std::get<1>(clause);
@@ -2170,8 +2174,14 @@ void OperationFormat::genElementPrinter(FormatElement *element,
21702174
for (VariableElement *var : vars) {
21712175
TypeSwitch<FormatElement *>(var)
21722176
.Case([&](AttributeVariable *attrEle) {
2173-
body << " || " << op.getGetterName(attrEle->getVar()->name)
2177+
body << " || (" << op.getGetterName(attrEle->getVar()->name)
21742178
<< "Attr()";
2179+
Attribute attr = attrEle->getVar()->attr;
2180+
if (attr.hasDefaultValue()) {
2181+
// Don't print default-valued attributes.
2182+
genNonDefaultValueCheck(body, op, *attrEle);
2183+
}
2184+
body << ")";
21752185
})
21762186
.Case([&](OperandVariable *ele) {
21772187
if (ele->getVar()->isVariadic()) {

0 commit comments

Comments
 (0)