text
stringlengths
0
1.05M
-- If this identifier is the name of a constant, we have to turn it
-- into a call to the corresponding function.
is_const <- lookupConst name
case is_const of
Just ses -> return ses
Nothing -> (: []) . I.Var <$> internaliseIdent (E.Ident name (Info t) loc)
internaliseExp desc (E.Index e idxs (Info ret, Info retext) loc) = do
vs <- internaliseExpToVars "indexed" e
dims <- case vs of
[] -> return [] -- Will this happen?
v : _ -> I.arrayDims <$> lookupType v
(idxs', cs) <- internaliseSlice loc dims idxs
let index v = do
v_t <- lookupType v
return $ I.BasicOp $ I.Index v $ fullSlice v_t idxs'
ses <- certifying cs $ letSubExps desc =<< mapM index vs
bindExtSizes (E.toStruct ret) retext ses
return ses
-- XXX: we map empty records and tuples to bools, because otherwise
-- arrays of unit will lose their sizes.
internaliseExp _ (E.TupLit [] _) =
return [constant True]
internaliseExp _ (E.RecordLit [] _) =
return [constant True]
internaliseExp desc (E.TupLit es _) = concat <$> mapM (internaliseExp desc) es
internaliseExp desc (E.RecordLit orig_fields _) =
concatMap snd . sortFields . M.unions <$> mapM internaliseField orig_fields
where
internaliseField (E.RecordFieldExplicit name e _) =
M.singleton name <$> internaliseExp desc e
internaliseField (E.RecordFieldImplicit name t loc) =
internaliseField $
E.RecordFieldExplicit
(baseName name)
(E.Var (E.qualName name) t loc)
loc
internaliseExp desc (E.ArrayLit es (Info arr_t) loc)
-- If this is a multidimensional array literal of primitives, we
-- treat it specially by flattening it out followed by a reshape.
-- This cuts down on the amount of statements that are produced, and
-- thus allows us to efficiently handle huge array literals - a
-- corner case, but an important one.
| Just ((eshape, e') : es') <- mapM isArrayLiteral es,
not $ null eshape,
all ((eshape ==) . fst) es',
Just basetype <- E.peelArray (length eshape) arr_t = do
let flat_lit = E.ArrayLit (e' ++ concatMap snd es') (Info basetype) loc
new_shape = length es : eshape
flat_arrs <- internaliseExpToVars "flat_literal" flat_lit
forM flat_arrs $ \flat_arr -> do
flat_arr_t <- lookupType flat_arr
let new_shape' =
reshapeOuter
(map (DimNew . intConst Int32 . toInteger) new_shape)
1
$ I.arrayShape flat_arr_t
letSubExp desc $ I.BasicOp $ I.Reshape new_shape' flat_arr
| otherwise = do
es' <- mapM (internaliseExp "arr_elem") es
arr_t_ext <- internaliseReturnType (E.toStruct arr_t)
rowtypes <-
case mapM (fmap rowType . hasStaticShape . I.fromDecl) arr_t_ext of
Just ts -> pure ts
Nothing ->
-- XXX: the monomorphiser may create single-element array
-- literals with an unknown row type. In those cases we
-- need to look at the types of the actual elements.
-- Fixing this in the monomorphiser is a lot more tricky
-- than just working around it here.
case es' of
[] -> error $ "internaliseExp ArrayLit: existential type: " ++ pretty arr_t
e' : _ -> mapM subExpType e'
let arraylit ks rt = do
ks' <-
mapM
( ensureShape
"shape of element differs from shape of first element"
loc
rt
"elem_reshaped"
)
ks
return $ I.BasicOp $ I.ArrayLit ks' rt
letSubExps desc
=<< if null es'
then mapM (arraylit []) rowtypes
else zipWithM arraylit (transpose es') rowtypes
where
isArrayLiteral :: E.Exp -> Maybe ([Int], [E.Exp])
isArrayLiteral (E.ArrayLit inner_es _ _) = do
(eshape, e) : inner_es' <- mapM isArrayLiteral inner_es
guard $ all ((eshape ==) . fst) inner_es'
return (length inner_es : eshape, e ++ concatMap snd inner_es')
isArrayLiteral e =
Just ([], [e])
internaliseExp desc (E.Range start maybe_second end (Info ret, Info retext) loc) = do