diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td index 24950322c3ca9..dc38e56d93c66 100644 --- a/flang/include/flang/Optimizer/Dialect/FIROps.td +++ b/flang/include/flang/Optimizer/Dialect/FIROps.td @@ -3074,6 +3074,7 @@ def fir_DeclareOp : fir_Op<"declare", [AttrSizedOperandSegments, AnyRefOrBox:$memref, Optional:$shape, Variadic:$typeparams, + Optional:$dummy_scope, Builtin_StringAttr:$uniq_name, OptionalAttr:$fortran_attrs, OptionalAttr:$cuda_attr @@ -3083,7 +3084,8 @@ def fir_DeclareOp : fir_Op<"declare", [AttrSizedOperandSegments, let assemblyFormat = [{ $memref (`(` $shape^ `)`)? (`typeparams` $typeparams^)? - attr-dict `:` functional-type(operands, results) + (`dummy_scope` $dummy_scope^)? + attr-dict `:` functional-type(operands, results) }]; let hasVerifier = 1; @@ -3247,6 +3249,86 @@ def fir_CUDADeallocateOp : fir_Op<"cuda_deallocate", let hasVerifier = 1; } +def fir_DummyScopeOp : fir_Op<"dummy_scope", + [MemoryEffects<[MemWrite]>]> { + let summary = "Define a scope for dummy arguments"; + + let description = [{ + An abstract handle to be used to associate dummy arguments of the same + subroutine between each other. By lowering, all [hl]fir.declare + operations representing declarations of dummy arguments of a subroutine + use the result of this operation. This allows recognizing the references + of these dummy arguments as belonging to the same runtime instance + of the subroutine even after MLIR inlining. Thus, the Fortran aliasing + rules might be applied to those references based on the original + declarations of the dummy arguments. + For example: + ``` + subroutine test(x, y) + real, target :: x, y + x = y ! may alias + call inner(x, y) + contains + subroutine inner(x, y) + real :: x, y + x = y ! may not alias + end subroutine inner + end subroutine test + ``` + After MLIR inlining this may look like this: + ``` + func.func @_QPtest( + %arg0: !fir.ref {fir.target}, + %arg1: !fir.ref {fir.target}) { + %0 = fir.declare %arg0 {fortran_attrs = #fir.var_attrs} : + (!fir.ref) -> !fir.ref + %1 = fir.declare %arg1 {fortran_attrs = #fir.var_attrs} : + (!fir.ref) -> !fir.ref + %2 = fir.load %1 : !fir.ref + fir.store %2 to %0 : !fir.ref + %3 = fir.declare %0 : (!fir.ref) -> !fir.ref + %4 = fir.declare %1 : (!fir.ref) -> !fir.ref + %5 = fir.load %4 : !fir.ref + fir.store %5 to %3 : !fir.ref + return + } + ``` + Without marking %3 and %4 as declaring the dummy arguments + of the same runtime instance of `inner` subroutine the FIR + AliasAnalysis cannot deduce non-aliasing for the second load/store pair. + This information may be preserved by using fir.dummy_scope operation: + ``` + func.func @_QPtest( + %arg0: !fir.ref {fir.target}, + %arg1: !fir.ref {fir.target}) { + %h1 = fir.dummy_scope : i1 + %0 = fir.declare %arg0 dummy_scope(%h1) + {fortran_attrs = #fir.var_attrs} : + (!fir.ref) -> !fir.ref + %1 = fir.declare %arg1 dummy_scope(%h1) + {fortran_attrs = #fir.var_attrs} : + (!fir.ref) -> !fir.ref + %2 = fir.load %1 : !fir.ref + fir.store %2 to %0 : !fir.ref + %h2 = fir.dummy_scope : i1 + %3 = fir.declare %0 dummy_scope(%h2) : (!fir.ref) -> !fir.ref + %4 = fir.declare %1 dummy_scope(%h2) : (!fir.ref) -> !fir.ref + %5 = fir.load %4 : !fir.ref + fir.store %5 to %3 : !fir.ref + return + } + ``` + Note that even if `inner` is called and inlined twice inside + `test`, the two inlined instances of `inner` must use two different + fir.dummy_scope operations for their fir.declare ops. This + two distinct fir.dummy_scope must remain distinct during the optimizations. + This is guaranteed by the write memory effect on the DebuggingResource. + }]; + + let results = (outs fir_DummyScopeType); + let assemblyFormat = "attr-dict `:` type(results)"; +} + def fir_CUDAAllocOp : fir_Op<"cuda_alloc", [AttrSizedOperandSegments, MemoryEffects<[MemAlloc]>]> { let summary = "Allocate an object on device"; diff --git a/flang/include/flang/Optimizer/Dialect/FIRTypes.td b/flang/include/flang/Optimizer/Dialect/FIRTypes.td index 7378ed93944c9..ae984de63db42 100644 --- a/flang/include/flang/Optimizer/Dialect/FIRTypes.td +++ b/flang/include/flang/Optimizer/Dialect/FIRTypes.td @@ -576,6 +576,17 @@ def fir_VoidType : FIR_Type<"Void", "void"> { let genStorageClass = 0; } +def fir_DummyScopeType : FIR_Type<"DummyScope", "dscope"> { + let summary = "Dummy scope type"; + + let description = [{ + `fir.dscope` is a type returned by fir.dummy_scope operation. + It defines a unique identifier for a runtime instance of a subroutine + that is used by the [hl]fir.declare operations representing + the dummy arguments' declarations. + }]; +} + // Whether a type is a BaseBoxType def IsBaseBoxTypePred : CPred<"mlir::isa<::fir::BaseBoxType>($_self)">; diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td index 743a6c98ec1a0..ee3c26800ae3a 100644 --- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td +++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td @@ -87,6 +87,7 @@ def hlfir_DeclareOp : hlfir_Op<"declare", [AttrSizedOperandSegments, AnyRefOrBox:$memref, Optional:$shape, Variadic:$typeparams, + Optional:$dummy_scope, Builtin_StringAttr:$uniq_name, OptionalAttr:$fortran_attrs, OptionalAttr:$cuda_attr @@ -96,7 +97,8 @@ def hlfir_DeclareOp : hlfir_Op<"declare", [AttrSizedOperandSegments, let assemblyFormat = [{ $memref (`(` $shape^ `)`)? (`typeparams` $typeparams^)? - attr-dict `:` functional-type(operands, results) + (`dummy_scope` $dummy_scope^)? + attr-dict `:` functional-type(operands, results) }]; let builders = [ diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index ce7ee22d5d774..5bd3ec8d18450 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -281,6 +281,20 @@ class DeclareOpConversion : public mlir::OpRewritePattern { } }; +class DummyScopeOpConversion + : public mlir::OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + + mlir::LogicalResult + matchAndRewrite(fir::DummyScopeOp dummyScopeOp, + mlir::PatternRewriter &rewriter) const override { + rewriter.replaceOpWithNewOp(dummyScopeOp, + dummyScopeOp.getType()); + return mlir::success(); + } +}; + class CodeGenRewrite : public fir::impl::CodeGenRewriteBase { public: void runOnOperation() override final { @@ -293,6 +307,7 @@ class CodeGenRewrite : public fir::impl::CodeGenRewriteBase { target.addIllegalOp(); target.addIllegalOp(); target.addIllegalOp(); + target.addIllegalOp(); target.addDynamicallyLegalOp([](fir::EmboxOp embox) { return !(embox.getShape() || mlir::isa( @@ -321,5 +336,6 @@ std::unique_ptr fir::createFirCodeGenRewritePass() { void fir::populatePreCGRewritePatterns(mlir::RewritePatternSet &patterns) { patterns.insert(patterns.getContext()); + DeclareOpConversion, DummyScopeOpConversion>( + patterns.getContext()); } diff --git a/flang/lib/Optimizer/CodeGen/TypeConverter.cpp b/flang/lib/Optimizer/CodeGen/TypeConverter.cpp index fb2ec3f0b2f5e..729ece6fc1774 100644 --- a/flang/lib/Optimizer/CodeGen/TypeConverter.cpp +++ b/flang/lib/Optimizer/CodeGen/TypeConverter.cpp @@ -115,6 +115,11 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA, return mlir::LLVM::LLVMStructType::getLiteral( none.getContext(), std::nullopt, /*isPacked=*/false); }); + addConversion([&](fir::DummyScopeType dscope) { + // DummyScopeType values must not have any uses after PreCGRewrite. + // Convert it here to i1 just in case it survives. + return mlir::IntegerType::get(&getContext(), 1); + }); // FIXME: https://reviews.llvm.org/D82831 introduced an automatic // materialization of conversion around function calls that is not working // well with fir lowering to llvm (incorrect llvm.mlir.cast are inserted). diff --git a/flang/lib/Optimizer/Dialect/FIRType.cpp b/flang/lib/Optimizer/Dialect/FIRType.cpp index d9c387ad950e8..daa3ac905dad5 100644 --- a/flang/lib/Optimizer/Dialect/FIRType.cpp +++ b/flang/lib/Optimizer/Dialect/FIRType.cpp @@ -1340,7 +1340,7 @@ void FIROpsDialect::registerTypes() { fir::ComplexType, FieldType, HeapType, fir::IntegerType, LenType, LogicalType, LLVMPointerType, PointerType, RealType, RecordType, ReferenceType, SequenceType, ShapeType, ShapeShiftType, ShiftType, - SliceType, TypeDescType, fir::VectorType>(); + SliceType, TypeDescType, fir::VectorType, fir::DummyScopeType>(); fir::ReferenceType::attachInterface< OpenMPPointerLikeModel>(*getContext()); fir::ReferenceType::attachInterface< diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp index 0d62ca4954e6b..4b586ad1d3a4a 100644 --- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp +++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp @@ -133,7 +133,8 @@ void hlfir::DeclareOp::build(mlir::OpBuilder &builder, mlir::Type hlfirVariableType = getHLFIRVariableType(inputType, hasExplicitLbs); build(builder, result, {hlfirVariableType, inputType}, memref, shape, - typeparams, nameAttr, fortran_attrs, cuda_attr); + typeparams, /*dummy_scope=*/nullptr, nameAttr, fortran_attrs, + cuda_attr); } mlir::LogicalResult hlfir::DeclareOp::verify() { diff --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp index 517285dce133d..3570e0011ca7e 100644 --- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp @@ -328,8 +328,8 @@ class DeclareOpConversion : public mlir::OpRewritePattern { cudaAttr = fir::CUDADataAttributeAttr::get(rewriter.getContext(), *attr); auto firDeclareOp = rewriter.create( loc, memref.getType(), memref, declareOp.getShape(), - declareOp.getTypeparams(), declareOp.getUniqName(), fortranAttrs, - cudaAttr); + declareOp.getTypeparams(), declareOp.getDummyScope(), + declareOp.getUniqName(), fortranAttrs, cudaAttr); // Propagate other attributes from hlfir.declare to fir.declare. // OpenACC's acc.declare is one example. Right now, the propagation diff --git a/flang/test/Fir/dummy-scope-codegen.fir b/flang/test/Fir/dummy-scope-codegen.fir new file mode 100644 index 0000000000000..caef3c1b25783 --- /dev/null +++ b/flang/test/Fir/dummy-scope-codegen.fir @@ -0,0 +1,9 @@ +// RUN: fir-opt --cg-rewrite %s -o - | FileCheck %s + +func.func @dummy_scope(%arg0: !fir.ref) { + %scope = fir.dummy_scope : !fir.dscope + %0 = fir.declare %arg0 dummy_scope %scope {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> !fir.ref + return +} +// CHECK-LABEL: func.func @dummy_scope( +// CHECK-NEXT: return diff --git a/flang/test/Fir/dummy_scope.fir b/flang/test/Fir/dummy_scope.fir new file mode 100644 index 0000000000000..58985923a8f44 --- /dev/null +++ b/flang/test/Fir/dummy_scope.fir @@ -0,0 +1,34 @@ +// RUN: fir-opt %s | fir-opt | FileCheck %s +// RUN: fir-opt %s | fir-opt -cse | FileCheck %s + +// CHECK-LABEL: func.func @dummy_scope( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref) { +// CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> !fir.ref +// CHECK: return +// CHECK: } +func.func @dummy_scope(%arg0: !fir.ref) { + %scope = fir.dummy_scope : !fir.dscope + %0 = fir.declare %arg0 dummy_scope %scope {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> !fir.ref + return +} + +// CHECK-LABEL: func.func @dummy_scopes( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref) { +// CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> !fir.ref +// CHECK: %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_4:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[VAL_3]] {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> !fir.ref +// CHECK: %[[VAL_5:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_6:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[VAL_5]] {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> !fir.ref +// CHECK: return +// CHECK: } +func.func @dummy_scopes(%arg0: !fir.ref) { + %scope_out = fir.dummy_scope : !fir.dscope + %0 = fir.declare %arg0 dummy_scope %scope_out {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> !fir.ref + %scope_in1 = fir.dummy_scope : !fir.dscope + %1 = fir.declare %arg0 dummy_scope %scope_in1 {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> !fir.ref + %scope_in2 = fir.dummy_scope : !fir.dscope + %2 = fir.declare %arg0 dummy_scope %scope_in2 {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> !fir.ref + return +} diff --git a/flang/test/HLFIR/declare-codegen.fir b/flang/test/HLFIR/declare-codegen.fir index 3e80a52be4524..9f51d0fbc7afd 100644 --- a/flang/test/HLFIR/declare-codegen.fir +++ b/flang/test/HLFIR/declare-codegen.fir @@ -200,3 +200,13 @@ func.func @test_optional_declare(%arg0: !fir.box>) { // CHECK: %[[VAL_7:.*]] = fir.absent !fir.box> // CHECK: fir.result %[[VAL_7]] : !fir.box> // CHECK: } + +func.func @dummy_scope(%arg0: !fir.ref) { + %scope = fir.dummy_scope : !fir.dscope + %0:2 = hlfir.declare %arg0 dummy_scope %scope {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) + return +} +// CHECK-LABEL: func.func @dummy_scope( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref) { +// CHECK: %[[SCOPE:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[SCOPE]] {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> !fir.ref diff --git a/flang/test/HLFIR/dummy_scope.fir b/flang/test/HLFIR/dummy_scope.fir new file mode 100644 index 0000000000000..6b5c61e21f1d1 --- /dev/null +++ b/flang/test/HLFIR/dummy_scope.fir @@ -0,0 +1,34 @@ +// RUN: fir-opt %s | fir-opt | FileCheck %s +// RUN: fir-opt %s | fir-opt -cse | FileCheck %s + +// CHECK-LABEL: func.func @dummy_scope( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref) { +// CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +// CHECK: return +// CHECK: } +func.func @dummy_scope(%arg0: !fir.ref) { + %scope = fir.dummy_scope : !fir.dscope + %0:2 = hlfir.declare %arg0 dummy_scope %scope {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) + return +} + +// CHECK-LABEL: func.func @dummy_scopes( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref) { +// CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_3]] {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_5:.*]] = fir.dummy_scope : !fir.dscope +// CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_5]] {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +// CHECK: return +// CHECK: } +func.func @dummy_scopes(%arg0: !fir.ref) { + %scope_out = fir.dummy_scope : !fir.dscope + %0:2 = hlfir.declare %arg0 dummy_scope %scope_out {uniq_name = "x"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) + %scope_in1 = fir.dummy_scope : !fir.dscope + %1:2 = hlfir.declare %arg0 dummy_scope %scope_in1 {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) + %scope_in2 = fir.dummy_scope : !fir.dscope + %2:2 = hlfir.declare %arg0 dummy_scope %scope_in2 {uniq_name = "innerEx"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) + return +} diff --git a/flang/unittests/Optimizer/FortranVariableTest.cpp b/flang/unittests/Optimizer/FortranVariableTest.cpp index 790f735a6cf29..f5f559ef887c8 100644 --- a/flang/unittests/Optimizer/FortranVariableTest.cpp +++ b/flang/unittests/Optimizer/FortranVariableTest.cpp @@ -48,7 +48,8 @@ TEST_F(FortranVariableTest, SimpleScalar) { mlir::Value addr = builder->create(loc, eleType); auto name = mlir::StringAttr::get(&context, "x"); auto declare = builder->create(loc, addr.getType(), addr, - /*shape=*/mlir::Value{}, /*typeParams=*/std::nullopt, name, + /*shape=*/mlir::Value{}, /*typeParams=*/std::nullopt, + /*dummy_scope=*/nullptr, name, /*fortran_attrs=*/fir::FortranVariableFlagsAttr{}, /*cuda_attr=*/fir::CUDADataAttributeAttr{}); @@ -74,7 +75,7 @@ TEST_F(FortranVariableTest, CharacterScalar) { loc, eleType, /*pinned=*/false, typeParams); auto name = mlir::StringAttr::get(&context, "x"); auto declare = builder->create(loc, addr.getType(), addr, - /*shape=*/mlir::Value{}, typeParams, name, + /*shape=*/mlir::Value{}, typeParams, /*dummy_scope=*/nullptr, name, /*fortran_attrs=*/fir::FortranVariableFlagsAttr{}, /*cuda_attr=*/fir::CUDADataAttributeAttr{}); @@ -105,7 +106,7 @@ TEST_F(FortranVariableTest, SimpleArray) { mlir::Value shape = createShape(extents); auto name = mlir::StringAttr::get(&context, "x"); auto declare = builder->create(loc, addr.getType(), addr, - shape, /*typeParams*/ std::nullopt, name, + shape, /*typeParams*/ std::nullopt, /*dummy_scope=*/nullptr, name, /*fortran_attrs=*/fir::FortranVariableFlagsAttr{}, /*cuda_attr=*/fir::CUDADataAttributeAttr{}); @@ -136,7 +137,7 @@ TEST_F(FortranVariableTest, CharacterArray) { mlir::Value shape = createShape(extents); auto name = mlir::StringAttr::get(&context, "x"); auto declare = builder->create(loc, addr.getType(), addr, - shape, typeParams, name, + shape, typeParams, /*dummy_scope=*/nullptr, name, /*fortran_attrs=*/fir::FortranVariableFlagsAttr{}, /*cuda_attr=*/fir::CUDADataAttributeAttr{});