{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide #-}
module Data.Array.Accelerate.Trafo (
Phase(..), phases,
convertAcc, convertAccWith,
Afunction, AfunctionR,
convertAfun, convertAfunWith,
module Data.Array.Accelerate.Trafo.Fusion,
module Data.Array.Accelerate.Trafo.Substitution,
Match(..), (:~:)(..),
matchDelayedOpenAcc, hashDelayedOpenAcc,
) where
import Control.DeepSeq
import Data.Typeable
import Data.Array.Accelerate.Smart
import Data.Array.Accelerate.Pretty ( )
import Data.Array.Accelerate.Array.Sugar ( Arrays, Elt )
import Data.Array.Accelerate.Trafo.Base
import Data.Array.Accelerate.Trafo.Fusion hiding ( convertAcc, convertAfun )
import Data.Array.Accelerate.Trafo.Sharing ( Function, FunctionR, Afunction, AfunctionR )
import Data.Array.Accelerate.Trafo.Substitution
import qualified Data.Array.Accelerate.AST as AST
import qualified Data.Array.Accelerate.Trafo.Fusion as Fusion
import qualified Data.Array.Accelerate.Trafo.Rewrite as Rewrite
import qualified Data.Array.Accelerate.Trafo.Simplify as Rewrite
import qualified Data.Array.Accelerate.Trafo.Sharing as Sharing
#ifdef ACCELERATE_DEBUG
import Text.Printf
import System.IO.Unsafe
import Data.Array.Accelerate.Debug hiding ( when )
import qualified Data.Array.Accelerate.Debug as Debug
#endif
data Phase = Phase
{
recoverAccSharing :: Bool
, recoverExpSharing :: Bool
, recoverSeqSharing :: Bool
, floatOutAccFromExp :: Bool
, enableAccFusion :: Bool
, convertOffsetOfSegment :: Bool
}
phases :: Phase
phases = Phase
{ recoverAccSharing = True
, recoverExpSharing = True
, recoverSeqSharing = True
, floatOutAccFromExp = True
, enableAccFusion = True
, convertOffsetOfSegment = False
}
when :: (a -> a) -> Bool -> a -> a
when f True = f
when _ False = id
convertAcc :: Arrays arrs => Acc arrs -> DelayedAcc arrs
convertAcc = convertAccWith phases
convertAccWith :: Arrays arrs => Phase -> Acc arrs -> DelayedAcc arrs
convertAccWith Phase{..} acc
= phase "array-fusion" (Fusion.convertAcc enableAccFusion)
$ phase "rewrite-segment-offset" Rewrite.convertSegments `when` convertOffsetOfSegment
$ phase "sharing-recovery" (Sharing.convertAcc recoverAccSharing recoverExpSharing recoverSeqSharing floatOutAccFromExp)
$ acc
convertAfun :: Afunction f => f -> DelayedAfun (AfunctionR f)
convertAfun = convertAfunWith phases
convertAfunWith :: Afunction f => Phase -> f -> DelayedAfun (AfunctionR f)
convertAfunWith Phase{..} acc
= phase "array-fusion" (Fusion.convertAfun enableAccFusion)
$ phase "rewrite-segment-offset" Rewrite.convertSegmentsAfun `when` convertOffsetOfSegment
$ phase "sharing-recovery" (Sharing.convertAfun recoverAccSharing recoverExpSharing recoverSeqSharing floatOutAccFromExp)
$ acc
convertExp :: Elt e => Exp e -> AST.Exp () e
convertExp
= phase "exp-simplify" Rewrite.simplify
. phase "sharing-recovery" (Sharing.convertExp (recoverExpSharing phases))
convertFun :: Function f => f -> AST.Fun () (FunctionR f)
convertFun
= phase "exp-simplify" Rewrite.simplify
. phase "sharing-recovery" (Sharing.convertFun (recoverExpSharing phases))
instance Arrays arrs => Show (Acc arrs) where
show = withSimplStats . show . convertAcc
instance Afunction (Acc a -> f) => Show (Acc a -> f) where
show = withSimplStats . show . convertAfun
instance Elt e => Show (Exp e) where
show = withSimplStats . show . convertExp
instance Function (Exp a -> f) => Show (Exp a -> f) where
show = withSimplStats . show . convertFun
withSimplStats :: String -> String
#ifdef ACCELERATE_DEBUG
withSimplStats x = unsafePerformIO $ do
Debug.when dump_simpl_stats $ x `deepseq` dumpSimplStats
return x
#else
withSimplStats x = x
#endif
phase :: NFData b => String -> (a -> b) -> a -> b
#ifdef ACCELERATE_DEBUG
phase n f x = unsafePerformIO $ do
enabled <- queryFlag dump_phases
if enabled
then timed dump_phases (\wall cpu -> printf "phase %s: %s" n (elapsed wall cpu)) (return $!! f x)
else return (f x)
#else
phase _ f x = f x
#endif