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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 | class MemrefBufferFType(MLIRBufferFType, MLIRUnpackableFType):
"""
A ftype for memref buffers that defines MLIR operations.
"""
def __init__(self, element_type: FType):
self._element_type = element_type
def __eq__(self, other):
if not isinstance(other, MemrefBufferFType):
return False
return self._element_type == other._element_type
def __hash__(self):
return hash(("MemrefBufferFType", self._element_type))
@property
def element_type(self):
return self._element_type
def __call__(self, length: int = 0):
library = MLIRMemrefBufferBackend.library(self)
descriptor = library.alloc(length)
return MemrefBuffer(descriptor, self.element_type, library)
# This is the pointer type pass into the params
def mlir_type(self):
return "!llvm.ptr"
# This is the rank-1 sized memref type used for MLIR buffer operations.
def mlir_buffer_type(self):
return f"memref<?x{mlir_type(self.element_type)}>"
# this is the rank-1 memref descriptor type
def mlir_descriptor_type(self):
return "!llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>"
# Return the current length of the MLIR NumPy buffer
def mlir_length(self, ctx: MLIRContext, buf: MLIRMemrefBufferFields):
desc_t = self.mlir_descriptor_type()
desc = ctx.new_ssa()
ctx.exec(f"{ctx.feed}{desc} = llvm.load {buf.box} : !llvm.ptr -> {desc_t}")
buffer = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{buffer} = builtin.unrealized_conversion_cast "
f"{desc} : {desc_t} to {self.mlir_buffer_type()}"
)
dim = ctx.constant(0, "index")
result = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{result} = memref.dim {buffer}, {dim} : "
f"{self.mlir_buffer_type()}"
)
return result
# Load the buffer into an SSA value
def mlir_load(
self,
ctx: MLIRContext,
buf: MLIRMemrefBufferFields,
idx: AssemblyExpression,
):
desc_t = self.mlir_descriptor_type()
desc = ctx.new_ssa()
ctx.exec(f"{ctx.feed}{desc} = llvm.load {buf.box} : !llvm.ptr -> {desc_t}")
buffer = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{buffer} = builtin.unrealized_conversion_cast "
f"{desc} : {desc_t} to {self.mlir_buffer_type()}"
)
index = mlir_cast_value(ctx, ctx(idx), idx.result_type, ftypes.intp)
result = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{result} = memref.load {buffer}[{index}] : "
f"{self.mlir_buffer_type()}"
)
return result
# Store a value into the memref buffer
def mlir_store(
self,
ctx: MLIRContext,
buf: MLIRMemrefBufferFields,
idx: AssemblyExpression,
value: AssemblyExpression,
):
desc_t = self.mlir_descriptor_type()
desc = ctx.new_ssa()
ctx.exec(f"{ctx.feed}{desc} = llvm.load {buf.box} : !llvm.ptr -> {desc_t}")
buffer = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{buffer} = builtin.unrealized_conversion_cast "
f"{desc} : {desc_t} to {self.mlir_buffer_type()}"
)
index = mlir_cast_value(ctx, ctx(idx), idx.result_type, ftypes.intp)
val = ctx(value)
ctx.exec(
f"{ctx.feed}memref.store {val}, {buffer}[{index}] : "
f"{self.mlir_buffer_type()}"
)
# Resize the memref buffer using memref.realloc
def mlir_resize(
self,
ctx: MLIRContext,
buf: MLIRMemrefBufferFields,
new_len: AssemblyExpression,
):
desc_t = self.mlir_descriptor_type()
desc = ctx.new_ssa()
ctx.exec(f"{ctx.feed}{desc} = llvm.load {buf.box} : !llvm.ptr -> {desc_t}")
buffer = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{buffer} = builtin.unrealized_conversion_cast "
f"{desc} : {desc_t} to {self.mlir_buffer_type()}"
)
result = ctx.new_ssa()
length = mlir_cast_value(ctx, ctx(new_len), new_len.result_type, ftypes.intp)
memref_t = self.mlir_buffer_type()
ctx.exec(
f"{ctx.feed}{result} = memref.realloc {buffer}({length}) : "
f"{memref_t} to {memref_t}"
)
desc = ctx.new_ssa()
ctx.exec(
f"{ctx.feed}{desc} = builtin.unrealized_conversion_cast "
f"{result} : {memref_t} to {desc_t}"
)
ctx.exec(f"{ctx.feed}llvm.store {desc}, {buf.box} : {desc_t}, !llvm.ptr")
# Use the pointer as the descriptor box.
def mlir_unpack(self, ctx: MLIRContext, _, val):
return MLIRMemrefBufferFields(ctx(val))
def mlir_repack(self, ctx, var_n, obj):
# Buffer operations already update the incoming descriptor in place.
pass
# Pass the address of the Python-owned descriptor across the MLIR ABI.
def serialize_to_mlir(self, obj: MemrefBuffer):
return ctypes.c_void_p(ctypes.addressof(obj.buffer))
def deserialize_from_mlir(self, obj, mlir_buffer):
# No copy-back is needed because Python and MLIR share the same allocation.
pass
# Wrap a MLIR descriptor for use as a Python Memref Buffer.
def construct_from_mlir(self, mlir_buffer):
descriptor = ctypes.cast(
mlir_buffer, ctypes.POINTER(mlir_ctype(self.mlir_buffer_type()))
).contents
return MemrefBuffer(
descriptor, self.element_type, MLIRMemrefBufferBackend.library(self)
)
|