Skip to content

Commit

Permalink
[Flang][MLIR] Alter Fir.GlobalOp to print and lower external attributes
Browse files Browse the repository at this point in the history
Fir.GlobalOp's currently do not respect attributes that
are applied to them, this change will do two things:

- Allow lowering of arbitrary attributes applied to
Fir.GlobalOp's to LLVMGlobalOp's during CodeGen
- Allow printing and parsing of arbitrarily applied attributes

This allows applying other dialects attributes (or other
fir attributes) to fir.GlobalOps on the fly and have them
exist in the resulting LLVM dialect IR or FIR IR.

Reviewer: jeanPerier

Differential Revision: https://reviews.llvm.org/D148352
  • Loading branch information
agozillon committed Apr 20, 2023
1 parent 2e275e2 commit 6b44274
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2855,6 +2855,16 @@ struct GlobalOpConversion : public FIROpConversion<fir::GlobalOp> {
auto isConst = global.getConstant().has_value();
auto g = rewriter.create<mlir::LLVM::GlobalOp>(
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<llvm::StringRef> 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()) {
Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,9 @@ mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser,
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());
Expand Down Expand Up @@ -1342,6 +1345,7 @@ void fir::GlobalOp::print(mlir::OpAsmPrinter &p) {
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()))
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Fir/global-attributes.fir
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6b44274

Please sign in to comment.