Skip to content

Commit e521859

Browse files
committed
[mlir][ODS] Omit printing default-valued attributes in oilists
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.
1 parent 6172960 commit e521859

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+5-5
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/tools/mlir-tblgen/OpFormatGen.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,15 @@ static void genEnumAttrPrinter(const NamedAttribute *var, const Operator &op,
20092009
" }\n";
20102010
}
20112011

2012+
static void genNonDefaultValueCheck(MethodBody &body, const Operator &op,
2013+
AttributeVariable &attrElement) {
2014+
FmtContext fctx;
2015+
Attribute attr = attrElement.getVar()->attr;
2016+
fctx.withBuilder("::mlir::OpBuilder((*this)->getContext())");
2017+
body << " && " << op.getGetterName(attrElement.getVar()->name) << "Attr() != "
2018+
<< tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue());
2019+
}
2020+
20122021
/// Generate the check for the anchor of an optional group.
20132022
static void genOptionalGroupPrinterAnchor(FormatElement *anchor,
20142023
const Operator &op,
@@ -2042,12 +2051,7 @@ static void genOptionalGroupPrinterAnchor(FormatElement *anchor,
20422051
if (attr.hasDefaultValue()) {
20432052
// Consider a default-valued attribute as present if it's not the
20442053
// 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());
2054+
genNonDefaultValueCheck(body, op, *element);
20512055
return;
20522056
}
20532057
llvm_unreachable("attribute must be optional or default-valued");
@@ -2158,7 +2162,6 @@ void OperationFormat::genElementPrinter(FormatElement *element,
21582162

21592163
// Emit the OIList
21602164
if (auto *oilist = dyn_cast<OIListElement>(element)) {
2161-
genLiteralPrinter(" ", body, shouldEmitSpace, lastWasPunctuation);
21622165
for (auto clause : oilist->getClauses()) {
21632166
LiteralElement *lelement = std::get<0>(clause);
21642167
ArrayRef<FormatElement *> pelement = std::get<1>(clause);
@@ -2170,8 +2173,14 @@ void OperationFormat::genElementPrinter(FormatElement *element,
21702173
for (VariableElement *var : vars) {
21712174
TypeSwitch<FormatElement *>(var)
21722175
.Case([&](AttributeVariable *attrEle) {
2173-
body << " || " << op.getGetterName(attrEle->getVar()->name)
2176+
body << " || (" << op.getGetterName(attrEle->getVar()->name)
21742177
<< "Attr()";
2178+
Attribute attr = attrEle->getVar()->attr;
2179+
if (attr.hasDefaultValue()) {
2180+
// Don't print default-valued attributes.
2181+
genNonDefaultValueCheck(body, op, *attrEle);
2182+
}
2183+
body << ")";
21752184
})
21762185
.Case([&](OperandVariable *ele) {
21772186
if (ele->getVar()->isVariadic()) {

0 commit comments

Comments
 (0)