Skip to content

Commit 0b54e33

Browse files
[flang] Add FIR attributes and apply them to dummy arguments (#115686)
To determine if a function's dummy argument is nocapture, add the asynchronous attribute to the FIR attribute. The volatile attribute will also be used to determine nocapture assignment, but this will remain a TODO until other processing using volatile is implemented. I will post another patch to apply nocapture. See also the discussion in the following discourse post. https://discourse.llvm.org/t/applying-the-nocapture-attribute-to-reference-passed-arguments-in-fortran-subroutines/81401
1 parent d3da788 commit 0b54e33

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

flang/include/flang/Optimizer/Dialect/FIROpsSupport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ constexpr llvm::StringRef getOptionalAttrName() { return "fir.optional"; }
7575
/// Attribute to mark Fortran entities with the TARGET attribute.
7676
static constexpr llvm::StringRef getTargetAttrName() { return "fir.target"; }
7777

78+
/// Attribute to mark Fortran entities with the ASYNCHRONOUS attribute.
79+
static constexpr llvm::StringRef getAsynchronousAttrName() {
80+
return "fir.asynchronous";
81+
}
82+
83+
/// Attribute to mark Fortran entities with the VOLATILE attribute.
84+
static constexpr llvm::StringRef getVolatileAttrName() {
85+
return "fir.volatile";
86+
}
87+
7888
/// Attribute to mark that a function argument is a character dummy procedure.
7989
/// Character dummy procedure have special ABI constraints.
8090
static constexpr llvm::StringRef getCharacterProcedureDummyAttrName() {

flang/lib/Lower/CallInterface.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,17 +1107,22 @@ class Fortran::lower::CallInterfaceImpl {
11071107
};
11081108
if (obj.attrs.test(Attrs::Optional))
11091109
addMLIRAttr(fir::getOptionalAttrName());
1110-
// Skipping obj.attrs.test(Attrs::Asynchronous), this does not impact the
1111-
// way the argument is passed given flang implement asynch IO synchronously.
1112-
// TODO: it would be safer to treat them as volatile because since Fortran
1113-
// 2018 asynchronous can also be used for C defined asynchronous user
1114-
// processes (see 18.10.4 Asynchronous communication).
11151110
if (obj.attrs.test(Attrs::Contiguous))
11161111
addMLIRAttr(fir::getContiguousAttrName());
11171112
if (obj.attrs.test(Attrs::Value))
11181113
isValueAttr = true; // TODO: do we want an mlir::Attribute as well?
1119-
if (obj.attrs.test(Attrs::Volatile))
1114+
if (obj.attrs.test(Attrs::Volatile)) {
11201115
TODO(loc, "VOLATILE in procedure interface");
1116+
addMLIRAttr(fir::getVolatileAttrName());
1117+
}
1118+
// obj.attrs.test(Attrs::Asynchronous) does not impact the way the argument
1119+
// is passed given flang implement asynch IO synchronously. However, it's
1120+
// added to determine whether the argument is captured.
1121+
// TODO: it would be safer to treat them as volatile because since Fortran
1122+
// 2018 asynchronous can also be used for C defined asynchronous user
1123+
// processes (see 18.10.4 Asynchronous communication).
1124+
if (obj.attrs.test(Attrs::Asynchronous))
1125+
addMLIRAttr(fir::getAsynchronousAttrName());
11211126
if (obj.attrs.test(Attrs::Target))
11221127
addMLIRAttr(fir::getTargetAttrName());
11231128
if (obj.cudaDataAttr)

flang/test/Lower/HLFIR/select-rank.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ subroutine test_branching(x)
416416
! CHECK: }
417417

418418
! CHECK-LABEL: func.func @_QPtest_rank_star_attributes(
419-
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>> {fir.bindc_name = "x", fir.optional, fir.target}) {
419+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>> {fir.asynchronous, fir.bindc_name = "x", fir.optional, fir.target}) {
420420
! CHECK: %[[VAL_1:.*]] = fir.dummy_scope : !fir.dscope
421421
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_1]] {fortran_attrs = #fir.var_attrs<asynchronous, optional, target>, uniq_name = "_QFtest_rank_star_attributesEx"} : (!fir.box<!fir.array<*:f32>>, !fir.dscope) -> (!fir.box<!fir.array<*:f32>>, !fir.box<!fir.array<*:f32>>)
422422
! CHECK: %[[VAL_3:.*]] = arith.constant 2 : i8

flang/test/Lower/attributes.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ subroutine foo2(x, i)
2727
subroutine foo3(x)
2828
real, optional, contiguous :: x(:)
2929
end subroutine
30+
31+
! CHECK-LABEL: func @_QPfoo4
32+
! CHECK-SAME: %arg0: !fir.ref<f32> {fir.bindc_name = "x", fir.target}
33+
! CHECK-SAME: %arg1: !fir.ref<f32> {fir.asynchronous, fir.bindc_name = "y"}
34+
subroutine foo4(x, y)
35+
real, target :: x
36+
real, asynchronous :: y
37+
end subroutine

0 commit comments

Comments
 (0)