Skip to content

Commit 8c106a1

Browse files
authored
[flang] Fix attribute printing for fir.global op (#81197)
The custom printer for `fir.global` was eluding all the attributes present on the op when printing the attribute dictionary. So any attribute that is not part of the pretty printing was therefore discarded. This patch fix the printer and also make use of the getters for the attribute names when they are hardcoded.
1 parent f720150 commit 8c106a1

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,12 +1348,12 @@ mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser,
13481348
if (parser.parseOptionalAttrDict(result.attributes))
13491349
return mlir::failure();
13501350

1351-
if (succeeded(parser.parseOptionalKeyword("constant"))) {
1351+
if (succeeded(parser.parseOptionalKeyword(getConstantAttrNameStr()))) {
13521352
// if "constant" keyword then mark this as a constant, not a variable
1353-
result.addAttribute("constant", builder.getUnitAttr());
1353+
result.addAttribute(getConstantAttrNameStr(), builder.getUnitAttr());
13541354
}
13551355

1356-
if (succeeded(parser.parseOptionalKeyword("target")))
1356+
if (succeeded(parser.parseOptionalKeyword(getTargetAttrNameStr())))
13571357
result.addAttribute(getTargetAttrNameStr(), builder.getUnitAttr());
13581358

13591359
mlir::Type globalType;
@@ -1382,11 +1382,16 @@ void fir::GlobalOp::print(mlir::OpAsmPrinter &p) {
13821382
p.printAttributeWithoutType(getSymrefAttr());
13831383
if (auto val = getValueOrNull())
13841384
p << '(' << val << ')';
1385-
p.printOptionalAttrDict((*this)->getAttrs(), (*this).getAttributeNames());
1386-
if (getOperation()->getAttr(fir::GlobalOp::getConstantAttrNameStr()))
1387-
p << " constant";
1385+
// Print all other attributes that are not pretty printed here.
1386+
p.printOptionalAttrDict((*this)->getAttrs(), /*elideAttrs=*/{
1387+
getSymNameAttrName(), getSymrefAttrName(),
1388+
getTypeAttrName(), getConstantAttrName(),
1389+
getTargetAttrName(), getLinkNameAttrName(),
1390+
getInitValAttrName()});
1391+
if (getOperation()->getAttr(getConstantAttrName()))
1392+
p << " " << getConstantAttrNameStr();
13881393
if (getOperation()->getAttr(getTargetAttrName()))
1389-
p << " target";
1394+
p << " " << getTargetAttrNameStr();
13901395
p << " : ";
13911396
p.printType(getType());
13921397
if (hasInitializationBody()) {

flang/test/Fir/fir-ops.fir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,3 +893,10 @@ func.func @test_box_typecode(%a: !fir.class<none>) {
893893
// CHECK-LABEL: func.func @test_box_typecode(
894894
// CHECK-SAME: %[[A:.*]]: !fir.class<none>)
895895
// CHECK: %{{.*}} = fir.box_typecode %[[A]] : (!fir.class<none>) -> i32
896+
897+
fir.global @t1 {keep_my_attr = "data"} : i32 {
898+
%1 = arith.constant 0 : i32
899+
fir.has_value %1 : i32
900+
}
901+
902+
// CHECK-LABEL: fir.global @t1 {keep_my_attr = "data"} : i32

0 commit comments

Comments
 (0)