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

ARM hard-float codgen issue when -O2 present

Open ltertan opened this issue 2 years ago • 11 comments

Consider the following code: https://godbolt.org/z/ThT1YdG15

volatile float init=0;
volatile float *stage=&init;

float f1 = 0, f2 = 143;

int main()
{
	f1 = *stage * 3.14;
	if (f1 > 1.0F) {
		f2 = 3.14F/f1;
	} else {
		f2 = 0.0F;
	}

	return 0;
}

When compiled with the next set of options:

-mhard-float --target=arm -mcpu=cortex-r52

It will produce valid code, as the vdiv.f32 is inside the LBB0_1 block (as a result of the vcmp.f32 logic)

        vcmp.f32        s0, s2
        vmrs    APSR_nzcv, fpscr
        ble     .LBB0_2
        b       .LBB0_1
.LBB0_1:
        movw    r0, :lower16:f1
        movt    r0, :upper16:f1
        vldr    s2, [r0]
        vldr    s0, .LCPI0_1
        vdiv.f32        s0, s0, s2

When compiled with the below set of options, it fails to restrict the vdiv.f32 and it may end up in a division by zero scenario:

-mhard-float --target=arm -mcpu=cortex-r52 -O2
        vcvt.f64.f32    d16, s0
        vmul.f64        d16, d16, d17
        vcvt.f32.f64    s0, d16
        vcmp.f32        s0, s4
        vldr    s4, .LCPI0_4
        vdiv.f32        s2, s2, s0
        vstr    s0, [r2]

I am able to see this behaviour on all versions between 9 and 18. Have not checked for older than 9.

It also reproduces on other optim variants as well (-O, Oz etc.).

ltertan avatar Dec 20 '23 16:12 ltertan

@llvm/issue-subscribers-backend-arm

Author: Laurentiu Tertan (ltertan)

Consider the following code:
volatile float init=0;
volatile float *stage=&init;

float f1 = 0, f2 = 143;

int main()
{
	f1 = *stage * 3.14;
	if (f1 > 1.0F) {
		f2 = 3.14F/f1;
	} else {
		f2 = 0.0F;
	}

	return 0;
}

When compiled with the next set of options:

-mhard-float --target=arm -mcpu=cortex-r52

It will produce valid code, as the vdiv.f32 is inside the LBB0_1 block (as a result of the vcmp.f32 logic)

        vcmp.f32        s0, s2
        vmrs    APSR_nzcv, fpscr
        ble     .LBB0_2
        b       .LBB0_1
.LBB0_1:
        movw    r0, :lower16:f1
        movt    r0, :upper16:f1
        vldr    s2, [r0]
        vldr    s0, .LCPI0_1
        vdiv.f32        s0, s0, s2

When compiled with the below set of options, it fails to restrict the vdiv.f32 and it may end up in a division by zero scenario:

-mhard-float --target=arm -mcpu=cortex-r52 -O2
        vcvt.f64.f32    d16, s0
        vmul.f64        d16, d16, d17
        vcvt.f32.f64    s0, d16
        vcmp.f32        s0, s4
        vldr    s4, .LCPI0_4
        vdiv.f32        s2, s2, s0
        vstr    s0, [r2]

I am able to see this behaviour on all versions between 9 and 18. Have not checked for older than 9.

It also reproduces on other optim variants as well (-O, Oz etc.).

llvmbot avatar Dec 20 '23 16:12 llvmbot

Hi. Dividing by zero is not invalid in itself (for floating point). Although it can be a bit slow.

By default llvm's floating point exception model assumes that floating-pointer operations do not trap. There is a -ftrapping-math option to let them be more constrained, but unfortunately it does not work for Arm yet.

davemgreen avatar Dec 20 '23 17:12 davemgreen

The compiler cannot know there is a divide by 0 due to the stage variable being volatile (Note that f1 is computed based on stage).

ltertan avatar Dec 20 '23 17:12 ltertan

There is no violation of aliasing rules as well. So this looks like a real bug.

RajuSubbian avatar Dec 20 '23 17:12 RajuSubbian

There is no violation of aliasing rules as well. So this looks like a real bug.

Not sure what you mean by this, but there's nothing wrong here. It is not invalid to introduce a floating point divide by zero.

arsenm avatar Dec 21 '23 09:12 arsenm

@arsenm Please let us know where is divide by zero coming from. The expression in this statement

f1 = *stage * 3.14;

cannot be computed to 0 as the variable stage is pointing to a volatile var.

volatile float init=0; volatile float *stage=&init;

RajuSubbian avatar Dec 21 '23 11:12 RajuSubbian

The real question is, is it faster to this branchless or not? The divide by 0 is misleading here due to clang defaulting to no trapping math but I am not sure it is faster to do the divide always than having a branch.

pinskia avatar Dec 22 '23 03:12 pinskia

@arsenm Please let us know where is divide by zero coming from. The expression in this statement

f1 = *stage * 3.14;

cannot be computed to 0 as the variable stage is pointing to a volatile var.

volatile float init=0; volatile float *stage=&init;

I don't understand what you are asking. If the backend realized that f1 would be 0, the fdiv would have simplified to an infinity and the vdiv instruction would not be in the output.

arsenm avatar Dec 22 '23 04:12 arsenm

@arsenm I am saying that backend cannot know that f1 would be 0 as the computation of the expression, *stage * 3.14, relies on value of volatile var '*stage' whose value cannot be known ahead of time.

RajuSubbian avatar Dec 22 '23 10:12 RajuSubbian

Out of interest, how are you detecting the fp divide by zero? Through the exception flags?

The issue is that:

  • LLVM by default assumes -fno-trapping-math, so assumes fp operations do not have side effects and x/0 is just equal to inf (or -inf or nan, depending on x).
  • (GCC assumes the opposite by default).
  • There is an option -ftrapping-math which can then model fp side effects, but unfortunately it is not yet supported for Arm.
  • This is only a problem if you are detecting the fp exception somehow, which presumably means you are reading the exception flags?

If you want a workaround, I would probably suggest putting the fdiv into an inline assembly block. Something like this: https://godbolt.org/z/fMcTv4a5T

davemgreen avatar Dec 22 '23 11:12 davemgreen

@davemgreen Yes. We are using exception flags to monitor divide by 0 scenarios. Thanks for the workaround suggestion.

RajuSubbian avatar Dec 30 '23 07:12 RajuSubbian