Skip to content

Conversation

@copybara-service
Copy link

@copybara-service copybara-service bot commented Dec 3, 2024

Reuse buffers across scopes.

Previously buffer reuse pass would merge allocations at the innermost scope and hoist allocations to the outermost scope. This misses some reuse opportunities. For example, the two allocations in the separate scf blocks won't get merged before this CL.

  scf.if {
    scf.if {
      %a = memref.alloc() : memref<f32>
      "some_a.op"(%a) : (memref<f32>) -> ()
    }
    scf.if {
      %b = memref.alloc() : memref<f32>
      "some_b.op"(%b) : (memref<f32>) -> ()
    }
  }

Before this CL, two allocations are hoisted to the top level.

  %a = memref.alloc() : memref<f32>
  %b = memref.alloc() : memref<f32>
  scf.if {
    scf.if {
      "some_a.op"(%a) : (memref<f32>) -> ()
    }
    scf.if {
      "some_b.op"(%b) : (memref<f32>) -> ()
    }
  }

After this CL, only one allocation is needed because we run buffer reuse after each hoist. It detected that the buffers could be reused after the two allocations are hoisted out of the their separate scf blocks but before they are hoisted again to the top.

  memref.alloc() : memref<f32>
  scf.if {
    scf.if {
      "some_a.op"(%a) : (memref<f32>) -> ()
    }
    scf.if {
      "some_b.op"(%b) : (memref<f32>) -> ()
    }
  }

In summary this CL tries to apply buffer reuse after hoisting allocations at each scope level, which fixes this issue.

@copybara-service copybara-service bot force-pushed the exported_pr_702417144 branch from 9de8664 to 4f970ed Compare December 4, 2024 01:58
@copybara-service copybara-service bot changed the title Optimize buffer-reuse pass to reuse buffers after each hoist. Optimize buffer-reuse pass. Dec 4, 2024
@copybara-service copybara-service bot force-pushed the exported_pr_702417144 branch from 4f970ed to d127ecc Compare December 4, 2024 07:14
@copybara-service copybara-service bot changed the title Optimize buffer-reuse pass. Reuse buffers across scopes. Dec 4, 2024
@copybara-service copybara-service bot force-pushed the exported_pr_702417144 branch 2 times, most recently from a1ef4eb to 44e4923 Compare December 4, 2024 19:56
Previously buffer reuse pass would merge allocations at the innermost scope and hoist allocations to the outermost scope. This misses some reuse opportunities. For example, the two allocations in the separate scf blocks won't get merged before this CL.
```
  scf.if {
    scf.if {
      %a = memref.alloc() : memref<f32>
      "some_a.op"(%a) : (memref<f32>) -> ()
    }
    scf.if {
      %b = memref.alloc() : memref<f32>
      "some_b.op"(%b) : (memref<f32>) -> ()
    }
  }
```
Before this CL, two allocations are hoisted to the top level.
```
  %a = memref.alloc() : memref<f32>
  %b = memref.alloc() : memref<f32>
  scf.if {
    scf.if {
      "some_a.op"(%a) : (memref<f32>) -> ()
    }
    scf.if {
      "some_b.op"(%b) : (memref<f32>) -> ()
    }
  }
```
After this CL, only one allocation is needed because we run buffer reuse after each hoist. It detected that the buffers could be reused after the two allocations are hoisted out of the their separate scf blocks but before they are hoisted again to the top.
```
  memref.alloc() : memref<f32>
  scf.if {
    scf.if {
      "some_a.op"(%a) : (memref<f32>) -> ()
    }
    scf.if {
      "some_b.op"(%b) : (memref<f32>) -> ()
    }
  }
```
In summary this CL tries to apply buffer reuse after hoisting allocations at each scope level, which fixes this issue.

PiperOrigin-RevId: 702812416
@copybara-service copybara-service bot force-pushed the exported_pr_702417144 branch from 44e4923 to 72f5776 Compare December 4, 2024 20:26
@copybara-service copybara-service bot closed this Dec 4, 2024
@copybara-service copybara-service bot deleted the exported_pr_702417144 branch December 4, 2024 20:26
@copybara-service copybara-service bot merged commit 72f5776 into master Dec 4, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant