[flang-commits] [flang] 6b44274 - [Flang][MLIR] Alter Fir.GlobalOp to print and lower external attributes
Andrew Gozillon via flang-commits
flang-commits at lists.llvm.org
Thu Apr 20 05:06:43 PDT 2023
Author: Andrew Gozillon
Date: 2023-04-20T07:06:05-05:00
New Revision: 6b44274d8309c4e8daeecdcc6a14259015a3923f
URL: https://github.com/llvm/llvm-project/commit/6b44274d8309c4e8daeecdcc6a14259015a3923f
DIFF: https://github.com/llvm/llvm-project/commit/6b44274d8309c4e8daeecdcc6a14259015a3923f.diff
LOG: [Flang][MLIR] Alter Fir.GlobalOp to print and lower external attributes
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
Added:
flang/test/Fir/global-attributes.fir
Modified:
flang/lib/Optimizer/CodeGen/CodeGen.cpp
flang/lib/Optimizer/Dialect/FIROps.cpp
Removed:
################################################################################
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 2e461d704cbf1..72f02cb425299 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -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()) {
diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index 3188a166ef0cd..3e158183d0138 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -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());
@@ -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()))
diff --git a/flang/test/Fir/global-attributes.fir b/flang/test/Fir/global-attributes.fir
new file mode 100644
index 0000000000000..24442d671504f
--- /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
+}
More information about the flang-commits
mailing list