[Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

ed@modk.it
Hi All,

Clang lacks support for the -fsingle-precision-constant flag.  Are there specific reasons for this or is it just waiting to be implemented?


Once all of your data is defined as float, there are still cases where you may unknowingly cause the compiler to generate double precision operations. The most common issue is when floating point literals such as 3.14159 are used. In C these literals are of type double, and if they are used in an expression consisting of single precision operands, the operations will be promoted to double precision. The proper way to specify a single precision literal is to use an 'f' suffix, 3.14159f.

While defining literals without the 'f' suffix may point to performance issues, in our case the target crashes when the 'f' is omitted.  This is an end user programmable system where we can't expect users to add the 'f'.  I don't fully understand why this occurs (this is a bare metal system where user code is run by a quasi-OS which is maintained by a different team and itself is compiled with a different compiler so it's hard to track down) we remedied this with the -fsingle-precision-constant in gcc.  Now that we're moving to clang/llvm we need a solution and expecting the user to add 'f' is not going to work.  

Any thoughts?

Thanks,
Ed

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: [Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

Reid Kleckner-2
No real reason, I'm sure, other than a general desire to limit the size of the zoo of flags that need maintenance.

On Wed, Jul 15, 2015 at 5:01 AM, [hidden email] <[hidden email]> wrote:
Hi All,

Clang lacks support for the -fsingle-precision-constant flag.  Are there specific reasons for this or is it just waiting to be implemented?


Once all of your data is defined as float, there are still cases where you may unknowingly cause the compiler to generate double precision operations. The most common issue is when floating point literals such as 3.14159 are used. In C these literals are of type double, and if they are used in an expression consisting of single precision operands, the operations will be promoted to double precision. The proper way to specify a single precision literal is to use an 'f' suffix, 3.14159f.

While defining literals without the 'f' suffix may point to performance issues, in our case the target crashes when the 'f' is omitted.  This is an end user programmable system where we can't expect users to add the 'f'.  I don't fully understand why this occurs (this is a bare metal system where user code is run by a quasi-OS which is maintained by a different team and itself is compiled with a different compiler so it's hard to track down) we remedied this with the -fsingle-precision-constant in gcc.  Now that we're moving to clang/llvm we need a solution and expecting the user to add 'f' is not going to work.  

Any thoughts?

Thanks,
Ed

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: [Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

ed@modk.it
Thanks for the response.  If we add the support would you accept the patch?  Seems like a pretty straightforward flag since it maps directly to NumericLiteralParser::NumericLiteralParser within LiteralSupport.cpp.  I understand the maintenance concern with flags that affect multiple points in code though.

Still trying to get the bottom of why we're crashing with double floating point literal.  It seems the fpu in question (fpv4-sp-d16) only supports 32 bit operations so this is somehow getting passed to a software wrapper and crashing there.  But to be fair this was crashing in gcc too which is why we added the -fsingle-precision-constant flag since we didn't care about the precision of the constants and we're using floats everywhere anyway for performance.

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: [Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

Rick Mann

> On Jul 15, 2015, at 09:08 , [hidden email] wrote:
>
> Thanks for the response.  If we add the support would you accept the patch?  Seems like a pretty straightforward flag since it maps directly to NumericLiteralParser::NumericLiteralParser within LiteralSupport.cpp.  I understand the maintenance concern with flags that affect multiple points in code though.
>
> Still trying to get the bottom of why we're crashing with double floating point literal.  It seems the fpu in question (fpv4-sp-d16) only supports 32 bit operations so this is somehow getting passed to a software wrapper and crashing there.  But to be fair this was crashing in gcc too which is why we added the -fsingle-precision-constant flag since we didn't care about the precision of the constants and we're using floats everywhere anyway for performance.

Shouldn't the code generator know to make the constants 32-bit if that's all the target supports? Or does it emulate 64-bit in software?

--
Rick Mann
[hidden email]



_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: [Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

Philip Reames-4
In reply to this post by ed@modk.it
On 07/15/2015 05:01 AM, [hidden email] wrote:
Hi All,

Clang lacks support for the -fsingle-precision-constant flag.  Are there specific reasons for this or is it just waiting to be implemented?


Once all of your data is defined as float, there are still cases where you may unknowingly cause the compiler to generate double precision operations. The most common issue is when floating point literals such as 3.14159 are used. In C these literals are of type double, and if they are used in an expression consisting of single precision operands, the operations will be promoted to double precision. The proper way to specify a single precision literal is to use an 'f' suffix, 3.14159f.

While defining literals without the 'f' suffix may point to performance issues, in our case the target crashes when the 'f' is omitted.  This is an end user programmable system where we can't expect users to add the 'f'.  I don't fully understand why this occurs (this is a bare metal system where user code is run by a quasi-OS which is maintained by a different team and itself is compiled with a different compiler so it's hard to track down) we remedied this with the -fsingle-precision-constant in gcc.  Now that we're moving to clang/llvm we need a solution and expecting the user to add 'f' is not going to work. 
Another approach might be to add a dedicated warning (off by default) which warns or errors on these cases.  It also seems like a case where a clang-tidy rewrite rule could help. 

Any thoughts?

Thanks,
Ed


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: [Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

ed@modk.it
In reply to this post by Rick Mann
> Still trying to get the bottom of why we're crashing with double floating point literal.  It seems the fpu in question (fpv4-sp-d16) only supports 32 bit operations so this is somehow getting passed to a software wrapper and crashing there.  But to be fair this was crashing in gcc too which is why we added the -fsingle-precision-constant flag since we didn't care about the precision of the constants and we're using floats everywhere anyway for performance.

Shouldn't the code generator know to make the constants 32-bit if that's all the target supports? Or does it emulate 64-bit in software?

Yes I believe it is (trying to) emulate in software.  There's no requirement to have an FPU at all so this is the expected behavior.



_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: [Clang] Reasons for lack of -fsingle-precision-constant support? Alternatives?

ed@modk.it
In reply to this post by Philip Reames-4
Another approach might be to add a dedicated warning (off by default) which warns or errors on these cases.  It also seems like a case where a clang-tidy rewrite rule could help. 

That would make sense but our tools are so high level we don't even generate compiler errors because there should be none by design:  http://modkit.com/vex 

We can certainly append the 'f' in our highest level tools but we're working on some tools that are closer to the metal and want to keep things 1:1.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev