From 3301f3e57951c0a8e2bba78611bbf10a8387ed9f Mon Sep 17 00:00:00 2001 From: Susan Tan Date: Thu, 24 Apr 2025 08:58:30 -0700 Subject: [PATCH 1/4] propogate attributes through cg-rewrite --- flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp | 20 +++++++++++++++++- flang/test/Fir/declare-codegen.fir | 22 +++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index d09d7d397e8b7f..052c96b15570f5 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -281,6 +281,19 @@ class DeclareOpConversion : public mlir::OpRewritePattern { matchAndRewrite(fir::DeclareOp declareOp, mlir::PatternRewriter &rewriter) const override { if (!preserveDeclare) { + auto memrefOp = declareOp.getMemref().getDefiningOp(); + if(!memrefOp){ + rewriter.replaceOp(declareOp, declareOp.getMemref()); + return mlir::success(); + } + + // attach metadatas from the fir.declare to its memref (if it's an operation) + mlir::NamedAttrList elidedAttrs = + mlir::NamedAttrList{memrefOp->getAttrs()}; + for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) + if (!elidedAttrs.get(attr.getName())) + memrefOp->setAttr(attr.getName(), attr.getValue()); + rewriter.replaceOp(declareOp, declareOp.getMemref()); return mlir::success(); } @@ -299,11 +312,16 @@ class DeclareOpConversion : public mlir::OpRewritePattern { else return mlir::failure(); } - // FIXME: Add FortranAttrs and CudaAttrs + auto xDeclOp = rewriter.create( loc, declareOp.getType(), declareOp.getMemref(), shapeOpers, shiftOpers, declareOp.getTypeparams(), declareOp.getDummyScope(), declareOp.getUniqName()); + + // attach metadatas from fir.declare to fircg.ext_declare + for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) + xDeclOp->setAttr(attr.getName(), attr.getValue()); + LLVM_DEBUG(llvm::dbgs() << "rewriting " << declareOp << " to " << xDeclOp << '\n'); rewriter.replaceOp(declareOp, xDeclOp.getOperation()->getResults()); diff --git a/flang/test/Fir/declare-codegen.fir b/flang/test/Fir/declare-codegen.fir index fe8d84ef2d19f9..ad0208219ddf72 100644 --- a/flang/test/Fir/declare-codegen.fir +++ b/flang/test/Fir/declare-codegen.fir @@ -23,7 +23,7 @@ func.func private @bar(%arg0: !fir.ref>) // DECL-LABEL: func.func @test( // DECL-SAME: %[[arg0:.*]]: !fir.ref>) { -// DECL: fircg.ext_declare +// DECL: fircg.ext_declare{{.*}}{uniq_name = "_QFarray_numeric_lboundsEx"} func.func @useless_shape_with_duplicate_extent_operand(%arg0: !fir.ref>) { @@ -37,7 +37,7 @@ func.func @useless_shape_with_duplicate_extent_operand(%arg0: !fir.ref>) { @@ -51,4 +51,20 @@ func.func @unreachable_code(%arg0: !fir.ref>) { // NODECL-LABEL: func.func @unreachable_code( // NODECL-NOT: uniq_name = "live_code" // DECL-LABEL: func.func @unreachable_code( -// DECL: uniq_name = "live_code" +// DECL: fircg.ext_declare{{.*}}{uniq_name = "live_code"} +// DECL: fir.declare{{.*}}{uniq_name = "dead_code"} + + +// Test that attributes get attached to the memref operation when fir.declare is not preserved +func.func @test_local_memref() { + %0 = fir.alloca !fir.array<10xi32> {uniq_name = "local_array"} + %1 = fir.declare %0 {uniq_name = "local_array_declare", acc.declare = #acc.declare} : (!fir.ref>) -> !fir.ref> + return +} +// NODECL-LABEL: func.func @test_local_memref() { + +// Note: the uniq_name is not attached of fir.declare is not preserved because it conflicts with the uniq_name of the memref +// NODECL: fir.alloca{{.*}}{acc.declare = #acc.declare, uniq_name = "local_array"} + +// DECL-LABEL: func.func @test_local_memref() { +// DECL: fircg.ext_declare{{.*}}{acc.declare = #acc.declare, uniq_name = "local_array_declare"} From 353e86a946eb79011fd4f52f9e1d776b8b2eca6e Mon Sep 17 00:00:00 2001 From: Susan Tan Date: Thu, 24 Apr 2025 08:59:33 -0700 Subject: [PATCH 2/4] format --- flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index 052c96b15570f5..0b9de86de1e955 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -281,13 +281,14 @@ class DeclareOpConversion : public mlir::OpRewritePattern { matchAndRewrite(fir::DeclareOp declareOp, mlir::PatternRewriter &rewriter) const override { if (!preserveDeclare) { - auto memrefOp = declareOp.getMemref().getDefiningOp(); - if(!memrefOp){ - rewriter.replaceOp(declareOp, declareOp.getMemref()); - return mlir::success(); + auto memrefOp = declareOp.getMemref().getDefiningOp(); + if (!memrefOp) { + rewriter.replaceOp(declareOp, declareOp.getMemref()); + return mlir::success(); } - - // attach metadatas from the fir.declare to its memref (if it's an operation) + + // attach metadatas from the fir.declare to its memref (if it's an + // operation) mlir::NamedAttrList elidedAttrs = mlir::NamedAttrList{memrefOp->getAttrs()}; for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) @@ -319,8 +320,8 @@ class DeclareOpConversion : public mlir::OpRewritePattern { declareOp.getUniqName()); // attach metadatas from fir.declare to fircg.ext_declare - for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) - xDeclOp->setAttr(attr.getName(), attr.getValue()); + for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) + xDeclOp->setAttr(attr.getName(), attr.getValue()); LLVM_DEBUG(llvm::dbgs() << "rewriting " << declareOp << " to " << xDeclOp << '\n'); From aa366aed1644489dc94f3471920c2c13e82baec4 Mon Sep 17 00:00:00 2001 From: Susan Tan Date: Mon, 28 Apr 2025 09:41:07 -0700 Subject: [PATCH 3/4] remove propogation when fir.declare is removed --- flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp | 14 -------------- flang/test/Fir/declare-codegen.fir | 5 ----- 2 files changed, 19 deletions(-) diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index 0b9de86de1e955..587bcaf718c0bc 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -281,20 +281,6 @@ class DeclareOpConversion : public mlir::OpRewritePattern { matchAndRewrite(fir::DeclareOp declareOp, mlir::PatternRewriter &rewriter) const override { if (!preserveDeclare) { - auto memrefOp = declareOp.getMemref().getDefiningOp(); - if (!memrefOp) { - rewriter.replaceOp(declareOp, declareOp.getMemref()); - return mlir::success(); - } - - // attach metadatas from the fir.declare to its memref (if it's an - // operation) - mlir::NamedAttrList elidedAttrs = - mlir::NamedAttrList{memrefOp->getAttrs()}; - for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) - if (!elidedAttrs.get(attr.getName())) - memrefOp->setAttr(attr.getName(), attr.getValue()); - rewriter.replaceOp(declareOp, declareOp.getMemref()); return mlir::success(); } diff --git a/flang/test/Fir/declare-codegen.fir b/flang/test/Fir/declare-codegen.fir index ad0208219ddf72..cf4f28eda1cb4b 100644 --- a/flang/test/Fir/declare-codegen.fir +++ b/flang/test/Fir/declare-codegen.fir @@ -61,10 +61,5 @@ func.func @test_local_memref() { %1 = fir.declare %0 {uniq_name = "local_array_declare", acc.declare = #acc.declare} : (!fir.ref>) -> !fir.ref> return } -// NODECL-LABEL: func.func @test_local_memref() { - -// Note: the uniq_name is not attached of fir.declare is not preserved because it conflicts with the uniq_name of the memref -// NODECL: fir.alloca{{.*}}{acc.declare = #acc.declare, uniq_name = "local_array"} - // DECL-LABEL: func.func @test_local_memref() { // DECL: fircg.ext_declare{{.*}}{acc.declare = #acc.declare, uniq_name = "local_array_declare"} From 196dfebba8dc22b2dfd9ec218cbcaffea33964bd Mon Sep 17 00:00:00 2001 From: Susan Tan Date: Mon, 28 Apr 2025 10:44:51 -0700 Subject: [PATCH 4/4] update comment --- flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index 587bcaf718c0bc..c1e127260be824 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -305,7 +305,8 @@ class DeclareOpConversion : public mlir::OpRewritePattern { declareOp.getTypeparams(), declareOp.getDummyScope(), declareOp.getUniqName()); - // attach metadatas from fir.declare to fircg.ext_declare + // Propagate all attributes to fircg.ext_declare to avoid losing metadata + // including CUDA and OpenACC attributes. for (const mlir::NamedAttribute &attr : declareOp->getAttrs()) xDeclOp->setAttr(attr.getName(), attr.getValue());