Matthew Pickering pushed to branch wip/bytecode-lib-shared-object at Glasgow Haskell Compiler / GHC
Commits:
-
25af941a
by Matthew Pickering at 2025-10-28T09:19:20+00:00
8 changed files:
- compiler/GHC/Linker/ByteCode.hs
- testsuite/tests/cabal/BytecodeForeign.hs
- testsuite/tests/cabal/Makefile
- testsuite/tests/cabal/all.T
- + testsuite/tests/cabal/pkg_bytecode_with_gbc.stderr
- + testsuite/tests/cabal/pkg_bytecode_with_gbc.stdout
- + testsuite/tests/cabal/pkg_bytecode_with_o.stderr
- + testsuite/tests/cabal/pkg_bytecode_with_o.stdout
Changes:
| ... | ... | @@ -9,13 +9,17 @@ import GHC.Driver.Env |
| 9 | 9 | import GHC.Utils.Outputable
|
| 10 | 10 | import GHC.Linker.Loader
|
| 11 | 11 | import qualified Data.ByteString as BS
|
| 12 | +import Data.List (partition)
|
|
| 13 | +import GHC.Driver.Phases (isObjectFilename)
|
|
| 12 | 14 | |
| 13 | 15 | |
| 14 | 16 | linkBytecodeLib :: HscEnv -> [ModuleByteCode] -> IO ()
|
| 15 | 17 | linkBytecodeLib hsc_env gbcs = do
|
| 16 | 18 | let dflags = hsc_dflags hsc_env
|
| 17 | 19 | -- The .gbc files from the command line
|
| 18 | - let bytecodeObjects = [f | FileOption _ f <- ldInputs dflags]
|
|
| 20 | + let fileArguments = [f | FileOption _ f <- ldInputs dflags]
|
|
| 21 | + |
|
| 22 | + let (objectFiles, bytecodeObjects) = partition (isObjectFilename (targetPlatform dflags)) fileArguments
|
|
| 19 | 23 | |
| 20 | 24 | let logger = hsc_logger hsc_env
|
| 21 | 25 | let allFiles = (map text bytecodeObjects) ++ [ angleBrackets (text "in-memory" <+> ppr (gbc_module bco)) | bco <- gbcs ]
|
| ... | ... | @@ -28,7 +32,7 @@ linkBytecodeLib hsc_env gbcs = do |
| 28 | 32 | |
| 29 | 33 | let (all_cbcs, foreign_stubs) = unzip [ (bs, fs) | ModuleByteCode _m bs fs <- on_disk_bcos ++ gbcs]
|
| 30 | 34 | |
| 31 | - foreign_stub_lib <- mkDynLoadLib hsc_env id [] [{-TODO-}] (concat foreign_stubs)
|
|
| 35 | + foreign_stub_lib <- mkDynLoadLib hsc_env id [] [{-TODO-}] (concat foreign_stubs ++ objectFiles)
|
|
| 32 | 36 | |
| 33 | 37 | fc <- case foreign_stub_lib of
|
| 34 | 38 | Just (fp, _, _) -> do
|
| ... | ... | @@ -24,5 +24,4 @@ testForeign = do |
| 24 | 24 | pureFunction :: Int -> Int -> Int
|
| 25 | 25 | pureFunction x y = x + y
|
| 26 | 26 | |
| 27 | -$(addForeignFilePath LangC "BytecodeForeign.c" >> return [])
|
|
| 28 | 27 |
| ... | ... | @@ -313,7 +313,38 @@ pkg_bytecode_foreign : |
| 313 | 313 | $(LOCAL_GHC_PKG08) init $(PKGCONF08)
|
| 314 | 314 | mkdir outdir
|
| 315 | 315 | mv BytecodeForeign.hs outdir/
|
| 316 | + echo '$$(addForeignFilePath LangC "BytecodeForeign.c" >> return [])' >> outdir/BytecodeForeign.hs
|
|
| 316 | 317 | mv BytecodeForeign.c outdir/
|
| 317 | 318 | cd outdir && $(TEST_HC) -bytecodelib -hisuf=dyn_hi -dynamic -o testpkg-1.2.3.4-XXX.bytecode BytecodeForeign.hs -fbyte-code -fwrite-interface -fwrite-byte-code -this-unit-id=testpkg-1.2.3.4-XXX
|
| 318 | 319 | $(LOCAL_GHC_PKG09) register --force bytecode_foreign.pkg
|
| 319 | 320 | cat bytecode_foreign.script | $(TEST_HC) $(TEST_HC_OPTS_INTERACTIVE) -package testpkg -package-db $(PKGCONF09)
|
| 321 | + |
|
| 322 | +# Test that bytecode generation works by first compiling C to .o, then passing .o to GHC
|
|
| 323 | +PKGCONF10=bytecode_with_o.package.conf
|
|
| 324 | +LOCAL_GHC_PKG10 = '$(GHC_PKG)' --no-user-package-db -f $(PKGCONF10)
|
|
| 325 | +pkg_bytecode_with_o :
|
|
| 326 | + $(LOCAL_GHC_PKG10) init $(PKGCONF10)
|
|
| 327 | + mkdir outdir
|
|
| 328 | + mv BytecodeForeign.hs outdir/
|
|
| 329 | + mv BytecodeForeign.c outdir/
|
|
| 330 | + cd outdir && $(TEST_HC) -c BytecodeForeign.c
|
|
| 331 | + cd outdir && $(TEST_HC) -bytecodelib -hisuf=dyn_hi -dynamic -o testpkg-1.2.3.4-XXX.bytecode BytecodeForeign.hs BytecodeForeign.o -fbyte-code -fwrite-interface -fwrite-byte-code -this-unit-id=testpkg-1.2.3.4-XXX
|
|
| 332 | + $(LOCAL_GHC_PKG10) register --force bytecode_foreign.pkg
|
|
| 333 | + cat bytecode_foreign.script | $(TEST_HC) $(TEST_HC_OPTS_INTERACTIVE) -package testpkg -package-db $(PKGCONF10)
|
|
| 334 | + |
|
| 335 | +# Test that bytecode generation works by first compiling C to .o and Haskell to .gbc independently
|
|
| 336 | +PKGCONF11=bytecode_with_gbc.package.conf
|
|
| 337 | +LOCAL_GHC_PKG11 = '$(GHC_PKG)' --no-user-package-db -f $(PKGCONF11)
|
|
| 338 | +pkg_bytecode_with_gbc :
|
|
| 339 | + $(LOCAL_GHC_PKG11) init $(PKGCONF11)
|
|
| 340 | + mkdir outdir
|
|
| 341 | + mv BytecodeForeign.hs outdir/
|
|
| 342 | + mv BytecodeForeign.c outdir/
|
|
| 343 | + cd outdir && $(TEST_HC) -c BytecodeForeign.c
|
|
| 344 | + cd outdir && $(TEST_HC) BytecodeForeign.hs -fbyte-code -fwrite-interface -hisuf=dyn_hi -dynamic -fwrite-byte-code -this-unit-id=testpkg-1.2.3.4-XXX -o BytecodeForeign.gbc
|
|
| 345 | + cd outdir && $(TEST_HC) -bytecodelib -hisuf=dyn_hi -dynamic -o testpkg-1.2.3.4-XXX.bytecode BytecodeForeign.gbc BytecodeForeign.o
|
|
| 346 | + $(LOCAL_GHC_PKG11) register --force bytecode_foreign.pkg
|
|
| 347 | + cat bytecode_foreign.script | $(TEST_HC) $(TEST_HC_OPTS_INTERACTIVE) -package testpkg -package-db $(PKGCONF11)
|
|
| 348 | + |
|
| 349 | + |
|
| 350 | + |
| ... | ... | @@ -43,6 +43,14 @@ test('pkg_bytecode_foreign', [extra_files(['bytecode_foreign.pkg', "BytecodeFore |
| 43 | 43 | , normalise_errmsg_fun(normaliseDynlibNames, ignore_warnings)
|
| 44 | 44 | , copy_files], makefile_test, [])
|
| 45 | 45 | |
| 46 | +test('pkg_bytecode_with_o', [extra_files(['bytecode_foreign.pkg', "BytecodeForeign.hs", "BytecodeForeign.c", "bytecode_foreign.script"])
|
|
| 47 | + , normalise_errmsg_fun(normaliseDynlibNames, ignore_warnings)
|
|
| 48 | + , copy_files], makefile_test, [])
|
|
| 49 | + |
|
| 50 | +test('pkg_bytecode_with_gbc', [extra_files(['bytecode_foreign.pkg', "BytecodeForeign.hs", "BytecodeForeign.c", "bytecode_foreign.script"])
|
|
| 51 | + , normalise_errmsg_fun(normaliseDynlibNames, ignore_warnings)
|
|
| 52 | + , copy_files], makefile_test, [])
|
|
| 53 | + |
|
| 46 | 54 | # Test that we *can* compile a module that also belongs to a package
|
| 47 | 55 | # (this was disallowed in GHC 6.4 and earlier)
|
| 48 | 56 | test('pkg01', normal, compile, [''])
|
| 1 | +when making flags consistent: warning: [GHC-74335] [-Winconsistent-flags]
|
|
| 2 | + Byte-code linking does not currently support linking an executable, enabling -no-link
|
|
| 3 | + |
|
| 4 | +testpkg-1.2.3.4: cannot find any of ["libtestpkg-1.2.3.4-XXX.a","libtestpkg-1.2.3.4-XXX_p.a","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.dylib","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dylib","testpkg-1.2.3.4-XXX-ghc9.15.20251010.dll","testpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dll"] on library path (ignoring) |
| 1 | +[1 of 1] Compiling BytecodeForeign ( BytecodeForeign.hs, BytecodeForeign.gbc )
|
|
| 2 | +Reading package info from "bytecode_foreign.pkg" ... done.
|
|
| 3 | +8
|
|
| 4 | +42 |
| 1 | +testpkg-1.2.3.4: cannot find any of ["libtestpkg-1.2.3.4-XXX.a","libtestpkg-1.2.3.4-XXX_p.a","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.so","libtestpkg-1.2.3.4-XXX-ghc9.15.20251010.dylib","libtestpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dylib","testpkg-1.2.3.4-XXX-ghc9.15.20251010.dll","testpkg-1.2.3.4-XXX_p-ghc9.15.20251010.dll"] on library path (ignoring) |
| 1 | +[1 of 2] Compiling BytecodeForeign ( BytecodeForeign.hs, BytecodeForeign.gbc )
|
|
| 2 | +[2 of 2] Linking testpkg-1.2.3.4-XXX.bytecode
|
|
| 3 | +Reading package info from "bytecode_foreign.pkg" ... done.
|
|
| 4 | +8
|
|
| 5 | +42 |