#14872 closed feature request (fixed)

Hex Literals in GHC Core

Reported by: andrewthad Owned by: andrewthad
Priority: lowest Milestone: 8.6.1
Component: Compiler Version: 8.2.2
Keywords: newcomer Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Sometimes, when I'm doing stuff that involves twiddling bits, it would be nicer to see integer literals in hexadecimal when I dump core with -ddump-simpl. For example, in a project I'm working on, I've got this:

detectNull :: Word -> Word
detectNull x = (x - repeatHexZeroOne) .&. complement x .&. repeatHexEightZero

detectArtifact :: Word -> Word -> Word
detectArtifact x artifact = detectNull (applyArtifact x artifact)

applyArtifact :: Word -> Word -> Word
applyArtifact = xor

repeatHexZeroOne :: Word
repeatHexZeroOne = div maxBound 255

repeatHexEightZero :: Word
repeatHexEightZero = 128 * (div maxBound 255 :: Word)

Once everything gets unboxed and constant-folding happens, in GHC core, the places where I used repeatHexZeroOne show 72340172838076673## (on a 64-bit machine). This is accurate, but it would be nice I could give a flag to make it show 0x0101010101010101## instead. This would make it easier for me to confirm that the arithmetic I used to generate a bit pattern actually generated what I thought it did. Admittedly, we'd probably want leading zeroes to get chopped off so that small integer literals didn't show up with 15 zeroes in front of them. So, realistically, it might show up as 0x101010101010101##. Or maybe it could always to padded with leading zeroes until the length was a power of two. Anyway, not important, but I thought it would be nice to have. Possible flag name: -ddump-hex-literals.

Change History (9)

comment:1 Changed 21 months ago by simonpj

Fine with me. Maybe Word# should always be shown in hex? But yes, a flag to control is always good.

comment:2 Changed 21 months ago by mpickering

Keywords: newcomer added

comment:3 Changed 21 months ago by andrewthad

I'm working on this now. I should have something up on phabricator soon.

comment:4 Changed 21 months ago by ulysses4ever

Owner: set to andrewthad

comment:5 Changed 21 months ago by andrewthad

I went ahead and pushed it up to github in case I somehow destroy it by accident: https://github.com/andrewthad/ghc/tree/dump-word-hex-literals

I'll get this on phabricator soon. I'm getting a weird linking error late in compilation, but I suspect that's from failing to update submodules.

comment:6 Changed 21 months ago by andrewthad

In case anyone has seen this error before:

"@LdNoGoldCmd@"  -r  -o libraries/ghc-prim/dist-install/build/HSghc-prim-  libraries/ghc-prim/dist-install/build/GHC/CString.o libraries/ghc-prim/dist-install/build/GHC/Classes.o libraries/ghc-prim/dist-install/build/GHC/Debug.o libraries/ghc-prim/dist-install/build/GHC/IntWord64.o libraries/ghc-prim/dist-install/build/GHC/Magic.o libraries/ghc-prim/dist-install/build/GHC/PrimopWrappers.o libraries/ghc-prim/dist-install/build/GHC/Tuple.o libraries/ghc-prim/dist-install/build/GHC/Types.o   libraries/ghc-prim/dist-install/build/cbits/atomic.o libraries/ghc-prim/dist-install/build/cbits/bswap.o libraries/ghc-prim/dist-install/build/cbits/clz.o libraries/ghc-prim/dist-install/build/cbits/ctz.o libraries/ghc-prim/dist-install/build/cbits/debug.o libraries/ghc-prim/dist-install/build/cbits/longlong.o libraries/ghc-prim/dist-install/build/cbits/pdep.o libraries/ghc-prim/dist-install/build/cbits/pext.o libraries/ghc-prim/dist-install/build/cbits/popcnt.o libraries/ghc-prim/dist-install/build/cbits/word2float.o   
/bin/bash: @LdNoGoldCmd@: command not found
libraries/ghc-prim/ghc.mk:4: recipe for target 'libraries/ghc-prim/dist-install/build/HSghc-prim-' failed

I'll try blowing out the directory later today and reapplying the patch to master if this isn't something that anyone else knows a simple fix for.

comment:7 Changed 21 months ago by andrewthad

Differential is on phabricator now: https://phabricator.haskell.org/D4465

comment:8 Changed 20 months ago by Ben Gamari <ben@…>

In a00b88b/ghc:

Implement -dword-hex-literals

Provide flag for showing showing Word# and Word64# as hexadecimal when
dumping GHC core.  The only affects Word, not Int, and it prefixes the
hexadecimal with enough zeroes to make the total character count a power
of two. For example:

- 0x0C0C instead of 0xC0C
- 0x00BA00BA instead of 0xBA00BA

This also affects the presentation of Word# and Word64# in GHC's error
messages. It is not expected that the flag will be used for this, but
it is a side-effect worth noting.

Test Plan: none

Reviewers: bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, mpickering, rwbarton, thomie, carter, andrewthad

GHC Trac Issues: #14872

Differential Revision: https://phabricator.haskell.org/D4465

comment:9 Changed 20 months ago by bgamari

Milestone: 8.6.1
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.