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 "xioutils.h"
14#include <assert.h>
15#include <iostream>
16
18 int size) {
19 int size_bytes = size * sizeof(int);
20 handle.virtualAddr = std::malloc(size_bytes);
21 if (handle.virtualAddr) {
22 handle.size = size_bytes;
23 // assign physical space in SystemC DDR memory controller
24 handle.physicalAddr = nextAlignedAddr;
25 // adjust nextAlignedAddr to the next 128-bit aligned address
26 nextAlignedAddr = nextAlignedAddr + size_bytes;
27 uint64_t gapToAligned = nextAlignedAddr % 16; // 16byte (128bit)
28 if (gapToAligned > 0)
29 nextAlignedAddr += (16 - gapToAligned);
30 } else {
31 printf("ExtMemModel: Failed to allocate %d memory.\n", size_bytes);
32 }
33
34 _xaie->allocations.push_back(handle);
35
36 std::cout << "ExtMemModel constructor: " << _xaie << " virtual address "
37 << std::hex << handle.virtualAddr << ", physical address "
38 << handle.physicalAddr << ", size " << std::dec << handle.size
39 << std::endl;
40 return (int *)handle.virtualAddr;
41}
42
44 aiesim_ReadGM(handle.physicalAddr, handle.virtualAddr, handle.size);
45}
46
48 aiesim_WriteGM(handle.physicalAddr, handle.virtualAddr, handle.size);
49}
50
52 std::cout << "get_device_address: " << _xaie << " VA " << std::hex << VA
53 << std::dec << "\n";
54 for (auto i : _xaie->allocations) {
55 std::cout << "get_device_address: virtual address " << std::hex
56 << i.virtualAddr << ", physical address " << i.physicalAddr
57 << ", size " << std::dec << i.size << std::endl;
58
59 if (i.virtualAddr == VA)
60 return i.physicalAddr;
61 }
62 printf("ERROR: cannot get device address for allocation!\n");
63 assert(false);
64 return 0;
65}
u64 mlir_aie_get_device_address(aie_libxaie_ctx_t *_xaie, 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.
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.
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