Compiler Settings: -fno-fast-math vs "Enable Fast Math"

In XCode's Metal Compiler Build Options there is a built-in field for Enable Fast Math which in XCode is NO by default.


I'm working on a shader for an iOS app that exhibited drastically different behaviour on different iPad's. The error looked like a math precision issue so as the shader doesn't need to be super fast and as a test I added -fno-fast-math to XCode's Other Metal Compiler Flags and sure enough the issue appears to be cured.


My question is, is this a bug in XCode or is Enable Fast Math a different option to -fno-fast-math ( I dont't imagine it is ).


Or... could my issue be a result of some other difference between devices that I'm not aware of.


However, like I say, the issue appears to be cured by explitly declaring -fno-fast-math in the build settings.


XCode Version 11.4 (11E146)

Various iPads, running iOS 11, 12 and 13.

Replies

NB: For my purpose, after a deeper read of the documentation, rather than set the entire shader to -fno-fast-math I've also found using the namespace metal::precise:: seems to work as expected. But my original question about the compiler setting is still valid I think. I don't consider myself an expert in MSL so I'm interested in knowing what is considered best practice etc.

By default Xcode sets Enable Fast Math flags to YES which should correspond to passing the -ffast-math flag to the Metal compiler.

If you change the option to NO this will correspond to the -fno-fast-math option being passed to the compiler. You can check what options are being passed to the compiler by inspecting the build logs in Xcode. You shouldn't need to add -fno-fast-math to the Other Metal Compiler Flags for this purpose.


The namespace metal::precise provides developers with a mechanism to use the more precise version of Metal standard library functions in a more fine grained way. Fast math allows for compiler optimizations like reassociation over common operations like addition/subtraction/etc. which are not valid under precise math rules. Mpre information about this can be found in the Metal Shading Language Specification under the Section Math Intrinsics Compiler Options. For best performance, in general it is better to try and keep as much of the shader using Fast Math.