Skip to content

Commit b5f2c2c

Browse files
committed
Query rather than check types of scalar operations in Imp.
1 parent 9ed664c commit b5f2c2c

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/lib/Imp.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import MTL1
2929
import Name
3030
import Builder
3131
import Syntax
32-
import CheckType (CheckableE (..), checkUnOp, checkBinOp)
32+
import CheckType (CheckableE (..))
3333
import Simplify
3434
import LabeledItems
3535
import QueryType
@@ -1590,9 +1590,10 @@ impInstrTypes instr = case instr of
15901590
-- TODO: reuse type rules in Type.hs
15911591
impOpType :: IPrimOp n -> IType
15921592
impOpType pop = case pop of
1593-
ScalarBinOp op x y -> runHardFail $ checkBinOp op (getIType x) (getIType y)
1594-
ScalarUnOp op x -> runHardFail $ checkUnOp op (getIType x)
1595-
VectorBinOp op x y -> runHardFail $ checkBinOp op (getIType x) (getIType y)
1593+
ScalarBinOp op x _ -> typeBinOp op (getIType x)
1594+
-- All unary ops preserve the type of their input
1595+
ScalarUnOp _ x -> getIType x
1596+
VectorBinOp op x _ -> typeBinOp op (getIType x)
15961597
Select _ x _ -> getIType x
15971598
VectorPack xs -> Vector ty where Scalar ty = getIType $ head xs
15981599
VectorIndex x _ -> Scalar ty where Vector ty = getIType x

src/lib/QueryType.hs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module QueryType (
88
instantiateDataDef, instantiateDepPairTy, instantiatePi, instantiateTabPi,
99
litType, lamExprTy,
1010
numNaryPiArgs, naryLamExprType,
11-
oneEffect, projectLength, sourceNameType, typeAsBinderNest
11+
oneEffect, projectLength, sourceNameType, typeAsBinderNest, typeBinOp
1212
) where
1313

1414
import Control.Monad
@@ -203,6 +203,20 @@ typeAsBinderNest ty = do
203203
return $ Abs (Nest (ignored:>ty) Empty) body
204204
{-# INLINE typeAsBinderNest #-}
205205

206+
typeBinOp :: BinOp -> BaseType -> BaseType
207+
typeBinOp binop xTy = case binop of
208+
IAdd -> xTy; ISub -> xTy
209+
IMul -> xTy; IDiv -> xTy
210+
IRem -> xTy;
211+
ICmp _ -> Scalar Word8Type
212+
FAdd -> xTy; FSub -> xTy
213+
FMul -> xTy; FDiv -> xTy;
214+
FPow -> xTy
215+
FCmp _ -> Scalar Word8Type
216+
BAnd -> xTy; BOr -> xTy
217+
BXor -> xTy
218+
BShL -> xTy; BShR -> xTy
219+
206220
-- === computing effects ===
207221

208222
computeAbsEffects :: (EnvExtender m, SubstE Name e)
@@ -459,19 +473,7 @@ getTypePrimOp op = case op of
459473
TabCon ty _ -> substM ty
460474
ScalarBinOp binop x _ -> do
461475
xTy <- getTypeBaseType x
462-
resTy <- return $ case binop of
463-
IAdd -> xTy; ISub -> xTy
464-
IMul -> xTy; IDiv -> xTy
465-
IRem -> xTy;
466-
ICmp _ -> Scalar Word8Type
467-
FAdd -> xTy; FSub -> xTy
468-
FMul -> xTy; FDiv -> xTy;
469-
FPow -> xTy
470-
FCmp _ -> Scalar Word8Type
471-
BAnd -> xTy; BOr -> xTy
472-
BXor -> xTy
473-
BShL -> xTy; BShR -> xTy
474-
return $ TC $ BaseType resTy
476+
return $ TC $ BaseType $ typeBinOp binop xTy
475477
-- All unary ops preserve the type of the input
476478
ScalarUnOp _ x -> getTypeE x
477479
Select _ x _ -> getTypeE x

0 commit comments

Comments
 (0)