This is an archive of the discontinued LLVM Phabricator instance.

[OpenCL] Prevent fused mul and add by default
AbandonedPublic

Authored by Anastasia on May 22 2020, 7:57 AM.

Details

Reviewers
rjmccall
arsenm
Summary

Currently, clang will generate fused operation fmuladd for expressions like a*b+c. However, I can't find anything in the spec that explains this behavior. But clang was doing this for almost 10 years....

I looked at:
s6.13.2 where it details fp conract pragma:

// on-off-switch is one of ON, OFF, or DEFAULT.
// The DEFAULT value is ON.
#pragma OPENCL FP_CONTRACT on-off-switch

The default behavior here refers to the default value of the pragma when used in the source code.

s7.4 table 38

x * y + z
Implemented either as a correctly rounded fma or as a multiply and an add both of which are correctly rounded.

This table described behavior when -cl-unsafe-math-optimizations flag is passed.

I can't find anything else that would justify allowing fused operations, therefore I assume this is not safe to do and can cause issues on some applications/targets

Diff Detail

Event Timeline

Anastasia created this revision.May 22 2020, 7:57 AM
svenvh added a subscriber: svenvh.May 22 2020, 8:49 AM

I think "the default value is on" is meant to imply that contraction is allowed by default, not that you can write the pragma without an on-off-switch and it means "on".

FWIW, that would be consistent with C, which also enables contraction by default.

Contraction is just an optimization technique. Unless a user explicitly requests strict FP semantics, contraction does not break C++ semantics.

arsenm requested changes to this revision.May 27 2020, 4:23 PM

I think the current handling is correct. As you said, the specified default is FP_CONTRACT ON. The description of the mad function in the table is unrelated, since that's the definition and won't change based on the pragma.

This revision now requires changes to proceed.May 27 2020, 4:23 PM
Anastasia abandoned this revision.May 28 2020, 9:00 AM