Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUNTIME] Hexagon driver for offloading kernels to simulator #5492

Merged
merged 3 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmake/modules/Hexagon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

include(ExternalProject)

set(PICK_SIM "sim")
set(PICK_HW "target")
set(PICK_NONE "OFF")
Expand Down Expand Up @@ -77,6 +79,13 @@ if(USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}")
include_directories("${HEXAGON_TOOLCHAIN}/include/iss")
link_directories("${HEXAGON_TOOLCHAIN}/lib/iss")
list(APPEND TVM_RUNTIME_LINKER_LIBS "-lwrapper")
ExternalProject_Add(sim_dev
SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/runtime/hexagon/sim/driver"
CMAKE_ARGS
"-DCMAKE_C_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang"
"-DCMAKE_CXX_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang++"
INSTALL_COMMAND "true"
)
elseif(USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
find_hexagon_sdk_root()
find_hexagon_toolchain()
Expand Down
62 changes: 62 additions & 0 deletions src/runtime/hexagon/sim/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the Apache Software Foundation (ASF) under one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better move this to cmake/modules/contrib dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a standalone CMakeLists.txt. The files in contrib seem to be some cmake "sub-files" for including in other cmake files. Could you elaborate on what you were suggesting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VTA.cmake is an example of CMake config for building standalone lib. Also, given that it's unfriendly to require users to navigate to src/runtime/hexagon/sim/driver, and having CMakeLists.txt exist under src directory seems a bit odd to me, I think it's better to put this under cmake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It's not just for a standalone lib/program, it's a standalone CMakeLists.txt. All other cmake files in the cmake directory are included in the main CMakeLists.txt. This one isn't.
  2. The driver has to be compiled with hexagon-clang. That doesn't correlate with any other compilation that uses hexagon-clang (libtvm_runtime.so needs to be compiled for Hexagon in both cases, when using a simulator and a hardware device), so it shouldn't really be included in the main CMakeFile.txt.

I was planning to add it as a dependency to the main CMakeLists.txt after it's checked in. This would remove the need for manual running.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong opinion on this, but suggest to improve the experience of potential users. They suppose to turn USE_HEXAGON_SIM_DRIVER flag ON in the config.cmake to have this driver build automatically, instead of navigating to src/runtime/hexagon/sim/driver and start another build configure again.

To achieve this goal,

  1. Rename this file to HexagonSimDriver.cmake, and have it included in the main CMakeLists.txt
  2. The following CMake config would be helpful to locate the hexagon-clang and have the proposed sim driver correctly compiled.

https://github.com/apache/incubator-tvm/blob/19f322d70335c13c0d4f7af15c684bd414b90b40/cmake/modules/Hexagon.cmake#L22

Copy link
Contributor Author

@kparzysz-quic kparzysz-quic May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've already added a commit that makes this a dependency and now it's compiled automatically. Does that address your concerns about user-friendliness?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, thanks.

# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

project(SIM_DEV C CXX)
cmake_minimum_required(VERSION 3.0.2)

set(CMAKE_SYSTEM_NAME "Linux")

if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/config.cmake)
include(${CMAKE_CURRENT_BINARY_DIR}/config.cmake)
endif()

set(EXTRA_CXX_FLAGS
"-O2"
"-Wno-format"
"-mhvx -mhvx-length=128b"
"-mv60"
"-stdlib=libc++"
)

set(EXTRA_LINK_FLAGS
"-stdlib=libc++"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I met the symbol (related with pthread symbol If I remember correctly) can not find. The fix is to use -stdlib=libstdc++. The complete command is -G0 -ldl -stdlib=libstdc++ -Wl,--force-dynamic -Wl,-E -Wl,--whole-archive -lm, which is the same as your original pr:https://github.com/kparzysz-quic/tvm/blob/b4e5960bb4f804e42c0abe60045621ae1d075f27/src/runtime/hexagon/sim/driver/sim_device.cc#L38. Now, we change to libc++, could you explain how does it solve?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We define our own pthread symbols now, so it works ok now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining, make sense to me now

"-G0"
"-Wl,--force-dynamic"
"-Wl,--export-dynamic"
"-Wl,--whole-archive" # This should link entire libc, libc++ and libc+abi.
"-Wl,--defsym=HEAP_SIZE=0x40000000"
)

string(REGEX REPLACE ";" " " EXTRA_CXX_FLAGS_STR "${EXTRA_CXX_FLAGS}")
string(REGEX REPLACE ";" " " EXTRA_LINK_FLAGS_STR "${EXTRA_LINK_FLAGS}")

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${EXTRA_CXX_FLAGS_STR} ${CMAKE_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${EXTRA_LINK_FLAGS_STR} ${CMAKE_EXE_LINKER_FLAGS}")

# Set project properties.

file(GLOB SOURCE_FILES "*.cc")
add_executable(sim_dev ${SOURCE_FILES})
target_include_directories(sim_dev
PUBLIC "."
PUBLIC ".."
PUBLIC "../../../../../include"
PUBLIC "../../../../../3rdparty/dlpack/include"
)

target_link_libraries(sim_dev "-ldl")
38 changes: 38 additions & 0 deletions src/runtime/hexagon/sim/driver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

# Hexagon simulator driver

The driver (`sim_dev` executable) is the process running on the Hexagon simulator that handles the Hexagon-side communication with the TVM runtime running on x86. The location of `sim_dev` should be added to `PATH` before running any python code that uses Hexagon. The `sim_dev` executable is not intended to be run by users, it is automatically loaded by the simulator control code (in `hexagon_device_sim.cc`).

### Prerequisites

1. Hexagon C/C++ toolchain (such as the one in Hexagon SDK version 3.5.0 or later).

Hexagon SDK is available at //developer.qualcomm.com/software/hexagon-dsp-sdk.

### Configuring

Set
```
CMAKE_C_COMPILER=hexagon-clang
CMAKE_CXX_COMPILER=hexagon-clang++
```

### Building

There are no special options required for `make` (or the tool selected with `cmake`). The location of the resulting binary `sim_dev` should be added to `PATH`.
Loading