-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathArray.hs
More file actions
262 lines (232 loc) · 8.47 KB
/
Array.hs
File metadata and controls
262 lines (232 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_HADDOCK hide #-}
-- |
-- Module : Data.Array.Accelerate.LLVM.CodeGen.Array
-- Copyright : [2015..2020] The Accelerate Team
-- License : BSD3
--
-- Maintainer : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability : experimental
-- Portability : non-portable (GHC extensions)
--
module Data.Array.Accelerate.LLVM.CodeGen.Array (
readArray,
writeArray,
) where
import Control.Applicative
import Prelude hiding ( read )
import Data.Bits
import LLVM.AST.Type.AddrSpace
import LLVM.AST.Type.Instruction
import LLVM.AST.Type.Instruction.Volatile
import LLVM.AST.Type.Operand
import LLVM.AST.Type.Constant
import LLVM.AST.Type.Representation
import Data.Array.Accelerate.Representation.Array
import Data.Array.Accelerate.Representation.Type
import Data.Array.Accelerate.LLVM.CodeGen.IR
import Data.Array.Accelerate.LLVM.CodeGen.Monad
import Data.Array.Accelerate.LLVM.CodeGen.Ptr
import Data.Array.Accelerate.LLVM.CodeGen.Sugar
import Data.Array.Accelerate.LLVM.CodeGen.Constant
-- | Read a value from an array at the given index
--
{-# INLINEABLE readArray #-}
readArray
:: IntegralType int
-> IRArray (Array sh e)
-> Operands int
-> CodeGen arch (Operands e)
readArray int (IRArray (ArrayR _ tp) _ adata addrspace volatility) (op int -> ix) =
readArrayData addrspace volatility int ix tp adata
readArrayData
:: AddrSpace
-> Volatility
-> IntegralType int
-> Operand int
-> TypeR e
-> Operands e
-> CodeGen arch (Operands e)
readArrayData a v i ix = read
where
read :: TypeR e -> Operands e -> CodeGen arch (Operands e)
read TupRunit OP_Unit = return OP_Unit
read (TupRpair t2 t1) (OP_Pair a2 a1) = OP_Pair <$> read t2 a2 <*> read t1 a1
read (TupRsingle e) (asPtr a . op e -> arr) = ir e <$> readArrayPrim a v e i arr ix
readArrayPrim
:: AddrSpace
-> Volatility
-> ScalarType e
-> IntegralType int
-> Operand (Ptr e)
-> Operand int
-> CodeGen arch (Operand e)
readArrayPrim a v e i arr ix = do
p <- getElementPtr a e i arr ix
x <- load a e v p
return x
-- | Write a value into an array at the given index
--
{-# INLINEABLE writeArray #-}
writeArray
:: IntegralType int
-> IRArray (Array sh e)
-> Operands int
-> Operands e
-> CodeGen arch ()
writeArray int (IRArray (ArrayR _ tp) _ adata addrspace volatility) (op int -> ix) val =
writeArrayData addrspace volatility int ix tp adata val
writeArrayData
:: AddrSpace
-> Volatility
-> IntegralType int
-> Operand int
-> TypeR e
-> Operands e
-> Operands e
-> CodeGen arch ()
writeArrayData a v i ix = write
where
write :: TypeR e -> Operands e -> Operands e -> CodeGen arch ()
write TupRunit OP_Unit OP_Unit = return ()
write (TupRpair t2 t1) (OP_Pair a2 a1) (OP_Pair v2 v1) = write t1 a1 v1 >> write t2 a2 v2
write (TupRsingle e) (asPtr a . op e -> arr) (op e -> val) = writeArrayPrim a v e i arr ix val
writeArrayPrim
:: AddrSpace
-> Volatility
-> ScalarType e
-> IntegralType int
-> Operand (Ptr e)
-> Operand int
-> Operand e
-> CodeGen arch ()
writeArrayPrim a v e i arr ix x = do
p <- getElementPtr a e i arr ix
_ <- store a v e p x
return ()
-- | A wrapper around the GetElementPtr instruction, which correctly
-- computes the pointer offset for non-power-of-two SIMD types
--
getElementPtr
:: AddrSpace
-> ScalarType e
-> IntegralType int
-> Operand (Ptr e)
-> Operand int
-> CodeGen arch (Operand (Ptr e))
getElementPtr _ SingleScalarType{} _ arr ix = instr' $ GetElementPtr arr [ix]
getElementPtr a (VectorScalarType v) i arr ix
| VectorType n _ <- v
, IntegralDict <- integralDict i
= if popCount n == 1
then instr' $ GetElementPtr arr [ix]
else do
-- Note the initial zero into to the GEP instruction. It is not
-- really recommended to use GEP to index into vector elements, but
-- is not forcefully disallowed (at this time)
ix' <- instr' $ Mul (IntegralNumType i) ix (integral i (fromIntegral n))
p' <- instr' $ GetElementPtr arr [integral i 0, ix']
p <- instr' $ PtrCast (PtrPrimType (ScalarPrimType (VectorScalarType v)) a) p'
return p
-- | A wrapper around the Load instruction, which splits non-power-of-two
-- SIMD types into a sequence of smaller reads.
--
-- Note: [Non-power-of-two loads and stores]
--
-- Splitting this operation a sequence of smaller power-of-two stores does
-- not work because those instructions may (will) violate alignment
-- restrictions, causing a general protection fault. So, we simply
-- implement those stores as a sequence of stores for each individual
-- element.
--
-- We could do runtime checks for what the pointer alignment is and perform
-- a vector store when we align on the right boundary, but I'm not sure the
-- extra complexity is worth it.
--
load :: AddrSpace
-> ScalarType e
-> Volatility
-> Operand (Ptr e)
-> CodeGen arch (Operand e)
load addrspace e v p
| SingleScalarType{} <- e = instr' $ Load e v p
| VectorScalarType s <- e
, VectorType n base <- s
= if popCount n == 1
then instr' $ Load e v p
else do
p' <- instr' $ PtrCast (PtrPrimType (ScalarPrimType (SingleScalarType base)) addrspace) p
--
let go i w
| i >= n = return w
| otherwise = do
q <- instr' $ GetElementPtr p' [integral integralType i]
r <- instr' $ Load (SingleScalarType base) v q
w' <- instr' $ InsertElement integralType w (constOp i) r
go (i+1) w'
--
go 0 (undef e)
-- | A wrapper around the Store instruction, which splits non-power-of-two
-- SIMD types into a sequence of smaller writes.
--
-- See: [Non-power-of-two loads and stores]
--
store :: AddrSpace
-> Volatility
-> ScalarType e
-> Operand (Ptr e)
-> Operand e
-> CodeGen arch ()
store addrspace volatility e p v
| SingleScalarType{} <- e = do_ $ Store volatility p v
| VectorScalarType s <- e
, VectorType n base <- s
= if popCount n == 1
then do_ $ Store volatility p v
else do
p' <- instr' $ PtrCast (PtrPrimType (ScalarPrimType (SingleScalarType base)) addrspace) p
--
let go i
| i >= n = return ()
| otherwise = do
x <- instr' $ ExtractElement integralType v (constOp i)
q <- instr' $ GetElementPtr p' [integral integralType i]
_ <- instr' $ Store volatility q x
go (i+1)
go 0
{--
let
go :: forall arch n t. SingleType t -> Int32 -> Operand (Ptr t) -> Operand (Vec n t) -> CodeGen arch ()
go t offset ptr' val'
| offset >= size = return ()
| otherwise = do
let remaining = size - offset
this = setBit 0 (finiteBitSize remaining - countLeadingZeros remaining - 1)
vec' = VectorType (fromIntegral this) t
ptr_vec' = PtrPrimType (ScalarPrimType (VectorScalarType vec')) addrspace
repack :: Int32 -> Operand (Vec m t) -> CodeGen arch (Operand (Vec m t))
repack j u
| j >= this = return u
| otherwise = do
x <- instr' $ ExtractElement (offset + j) val'
v <- instr' $ InsertElement j u x
repack (j+1) v
if remaining == 1
then do
x <- instr' $ ExtractElement offset val'
_ <- instr' $ Store volatility ptr' x
return ()
else do
v <- repack 0 $ undef (VectorScalarType vec')
p <- instr' $ PtrCast ptr_vec' ptr'
_ <- instr' $ Store volatility p v
q <- instr' $ GetElementPtr ptr' [integral integralType this]
go t (offset + this) q val'
ptr' <- instr' $ PtrCast (PtrPrimType (ScalarPrimType (SingleScalarType base)) addrspace) ptr
go base 0 ptr' val
where
VectorType (fromIntegral -> size) base = vec
--}