Skip to content

Commit 0aa9fad

Browse files
committed
Some change to reduce cpp usage in the Main file.
- Made separate modules for each ghc version in the hopes that maintaining them would become easier than the many nested if, else we have in cpp right now.
1 parent 3c6535a commit 0aa9fad

File tree

10 files changed

+1017
-273
lines changed

10 files changed

+1017
-273
lines changed

fusion-plugin.cabal

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ source-repository head
4343

4444
library
4545
exposed-modules: Fusion.Plugin
46+
other-modules:
47+
Fusion.Plugin.Ghc
48+
if impl(ghc >= 9.5.0)
49+
other-modules:
50+
Fusion.Plugin.GhcHead
51+
elif impl(ghc >= 9.4.0)
52+
other-modules:
53+
Fusion.Plugin.Ghc940
54+
elif impl(ghc >= 9.3.0)
55+
other-modules:
56+
Fusion.Plugin.Ghc930
57+
elif impl(ghc >= 9.2.2)
58+
other-modules:
59+
Fusion.Plugin.Ghc922
60+
elif impl(ghc >= 9.2.0)
61+
other-modules:
62+
Fusion.Plugin.Ghc920
63+
elif impl(ghc >= 9.0.0)
64+
other-modules:
65+
Fusion.Plugin.Ghc900
66+
else
67+
if impl(ghc >= 8.6.0)
68+
other-modules:
69+
Fusion.Plugin.Ghc860
70+
4671
build-depends: base >= 4.0 && < 5.0
4772
, containers >= 0.5.6.2 && < 0.7
4873
, directory >= 1.2.2.0 && < 1.4

src/Fusion/Plugin.hs

Lines changed: 8 additions & 273 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,6 @@ import Data.Generics.Schemes (everywhere)
5858
import Data.Generics.Aliases (mkT)
5959
import Debug.Trace (trace)
6060
import qualified Data.List as DL
61-
62-
-- Imports for specific compiler versions
63-
#if MIN_VERSION_ghc(9,2,0)
64-
import Data.Char (isSpace)
65-
import Text.Printf (printf)
66-
import GHC.Core.Ppr (pprCoreBindingsWithSize, pprRules)
67-
import GHC.Types.Name.Ppr (mkPrintUnqualified)
68-
import GHC.Utils.Logger (Logger)
69-
#endif
70-
71-
-- dump-core option related imports
72-
#if MIN_VERSION_ghc(9,3,0)
73-
import GHC.Utils.Logger (putDumpFile, logFlags, LogFlags(..))
74-
#elif MIN_VERSION_ghc(9,2,0)
75-
import GHC.Utils.Logger (putDumpMsg)
76-
#elif MIN_VERSION_ghc(9,0,0)
77-
-- dump core option not supported
78-
#else
79-
import Control.Monad (unless)
80-
import Data.Char (isSpace)
81-
import Data.IORef (readIORef, writeIORef)
82-
import Data.Time (getCurrentTime)
83-
import System.Directory (createDirectoryIfMissing)
84-
import System.FilePath ((</>), takeDirectory)
85-
import System.IO (Handle, IOMode(..), withFile, hSetEncoding, utf8)
86-
import Text.Printf (printf)
87-
import ErrUtils (mkDumpDoc, Severity(..))
88-
import PprCore (pprCoreBindingsWithSize, pprRules)
89-
import qualified Data.Set as Set
90-
#endif
9161
#endif
9262

9363
-- Implicit imports
@@ -100,6 +70,7 @@ import GhcPlugins
10070

10171
-- Imports from this package
10272
import Fusion.Plugin.Types (Fuse(..))
73+
import qualified Fusion.Plugin.Ghc
10374

10475
-- $using
10576
--
@@ -684,34 +655,7 @@ fusionMarkInline pass opt failIt transform =
684655
-------------------------------------------------------------------------------
685656

