Skip to content

Commit 46839d2

Browse files
authored
improving debug information and adding slice to populate index in affine promotion (#409)
1 parent 214ac4b commit 46839d2

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

flang/lib/Optimizer/Transforms/AffineDemotion.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class ConvertConversion : public mlir::OpRewritePattern<fir::ConvertOp> {
7777
matchAndRewrite(fir::ConvertOp op,
7878
mlir::PatternRewriter &rewriter) const override {
7979
if (op.res().getType().isa<mlir::MemRefType>()) {
80+
// due to index calculation moving to affine maps we still need to
81+
// add converts for sequence types this has a side effect of losing
82+
// some information about arrays with known dimensions by creating:
83+
// fir.convert %arg0 : (!fir.ref<!fir.array<5xi32>>) -> !fir.ref<!fir.array<?xi32>>
8084
if (auto refTy = op.value().getType().dyn_cast<fir::ReferenceType>())
8185
if (auto arrTy = refTy.getEleTy().dyn_cast<fir::SequenceType>()) {
8286
fir::SequenceType::Shape flatShape = {fir::SequenceType::getUnknownExtent()};

flang/lib/Optimizer/Transforms/AffinePromotion.cpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,20 @@ class AffineLoopAnalysis {
5151
return analyzeMemoryAccess(loopOperation) &&
5252
analyzeBody(loopOperation, functionAnalysis);
5353
}
54-
bool analyzeReference(mlir::Value);
54+
bool analyzeReference(mlir::Value, mlir::Operation *);
5555
bool analyzeMemoryAccess(fir::DoLoopOp loopOperation) {
5656
for (auto loadOp : loopOperation.getOps<fir::LoadOp>())
57-
if (!analyzeReference(loadOp.memref()))
57+
if (!analyzeReference(loadOp.memref(), loadOp))
5858
return false;
5959
for (auto storeOp : loopOperation.getOps<fir::StoreOp>())
60-
if (!analyzeReference(storeOp.memref()))
60+
if (!analyzeReference(storeOp.memref(), storeOp))
6161
return false;
6262
return true;
6363
}
6464
};
6565

6666
/// Calculates arguments for creating an IntegerSet symCount, dimCount are the
67-
/// final number of symbols and dimensions of the affine map. If integer set if
67+
/// final number of symbols and dimensions of the affine map. Integer set if
6868
/// possible is in Optional IntegerSet
6969
class AffineIfCondition {
7070
public:
@@ -213,37 +213,43 @@ class AffineFunctionAnalysis {
213213
llvm::DenseMap<mlir::Operation *, AffineIfAnalysis> ifAnalysisMap;
214214
};
215215

216-
bool analyzeCoordinate(mlir::Value coordinate) {
216+
bool analyzeCoordinate(mlir::Value coordinate, mlir::Operation *op) {
217217
if (auto blockArg = coordinate.dyn_cast<mlir::BlockArgument>()) {
218218
if (isa<fir::DoLoopOp>(blockArg.getOwner()->getParentOp())) {
219219
return true;
220220
} else {
221-
llvm::dbgs() << "AffineLoopAnalysis: array coordinate is not a "
222-
"loop induction variable (owner not loopOp)\n";
221+
LLVM_DEBUG(llvm::dbgs()
222+
<< "AffineLoopAnalysis: array coordinate is not a "
223+
"loop induction variable (owner not loopOp)\n";
224+
op->dump(););
223225
return false;
224226
}
225227
} else {
226-
llvm::dbgs() << "AffineLoopAnalysis: array coordinate is not a loop "
227-
"induction variable (not a block argument)\n";
228+
LLVM_DEBUG(llvm::dbgs()
229+
<< "AffineLoopAnalysis: array coordinate is not a loop "
230+
"induction variable (not a block argument)\n";
231+
op->dump(); coordinate.getDefiningOp()->dump(););
228232
return false;
229233
}
230234
}
231-
bool AffineLoopAnalysis::analyzeReference(mlir::Value memref) {
235+
bool AffineLoopAnalysis::analyzeReference(mlir::Value memref,
236+
mlir::Operation *op) {
232237
if (auto acoOp = memref.getDefiningOp<ArrayCoorOp>()) {
233238
bool canPromote = true;
234239
for (auto coordinate : acoOp.indices())
235-
canPromote = canPromote && analyzeCoordinate(coordinate);
240+
canPromote = canPromote && analyzeCoordinate(coordinate, op);
236241
return canPromote;
237242
}
238243
if (auto coOp = memref.getDefiningOp<CoordinateOp>()) {
239244
LLVM_DEBUG(llvm::dbgs() << "AffineLoopAnalysis: cannot promote loop, "
240-
"array uses non ArrayCoorOp\n";
241-
coOp.dump(););
245+
"array memory operation uses non ArrayCoorOp\n";
246+
op->dump(); coOp.dump(););
242247

243248
return false;
244249
}
245250
LLVM_DEBUG(llvm::dbgs() << "AffineLoopAnalysis: unknown type of memory "
246-
"reference for array load\n";);
251+
"reference for array load\n";
252+
op->dump(););
247253
return false;
248254
}
249255

@@ -311,38 +317,48 @@ mlir::Type coordinateArrayElement(fir::ArrayCoorOp op) {
311317
void populateIndexArgs(fir::ArrayCoorOp acoOp, fir::ShapeOp shape,
312318
SmallVectorImpl<mlir::Value> &indexArgs,
313319
mlir::PatternRewriter &rewriter) {
314-
auto iter = shape.extents().begin();
315320
auto one = rewriter.create<mlir::ConstantOp>(
316321
acoOp.getLoc(), rewriter.getIndexType(), rewriter.getIndexAttr(1));
317-
auto end = shape.extents().size();
318-
for (decltype(end) i = 0; i < end; ++i) {
322+
auto extents = shape.extents();
323+
for (auto i = extents.begin(); i < extents.end(); i++) {
319324
indexArgs.push_back(one);
320-
indexArgs.push_back(*iter++);
325+
indexArgs.push_back(*i);
321326
indexArgs.push_back(one);
322327
}
323328
}
329+
324330
void populateIndexArgs(fir::ArrayCoorOp acoOp, fir::ShapeShiftOp shape,
325331
SmallVectorImpl<mlir::Value> &indexArgs,
326332
mlir::PatternRewriter &rewriter) {
327-
auto iter = shape.pairs().begin();
328333
auto one = rewriter.create<mlir::ConstantOp>(
329334
acoOp.getLoc(), rewriter.getIndexType(), rewriter.getIndexAttr(1));
330-
auto end = shape.pairs().size();
331-
for (decltype(end) i = 0; i < end; ++(++i)) {
332-
indexArgs.push_back(*iter++);
333-
indexArgs.push_back(*iter++);
335+
auto extents = shape.pairs();
336+
for (auto i = extents.begin(); i < extents.end();) {
337+
indexArgs.push_back(*i++);
338+
indexArgs.push_back(*i++);
334339
indexArgs.push_back(one);
335340
}
336341
}
342+
void populateIndexArgs(fir::ArrayCoorOp acoOp, fir::SliceOp slice,
343+
SmallVectorImpl<mlir::Value> &indexArgs,
344+
mlir::PatternRewriter &rewriter) {
345+
auto extents = slice.triples();
346+
for (auto i = extents.begin(); i < extents.end();) {
347+
indexArgs.push_back(*i++);
348+
indexArgs.push_back(*i++);
349+
indexArgs.push_back(*i++);
350+
}
351+
}
352+
337353
void populateIndexArgs(fir::ArrayCoorOp acoOp,
338354
SmallVectorImpl<mlir::Value> &indexArgs,
339355
mlir::PatternRewriter &rewriter) {
340-
if (auto shape = acoOp.shape().getDefiningOp<ShapeOp>()) {
356+
if (auto shape = acoOp.shape().getDefiningOp<ShapeOp>())
341357
return populateIndexArgs(acoOp, shape, indexArgs, rewriter);
342-
}
343358
if (auto shapeShift = acoOp.shape().getDefiningOp<ShapeShiftOp>())
344359
return populateIndexArgs(acoOp, shapeShift, indexArgs, rewriter);
345-
llvm::dbgs() << "AffinePromotion: need to populateIndexArgs for slice\n";
360+
if (auto slice = acoOp.shape().getDefiningOp<SliceOp>())
361+
return populateIndexArgs(acoOp, slice, indexArgs, rewriter);
346362
return;
347363
}
348364

flang/test/Fir/arr-end-end.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! Test affine pipeline
22
! RUN: bbc --emit-fir --gen-array-coor=true %s -o - | tco --flang-memref-dataflow-opt --fir-loop-result-opt --canonicalize --loop-invariant-code-motion --promote-to-affine --affine-loop-invariant-code-motion --simplify-affine-structures --memref-dataflow-opt --cse --demote-affine --lower-affine | tco | llc | as -o %t
3-
! RUN: cc %t %S/arr-driver.c
3+
! RUN: %CC -std=c99 %t %S/arr-driver.c
44
! RUN: ./a.out | FileCheck %s
55

66
! CHECK: f1dc: success

0 commit comments

Comments
 (0)