Skip to content

Commit 21147e7

Browse files
[mlir][acc] Add loop tiling utilities for OpenACC (llvm#171490)
Add utilities in OpenACCUtilsTiling.h/.cpp to support tiling transformations on acc.loop operations: - uncollapseLoops: Expand collapsed loops with multiple IVs into nested loop structures when tile count exceeds collapse count - tileACCLoops: Transform loop nests into tile and element loops based on provided tile sizes, with automatic resolution of unknown tile sizes (tile(*) represented as -1) These utilities prepare for the ACCLoopTiling pass which handles the OpenACC loop tile directive. --------- Co-authored-by: Vijay Kandiah <vkandiah@nvidia.com>
1 parent 29760ce commit 21147e7

File tree

5 files changed

+746
-0
lines changed

5 files changed

+746
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===- OpenACCUtilsTiling.h - OpenACC Loop Tiling Utilities -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains utility functions for tiling OpenACC loops.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_OPENACC_OPENACCUTILSTILING_H_
14+
#define MLIR_DIALECT_OPENACC_OPENACCUTILSTILING_H_
15+
16+
#include "mlir/Dialect/OpenACC/OpenACC.h"
17+
#include "mlir/IR/PatternMatch.h"
18+
#include "llvm/ADT/SmallVector.h"
19+
20+
namespace mlir {
21+
namespace acc {
22+
23+
/// Uncollapse tile loops with multiple IVs and collapseCount < tileCount.
24+
/// This is used to prepare loops for tiling when the collapse count is less
25+
/// than the tile count.
26+
///
27+
/// \param origLoop The original loop operation to uncollapse.
28+
/// \param tileCount The number of tile dimensions.
29+
/// \param collapseCount The collapse count from the original loop.
30+
/// \param rewriter The rewriter to use for modifications.
31+
/// \return A vector of uncollapsed loop operations.
32+
llvm::SmallVector<mlir::acc::LoopOp>
33+
uncollapseLoops(mlir::acc::LoopOp origLoop, unsigned tileCount,
34+
unsigned collapseCount, mlir::RewriterBase &rewriter);
35+
36+
/// Tile ACC loops according to the given tile sizes.
37+
///
38+
/// Tiling a 2-level nested loop will create two 'tile' loops containing two
39+
/// 'element' loops. The transformation looks like:
40+
///
41+
/// Before Tiling:
42+
/// \code
43+
/// #pragma acc loop tile(tile_size1, tile_size2)
44+
/// for (i = lb1; i < ub1; i += step1) { // original loop
45+
/// for (j = lb2; j < ub2; j += step2) {
46+
/// a[i,j] = i + j;
47+
/// }
48+
/// }
49+
/// \endcode
50+
///
51+
/// After Tiling:
52+
/// \code
53+
/// for (i = lb1; i < ub1; i += (step1 * tile_size1)) { // tile loop 1
54+
/// for (j = lb2; j < ub2; j += (step2 * tile_size2)) { // tile loop 2
55+
/// for (ii = i; ii < min(ub1, (step1 * tile_size1) + i); ii += step1) {
56+
/// // element loop 1
57+
/// for (jj = j; jj < min(ub2, (step2 * tile_size2) + j); jj += step2)
58+
/// { // element loop 2
59+
/// a[ii,jj] = i + j;
60+
/// }
61+
/// }
62+
/// }
63+
/// }
64+
/// \endcode
65+
///
66+
/// Unknown tile sizes (represented as -1 in acc dialect for `tile(*)`) are
67+
/// resolved to the provided default tile size.
68+
///
69+
/// \param tileLoops The loops to tile (outermost first).
70+
/// \param tileSizes The tile sizes for each dimension. Values of -1 are
71+
/// treated as unknown and resolved to defaultTileSize.
72+
/// \param defaultTileSize The default tile size to use for unknown (*) tiles.
73+
/// \param rewriter The rewriter to use for modifications.
74+
/// \return The outermost loop after tiling.
75+
mlir::acc::LoopOp tileACCLoops(llvm::SmallVector<mlir::acc::LoopOp> &tileLoops,
76+
const llvm::SmallVector<mlir::Value> &tileSizes,
77+
int32_t defaultTileSize,
78+
mlir::RewriterBase &rewriter);
79+
80+
} // namespace acc
81+
} // namespace mlir
82+
83+
#endif // MLIR_DIALECT_OPENACC_OPENACCUTILSTILING_H_

mlir/lib/Dialect/OpenACC/Utils/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_mlir_dialect_library(MLIROpenACCUtils
2+
OpenACCUtilsTiling.cpp
23
OpenACCUtils.cpp
34

45
ADDITIONAL_HEADER_DIRS
@@ -14,7 +15,9 @@ add_mlir_dialect_library(MLIROpenACCUtils
1415
MLIROpenACCTypeInterfacesIncGen
1516

1617
LINK_LIBS PUBLIC
18+
MLIRArithDialect
1719
MLIROpenACCDialect
1820
MLIRIR
1921
MLIRSupport
22+
MLIRTransformUtils
2023
)

0 commit comments

Comments
 (0)