Skip to content

Commit 8b07872

Browse files
committed
[mlir] Fix liveness analysis.
Signed-off-by: ikulagin <[email protected]>
1 parent 87452bc commit 8b07872

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

mlir/lib/Analysis/Liveness.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ struct BlockInfoBuilder {
6767
// Mark all nested operation results as defined, and nested operation
6868
// operands as used. All defined value will be removed from the used set
6969
// at the end.
70-
block->walk([&](Operation *op) {
71-
for (Value result : op->getResults())
72-
defValues.insert(result);
73-
for (Value operand : op->getOperands())
74-
useValues.insert(operand);
70+
block->walk([&](Block *nestedBlock) {
71+
if (block != nestedBlock) {
72+
for (BlockArgument arg : nestedBlock->getArguments()) {
73+
defValues.insert(arg);
74+
}
75+
}
76+
for (Operation &op : *nestedBlock) {
77+
for (Value result : op.getResults())
78+
defValues.insert(result);
79+
for (Value operand : op.getOperands())
80+
useValues.insert(operand);
81+
}
7582
});
7683
llvm::set_subtract(useValues, defValues);
7784
}

mlir/test/Analysis/test-liveness.mlir

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,59 @@ func.func @nested_region3(
493493
}
494494
return %1 : i32
495495
}
496+
497+
// -----
498+
499+
// CHECK-LABEL: Testing : nested_region4
500+
501+
func.func @nested_region4(%arg0: index, %arg1: index, %arg2: index) {
502+
// CHECK: Block: 0
503+
// CHECK-NEXT: LiveIn:
504+
// CHECK-NEXT: LiveOut:
505+
// CHECK-NEXT: BeginLivenessIntervals
506+
// CHECK-NEXT: val_3
507+
// CHECK-NEXT: %c0_i32 = arith.constant 0
508+
// CHECK-NEXT: %c1_i32 = arith.constant 1
509+
// CHECK-NEXT: %0 = scf.for
510+
// COM: Skipping the body of the scf.for...
511+
// CHECK: val_4
512+
// CHECK-NEXT: %c1_i32 = arith.constant 1
513+
// CHECK-NEXT: %0 = scf.for
514+
// COM: Skipping the body of the scf.for...
515+
// CHECK: // %1 = arith.addi
516+
// CHECK-NEXT: val_5
517+
// CHECK-NEXT: %0 = scf.for
518+
// COM: Skipping the body of the scf.for...
519+
// CHECK: EndLivenessIntervals
520+
// CHECK-NEXT: BeginCurrentlyLive
521+
// CHECK-NEXT: %c0_i32 = arith.constant 0
522+
// CHECK-SAME: arg0@0 arg1@0 arg2@0 val_3
523+
// CHECK-NEXT: %c1_i32 = arith.constant 1
524+
// CHECK-SAME: arg0@0 arg1@0 arg2@0 val_3 val_4
525+
// CHECK-NEXT: %0 = scf.for
526+
// COM: Skipping the body of the scf.for...
527+
// CHECK: arg0@0 arg1@0 arg2@0 val_3 val_4 val_5
528+
// CHECK-NEXT: EndCurrentlyLive
529+
%c0_i32 = arith.constant 0 : i32
530+
%c1_i32 = arith.constant 1 : i32
531+
532+
%0 = scf.for %arg3 = %arg0 to %arg1 step %arg2 iter_args(%arg4 = %c0_i32) -> (i32) {
533+
// CHECK-NEXT: Block: 1
534+
// CHECK-NEXT: LiveIn: val_4
535+
// CHECK-NEXT: LiveOut:
536+
// CHECK-NEXT: BeginLivenessIntervals
537+
// CHECK-NEXT: val_8
538+
// CHECK-NEXT: %1 = arith.addi
539+
// CHECK-NEXT: scf.yield %1
540+
// CHECK-NEXT: EndLivenessIntervals
541+
// CHECK-NEXT: BeginCurrentlyLive
542+
// CHECK-NEXT: %1 = arith.addi
543+
// CHECK-SAME: val_4 arg0@1 arg1@1 val_8
544+
// CHECK-NEXT: scf.yield %1
545+
// CHECK-SAME: val_8
546+
// CHECK-NEXT: EndCurrentlyLive
547+
%1 = arith.addi %arg4, %c1_i32 : i32
548+
scf.yield %1 : i32
549+
}
550+
return
551+
}

0 commit comments

Comments
 (0)