Skip to content

Commit 0dec9b4

Browse files
authored
Merge pull request #73 from tmcdonell/wip/ghc-9
Support GHC-9
2 parents c68edab + 60d4c4b commit 0dec9b4

26 files changed

Lines changed: 359 additions & 219 deletions

File tree

.github/workflows/ci-linux.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
strategy:
2020
matrix:
2121
include:
22+
- ghc: "9.0"
23+
llvm: "12"
24+
cuda: "11.4"
25+
2226
- ghc: "8.10"
2327
llvm: "9"
2428
cuda: "10.2"

.github/workflows/ci-macos.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ jobs:
1919
strategy:
2020
matrix:
2121
include:
22+
- ghc: "9.0"
23+
llvm: "12"
24+
2225
- ghc: "8.10"
2326
llvm: "9"
2427

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ stack.yaml
1313
tags
1414
local/
1515
/stack.yaml.lock
16+
.ipynb_checkpoints

accelerate-llvm-native/accelerate-llvm-native.cabal

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ description:
2626
.
2727
Example using Homebrew on macOS:
2828
.
29-
> brew install llvm-hs/llvm/llvm-9
29+
> brew install llvm-hs/llvm/llvm-12
3030
.
3131
/Debian & Ubuntu/
3232
.
@@ -35,13 +35,13 @@ description:
3535
instructions for adding the correct package database for your OS version,
3636
and then:
3737
.
38-
> apt-get install llvm-9-dev
38+
> apt-get install llvm-12-dev
3939
.
4040
/Building from source/
4141
.
4242
If your OS does not have an appropriate LLVM distribution available, you can
4343
also build from source. Detailed build instructions are available on
44-
<http://releases.llvm.org/9.0.0/docs/CMake.html LLVM.org>. Make sure to
44+
<http://releases.llvm.org/12.0.0/docs/CMake.html LLVM.org>. Make sure to
4545
include the cmake build options
4646
@-DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_LINK_LLVM_DYLIB=ON@ so that the @libLLVM@
4747
shared library will be built.
@@ -107,6 +107,7 @@ Library
107107
Data.Array.Accelerate.LLVM.Native.Plugin.BuildInfo
108108

109109
Control.Concurrent.Extra
110+
Language.Haskell.TH.Extra
110111

111112
Paths_accelerate_llvm_native
112113

@@ -125,8 +126,8 @@ Library
125126
, ghc
126127
, hashable >= 1.0
127128
, libffi >= 0.1
128-
, llvm-hs >= 4.1 && < 9.1
129-
, llvm-hs-pure >= 4.1 && < 9.1
129+
, llvm-hs >= 4.1 && < 13
130+
, llvm-hs-pure >= 4.1 && < 13
130131
, lockfree-queue >= 0.2
131132
, mtl >= 2.2.1
132133
, template-haskell

accelerate-llvm-native/src/Data/Array/Accelerate/LLVM/Native.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ import Data.Array.Accelerate.LLVM.Native.Debug as Debug
8181

8282
import Control.Monad.Trans
8383
import System.IO.Unsafe
84-
import qualified Language.Haskell.TH as TH
84+
import qualified Language.Haskell.TH.Extra as TH
8585
import qualified Language.Haskell.TH.Syntax as TH
8686

8787
import GHC.Stack
@@ -410,8 +410,8 @@ runQ' using target f = do
410410
go (Alam lhs l) xs as stmts = do
411411
x <- TH.newName "x" -- lambda bound variable
412412
a <- TH.newName "a" -- local array name
413-
s <- TH.bindS (TH.varP a) [| useRemoteAsync $(TH.unTypeQ $ liftArraysR (lhsToTupR lhs)) (fromArr $(TH.varE x)) |]
414-
go l (TH.varP x : xs) ([| ($(TH.unTypeQ $ liftALeftHandSide lhs), $(TH.varE a)) |] : as) (return s : stmts)
413+
let s = TH.bindS (TH.varP a) [| useRemoteAsync $(TH.unTypeCode $ liftArraysR (lhsToTupR lhs)) (fromArr $(TH.varE x)) |]
414+
go l (TH.varP x : xs) ([| ($(TH.unTypeCode $ liftALeftHandSide lhs), $(TH.varE a)) |] : as) (s : stmts)
415415

