Zubin pushed to branch wip/linkable-hashes at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • compiler/GHC/ByteCode/Binary.hs
    ... ... @@ -66,6 +66,7 @@ type BytecodeLib = BytecodeLibX (Maybe InterpreterLibrary)
    66 66
     -- | A bytecode library is a collection of CompiledByteCode objects and a .so file containing the combination of foreign stubs
    
    67 67
     data BytecodeLibX a = BytecodeLib {
    
    68 68
         bytecodeLibUnitId :: UnitId,
    
    69
    +    bytecodeLibInputsHash :: Fingerprint, -- ^ See Note [Hash of bytecode libs] in GHC.ByteCode.Serialize
    
    69 70
         bytecodeLibFiles :: [CompiledByteCode],
    
    70 71
         bytecodeLibForeign :: a -- A library file containing the combination of foreign stubs. (Ie arising from CApiFFI)
    
    71 72
     }
    
    ... ... @@ -113,12 +114,14 @@ instance Binary OnDiskModuleByteCode where
    113 114
     instance Binary OnDiskBytecodeLib where
    
    114 115
       get bh = do
    
    115 116
         bytecodeLibUnitId <- get bh
    
    117
    +    bytecodeLibInputsHash <- get bh
    
    116 118
         bytecodeLibFiles <- get bh
    
    117 119
         bytecodeLibForeign <- get bh
    
    118 120
         pure BytecodeLib {..}
    
    119 121
     
    
    120 122
       put_ bh BytecodeLib {..} = do
    
    121 123
         put_ bh bytecodeLibUnitId
    
    124
    +    put_ bh bytecodeLibInputsHash
    
    122 125
         put_ bh bytecodeLibFiles
    
    123 126
         put_ bh bytecodeLibForeign
    
    124 127
     
    

  • compiler/GHC/ByteCode/Serialize.hs
    ... ... @@ -16,7 +16,7 @@ module GHC.ByteCode.Serialize
    16 16
       , writeBytecodeLib
    
    17 17
       , readBytecodeLib
    
    18 18
       , readBytecodeLibInputsHash
    
    19
    -  , bytecodeLibInputsHash
    
    19
    +  , fingerprintBytecodeLibInputs
    
    20 20
       , mkModuleByteCode
    
    21 21
       , fingerprintModuleByteCodeContents
    
    22 22
       , decodeOnDiskModuleByteCode
    
    ... ... @@ -122,23 +122,19 @@ headers]. The constituent hashes are sorted before they are combined, so
    122 122
     the order of the modules doesn't matter.
    
    123 123
     -}
    
    124 124
     
    
    125
    --- | The hash a bytecode library records for its inputs, given the hash of
    
    126
    --- each constituent.
    
    125
    +-- | The 'bytecodeLibInputsHash' of a bytecode library, given the hash of each
    
    126
    +-- constituent.
    
    127 127
     -- See Note [Hash of bytecode libs]
    
    128
    -bytecodeLibInputsHash :: [Fingerprint] -> Fingerprint
    
    129
    -bytecodeLibInputsHash = fingerprintFingerprints . sort
    
    130
    -
    
    131
    -writeBytecodeLib
    
    132
    -  :: Fingerprint -- ^ Hash of the inputs, from 'bytecodeLibInputsHash'
    
    133
    -  -> BytecodeLib
    
    134
    -  -> FilePath
    
    135
    -  -> IO ()
    
    136
    -writeBytecodeLib inputs_hash lib path = do
    
    128
    +fingerprintBytecodeLibInputs :: [Fingerprint] -> Fingerprint
    
    129
    +fingerprintBytecodeLibInputs = fingerprintFingerprints . sort
    
    130
    +
    
    131
    +writeBytecodeLib :: BytecodeLib -> FilePath -> IO ()
    
    132
    +writeBytecodeLib lib path = do
    
    137 133
       odbco <- encodeBytecodeLib lib
    
    138 134
       createDirectoryIfMissing True (takeDirectory path)
    
    139 135
       bh' <- openBinMem initBinMemSize
    
    140 136
       bh <- addBinNameWriter bh'
    
    141
    -  writePersistentBytecodeHeader BytecodeLibraryFile inputs_hash bh
    
    137
    +  writePersistentBytecodeHeader BytecodeLibraryFile (bytecodeLibInputsHash lib) bh
    
    142 138
       putWithUserData QuietBinIFace NormalCompression bh odbco
    
    143 139
       writeBinMem bh path
    
    144 140
     
    
    ... ... @@ -178,6 +174,7 @@ decodeOnDiskBytecodeLib hsc_env odbco = do
    178 174
       foreign_contents <- traverse (writeInterpreterLibraryFile (hsc_logger hsc_env) (hsc_tmpfs hsc_env) (tmpDir (hsc_dflags hsc_env)))  (bytecodeLibForeign odbco)
    
    179 175
       pure $ BytecodeLib {
    
    180 176
         bytecodeLibUnitId = bytecodeLibUnitId odbco,
    
    177
    +    bytecodeLibInputsHash = bytecodeLibInputsHash odbco,
    
    181 178
         bytecodeLibFiles = bytecodeLibFiles odbco,
    
    182 179
         bytecodeLibForeign = foreign_contents
    
    183 180
        }
    
    ... ... @@ -187,6 +184,7 @@ encodeBytecodeLib (BytecodeLib {..}) = do
    187 184
       foreign_contents <- traverse readInterpreterLibraryFile bytecodeLibForeign
    
    188 185
       pure $ BytecodeLib {
    
    189 186
         bytecodeLibUnitId = bytecodeLibUnitId,
    
    187
    +    bytecodeLibInputsHash = bytecodeLibInputsHash,
    
    190 188
         bytecodeLibFiles = bytecodeLibFiles,
    
    191 189
         bytecodeLibForeign = foreign_contents
    
    192 190
        }
    

  • compiler/GHC/Driver/Pipeline.hs
    ... ... @@ -129,7 +129,7 @@ import qualified Data.List.NonEmpty as NE
    129 129
     import Data.List.NonEmpty (NonEmpty(..))
    
    130 130
     
    
    131 131
     import GHC.Utils.Fingerprint ( getFileHash )
    
    132
    -import GHC.ByteCode.Serialize ( bytecodeLibInputsHash, readBytecodeLibInputsHash )
    
    132
    +import GHC.ByteCode.Serialize ( fingerprintBytecodeLibInputs, readBytecodeLibInputsHash )
    
    133 133
     import GHC.Tc.Utils.Monad (shutdownTcMPluginsIO, FrontendResult (..), tcg_plugins)
    
    134 134
     
    
    135 135
     
    
    ... ... @@ -551,7 +551,7 @@ checkBytecodeLibraryLinkingNeeded _logger dflags unit_env linkables _pkg_deps =
    551 551
         Just _ -> do
    
    552 552
             -- See Note [Hash of bytecode libs] in GHC.ByteCode.Serialize
    
    553 553
             recorded <- readBytecodeLibInputsHash exe_file
    
    554
    -        let current = bytecodeLibInputsHash (map linkableHash linkables)
    
    554
    +        let current = fingerprintBytecodeLibInputs (map linkableHash linkables)
    
    555 555
             if recorded == Just current
    
    556 556
               then return UpToDate
    
    557 557
               else return $ needsRecompileBecause ObjectsChanged
    

  • compiler/GHC/Linker/ByteCode.hs
    ... ... @@ -37,13 +37,13 @@ linkBytecodeLib hsc_env gbcs = do
    37 37
     
    
    38 38
       let bytecodeLib' = BytecodeLib {
    
    39 39
         bytecodeLibUnitId = homeUnitId_ dflags,
    
    40
    +    bytecodeLibInputsHash = fingerprintBytecodeLibInputs
    
    41
    +      [ gbc_hash m | m <- on_disk_bcos ++ gbcs ],
    
    40 42
         bytecodeLibFiles = all_cbcs,
    
    41 43
         bytecodeLibForeign = interpreter_foreign_lib
    
    42 44
       }
    
    43
    -  let inputs_hash = bytecodeLibInputsHash
    
    44
    -        [ gbc_hash m | m <- on_disk_bcos ++ gbcs ]
    
    45 45
       let output_fn = fromMaybe "a.out" (outputFile dflags)
    
    46
    -  writeBytecodeLib inputs_hash bytecodeLib' output_fn
    
    46
    +  writeBytecodeLib bytecodeLib' output_fn
    
    47 47
       return ()
    
    48 48
     
    
    49 49
     
    

  • compiler/GHC/Linker/Loader.hs
    ... ... @@ -1388,7 +1388,7 @@ loadBytecodeLibrary :: HscEnv -> Interp -> LoaderState -> FilePath -> IO LoaderS
    1388 1388
     loadBytecodeLibrary hsc_env interp pls path = do
    
    1389 1389
       path' <- canonicalizePath path -- Note [loadObj and relative paths]
    
    1390 1390
       -- 1. Read the bytecode library
    
    1391
    -  (BytecodeLib uid cbcs stubs_so) <- decodeOnDiskBytecodeLib hsc_env =<< readBytecodeLib hsc_env path'
    
    1391
    +  (BytecodeLib uid _hash cbcs stubs_so) <- decodeOnDiskBytecodeLib hsc_env =<< readBytecodeLib hsc_env path'
    
    1392 1392
       debugTraceMsg (hsc_logger hsc_env) 3 $ text "loadBytecodeLibrary: " $$ vcat [ text "uid: " <+> ppr uid
    
    1393 1393
                                                                                  , text "cbcs: " <+> ppr (length cbcs)
    
    1394 1394
                                                                                  , text "stubs_so: " <+> ppr stubs_so ]