llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

StructurizeCFG does not handle branching out of loops into function exit block

Open llvmbot opened this issue 11 years ago • 4 comments

Bugzilla Link 25378
Version trunk
OS All
Blocks llvm/llvm-project#28112
Attachments Repro case
Reporter LLVM Bugzilla Contributor
CC @ekatz,@arsenm,@ssahasra

Extended Description

I've been investigating the StructurizeCFG pass, and it looks like it has trouble handling CFG edges that break out of a loop and go directly to the function exit.

The input IR I have is the following:

define <4 x float> @​structurizer_test(<4 x float> %inp.coerce) { %1 = extractelement <4 x float> %inp.coerce, i32 0 %2 = fcmp ogt float %1, 0.000000e+00 br i1 %2, label %.lr.ph.i, label %._crit_edge.i

.lr.ph.i: ; preds = %7, %0 %i.03.i = phi float [ %8, %7 ], [ 0.000000e+00, %0 ] %ret.02.i = phi <4 x float> [ %5, %7 ], [ <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>, %0 ] %3 = extractelement <4 x float> %ret.02.i, i32 0 %4 = fadd fast float %3, 0xBFB99999A0000000 %5 = insertelement <4 x float> %ret.02.i, float %4, i32 0 %6 = fcmp olt float %4, 5.000000e-01 br i1 %6, label %_Z9get_colorDv2_f.exit, label %7

;

._crit_edge.i: ; preds = %7, %0 %ret.0.lcssa.i = phi <4 x float> [ <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>, %0 ], [ %5, %7 ] %10 = insertelement <4 x float> %ret.0.lcssa.i, float 0.000000e+00, i32 2 br label %_Z9get_colorDv2_f.exit

_Z9get_colorDv2_f.exit: ; preds = %._crit_edge.i, %.lr.ph.i %.0.i = phi <4 x float> [ %10, %._crit_edge.i ], [ %5, %.lr.ph.i ] ret <4 x float> %.0.i }

After structurization, I have a module that has what looks like a reasonable CFG, but bad branch conditions and PHIs:

define <4 x float> @​structurizer_test(<4 x float> %inp.coerce) { %1 = extractelement <4 x float> %inp.coerce, i32 0 %2 = fcmp ogt float %1, 0.000000e+00 %3 = xor i1 %2, true br label %Flow

Flow: ; preds = %Flow1, %0 %4 = phi <4 x float> [ %14, %Flow1 ], [ <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>, %0 ] %5 = phi <4 x float> [ %16, %Flow1 ], [ <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>, %0 ] %6 = phi float [ %17, %Flow1 ], [ 0.000000e+00, %0 ] %7 = phi i1 [ %18, %Flow1 ], [ %3, %0 ] %8 = phi i1 [ false, %Flow1 ], [ %2, %0 ] br i1 %8, label %.lr.ph.i, label %Flow1

.lr.ph.i: ; preds = %Flow %i.03.i = phi float [ %6, %Flow ] %ret.02.i = phi <4 x float> [ %5, %Flow ] %9 = extractelement <4 x float> %ret.02.i, i32 0 %10 = fadd fast float %9, 0xBFB99999A0000000 %11 = insertelement <4 x float> %ret.02.i, float %10, i32 0 %12 = fcmp olt float %10, 5.000000e-01 %13 = xor i1 %12, true br i1 %13, label %19, label %Flow2

Flow1: ; preds = %Flow2, %Flow %14 = phi <4 x float> [ %23, %Flow2 ], [ %4, %Flow ] %15 = phi <4 x float> [ %11, %Flow2 ], [ undef, %Flow ] %16 = phi <4 x float> [ %24, %Flow2 ], [ %5, %Flow ] %17 = phi float [ %25, %Flow2 ], [ %6, %Flow ] %18 = phi i1 [ %26, %Flow2 ], [ %7, %Flow ] br i1 true, label %Flow3, label %Flow

;

Flow2: ; preds = %19, %.lr.ph.i %23 = phi <4 x float> [ %11, %19 ], [ %4, %.lr.ph.i ] %24 = phi <4 x float> [ %11, %19 ], [ undef, %.lr.ph.i ] %25 = phi float [ %20, %19 ], [ undef, %.lr.ph.i ] %26 = phi i1 [ %22, %19 ], [ %7, %.lr.ph.i ] br label %Flow1

Flow3: ; preds = %Flow1 br i1 %18, label %._crit_edge.i, label %_Z9get_colorDv2_f.exit

._crit_edge.i: ; preds = %Flow3 %ret.0.lcssa.i = phi <4 x float> [ %14, %Flow3 ] %27 = insertelement <4 x float> %ret.0.lcssa.i, float 0.000000e+00, i32 2 br label %_Z9get_colorDv2_f.exit

_Z9get_colorDv2_f.exit: ; preds = %._crit_edge.i, %Flow3 %.0.i = phi <4 x float> [ %15, %Flow3 ], [ %27, %._crit_edge.i ] ret <4 x float> %.0.i }

Note the undef values in some of the PHIs and 'i1 true' for the loop branch condition.

llvmbot avatar Nov 02 '15 19:11 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#31240

Artem-B avatar Nov 26 '21 22:11 Artem-B

mentioned in issue llvm/llvm-project#28112

jlebar avatar Nov 26 '21 21:11 jlebar

The AMDGPU backend uses a big hammer to workaround this problem: the recently introduced unify-loop-exits pass. See the tests added under tests/Transforms/StructurizeCFG/workarounds.

ssahasra avatar May 06 '20 15:05 ssahasra

This is still reproducible in current trunk (and LLVM 10.0).

the new branch "br i1 true, label %Flow3, label %Flow" is incorrect, as it breaks the loop (always).

ekatz avatar Apr 30 '20 11:04 ekatz