MLIR-AIE
test_library.h
Go to the documentation of this file.
1//===- test_library.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//
9//===----------------------------------------------------------------------===//
10#ifndef AIE_TEST_LIBRARY_H
11#define AIE_TEST_LIBRARY_H
12
13#include "target.h"
14#include <stdio.h>
15#include <stdlib.h>
16
17#ifdef HSA_RUNTIME
18#include "hsa/hsa.h"
19#include "hsa/hsa_ext_amd.h"
20#include "hsa_ext_air.h"
21#endif
22
23#ifdef HSA_RUNTIME
24template <typename T>
25inline void mlir_aie_write_pkt(hsa_queue_t *q, uint32_t packet_id, T *pkt) {
26 reinterpret_cast<T *>(q->base_address)[packet_id] = *pkt;
27}
28#endif
29
30extern "C" {
31
32#define mlir_aie_check(s, r, v, errors) \
33 if (r != v) { \
34 printf("ERROR %s: %s expected %d, but was %d!\n", s, #r, v, r); \
35 errors++; \
36 }
37#define mlir_aie_check_float(s, r, v, errors) \
38 if (r != v) { \
39 printf("ERROR %s: %s expected %f, but was %f!\n", s, #r, v, r); \
40 errors++; \
41 }
42
43// class for using events and PF cpounters
45public:
46 EventMonitor(aie_libxaie_ctx_t *_xaie, u32 _col, u32 _row, u32 _pfc,
47 XAie_Events _startE, XAie_Events _endE, XAie_Events _resetE,
48 XAie_ModuleType _module) {
49 // EventMonitor(struct XAieGbl_Tile *_tilePtr, u32 _pfc, u32 _startE, u32
50 // _endE,
51 // u32 _resetE, XAie_ModuleType _module) {
52 // tilePtr = _tilePtr;
53 devInst = &(_xaie->DevInst);
54 row = _row;
55 col = _col;
56 pfc = _pfc;
57 mode = _module;
58 XAie_PerfCounterControlSet(devInst, XAie_TileLoc(col, row), mode, pfc,
59 _startE, _endE);
60
61 // mode = _mode; // 0: Core, 1: PL, 2, Mem
62 // if (mode == MODE_CORE) {
63 // XAieTileCore_PerfCounterControl(tilePtr, pfc, _startE, _endE, _resetE);
64 // } else if (mode == MODE_PL) {
65 // XAieTilePl_PerfCounterControl(tilePtr, pfc, _startE, _endE, _resetE);
66 // } else {
67 // XAieTileMem_PerfCounterControl(tilePtr, pfc, _startE, _endE, _resetE);
68 // }
69 }
70 void set() {
71 // XAie_PerfCounterSet(devInst, XAie_TileLoc(col,row), mode, pfc, val);
72 XAie_PerfCounterGet(devInst, XAie_TileLoc(col, row), mode, pfc, &start);
73 // if (mode == MODE_CORE) {
74 // start = XAieTileCore_PerfCounterGet(tilePtr, pfc);
75 // } else if (mode == MODE_PL) {
76 // start = XAieTilePl_PerfCounterGet(tilePtr, pfc);
77 // } else {
78 // start = XAieTileMem_PerfCounterGet(tilePtr, pfc);
79 // }
80 }
81 u32 read() {
82 u32 val;
83 XAie_PerfCounterGet(devInst, XAie_TileLoc(col, row), mode, pfc, &val);
84 return val;
85 // if (mode == MODE_CORE) {
86 // return XAieTileCore_PerfCounterGet(tilePtr, pfc);
87 // } else if (mode == MODE_PL) {
88 // return XAieTilePl_PerfCounterGet(tilePtr, pfc);
89 // } else {
90 // return XAieTileMem_PerfCounterGet(tilePtr, pfc);
91 // }
92 }
93 u32 diff() {
94 u32 end;
95 XAie_PerfCounterGet(devInst, XAie_TileLoc(col, row), mode, pfc, &end);
96 // if (mode == MODE_CORE) {
97 // end = XAieTileCore_PerfCounterGet(tilePtr, pfc);
98 // } else if (mode == MODE_PL) {
99 // end = XAieTilePl_PerfCounterGet(tilePtr, pfc);
100 // } else {
101 // end = XAieTileMem_PerfCounterGet(tilePtr, pfc);
102 // }
103 if (end < start) {
104 printf("WARNING: EventMonitor: performance counter wrapped!\n");
105 return 0; // TODO: fix this
106 } else {
107 return end - start;
108 }
109 }
110
111private:
112 u32 start;
113 u32 pfc;
114 XAie_ModuleType mode;
115 u8 col, row;
116 XAie_DevInst *devInst;
117};
118
119/*
120 ******************************************************************************
121 * Common functions
122 ******************************************************************************
123 */
124
125// This is a more elegant solution
126#ifdef HSA_RUNTIME
127hsa_status_t mlir_aie_packet_req_translation(hsa_agent_dispatch_packet_t *pkt,
128 uint64_t va);
129
130hsa_status_t mlir_aie_packet_nd_memcpy(
131 hsa_agent_dispatch_packet_t *pkt, uint16_t herd_id, uint8_t col,
132 uint8_t direction, uint8_t channel, uint8_t burst_len, uint8_t memory_space,
133 uint64_t phys_addr, uint32_t transfer_length1d, uint32_t transfer_length2d,
134 uint32_t transfer_stride2d, uint32_t transfer_length3d,
135 uint32_t transfer_stride3d, uint32_t transfer_length4d,
136 uint32_t transfer_stride4d);
137
138hsa_status_t mlir_aie_queue_dispatch_and_wait(
139 hsa_agent_t *agent, hsa_queue_t *q, uint64_t packet_id, uint64_t doorbell,
140 hsa_agent_dispatch_packet_t *pkt, bool destroy_signal = true);
141
142#endif
143
144/// @brief Initialize libXAIE and allocate a new context object.
145/// @return A pointer to the context
148
149int mlir_aie_init_device(aie_libxaie_ctx_t *ctx, uint32_t device_id = 0);
150
151int mlir_aie_acquire_lock(aie_libxaie_ctx_t *ctx, int col, int row, int lockid,
152 int lockval, int timeout);
153int mlir_aie_release_lock(aie_libxaie_ctx_t *ctx, int col, int row, int lockid,
154 int lockval, int timeout);
155u32 mlir_aie_read32(aie_libxaie_ctx_t *ctx, u64 addr);
156void mlir_aie_write32(aie_libxaie_ctx_t *ctx, u64 addr, u32 val);
157u32 mlir_aie_data_mem_rd_word(aie_libxaie_ctx_t *ctx, int col, int row,
158 u64 addr);
159void mlir_aie_data_mem_wr_word(aie_libxaie_ctx_t *ctx, int col, int row,
160 u64 addr, u32 data);
161
162u64 mlir_aie_get_tile_addr(aie_libxaie_ctx_t *ctx, int col, int row);
163
164/// Dump the contents of the memory associated with the given tile.
165void mlir_aie_dump_tile_memory(aie_libxaie_ctx_t *ctx, int col, int row);
166
167/// Clear the contents of the memory associated with the given tile.
168void mlir_aie_clear_tile_memory(aie_libxaie_ctx_t *ctx, int col, int row);
169
170/// Print the status of a dma represented by the given tile.
171void mlir_aie_print_dma_status(aie_libxaie_ctx_t *ctx, int col, int row);
172
173/// Print the status of a memtiledma represented by the given location.
174void mlir_aie_print_memtiledma_status(aie_libxaie_ctx_t *ctx, int col, int row);
175
176/// Print the status of a shimdma represented by the given location (row = 0).
177void mlir_aie_print_shimdma_status(aie_libxaie_ctx_t *ctx, int col, int row);
178
179/// Print the status of a core represented by the given tile.
180void mlir_aie_print_tile_status(aie_libxaie_ctx_t *ctx, int col, int row);
181
182/// Zero out the program and configuration memory of the tile.
183void mlir_aie_clear_config(aie_libxaie_ctx_t *ctx, int col, int row);
184
185/// Zero out the configuration memory of the shim tile.
186void mlir_aie_clear_shim_config(aie_libxaie_ctx_t *ctx, int col, int row);
187
188void computeStats(u32 performance_counter[], int n);
189
190} // extern "C"
191
192#endif
EventMonitor(aie_libxaie_ctx_t *_xaie, u32 _col, u32 _row, u32 _pfc, XAie_Events _startE, XAie_Events _endE, XAie_Events _resetE, XAie_ModuleType _module)
XAie_DevInst DevInst
Definition target.h:33
void mlir_aie_print_memtiledma_status(aie_libxaie_ctx_t *ctx, int col, int row)
Print the status of a memtiledma represented by the given location.
int mlir_aie_acquire_lock(aie_libxaie_ctx_t *ctx, int col, int row, int lockid, int lockval, int timeout)
Acquire a physical lock.
u32 mlir_aie_data_mem_rd_word(aie_libxaie_ctx_t *ctx, int col, int row, u64 addr)
Read a value from the data memory of a particular tile memory.
void mlir_aie_clear_tile_memory(aie_libxaie_ctx_t *ctx, int col, int row)
Clear the contents of the memory associated with the given tile.
u64 mlir_aie_get_tile_addr(aie_libxaie_ctx_t *ctx, int col, int row)
Return the base address of the given tile.
void mlir_aie_clear_shim_config(aie_libxaie_ctx_t *ctx, int col, int row)
Zero out the configuration memory of the shim tile.
void mlir_aie_clear_config(aie_libxaie_ctx_t *ctx, int col, int row)
Zero out the program and configuration memory of the tile.
void mlir_aie_print_dma_status(aie_libxaie_ctx_t *ctx, int col, int row)
Print the status of a dma represented by the given tile.
void mlir_aie_deinit_libxaie(aie_libxaie_ctx_t *)
Release access to the libXAIE context.
int mlir_aie_release_lock(aie_libxaie_ctx_t *ctx, int col, int row, int lockid, int lockval, int timeout)
Release a physical lock.
int mlir_aie_init_device(aie_libxaie_ctx_t *ctx, uint32_t device_id=0)
Initialize the device represented by the context.
void mlir_aie_print_shimdma_status(aie_libxaie_ctx_t *ctx, int col, int row)
Print the status of a shimdma represented by the given location (row = 0).
u32 mlir_aie_read32(aie_libxaie_ctx_t *ctx, u64 addr)
Read the AIE configuration memory at the given physical address.
void mlir_aie_dump_tile_memory(aie_libxaie_ctx_t *ctx, int col, int row)
Dump the contents of the memory associated with the given tile.
void computeStats(u32 performance_counter[], int n)
Given an array of values, compute and print statistics about those values.
void mlir_aie_write32(aie_libxaie_ctx_t *ctx, u64 addr, u32 val)
Write the AIE configuration memory at the given physical address.
aie_libxaie_ctx_t * mlir_aie_init_libxaie()
Initialize libXAIE and allocate a new context object.
void mlir_aie_data_mem_wr_word(aie_libxaie_ctx_t *ctx, int col, int row, u64 addr, u32 data)
Write a value to the data memory of a particular tile memory.
void mlir_aie_print_tile_status(aie_libxaie_ctx_t *ctx, int col, int row)
Print the status of a core represented by the given tile.