/compiler/vectorise/Vectorise/Type/Classify.hs

https://github.com/pepeiborra/ghc · Haskell · 103 lines · 55 code · 15 blank · 33 comment · 4 complexity · b0fc2d1fdc96e1b58110c1d93644e8e3 MD5 · raw file

  1. -- Extract from a list of type constructors those (1) which need to be vectorised and (2) those
  2. -- that could be, but need not be vectorised (as a scalar representation is sufficient and more
  3. -- efficient). The type constructors that cannot be vectorised will be dropped.
  4. --
  5. -- A type constructor will only be vectorised if it is
  6. --
  7. -- (1) a data type constructor, with vanilla data constructors (i.e., data constructors admitted by
  8. -- Haskell 98) and
  9. -- (2) at least one of the type constructors that appears in its definition is also vectorised.
  10. --
  11. -- If (1) is met, but not (2), the type constructor may appear in vectorised code, but there is no
  12. -- need to vectorise that type constructor itself. This holds, for example, for all enumeration
  13. -- types. As '([::])' is being vectorised, any type constructor whose definition involves
  14. -- '([::])', either directly or indirectly, will be vectorised.
  15. module Vectorise.Type.Classify (
  16. classifyTyCons
  17. ) where
  18. import UniqSet
  19. import UniqFM
  20. import DataCon
  21. import TyCon
  22. import TypeRep
  23. import Type
  24. import Digraph
  25. -- |From a list of type constructors, extract those thatcan be vectorised, returning them in two
  26. -- sets, where the first result list /must be/ vectorised and the second result list /need not be/
  27. -- vectroised.
  28. -- The first argument determines the /conversion status/ of external type constructors as follows:
  29. --
  30. -- * tycons which have converted versions are mapped to 'True'
  31. -- * tycons which are not changed by vectorisation are mapped to 'False'
  32. -- * tycons which can't be converted are not elements of the map
  33. --
  34. classifyTyCons :: UniqFM Bool -- ^type constructor conversion status
  35. -> [TyCon] -- ^type constructors that need to be classified
  36. -> ([TyCon], [TyCon]) -- ^tycons to be converted & not to be converted
  37. classifyTyCons convStatus tcs = classify [] [] convStatus (tyConGroups tcs)
  38. where
  39. classify conv keep _ [] = (conv, keep)
  40. classify conv keep cs ((tcs, ds) : rs)
  41. | can_convert && must_convert
  42. = classify (tcs ++ conv) keep (cs `addListToUFM` [(tc, True) | tc <- tcs]) rs
  43. | can_convert
  44. = classify conv (tcs ++ keep) (cs `addListToUFM` [(tc, False) | tc <- tcs]) rs
  45. | otherwise
  46. = classify conv keep cs rs
  47. where
  48. refs = ds `delListFromUniqSet` tcs
  49. can_convert = isNullUFM (refs `minusUFM` cs) && all convertable tcs
  50. must_convert = foldUFM (||) False (intersectUFM_C const cs refs)
  51. convertable tc = isDataTyCon tc && all isVanillaDataCon (tyConDataCons tc)
  52. -- Used to group type constructors into mutually dependent groups.
  53. --
  54. type TyConGroup = ([TyCon], UniqSet TyCon)
  55. -- Compute mutually recursive groups of tycons in topological order.
  56. --
  57. tyConGroups :: [TyCon] -> [TyConGroup]
  58. tyConGroups tcs = map mk_grp (stronglyConnCompFromEdgedVertices edges)
  59. where
  60. edges = [((tc, ds), tc, uniqSetToList ds) | tc <- tcs
  61. , let ds = tyConsOfTyCon tc]
  62. mk_grp (AcyclicSCC (tc, ds)) = ([tc], ds)
  63. mk_grp (CyclicSCC els) = (tcs, unionManyUniqSets dss)
  64. where
  65. (tcs, dss) = unzip els
  66. -- |Collect the set of TyCons used by the representation of some data type.
  67. --
  68. tyConsOfTyCon :: TyCon -> UniqSet TyCon
  69. tyConsOfTyCon = tyConsOfTypes . concatMap dataConRepArgTys . tyConDataCons
  70. -- |Collect the set of TyCons that occur in these types.
  71. --
  72. tyConsOfTypes :: [Type] -> UniqSet TyCon
  73. tyConsOfTypes = unionManyUniqSets . map tyConsOfType
  74. -- |Collect the set of TyCons that occur in this type.
  75. --
  76. tyConsOfType :: Type -> UniqSet TyCon
  77. tyConsOfType ty
  78. | Just ty' <- coreView ty = tyConsOfType ty'
  79. tyConsOfType (TyVarTy _) = emptyUniqSet
  80. tyConsOfType (TyConApp tc tys) = extend (tyConsOfTypes tys)
  81. where
  82. extend | isUnLiftedTyCon tc
  83. || isTupleTyCon tc = id
  84. | otherwise = (`addOneToUniqSet` tc)
  85. tyConsOfType (AppTy a b) = tyConsOfType a `unionUniqSets` tyConsOfType b
  86. tyConsOfType (FunTy a b) = (tyConsOfType a `unionUniqSets` tyConsOfType b)
  87. `addOneToUniqSet` funTyCon
  88. tyConsOfType (ForAllTy _ ty) = tyConsOfType ty