Skip to content

Commit f726c81

Browse files
authored
[tosa][NFC] fix ArrayRef lifetime issue for indexShap (#4224)
clang report warning for object lifetime issue indexShape is ArrayRef and makeShapeTorchCompatible will return SmallVector. The Vector will be destroyed before using indexShap. ```bash torch-mlir/lib/Conversion/TorchToTosa/TorchToTosa.cpp:4232:18: warning: object backing the pointer indexShape will be destroyed at the end of the full-expression [-Wdangling-assignment-gsl] 4232 | indexShape = makeShapeTorchCompatible({1}); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ```
1 parent 1ad9702 commit f726c81

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/Conversion/TorchToTosa/TorchToTosa.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4219,7 +4219,7 @@ LogicalResult ConvertAtenOp<AtenIndexSelectOp>::matchAndRewrite(
42194219

42204220
auto index = adaptor.getIndex();
42214221
auto indexType = dyn_cast<RankedTensorType>(index.getType());
4222-
auto indexShape = indexType.getShape();
4222+
ArrayRef<int64_t> indexShape = indexType.getShape();
42234223

42244224
if (!indexType)
42254225
return rewriter.notifyMatchFailure(
@@ -4228,8 +4228,12 @@ LogicalResult ConvertAtenOp<AtenIndexSelectOp>::matchAndRewrite(
42284228
auto inputShape = inputType.getShape();
42294229
int inputRank = inputType.getRank();
42304230

4231+
// indexShape is reference. storing actual data in SmallVector to avoid
4232+
// use-after-free
4233+
SmallVector<int64_t> indexShapeTorchCompatible{};
42314234
if (indexType.getRank() == 0) {
4232-
indexShape = makeShapeTorchCompatible({1});
4235+
indexShapeTorchCompatible = makeShapeTorchCompatible({1});
4236+
indexShape = indexShapeTorchCompatible;
42334237
index = rewriter.create<tosa::ReshapeOp>(
42344238
op->getLoc(),
42354239
RankedTensorType::get(indexShape, indexType.getElementType()), index,

0 commit comments

Comments
 (0)