Skip to content

Commit

Permalink
Remove Algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
dougalm committed May 7, 2024
1 parent 7d21536 commit 2b6d5b3
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 319 deletions.
1 change: 0 additions & 1 deletion dex.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ flag debug

library
exposed-modules: AbstractSyntax
, Algebra
, Builder
, CUDA
, CheapReduction
Expand Down
247 changes: 0 additions & 247 deletions src/lib/Algebra.hs

This file was deleted.

74 changes: 3 additions & 71 deletions src/lib/Imp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Imp
, repValFromFlatList, addImpTracing
-- These are just for the benefit of serialization/printing. otherwise we wouldn't need them
, BufferType (..), IdxNest, IndexStructure, IExprInterpretation (..), typeToTree
, computeOffset, getIExprInterpretation
, getIExprInterpretation
, isSingletonType, singletonTypeVal
) where

Expand All @@ -28,7 +28,6 @@ import Control.Monad.Writer.Strict
import Control.Monad.State.Strict hiding (State)
import qualified Control.Monad.State.Strict as MTL

import Algebra
import Builder
import CheapReduction
import CheckType (CheckableE (..))
Expand Down Expand Up @@ -854,16 +853,7 @@ buildGarbageVal ty =
-- === Operations on dests ===

indexDest :: Emits n => Dest n -> SAtom n -> SubstImpM i n (Dest n)
indexDest (Dest (TyCon (TabPi tabTy)) tree) i = do
eltTy <- instantiate tabTy [i]
ord <- ordinalImp (tabIxType tabTy) i
leafTys <- typeToTree $ toType tabTy
Dest eltTy <$> forM (zipTrees leafTys tree) \(leafTy, ptr) -> do
BufferType ixStruct _ <- return $ getRefBufferType leafTy
offset <- computeOffsetImp ixStruct ord
impOffset ptr offset
indexDest _ _ = error "expected a reference to a table"
{-# INLINE indexDest #-}
indexDest (Dest (TyCon (TabPi tabTy)) tree) i = undefined

projectDest :: Int -> Dest n -> Dest n
projectDest i (Dest (TyCon (ProdType tys)) (Branch ds)) =
Expand All @@ -876,52 +866,14 @@ type SBuilderM = BuilderM SimpIR

computeElemCountImp :: Emits n => IndexStructure SimpIR n -> SubstImpM i n (IExpr n)
computeElemCountImp Singleton = return $ IIdxRepVal 1
computeElemCountImp idxs = do
result <- liftBuilderImp do
idxs' <- sinkM idxs
computeElemCount idxs'
fromScalarAtom result

computeOffsetImp
:: Emits n => IndexStructure SimpIR n -> IExpr n -> SubstImpM i n (IExpr n)
computeOffsetImp idxs ixOrd = do
let ixOrd' = toScalarAtom ixOrd
result <- liftBuilderImp do
PairE idxs' ixOrd'' <- sinkM $ PairE idxs ixOrd'
computeOffset idxs' ixOrd''
fromScalarAtom result
computeElemCountImp _ = undefined

computeElemCount :: Emits n => IndexStructure SimpIR n -> SBuilderM n (Atom SimpIR n)
computeElemCount (EmptyAbs Empty) =
-- XXX: this optimization is important because we don't want to emit any decls
-- in the case that we don't have any indices. The more general path will
-- still compute `1`, but it might emit decls along the way.
return $ IdxRepVal 1
computeElemCount idxNest' = do
let (idxList, idxNest) = indexStructureSplit idxNest'
sizes <- forM idxList indexSetSize
listSize <- foldM imul (IdxRepVal 1) sizes
nestSize <- elemCountPoly idxNest
imul listSize nestSize

elemCountPoly :: Emits n => IndexStructure SimpIR n -> SBuilderM n (Atom SimpIR n)
elemCountPoly (Abs bs UnitE) = case bs of
Empty -> return $ IdxRepVal 1
Nest b@(PairB (LiftB d) (_:>t)) rest -> do
curSize <- indexSetSize $ IxType t d
restSizes <- computeSizeGivenOrdinal b $ EmptyAbs rest
sumUsingPolysImp curSize restSizes

computeSizeGivenOrdinal
:: EnvReader m
=> IxBinder SimpIR n l -> IndexStructure SimpIR l
-> m n (Abs SBinder SExpr n)
computeSizeGivenOrdinal (PairB (LiftB d) (b:>t)) idxStruct = liftBuilder do
withFreshBinder noHint IdxRepTy \bOrdinal ->
Abs bOrdinal <$> buildBlock do
i <- unsafeFromOrdinal (sink $ IxType t d) $ toAtom $ sink $ binderVar bOrdinal
idxStruct' <- applySubst (b@>SubstVal i) idxStruct
elemCountPoly $ sink idxStruct'

-- Split the index structure into a prefix of non-dependent index types
-- and a trailing nest of indices that can contain inter-dependencies.
Expand All @@ -933,26 +885,6 @@ indexStructureSplit s@(Abs (Nest (PairB (LiftB d) b) rest) UnitE) =
HoistSuccess rest' -> (IxType (binderType b) d:ans1, ans2)
where (ans1, ans2) = indexStructureSplit rest'

computeOffset :: forall n. Emits n
=> IndexStructure SimpIR n -> SAtom n -> SBuilderM n (SAtom n)
computeOffset (EmptyAbs (Nest _ Empty)) i = return i -- optimization
computeOffset (EmptyAbs (Nest b idxs)) idxOrdinal = do
case hoist b (EmptyAbs idxs) of
HoistFailure _ -> do
rhsElemCounts <- computeSizeGivenOrdinal b (EmptyAbs idxs)
sumUsingPolysImp idxOrdinal rhsElemCounts
HoistSuccess idxs' -> do
stride <- computeElemCount idxs'
idxOrdinal `imul` stride
computeOffset _ _ = error "Expected a nonempty nest of idx binders"

sumUsingPolysImp
:: Emits n => SAtom n
-> Abs SBinder SExpr n -> BuilderM SimpIR n (SAtom n)
sumUsingPolysImp lim (Abs i body) = do
ab <- hoistDecls i body
sumUsingPolys lim ab

hoistDecls
:: ( Builder SimpIR m, EnvReader m, Emits n
, BindsNames b, BindsEnv b, RenameB b, SinkableB b)
Expand Down

0 comments on commit 2b6d5b3

Please sign in to comment.