flang-compiler / f18-llvm-project

Fork of llvm/llvm-project for f18. In sync with f18-mlir and f18.
http://llvm.org
28 stars 16 forks source link

FIR CSE bug: load wrongly optimized-out. #208

Closed jeanPerier closed 4 years ago

jeanPerier commented 4 years ago

Load of a variable is wrongly optimized out after a fir.if where the variable is modified:

Input FIR (important load at %4 given fir.store in fir.if):

func @foo(%arg0: !fir.ref<f32>) -> f32 {
  %0 = fir.alloca f32 {name = "x"}
  %1 = fir.load %arg0 : !fir.ref<f32>
  fir.store %1 to %0 : !fir.ref<f32>
  %cst = constant 0.000000e+00 : f32
  %2 = fir.load %0 : !fir.ref<f32>
  %3 = fir.cmpf "olt", %2, %cst : f32
  fir.if %3 {
    %7 = fir.load %0 : !fir.ref<f32>
    %8 = fir.negf %7 : f32
    fir.store %8 to %0 : !fir.ref<f32>
  } else {
  }
  %cst_0 = constant 1.000000e+00 : f32
  %4 = fir.load %0 : !fir.ref<f32>
  %5 = fir.addf %4, %cst_0 : f32
  fir.store %5 to %0 : !fir.ref<f32>
  %6 = fir.load %0 : !fir.ref<f32>
  return %6 : f32
}

tco -o -: (%10 is wrong)

define float @foo(float* %0) !dbg !3 {
  %2 = alloca float, i64 1, align 4, !dbg !7
  %3 = load float, float* %0, align 4, !dbg !9
  store float %3, float* %2, align 4, !dbg !10
  %4 = load float, float* %2, align 4, !dbg !11
  %5 = fcmp olt float %4, 0.000000e+00, !dbg !12
  br i1 %5, label %6, label %9, !dbg !13

6:                                                ; preds = %1
  %7 = load float, float* %2, align 4, !dbg !14
  %8 = fneg float %7, !dbg !15
  store float %8, float* %2, align 4, !dbg !16
  br label %9, !dbg !13

9:                                                ; preds = %6, %1
  %10 = fadd float %4, 1.000000e+00, !dbg !17
  store float %10, float* %2, align 4, !dbg !18
  %11 = load float, float* %2, align 4, !dbg !19
  ret float %11, !dbg !20
}

With -dsiable-cse, the output form tco after block 9 is as expected:

9:                                                ; preds = %6, %1
  %10 = load float, float* %2, align 4, !dbg !17
  %11 = fadd float %10, 1.000000e+00, !dbg !18
schweitzpgi commented 4 years ago

fixed. test added.