Skip to content

[mlir] Fix liveness analysis #88848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2024
Merged

[mlir] Fix liveness analysis #88848

merged 5 commits into from
May 23, 2024

Conversation

ikulagin
Copy link
Contributor

The current implementation does not take into account definitions created by arguments of nested blocks. This leads to an incorrect construction of the live-in set of an outer block. Arguments of nested blocks are added to the live-in set of an outer block.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the mlir label Apr 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2024

@llvm/pr-subscribers-mlir

Author: Ivan Kulagin (ikulagin)

Changes

The current implementation does not take into account definitions created by arguments of nested blocks. This leads to an incorrect construction of the live-in set of an outer block. Arguments of nested blocks are added to the live-in set of an outer block.


Full diff: https://github.com/llvm/llvm-project/pull/88848.diff

2 Files Affected:

  • (modified) mlir/lib/Analysis/Liveness.cpp (+12-5)
  • (modified) mlir/test/Analysis/test-liveness.mlir (+56)
diff --git a/mlir/lib/Analysis/Liveness.cpp b/mlir/lib/Analysis/Liveness.cpp
index a8e0daeabf4061..50ac7129dab87f 100644
--- a/mlir/lib/Analysis/Liveness.cpp
+++ b/mlir/lib/Analysis/Liveness.cpp
@@ -67,11 +67,18 @@ struct BlockInfoBuilder {
     // Mark all nested operation results as defined, and nested operation
     // operands as used. All defined value will be removed from the used set
     // at the end.
-    block->walk([&](Operation *op) {
-      for (Value result : op->getResults())
-        defValues.insert(result);
-      for (Value operand : op->getOperands())
-        useValues.insert(operand);
+    block->walk([&](Block *nestedBlock) {
+      if (block != nestedBlock) {
+        for (BlockArgument arg : nestedBlock->getArguments()) {
+          defValues.insert(arg);
+        }
+      }
+      for (Operation &op : *nestedBlock) {
+        for (Value result : op.getResults())
+          defValues.insert(result);
+        for (Value operand : op.getOperands())
+          useValues.insert(operand);
+      }
     });
     llvm::set_subtract(useValues, defValues);
   }
diff --git a/mlir/test/Analysis/test-liveness.mlir b/mlir/test/Analysis/test-liveness.mlir
index 8ae3d09a6cd122..17b5e7d5ae8990 100644
--- a/mlir/test/Analysis/test-liveness.mlir
+++ b/mlir/test/Analysis/test-liveness.mlir
@@ -493,3 +493,59 @@ func.func @nested_region3(
   }
   return %1 : i32
 }
+
+// -----
+
+// CHECK-LABEL: Testing : nested_region4
+
+func.func @nested_region4(%arg0: index, %arg1: index, %arg2: index) {
+  // CHECK: Block: 0
+  // CHECK-NEXT: LiveIn:
+  // CHECK-NEXT: LiveOut:
+  // CHECK-NEXT: BeginLivenessIntervals
+  // CHECK-NEXT: val_3
+  // CHECK-NEXT: %c0_i32 = arith.constant 0
+  // CHECK-NEXT: %c1_i32 = arith.constant 1
+  // CHECK-NEXT: %0 = scf.for
+  // COM: Skipping the body of the scf.for...
+  // CHECK:      val_4
+  // CHECK-NEXT: %c1_i32 = arith.constant 1
+  // CHECK-NEXT: %0 = scf.for
+  // COM: Skipping the body of the scf.for...
+  // CHECK:      // %1 = arith.addi
+  // CHECK-NEXT: val_5
+  // CHECK-NEXT: %0 = scf.for
+  // COM: Skipping the body of the scf.for...
+  // CHECK:      EndLivenessIntervals
+  // CHECK-NEXT: BeginCurrentlyLive
+  // CHECK-NEXT: %c0_i32 = arith.constant 0
+  // CHECK-SAME: arg0@0 arg1@0 arg2@0 val_3
+  // CHECK-NEXT: %c1_i32 = arith.constant 1
+  // CHECK-SAME: arg0@0 arg1@0 arg2@0 val_3 val_4
+  // CHECK-NEXT: %0 = scf.for
+  // COM: Skipping the body of the scf.for...
+  // CHECK:      arg0@0 arg1@0 arg2@0 val_3 val_4 val_5
+  // CHECK-NEXT: EndCurrentlyLive
+  %c0_i32 = arith.constant 0 : i32
+  %c1_i32 = arith.constant 1 : i32
+
+  %0 = scf.for %arg3 = %arg0 to %arg1 step %arg2 iter_args(%arg4 = %c0_i32) -> (i32) {
+    // CHECK-NEXT: Block: 1
+    // CHECK-NEXT: LiveIn: val_4
+    // CHECK-NEXT: LiveOut:
+    // CHECK-NEXT: BeginLivenessIntervals
+    // CHECK-NEXT: val_8
+    // CHECK-NEXT: %1 = arith.addi
+    // CHECK-NEXT: scf.yield %1
+    // CHECK-NEXT: EndLivenessIntervals
+    // CHECK-NEXT: BeginCurrentlyLive
+    // CHECK-NEXT: %1 = arith.addi
+    // CHECK-SAME: val_4 arg0@1 arg1@1 val_8
+    // CHECK-NEXT: scf.yield %1
+    // CHECK-SAME: val_8
+    // CHECK-NEXT: EndCurrentlyLive
+    %1 = arith.addi %arg4, %c1_i32 : i32
+    scf.yield %1 : i32
+  }
+  return
+}

@ikulagin
Copy link
Contributor Author

@dfki-mako @jurahul Please take a look at the suggested fix.

@makslevental
Copy link
Contributor

For added context, prior to this change

// mlir-opt -pass-pipeline="builtin.module(func.func(test-print-liveness))" testliveness.mlir
module {
func.func @test() {
  %alloc = memref.alloc() : memref<16xf16>
  %c0 = arith.constant 0 : index
  %c10 = arith.constant 10 : index
  %c1 = arith.constant 1 : index
  %0 = scf.for %arg1 = %c0 to %c10 step %c1 iter_args(%arg2 = %alloc) -> memref<16xf16> {
    scf.yield %arg2 : memref<16xf16>
  }
  return
}
}

gives

Testing : test
// ---- Liveness -----
// - Block: 0
// --- LiveIn: arg1@1 
// --- LiveOut: 
// --- BeginLivenessIntervals
// val_0 :
//     %alloc = memref.alloc() : memref<16xf16>
//     %c0 = arith.constant 0 : index
//     %c10 = arith.constant 10 : index
//     %c1 = arith.constant 1 : index
//     %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %alloc) -> (memref<16xf16>) {
  scf.yield %arg1 : memref<16xf16>
}
// val_1 :
//     %c0 = arith.constant 0 : index
//     %c10 = arith.constant 10 : index
//     %c1 = arith.constant 1 : index
//     %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %alloc) -> (memref<16xf16>) {
  scf.yield %arg1 : memref<16xf16>
}
// val_2 :
//     %c10 = arith.constant 10 : index
//     %c1 = arith.constant 1 : index
//     %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %alloc) -> (memref<16xf16>) {
  scf.yield %arg1 : memref<16xf16>
}
// val_3 :
//     %c1 = arith.constant 1 : index
//     %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %alloc) -> (memref<16xf16>) {
  scf.yield %arg1 : memref<16xf16>
}
// val_4 :
//     %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %alloc) -> (memref<16xf16>) {
  scf.yield %arg1 : memref<16xf16>
}
// --- EndLivenessIntervals
// --- BeginCurrentlyLive
//     %alloc = memref.alloc() : memref<16xf16> [val_0 arg1@1]
//     %c0 = arith.constant 0 : index [val_0 val_1 arg1@1]
//     %c10 = arith.constant 10 : index [val_0 val_1 val_2 arg1@1]
//     %c1 = arith.constant 1 : index [val_0 val_1 val_2 val_3 arg1@1]
//     %0 = scf.for %arg0 = %c0 to %c10 step %c1 iter_args(%arg1 = %alloc) -> (memref<16xf16>) {
  scf.yield %arg1 : memref<16xf16>
} [val_0 val_1 val_2 val_3 val_4 arg1@1]
// --- EndCurrentlyLive
// - Block: 1
// --- LiveIn: 
// --- LiveOut: 
// --- BeginLivenessIntervals
// --- EndLivenessIntervals
// --- BeginCurrentlyLive
//     scf.yield %arg1 : memref<16xf16> [arg0@1 arg1@1]
// --- EndCurrentlyLive
// -------------------

@ikulagin ikulagin requested a review from joker-eph April 21, 2024 19:17
@makslevental
Copy link
Contributor

@ikulagin Let us know when you're ready for this to be merged.

@ikulagin
Copy link
Contributor Author

@ikulagin Let us know when you're ready for this to be merged.

@makslevental All fixes have been made, PR is ready for merge.

@makslevental
Copy link
Contributor

@ikulagin do you need us to merge this for you?

@ikulagin
Copy link
Contributor Author

@makslevental I suggest minimizing the test as I wrote in this comment, what's your opinion?

@makslevental
Copy link
Contributor

@makslevental I suggest minimizing the test as I wrote in this comment, what's your opinion?

I don't have an opinion - I think tight tests aren't so bad (they can be useful as smoke signals sometimes) but if you want to reduce then sure it makes sense.

@joker-eph joker-eph merged commit 63e8c0a into llvm:main May 23, 2024
4 of 6 checks passed
Copy link

@ikulagin Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@ikulagin ikulagin deleted the fix-liveness branch May 25, 2024 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants