Skip to content

Commit f45f4ae

Browse files
authored
Avoid unnecessary erasing of constant Locs (#151573)
Do not erase location info when moving an op within the same block. Since #75415 , the FoldUtils.cpp erases the location information when moving an operation. This was being done even when an operation was moved to the front of a block it was already in. In TFLite, this location information is used to provide meaningful names for tensors, which aids in debugging and mapping compiled tensors back to their original layers. The aggressive erasure of location info caused many tensors in TFLite models to receive generic names (e.g., tfl.pseudo_qconst), making the models harder to inspect. This change modifies the logic to preserve the location of an operation when it is moved within the same block. The location is now only erased when the operation is moved from a different block entirely. This ensures that most tensor names are preserved, improving the debugging experience for TFLite models.
1 parent ed294c2 commit f45f4ae

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

mlir/lib/Transforms/Utils/FoldUtils.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,14 @@ bool OperationFolder::insertKnownConstant(Operation *op, Attribute constValue) {
154154
// already at the front of the block, or the previous operation is already a
155155
// constant we unique'd (i.e. one we inserted), then we don't need to do
156156
// anything. Otherwise, we move the constant to the insertion block.
157+
// The location info is erased if the constant is moved to a different block.
157158
Block *insertBlock = &insertRegion->front();
158-
if (opBlock != insertBlock || (&insertBlock->front() != op &&
159-
!isFolderOwnedConstant(op->getPrevNode()))) {
159+
if (opBlock != insertBlock) {
160160
op->moveBefore(&insertBlock->front());
161161
op->setLoc(erasedFoldedLocation);
162+
} else if (&insertBlock->front() != op &&
163+
!isFolderOwnedConstant(op->getPrevNode())) {
164+
op->moveBefore(&insertBlock->front());
162165
}
163166

164167
folderConstOp = op;

mlir/test/Transforms/canonicalize-debuginfo.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func.func @merge_constants() -> (index, index, index, index) {
1515

1616
// CHECK-LABEL: func @simple_hoist
1717
func.func @simple_hoist(%arg0: memref<8xi32>) -> i32 {
18-
// CHECK: arith.constant 88 : i32 loc(#[[UnknownLoc:.*]])
18+
// CHECK: arith.constant 88 : i32 loc(#[[ConstLoc2:.*]])
1919
// CHECK: arith.constant 42 : i32 loc(#[[ConstLoc0:.*]])
2020
// CHECK: arith.constant 0 : index loc(#[[ConstLoc1:.*]])
2121
%0 = arith.constant 42 : i32 loc("simple_hoist":0:0)
@@ -26,9 +26,9 @@ func.func @simple_hoist(%arg0: memref<8xi32>) -> i32 {
2626

2727
return %2 : i32
2828
}
29-
// CHECK-DAG: #[[UnknownLoc]] = loc(unknown)
3029
// CHECK-DAG: #[[ConstLoc0]] = loc("simple_hoist":0:0)
3130
// CHECK-DAG: #[[ConstLoc1]] = loc("simple_hoist":1:0)
31+
// CHECK-DAG: #[[ConstLoc2]] = loc("simple_hoist":2:0)
3232

3333
// -----
3434

0 commit comments

Comments
 (0)