686657
fusionSimplify :: HscEnv -> DynFlags -> CoreToDo
687-
fusionSimplify _hsc_env dflags =
688-
let mode =
689-
SimplMode
690-
{ sm_phase = InitialPhase
691-
, sm_names = ["Fusion Plugin Inlining"]
692-
, sm_dflags = dflags
693-
, sm_rules = gopt Opt_EnableRewriteRules dflags
694-
, sm_eta_expand = gopt Opt_DoLambdaEtaExpansion dflags
695-
, sm_inline = True
696-
, sm_case_case = True
697-
#if MIN_VERSION_ghc(9,2,0)
698-
, sm_uf_opts = unfoldingOpts dflags
699-
, sm_pre_inline = gopt Opt_SimplPreInlining dflags
700-
, sm_logger = hsc_logger _hsc_env
701-
#endif
702-
#if MIN_VERSION_ghc(9,2,2)
703-
, sm_cast_swizzle = True
704-
#endif
705-
#if MIN_VERSION_ghc(9,5,0)
706-
, sm_float_enable = floatEnable dflags
707-
#endif
708-
}
709-
in CoreDoSimplify
710-
#if MIN_VERSION_ghc(9,5,0)
711-
(CoreDoSimplifyOpts (maxSimplIterations dflags) mode)
712-
#else
713-
(maxSimplIterations dflags) mode
714-
#endif
658+
fusionSimplify = Fusion.Plugin.Ghc.coreToDo
715659

716660
-------------------------------------------------------------------------------
717661
-- Report unfused constructors
@@ -772,209 +716,12 @@ fusionReport mesg reportMode guts = do
772716
-- Dump core passes
773717
-------------------------------------------------------------------------------
774718

