~jojo/Carth

ref: 2219ea535f4a0fd0afd9000ba5c6918c6b276210 Carth/src/Compile.hs -rw-r--r-- 8.6 KiB
2219ea53JoJo remove no longer applicable TODOs 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings, LambdaCase
           , OverloadedStrings #-}

module Compile (compile, run) where

import LLVM.Prelude
import LLVM.Context
import LLVM.Module
import LLVM.Target
import LLVM.Target.Options
import LLVM.Analysis
import LLVM.OrcJIT
import LLVM.OrcJIT.CompileLayer as CL
import LLVM.Linking
import LLVM.PassManager
import LLVM.Exception
import qualified LLVM.Relocation as Reloc
import qualified LLVM.CodeModel as CodeModel
import qualified LLVM.CodeGenOpt as CodeGenOpt
import LLVM.AST.DataLayout
import qualified LLVM.AST as LLAST
import Control.Monad.Catch
import System.FilePath
import System.Process
import System.Exit
import Data.IORef
import Data.String
import qualified Data.Map as Map
import Foreign.Ptr
import Prelude hiding (mod)

import Misc
import Conf
import qualified Optimized as Ast
import Codegen
import Err
import Pretty


compile :: FilePath -> CompileConfig -> Ast.Program -> IO ()
compile = handleProgram compileModule

run :: FilePath -> RunConfig -> Ast.Program -> IO ()
run = handleProgram orcJitModule

handleProgram
    :: Config cfg
    => (cfg -> TargetMachine -> Module -> IO ())
    -> FilePath
    -> cfg
    -> Ast.Program
    -> IO ()
handleProgram f file cfg pgm = withContext $ \ctx ->
    -- When `--debug` is given, only -O1 optimize the code. Otherwise, optimize
    -- by -O2. No point in going further to -O3, as those optimizations are
    -- expensive and seldom actually improve the performance in a statistically
    -- significant way.
    --
    -- A minimum optimization level of -O1 ensures that all sibling calls are
    -- optimized, even if we don't use a calling convention like `fastcc` that
    -- can optimize any tail call.
    let optLvl = if (getDebug cfg) then CodeGenOpt.Less else CodeGenOpt.Default
    in
        withMyTargetMachine optLvl $ \tm -> do
            layout <- getTargetMachineDataLayout tm
            triple <- getProcessTargetTriple
            verbose cfg ("   Generating LLVM")
            amod <- codegen' layout triple file pgm
            when (getDebug cfg) (writeFile ".dbg.gen.ll" (pretty amod))
            flip
                    catch
                    (\case
                        EncodeException msg -> ice $ "LLVM encode exception:\n" ++ msg
                    )
                $ withModuleFromAST ctx amod
                $ \mod -> do
                      verbose cfg ("   Verifying LLVM")
                      when (getDebug cfg) $ writeLLVMAssemblyToFile' ".dbg.ll" mod
                      catch (verify mod) $ \case
                          VerifyException msg ->
                              ice $ "LLVM verification exception:\n" ++ msg
                      withPassManager (optPasses optLvl tm) $ \passman -> do
                          verbose cfg "   Optimizing"
                          r <- runPassManager passman mod
                          when (not r) $ putStrLn "DEBUG: runPassManager returned False"
                          when (getDebug cfg) $ writeLLVMAssemblyToFile' ".dbg.opt.ll" mod
                          f cfg tm mod

codegen' :: DataLayout -> ShortByteString -> FilePath -> Ast.Program -> IO LLAST.Module
codegen' layout triple f pgm = case codegen layout triple f pgm of
    Right m -> pure m
    Left e -> printGenErr e *> abort f

compileModule :: CompileConfig -> TargetMachine -> Module -> IO ()
compileModule cfg tm mod = do
    let exefile = cOutfile cfg
        ofile = replaceExtension exefile "o"
    verbose cfg "   Writing object"
    writeObjectToFile tm (File ofile) mod
    verbose cfg ("   Linking")
    callProcess
        (cCompiler cfg)
        [ "-o"
        , exefile
        , ofile
        , "-l:libcarth_std_rs.a"
        , "-lsigsegv"
        , "-ldl"
        , "-lpthread"
        , "-lm"
        , "-lgc"
        , "-lssl"
        , "-lcrypto"
        ]

foreign import ccall "dynamic"
  mkMain :: FunPtr (IO Int32) -> IO Int32

