Fortran Array Descriptors in Tight Loop (flang-new)
I have come across a small OpenMP example where the parallel code optimized at optimization level -O3 contains many store operations in the tight loop. The issue is related with the array descriptors. Flang generates optimized code for the code example in the left column. Adding a second access to the allocatable array completely alters the IR. I compiled the two programs with flang-new -fopenmp -O3 -emit-llvm -S.
| Expected Behavior | Suspected Bug |
|
|
LLVM IR at -O0
When disabling optimizations, there are some differences in the LLVM IR that I do not understand how are related with the relatively small change in the program.
The IR generated from the program in the left column contains the following array descriptor in the tight loop:
omp.wsloop.region: ; preds = %omp_loop.body
...
%22 = load { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEarr, align 8
store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %22, ptr %loadgep_2, align 8
...
The example in the right column contains two array descriptors:
omp.wsloop.region: ; preds = %omp_loop.body
...
%23 = load { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEarr, align 8
store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %23, ptr %loadgep_2, align 8
...
%36 = load { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr @_QFEarr, align 8
store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %36, ptr %loadgep_4, align 8
...
LLVM IR at -O3
At optimization level three, the array descriptor is completely eliminated the tight loop from the example program from the left column:
vector.body: ; preds = %vector.body, %vector.ph
%index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
%22 = or i32 %index, 1
%23 = add i32 %22, %3
%24 = sext i32 %23 to i64
%25 = add nsw i64 %24, -1
%26 = getelementptr double, ptr %.unpack, i64 %25
store <2 x double> %21, ptr %26, align 8, !tbaa !4, !alias.scope !13
%index.next = add nuw i32 %index, 2
%27 = icmp eq i32 %index.next, %n.vec
br i1 %27, label %middle.block, label %vector.body, !llvm.loop !15
In case of the program from the right column, the optimizers do not eliminate the array descriptors which results in very inefficient code.
omp_loop.body: ; preds = %omp_loop.body.lr.ph, %omp_loop.body
%omp_loop.iv66 = phi i32 [ 0, %omp_loop.body.lr.ph ], [ %5, %omp_loop.body ]
%5 = add nuw i32 %omp_loop.iv66, 1
%6 = add i32 %5, %3
%7 = load i32, ptr %loadgep_, align 4, !tbaa !4
%8 = sitofp i32 %7 to float
%9 = fdiv contract float 1.000000e+00, %8
%10 = fpext float %9 to double
store ptr %.unpack, ptr %loadgep_2, align 8, !tbaa !8
store i64 8, ptr %loadgep_2.repack17, align 8, !tbaa !8
store i32 20180515, ptr %loadgep_2.repack19, align 8, !tbaa !8
store i8 1, ptr %loadgep_2.repack21, align 4, !tbaa !8
store i8 28, ptr %loadgep_2.repack23, align 1, !tbaa !8
store i8 2, ptr %loadgep_2.repack25, align 2, !tbaa !8
store i8 0, ptr %loadgep_2.repack27, align 1, !tbaa !8
store i64 1, ptr %loadgep_2.repack29, align 8, !tbaa !8
store i64 %.unpack12.unpack.unpack14, ptr %loadgep_2.repack29.repack31, align 8, !tbaa !8
store i64 8, ptr %loadgep_2.repack29.repack33, align 8, !tbaa !8
%11 = sext i32 %6 to i64
%12 = add nsw i64 %11, -1
%13 = getelementptr double, ptr %.unpack, i64 %12
store double %10, ptr %13, align 8, !tbaa !4
store ptr %.unpack, ptr %loadgep_4, align 8, !tbaa !8
store i64 8, ptr %loadgep_4.repack47, align 8, !tbaa !8
store i32 20180515, ptr %loadgep_4.repack49, align 8, !tbaa !8
store i8 1, ptr %loadgep_4.repack51, align 4, !tbaa !8
store i8 28, ptr %loadgep_4.repack53, align 1, !tbaa !8
store i8 2, ptr %loadgep_4.repack55, align 2, !tbaa !8
store i8 0, ptr %loadgep_4.repack57, align 1, !tbaa !8
store i64 1, ptr %loadgep_4.repack59, align 8, !tbaa !8
store i64 %.unpack12.unpack.unpack14, ptr %loadgep_4.repack59.repack61, align 8, !tbaa !8
store i64 8, ptr %loadgep_4.repack59.repack63, align 8, !tbaa !8
%exitcond.not = icmp eq i32 %omp_loop.iv66, %reass.sub
br i1 %exitcond.not, label %omp_loop.exit, label %omp_loop.body
At first glance, it looked like the many store instructions in the above example came from InstCombinePass, but the optimization pass is just simplifying the store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } instructions.
I am in doubt about whether this is a frontend or optimizer related issue, but I think the fact that the unoptimized intermediate representation is almost identical for the two examples points towards the latter.
@llvm/issue-subscribers-openmp
Update: I think the issue is in LICMPass. The loop invariant code (the array descriptors) is moved from %omp_loop.body to %omp_loop.body.lr.ph in the example that results in optimized code. It fails to move the array descriptors out of %omp_loop.body in the seconds example program.
As with the other example, we have the fir-memref-dataflow-opt pass that should forward loads to stores and may be delete dead stores. With this pass, we get much better results.
Note: The pass is not enabled by default.
omp.wsloop.region: ; preds = %omp_loop.body
%11 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %loadgep_, i32 0, i32 7, i64 0, i32 0
%12 = load i64, ptr %11, align 8
%13 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %loadgep_, i32 0, i32 7, i64 0, i32 1
%14 = load i64, ptr %13, align 8
%15 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %loadgep_, i32 0, i32 7, i64 0, i32 2
%16 = load i64, ptr %15, align 8
%17 = sext i32 %10 to i64
%18 = sub i64 %17, %12
%19 = getelementptr double, ptr %loadgep_2, i64 %18
store double 0x3EB0000000000000, ptr %19, align 8
br label %omp.region.cont2
Command line
./bin/flang-new -fc1 -emit-fir -O3 -fopenmp dupl.f90 -o - | ./bin/fir-opt -fir-memref-dataflow-opt --cse --canonicalize --cfg-conversion --cg-rewrite --fir-to-llvm-ir | ./bin/mlir-translate --mlir-to-llvmir -allow-unregistered-dialect
Hi Kiran
Yes, you are right. The pass you mention is an important step towards resolving the problem. But I would still argue that the quality of the code could be substantially improved. In the optimized code emitted with the-fir-memref-dataflow-opt pass, the loop invariant code motion pass still does not move the loads of the array descriptor out of the tight loop.
Yes, that is correct. We should check whether the FIR operations that extract information from the descriptor can be hoisted in MLIR by marking them as AlwaysSpeculatable or ConditionallySpeculatable. This might also require adding the LoopLikeInterface for the OpenMP dialect omp.wsloop operation.
LLVM might probably not have enough information to hoist it out due to a lack of alias information.
I can't reproduce this with current flang. Are you happy to close?