Skip to content

Commit 404fbf7

Browse files
authored
[Hexagon] Refactor directory structure to accommodate new runtime (#9354)
* Move hexagon runtime files used for ARM offload to Hexagon into runtime/hexagon/android and runtime files compiled for hexagon into runtime/hexagon/hexagon. Use common hexagon_module.h for both hexagon and android runtimes. * Apply clang formatting to hexagon files on main. * Apply cpp-lint
1 parent f7430e9 commit 404fbf7

29 files changed

+173
-136
lines changed

cmake/modules/Hexagon.cmake

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ endif()
137137
if(USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}")
138138
find_hexagon_toolchain()
139139
message(STATUS "Hexagon toolchain: ${HEXAGON_TOOLCHAIN}")
140-
file(GLOB RUNTIME_HEXAGON_SIM_SRCS src/runtime/hexagon/sim/*.cc)
140+
file(GLOB RUNTIME_HEXAGON_SIM_SRCS src/runtime/hexagon/android/sim/*.cc)
141141
include_directories(SYSTEM "${HEXAGON_TOOLCHAIN}/include/iss")
142142
link_directories("${HEXAGON_TOOLCHAIN}/lib/iss")
143143
list(APPEND TVM_RUNTIME_LINKER_LIBS "-lwrapper")
144144
ExternalProject_Add(sim_dev
145-
SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/runtime/hexagon/sim/driver"
145+
SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/runtime/hexagon/android/sim/driver"
146146
CMAKE_ARGS
147147
"-DCMAKE_C_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang"
148148
"-DCMAKE_CXX_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang++"
@@ -152,7 +152,7 @@ if(USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}")
152152
elseif(USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
153153
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
154154
find_hexagon_toolchain()
155-
file(GLOB RUNTIME_HEXAGON_DEVICE_SRCS src/runtime/hexagon/target/*.cc)
155+
file(GLOB RUNTIME_HEXAGON_DEVICE_SRCS src/runtime/hexagon/android/target/*.cc)
156156

157157
include_directories(SYSTEM
158158
${HEXAGON_SDK_INCLUDES}
@@ -166,7 +166,10 @@ elseif(USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
166166
endif()
167167
endif()
168168

169-
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/*.cc)
169+
if(BUILD_FOR_HEXAGON AND USE_HEXAGON_DEVICE STREQUAL "${PICK_NONE}")
170+
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/hexagon/*.cc)
171+
else()
172+
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/android/*.cc)
173+
endif()
170174
list(APPEND RUNTIME_SRCS ${RUNTIME_HEXAGON_SRCS} ${RUNTIME_HEXAGON_SIM_SRCS}
171175
${RUNTIME_HEXAGON_DEVICE_SRCS})
172-
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef TVM_RUNTIME_HEXAGON_ANDROID_HEXAGON_DEVICE_H_
21+
#define TVM_RUNTIME_HEXAGON_ANDROID_HEXAGON_DEVICE_H_
22+
23+
#include <tvm/runtime/logging.h>
24+
#include <tvm/runtime/module.h>
25+
26+
#include <memory>
27+
#include <string>
28+
29+
#include "../../meta_data.h"
30+
31+
namespace tvm {
32+
namespace runtime {
33+
namespace hexagon {
34+
35+
/*!
36+
* \brief Low-level interface for communicating with Hexagon devices.
37+
*/
38+
class Device {
39+
public:
40+
/*!
41+
* \brief Allocate memory on device.
42+
* \param size Requested size.
43+
* \param align Requested alignment.
44+
* \return Pointer (local to the device) of the allocated memory,
45+
* or nullptr if allocation failed.
46+
*/
47+
virtual void* Alloc(unsigned size, unsigned align) = 0;
48+
/*!
49+
* \brief Release allocated memory on device.
50+
* \param ptr Pointer to memory previously allocated by \ref Alloc.
51+
*/
52+
virtual void Free(void* ptr) = 0;
53+
/*!
54+
* \brief Allocate VTCM memory on device.
55+
* \param size Requested size.
56+
* \param align Requested alignment.
57+
* \return Pointer (local to the device) of the allocated memory,
58+
* or nullptr if allocation failed.
59+
*/
60+
virtual void* AllocVtcm(unsigned size, unsigned align) = 0;
61+
/*!
62+
* \brief Release allocated VTCM memory on device.
63+
* \param ptr Pointer to memory previously allocated by \ref AllocVtcm.
64+
*/
65+
virtual void FreeVtcm(void* ptr) = 0;
66+
/*!
67+
* \brief Copy a block of data on device to another location on the device.
68+
* \param dst Pointer (local to device) to the destination buffer.
69+
* \param src Pointer (local to device) of the source buffer.
70+
* \param len Number of bytes to copy.
71+
*/
72+
virtual void CopyDeviceToDevice(void* dst, const void* src, unsigned len) = 0;
73+
/*!
74+
* \brief Copy a block of data from device to host.
75+
* \param host_dst Pointer (local to host) to the destination buffer.
76+
* \param src Pointer (local to device) to the source buffer.
77+
* \param len Number of bytes to copy.
78+
*/
79+
virtual void CopyDeviceToHost(void* host_dst, const void* src, unsigned len) = 0;
80+
/*!
81+
* \brief Copy a block of data from host to device.
82+
* \param dst Pointer (local to device) to the destination buffer.
83+
* \param host_src Pointer (local to host) to the source buffer.
84+
* \param len Number of bytes to copy.
85+
*/
86+
virtual void CopyHostToDevice(void* dst, const void* host_src, unsigned len) = 0;
87+
/*!
88+
* \brief Load a module (typically a shared library) into device.
89+
* \param data Name of the shared library.
90+
* \param fmt Format of the library (currently ignored).
91+
* \return Pointer to the loaded module.
92+
* \note Currently only one module can be loaded at any given time.
93+
*/
94+
virtual void* Load(const std::string& data, const std::string& fmt) = 0;
95+
/*!
96+
* \brief Unload a module from device.
97+
* \param mod Pointer to a loaded module returned by \ref Load.
98+
*/
99+
virtual void Unload(void* mod) = 0;
100+
/*!
101+
* \brief Find the address of an object in the currently loaded module.
102+
* \param sym Name of the object.
103+
* \return Address of the located object, or nullptr if object was
104+
* not found.
105+
*/
106+
virtual void* Resolve(const std::string& sym) = 0;
107+
/*!
108+
* \brief Invoke a function on device with given arguments.
109+
* \param func Address (local to device) of the function to call.
110+
* \param scalar Pointer to an array of 32-bit values that will be
111+
* passed via consecutive registers: r0..r5. This array
112+
* includes dummy values for skipped registers.
113+
* \param sc_num Number of values in the "scalar" array.
114+
* \param stack Pointer to an array of 32-bit values that will be
115+
* passed on the stack. This array includes dummy values
116+
* for padding.
117+
* \param st_num Number of values in the "stack" array.
118+
*/
119+
virtual void Call(void* func, uint32_t* scalar, unsigned sc_num, uint32_t* stack,
120+
unsigned st_num) = 0;
121+
122+
virtual ~Device() = 0;
123+
124+
static std::shared_ptr<Device> Global();
125+
static bool ValidateDeviceId(decltype(DLDevice::device_id) device_id) {
126+
// Only supporting a single device for now.
127+
return device_id == 0;
128+
}
129+
};
130+
131+
} // namespace hexagon
132+
133+
} // namespace runtime
134+
} // namespace tvm
135+
#endif // TVM_RUNTIME_HEXAGON_ANDROID_HEXAGON_DEVICE_H_

