on master help sign in
Optimization level selection
7b6448eBenjamin Saunders
src/IRTS/CodegenLLVM.hs+4−3
@@ -54,10 +54,11 @@ data Target = Target { triple :: String, dataLayout :: DataLayout }
54codegenLLVM :: [(TT.Name, SDecl)] ->
54codegenLLVM :: [(TT.Name, SDecl)] ->
55 String -> -- target triple
55 String -> -- target triple
56 String -> -- target CPU
56 String -> -- target CPU
57+ Int -> -- Optimization degree
57 FilePath -> -- output file name
58 FilePath -> -- output file name
58 OutputType ->
59 OutputType ->
59 IO ()
60 IO ()
60−codegenLLVM defs triple cpu file outty = withContext $ \context -> do
61+codegenLLVM defs triple cpu optimize file outty = withContext $ \context -> do
61 initializeAllTargets
62 initializeAllTargets
62 (target, _) <- failInIO $ lookupTarget Nothing triple
63 (target, _) <- failInIO $ lookupTarget Nothing triple
63 withTargetOptions $ \options ->
64 withTargetOptions $ \options ->
@@ -66,11 +67,11 @@ codegenLLVM defs triple cpu file outty = withContext $ \context -> do
66 let ast = codegen (Target triple layout) (map snd defs)
67 let ast = codegen (Target triple layout) (map snd defs)
67 result <- runErrorT . M.withModuleFromAST context ast $ \m ->
68 result <- runErrorT . M.withModuleFromAST context ast $ \m ->
68 do let opts = defaultCuratedPassSetSpec
69 do let opts = defaultCuratedPassSetSpec
69− { optLevel = Just 3
70+ { optLevel = Just optimize
70 , simplifyLibCalls = Just True
71 , simplifyLibCalls = Just True
71 , useInlinerWithThreshold = Just 225
72 , useInlinerWithThreshold = Just 225
72 }
73 }
73− withPassManager opts $ \pm -> runPassManager pm m
74+ when (optimize /= 0) $ withPassManager opts $ void . flip runPassManager m
74 outputModule tm file outty m
75 outputModule tm file outty m
75 case result of
76 case result of
76 Right _ -> return ()
77 Right _ -> return ()
src/IRTS/Compiler.hs+2−1
@@ -66,6 +66,7 @@ compile codegen f tm
66 Just f -> liftIO $ writeFile f (dumpDefuns defuns)
66 Just f -> liftIO $ writeFile f (dumpDefuns defuns)
67 triple <- targetTriple
67 triple <- targetTriple
68 cpu <- targetCPU
68 cpu <- targetCPU
69+ optimize <- optLevel
69 iLOG "Building output"
70 iLOG "Building output"
70 case checked of
71 case checked of
71 OK c -> liftIO $ case codegen of
72 OK c -> liftIO $ case codegen of
@@ -79,7 +80,7 @@ compile codegen f tm
79 codegenJavaScript JavaScript c f outty
80 codegenJavaScript JavaScript c f outty
80 ViaNode ->
81 ViaNode ->
81 codegenJavaScript Node c f outty
82 codegenJavaScript Node c f outty
82− ViaLLVM -> codegenLLVM c triple cpu f outty
83+ ViaLLVM -> codegenLLVM c triple cpu optimize f outty
83
84
84 Bytecode -> dumpBC c f
85 Bytecode -> dumpBC c f
85 Error e -> fail $ show e
86 Error e -> fail $ show e
src/Idris/AbsSyntax.hs+10
@@ -390,6 +390,16 @@ targetCPU :: Idris String
390targetCPU = do i <- getIState
390targetCPU = do i <- getIState
391 return (opt_cpu (idris_options i))
391 return (opt_cpu (idris_options i))
392
392
393+setOptLevel :: Int -> Idris ()
394+setOptLevel t = do i <- getIState
395+ let opts = idris_options i
396+ opt' = opts { opt_optLevel = t }
397+ putIState $ i { idris_options = opt' }
398+
399+optLevel :: Idris Int
400+optLevel = do i <- getIState
401+ return (opt_optLevel (idris_options i))
402+
393verbose :: Idris Bool
403verbose :: Idris Bool
394verbose = do i <- getIState
404verbose = do i <- getIState
395 return (opt_verbose (idris_options i))
405 return (opt_verbose (idris_options i))
src/Idris/AbsSyntaxTree.hs+2
@@ -39,6 +39,7 @@ data IOption = IOption { opt_logLevel :: Int,
39 opt_importdirs :: [FilePath],
39 opt_importdirs :: [FilePath],
40 opt_triple :: String,
40 opt_triple :: String,
41 opt_cpu :: String,
41 opt_cpu :: String,
42+ opt_optLevel :: Int,
42 opt_cmdline :: [Opt] -- remember whole command line
43 opt_cmdline :: [Opt] -- remember whole command line
43 }
44 }
44 deriving (Show, Eq)
45 deriving (Show, Eq)
@@ -58,6 +59,7 @@ defaultOpts = IOption { opt_logLevel = 0
58 , opt_importdirs = []
59 , opt_importdirs = []
59 , opt_triple = ""
60 , opt_triple = ""
60 , opt_cpu = ""
61 , opt_cpu = ""
62+ , opt_optLevel = 2
61 , opt_cmdline = []
63 , opt_cmdline = []
62 }
64 }
63
65
src/Idris/REPL.hs+8
@@ -646,6 +646,9 @@ idrisMain opts =
646 let bcs = opt getBC opts
646 let bcs = opt getBC opts
647 let vm = opt getFOVM opts
647 let vm = opt getFOVM opts
648 let pkgdirs = opt getPkgDir opts
648 let pkgdirs = opt getPkgDir opts
649+ let optimize = case opt getOptLevel opts of
650+ [] -> 2
651+ xs -> last xs
649 trpl <- case opt getTriple opts of
652 trpl <- case opt getTriple opts of
650 [] -> liftIO $ getDefaultTargetTriple
653 [] -> liftIO $ getDefaultTargetTriple
651 xs -> return (last xs)
654 xs -> return (last xs)
@@ -675,6 +678,7 @@ idrisMain opts =
675 setCodegen cgn
678 setCodegen cgn
676 setTargetTriple trpl
679 setTargetTriple trpl
677 setTargetCPU tcpu
680 setTargetCPU tcpu
681+ setOptLevel optimize
678 when (Verbose `elem` opts) $ setVerbose True
682 when (Verbose `elem` opts) $ setVerbose True
679 mapM_ makeOption opts
683 mapM_ makeOption opts
680 -- if we have the --fovm flag, drop into the first order VM testing
684 -- if we have the --fovm flag, drop into the first order VM testing
@@ -799,6 +803,10 @@ getCPU :: Opt -> Maybe String
799getCPU (TargetCPU x) = Just x
803getCPU (TargetCPU x) = Just x
800getCPU _ = Nothing
804getCPU _ = Nothing
801
805
806+getOptLevel :: Opt -> Maybe Int
807+getOptLevel (OptLevel x) = Just x
808+getOptLevel _ = Nothing
809+
802opt :: (Opt -> Maybe a) -> [Opt] -> [a]
810opt :: (Opt -> Maybe a) -> [Opt] -> [a]
803opt = mapMaybe
811opt = mapMaybe
804
812