Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3690e54
Add Ksc.CatLang
tomjaguarpaw May 14, 2020
a709fb8
theDefsViaCatLang
tomjaguarpaw May 14, 2020
e445646
wsp
tomjaguarpaw May 14, 2020
9192778
Split off displayCppGenDefsDiffs
tomjaguarpaw May 14, 2020
1d562f5
Inline displayCppGenDiffs
tomjaguarpaw May 14, 2020
4ce0017
Split off displayCppGenAndCompileDefsDiffs
tomjaguarpaw May 14, 2020
eba79f6
Add displayCppGenCompileAndRunViaCatLang
tomjaguarpaw May 14, 2020
69cecfd
Split off testRunKSVia
tomjaguarpaw May 14, 2020
8ab6dd1
Add tests for gmm.ks and fold.ks that go roundtripped via CatLang
tomjaguarpaw May 14, 2020
aa04057
Correct spelling
tomjaguarpaw Apr 14, 2020
821cd02
DO NOT MERGE Not useful resolve conflict
tomjaguarpaw Apr 7, 2020
0562166
Whitespace
tomjaguarpaw Apr 7, 2020
9dd25ad
Don't use underscores in notInScope
tomjaguarpaw Apr 8, 2020
582f6c0
Whitespace
tomjaguarpaw Apr 8, 2020
bed7736
Rename so that nomenclature is more accurate post one-arg
tomjaguarpaw Apr 8, 2020
c5a0b22
Define and use tVarName
tomjaguarpaw Apr 8, 2020
b02d375
Rename nameOfVar to varName
tomjaguarpaw Apr 8, 2020
e95d38a
Informal pretty printer: remove spaces around args and unpack tuples
tomjaguarpaw Apr 8, 2020
7f5882e
Use backticks around infix functions
tomjaguarpaw Apr 8, 2020
2b5ffbc
Use parens when necessary in the rhs of lets?
tomjaguarpaw Apr 8, 2020
00ad9d3
Optimise selection functions when they are applied
tomjaguarpaw Apr 14, 2020
04b9e3b
Add optDrvFun
tomjaguarpaw Apr 14, 2020
a994035
Improve AD.tex
Apr 22, 2020
bfa8d9f
Simpler count
tomjaguarpaw Apr 29, 2020
cf3d525
Start on Elliotisation branch
tomjaguarpaw May 14, 2020
244262e
Add fwdt$ for div@ii, mul@ii and to_float
tomjaguarpaw Apr 14, 2020
545b700
Make Prune into a lone function
May 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified doc/AD.pdf
Binary file not shown.
265 changes: 237 additions & 28 deletions doc/AD.tex

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions knossos.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ executable ksc
CSE
Cgen
KMonad
Ksc.CatLang
Ksc.Futhark
Ksc.Pipeline
Lang
Expand Down
10 changes: 9 additions & 1 deletion src/ksc/AD.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ gradBuild TupleAD s n ti body
grad_body = mkLet (gradTVar TupleAD s ti)
(Tuple [Var ti, lmZero s (Var ti)]) $
gradE TupleAD s body
gradBuild SplitAD _ _ _ _ = error "gradBuild:SplitAD" -- Doesn't make sense

---------------
gradFold :: ADPlan -> Shape -> TVar -> TExpr -> TExpr -> TExpr -> TExpr
gradFold BasicAD s ti body acc v =
Expand Down Expand Up @@ -156,6 +158,8 @@ gradFold TupleAD s _ti _body acc _v =
lmDummyFold (TypeTuple [t_acc, TypeLM (typeof s) t_acc])
where t_acc = typeof acc

gradFold SplitAD _ _ _ _ _ = error "gradFold:SplitAD" -- Doesn't make sense

---------------
gradCall :: ADPlan -> Shape -> TFun -> TExpr -> TExpr
gradCall BasicAD s f args
Expand Down Expand Up @@ -194,6 +198,8 @@ gradCall TupleAD s f args
grad_arg = gradE TupleAD s arg
arg = args

gradCall SplitAD _ _ _ = error "gradCall:SplitAD" -- Doesn't make sense

----------------------
gradLet :: HasCallStack => ADPlan -> Shape -> TVar -> TExpr -> TExpr -> TExpr
gradLet BasicAD s v e1 e2
Expand All @@ -212,6 +218,8 @@ gradLet TupleAD s v e1 e2
gradE TupleAD s e2
where dv = gradTVar TupleAD s v

gradLet SplitAD _ _ _ _ = error "gradLet:SplitAD" -- Doesn't make sense

