Skip to content

Commit e52c941

Browse files
authored
[mlir][sparse] minor cleanup of transform/utils (llvm#75396)
Consistent include macro naming Modified and added comments
1 parent f0d4811 commit e52c941

File tree

6 files changed

+50
-35
lines changed

6 files changed

+50
-35
lines changed

mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenEnv.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_CODEGENENV_H_
14-
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_CODEGENENV_H_
13+
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENENV_H_
14+
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENENV_H_
1515

1616
#include "CodegenUtils.h"
1717
#include "LoopEmitter.h"
@@ -206,4 +206,4 @@ class CodegenEnv {
206206
} // namespace sparse_tensor
207207
} // namespace mlir
208208

209-
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_CODEGENENV_H_
209+
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENENV_H_

mlir/lib/Dialect/SparseTensor/Transforms/Utils/CodegenUtils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_CODEGENUTILS_H_
14-
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_CODEGENUTILS_H_
13+
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENUTILS_H_
14+
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENUTILS_H_
1515

1616
#include "mlir/Dialect/Arith/IR/Arith.h"
1717
#include "mlir/Dialect/Complex/IR/Complex.h"
@@ -434,4 +434,4 @@ inline bool isZeroRankedTensorOrScalar(Type type) {
434434
} // namespace sparse_tensor
435435
} // namespace mlir
436436

437-
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_CODEGENUTILS_H_
437+
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_CODEGENUTILS_H_

mlir/lib/Dialect/SparseTensor/Transforms/Utils/IterationGraphSorter.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- LoopScheduler.cpp -------------------------------------------------===//
1+
//===- IterationGraphSorter.cpp -------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -20,11 +20,10 @@ using namespace mlir::sparse_tensor;
2020

