[flang-commits] [flang] [flang][OpenMP] Enable lastprivate with collapse (PR #93778)

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Mon Jun 3 02:27:05 PDT 2024


kiranchandramohan wrote:

> As @kiranchandramohan mentioned, `DataSharingProcessor::insertLastPrivateCompare` is currently assuming collapse == 1.
> 
> While this patch may produce correct results, it would result in the lastprivate variable being assigned to on every iteration of the inner loops, when on the last iteration of the outer loop, which doesn't seem right.
> 
> However, checking all related iteration variables during many iterations would not be very efficient. IIRC, some time ago it was suggested that lastprivate handling should be moved to `omp` dialect, to make it possible to update the lastprivate variable in the end of the loop and avoid iteration variable comparisons.

The previous suggestion was to create an `omp.lastprivate` operation that takes the place of the `fir.if` that we insert for the lastprivate comparison. Now that @ergawy has made progress with the delayed privatization, you might be able to use the infrastructure he created to do this. The idea is to use the `%.omp.is_last` variable (which identifies whether this is the last iteration or not) that is given as input to the `__kmpc_for_static_init_4` openmp runtime call. You can use this in the exit block of the loop and do a comparison and branch to either do the lastprivate update (if it is the last iteration) or exit. All this to be done during translation or in the OpenMP IRBuilder.

```
call void @__kmpc_for_static_init_4(ptr @1, i32 %2, i32 34, ptr %.omp.is_last, ptr %.omp.lb, ptr %.omp.ub, ptr %.omp.stride, i32 1, i32 1)
; ... intervening code
omp.loop.exit:                                    ; preds = %omp.inner.for.end
  call void @__kmpc_for_static_fini(ptr @1, i32 %2)
  %14 = load i32, ptr %.omp.is_last, align 4
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %.omp.lastprivate.then, label %.omp.lastprivate.done

.omp.lastprivate.then:                            ; preds = %omp.loop.exit
  %16 = load i32, ptr %x2, align 4
  store i32 %16, ptr %0, align 4
  br label %.omp.lastprivate.done

.omp.lastprivate.done:                            ; preds = %.omp.lastprivate.then, %omp.loop.exit
  ret void
  ```





https://github.com/llvm/llvm-project/pull/93778


More information about the flang-commits mailing list