orcJitModule :: RunConfig -> TargetMachine -> Module -> IO ()
orcJitModule cfg tm mod = do
    verbose cfg "   Running with OrcJIT"
    let libs = ["libsigsegv.so", "libcarth_std_rs.so", "libgc.so"]
    forM_ libs $ \lib -> do
        verbose cfg $ "   Loading symbols of " ++ lib
        r <- loadLibraryPermanently (Just lib)
        when r (putStrLn ("   Error loading " ++ lib) *> exitFailure)
    resolvers <- newIORef Map.empty
    let linkingResolver key = fmap (Map.! key) (readIORef resolvers)
    session <- createExecutionSession
    linkLay <- newObjectLinkingLayer session linkingResolver
    compLay <- newIRCompileLayer linkLay tm
    let resolver' = resolver compLay
    withSymbolResolver session (SymbolResolver resolver') $ \resolverPtr ->
        withModuleKey session $ \modKey -> do
            modifyIORef' resolvers (Map.insert modKey resolverPtr)
            withModule compLay modKey mod $ do
                mangleSymbol compLay "main" >>= resolver' >>= \case
                    Left err -> do
                        putStrLn "   Error during JIT symbol resolution"
                        putStrLn ("   error: " ++ show err)
                        exitFailure
                    Right (JITSymbol mainAddr _) ->
                        mkMain (castPtrToFunPtr (wordPtrToPtr mainAddr)) $> ()
    disposeCompileLayer compLay
    disposeLinkingLayer linkLay
    disposeExecutionSession session

-- Following are some useful things to know regarding symbol resolution when it
-- comes to JIT, LLVM, and OrcJIT. I'm not sure about all of this, so take it
-- with a grain of salt.
--
-- - `CompileLayer.findSymbol`: Only looks in the compile-layer, which includes
--   our compiled LLVM modules, but not linked object code, or linked shared
--   libraries.
--
-- - `LinkingLayer.findSymbol`: Looks in the linking-layer, a superset of the
--   compile-layer that includes all object code added to the layer with
--   `addObjectFile`.
--
-- - `Linking.getSymbolAddressInProcess`: Looks in the address-space of the
--   running process, which includes all shared object code added with
--   `Linking.loadLibraryPermanently`. Disjoint from the compile and linking
--   layer.
resolver :: CompileLayer cl => cl -> MangledSymbol -> IO (Either JITSymbolError JITSymbol)
resolver compLay symb =
    let
        flags = JITSymbolFlags { jitSymbolWeak = False
                               , jitSymbolCommon = False
                               , jitSymbolAbsolute = False
                               , jitSymbolExported = True
                               }
        err = fromString ("Error resolving symbol: " ++ show symb)
        findInLlvmModules = CL.findSymbol compLay symb False
        findInSharedObjects = getSymbolAddressInProcess symb <&> \addr -> if addr == 0
            then Left (JITSymbolError err)
            else Right (JITSymbol addr flags)
    in
        findInLlvmModules >>= \case
            Right js -> pure (Right js)
            Left _ -> findInSharedObjects

-- | `writeLLVMAssemblyToFile` doesn't clear file contents before writing,
--   so this is a workaround.
writeLLVMAssemblyToFile' :: FilePath -> Module -> IO ()
writeLLVMAssemblyToFile' f m = do
    writeFile f ""
    writeLLVMAssemblyToFile (File f) m

withMyTargetMachine :: CodeGenOpt.Level -> (TargetMachine -> IO a) -> IO a
withMyTargetMachine codeGenOpt f = do
    initializeAllTargets
    triple <- getProcessTargetTriple
    cpu <- getHostCPUName
    features <- getHostCPUFeatures
    (target, _) <- lookupTarget Nothing triple
    withTargetOptions $ \toptions -> do
        options <- peekTargetOptions toptions
        pokeTargetOptions (options { guaranteedTailCallOptimization = True }) toptions
        withTargetMachine target
                          triple
                          cpu
                          features
                          toptions
                          Reloc.PIC
                          CodeModel.Default
                          codeGenOpt
                          f

optPasses :: CodeGenOpt.Level -> TargetMachine -> PassSetSpec
optPasses level tm =
    let levelN = case level of
            CodeGenOpt.None -> 0
            CodeGenOpt.Less -> 1
            CodeGenOpt.Default -> 2
            CodeGenOpt.Aggressive -> 3
    in  CuratedPassSetSpec { optLevel = Just levelN
                           , sizeLevel = Nothing
                           , unitAtATime = Nothing
                           , simplifyLibCalls = Nothing
                           , loopVectorize = Nothing
                           , superwordLevelParallelismVectorize = Nothing
                           , useInlinerWithThreshold = Nothing
                           , dataLayout = Nothing
                           , targetLibraryInfo = Nothing
                           , targetMachine = Just tm
                           }