Skip to content

Commit

Permalink
[flang][FIR] add fir.is_assumed_size operation (#93853)
Browse files Browse the repository at this point in the history
Assumed-rank fir.box/class may describe assumed-size array. This case
needs special handling in SELECT RANK. It is not possible to generate
FIR code to detect that a fir.box is an assumed-size (the way to detect
that is to check that upper dimension extent is -1 in the descriptor).

Instead of emitting a runtime call directly in lowering, add an
operation that can later be lowered to a runtime call or inline code
when the descriptor layout is known.
  • Loading branch information
jeanPerier authored May 31, 2024
1 parent f49d26b commit 5228c2c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,23 @@ def fir_BoxIsArrayOp : fir_SimpleOp<"box_isarray", [NoMemoryEffect]> {
let results = (outs BoolLike);
}

def fir_IsAssumedSizeOp : fir_SimpleOp<"is_assumed_size", [NoMemoryEffect]> {
let summary = "detect if a boxed value is an assumed-size array";

let description = [{
Fir box SSA values may describe assumed-size arrays. This operation
allows detecting this, even for assumed-rank box.

```
%a = fir.is_assumed_size %b : (!fir.box<!fir.array<*:f64>>) -> i1
```
}];

let arguments = (ins BoxOrClassType:$val);

let results = (outs BoolLike);
}

def fir_BoxIsPtrOp : fir_SimpleOp<"box_isptr", [NoMemoryEffect]> {
let summary = "is the boxed value a POINTER?";

Expand Down
11 changes: 11 additions & 0 deletions flang/test/Fir/fir-ops.fir
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,14 @@ func.func @test_rebox_assumed_rank(%arg0: !fir.box<!fir.array<*:f32>> ) {
// CHECK: fir.rebox_assumed_rank %[[A]] lbs ones : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>
// CHECK: fir.rebox_assumed_rank %[[A]] lbs zeroes : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>
// CHECK: fir.rebox_assumed_rank %[[A]] lbs preserve : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>

func.func @test_is_assumed_size(%arg0: !fir.class<!fir.array<*:none>>, %arg1 : !fir.box<!fir.array<?xf32>>) {
%1 = fir.is_assumed_size %arg0 : (!fir.class<!fir.array<*:none>>) -> i1
%2 = fir.is_assumed_size %arg1 : (!fir.box<!fir.array<?xf32>>) -> i1
return
}
// CHECK-LABEL: func.func @test_is_assumed_size(
// CHECK-SAME: %[[A:.*]]: !fir.class<!fir.array<*:none>>,
// CHECK-SAME: %[[B:.*]]: !fir.box<!fir.array<?xf32>>)
// CHECK: fir.is_assumed_size %[[A]] : (!fir.class<!fir.array<*:none>>) -> i1
// CHECK: fir.is_assumed_size %[[B]] : (!fir.box<!fir.array<?xf32>>) -> i1
8 changes: 8 additions & 0 deletions flang/test/Fir/invalid.fir
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,11 @@ func.func @bad_rebox_assumed_rank_3(%arg0: !fir.box<!fir.array<*:f32>> ) {
%1 = fir.rebox_assumed_rank %arg0 lbs ones : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:i32>>
return
}

// -----

func.func @bad_is_assumed_size(%arg0: !fir.ref<!fir.array<*:none>>) {
// expected-error@+1{{op operand #0 must be box or class, but got '!fir.ref<!fir.array<*:none>>'}}
%1 = fir.is_assumed_size %arg0 : (!fir.ref<!fir.array<*:none>>) -> i1
return
}

0 comments on commit 5228c2c

Please sign in to comment.