MLIR-AIE 1.4.5.dev
memory_allocator.cpp
Go to the documentation of this file.
1//===- memory_allocator.cpp -------------------------------------*- 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#include "memory_allocator.h"
10#include <assert.h>
11#include <iostream>
12
13extern "C" {
14
15void ess_WriteGM(uint64_t addr, const void *data, uint64_t size);
16void ess_ReadGM(uint64_t addr, void *data, uint64_t size);
17}
18
20 int size) {
21 int size_bytes = size * sizeof(int);
22 handle.virtualAddr = std::malloc(size_bytes);
23 if (handle.virtualAddr) {
24 handle.size = size_bytes;
25 // assign physical space in SystemC DDR memory controller
26 handle.physicalAddr = nextAlignedAddr;
27 // adjust nextAlignedAddr to the next 128-bit aligned address
28 nextAlignedAddr = nextAlignedAddr + size_bytes;
29 uint64_t gapToAligned = nextAlignedAddr % 16; // 16byte (128bit)
30 if (gapToAligned > 0)
31 nextAlignedAddr += (16 - gapToAligned);
32 } else {
33 printf("ExtMemModel: Failed to allocate %d memory.\n", size_bytes);
34 }
35
36 ctx->allocations.push_back(handle);
37
38 std::cout << "ExtMemModel constructor: " << ctx << " virtual address "
39 << std::hex << handle.virtualAddr << ", physical address "
40 << handle.physicalAddr << ", size " << std::dec << handle.size
41 << std::endl;
42 return (int *)handle.virtualAddr;
43}
44
46 ess_ReadGM(handle.physicalAddr, handle.virtualAddr, handle.size);
47}
48
50 ess_WriteGM(handle.physicalAddr, handle.virtualAddr, handle.size);
51}
52
54 std::cout << "get_device_address: " << ctx << " VA " << std::hex << VA
55 << std::dec << "\n";
56 for (auto i : ctx->allocations) {
57 std::cout << "get_device_address: virtual address " << std::hex
58 << i.virtualAddr << ", physical address " << i.physicalAddr
59 << ", size " << std::dec << i.size << std::endl;
60
61 if (i.virtualAddr == VA)
62 return i.physicalAddr;
63 }
64 printf("ERROR: cannot get device address for allocation!\n");
65 assert(false);
66 return 0;
67}
void ess_ReadGM(uint64_t addr, void *data, uint64_t size)
u64 mlir_aie_get_device_address(aie_libxaie_ctx_t *ctx, void *VA)
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.
void ess_WriteGM(uint64_t addr, const void *data, uint64_t size)
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.
std::list< ext_mem_model_t > allocations
Definition target.h:26
uint64_t physicalAddr
Definition target.h:16
size_t size
Definition target.h:17
void * virtualAddr
Definition target.h:15