Opened 8 years ago

Closed 8 years ago

#5423 closed bug (fixed)

Registers above R8 corrupt when called through prim import

Reported by: pumpkin Owned by: igloo
Priority: high Milestone: 7.4.1
Component: Compiler Version: 7.2.1
Keywords: Cc: ekmett@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


I write a cmm function:

  foreign "C" printf("%d %d %d %d %d %d %d %d %d\n", R1, R2, R3, R4, R5, R6, R7, R8, R9);

and a binding to it in Haskell:

foreign import prim "primbug" primbug :: Int# -> Int# -> Int# -> Int# 
                                      -> Int# -> Int# -> Int# -> Int#
                                      -> Int#
                                      -> Int#

-- This should resemble id
bug :: Int -> Int
bug (I# i) = I# (primbug 0# 111# 222# 333# 444# 555# 666# 777# i)

main = print (bug 888)

and the program outputs:

0 111 222 333 444 555 666 777 0

The contents of R9 have been lost somehow! The result of this corruption isn't always 0, if that helps track it down: in my more complicated use case, I'm getting smallish numbers (two hex digits) instead of a legitimate pointer.

I'm attaching a small cabal package to help reproduce it, because it's a pain to build.

Attachments (1)

primbug-0.1.tar.gz (1.5 KB) - added by pumpkin 8 years ago.

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by pumpkin

Attachment: primbug-0.1.tar.gz added

comment:1 Changed 8 years ago by ekmett

Cc: ekmett@… added

comment:2 Changed 8 years ago by pumpkin

This bug is, like #5422, caused by pervasive assumptions in the GHC source that no more than 10 (or even 8 or 6 in some places – it isn't consistent) VanillaRegs will be used.

comment:3 Changed 8 years ago by ezyang

At the very least the compiler should complain about foreign prims that have too many arguments.

I think what we actually want here for the intended application (integer-gmp style bindings) is some sort of facility for inlinable C--. In this case, we can reuse the local registers that already hold all of these values, for only a small code increase.

comment:4 Changed 8 years ago by igloo

Milestone: 7.4.1
Priority: normalhigh

Let's give a better error in 7.4.

comment:5 Changed 8 years ago by igloo

Owner: set to igloo

comment:6 Changed 8 years ago by igloo@…

commit d535ef006d85dbdb7cda2b09c5bc35cb80108909

Author: Ian Lynagh <>
Date:   Sun Nov 6 18:11:56 2011 +0000

    Allow the use of R9 and R10 in primops; fixes trac #5423

 compiler/codeGen/CgUtils.hs          |   12 ++++++++++++
 compiler/codeGen/StgCmmUtils.hs      |    6 ++++++
 compiler/nativeGen/PPC/Regs.hs       |    6 ++++++
 compiler/nativeGen/SPARC/RegPlate.hs |    6 ++++++
 compiler/nativeGen/X86/Regs.hs       |    6 ++++++
 includes/rts/Constants.h             |    2 +-
 includes/stg/MachRegs.h              |    6 +++++-
 includes/stg/Regs.h                  |    6 ++++++
 8 files changed, 48 insertions(+), 2 deletions(-)

comment:7 Changed 8 years ago by igloo

Resolution: fixed
Status: newclosed

10 regs can now be used.

Note: See TracTickets for help on using tickets.