Skip to content

Commit da5fe23

Browse files
Djordje Todorovicdjolertrk
authored andcommitted
[mlir][LowerToAffineLoops] Handle tensors of rank 0
This will fix the case: $ toyc -emit=jit test.toy $ cat test.toy def main() { var a = 1; print(a); } Without this patch it would trigger an assertion. Differential Revision: https://reviews.llvm.org/D77464
1 parent 6a800f6 commit da5fe23

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

mlir/examples/toy/Ch5/mlir/LowerToAffineLoops.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,15 @@ struct ConstantOpLowering : public OpRewritePattern<toy::ConstantOp> {
155155
// operations.
156156
auto valueShape = memRefType.getShape();
157157
SmallVector<Value, 8> constantIndices;
158-
for (auto i : llvm::seq<int64_t>(
159-
0, *std::max_element(valueShape.begin(), valueShape.end())))
160-
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, i));
158+
159+
if (!valueShape.empty()) {
160+
for (auto i : llvm::seq<int64_t>(
161+
0, *std::max_element(valueShape.begin(), valueShape.end())))
162+
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, i));
163+
} else {
164+
// This is the case of a tensor of rank 0.
165+
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, 0));
166+
}
161167

162168
// The constant operation represents a multi-dimensional constant, so we
163169
// will need to generate a store for each of the elements. The following

mlir/examples/toy/Ch6/mlir/LowerToAffineLoops.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,15 @@ struct ConstantOpLowering : public OpRewritePattern<toy::ConstantOp> {
155155
// operations.
156156
auto valueShape = memRefType.getShape();
157157
SmallVector<Value, 8> constantIndices;
158-
for (auto i : llvm::seq<int64_t>(
159-
0, *std::max_element(valueShape.begin(), valueShape.end())))
160-
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, i));
161158

159+
if (!valueShape.empty()) {
160+
for (auto i : llvm::seq<int64_t>(
161+
0, *std::max_element(valueShape.begin(), valueShape.end())))
162+
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, i));
163+
} else {
164+
// This is the case of a tensor of rank 0.
165+
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, 0));
166+
}
162167
// The constant operation represents a multi-dimensional constant, so we
163168
// will need to generate a store for each of the elements. The following
164169
// functor recursively walks the dimensions of the constant shape,

mlir/examples/toy/Ch7/mlir/LowerToAffineLoops.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,15 @@ struct ConstantOpLowering : public OpRewritePattern<toy::ConstantOp> {
155155
// operations.
156156
auto valueShape = memRefType.getShape();
157157
SmallVector<Value, 8> constantIndices;
158-
for (auto i : llvm::seq<int64_t>(
159-
0, *std::max_element(valueShape.begin(), valueShape.end())))
160-
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, i));
158+
159+
if (!valueShape.empty()) {
160+
for (auto i : llvm::seq<int64_t>(
161+
0, *std::max_element(valueShape.begin(), valueShape.end())))
162+
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, i));
163+
} else {
164+
// This is the case of a tensor of rank 0.
165+
constantIndices.push_back(rewriter.create<ConstantIndexOp>(loc, 0));
166+
}
161167

162168
// The constant operation represents a multi-dimensional constant, so we
163169
// will need to generate a store for each of the elements. The following

0 commit comments

Comments
 (0)