diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp --- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp +++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp @@ -2855,6 +2855,16 @@ auto isConst = global.getConstant().has_value(); auto g = rewriter.create( loc, tyAttr, isConst, linkage, global.getSymName(), initAttr); + + // Apply all non-Fir::GlobalOp attributes to the LLVM::GlobalOp, preserving + // them; whilst taking care not to apply attributes that are lowered in + // other ways. + llvm::SmallDenseSet elidedAttrsSet( + global.getAttributeNames().begin(), global.getAttributeNames().end()); + for (auto &attr : global->getAttrs()) + if (!elidedAttrsSet.contains(attr.getName().strref())) + g->setAttr(attr.getName(), attr.getValue()); + auto &gr = g.getInitializerRegion(); rewriter.inlineRegionBefore(global.getRegion(), gr, gr.end()); if (!gr.empty()) { diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp --- a/flang/lib/Optimizer/Dialect/FIROps.cpp +++ b/flang/lib/Optimizer/Dialect/FIROps.cpp @@ -1308,6 +1308,9 @@ simpleInitializer = true; } + if (parser.parseOptionalAttrDict(result.attributes)) + return mlir::failure(); + if (succeeded(parser.parseOptionalKeyword("constant"))) { // if "constant" keyword then mark this as a constant, not a variable result.addAttribute("constant", builder.getUnitAttr()); @@ -1342,6 +1345,7 @@ p.printAttributeWithoutType(getSymrefAttr()); if (auto val = getValueOrNull()) p << '(' << val << ')'; + p.printOptionalAttrDict((*this)->getAttrs(), (*this).getAttributeNames()); if (getOperation()->getAttr(fir::GlobalOp::getConstantAttrNameStr())) p << " constant"; if (getOperation()->getAttr(getTargetAttrName())) diff --git a/flang/test/Fir/global-attributes.fir b/flang/test/Fir/global-attributes.fir new file mode 100644 --- /dev/null +++ b/flang/test/Fir/global-attributes.fir @@ -0,0 +1,17 @@ +// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s +// RUN: tco --fir-to-llvm-ir %s | FileCheck %s +// RUN: fir-opt %s | FileCheck %s --check-prefix=READ-OUT +// RUN: tco --emit-fir %s | FileCheck %s --check-prefix=READ-OUT + +// CHECK: llvm.mlir.global external @_QMtest_0Edata_int() {{{.*}}test = "string_attribute_maintained"{{.*}}} : i32 { +// CHECK: [[CST0:%.*]] = llvm.mlir.constant(10 : i32) : i32 +// CHECK: llvm.return [[CST0]] : i32 +// CHECK: } +// READ-OUT: fir.global @_QMtest_0Edata_int {test = "string_attribute_maintained"} : i32 { +// READ-OUT: %c10_i32 = arith.constant 10 : i32 +// READ-OUT: fir.has_value %c10_i32 : i32 +// READ-OUT: } +fir.global @_QMtest_0Edata_int {test = "string_attribute_maintained"} : i32 { + %c10_i32 = arith.constant 10 : i32 + fir.has_value %c10_i32 : i32 +}