Skip to content

Commit 3301f3e

Browse files
committed
propogate attributes through cg-rewrite
1 parent feaa5aa commit 3301f3e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ class DeclareOpConversion : public mlir::OpRewritePattern<fir::DeclareOp> {
281281
matchAndRewrite(fir::DeclareOp declareOp,
282282
mlir::PatternRewriter &rewriter) const override {
283283
if (!preserveDeclare) {
284+
auto memrefOp = declareOp.getMemref().getDefiningOp();
285+
if(!memrefOp){
286+
rewriter.replaceOp(declareOp, declareOp.getMemref());
287+
return mlir::success();
288+
}
289+
290+
// attach metadatas from the fir.declare to its memref (if it's an operation)
291+
mlir::NamedAttrList elidedAttrs =
292+
mlir::NamedAttrList{memrefOp->getAttrs()};
293+
for (const mlir::NamedAttribute &attr : declareOp->getAttrs())
294+
if (!elidedAttrs.get(attr.getName()))
295+
memrefOp->setAttr(attr.getName(), attr.getValue());
296+
284297
rewriter.replaceOp(declareOp, declareOp.getMemref());
285298
return mlir::success();
286299
}
@@ -299,11 +312,16 @@ class DeclareOpConversion : public mlir::OpRewritePattern<fir::DeclareOp> {
299312
else
300313
return mlir::failure();
301314
}
302-
// FIXME: Add FortranAttrs and CudaAttrs
315+
303316
auto xDeclOp = rewriter.create<fir::cg::XDeclareOp>(
304317
loc, declareOp.getType(), declareOp.getMemref(), shapeOpers, shiftOpers,
305318
declareOp.getTypeparams(), declareOp.getDummyScope(),
306319
declareOp.getUniqName());
320+
321+
// attach metadatas from fir.declare to fircg.ext_declare
322+
for (const mlir::NamedAttribute &attr : declareOp->getAttrs())
323+
xDeclOp->setAttr(attr.getName(), attr.getValue());
324+
307325
LLVM_DEBUG(llvm::dbgs()
308326
<< "rewriting " << declareOp << " to " << xDeclOp << '\n');
309327
rewriter.replaceOp(declareOp, xDeclOp.getOperation()->getResults());

flang/test/Fir/declare-codegen.fir

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func.func private @bar(%arg0: !fir.ref<!fir.array<12x23xi32>>)
2323

2424
// DECL-LABEL: func.func @test(
2525
// DECL-SAME: %[[arg0:.*]]: !fir.ref<!fir.array<12x23xi32>>) {
26-
// DECL: fircg.ext_declare
26+
// DECL: fircg.ext_declare{{.*}}{uniq_name = "_QFarray_numeric_lboundsEx"}
2727

2828

2929
func.func @useless_shape_with_duplicate_extent_operand(%arg0: !fir.ref<!fir.array<3x3xf32>>) {
@@ -37,7 +37,7 @@ func.func @useless_shape_with_duplicate_extent_operand(%arg0: !fir.ref<!fir.arra
3737
// NODECL-NEXT: return
3838

3939
// DECL-LABEL: func.func @useless_shape_with_duplicate_extent_operand(
40-
// DECL: fircg.ext_declare
40+
// DECL: fircg.ext_declare{{.*}}{uniq_name = "u"}
4141

4242
// Test DCE does not crash because of unreachable code.
4343
func.func @unreachable_code(%arg0: !fir.ref<!fir.char<1,10>>) {
@@ -51,4 +51,20 @@ func.func @unreachable_code(%arg0: !fir.ref<!fir.char<1,10>>) {
5151
// NODECL-LABEL: func.func @unreachable_code(
5252
// NODECL-NOT: uniq_name = "live_code"
5353
// DECL-LABEL: func.func @unreachable_code(
54-
// DECL: uniq_name = "live_code"
54+
// DECL: fircg.ext_declare{{.*}}{uniq_name = "live_code"}
55+
// DECL: fir.declare{{.*}}{uniq_name = "dead_code"}
56+
57+
58+
// Test that attributes get attached to the memref operation when fir.declare is not preserved
59+
func.func @test_local_memref() {
60+
%0 = fir.alloca !fir.array<10xi32> {uniq_name = "local_array"}
61+
%1 = fir.declare %0 {uniq_name = "local_array_declare", acc.declare = #acc.declare<dataClause = acc_create>} : (!fir.ref<!fir.array<10xi32>>) -> !fir.ref<!fir.array<10xi32>>
62+
return
63+
}
64+
// NODECL-LABEL: func.func @test_local_memref() {
65+
66+
// Note: the uniq_name is not attached of fir.declare is not preserved because it conflicts with the uniq_name of the memref
67+
// NODECL: fir.alloca{{.*}}{acc.declare = #acc.declare<dataClause = acc_create>, uniq_name = "local_array"}
68+
69+
// DECL-LABEL: func.func @test_local_memref() {
70+
// DECL: fircg.ext_declare{{.*}}{acc.declare = #acc.declare<dataClause = acc_create>, uniq_name = "local_array_declare"}

0 commit comments

Comments
 (0)