[Git][ghc/ghc][wip/reduce-type-in-stg] Add note about tagToEnum# in STG
Jaro Reinders pushed to branch wip/reduce-type-in-stg at Glasgow Haskell Compiler / GHC Commits: 87077c02 by Jaro Reinders at 2026-03-02T11:57:48+01:00 Add note about tagToEnum# in STG - - - - - 3 changed files: - compiler/GHC/CoreToStg.hs - compiler/GHC/Stg/Syntax.hs - compiler/GHC/StgToCmm/Prim.hs Changes: ===================================== compiler/GHC/CoreToStg.hs ===================================== @@ -548,8 +548,11 @@ mkStgApp f how_bound core_args stg_args res_ty else StgConApp dc NoNumber stg_args [] - -- We rewrite the TagToEnum primop to a special StgTagToEnumOp which contains information about the type constructor - PrimOpId TagToEnumOp _ -> StgOpApp (StgTagToEnumOp (tcTyConAppTyCon res_ty)) stg_args res_kind + -- We rewrite the 'tagToEnum#' primop to a special 'StgTagToEnumOp' which + -- stores the type constructor information. See Note [tagToEnum# in STG] + -- in GHC.Stg.Syntax. + PrimOpId TagToEnumOp _ -> + StgOpApp (StgTagToEnumOp (tcTyConAppTyCon res_ty)) stg_args res_kind -- Some primitive operator that might be implemented as a library call. -- As noted by Note [Eta expanding primops] in GHC.Builtin.PrimOps ===================================== compiler/GHC/Stg/Syntax.hs ===================================== @@ -766,6 +766,30 @@ StgOp An StgOp allows us to group together PrimOps and ForeignCalls. It's quite useful to move these around together, notably in StgOpApp and COpStmt. + +Note [tagToEnum# in STG] +~~~~~~~~~~~~~~~~~~~~~~~~ + +STG is untyped, but 'tagToEnum#' needs type information, so we make it a special +STG operation which stores the type constructor information alongside it. + +This happens in three main steps throughout the compiler: + +1. The type checker ensures 'tagToEnum#' is applied to a concrete type. +2. When converting Core to STG, we rewrite the 'tagToEnum#' primop to a special + 'StgTagToEnumOp' along with the type constructor info (the 'TyCon'). +3. This information is used for code generation in the back end. + +At run-time, the 'tagToEnum#' operation converts an integer to a constructor of +an enumeration data type. Given an integer, it produces a pointer to a data +constructor. Hence, we need information about where the constructors are stored +in memory. + +To preserve this information we desugar the 'tagToEnum#' primop into a special +'StgTagToEnumOp' which has an extra field to store the type constructor +information. This desugaring happens when converting Core to STG, which is the +last moment that we still have access to the type information. + -} data StgOp @@ -780,7 +804,7 @@ data StgOp -- GHC.StgToCmm.Foreign. -- See Note [Unlifted boxed arguments to foreign calls] - | StgTagToEnumOp TyCon + | StgTagToEnumOp TyCon -- See Note [tagToEnum# in STG] {- ************************************************************************ ===================================== compiler/GHC/StgToCmm/Prim.hs ===================================== @@ -1685,8 +1685,9 @@ emitPrimOp cfg primop = then Left (MO_S_Mul2 (wordWidth platform)) else Right genericIntMul2Op - -- tagToEnum# is removed in CoreToStg and rewritten to a special StgTagToEnumOp - -- See Note [?] + -- 'tagToEnum#' is removed in CoreToStg and rewritten to a special + -- 'StgTagToEnumOp' from GHC.Stg.Syntax instead. + -- See Note [tagToEnum# in STG] in GHC.Stg.Syntax TagToEnumOp -> panic "emitPrimOp: TagToEnumOp should have been gone by now" -- Out of line primops. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/87077c02e07a6c8fe7c5eab3a40bf409... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/87077c02e07a6c8fe7c5eab3a40bf409... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Jaro Reinders (@jaro)