775-
-- Only for GHC versions before 9.0.0
776-
#if !MIN_VERSION_ghc(9,0,0)
777-
chooseDumpFile :: DynFlags -> FilePath -> Maybe FilePath
778-
chooseDumpFile dflags suffix
779-
| Just prefix <- getPrefix
780-
781-
= Just $ setDir (prefix ++ suffix)
782-
783-
| otherwise
784-
785-
= Nothing
786-
787-
where getPrefix
788-
-- dump file location is being forced
789-
-- by the --ddump-file-prefix flag.
790-
| Just prefix <- dumpPrefixForce dflags
791-
= Just prefix
792-
-- dump file location chosen by DriverPipeline.runPipeline
793-
| Just prefix <- dumpPrefix dflags
794-
= Just prefix
795-
-- we haven't got a place to put a dump file.
796-
| otherwise
797-
= Nothing
798-
setDir f = case dumpDir dflags of
799-
Just d -> d </> f
800-
Nothing -> f
801-
802-
-- Copied from GHC.Utils.Logger
803-
withDumpFileHandle :: DynFlags -> FilePath -> (Maybe Handle -> IO ()) -> IO ()
804-
withDumpFileHandle dflags suffix action = do
805-
let mFile = chooseDumpFile dflags suffix
806-
case mFile of
807-
Just fileName -> do
808-
let gdref = generatedDumps dflags
809-
gd <- readIORef gdref
810-
let append = Set.member fileName gd
811-
mode = if append then AppendMode else WriteMode
812-
unless append $
813-
writeIORef gdref (Set.insert fileName gd)
814-
createDirectoryIfMissing True (takeDirectory fileName)
815-
withFile fileName mode $ \handle -> do
816-
-- We do not want the dump file to be affected by
817-
-- environment variables, but instead to always use
818-
-- UTF8. See:
819-
-- https://gitlab.haskell.org/ghc/ghc/issues/10762
820-
hSetEncoding handle utf8
821-
action (Just handle)
822-
Nothing -> action Nothing
823-
824-
dumpSDocWithStyle :: PprStyle -> DynFlags -> FilePath -> String -> SDoc -> IO ()
825-
dumpSDocWithStyle sty dflags suffix hdr doc =
826-
withDumpFileHandle dflags suffix writeDump
827-
where
828-
-- write dump to file
829-
writeDump (Just handle) = do
830-
doc' <- if null hdr
831-
then return doc
832-
else do t <- getCurrentTime
833-
let timeStamp = if (gopt Opt_SuppressTimestamps dflags)
834-
then empty
835-
else text (show t)
836-
let d = timeStamp
837-
$$ blankLine
838-
$$ doc
839-
return $ mkDumpDoc hdr d
840-
defaultLogActionHPrintDoc dflags handle doc' sty
841-
842-
-- write the dump to stdout
843-
writeDump Nothing = do
844-
let (doc', severity)
845-
| null hdr = (doc, SevOutput)
846-
| otherwise = (mkDumpDoc hdr doc, SevDump)
847-
putLogMsg dflags NoReason severity noSrcSpan sty doc'
848-
849-
dumpSDoc :: DynFlags -> PrintUnqualified -> FilePath -> String -> SDoc -> IO ()
850-
dumpSDoc dflags print_unqual
851-
= dumpSDocWithStyle dump_style dflags
852-
where dump_style = mkDumpStyle dflags print_unqual
853-
#endif
854-
855-
-- dump core not supported on 9.0.0, 9.0.0 does not export Logger
856-
#if __GLASGOW_HASKELL__!=900
857-
-- Only for GHC versions >= 9.2.0
858-
#if MIN_VERSION_ghc(9,2,0)
859-
dumpPassResult ::
860-
Logger
861-
-> DynFlags
862-
-> PrintUnqualified
863-
-> SDoc -- Header
864-
-> SDoc -- Extra info to appear after header
865-
-> CoreProgram -> [CoreRule]
866-
-> IO ()
867-
dumpPassResult logger dflags unqual hdr extra_info binds rules = do
868-
#if MIN_VERSION_ghc(9,3,0)
869-
let flags = logFlags logger
870-
let getDumpAction = putDumpFile
871-
#else
872-
let flags = dflags
873-
let getDumpAction = putDumpMsg
874-
#endif
875-
(getDumpAction logger)
876-
flags dump_style Opt_D_dump_simpl title undefined dump_doc
877-
878-
where
879-
880-
title = showSDoc dflags hdr
881-
882-
dump_style = mkDumpStyle unqual
883-
884-
#else
885-
886-
dumpPassResult :: DynFlags
887-
-> PrintUnqualified
888-
-> FilePath
889-
-> SDoc -- Header
890-
-> SDoc -- Extra info to appear after header
891-
-> CoreProgram -> [CoreRule]
892-
-> IO ()
893-
dumpPassResult dflags unqual suffix hdr extra_info binds rules = do
894-
dumpSDoc dflags unqual suffix (showSDoc dflags hdr) dump_doc
895-
896-
where
897-
898-
#endif
899-
dump_doc = vcat [ nest 2 extra_info
900-
, blankLine
901-
, pprCoreBindingsWithSize binds
902-
, ppUnless (null rules) pp_rules ]
903-
pp_rules = vcat [ blankLine
904-
, text "------ Local rules for imported ids --------"
905-
, pprRules rules ]
906-
907-
filterOutLast :: (a -> Bool) -> [a] -> [a]
908-
filterOutLast _ [] = []
909-
filterOutLast p [x]
910-
| p x = []
911-
| otherwise = [x]
912-
filterOutLast p (x:xs) = x : filterOutLast p xs
913-
914-
dumpResult
915-
#if MIN_VERSION_ghc(9,2,0)
916-
:: Logger
917-
-> DynFlags
918-
#else
919-
:: DynFlags
920-
#endif
921-
-> PrintUnqualified
922-
-> Int
923-
-> SDoc
924-
-> CoreProgram
925-
-> [CoreRule]
926-
-> IO ()
927-
#if MIN_VERSION_ghc(9,2,0)
928-
dumpResult logger dflags print_unqual counter todo binds rules =
929-
dumpPassResult logger1 dflags print_unqual hdr (text "") binds rules
930-
#else
931-
dumpResult dflags print_unqual counter todo binds rules =
932-
dumpPassResult
933-
dflags print_unqual (_suffix ++ "dump-simpl") hdr (text "") binds rules
934-
#endif
935-
936-
where
937-
938-
hdr = text "["
939-
GhcPlugins.<> int counter
940-
GhcPlugins.<> text "] "
941-
GhcPlugins.<> todo
942-
943-
_suffix = printf "%02d" counter ++ "-"
944-
++ (map (\x -> if isSpace x then '-' else x)
945-
$ filterOutLast isSpace
946-
$ takeWhile (/= '(')
947-
$ showSDoc dflags todo)
948-
++ "."
949-
950-
#if MIN_VERSION_ghc(9,4,0)
951-
prefix = log_dump_prefix (logFlags logger) ++ _suffix
952-
logger1 = logger {logFlags = (logFlags logger) {log_dump_prefix = prefix}}
953-
#elif MIN_VERSION_ghc(9,2,0)
954-
logger1 = logger
955-
#endif
956-
#endif
957-
958719
dumpCore :: Int -> SDoc -> ModGuts -> CoreM ModGuts
959720
dumpCore counter title guts = do
960721
dflags <- getDynFlags
961722
putMsgS $ "fusion-plugin: dumping core "
962723
++ show counter ++ " " ++ showSDoc dflags title
963-
964-
#if MIN_VERSION_ghc(9,2,0)
965-
hscEnv <- getHscEnv
966-
let logger = hsc_logger hscEnv
967-
let print_unqual =
968-
mkPrintUnqualified (hsc_unit_env hscEnv) (mg_rdr_env guts)
969-
liftIO $ dumpResult logger dflags print_unqual counter
970-
title (mg_binds guts) (mg_rules guts)
971-
#elif MIN_VERSION_ghc(9,0,0)
972-
putMsgS $ "fusion-plugin: dump-core not supported on GHC 9.0 "
973-
#else
974-
let print_unqual = mkPrintUnqualified dflags (mg_rdr_env guts)
975-
liftIO $ dumpResult dflags print_unqual counter
976-
title (mg_binds guts) (mg_rules guts)
977-
#endif
724+
Fusion.Plugin.Ghc.dumpCore counter title guts
978725
return guts
979726

980727
dumpCorePass :: Int -> SDoc -> CoreToDo
@@ -1002,19 +749,10 @@ insertAfterSimplPhase0 origTodos ourTodos report =
1002749
where
1003750
go False [] = error "Simplifier phase 0/\"main\" not found"
1004751
go True [] = []
1005-
#if MIN_VERSION_ghc(9,5,0)
1006-
go _ (todo@(CoreDoSimplify (CoreDoSimplifyOpts _ SimplMode
1007-
{ sm_phase = Phase 0
1008-
, sm_names = ["main"]
1009-
})):todos)
1010-
#else
1011-
go _ (todo@(CoreDoSimplify _ SimplMode
1012-
{ sm_phase = Phase 0
1013-
, sm_names = ["main"]
1014-
}):todos)
1015-
#endif
1016-
= todo : ourTodos ++ go True todos
1017-
go found (todo:todos) = todo : go found todos
752+
go found (todo:todos) =
753+
if Fusion.Plugin.Ghc.isPhase0MainTodo todo
754+
then todo : ourTodos ++ go True todos
755+
else todo : go found todos
1018756

1019757
install :: [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
1020758
install args todos = do
@@ -1058,9 +796,6 @@ install _ todos = do
1058796
#endif
1059797

1060798
plugin :: Plugin
1061-
plugin = defaultPlugin
799+
plugin = Fusion.Plugin.Ghc.defaultPurePlugin
1062800
{ installCoreToDos = install
1063-
#if MIN_VERSION_ghc(8,6,0)
1064-
, pluginRecompile = purePlugin
1065-
#endif
1066801
}

0 commit comments

Comments
 (0)