416416
go (Abody b) xs as stmts = do
417417
r <- TH.newName "r" -- result
@@ -423,8 +423,8 @@ runQ' using target f = do
423423
TH.lamE (reverse xs)
424424
[| $using . phase Execute elapsedP . evalNative $target . evalPar $
425425
$(TH.doE ( reverse stmts ++
426-
[ TH.bindS (TH.varP r) [| executeOpenAcc $(TH.unTypeQ body) $aenv |]
427-
, TH.bindS (TH.varP s) [| getArrays $(TH.unTypeQ (liftArraysR (arraysR b))) $(TH.varE r) |]
426+
[ TH.bindS (TH.varP r) [| executeOpenAcc $(TH.unTypeCode body) $aenv |]
427+
, TH.bindS (TH.varP s) [| getArrays $(TH.unTypeCode (liftArraysR (arraysR b))) $(TH.varE r) |]
428428
, TH.noBindS [| return $ toArr $(TH.varE s) |]
429429
]))
430430
|]

accelerate-llvm-native/src/Data/Array/Accelerate/LLVM/Native/CodeGen/Permute.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ mkPermuteP_rmw uid aenv repr shr rmw update project marr =
197197
| IntegralNumType{} <- t -> void . instr' $ AtomicRMW t NonVolatile rmw addr (op t r) (CrossThread, AcquireRelease)
198198
| RMW.Add <- rmw -> atomicCAS_rmw s (A.add t r) addr
199199
| RMW.Sub <- rmw -> atomicCAS_rmw s (A.sub t r) addr
200-
#endif
201200
_ | RMW.Min <- rmw -> atomicCAS_cmp s A.lt addr (op s r)
202201
| RMW.Max <- rmw -> atomicCAS_cmp s A.gt addr (op s r)
203202
_ -> internalError "unexpected transition"
203+
#endif
204204
--
205205
_ -> internalError "unexpected transition"
206206

accelerate-llvm-native/src/Data/Array/Accelerate/LLVM/Native/Embed.hs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ import Data.Array.Accelerate.LLVM.Native.State
3636
import Data.Array.Accelerate.LLVM.Native.Target
3737

3838
import Control.Concurrent.Unique
39-
import Control.Monad
4039
import Data.Hashable
4140
import Foreign.Ptr
42-
import Language.Haskell.TH ( Q, TExp )
41+
import Language.Haskell.TH.Extra ( Q, CodeQ )
4342
import Numeric
4443
import System.IO.Unsafe
45-
import qualified Language.Haskell.TH as TH
44+
import qualified Language.Haskell.TH.Extra as TH
4645
import qualified Language.Haskell.TH.Syntax as TH
4746

4847
#if __GLASGOW_HASKELL__ >= 806
@@ -58,24 +57,27 @@ instance Embed Native where
5857
-- and generate FFI declarations to access the external functions of that file.
5958
-- The returned ExecutableR references the new FFI declarations.
6059
--
61-
embed :: Native -> ObjectR Native -> Q (TExp (ExecutableR Native))
62-
embed target (ObjectR uid nms !_) = do
63-
objFile <- getObjectFile
64-
funtab <- forM nms $ \fn -> return [|| ( $$(liftSBS fn), $$(makeFFI fn objFile) ) ||]
65-
--
66-
[|| NativeR (unsafePerformIO $ newLifetime (FunctionTable $$(listE funtab))) ||]
60+
embed :: Native -> ObjectR Native -> CodeQ (ExecutableR Native)
61+
embed target (ObjectR uid nms !_) =
62+
TH.bindCode getObjectFile $ \objFile ->
63+
[|| NativeR (unsafePerformIO $ newLifetime (FunctionTable $$(listE (makeTable objFile nms)))) ||]
6764
where
68-
listE :: [Q (TExp a)] -> Q (TExp [a])
69-
listE xs = TH.unsafeTExpCoerce (TH.listE (map TH.unTypeQ xs))
70-
71-
makeFFI :: ShortByteString -> FilePath -> Q (TExp (FunPtr ()))
72-
makeFFI (S8.unpack -> fn) objFile = do
73-
i <- TH.runIO newUnique
74-
fn' <- TH.newName ("__accelerate_llvm_native_" ++ showHex (hash i) [])
75-
dec <- TH.forImpD TH.CCall TH.Unsafe ('&':fn) fn' [t| FunPtr () |]
76-
ann <- TH.pragAnnD (TH.ValueAnnotation fn') [| (Object objFile) |]
77-
TH.addTopDecls [dec, ann]
78-
TH.unsafeTExpCoerce (TH.varE fn')
65+
listE :: [CodeQ a] -> CodeQ [a]
66+
listE xs = TH.unsafeCodeCoerce (TH.listE (map TH.unTypeCode xs))
67+
68+
makeTable :: FilePath -> [ShortByteString] -> [CodeQ (ShortByteString, FunPtr ())]
69+
makeTable objFile = map (\fn -> [|| ( $$(liftSBS fn), $$(makeFFI fn objFile) ) ||])
70+
71+
makeFFI :: ShortByteString -> FilePath -> CodeQ (FunPtr ())
72+
makeFFI (S8.unpack -> fn) objFile = TH.bindCode go (TH.unsafeCodeCoerce . return)
73+
where
74+
go = do
75+
i <- TH.runIO newUnique
76+
fn' <- TH.newName ("__accelerate_llvm_native_" ++ showHex (hash i) [])
77+
dec <- TH.forImpD TH.CCall TH.Unsafe ('&':fn) fn' [t| FunPtr () |]
78+
ann <- TH.pragAnnD (TH.ValueAnnotation fn') [| (Object objFile) |]
79+
TH.addTopDecls [dec, ann]
80+
TH.varE fn'
7981

8082
-- Note: [Template Haskell and raw object files]
8183
--

accelerate-llvm-native/src/Data/Array/Accelerate/LLVM/Native/Plugin.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ import Data.IORef
2727
import Data.List
2828
import qualified Data.Map as Map
2929

30+
#if __GLASGOW_HASKELL__ >= 900
31+
import GHC.Plugins
32+
import GHC.Runtime.Linker
33+
#else
3034
import GhcPlugins
3135
import Linker
3236
import SysTools
37+
#endif
3338

3439

3540
-- | This GHC plugin is required to support ahead-of-time compilation for the
@@ -141,7 +146,11 @@ objectPaths guts (Rec bs) = concat <$> mapM (objectAnns guts) (map fst bs)
141146
objectAnns :: ModGuts -> CoreBndr -> CoreM [FilePath]
142147
objectAnns guts bndr = do
143148
anns <- getAnnotations deserializeWithData guts
144-
return [ path | Object path <- lookupWithDefaultUFM anns [] (varUnique bndr) ]
149+
#if __GLASGOW_HASKELL__ >= 900
150+
return [ path | Object path <- lookupWithDefaultUFM (snd anns) [] (varName bndr) ]
151+
#else
152+
return [ path | Object path <- lookupWithDefaultUFM anns [] (varUnique bndr) ]
153+
#endif
145154

146155
objectMapPath :: DynFlags -> FilePath
147156
objectMapPath DynFlags{..}

accelerate-llvm-native/src/Data/Array/Accelerate/LLVM/Native/Plugin/BuildInfo.hs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
module Data.Array.Accelerate.LLVM.Native.Plugin.BuildInfo
1414
where
1515

16+
#if __GLASGOW_HASKELL__ >= 900
17+
import GHC.Unit
18+
import GHC.Utils.Binary
19+
#else
20+
import Binary
1621
import Module
22+
#endif
1723

1824
import Data.Map ( Map )
19-
import Data.Serialize
20-
import Formatting
2125
import System.Directory
2226
import System.FilePath
23-
import qualified Data.ByteString as B
2427
import qualified Data.Map as Map
28+
import qualified Data.Map.Internal as Map
2529

2630
import Data.Array.Accelerate.Error
2731

@@ -34,28 +38,34 @@ readBuildInfo path = do
3438
exists <- doesFileExist path
3539
if not exists
3640
then return Map.empty
37-
else do
38-
f <- B.readFile path
39-
case decode f of
40-
Left err -> internalError string err
41-
Right m -> return m
41+
else get =<< readBinMem path
4242

4343
writeBuildInfo :: FilePath -> Map Module [FilePath] -> IO ()
44-
writeBuildInfo path objs = B.writeFile path (encode objs)
44+
writeBuildInfo path objs = do
45+
h <- openBinMem 4096
46+
put_ h objs
47+
writeBinMem h path
4548

4649

47-
instance Serialize Module where
48-
put (Module p n) = put p >> put n
49-
get = do
50-
p <- get
51-
n <- get
52-
return (Module p n)
50+
instance (Binary k, Binary v) => Binary (Map k v) where
51+
get h = do
52+
t <- getByte h
53+
case t of
54+
0 -> return Map.Tip
55+
_ -> do
56+
s <- get h
57+
k <- get h
58+
a <- get h
59+
l <- get h
60+
r <- get h
61+
return $ Map.Bin s k a l r
5362

54-
instance Serialize UnitId where
55-
put u = put (unitIdString u)
56-
get = stringToUnitId <$> get
57-
58-
instance Serialize ModuleName where
59-
put m = put (moduleNameString m)
60-
get = mkModuleName <$> get
63+
put_ h Map.Tip = putByte h 0
64+
put_ h (Map.Bin s k a l r) = do
65+
putByte h 1
66+
put_ h s
67+
put_ h k
68+
put_ h a
69+
put_ h l
70+
put_ h r
6171

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../accelerate-llvm/src/Language/Haskell/TH/Extra.hs

0 commit comments

Comments
 (0)