Skip to content

Commit 153551a

Browse files
author
Peiming Liu
committed
[mlir][sparse] support type conversion from AoS COO to memrefs.
1 parent d2942a8 commit 153551a

File tree

4 files changed

+99
-9
lines changed

4 files changed

+99
-9
lines changed

mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ struct LevelType {
303303
}
304304

305305
/// Check if the `LevelType` is in the `LevelFormat`.
306-
template <LevelFormat fmt>
306+
template <LevelFormat... fmt>
307307
constexpr bool isa() const {
308-
return getLvlFmt() == fmt;
308+
return (... || (getLvlFmt() == fmt)) || false;
309309
}
310310

311311
/// Check if the `LevelType` has the properties

mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorType.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
namespace mlir {
1919
namespace sparse_tensor {
2020

21+
/// A simple structure that encodes a range of levels in the sparse tensors that
22+
/// forms a COO segment.
23+
struct COOSegment {
24+
std::pair<Level /*low*/, Level /*high*/> lvlRange;
25+
bool isSoA;
26+
27+
bool inSegment(Level l) const {
28+
return l >= lvlRange.first && l < lvlRange.second;
29+
}
30+
};
31+
2132
//===----------------------------------------------------------------------===//
2233
/// A wrapper around `RankedTensorType`, which has three goals:
2334
///
@@ -330,6 +341,9 @@ class SparseTensorType {
330341
/// Returns [un]ordered COO type for this sparse tensor type.
331342
RankedTensorType getCOOType(bool ordered) const;
332343

344+
/// Returns a list of COO segments in the sparse tensor types.
345+
SmallVector<COOSegment> getCOOSegments() const;
346+
333347
private:
334348
// These two must be const, to ensure coherence of the memoized fields.
335349
const RankedTensorType rtp;

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ void StorageLayout::foreachField(
7474
callback) const {
7575
const auto lvlTypes = enc.getLvlTypes();
7676
const Level lvlRank = enc.getLvlRank();
77-
const Level cooStart = SparseTensorType(enc).getCOOStart();
78-
const Level end = cooStart == lvlRank ? cooStart : cooStart + 1;
77+
SmallVector<COOSegment> cooSegs = SparseTensorType(enc).getCOOSegments();
7978
FieldIndex fieldIdx = kDataFieldStartingIdx;
79+
80+
Level l = 0;
81+
ArrayRef cooSegsRef = cooSegs;
8082
// Per-level storage.
81-
for (Level l = 0; l < end; l++) {
83+
while (l < lvlRank) {
8284
const auto lt = lvlTypes[l];
8385
if (isWithPosLT(lt)) {
8486
if (!(callback(fieldIdx++, SparseTensorFieldKind::PosMemRef, l, lt)))
@@ -88,6 +90,21 @@ void StorageLayout::foreachField(
8890
if (!(callback(fieldIdx++, SparseTensorFieldKind::CrdMemRef, l, lt)))
8991
return;
9092
}
93+
if (!cooSegsRef.empty() && cooSegsRef.front().inSegment(l)) {
94+
if (!cooSegsRef.front().isSoA) {
95+
// AoS COO, all singletons are fused into one memrefs. Skips the entire
96+
// COO segement.
97+
l = cooSegsRef.front().lvlRange.second;
98+
} else {
99+
// SoA COO, each singleton level has one memref.
100+
l++;
101+
}
102+
// Expire handled COO segment.
103+
cooSegsRef = cooSegsRef.drop_front();
104+
} else {
105+
// Non COO levels.
106+
l++;
107+
}
91108
}
92109
// The values array.
93110
if (!(callback(fieldIdx++, SparseTensorFieldKind::ValMemRef, kInvalidLevel,
@@ -796,13 +813,46 @@ bool mlir::sparse_tensor::SparseTensorType::isCOOType(Level startLvl,
796813
}
797814

798815
Level mlir::sparse_tensor::SparseTensorType::getCOOStart() const {
799-
if (hasEncoding() && lvlRank > 1)
800-
for (Level l = 0; l < lvlRank - 1; l++)
801-
if (isCOOType(l, /*isUnique=*/false))
802-
return l;
816+
SmallVector<COOSegment> coo = getCOOSegments();
817+
if (!coo.empty()) {
818+
assert(coo.size() == 1);
819+
return coo.front().lvlRange.first;
820+
}
803821
return lvlRank;
804822
}
805823

824+
SmallVector<COOSegment>
825+
mlir::sparse_tensor::SparseTensorType::getCOOSegments() const {
826+
SmallVector<COOSegment> ret;
827+
if (!hasEncoding() || lvlRank <= 1)
828+
return ret;
829+
830+
ArrayRef<LevelType> lts = getLvlTypes();
831+
Level l = 0;
832+
while (l < lvlRank) {
833+
auto lt = lts[l];
834+
if (lt.isa<LevelFormat::Compressed, LevelFormat::LooseCompressed>()) {
835+
auto cur = lts.begin() + l;
836+
auto end = std::find_if(cur + 1, lts.end(), [](LevelType lt) {
837+
return !lt.isa<LevelFormat::Singleton>();
838+
});
839+
unsigned cooLen = std::distance(cur, end);
840+
if (cooLen > 1) {
841+
// To support mixed SoA/AoS COO, we should break the segment when the
842+
// storage scheme changes, for now we faithfully assume that all
843+
// consecutive singleton levels have the same storage format as verified
844+
// STEA.
845+
ret.push_back(COOSegment{std::make_pair(l, l + cooLen),
846+
lts[l + 1].isa<LevelPropNonDefault::SoA>()});
847+
}
848+
l += cooLen;
849+
} else {
850+
l++;
851+
}
852+
}
853+
return ret;
854+
}
855+
806856
RankedTensorType
807857
mlir::sparse_tensor::SparseTensorType::getCOOType(bool ordered) const {
808858
SmallVector<LevelType> lvlTypes;

mlir/test/Dialect/SparseTensor/codegen.mlir

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton)
4949
}>
5050

51+
#SoACOO = #sparse_tensor.encoding<{
52+
map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton(soa))
53+
}>
54+
5155
#CooPNo = #sparse_tensor.encoding<{
5256
map = (d0, d1) -> (d1 : compressed(nonunique), d0 : singleton(nonordered))
5357
}>
@@ -67,6 +71,28 @@ func.func @sparse_nop(%arg0: tensor<?xf64, #SparseVector>) -> tensor<?xf64, #Spa
6771
return %arg0 : tensor<?xf64, #SparseVector>
6872
}
6973

74+
// CHECK-LABEL: func @sparse_nop_aos_coo(
75+
// CHECK-SAME: %[[POS:.*0]]: memref<?xindex>,
76+
// CHECK-SAME: %[[AoS_CRD:.*1]]: memref<?xindex>,
77+
// CHECK-SAME: %[[VAL:.*]]: memref<?xf64>,
78+
// CHECK-SAME: %[[A3:.*]]: !sparse_tensor.storage_specifier
79+
// CHECK: return %[[POS]], %[[AoS_CRD]], %[[VAL]], %[[A3]]
80+
func.func @sparse_nop_aos_coo(%arg0: tensor<?x?xf64, #Coo>) -> tensor<?x?xf64, #Coo> {
81+
return %arg0 : tensor<?x?xf64, #Coo>
82+
}
83+
84+
// CHECK-LABEL: func @sparse_nop_soa_coo(
85+
// CHECK-SAME: %[[POS:.*0]]: memref<?xindex>,
86+
// CHECK-SAME: %[[SoA_CRD_0:.*1]]: memref<?xindex>,
87+
// CHECK-SAME: %[[SoA_CRD_1:.*2]]: memref<?xindex>,
88+
// CHECK-SAME: %[[VAL:.*]]: memref<?xf64>,
89+
// CHECK-SAME: %[[A3:.*]]: !sparse_tensor.storage_specifier
90+
// CHECK: return %[[POS]], %[[SoA_CRD_0]], %[[SoA_CRD_1]], %[[VAL]], %[[A3]]
91+
func.func @sparse_nop_soa_coo(%arg0: tensor<?x?xf64, #SoACOO>) -> tensor<?x?xf64, #SoACOO> {
92+
return %arg0 : tensor<?x?xf64, #SoACOO>
93+
}
94+
95+
7096
// CHECK-LABEL: func @sparse_nop_multi_ret(
7197
// CHECK-SAME: %[[A0:.*0]]: memref<?xi32>,
7298
// CHECK-SAME: %[[A1:.*1]]: memref<?xi64>,

0 commit comments

Comments
 (0)