src/runtime/hexagon/hexagon_device_api.cc renamed to src/runtime/hexagon/android/hexagon_device_api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <algorithm>
2525
#include <cstring>
2626

27-
#include "hexagon_module.h"
27+
#include "hexagon_device.h"
2828

2929
namespace tvm {
3030
namespace runtime {

src/runtime/hexagon/hexagon_module.cc renamed to src/runtime/hexagon/android/hexagon_module.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
#include "hexagon_module.h"
20+
#include "../hexagon_module.h"
2121

2222
#ifdef __ANDROID__
2323
#include <android/log.h>
@@ -31,8 +31,8 @@
3131
#include <unordered_map>
3232
#include <vector>
3333

34-
#include "../file_utils.h"
35-
#include "../meta_data.h"
34+
#include "../../file_utils.h"
35+
#include "hexagon_device.h"
3636

3737
namespace tvm {
3838
namespace runtime {

src/runtime/hexagon/sim/driver/CMakeLists.txt renamed to src/runtime/hexagon/android/sim/driver/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ add_executable(sim_dev ${SOURCE_FILES})
6161
target_include_directories(sim_dev
6262
PUBLIC "."
6363
PUBLIC ".."
64-
PUBLIC "../../../../../include"
64+
PUBLIC "../../../../../../include"
6565
)
6666
target_include_directories(sim_dev SYSTEM
67-
PUBLIC "../../../../../3rdparty/dlpack/include"
67+
PUBLIC "../../../../../../3rdparty/dlpack/include"
6868
)
6969

7070
target_link_libraries(sim_dev "-ldl")
File renamed without changes.

src/runtime/hexagon/sim/driver/pthread.h renamed to src/runtime/hexagon/android/sim/driver/pthread.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
#ifndef TVM_RUNTIME_HEXAGON_SIM_DRIVER_PTHREAD_H_
21-
#define TVM_RUNTIME_HEXAGON_SIM_DRIVER_PTHREAD_H_
20+
#ifndef TVM_RUNTIME_HEXAGON_ANDROID_SIM_DRIVER_PTHREAD_H_
21+
#define TVM_RUNTIME_HEXAGON_ANDROID_SIM_DRIVER_PTHREAD_H_
2222

2323
#define _PROVIDE_POSIX_TIME_DECLS 1
2424
#include <time.h>
@@ -89,4 +89,4 @@ pthread_t pthread_self(void);
8989
}
9090
#endif
9191

92-
#endif // TVM_RUNTIME_HEXAGON_SIM_DRIVER_PTHREAD_H_
92+
#endif // TVM_RUNTIME_HEXAGON_ANDROID_SIM_DRIVER_PTHREAD_H_

src/runtime/hexagon/sim/driver/sched.h renamed to src/runtime/hexagon/android/sim/driver/sched.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
#ifndef TVM_RUNTIME_HEXAGON_SIM_DRIVER_SCHED_H_
21-
#define TVM_RUNTIME_HEXAGON_SIM_DRIVER_SCHED_H_
20+
#ifndef TVM_RUNTIME_HEXAGON_ANDROID_SIM_DRIVER_SCHED_H_
21+
#define TVM_RUNTIME_HEXAGON_ANDROID_SIM_DRIVER_SCHED_H_
2222

2323
#ifdef __cplusplus
2424
extern "C" {
@@ -28,4 +28,4 @@ int sched_yield(void);
2828
}
2929
#endif
3030

31-
#endif // TVM_RUNTIME_HEXAGON_SIM_DRIVER_SCHED_H_
31+
#endif // TVM_RUNTIME_HEXAGON_ANDROID_SIM_DRIVER_SCHED_H_

0 commit comments

Comments
 (0)