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