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