mirror of
https://github.com/anoma/juvix.git
synced 2024-12-14 08:27:03 +03:00
2f4a3f809b
## Overview This PR makes the compiler pipeline thread-safe so that the test suite can be run in parallel. This is achieved by: * Removing use of `{get, set, with}CurrentDir` functions. * Adding locking around shared file resources like the the global-project and internal build directory. NB: **Locking is disabled for the main compiler target**, as it is single threaded they are not required. ## Run test suite in parallel To run the test suite in parallel you must add `--ta '+RTS -N -RTS'` to your stack test arguments. For example: ``` stack test --fast --ta '+RTS -N -RTS' ``` The `-N` instructs the Haskell runtime to choose the number of threads to use based on how many processors there are on your machine. You can use `-Nn` to see the number of threads to `n`. These flags are already [set in the Makefile](e6dca22cfd/Makefile (L26)
) when you or CI uses `stack test`. ## Locking The Haskell package [filelock](https://hackage.haskell.org/package/filelock) is used for locking. File locks are used instead of MVars because Juvix code does not control when new threads are created, they are created by the test suite. This means that MVars created by Juvix code will have no effect, because they are created independently on each test-suite thread. Additionally the resources we're locking live on the filesystem and so can be conveniently tagged by path. ### FileLock The filelock library is wrapped in a FileLock effect:e6dca22cfd/src/Juvix/Data/Effect/FileLock/Base.hs (L6-L8)
There is an [IO interpreter](e6dca22cfd/src/Juvix/Data/Effect/FileLock/IO.hs (L8)
) that uses filelock and an [no-op interpreter](e6dca22cfd/src/Juvix/Data/Effect/FileLock/Permissive.hs (L7)
) that just runs actions unconditionally. ### TaggedLock To make the file locks simpler to use a TaggedLock effect is introduced:e6dca22cfd/src/Juvix/Data/Effect/TaggedLock/Base.hs (L5-L11)
And convenience function:e6dca22cfd/src/Juvix/Data/Effect/TaggedLock.hs (L28)
This allows an action to be locked, tagged by a directory that may or may not exist. For example in the following code, an action is performed on a directory `root` that may delete the directory before repopulating the files. So the lockfile cannot be stored in the `root` itself.e6dca22cfd/src/Juvix/Extra/Files.hs (L55-L60)
## Pipeline As noted above, we only use locking in the test suite. The main app target pipeline is single threaded and so locking is unnecessary. So the interpretation of locks is parameterised so that locking can be disablede6dca22cfd/src/Juvix/Compiler/Pipeline/Run.hs (L64)
92 lines
4.6 KiB
Haskell
92 lines
4.6 KiB
Haskell
module BackendGeb.FromCore.Base where
|
|
|
|
import Base
|
|
import Data.Text.IO qualified as TIO
|
|
import GHC.Base (seq)
|
|
import Juvix.Compiler.Backend (Target (TargetGeb))
|
|
import Juvix.Compiler.Backend.Geb qualified as Geb
|
|
import Juvix.Compiler.Core qualified as Core
|
|
import Juvix.Compiler.Core.Pretty qualified as Core
|
|
import Juvix.Data.Effect.TaggedLock
|
|
import Juvix.Prelude.Pretty
|
|
|
|
coreToGebTranslationAssertion ::
|
|
Path Abs Dir ->
|
|
Path Abs File ->
|
|
Path Abs File ->
|
|
(String -> IO ()) ->
|
|
Assertion
|
|
coreToGebTranslationAssertion root mainFile expectedFile step = do
|
|
step "Parse Juvix Core file"
|
|
input <- readFile . toFilePath $ mainFile
|
|
entryPoint <- set entryPointTarget TargetGeb <$> defaultEntryPointIO' LockModeExclusive root mainFile
|
|
case Core.runParserMain mainFile Core.emptyInfoTable input of
|
|
Left err -> assertFailure . show . pretty $ err
|
|
Right coreInfoTable -> coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step
|
|
|
|
coreToGebTranslationAssertion' ::
|
|
Core.InfoTable ->
|
|
EntryPoint ->
|
|
Path Abs File ->
|
|
(String -> IO ()) ->
|
|
Assertion
|
|
coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step = do
|
|
step "Prepare the Juvix Core node for translation to Geb"
|
|
case run . runReader entryPoint . runError @Geb.JuvixError $ Core.toGeb coreInfoTable of
|
|
Left err ->
|
|
assertFailure . show . pretty $
|
|
fromJuvixError @GenericError err
|
|
Right readyCoreInfoTable ->
|
|
length (fromText (Core.ppTrace readyCoreInfoTable) :: String) `seq` do
|
|
step "Translate the Juvix Core node to Geb"
|
|
let (translatedMorphism, translatedObj) = Geb.fromCore readyCoreInfoTable
|
|
step "Typecheck the translated Geb node"
|
|
let typeMorph =
|
|
Geb.TypedMorphism
|
|
{ _typedMorphism = translatedMorphism,
|
|
_typedMorphismObject = translatedObj
|
|
}
|
|
case run . runError @Geb.CheckingError $ Geb.check' typeMorph of
|
|
Left err ->
|
|
assertFailure . show . pretty $
|
|
fromJuvixError @GenericError (JuvixError err)
|
|
Right _ -> do
|
|
step "Try evaluating the JuvixCore node"
|
|
let resultCoreEval :: Core.Node = Core.evalInfoTable stderr readyCoreInfoTable
|
|
step "Translate the result of the evaluated JuvixCore node to Geb"
|
|
let (gebCoreEvalResult, _) = Geb.fromCore $ Core.setupMainFunction readyCoreInfoTable resultCoreEval
|
|
case ( Geb.eval' Geb.defaultEvalEnv translatedMorphism,
|
|
Geb.eval' Geb.defaultEvalEnv gebCoreEvalResult
|
|
) of
|
|
(Left err, _) -> do
|
|
step "The evaluation of the translated Geb node failed"
|
|
assertFailure . show . pretty $
|
|
fromJuvixError @GenericError (JuvixError err)
|
|
(_, Left err) -> do
|
|
step "The evaluation of gebCoreEvalResult failed"
|
|
assertFailure . show . pretty $ fromJuvixError @GenericError (JuvixError err)
|
|
( Right resEvalTranslatedMorph,
|
|
Right resEvalGebCoreEvalResult
|
|
) -> do
|
|
step "Compare the geb value of the Core eval output and the Geb eval output"
|
|
if
|
|
| resEvalTranslatedMorph /= resEvalGebCoreEvalResult ->
|
|
assertFailure "The evaluation for the Core node and the Geb node are not equal"
|
|
| otherwise -> do
|
|
let fpath = toFilePath expectedFile
|
|
expectedInput <- TIO.readFile fpath
|
|
step "Compare expected and actual program output"
|
|
let compareEvalOutput morph =
|
|
if
|
|
| Geb.quote resEvalTranslatedMorph /= morph ->
|
|
assertFailure $
|
|
"The result of evaluating the translated Geb"
|
|
<> "node is not equal to the expected output"
|
|
| otherwise -> assertBool "" True
|
|
case Geb.runParser expectedFile expectedInput of
|
|
Left parseErr -> assertFailure . show . pretty $ parseErr
|
|
Right (Geb.ExpressionMorphism m) -> compareEvalOutput m
|
|
Right (Geb.ExpressionTypedMorphism m) -> compareEvalOutput (m ^. Geb.typedMorphism)
|
|
Right (Geb.ExpressionObject _) ->
|
|
assertFailure "Expected a morphism, but got an object for the expected output"
|