Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,17 @@ class DeclareOpConversion : public mlir::OpRewritePattern<fir::DeclareOp> {
else
return mlir::failure();
}
// FIXME: Add FortranAttrs and CudaAttrs

auto xDeclOp = rewriter.create<fir::cg::XDeclareOp>(
loc, declareOp.getType(), declareOp.getMemref(), shapeOpers, shiftOpers,
declareOp.getTypeparams(), declareOp.getDummyScope(),
declareOp.getUniqName());

// 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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this not overriding the operandSegmentSizes attribute of the AttrSizedOperandSegments interface of the newly created xDeclOp with the one from the declareOp?

I would suggest checking if the attributes names are not already used in the xDeclop to avoid any issues.


LLVM_DEBUG(llvm::dbgs()
<< "rewriting " << declareOp << " to " << xDeclOp << '\n');
rewriter.replaceOp(declareOp, xDeclOp.getOperation()->getResults());
Expand Down
17 changes: 14 additions & 3 deletions flang/test/Fir/declare-codegen.fir
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func.func private @bar(%arg0: !fir.ref<!fir.array<12x23xi32>>)

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


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

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

// Test DCE does not crash because of unreachable code.
func.func @unreachable_code(%arg0: !fir.ref<!fir.char<1,10>>) {
Expand All @@ -51,4 +51,15 @@ func.func @unreachable_code(%arg0: !fir.ref<!fir.char<1,10>>) {
// 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<dataClause = acc_create>} : (!fir.ref<!fir.array<10xi32>>) -> !fir.ref<!fir.array<10xi32>>
return
}
// DECL-LABEL: func.func @test_local_memref() {
// DECL: fircg.ext_declare{{.*}}{acc.declare = #acc.declare<dataClause = acc_create>, uniq_name = "local_array_declare"}