Opened 7 months ago

Last modified 7 months ago

#8317 new task

Optimize tagToEnum# at Core level

Reported by: jstolarek Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.7
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By: #8326
Blocking: Related Tickets: #6135


Old comparison primops that returned Bool were implemented by inserting call to tagToEnum# in the code generator. This call to tagToEnum# was later optimized away by a special case in the code geenrator (see compiler/codeGen/StgCmmExpr.hs, Note [case on bool])). Now that we have new comparison primops (see #6135) we no longer insert tagToEnum# in the code generator - all uses of tagToEnum# come from explicit calls in a source program. But we still have that special case in the code generator to optimize away tagToEnum#. We should drop that special case in the code generator and handle scrutinizing of tagToEnum# at the Core level by:

  1. removing call to tagToEnum# in the scrutinee
  2. adding calls to dataToTag# in case branches
  3. constant-folding inserted dataToTag#

Here is an example. This Haskell code:

if tagToEnum# (a ># b)
then E1
else E2

will be translated to this Core:

case tagToEnum# (a ># b) of
  True  -> E1
  False -> E2

and optimized like this:

case a ># b of
  dataToTag# True  -> E1
  dataToTag# False -> E2


case a ># b of
  1 -> E1
  0 -> E2

Change History (4)

comment:1 Changed 7 months ago by jstolarek

  • Blocked By 8326 added

comment:2 Changed 7 months ago by Simon Peyton Jones <simonpj@…>

In 62c405854afbeb6dabdaf5c737a2d7f625a2b3cb/ghc:

Optimise (case tagToEnum# x of ..) as in Trac #8317

See Note [Optimising tagToEnum#] in Simplify

comment:3 Changed 7 months ago by Jan Stolarek <jan.stolarek@…>

In 53948f915140396acd1b80c6a7a252b2d1e12635/ghc:

Restore old names of comparison primops

In 6579a6c we removed existing comparison primops and introduced new ones
returning Int# instead of Bool. This commit (and associated commits in
array, base, dph, ghc-prim, integer-gmp, integer-simple, primitive, testsuite and
template-haskell) restores old names of primops. This allows us to keep
our API cleaner at the price of not having backwards compatibility.

This patch also temporalily disables fix for #8317 (optimization of
tagToEnum# at Core level). We need to fix #8326 first, otherwise
our primops code will be very slow.

comment:4 Changed 7 months ago by jstolarek

Simon's fix is temporalily commented out, until we solve #8326. Once we've done that we have to uncomment the code.

Note: See TracTickets for help on using tickets.