Implement unboxed sum primitive type
ClosedPublic

Authored by osa1 on May 23 2016, 5:22 PM.

Details

Summary

This patch implements primitive unboxed sum types, as described in https://ghc.haskell.org/trac/ghc/wiki/UnpackedSumTypes.

Main changes are:

  • Add new syntax for unboxed sums types, terms and patterns. Hidden behind -XUnboxedSums.
  • Add unlifted unboxed sum type constructors and data constructors, extend type and pattern checkers and desugarer.
  • Add new RuntimeRep for unboxed sums.
  • Extend unarise pass to translate unboxed sums to unboxed tuples right before code generation.
  • Add StgRubbishArg to StgArg, and a new type CmmArg for better code generation when sum values are involved.
  • Add user manual section for unboxed sums.

Some other changes:

  • Generalize UbxTupleRep to MultiRep and UbxTupAlt to MultiValAlt to be able to use those with both sums and tuples.
  • Don't use tyConPrimRep in isVoidTy: tyConPrimRep is really wrong, given an Any TyCon, there's no way to tell what its kind is, but kindPrimRep and in turn tyConPrimRep returns PtrRep.

Not included in this patch:

  • Update Haddock for new the new unboxed sum syntax.
  • TemplateHaskell support is left as future work.

For reviewers:

  • Front-end code is mostly trivial and adapted from unboxed tuple code for type checking, pattern checking, renaming, desugaring etc.
  • Main translation routines are in RepType and UnariseStg. Documentation in UnariseStg should be enough for understanding what's going on.

Credits:

  • Johan Tibell wrote the initial front-end and interface file extensions.
  • Simon Peyton Jones reviewed this patch many times, wrote some code, and helped with debugging.

Diff Detail

Repository
rGHC Glasgow Haskell Compiler
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
There are a very large number of changes, so older changes are hidden. Show Older Changes
osa1 updated this revision to Diff 8205.Jul 15 2016, 5:03 AM
osa1 edited edge metadata.
  • comments
osa1 marked 10 inline comments as done.Jul 15 2016, 5:24 AM
osa1 added inline comments.
compiler/ghci/RtClosureInspect.hs
834–836

This is a bit tricky .. I'll leave this for now until I find an unboxed sum example that runs this code.

osa1 updated this revision to Diff 8206.Jul 15 2016, 5:25 AM
osa1 edited edge metadata.
  • comments
osa1 updated this revision to Diff 8207.Jul 15 2016, 7:14 AM
osa1 edited edge metadata.
  • minor refactoring
osa1 updated this revision to Diff 8208.Jul 15 2016, 8:47 AM
osa1 edited edge metadata.
  • Don't run sum tests with ghci -- bytecode compiler doesn't support unboxed tuples and sums
osa1 updated this revision to Diff 8209.Jul 15 2016, 9:31 AM
osa1 marked an inline comment as done.
osa1 edited edge metadata.
  • update a comment
  • minor
osa1 added a comment.Jul 15 2016, 9:34 AM

This patch currently passes slow validate.

Things left to do:

  • Get @simonpj's review (especially the parts about assertions and TODOs)
  • API-annotations. (for that I need to rebase, for that I need D2400)

Thanks for weeding through my inline comments, @osa1! Two more things/questions:

  1. Can you add a brief blurb to utils/mkUserGuidePart/Options/Language.hs about -XUnboxedSums? For example, here's what I added for -XDerivingStrategies in D2280.
  2. Do you plan to add Template Haskell support for unboxed sums in this patch, or is that future work? I ask since we had a bit of a pre-8.0 release crisis involving discontinuities between GHC features and their TH counterparts :)
osa1 added a comment.Jul 15 2016, 10:03 AM

Thanks for weeding through my inline comments, @osa1! Two more things/questions:

  1. Can you add a brief blurb to utils/mkUserGuidePart/Options/Language.hs about -XUnboxedSums? For example, here's what I added for -XDerivingStrategies in D2280.

Patch coming

  1. Do you plan to add Template Haskell support for unboxed sums in this patch, or is that future work? I ask since we had a bit of a pre-8.0 release crisis involving discontinuities between GHC features and their TH counterparts :)

Hmmm I completely forgot about that. I think we should definitely do that, but let's leave that to another patch as this is already big enough to break Phabricator.

osa1 updated this revision to Diff 8210.Jul 15 2016, 10:07 AM
osa1 edited edge metadata.
  • Update user guide generator
osa1 updated this revision to Diff 8211.Jul 15 2016, 10:27 AM
osa1 edited edge metadata.
  • Next major version is 8.3, not 8.2
RyanGlScott added inline comments.Jul 15 2016, 10:29 AM
utils/mkUserGuidePart/Options/Language.hs
732

Shouldn't this be 8.2.1?

osa1 updated this revision to Diff 8212.Jul 15 2016, 10:53 AM
osa1 edited edge metadata.
  • next major version
