[LLVMdev] some thoughts on the semantics of !fpmath

Dan Gohman gohman at apple.com
Tue Apr 17 10:09:51 PDT 2012


On Apr 16, 2012, at 11:50 PM, Duncan Sands <baldrick at free.fr> wrote:

> Hi Dan,
> 
>> I realize that some of these thoughts apply equally to the
>> prior !fpaccuracy metadata that's been around a while, but I
>> hadn't looked at it closely until now.
>> 
>> The !fpmath metadata violates the original spirit of
>> metadata, which is that metadata is only supposed to exclude
>> possible runtime conditions, rather than to introduce new
>> possible runtime possibilities. The motivation for this is
>> that optimizers that ignore metadata should always be safe.
>> With !fpmath, theoretically there are ways this can fail.
> 
> I don't understand how it can not be safe: if the metadata is dropped
> then the optimizers have to be more strict, thus it is safe.

Here's an example:

  %z = fadd float %x, %y, !fpmath !{ float 1000.0 }
  %p = fcmp ogt %z, 0.0
  br i1 %p, label %true, label %false
true:
  %d = call float @llvm.sqrt.f32(float %z)

This ought to be safe, since no matter now imprecise the fadd is,
the compare and branch protects the sqrt.

Now suppose a metadata-unaware optimizer can do value-range analysis
and can prove that the fadd always produces a positive finite value.
Then it's safe to replace the fcmp with true and delete the branch.

Then a metadata-aware codegen translates the fadd with reduced
precision, as permitted by the metadata. At runtime, the fadd may
return a negative value sometimes, because that could well be within
1000.0 ULPs of an expected non-negative result. But the branch is
now gone, and undefined behavior sqrts out.

Dan




More information about the llvm-dev mailing list