Opened 4 years ago

Last modified 4 years ago

#10555 new bug

RULE left-hand side too complicated to desugar

Reported by: yongqli Owned by:
Priority: low Milestone:
Component: Compiler Version: 7.0.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: yes
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by thomie)

GHC reports multiple "RULE left-hand side too complicated to desugar" warnings for the fast-math package.

Here is an testcase, extracted from fast-math/Numeric/FastMath/Approximation.hs:

{-# LANGUAGE MagicHash #-}
module T10555 where

import GHC.Exts

{-# RULES

"double /,+ distribute" forall x y1 y2. (y1 /## x) +## (y2 /## x) 
    = (y1 +## y2) /## x

"double /,- distribute" forall x y1 y2. (y1 /## x) -## (y2 /## x) 
    = (y1 -## y2) /## x

"float /,- distribute" forall x y1 y2. (y1 `divideFloat#` x) `minusFloat#` (y2 `divideFloat#` x) 
    = (y1 `minusFloat#` y2) `divideFloat#` x

  #-}
2$ ghc-7.10.2 T10555.hs  -fforce-recomp
[1 of 1] Compiling T10555           ( T10555.hs, T10555.o )

T10555.hs:8:1: Warning:
    RULE left-hand side too complicated to desugar
      Optimised lhs: case /## y2 x of wild_00 { __DEFAULT ->
                     (case /## y1 x of wild_X2 { __DEFAULT -> +## wild_X2 }) wild_00
                     }
      Orig lhs: case /## y2 x of wild_00 { __DEFAULT ->
                (case /## y1 x of wild_00 { __DEFAULT -> +## wild_00 }) wild_00
                }

T10555.hs:11:1: Warning:
    RULE left-hand side too complicated to desugar
      Optimised lhs: case /## y2 x of wild_00 { __DEFAULT ->
                     (case /## y1 x of wild_X2 { __DEFAULT -> -## wild_X2 }) wild_00
                     }
      Orig lhs: case /## y2 x of wild_00 { __DEFAULT ->
                (case /## y1 x of wild_00 { __DEFAULT -> -## wild_00 }) wild_00
                }

T10555.hs:14:1: Warning:
    RULE left-hand side too complicated to desugar
      Optimised lhs: case divideFloat# y2 x of wild_00 { __DEFAULT ->
                     (case divideFloat# y1 x of wild_X2 { __DEFAULT ->
                      minusFloat# wild_X2
                      })
                       wild_00
                     }
      Orig lhs: case divideFloat# y2 x of wild_00 { __DEFAULT ->
                (case divideFloat# y1 x of wild_00 { __DEFAULT ->
                 minusFloat# wild_00
                 })
                  wild_00
                }

Change History (8)

comment:1 Changed 4 years ago by yongqli

Description: modified (diff)

comment:3 Changed 4 years ago by thomie

Resolution: duplicate
Status: newclosed

Thank you for the report. I analysed the problem in #10699. I also opened a pull request to fast-math that will make the package compile with ghc-7.10.2 (and HEAD). Maybe you could comment there if it works for you. Otherwise, please comment in #10699.

comment:4 Changed 4 years ago by simonpj

I did look at the "lhs too complicated to desugar" issue. There's a good reason for it, and I'm not sure what to do.

The issue is this. Most numeric primops are pure and can't fail, so they can be subject to aggressive code motion. Division, unlike most numeric primops, can fail. So GHC restricts its code motion by case-binding it. In particular something like

  a /## (b /## c)

doesn't obey the "let/app invariant" (see CoreSyn.hs) and gets turned into

  case b /## c of r -> a /## r

But the LHS of a rule is supposed to look like f e1 .. en.

Maybe the let/app invariant is too strong -- after all, we only support imprecise exceptions for things like divide-by-zero. But if you say

  if x >## 3## then
     y /## x
  else ...

you jolly well don't expect the (y /## x) to be executed until after testing x >## 3, so we need to restrict code motion.

Not clear what to do here. Avoid division primops on the LHS of a rule!

comment:5 Changed 4 years ago by simonpj

Resolution: duplicate
Status: closednew

PS: this is not the same issue as #10699.

comment:6 Changed 4 years ago by thomie

Description: modified (diff)
Version: 7.10.2-rc17.0.4

I changed the description to not include the panic from #10699.

comment:7 Changed 4 years ago by thomie

Priority: normallow

comment:8 Changed 4 years ago by thomie

Description: modified (diff)
Note: See TracTickets for help on using tickets.