2121
namespace {
2222

23-
/// A helper class that visits an affine expression and tries to find an
24-
/// AffineDimExpr to which the corresponding iterator from a GenericOp matches
25-
/// the desired iterator type.
26-
/// If there is no matched iterator type, returns the first DimExpr in the
27-
/// expression.
23+
/// A helper class that visits an affine expression and tries to find
24+
/// an AffineDimExpr to which the corresponding iterator from a GenericOp
25+
/// matches the desired iterator type. If there is no matched iterator
26+
/// type, the method returns the first DimExpr in the expression.
2827
class AffineDimFinder : public AffineExprVisitor<AffineDimFinder> {
2928
public:
3029
explicit AffineDimFinder(ArrayRef<utils::IteratorType> itTypes)
@@ -81,11 +80,9 @@ inline static bool includesDenseOutput(SortMask mask) {
8180
return includesAny(mask, SortMask::kIncludeDenseOutput);
8281
}
8382

84-
/// A helper to compute a topological sort. O(n^2) time complexity
85-
/// as we use adj matrix for the graph.
86-
/// The sorted result will put the first Reduction iterator to the
87-
/// latest possible position.
8883
AffineMap IterationGraphSorter::topoSort() {
84+
// The sorted result will put the first Reduction iterator to the
85+
// latest possible position.
8986
std::vector<unsigned> redIt; // reduce iterator with 0 degree
9087
std::vector<unsigned> parIt; // parallel iterator with 0 degree
9188
const unsigned numLoops = getNumLoops();
@@ -170,6 +167,7 @@ AffineMap IterationGraphSorter::sort(SortMask mask, Value ignored) {
170167
// Reset the interation graph.
171168
for (auto &row : itGraph)
172169
std::fill(row.begin(), row.end(), false);
170+
173171
// Reset cached in-degree.
174172
std::fill(inDegree.begin(), inDegree.end(), 0);
175173

@@ -179,7 +177,6 @@ AffineMap IterationGraphSorter::sort(SortMask mask, Value ignored) {
179177
// Skip dense inputs when not requested.
180178
if ((!enc && !includesDenseInput(mask)) || in == ignored)
181179
continue;
182-
183180
addConstraints(in, map);
184181
}
185182

mlir/lib/Dialect/SparseTensor/Transforms/Utils/IterationGraphSorter.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
//===- LoopScheduler.h -----------------------------------------*- C++ -*-===//
1+
//===- IterationGraphSorter.h -----------------------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// This header file defines the iteration graph sorter (top-sort scheduling).
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_ITERATIONGRAPHSORTER_H_
14+
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_ITERATIONGRAPHSORTER_H_
815

916
#include "mlir/IR/AffineMap.h"
1017

@@ -21,7 +28,7 @@ class GenericOp;
2128

2229
namespace sparse_tensor {
2330

24-
/// Iteration graph sorting.
31+
/// Iteration graph sorting mask,
2532
enum class SortMask : unsigned {
2633
// The individual mask bits.
2734
kIncludeDenseOutput = 0x1, // b001
@@ -34,40 +41,52 @@ enum class SortMask : unsigned {
3441

3542
class IterationGraphSorter {
3643
public:
37-
// Constructs a scheduler from linalg.generic
38-
// Maybe reuses the class to schedule foreach as well (to address
39-
// non-permutation, e.g, traverse CSR in BSR order).
44+
/// Factory method that construct an iteration graph sorter
45+
/// for the given linalg.generic operation.
4046
static IterationGraphSorter fromGenericOp(linalg::GenericOp genericOp);
4147

42-
// Returns a permutation that represents the scheduled loop order.
43-
// Note that the returned AffineMap could be null if the kernel can not be
44-
// schedule due to cycles in the iteration graph.
48+
/// Returns a permutation that represents the scheduled loop order.
49+
/// Note that the returned AffineMap could be null if the kernel
50+
/// cannot be scheduled due to cyclic iteration graph.
4551
[[nodiscard]] AffineMap sort(SortMask mask, Value ignored = nullptr);
52+
53+
/// Returns the number of loops in the iteration graph.
4654
unsigned getNumLoops() const { return loop2OutLvl.getNumDims(); }
4755

4856
private:
57+
// Private constructor.
4958
IterationGraphSorter(SmallVector<Value> &&ins,
5059
SmallVector<AffineMap> &&loop2InsLvl, Value out,
5160
AffineMap loop2OutLvl,
5261
SmallVector<utils::IteratorType> &&iterTypes);
5362

63+
// Adds all the constraints in the given loop to level map.
5464
void addConstraints(Value t, AffineMap loop2LvlMap);
65+
66+
/// A helper to compute a topological sort. The method has an
67+
/// O(n^2) time complexity since we use an adjacency matrix
68+
/// representation for the iteration graph.
5569
AffineMap topoSort();
5670

5771
// Input tensors and associated loop to level maps.
5872
SmallVector<Value> ins;
5973
SmallVector<AffineMap> loop2InsLvl;
74+
6075
// Output tensor and associated loop to level map.
6176
Value out;
6277
AffineMap loop2OutLvl;
63-
// Loop type;
78+
79+
// Loop itation types;
6480
SmallVector<utils::IteratorType> iterTypes;
6581

6682
// Adjacent matrix that represents the iteration graph.
6783
std::vector<std::vector<bool>> itGraph;
84+
6885
// InDegree used for topo sort.
6986
std::vector<unsigned> inDegree;
7087
};
7188

7289
} // namespace sparse_tensor
7390
} // namespace mlir
91+
92+
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_ITERATIONGRAPHSORTER_H_

mlir/lib/Dialect/SparseTensor/Transforms/Utils/LoopEmitter.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_SPARSETENSORLOOPEMITTER_H_
10-
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_SPARSETENSORLOOPEMITTER_H_
9+
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_LOOPEMITTER_H_
10+
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_LOOPEMITTER_H_
1111

1212
#include <vector>
1313

@@ -22,7 +22,7 @@ namespace sparse_tensor {
2222
// A compressed <tensor id, level> pair.
2323
using TensorLevel = unsigned;
2424

25-
//===----------------------------------------------------------------------===//
25+
//
2626
// SparseTensorLoopEmiter class, manages sparse tensors and helps to
2727
// generate loop structure to (co)-iterate sparse tensors.
2828
//
@@ -48,8 +48,7 @@ using TensorLevel = unsigned;
4848
// loopEmiter.exitCurrentLoop(); // exit k
4949
// loopEmiter.exitCurrentLoop(); // exit j
5050
// loopEmiter.exitCurrentLoop(); // exit i
51-
//===----------------------------------------------------------------------===//
52-
51+
//
5352
class LoopEmitter {
5453
public:
5554
/// Optional callback function to setup dense output tensors when
@@ -705,4 +704,4 @@ class LoopEmitter {
705704
} // namespace sparse_tensor
706705
} // namespace mlir
707706

708-
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_SPARSETENSORLOOPEMITTER_H_
707+
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_LOOPEMITTER_H_

mlir/lib/Dialect/SparseTensor/Transforms/Utils/SparseTensorDescriptor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_SPARSETENSORDESCRIPTOR_H_
14-
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_SPARSETENSORDESCRIPTOR_H_
13+
#ifndef MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_SPARSETENSORDESCRIPTOR_H_
14+
#define MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_SPARSETENSORDESCRIPTOR_H_
1515

1616
#include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
1717
#include "mlir/Dialect/SparseTensor/IR/SparseTensorStorageLayout.h"
@@ -262,4 +262,4 @@ getMutDescriptorFromTensorTuple(Value tensor, SmallVectorImpl<Value> &fields) {
262262
} // namespace sparse_tensor
263263
} // namespace mlir
264264

265-
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_SPARSETENSODESCRIPTOR_H_
265+
#endif // MLIR_DIALECT_SPARSETENSOR_TRANSFORMS_UTILS_SPARSETENSODESCRIPTOR_H_

0 commit comments

Comments
 (0)