Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change pretty-printing of floats because of LLVM IR restrictions #137

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
change pretty-printing of floats because of LLVM IR restrictions
As was pointed out by @tomsmeding here:
#135 (comment)
we cannot use 32-bit (8 characters) "float" constants in LLVM IR.

Instead, we must embed the 32-bit float as 64-bit double constants with
the same meaning.
  • Loading branch information
Ptival committed Jul 15, 2024
commit 50173e773568a34411ad3f72ec3d25a664827fc0
10 changes: 6 additions & 4 deletions src/Text/LLVM/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Data.Char (isAlphaNum,isAscii,isDigit,isPrint,ord,toUpper)
import Data.List (intersperse)
import qualified Data.Map as Map
import Data.Maybe (catMaybes,fromMaybe,isJust)
import GHC.Float (castDoubleToWord64, castFloatToWord32)
import GHC.Float (castDoubleToWord64, float2Double)
import Numeric (showHex)
import Text.PrettyPrint.HughesPJ
import Data.Int
Expand Down Expand Up @@ -816,9 +816,11 @@ ppValue' pp val = case val of
ValBool b -> ppBool b
-- Note: for +Inf/-Inf/NaNs, we want to output the bit-correct sequence
ValFloat f ->
if isInfinite f || isNaN f
then text "0x" <> text (showHex (castFloatToWord32 f) "")
else float f
-- WARNING: You should **not** use `castFloatToWord32` or `float` here. LLVM IR does not
-- support 32-bit floating point constants, instead it wants to see 32-bit compatible 64-bit
-- constants. We want to preserve the exact mantissa (zero-extended to the right from 23 to
-- 52 bits), which happens to be the behavior of `float2Double`.
text "0x" <> text (showHex (castDoubleToWord64 (float2Double f)) "")
Comment on lines -819 to +823
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR, we will now always display float-typed constants in hexadecimal form, even values that aren't infinite or NaNs. Is that desirable? (As #135 (comment) shows, it's still possible to write things like float 1.0.)

Regardless of what choice we make here, we should add some test cases that exercise the pretty-printing code for non-infinite, non-NaN values at type float and double.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that I apparently haven't run into yet, but as also mentioned in the documentation, LLVM will only accept "readable" constants if they exactly match a floating-point value. In particular, 1.3 is accepted for double but not for float.
It may simply be easier to always output floating-point constants in hexadecimal form, so that we never run into issues like this. (And for my particular purpose, I don't care very much about readability of floating-point constants.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. That's quite tricky.

In that case, I agree that if there isn't a straightforward way to determine if LLVM will accept the output of calling the float function, then we should conservatively print all float constants in hexadecimal form. It would also be worth mentioning the exact floating-point value requirement in the comments here, as I wasn't aware of that additional subtlety until just now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, if float prints things more nicely, and in a way that LLVM accepts, I can look into making this change less radical.

ValDouble d ->
if isInfinite d || isNaN d
then text "0x" <> text (showHex (castDoubleToWord64 d) "")
Expand Down
16 changes: 12 additions & 4 deletions test/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,33 @@ tests = Tasty.testGroup "LLVM pretty-printing output tests"
--------
|]

-- NOTE: The following tests' expected output may look surprising. See the "WARNING" note in
-- `ppValue'` for details.

, testCase "Floats should use 64-bit constants" $
assertEqLines
(ppToText $ ppLLVM37 ppValue (ValFloat (castWord32ToFloat 0x42280000)))
"0x4045000000000000"

, testCase "Positive Infinity (float)" $
assertEqLines
(ppToText $ ppLLVM37 ppValue (ValFloat (castWord32ToFloat 0x7F800000)))
"0x7f800000"
"0x7ff0000000000000"

, testCase "Negative Infinity (float)" $
assertEqLines
(ppToText $ ppLLVM37 ppValue (ValFloat (castWord32ToFloat 0xFF800000)))
"0xff800000"
"0xfff0000000000000"

, testCase "NaN 1 (float)" $
assertEqLines
(ppToText $ ppLLVM37 ppValue (ValFloat (castWord32ToFloat 0x7FC00000)))
"0x7fc00000"
"0x7ff8000000000000"

, testCase "NaN 2 (float)" $
assertEqLines
(ppToText $ ppLLVM37 ppValue (ValFloat (castWord32ToFloat 0x7FD00000)))
"0x7fd00000"
"0x7ffa000000000000"

, testCase "Positive Infinity (double)" $
assertEqLines
Expand Down
Loading