osa1 updated this revision to Diff 8213.Jul 15 2016, 11:12 AM
osa1 edited edge metadata.
  • get rid of flattenRepType, start using repTypeArgs
osa1 updated this revision to Diff 8227.Jul 17 2016, 7:20 AM
osa1 edited edge metadata.
  • (rebase)
  • Fix .hi stuff
osa1 marked an inline comment as done.Jul 17 2016, 7:21 AM
osa1 updated this object.Jul 17 2016, 9:56 AM
osa1 edited edge metadata.
osa1 updated this revision to Diff 8229.Jul 17 2016, 9:58 AM
  • fix some api annotations
  • minor refactoring
  • api annotations tests
osa1 updated this object.Jul 17 2016, 9:58 AM
osa1 edited edge metadata.
osa1 marked an inline comment as done.Jul 17 2016, 10:04 AM

@alanz I fixed some of the api-annotations stuff but I'm still getting these errors:

---Problems (should be empty list)---
[(AnnVbar, unboxedsums1.hs:81:25)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums2.hs:34:19)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums3.hs:32:43)]

---Problems (should be empty list)---
[]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums6.hs:35:23)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums7.hs:24:15)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums8.hs:37:15)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums9.hs:26:31)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums10.hs:15:19)]

---Problems (should be empty list)---
[(AnnVbar, unboxedsums11.hs:15:20)]

These seem to be the same error: a bar before a term is somehow causing a problem. Any ideas what may be the problem?

alanz added a comment.Jul 17 2016, 4:26 PM

API Annotations problems.

The ExplicitSum type is a variant of ExplicitTuple, but using '|' instead of comma as the separator.

The parsing for an explicit tuple has been carefully tweaked to make sure each annotation has something in the AST it attaches to. So in

| commas tup_tail
    {% do { mapM_ (\ll -> addAnnotation ll AnnComma ll) (fst $1)
          ; return
               (Tuple (map (\l -> L l missingTupArg) (fst $1) ++ $2)) } }

the addAnnotation is mapped over the SrcSpans in (fst $1), which are added to the AST in the map of L l missingTupArg over it too.

This does not happen with the ExlicitSum, as the Missing tup args are not in the data type

osa1 updated this revision to Diff 8230.Jul 18 2016, 3:26 AM
  • fix build for GHC 7.10
osa1 updated this revision to Diff 8244.Jul 18 2016, 2:56 PM
osa1 edited edge metadata.
  • Edits with simon
  • minor refactoring, some comments
simonpj accepted this revision.Jul 19 2016, 10:40 AM
simonpj edited edge metadata.
simonpj added inline comments.
compiler/basicTypes/BasicTypes.hs
134

State that it is 1-indexed

compiler/cmm/CmmExpr.hs
80

See Note [Rubbish value overview] in StgSyn

compiler/cmm/MkGraph.hs
202

This is the key bit!!!! Point that out; refer to teh overview Note

compiler/codeGen/StgCmmEnv.hs
169

For ConApp, patterns, free vars, there are no voids. May be able to delete code.

compiler/codeGen/StgCmmMonad.hs
749

Another key point!!!!

compiler/codeGen/StgCmmPrim.hs
1774

allocHeapClosure: always CmmExpArg arguments?

compiler/ghci/ByteCodeGen.hs
563

Handles any pattern with a single non-void binder; in particular I/O monad returns (# State#, a #)

compiler/simplStg/RepType.hs-boot
1 ↗(On Diff #8244)

Kill ?

compiler/stgSyn/CoreToStg.hs
542

Point the relevant Note

compiler/stgSyn/StgSyn.hs
101

Point to the Note overview

osa1 updated this object.Jul 19 2016, 11:13 AM
osa1 edited edge metadata.
osa1 updated this revision to Diff 8263.Jul 19 2016, 2:00 PM
  • Up
  • more
  • apply Simon's changes
osa1 updated this revision to Diff 8281.Jul 20 2016, 8:58 AM
osa1 edited edge metadata.
  • remove commented-out assertions
  • some comments
  • fix build
osa1 added inline comments.Jul 20 2016, 9:04 AM
compiler/simplStg/RepType.hs-boot
1 ↗(On Diff #8244)
Module imports form a cycle:
         module ‘DataCon’ (compiler/basicTypes/DataCon.hs)
        imports ‘RepType’ (compiler/simplStg/RepType.hs)
  which imports ‘TysWiredIn’ (compiler/prelude/TysWiredIn.hs)
  which imports ‘DataCon’ (compiler/basicTypes/DataCon.hs)
osa1 updated this revision to Diff 8285.Jul 20 2016, 10:26 AM
osa1 edited edge metadata.
  • Implement unboxed sums
  • Put the assertions back, get rid of the boot file
osa1 updated this revision to Diff 8292.Jul 21 2016, 3:06 AM
osa1 edited edge metadata.

rebase

This revision was automatically updated to reflect the committed changes.