Skip to content

Commit f30a573

Browse files
vdonaldsonjeanPerier
authored andcommittedOct 28, 2021
Allocmem array extents (#1158)
Fixed sized allocmem array extents are part of the type, and should not be duplicated as dynamic values.
1 parent a8b8484 commit f30a573

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed
 

‎flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,17 @@ class ArrayUpdateConversionBase : public mlir::OpRewritePattern<ArrayOp> {
798798
rewriter.setInsertionPoint(loadOp);
799799
// Copy in.
800800
llvm::SmallVector<mlir::Value> extents;
801+
llvm::SmallVector<mlir::Value> nonconstantExtents;
801802
auto shapeOp = getOrReadExtentsAndShapeOp(loc, rewriter, load, extents);
803+
auto arrTy = fir::unwrapPassByRefType(load.memref().getType());
804+
auto seqTy = arrTy.template dyn_cast<fir::SequenceType>();
805+
assert(seqTy && "expecting sequence type");
806+
for (auto [s, x] : llvm::zip(seqTy.getShape(), extents))
807+
if (s == fir::SequenceType::getUnknownExtent())
808+
nonconstantExtents.emplace_back(x);
802809
auto allocmem = rewriter.create<AllocMemOp>(
803810
loc, dyn_cast_ptrOrBoxEleTy(load.memref().getType()),
804-
load.typeparams(), extents);
811+
load.typeparams(), nonconstantExtents);
805812
genArrayCopy(load.getLoc(), rewriter, allocmem, load.memref(), shapeOp,
806813
load);
807814
rewriter.setInsertionPoint(op);

‎flang/test/Fir/array-modify.fir

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func @overlap(%arg0: !fir.ref<!fir.array<100xf32>>) {
7979
// CHECK-DAG: %[[VAL_5:.*]] = arith.constant 0 : index
8080
// CHECK: %[[VAL_6:.*]] = fir.alloca f32
8181
// CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
82-
// CHECK: %[[VAL_8:.*]] = fir.allocmem !fir.array<100xf32>, %[[VAL_1]]
82+
// CHECK: %[[VAL_8:.*]] = fir.allocmem !fir.array<100xf32>{{$}}
8383
// CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_1]] : (index) -> index
8484
// CHECK: %[[VAL_10:.*]] = arith.constant 0 : index
8585
// CHECK: %[[VAL_11:.*]] = arith.constant 1 : index

‎flang/test/Fir/arrexp.fir

+1-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func @f4(%a : !fir.ref<!fir.array<?x?xf32>>, %b : !fir.ref<!fir.array<?x?xf32>>,
101101
// Array expression assignment with potentially non contiguous arrays (e.g.
102102
// `a = b + f`, with and v assumed shapes.
103103
// Tests that the stride from the descriptor is used.
104-
// CHECK-LINE: define void @f6
104+
// CHECK-LINE: define void @f5
105105
// CHECK: ({ float*, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }* %[[A:[^,]*]], { float*, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }* %[[B:[^,]*]], float %[[F:.*]])
106106
func @f5(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: !fir.box<!fir.array<?xf32>>, %arg2: f32) {
107107
%c0 = arith.constant 0 : index
@@ -133,7 +133,6 @@ func @f5(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: !fir.box<!fir.array<?xf32>>,
133133
return
134134
}
135135

136-
137136
// Overlapping array expression assignment with a potentially non
138137
// contiguous array (e.g. `a(2:10:1) = a(1:9:1) + f`, with a assumed shape).
139138
// Test that a temp is created.
@@ -151,7 +150,6 @@ func @f6(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: f32) {
151150
// CHECK: %[[SIZE:.*]] = mul i64 4, %[[EXTENT]]
152151
// CHECK: %[[MALLOC:.*]] = call i8* @malloc(i64 %[[SIZE]])
153152
// CHECK: %[[TMP:.*]] = bitcast i8* %[[MALLOC]] to float*
154-
155153
%1 = fir.slice %c2, %c10, %c1 : (index, index, index) -> !fir.slice<1>
156154
%2 = fir.array_load %arg0 [%1] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>) -> !fir.array<?xf32>
157155
%3 = fir.slice %c1, %c9, %c1 : (index, index, index) -> !fir.slice<1>

‎flang/test/Lower/array-temp.f90

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
! RUN: bbc %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func @_QPss1()
4+
subroutine ss1
5+
! CHECK: %[[aa:[0-9]+]] = fir.alloca !fir.array<2650000xf32> {bindc_name = "aa", uniq_name = "_QFss1Eaa"}
6+
! CHECK: %[[shape:[0-9]+]] = fir.shape {{.*}} : (index) -> !fir.shape<1>
7+
integer, parameter :: N = 2650000
8+
real aa(N)
9+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
10+
aa = -2
11+
! CHECK: %[[temp:[0-9]+]] = fir.allocmem !fir.array<2650000xf32>
12+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
13+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}} : (!fir.heap<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
14+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) [{{.*}}] {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
15+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) [{{.*}}] {{.*}} : (!fir.heap<!fir.array<2650000xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
16+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}} : (!fir.heap<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
17+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<2650000xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
18+
! CHECK: fir.freemem %[[temp]] : !fir.heap<!fir.array<2650000xf32>>
19+
aa(2:N) = aa(1:N-1) + 7.0
20+
! print*, aa(1:2), aa(N-1:N)
21+
end
22+
23+
! CHECK-LABEL: func @_QPss2
24+
subroutine ss2(N)
25+
! CHECK: %[[arg:[0-9]+]] = fir.load %arg0 : !fir.ref<i32>
26+
! CHECK: %[[n:[0-9]+]] = fir.convert %[[arg]] : (i32) -> index
27+
! CHECK: %[[aa:[0-9]+]] = fir.alloca !fir.array<?xf32>, %[[n]] {bindc_name = "aa", uniq_name = "_QFss2Eaa"}
28+
real aa(N)
29+
! CHECK: %[[shape:[0-9]+]] = fir.shape %[[n]] : (index) -> !fir.shape<1>
30+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
31+
aa = -2
32+
! CHECK: %[[temp:[0-9]+]] = fir.allocmem !fir.array<?xf32>, %[[n]]
33+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
34+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}} : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
35+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) [{{.*}}] {{.*}} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
36+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) [{{.*}}] {{.*}} : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
37+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}} : (!fir.heap<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
38+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
39+
! CHECK: fir.freemem %[[temp]] : !fir.heap<!fir.array<?xf32>>
40+
aa(2:N) = aa(1:N-1) + 7.0
41+
! print*, aa(1:2), aa(N-1:N)
42+
end
43+
44+
! CHECK-LABEL: func @_QPss3
45+
subroutine ss3(N)
46+
! CHECK: %[[arg:[0-9]+]] = fir.load %arg0 : !fir.ref<i32>
47+
! CHECK: %[[n:[0-9]+]] = fir.convert %[[arg]] : (i32) -> index
48+
! CHECK: %[[aa:[0-9]+]] = fir.alloca !fir.array<2x?xf32>, %[[n]] {bindc_name = "aa", uniq_name = "_QFss3Eaa"}
49+
real aa(2,N)
50+
! CHECK: %[[shape:[0-9]+]] = fir.shape {{.*}} %[[n]] : (index, index) -> !fir.shape<2>
51+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}}, {{.*}} : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
52+
aa = -2
53+
! CHECK: %[[temp:[0-9]+]] = fir.allocmem !fir.array<2x?xf32>, %[[n]]
54+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}}, {{.*}} : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
55+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}}, {{.*}} : (!fir.heap<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
56+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) [{{.*}}] {{.*}}, {{.*}} : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
57+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) [{{.*}}] {{.*}}, {{.*}} : (!fir.heap<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
58+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}}, {{.*}} : (!fir.heap<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
59+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}}, {{.*}} : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
60+
! CHECK: fir.freemem %[[temp]] : !fir.heap<!fir.array<2x?xf32>>
61+
aa(:,2:N) = aa(:,1:N-1) + 7.0
62+
! print*, aa(:,1:2), aa(:,N-1:N)
63+
end
64+
65+
! CHECK-LABEL: func @_QPss4
66+
subroutine ss4(N)
67+
! CHECK: %[[arg:[0-9]+]] = fir.load %arg0 : !fir.ref<i32>
68+
! CHECK: %[[n:[0-9]+]] = fir.convert %[[arg]] : (i32) -> index
69+
! CHECK: %[[aa:[0-9]+]] = fir.alloca !fir.array<?x2xf32>, %[[n]] {bindc_name = "aa", uniq_name = "_QFss4Eaa"}
70+
real aa(N,2)
71+
! CHECK: %[[shape:[0-9]+]] = fir.shape %[[n]], {{.*}} : (index, index) -> !fir.shape<2>
72+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}}, {{.*}} : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
73+
aa = -2
74+
! CHECK: %[[temp:[0-9]+]] = fir.allocmem !fir.array<?x2xf32>, %[[n]]
75+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}}, {{.*}} : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
76+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}}, {{.*}} : (!fir.heap<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
77+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) [{{.*}}] {{.*}}, {{.*}} : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
78+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) [{{.*}}] {{.*}}, {{.*}} : (!fir.heap<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<f32>
79+
! CHECK: fir.array_coor %[[temp]](%[[shape]]) {{.*}}, {{.*}} : (!fir.heap<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
80+
! CHECK: fir.array_coor %[[aa]](%[[shape]]) {{.*}}, {{.*}} : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
81+
! CHECK: fir.freemem %[[temp]] : !fir.heap<!fir.array<?x2xf32>>
82+
aa(2:N,:) = aa(1:N-1,:) + 7.0
83+
! print*, aa(1:2,:), aa(N-1:N,:)
84+
end
85+
86+
program p
87+
! call ss1
88+
! call ss2(2650000)
89+
! call ss3(2650000)
90+
! call ss4(2650000)
91+
end

0 commit comments

Comments
 (0)