Skip to content

Commit d722df2

Browse files
committed
Simplify the interface to peephole optimizations.
To wit, they should always return an Expr, which is may sometimes use the Atom contructor. Then the caller can use emitExprToAtom if it does not wish to emit the Atom.
1 parent ab43029 commit d722df2

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/lib/Optimize.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
4747
peepholeOp 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)
192192
peepholeExpr 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

src/lib/Simplify.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,7 @@ simplifyGenericOp op = do
758758
(substM >=> getRepType)
759759
(simplifyAtom >=> toDataAtomIgnoreRecon)
760760
(error "shouldn't have lambda left")
761-
result <- liftEnvReaderM (peepholeOp $ toPrimOp op') >>= \case
762-
Left a -> return a
763-
Right op'' -> emitOp op''
761+
result <- liftEnvReaderM (peepholeOp $ toPrimOp op') >>= emitExprToAtom
764762
liftSimpAtom ty result
765763
{-# INLINE simplifyGenericOp #-}
766764

0 commit comments

Comments
 (0)