[cfe-dev] CLang and ISO C math functions

Martin J. O'Riordan via cfe-dev cfe-dev at lists.llvm.org
Thu Sep 1 01:07:36 PDT 2016

Thanks Hal,


From: Hal Finkel [mailto:hfinkel at anl.gov] 
Sent: 01 September 2016 01:48

I would expect this behaviour if ‘-ffast-math -fno-math-errno’ was selected, but it isn’t, and I think that this is an invalid optimisation.  It also means that some of my math functional tests are not reporting honestly (this only happens when the argument(s) are constants).  Also, on our architecture, ‘double’ is FP32,

Does Clang for your target emit C-language "double" types as "double" at the IR level? If so, that's wrong. "double" at the IR level is assumed to be an IEEE double-precision number. All of the constant folding will do the wrong thing on your target if this is what is happening.


No, I have:


      DoubleFormat = &llvm::APFloat::IEEEsingle;


set in my ‘TargetInfo’, and the IR shows ‘f32’.  But it is the elision of the tests when I am not using ‘-ffast-math’ that I think is wrong, the tests are not present; I’m quite happy with this behaviour when ‘-ffast-math’ is used.  In the ‘foo’ example the calls to ‘foo’ are retained and the tests are present; it is only when I rename ‘foo’ to ‘exp’ or some other math function that this happens.


With ‘-fno-math-errno’ it can be assumed that the math functions have no other side-effects, and in combination with ‘-ffast-math’ the reduction to the optimal ‘return 0’ is perfect.  But neither of these options are selected.


I downloaded the official v3.8.0 distribution for X86 from the LLVM website and tried that, and got the same behaviour, it’s not particular to my out-of-tree changes.  With v3.9.0 RC3 the calls to the math functions are no longer elided, but the tests still are.


and it is probable that the compiler is using the host platform’s implementation which is FP64 for evaluating the test expressions,

Yes, that's right. See ConstantFoldScalarCall in LLVM's lib/Analysis/ConstantFolding.cpp. We're obviously aware this can cause issues when cross compiling. If you'd like to discuss this behavior, you should do so on llvm-dev. We might want to make this more configurable than it currently is.

I don’t have any issue with the constant-folding, just the elision of the tests.  I assume that the special handling of the C math functions is happening in CLang rather than LLVM, but I don’t generally look much at the semantic analysis code so I am not as familiar with it.  If the math library semantic issues are LLVM then I should post this on LLVM-Dev, but I think that this is more likely front-end issue - no?




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160901/825debda/attachment.html>

More information about the cfe-dev mailing list