@@ -43,7 +43,7 @@ optimize = dceTop -- Clean up user code
4343
4444-- === Peephole optimizations ===
4545
46- peepholeOp :: PrimOp SimpIR o -> EnvReaderM o (Either ( SAtom o ) ( PrimOp SimpIR o ) )
46+ peepholeOp :: PrimOp SimpIR o -> EnvReaderM o (SExpr o )
4747peepholeOp op = case op of
4848 MiscOp (CastOp (BaseTy (Scalar sTy)) (Con (Lit l))) -> return $ case foldCast sTy l of
4949 Just l' -> lit l'
@@ -72,14 +72,14 @@ peepholeOp op = case op of
7272 return $ lit $ Word8Lit $ lv .|. rv
7373 BinOp BAnd (Con (Lit (Word8Lit lv))) (Con (Lit (Word8Lit rv))) ->
7474 return $ lit $ Word8Lit $ lv .&. rv
75- MiscOp (ToEnum ty (Con (Lit (Word8Lit tag)))) -> Left <$> case ty of
76- SumTy cases -> return $ SumVal cases (fromIntegral tag) UnitVal
75+ MiscOp (ToEnum ty (Con (Lit (Word8Lit tag)))) -> case ty of
76+ SumTy cases -> return $ Atom $ SumVal cases (fromIntegral tag) UnitVal
7777 _ -> error " Ill typed ToEnum?"
78- MiscOp (SumTag (SumVal _ tag _)) -> return $ lit $ Word8Lit $ fromIntegral tag
78+ MiscOp (SumTag (SumVal _ tag _)) -> return $ lit $ Word8Lit $ fromIntegral tag
7979 _ -> return noop
8080 where
81- noop = Right op
82- lit = Left . Con . Lit
81+ noop = PrimOp op
82+ lit = Atom . Con . Lit
8383
8484 cmp :: Ord a => CmpOp -> a -> a -> Bool
8585 cmp = \ case
@@ -188,9 +188,9 @@ foldCast sTy l = case sTy of
188188 compare (0 - countTrailingZeros (round @ b @ a a))
189189 (0 - countTrailingZeros (round @ b @ a b))
190190
191- peepholeExpr :: SExpr o -> EnvReaderM o (Either ( SAtom o ) ( SExpr o ) )
191+ peepholeExpr :: SExpr o -> EnvReaderM o (SExpr o )
192192peepholeExpr expr = case expr of
193- PrimOp op -> fmap PrimOp <$> peepholeOp op
193+ PrimOp op -> peepholeOp op
194194 TabApp (Var t) [IdxRepVal ord] ->
195195 lookupAtomName t <&> \ case
196196 LetBound (DeclBinding ann _ (TabCon Nothing tabTy elems))
@@ -199,12 +199,12 @@ peepholeExpr expr = case expr of
199199 -- For example, it might be coming from an unsafe_from_ordinal that is
200200 -- under a case branch that would be dead for all invalid indices.
201201 if 0 <= ord && fromIntegral ord < length elems
202- then Left $ elems !! fromIntegral ord
203- else Right expr
204- _ -> Right expr
202+ then Atom $ elems !! fromIntegral ord
203+ else expr
204+ _ -> expr
205205 -- TODO: Apply a function to literals when it has a cheap body?
206206 -- Think, partial evaluation of threefry.
207- _ -> return $ Right expr
207+ _ -> return expr
208208 where isFinTabTy = \ case
209209 TabPi (TabPiType (_:> (IxType _ (IxDictRawFin _))) _) -> True
210210 _ -> False
@@ -277,9 +277,8 @@ ulExpr expr = case expr of
277277 _ -> nothingSpecial
278278 where
279279 inc i = modify \ (ULS n) -> ULS (n + i)
280- nothingSpecial = inc 1 >> (visitGeneric expr >>= liftEnvReaderM . peepholeExpr) >>= \ case
281- Left x -> return x
282- Right e -> emitExpr e
280+ nothingSpecial = inc 1 >> (visitGeneric expr >>= liftEnvReaderM . peepholeExpr)
281+ >>= emitExprToAtom
283282 unrollBlowupThreshold = 12
284283 withLocalAccounting m = do
285284 oldCost <- get
0 commit comments