[Git][ghc/ghc][wip/linkable-hashes] bytecode: Record the inputs hash in BytecodeLib
Zubin pushed to branch wip/linkable-hashes at Glasgow Haskell Compiler / GHC Commits: 0df8f110 by Zubin Duggal at 2026-11-24T19:51:06+05:30 bytecode: Record the inputs hash in BytecodeLib ------------------------- Metric Increase: MultiLayerModulesRecomp MultiLayerModulesTH_Make MultiLayerModulesTH_OneShot ------------------------- - - - - - 5 changed files: - compiler/GHC/ByteCode/Binary.hs - compiler/GHC/ByteCode/Serialize.hs - compiler/GHC/Driver/Pipeline.hs - compiler/GHC/Linker/ByteCode.hs - compiler/GHC/Linker/Loader.hs Changes: ===================================== compiler/GHC/ByteCode/Binary.hs ===================================== @@ -66,6 +66,7 @@ type BytecodeLib = BytecodeLibX (Maybe InterpreterLibrary) -- | A bytecode library is a collection of CompiledByteCode objects and a .so file containing the combination of foreign stubs data BytecodeLibX a = BytecodeLib { bytecodeLibUnitId :: UnitId, + bytecodeLibInputsHash :: Fingerprint, -- ^ See Note [Hash of bytecode libs] in GHC.ByteCode.Serialize bytecodeLibFiles :: [CompiledByteCode], bytecodeLibForeign :: a -- A library file containing the combination of foreign stubs. (Ie arising from CApiFFI) } @@ -113,12 +114,14 @@ instance Binary OnDiskModuleByteCode where instance Binary OnDiskBytecodeLib where get bh = do bytecodeLibUnitId <- get bh + bytecodeLibInputsHash <- get bh bytecodeLibFiles <- get bh bytecodeLibForeign <- get bh pure BytecodeLib {..} put_ bh BytecodeLib {..} = do put_ bh bytecodeLibUnitId + put_ bh bytecodeLibInputsHash put_ bh bytecodeLibFiles put_ bh bytecodeLibForeign ===================================== compiler/GHC/ByteCode/Serialize.hs ===================================== @@ -16,7 +16,7 @@ module GHC.ByteCode.Serialize , writeBytecodeLib , readBytecodeLib , readBytecodeLibInputsHash - , bytecodeLibInputsHash + , fingerprintBytecodeLibInputs , mkModuleByteCode , fingerprintModuleByteCodeContents , decodeOnDiskModuleByteCode @@ -122,23 +122,19 @@ headers]. The constituent hashes are sorted before they are combined, so the order of the modules doesn't matter. -} --- | The hash a bytecode library records for its inputs, given the hash of --- each constituent. +-- | The 'bytecodeLibInputsHash' of a bytecode library, given the hash of each +-- constituent. -- See Note [Hash of bytecode libs] -bytecodeLibInputsHash :: [Fingerprint] -> Fingerprint -bytecodeLibInputsHash = fingerprintFingerprints . sort - -writeBytecodeLib - :: Fingerprint -- ^ Hash of the inputs, from 'bytecodeLibInputsHash' - -> BytecodeLib - -> FilePath - -> IO () -writeBytecodeLib inputs_hash lib path = do +fingerprintBytecodeLibInputs :: [Fingerprint] -> Fingerprint +fingerprintBytecodeLibInputs = fingerprintFingerprints . sort + +writeBytecodeLib :: BytecodeLib -> FilePath -> IO () +writeBytecodeLib lib path = do odbco <- encodeBytecodeLib lib createDirectoryIfMissing True (takeDirectory path) bh' <- openBinMem initBinMemSize bh <- addBinNameWriter bh' - writePersistentBytecodeHeader BytecodeLibraryFile inputs_hash bh + writePersistentBytecodeHeader BytecodeLibraryFile (bytecodeLibInputsHash lib) bh putWithUserData QuietBinIFace NormalCompression bh odbco writeBinMem bh path @@ -178,6 +174,7 @@ decodeOnDiskBytecodeLib hsc_env odbco = do foreign_contents <- traverse (writeInterpreterLibraryFile (hsc_logger hsc_env) (hsc_tmpfs hsc_env) (tmpDir (hsc_dflags hsc_env))) (bytecodeLibForeign odbco) pure $ BytecodeLib { bytecodeLibUnitId = bytecodeLibUnitId odbco, + bytecodeLibInputsHash = bytecodeLibInputsHash odbco, bytecodeLibFiles = bytecodeLibFiles odbco, bytecodeLibForeign = foreign_contents } @@ -187,6 +184,7 @@ encodeBytecodeLib (BytecodeLib {..}) = do foreign_contents <- traverse readInterpreterLibraryFile bytecodeLibForeign pure $ BytecodeLib { bytecodeLibUnitId = bytecodeLibUnitId, + bytecodeLibInputsHash = bytecodeLibInputsHash, bytecodeLibFiles = bytecodeLibFiles, bytecodeLibForeign = foreign_contents } ===================================== compiler/GHC/Driver/Pipeline.hs ===================================== @@ -129,7 +129,7 @@ import qualified Data.List.NonEmpty as NE import Data.List.NonEmpty (NonEmpty(..)) import GHC.Utils.Fingerprint ( getFileHash ) -import GHC.ByteCode.Serialize ( bytecodeLibInputsHash, readBytecodeLibInputsHash ) +import GHC.ByteCode.Serialize ( fingerprintBytecodeLibInputs, readBytecodeLibInputsHash ) import GHC.Tc.Utils.Monad (shutdownTcMPluginsIO, FrontendResult (..), tcg_plugins) @@ -551,7 +551,7 @@ checkBytecodeLibraryLinkingNeeded _logger dflags unit_env linkables _pkg_deps = Just _ -> do -- See Note [Hash of bytecode libs] in GHC.ByteCode.Serialize recorded <- readBytecodeLibInputsHash exe_file - let current = bytecodeLibInputsHash (map linkableHash linkables) + let current = fingerprintBytecodeLibInputs (map linkableHash linkables) if recorded == Just current then return UpToDate else return $ needsRecompileBecause ObjectsChanged ===================================== compiler/GHC/Linker/ByteCode.hs ===================================== @@ -37,13 +37,13 @@ linkBytecodeLib hsc_env gbcs = do let bytecodeLib' = BytecodeLib { bytecodeLibUnitId = homeUnitId_ dflags, + bytecodeLibInputsHash = fingerprintBytecodeLibInputs + [ gbc_hash m | m <- on_disk_bcos ++ gbcs ], bytecodeLibFiles = all_cbcs, bytecodeLibForeign = interpreter_foreign_lib } - let inputs_hash = bytecodeLibInputsHash - [ gbc_hash m | m <- on_disk_bcos ++ gbcs ] let output_fn = fromMaybe "a.out" (outputFile dflags) - writeBytecodeLib inputs_hash bytecodeLib' output_fn + writeBytecodeLib bytecodeLib' output_fn return () ===================================== compiler/GHC/Linker/Loader.hs ===================================== @@ -1388,7 +1388,7 @@ loadBytecodeLibrary :: HscEnv -> Interp -> LoaderState -> FilePath -> IO LoaderS loadBytecodeLibrary hsc_env interp pls path = do path' <- canonicalizePath path -- Note [loadObj and relative paths] -- 1. Read the bytecode library - (BytecodeLib uid cbcs stubs_so) <- decodeOnDiskBytecodeLib hsc_env =<< readBytecodeLib hsc_env path' + (BytecodeLib uid _hash cbcs stubs_so) <- decodeOnDiskBytecodeLib hsc_env =<< readBytecodeLib hsc_env path' debugTraceMsg (hsc_logger hsc_env) 3 $ text "loadBytecodeLibrary: " $$ vcat [ text "uid: " <+> ppr uid , text "cbcs: " <+> ppr (length cbcs) , text "stubs_so: " <+> ppr stubs_so ] View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0df8f110cefa8b4c44d87f5ca63270bd... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0df8f110cefa8b4c44d87f5ca63270bd... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Zubin (@wz1000)