Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

[Flang][OpenMP][MLIR] Add pass to optimize out allocation for worksharing loop indices #228

Closed

Conversation

skatrak
Copy link
Contributor

@skatrak skatrak commented Sep 7, 2023

This pass rewrites this pattern commonly appearing for parallel worksharing loops:

omp.parallel   {
  %0 = fir.alloca i32 {adapt.valuebyref, pinned}
  %c1_i32 = arith.constant 1 : i32
  %c10_i32 = arith.constant 10 : i32
  %c1_i32_0 = arith.constant 1 : i32
  omp.wsloop   for  (%arg0) : i32 = (%c1_i32) to (%c10_i32) inclusive step (%c1_i32_0) {
    fir.store %arg0 to %0 : !fir.ref<i32>
    %c7_i32 = arith.constant 7 : i32
    %1 = fir.load %0 : !fir.ref<i32>
    %2 = fir.convert %1 : (i32) -> i64
    %c1_i64 = arith.constant 1 : i64
    %3 = arith.subi %2, %c1_i64 : i64
    %4 = fir.coordinate_of %0, %3 : (!fir.ref<!fir.array<10xi32>>, i64) -> !fir.ref<i32>
    fir.store %c7_i32 to %4 : !fir.ref<i32>
    omp.yield
  }
  omp.terminator
}

Into this, where allocations for loop index variables have been removed:

omp.parallel   {
  %c1_i32 = arith.constant 1 : i32
  %c10_i32 = arith.constant 10 : i32
  %c1_i32_0 = arith.constant 1 : i32
  omp.wsloop   for  (%arg0) : i32 = (%c1_i32) to (%c10_i32) inclusive step (%c1_i32_0) {
    %c7_i32 = arith.constant 7 : i32
    %2 = fir.convert %arg0 : (i32) -> i64
    %c1_i64 = arith.constant 1 : i64
    %3 = arith.subi %2, %c1_i64 : i64
    %4 = fir.coordinate_of %0, %3 : (!fir.ref<!fir.array<10xi32>>, i64) -> !fir.ref<i32>
    fir.store %c7_i32 to %4 : !fir.ref<i32>
    omp.yield
  }
  omp.terminator
}

@skatrak skatrak self-assigned this Sep 7, 2023
@skatrak
Copy link
Contributor Author

skatrak commented Sep 27, 2023

Closing due to creating equivalent upstream PR that won't probably be merged. Another approach will be needed.

@skatrak skatrak closed this Sep 27, 2023
@skatrak skatrak deleted the alloca-pass branch September 28, 2023 13:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant