on master help sign in
Optimization level selection
commit7b6448efb7bda0038239d9926b42d551484680ec authorBenjamin Saunders <besaunde@sfu.ca> dateFri 5 Jul 2013, 14:06 (13 years ago) parent63b69ff
5 files changed+26−4
src/IRTS/CodegenLLVM.hs+4−3 src/IRTS/Compiler.hs+2−1 src/Idris/AbsSyntax.hs+10 src/Idris/AbsSyntaxTree.hs+2 src/Idris/REPL.hs+8
src/IRTS/CodegenLLVM.hs+4−3
@@ -54,10 +54,11 @@ data Target = Target { triple :: String, dataLayout :: DataLayout }
5454codegenLLVM :: [(TT.Name, SDecl)] ->
5555 String -> -- target triple
5656 String -> -- target CPU
57+ Int -> -- Optimization degree
5758 FilePath -> -- output file name
5859 OutputType ->
5960 IO ()
60−codegenLLVM defs triple cpu file outty = withContext $ \context -> do
61+codegenLLVM defs triple cpu optimize file outty = withContext $ \context -> do
6162 initializeAllTargets
6263 (target, _) <- failInIO $ lookupTarget Nothing triple
6364 withTargetOptions $ \options ->
@@ -66,11 +67,11 @@ codegenLLVM defs triple cpu file outty = withContext $ \context -> do
6667 let ast = codegen (Target triple layout) (map snd defs)
6768 result <- runErrorT . M.withModuleFromAST context ast $ \m ->
6869 do let opts = defaultCuratedPassSetSpec
69− { optLevel = Just 3
70+ { optLevel = Just optimize
7071 , simplifyLibCalls = Just True
7172 , useInlinerWithThreshold = Just 225
7273 }
73− withPassManager opts $ \pm -> runPassManager pm m
74+ when (optimize /= 0) $ withPassManager opts $ void . flip runPassManager m
7475 outputModule tm file outty m
7576 case result of
7677 Right _ -> return ()
src/IRTS/Compiler.hs+2−1
@@ -66,6 +66,7 @@ compile codegen f tm
6666 Just f -> liftIO $ writeFile f (dumpDefuns defuns)
6767 triple <- targetTriple
6868 cpu <- targetCPU
69+ optimize <- optLevel
6970 iLOG "Building output"
7071 case checked of
7172 OK c -> liftIO $ case codegen of
@@ -79,7 +80,7 @@ compile codegen f tm
7980 codegenJavaScript JavaScript c f outty
8081 ViaNode ->
8182 codegenJavaScript Node c f outty
82− ViaLLVM -> codegenLLVM c triple cpu f outty
83+ ViaLLVM -> codegenLLVM c triple cpu optimize f outty
8384
8485 Bytecode -> dumpBC c f
8586 Error e -> fail $ show e
src/Idris/AbsSyntax.hs+10
@@ -390,6 +390,16 @@ targetCPU :: Idris String
390390targetCPU = do i <- getIState
391391 return (opt_cpu (idris_options i))
392392
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+
393403verbose :: Idris Bool
394404verbose = do i <- getIState
395405 return (opt_verbose (idris_options i))
src/Idris/AbsSyntaxTree.hs+2
@@ -39,6 +39,7 @@ data IOption = IOption { opt_logLevel :: Int,
3939 opt_importdirs :: [FilePath],
4040 opt_triple :: String,
4141 opt_cpu :: String,
42+ opt_optLevel :: Int,
4243 opt_cmdline :: [Opt] -- remember whole command line
4344 }
4445 deriving (Show, Eq)
@@ -58,6 +59,7 @@ defaultOpts = IOption { opt_logLevel = 0
5859 , opt_importdirs = []
5960 , opt_triple = ""
6061 , opt_cpu = ""
62+ , opt_optLevel = 2
6163 , opt_cmdline = []
6264 }
6365
src/Idris/REPL.hs+8
@@ -646,6 +646,9 @@ idrisMain opts =
646646 let bcs = opt getBC opts
647647 let vm = opt getFOVM opts
648648 let pkgdirs = opt getPkgDir opts
649+ let optimize = case opt getOptLevel opts of
650+ [] -> 2
651+ xs -> last xs
649652 trpl <- case opt getTriple opts of
650653 [] -> liftIO $ getDefaultTargetTriple
651654 xs -> return (last xs)
@@ -675,6 +678,7 @@ idrisMain opts =
675678 setCodegen cgn
676679 setTargetTriple trpl
677680 setTargetCPU tcpu
681+ setOptLevel optimize
678682 when (Verbose `elem` opts) $ setVerbose True
679683 mapM_ makeOption opts
680684 -- if we have the --fovm flag, drop into the first order VM testing
@@ -799,6 +803,10 @@ getCPU :: Opt -> Maybe String
799803getCPU (TargetCPU x) = Just x
800804getCPU _ = Nothing
801805
806+getOptLevel :: Opt -> Maybe Int
807+getOptLevel (OptLevel x) = Just x
808+getOptLevel _ = Nothing
809+
802810opt :: (Opt -> Maybe a) -> [Opt] -> [a]
803811opt = mapMaybe
804812