MLIR-AIE 1.4.5.dev
memory_allocator.h
Go to the documentation of this file.
1//===- memory_allocator.h ---------------------------------------*- C++ -*-===//
2//
3// Copyright (C) 2021-2022 Xilinx, Inc.
4// Copyright (C) 2023 Advanced Micro Devices, Inc.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef AIE_MEMORY_ALLOCATOR_H
10#define AIE_MEMORY_ALLOCATOR_H
11
12#include "target.h"
13
14#include <stdint.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18// #if defined(__AIESIM__)
19// #include "xioutils.h"
20// #include <iostream>
21// #endif
22
23extern "C" {
24
25/// Depending on the model of a particular device, this API supports several
26/// different scenarios for device memory allocation and reference.
27/// For instance, on the VCK190 with ARM host programmed through libXAIE,
28/// `mlir_aie_mem_alloc()` might allocate data in device DDR and return a
29/// cacheable mapping to it. `mlir_aie_sync_mem_cpu()` and
30/// `mlir_aie_sync_mem_dev()` would flush and invalidate caches.
31/// A device address would correspond to a DDR physical address.
32/// Alternatively in the AIESIM environment, `mlir_aie_mem_alloc()` allocates
33/// a duplicate buffer in the host memory and in the simulator memory for each
34/// allocation. `mlir_aie_sync_mem_cpu()` and `mlir_aie_sync_mem_dev()` make
35/// an explicit copy between these two buffers and device addresses are modeled
36/// in a simulator-specific way. Other combinations are also possible, largely
37/// representing different tradeoffs between efficiency of host data access vs.
38/// efficiency of accelerator access.
39
40// static variable for tracking current DDR physical addr during AIESIM
41static uint16_t nextAlignedAddr;
42
43/// @brief Allocate a buffer in device memory
44/// @param ctx The AIE context
45/// @param handle External memory model handle for tracking the allocation
46/// @param size The number of 32-bit words to allocate
47/// @return A host-side pointer that can write into the allocated buffer
48/// @note This is a minimal allocator intended for test use.
50 int size);
51
52/// @brief Synchronize the buffer from the device to the host CPU.
53/// This is expected to be called after the device writes data into
54/// device memory, so that the data can be read by the CPU. In
55/// a non-cache coherent system, this implies invalidating the
56/// processor cache associated with the buffer.
57/// @param handle External memory model handle identifying the buffer to
58/// synchronize
60
61/// @brief Synchronize the buffer from the host CPU to the device.
62/// This is expected to be called after the host writes data into
63/// device memory, so that the data can be read by the device. In
64/// a non-cache coherent system, this implies flushing the
65/// processor cache associated with the buffer.
66/// @param handle External memory model handle identifying the buffer to
67/// synchronize
69
70/// @brief Return a device address corresponding to the given host address.
71/// @param ctx The AIE context
72/// @param host_address A host-side pointer returned from mlir_aie_mem_alloc
73/// @return The device physical address corresponding to the host pointer
74u64 mlir_aie_get_device_address(aie_libxaie_ctx_t *ctx, void *host_address);
75
76} // extern "C"
77
78#endif
void mlir_aie_sync_mem_dev(ext_mem_model_t &handle)
Synchronize the buffer from the host CPU to the device.
u64 mlir_aie_get_device_address(aie_libxaie_ctx_t *ctx, void *host_address)
Return a device address corresponding to the given host address.
void mlir_aie_sync_mem_cpu(ext_mem_model_t &handle)
Synchronize the buffer from the device to the host CPU.
int * mlir_aie_mem_alloc(aie_libxaie_ctx_t *ctx, ext_mem_model_t &handle, int size)
Allocate a buffer in device memory.