1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
//===-- LowerRepackArrays.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
/// This pass expands fir.pack_array and fir.unpack_array operations
/// into sequences of other FIR operations and Fortran runtime calls.
/// This pass is using structured control flow FIR operations such
/// as fir.if, so its placement in the pipeline should guarantee
/// further lowering of these operations.
///
/// A fir.pack_array operation is converted into a sequence of checks
/// identifying whether an array needs to be copied into a contiguous
/// temporary. When the checks pass, a new memory allocation is done
/// for the temporary array (in either stack or heap memory).
/// If `fir.pack_array` does not have no_copy attribute, then
/// the original array is shallow-copied into the temporary.
///
/// A fir.unpack_array operations is converted into a check
/// of whether the original and the temporary arrays are different
/// memory. When the check passes, the temporary array might be
/// shallow-copied into the original array, and then the temporary
/// array is deallocated (if it was allocated in stack memory,
/// then there is no explicit deallocation).
//===----------------------------------------------------------------------===//
#include "flang/Optimizer/CodeGen/CodeGen.h"
#include "flang/Optimizer/Builder/Character.h"
#include "flang/Optimizer/Builder/FIRBuilder.h"
#include "flang/Optimizer/Builder/MutableBox.h"
#include "flang/Optimizer/Builder/Runtime/Allocatable.h"
#include "flang/Optimizer/Builder/Runtime/Transformational.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROps.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/OpenACC/Support/RegisterOpenACCExtensions.h"
#include "flang/Optimizer/OpenMP/Support/RegisterOpenMPExtensions.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
namespace fir {
#define GEN_PASS_DEF_LOWERREPACKARRAYSPASS
#include "flang/Optimizer/CodeGen/CGPasses.h.inc"
} // namespace fir
#define DEBUG_TYPE "lower-repack-arrays"
namespace {
class PackArrayConversion : public mlir::OpRewritePattern<fir::PackArrayOp> {
public:
using OpRewritePattern::OpRewritePattern;
mlir::LogicalResult
matchAndRewrite(fir::PackArrayOp op,
mlir::PatternRewriter &rewriter) const override;
private:
static constexpr llvm::StringRef bufferName = ".repacked";
// Return value of fir::BaseBoxType that represents a temporary
// array created for the original box with given lbounds/extents and
// type parameters. The new box has the same shape as the original
// array. If useStack is true, then the temporary will be allocated
// in stack memory (when possible).
static mlir::Value allocateTempBuffer(fir::FirOpBuilder &builder,
mlir::Location loc, bool useStack,
mlir::Value origBox,
llvm::ArrayRef<mlir::Value> lbounds,
llvm::ArrayRef<mlir::Value> extents,
llvm::ArrayRef<mlir::Value> typeParams);
// Generate value of fir::BaseBoxType that represents the result
// of the given fir.pack_array operation. The original box
// is assumed to be present (though, it may represent an empty array).
static mlir::FailureOr<mlir::Value> genRepackedBox(fir::FirOpBuilder &builder,
mlir::Location loc,
fir::PackArrayOp packOp);
};
class UnpackArrayConversion
: public mlir::OpRewritePattern<fir::UnpackArrayOp> {
public:
using OpRewritePattern::OpRewritePattern;
mlir::LogicalResult
matchAndRewrite(fir::UnpackArrayOp op,
mlir::PatternRewriter &rewriter) const override;
};
} // anonymous namespace
// Return true iff for the given original boxed array we can
// allocate temporary memory in stack memory.
// This function is used to synchronize allocation/deallocation
// implied by fir.pack_array and fir.unpack_array, because
// the presence of the stack attribute does not automatically
// mean that the allocation is actually done in stack memory.
// For example, we always do the heap allocation for polymorphic
// types using Fortran runtime. Currently, we allocate all
// repack temporaries of derived types as polymorphic,
// so that we can preserve the dynamic type of the original.
// Adding the polymorpic mold to fir.alloca and then using
// Fortran runtime to compute the allocation size could probably
// resolve this limitation.
static bool canAllocateTempOnStack(mlir::Value box) {
return !fir::isPolymorphicType(box.getType());
}
/// Return true if array repacking is safe either statically
/// (there are no 'is_safe' attributes) or dynamically
/// (neither of the 'is_safe' attributes claims 'isDynamicallySafe() == false').
/// \p op is either fir.pack_array or fir.unpack_array.
template <typename OP>
static bool repackIsSafe(OP op) {
bool isSafe = true;
if (auto isSafeAttrs = op.getIsSafe()) {
// We currently support only the attributes for which
// isDynamicallySafe() returns false.
for (auto attr : *isSafeAttrs) {
auto iface = mlir::cast<fir::SafeTempArrayCopyAttrInterface>(attr);
if (iface.isDynamicallySafe())
TODO(op.getLoc(), "dynamically safe array repacking");
else
isSafe = false;
}
}
return isSafe;
}
mlir::LogicalResult
PackArrayConversion::matchAndRewrite(fir::PackArrayOp op,
mlir::PatternRewriter &rewriter) const {
mlir::Value box = op.getArray();
// If repacking is not safe, then just use the original box.
if (!repackIsSafe(op)) {
rewriter.replaceOp(op, box);
return mlir::success();
}
mlir::Location loc = op.getLoc();
fir::FirOpBuilder builder(rewriter, op.getOperation());
if (op.getMaxSize() || op.getMaxElementSize() || op.getMinStride())
TODO(loc, "fir.pack_array with constraints");
if (op.getHeuristics() != fir::PackArrayHeuristics::None)
TODO(loc, "fir.pack_array with heuristics");
auto boxType = mlir::cast<fir::BaseBoxType>(box.getType());
// For now we have to always check if the box is present.
auto isPresent =
builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), box);
fir::IfOp ifOp = builder.create<fir::IfOp>(loc, boxType, isPresent,
/*withElseRegion=*/true);
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
// The box is present.
auto newBox = genRepackedBox(builder, loc, op);
if (mlir::failed(newBox))
return newBox;
builder.create<fir::ResultOp>(loc, *newBox);
// The box is not present. Return original box.
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
builder.create<fir::ResultOp>(loc, box);
rewriter.replaceOp(op, ifOp.getResult(0));
return mlir::success();
}
mlir::Value PackArrayConversion::allocateTempBuffer(
fir::FirOpBuilder &builder, mlir::Location loc, bool useStack,
mlir::Value origBox, llvm::ArrayRef<mlir::Value> lbounds,
llvm::ArrayRef<mlir::Value> extents,
llvm::ArrayRef<mlir::Value> typeParams) {
auto tempType = mlir::cast<fir::SequenceType>(
fir::extractSequenceType(origBox.getType()));
assert(tempType.getDimension() == extents.size() &&
"number of extents does not match the rank");
mlir::Value shape = builder.genShape(loc, extents);
auto [base, isHeapAllocation] = builder.createArrayTemp(
loc, tempType, shape, extents, typeParams,
fir::FirOpBuilder::genTempDeclareOp,
fir::isPolymorphicType(origBox.getType()) ? origBox : nullptr, useStack,
bufferName);
// Make sure canAllocateTempOnStack() can recognize when
// the temporary is actually allocated on the stack
// by createArrayTemp(). Otherwise, we may miss dynamic
// deallocation when lowering fir.unpack_array.
if (useStack && canAllocateTempOnStack(origBox))
assert(!isHeapAllocation && "temp must have been allocated on the stack");
mlir::Type ptrType = base.getType();
if (auto tempBoxType = mlir::dyn_cast<fir::BaseBoxType>(ptrType)) {
// We need to reset the CFI_attribute_allocatable before
// returning the temporary box to avoid any mishandling
// of the temporary box in Fortran runtime.
base = builder.create<fir::BoxAddrOp>(loc, fir::boxMemRefType(tempBoxType),
base);
ptrType = base.getType();
}
// Create the temporary using dynamic type of the original,
// if it is polymorphic, or it has a derived type with SEQUENCE
// or BIND attribute (such dummy arguments may have their dynamic
// type not exactly matching their static type).
// Note that for the latter case, the allocation can still be done
// without the mold, because the dynamic and static types
// must be storage compatible.
bool useDynamicType = fir::isBoxedRecordType(origBox.getType()) ||
fir::isPolymorphicType(origBox.getType());
mlir::Type tempBoxType =
fir::wrapInClassOrBoxType(fir::unwrapRefType(ptrType),
/*isPolymorphic=*/useDynamicType);
// Use the shape with proper lower bounds for the final box.
shape = builder.genShape(loc, lbounds, extents);
mlir::Value newBox =
builder.createBox(loc, tempBoxType, base, shape, /*slice=*/nullptr,
typeParams, useDynamicType ? origBox : nullptr);
// The new box might be !fir.class, while the original might be
// !fir.box - we have to add a conversion.
return builder.createConvert(loc, origBox.getType(), newBox);
}
mlir::FailureOr<mlir::Value>
PackArrayConversion::genRepackedBox(fir::FirOpBuilder &builder,
mlir::Location loc, fir::PackArrayOp op) {
mlir::OpBuilder::InsertionGuard guard(builder);
mlir::Value box = op.getArray();
llvm::SmallVector<mlir::Value> typeParams(op.getTypeparams().begin(),
op.getTypeparams().end());
auto boxType = mlir::cast<fir::BaseBoxType>(box.getType());
mlir::Type indexType = builder.getIndexType();
// If type parameters are not specified by fir.pack_array,
// figure out how many of them we need to read from the box.
unsigned numTypeParams = 0;
if (typeParams.size() == 0) {
if (auto recordType =
mlir::dyn_cast<fir::RecordType>(boxType.unwrapInnerType()))
if (recordType.getNumLenParams() != 0)
TODO(loc,
"allocating temporary for a parameterized derived type array");
if (auto charType =
mlir::dyn_cast<fir::CharacterType>(boxType.unwrapInnerType())) {
if (charType.hasDynamicLen()) {
// Read one length parameter from the box.
numTypeParams = 1;
} else {
// Place the constant length into typeParams.
mlir::Value length =
builder.createIntegerConstant(loc, indexType, charType.getLen());
typeParams.push_back(length);
}
}
}
// Create a temporay iff the original is not contigous and is not empty.
auto isNotContiguous = builder.genNot(
loc, builder.create<fir::IsContiguousBoxOp>(loc, box, op.getInnermost()));
auto dataAddr =
builder.create<fir::BoxAddrOp>(loc, fir::boxMemRefType(boxType), box);
auto isNotEmpty =
builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), dataAddr);
auto doPack =
builder.create<mlir::arith::AndIOp>(loc, isNotContiguous, isNotEmpty);
fir::IfOp ifOp =
builder.create<fir::IfOp>(loc, boxType, doPack, /*withElseRegion=*/true);
// Assume that the repacking is unlikely.
ifOp.setUnlikelyIfWeights();
// Return original box.
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
builder.create<fir::ResultOp>(loc, box);
// Create a new box.
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
// Get lower bounds and extents from the box.
llvm::SmallVector<mlir::Value, Fortran::common::maxRank> lbounds, extents;
fir::factory::genDimInfoFromBox(builder, loc, box, &lbounds, &extents,
/*strides=*/nullptr);
// Get the type parameters from the box, if needed.
llvm::SmallVector<mlir::Value> assumedTypeParams;
if (numTypeParams != 0) {
if (auto charType =
mlir::dyn_cast<fir::CharacterType>(boxType.unwrapInnerType()))
if (charType.hasDynamicLen()) {
fir::factory::CharacterExprHelper charHelper(builder, loc);
mlir::Value len = charHelper.readLengthFromBox(box, charType);
typeParams.push_back(builder.createConvert(loc, indexType, len));
}
if (numTypeParams != typeParams.size())
return emitError(loc) << "failed to compute the type parameters for "
<< op.getOperation() << '\n';
}
mlir::Value tempBox = allocateTempBuffer(builder, loc, op.getStack(), box,
lbounds, extents, typeParams);
if (!op.getNoCopy())
fir::runtime::genShallowCopy(builder, loc, tempBox, box,
/*resultIsAllocated=*/true);
builder.create<fir::ResultOp>(loc, tempBox);
return ifOp.getResult(0);
}
mlir::LogicalResult
UnpackArrayConversion::matchAndRewrite(fir::UnpackArrayOp op,
mlir::PatternRewriter &rewriter) const {
// If repacking is not safe, then just remove the operation.
if (!repackIsSafe(op)) {
rewriter.eraseOp(op);
return mlir::success();
}
mlir::Location loc = op.getLoc();
fir::FirOpBuilder builder(rewriter, op.getOperation());
mlir::Type predicateType = builder.getI1Type();
mlir::Value tempBox = op.getTemp();
mlir::Value originalBox = op.getOriginal();
// For now we have to always check if the box is present.
auto isPresent =
builder.create<fir::IsPresentOp>(loc, predicateType, originalBox);
builder.genIfThen(loc, isPresent).genThen([&]() {
mlir::Type addrType =
fir::HeapType::get(fir::extractSequenceType(tempBox.getType()));
mlir::Value tempAddr =
builder.create<fir::BoxAddrOp>(loc, addrType, tempBox);
mlir::Value originalAddr =
builder.create<fir::BoxAddrOp>(loc, addrType, originalBox);
auto isNotSame = builder.genPtrCompare(loc, mlir::arith::CmpIPredicate::ne,
tempAddr, originalAddr);
builder.genIfThen(loc, isNotSame)
.genThen([&]() {
// Copy from temporary to the original.
if (!op.getNoCopy())
fir::runtime::genShallowCopy(builder, loc, originalBox, tempBox,
/*resultIsAllocated=*/true);
// Deallocate, if it was allocated in heap.
// Note that the stack attribute does not always mean
// that the allocation was actually done in stack memory.
// There are currently cases where we delegate the allocation
// to the runtime that uses heap memory, even when the stack
// attribute is set on fir.pack_array.
if (!op.getStack() || !canAllocateTempOnStack(originalBox))
builder.create<fir::FreeMemOp>(loc, tempAddr);
})
.getIfOp()
.setUnlikelyIfWeights();
});
rewriter.eraseOp(op);
return mlir::success();
}
namespace {
class LowerRepackArraysPass
: public fir::impl::LowerRepackArraysPassBase<LowerRepackArraysPass> {
public:
using LowerRepackArraysPassBase<
LowerRepackArraysPass>::LowerRepackArraysPassBase;
void runOnOperation() override final {
auto *context = &getContext();
mlir::ModuleOp module = getOperation();
mlir::RewritePatternSet patterns(context);
patterns.insert<PackArrayConversion>(context);
patterns.insert<UnpackArrayConversion>(context);
mlir::GreedyRewriteConfig config;
config.setRegionSimplificationLevel(
mlir::GreedySimplifyRegionLevel::Disabled);
(void)applyPatternsGreedily(module, std::move(patterns), config);
}
void getDependentDialects(mlir::DialectRegistry ®istry) const override {
fir::acc::registerTransformationalAttrsDependentDialects(registry);
fir::omp::registerTransformationalAttrsDependentDialects(registry);
}
};
} // anonymous namespace
|