Skip to content

Commit

Permalink
Return boxed type in unpack{Float,Double}#
Browse files Browse the repository at this point in the history
In the primitives unpackFloat# and unpackDouble#, the result from
the primitive evaluator was an unboxed literal (i.e. Float#) when
the actual result of the function is a boxed literal (i.e. Float).
This led to #2097 having core that looked like

```
case 1.0# of
  F# x -> ...
```

which will rightly never match. When the primitives instead return
the correct type we get a case expression

```
case F# 1.0# of
  F# x -> ...
```

where the caseCon transformation can fire.
  • Loading branch information
Alex McKenna committed Feb 25, 2022
1 parent 6cf919a commit bc0f4b3
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions clash-ghc/src-ghc/Clash/GHC/Evaluator/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1788,11 +1788,15 @@ ghcPrimStep tcm isSubj pInfo tys args mach = case primName pInfo of

"Clash.Class.BitPack.Internal.unpackFloat#"
| [i] <- bitVectorLiterals' args
-> reduce . Literal . FloatLiteral $ unpack (toBV i :: BitVector 32)
-> let resTy = getResultTy tcm ty tys
val = unpack (toBV i :: BitVector 32)
in reduce (mkFloatCLit val resTy)

"Clash.Class.BitPack.Internal.unpackDouble#"
| [i] <- bitVectorLiterals' args
-> reduce . Literal . DoubleLiteral $ unpack (toBV i :: BitVector 64)
-> let resTy = getResultTy tcm ty tys
val = unpack (toBV i :: BitVector 64)
in reduce (mkDoubleCLit val resTy)

-- expIndex#
-- :: KnownNat m
Expand Down Expand Up @@ -4186,6 +4190,30 @@ bitVectorLitIntLit tcm tys args
| otherwise
= Nothing

mkFloatCLit :: Word32 -> Type -> Term
mkFloatCLit lit resTy =
App (Prim info) (Literal (FloatLiteral lit))
where
info = PrimInfo
{ primName = "GHC.Types.F#"
, primType = mkFunTy floatPrimTy resTy
, primWorkInfo = WorkNever
, primMultiResult = SingleResult
, primUnfolding = NoUnfolding
}

mkDoubleCLit :: Word64 -> Type -> Term
mkDoubleCLit lit resTy =
App (Prim info) (Literal (DoubleLiteral lit))
where
info = PrimInfo
{ primName = "GHC.Types.D#"
, primType = mkFunTy doublePrimTy resTy
, primWorkInfo = WorkNever
, primMultiResult = SingleResult
, primUnfolding = NoUnfolding
}

-- From an argument list to function of type
-- forall n. KnownNat n => ...
-- extract (nTy,nInt)
Expand Down

0 comments on commit bc0f4b3

Please sign in to comment.