{- Note [Shadowing after grad]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We transform let x = <rhs>
Expand All @@ -234,7 +242,7 @@ lmVCat_AD :: ADPlan -> [TExpr] -> TExpr
lmVCat_AD BasicAD ms = lmVCat ms
lmVCat_AD TupleAD ms = Tuple [ Tuple (map pFst ms)
, lmVCat (map pSnd ms) ]

lmVCat_AD SplitAD _ = error "lmVCat_AD:SplitAD" -- Doesn't make sense


---------------------------------
Expand Down
39 changes: 22 additions & 17 deletions src/ksc/Annotate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ScopedTypeVariables, TypeApplications #-}

module Annotate (
annotDecls, lintDefs
annotDecls, lintDefs, callResultTy_maybe, callResultTy
) where

import Lang
Expand Down Expand Up @@ -214,42 +214,47 @@ tcVar var mb_ty
-- callResultTy_maybe is given a (global) function and the type of its
-- argument, and returns the type of its result.
--
-- It has special cases for a bunch opuilt-in functions with polymorphic
-- It has special cases for a bunch built-in functions with polymorphic
-- types; that is, where the result type is a function of the argument types
-- Otherwise it just looks in the global symbol table.
callResultTy_maybe :: GblSymTab -> Fun -> Type
-> Either SDoc Type
callResultTy_maybe env fun args
callResultTy_maybe env fun arg_ty
| is_user_fun fun
= userCallResultTy_maybe fun env args
= userCallResultTy_maybe fun env arg_ty
| otherwise
= primCallResultTy_maybe fun (typeof args)
= primCallResultTy_maybe fun arg_ty
where
is_user_fun = isUserFun . funIdOfFun

callResultTy :: GblSymTab -> Fun -> Type -> Type
callResultTy gst fun arg_ty
= case callResultTy_maybe gst fun arg_ty of
Left err -> pprTrace "callResultType:" err TypeUnknown
Right res_ty -> res_ty

-----------------------------------------------
-- The typecheck monad
-----------------------------------------------

userCallResultTy_maybe :: HasCallStack => Fun -> GblSymTab
-> Type -> Either SDoc Type
userCallResultTy_maybe fn env args
= case lookupGblST (fn, typeof args) env of
Just def -> userCallResultTy_help def args
userCallResultTy_maybe fn env arg_ty
= case lookupGblST (fn, arg_ty) env of
Just def -> userCallResultTy_help def arg_ty
Nothing -> Left (text "Not in scope: userCall:" <+> ppr fn $$ ppr env)

userCallResultTy_help :: HasCallStack
=> TDef -> Type -> Either SDoc Type
userCallResultTy_help (Def { def_fun = fn
, def_res_ty = ret_ty
, def_args = params })
args
= case check_args bndr_tys arg_tys of
arg_ty
= case check_args bndr_ty arg_ty of
Just err -> Left err
Nothing -> Right ret_ty
where
bndr_tys = tVarType params
arg_tys = typeof args
bndr_ty = tVarType params

check_args :: Type -> Type -> Maybe SDoc
-- Return (Just err) if there's a wrong-ness
Expand Down Expand Up @@ -402,18 +407,18 @@ lookupLclTc v
Just ty -> return ty }

lookupGblTc :: Fun -> TypedExpr -> TcM Type
lookupGblTc fun args
lookupGblTc fun arg
= do { st <- getSymTabTc
; case callResultTy_maybe (gblST st) fun (typeof args) of
; case callResultTy_maybe (gblST st) fun (typeof arg) of
Left err -> do { addErr $ hang err 2 (mk_extra st)
; return TypeUnknown }
Right res_ty -> return res_ty }
where
mk_extra st
= vcat [ text "In a call of:" <+> ppr fun <+> parens (text (show fun))
, text " Arg types:" <+> ppr (typeof args)
, text " Args:" <+> ppr (exprOf args)
, text "ST lookup:" <+> ppr (Map.lookup (fun, typeof args) (gblST st))
, text " Arg types:" <+> ppr (typeof arg)
, text " Args:" <+> ppr (exprOf arg)
, text "ST lookup:" <+> ppr (Map.lookup (fun, typeof arg) (gblST st))
-- This is very verbose, and obscures error messages, but can be useful for typos.
-- Perhaps think about printing it only for failed lookup of userfun
-- , text "ST keys:" <+> gblDoc st
Expand Down
4 changes: 3 additions & 1 deletion src/ksc/Cgen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ params_withPackedParams param = case typeof param of
TypeTuple tys ->
let params = zipWith mkParam [1..] tys
mkParam i ty = TVar ty (Simple name)
where name = nameOfVar (tVarVar param) ++ "arg" ++ show i
where name = tVarName param ++ "arg" ++ show i
packParams = Let param (Tuple (map Var params))
in (params, ensureDon'tReuseParams params . packParams)
_ -> ([param], id)
Expand Down Expand Up @@ -525,6 +525,8 @@ cgenUserFun (f, ty) = case f of
DrvFun s (AD BasicAD Rev) -> "rev$" ++ cgenFunId (s, ty)
DrvFun s (AD TupleAD Fwd) -> "fwdt$" ++ cgenFunId (s, ty)
DrvFun s (AD TupleAD Rev) -> "revt$" ++ cgenFunId (s, ty)
DrvFun s (AD SplitAD Fwd) -> "revsf$" ++ cgenFunId (s, ty)
DrvFun s (AD SplitAD Rev) -> "revsr$" ++ cgenFunId (s, ty)

cgenAnyFun :: HasCallStack => (TFun, Type) -> CType -> String
cgenAnyFun (tf, ty) cftype = case tf of
Expand Down
Loading