From e7ae31f2ac821004456b08fecba1e60d719e4530 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 20:56:55 +0100 Subject: [PATCH 1/9] Make BUILD_HIPIFY_CLANG a cmake option Instead of deciding whether to build hipify-clang based on the presence of an LLVM path on the command line, have an explicit option. Do we want this default-on or default-off? I've defaulted it to on for now, but maybe we want the opposite? [ROCm/clr commit: 497a776f6c270ff14df02583594fcfeba4bfc586] --- projects/clr/hipamd/CMakeLists.txt | 9 ++++++++- projects/clr/hipamd/hipify-clang/CMakeLists.txt | 6 +----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/projects/clr/hipamd/CMakeLists.txt b/projects/clr/hipamd/CMakeLists.txt index 4c89c93668..1304e9f7d7 100644 --- a/projects/clr/hipamd/CMakeLists.txt +++ b/projects/clr/hipamd/CMakeLists.txt @@ -1,6 +1,11 @@ cmake_minimum_required(VERSION 2.8.3) project(hip) +############################# +# Options +############################# +option(BUILD_HIPIFY_CLANG "Enable building the CUDA->HIP converter" OFF) + ############################# # Setup config generation ############################# @@ -142,7 +147,9 @@ add_to_config(_buildInfo COMPILE_HIP_ATP_MARKER) # Build steps ############################# # Build clang hipify if enabled -add_subdirectory(hipify-clang) +if (BUILD_HIPIFY_CLANG) + add_subdirectory(hipify-clang) +endif() # Build hip_hcc if platform is hcc if(HIP_PLATFORM STREQUAL "hcc") diff --git a/projects/clr/hipamd/hipify-clang/CMakeLists.txt b/projects/clr/hipamd/hipify-clang/CMakeLists.txt index cb8354157b..7b43f2beb7 100644 --- a/projects/clr/hipamd/hipify-clang/CMakeLists.txt +++ b/projects/clr/hipamd/hipify-clang/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 2.8.8) project(hipify-clang) -set(BUILD_HIPIFY_CLANG 0 CACHE INTERNAL "") - if (HIPIFY_CLANG_LLVM_DIR) find_package(LLVM PATHS ${HIPIFY_CLANG_LLVM_DIR} REQUIRED NO_DEFAULT_PATH) else() @@ -10,7 +8,7 @@ else() return() endif() -option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" ON) +option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" OFF) # Disable the tests if `lit` or `FileCheck` is not installed. find_program(LIT_COMMAND lit) @@ -102,5 +100,3 @@ if (HIPIFY_CLANG_TESTS) add_dependencies(test-hipify-clang test-hipify) set_target_properties(test-hipify-clang PROPERTIES FOLDER "Tests") endif() - -set(BUILD_HIPIFY_CLANG 1 CACHE INTERNAL "") From 39b280d94847c301e8c7fc20c2c261973a58f0c1 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 20:59:30 +0100 Subject: [PATCH 2/9] Use add_dependencies to avoid duplication of pkg_hip_base [ROCm/clr commit: 414a473d5c6c78cc51d0bdcde20ea24ba7e022d7] --- projects/clr/hipamd/CMakeLists.txt | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/projects/clr/hipamd/CMakeLists.txt b/projects/clr/hipamd/CMakeLists.txt index 1304e9f7d7..dd8d604dd2 100644 --- a/projects/clr/hipamd/CMakeLists.txt +++ b/projects/clr/hipamd/CMakeLists.txt @@ -294,23 +294,19 @@ set(BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/packages/hip_base) configure_file(packaging/hip_base.txt ${BUILD_DIR}/CMakeLists.txt @ONLY) configure_file(packaging/hip_base.postinst ${BUILD_DIR}/postinst @ONLY) configure_file(packaging/hip_base.prerm ${BUILD_DIR}/prerm @ONLY) -if(NOT BUILD_HIPIFY_CLANG) - add_custom_target(pkg_hip_base COMMAND ${CMAKE_COMMAND} . - COMMAND rm -rf *.deb *.rpm *.tar.gz - COMMAND make package - COMMAND cp *.deb ${PROJECT_BINARY_DIR} - COMMAND cp *.rpm ${PROJECT_BINARY_DIR} - COMMAND cp *.tar.gz ${PROJECT_BINARY_DIR} - WORKING_DIRECTORY ${BUILD_DIR}) -else() - add_custom_target(pkg_hip_base COMMAND ${CMAKE_COMMAND} . - COMMAND rm -rf *.deb *.rpm *.tar.gz - COMMAND make package - COMMAND cp *.deb ${PROJECT_BINARY_DIR} - COMMAND cp *.rpm ${PROJECT_BINARY_DIR} - COMMAND cp *.tar.gz ${PROJECT_BINARY_DIR} - WORKING_DIRECTORY ${BUILD_DIR} - DEPENDS hipify-clang) + +add_custom_target(pkg_hip_base COMMAND ${CMAKE_COMMAND} . + COMMAND rm -rf *.deb *.rpm *.tar.gz + COMMAND make package + COMMAND cp *.deb ${PROJECT_BINARY_DIR} + COMMAND cp *.rpm ${PROJECT_BINARY_DIR} + COMMAND cp *.tar.gz ${PROJECT_BINARY_DIR} + WORKING_DIRECTORY ${BUILD_DIR} +) + +# Packaging needs to wait for hipify-clang to build if it's enabled... +if (BUILD_HIPIFY_CLANG) + add_dependencies(pkg_hip_base hipify-clang) endif() # Package: hip_hcc From 758c6ea57f229bfcd504f8a02f2902d2441b30f5 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 21:30:34 +0100 Subject: [PATCH 3/9] Don't attempt to find test dependencies if tests are disabled And while we're at it, introduce a handy program-finder macro [ROCm/clr commit: 384646334cca90b5d97c9eaa74904c6ea4f88bda] --- .../clr/hipamd/hipify-clang/CMakeLists.txt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/projects/clr/hipamd/hipify-clang/CMakeLists.txt b/projects/clr/hipamd/hipify-clang/CMakeLists.txt index 7b43f2beb7..13abd287c8 100644 --- a/projects/clr/hipamd/hipify-clang/CMakeLists.txt +++ b/projects/clr/hipamd/hipify-clang/CMakeLists.txt @@ -10,15 +10,6 @@ endif() option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" OFF) -# Disable the tests if `lit` or `FileCheck` is not installed. -find_program(LIT_COMMAND lit) -find_program(FILECHECK_COMMAND FileCheck) -find_program(SOCAT_COMMAND socat) -if (NOT LIT_COMMAND OR NOT FILECHECK_COMMAND OR NOT SOCAT_COMMAND) - set(HIPIFY_CLANG_TESTS OFF CACHE INTERNAL "") - message(STATUS "hipify-clang's tests are not being built because `lit`,`FileCheck` or `socat` could not be found.") -endif() - list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR}) include(AddLLVM) @@ -79,6 +70,17 @@ install(TARGETS hipify-clang DESTINATION bin) if (HIPIFY_CLANG_TESTS) find_package(PythonInterp 2.7 REQUIRED EXACT) + function (require_program PROGRAM_NAME) + find_program(FOUND_PROGRAM ${PROGRAM_NAME}) + if (NOT FOUND_PROGRAM) + message(FATAL_ERROR "Can't find ${PROGRAM_NAME}. Either set HIPIFY_CLANG_TESTS to OFF to disable hipify tests, or install the missing program.") + endif() + endfunction() + + require_program(lit) + require_program(FileCheck) + require_program(socat) + # Populates CUDA_TOOLKIT_ROOT_DIR, which is then applied to the lit config to give the # value of --cuda-path for the test runs. find_package(CUDA REQUIRED) From 84b2696f56b4c06cc6494fd0e9251d6d52b0d339 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 21:31:24 +0100 Subject: [PATCH 4/9] We no longer rely on HIPIFY_CLANG_LLVM_DIR to disable hipify-clang Since there's now an option for toggling hipify-clang, omitting the path is no longer something we need to check for. We'll still abort if LLVM isn't found, due to `REQUIRED`. [ROCm/clr commit: a13e98321528d4d1e0794abd7f0c713a11cb7baf] --- projects/clr/hipamd/hipify-clang/CMakeLists.txt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/projects/clr/hipamd/hipify-clang/CMakeLists.txt b/projects/clr/hipamd/hipify-clang/CMakeLists.txt index 13abd287c8..c8d205f0f4 100644 --- a/projects/clr/hipamd/hipify-clang/CMakeLists.txt +++ b/projects/clr/hipamd/hipify-clang/CMakeLists.txt @@ -1,12 +1,7 @@ cmake_minimum_required(VERSION 2.8.8) project(hipify-clang) -if (HIPIFY_CLANG_LLVM_DIR) - find_package(LLVM PATHS ${HIPIFY_CLANG_LLVM_DIR} REQUIRED NO_DEFAULT_PATH) -else() - message(STATUS "hipify-clang will not be built. To build it please specify absolute path to LLVM 3.8 or higher using HIPIFY_CLANG_LLVM_DIR") - return() -endif() +find_package(LLVM PATHS ${HIPIFY_CLANG_LLVM_DIR} REQUIRED NO_DEFAULT_PATH) option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" OFF) From 366c8b0dd5288abb20c9f7b36fbb6a5b31208394 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 21:32:51 +0100 Subject: [PATCH 5/9] Move the "LLVM found" print adjacent to the find_package call Very surprising that LLVM's finder module doesn't print this itself like _literally every other finder module_. Blarg. [ROCm/clr commit: 5211d48ca1dfaf9b8062076f2a66e652f3ea7e2b] --- projects/clr/hipamd/hipify-clang/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/projects/clr/hipamd/hipify-clang/CMakeLists.txt b/projects/clr/hipamd/hipify-clang/CMakeLists.txt index c8d205f0f4..e9709a6f6e 100644 --- a/projects/clr/hipamd/hipify-clang/CMakeLists.txt +++ b/projects/clr/hipamd/hipify-clang/CMakeLists.txt @@ -2,14 +2,13 @@ cmake_minimum_required(VERSION 2.8.8) project(hipify-clang) find_package(LLVM PATHS ${HIPIFY_CLANG_LLVM_DIR} REQUIRED NO_DEFAULT_PATH) +message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" OFF) list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR}) include(AddLLVM) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") - include_directories(${LLVM_INCLUDE_DIRS}) link_directories(${LLVM_LIBRARY_DIRS}) add_definitions(${LLVM_DEFINITIONS}) From 8f59de9d2b71a1e65f468b628af45be64bfb5aee Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 21:36:51 +0100 Subject: [PATCH 6/9] Use cmake's builtin mechanism for handling library locations See [the documentation](https://cmake.org/cmake/help/v3.0/command/find_package.html) for exactly how the search procedure works. If you want to use an LLVM from a specific location, use CMAKE_PREFIX_PATH as normal. No longer do we have a nonstandard HIPIFY_CLANG_LLVM_DIR variable for people to learn about. [ROCm/clr commit: de8dcf0ed234c012ac1e278683d6a8ab7371820b] --- projects/clr/hipamd/CMakeLists.txt | 7 ------- projects/clr/hipamd/hipify-clang/CMakeLists.txt | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/projects/clr/hipamd/CMakeLists.txt b/projects/clr/hipamd/CMakeLists.txt index dd8d604dd2..1c8f640afb 100644 --- a/projects/clr/hipamd/CMakeLists.txt +++ b/projects/clr/hipamd/CMakeLists.txt @@ -125,13 +125,6 @@ else() message(FATAL_ERROR "Don't know where to install HIP. Please specify absolute path using -DCMAKE_INSTALL_PREFIX") endif() -# Check if we need to build hipify-clang -if(NOT DEFINED HIPIFY_CLANG_LLVM_DIR) - if(DEFINED ENV{HIPIFY_CLANG_LLVM_DIR}) - set(HIPIFY_CLANG_LLVM_DIR $ENV{HIPIFY_CLANG_LLVM_DIR}) - endif() -endif() - # Check if we need to enable ATP marker if(NOT DEFINED COMPILE_HIP_ATP_MARKER) if(NOT DEFINED ENV{COMPILE_HIP_ATP_MARKER}) diff --git a/projects/clr/hipamd/hipify-clang/CMakeLists.txt b/projects/clr/hipamd/hipify-clang/CMakeLists.txt index e9709a6f6e..0dc1affec5 100644 --- a/projects/clr/hipamd/hipify-clang/CMakeLists.txt +++ b/projects/clr/hipamd/hipify-clang/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 2.8.8) project(hipify-clang) -find_package(LLVM PATHS ${HIPIFY_CLANG_LLVM_DIR} REQUIRED NO_DEFAULT_PATH) +find_package(LLVM REQUIRED) message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" OFF) From 4e924460b45d6b75c4aadf23df68b5c4c60e1202 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Tue, 24 Oct 2017 21:39:38 +0100 Subject: [PATCH 7/9] hipify does not add the `hipLaunchParm` option any more This was removed a while ago - seems like it uses a different variant of the launch kernel function now, so this is redundant. [ROCm/clr commit: 47c8293eaf711f2cefbefad2b0ff3e7850f1687f] --- projects/clr/hipamd/hipify-clang/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/projects/clr/hipamd/hipify-clang/README.md b/projects/clr/hipamd/hipify-clang/README.md index 20456f3bff..bb2ffe55b5 100644 --- a/projects/clr/hipamd/hipify-clang/README.md +++ b/projects/clr/hipamd/hipify-clang/README.md @@ -12,7 +12,6 @@ ## Using hipify-clang `hipify-clang` is a clang-based tool which can automate the translation of CUDA source code into portable HIP C++. -The tool can automatically add extra HIP arguments (notably the "hipLaunchParm" required at the beginning of every HIP kernel call). `hipify-clang` has some additional dependencies explained below and can be built as a separate make step. The instructions below are specifically for **Ubuntu 14.04** and **Ubuntu 16.04**. ### Build and install From ae21f6400c88c0ccade4779177304f7255f64059 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Wed, 25 Oct 2017 18:45:03 +0100 Subject: [PATCH 8/9] Update hipify-clang readme for simplified build process [ROCm/clr commit: f25afde2f577b25b0aaa1a60fe26a9744d83c99c] --- projects/clr/hipamd/hipify-clang/README.md | 88 +++++++++++----------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/projects/clr/hipamd/hipify-clang/README.md b/projects/clr/hipamd/hipify-clang/README.md index bb2ffe55b5..563440e709 100644 --- a/projects/clr/hipamd/hipify-clang/README.md +++ b/projects/clr/hipamd/hipify-clang/README.md @@ -1,3 +1,7 @@ +# hipify-clang + +`hipify-clang` is a clang-based tool to automatically translate CUDA source code into portable HIP C++. + ## Table of Contents @@ -9,67 +13,65 @@ -## Using hipify-clang +## Build and install -`hipify-clang` is a clang-based tool which can automate the translation of CUDA source code into portable HIP C++. -`hipify-clang` has some additional dependencies explained below and can be built as a separate make step. The instructions below are specifically for **Ubuntu 14.04** and **Ubuntu 16.04**. +### Dependencies -### Build and install +`hipify-clang` requires clang+llvm of at least version 3.8. -- Download and unpack clang+llvm 3.8 binary package preqrequisite. +In most cases, you can get a suitable version of clang+llvm with your package manager. + +Failing that, you can [download a release archive](http://releases.llvm.org/), extract it somewhere, and set +[CMAKE_PREFIX_PATH](https://cmake.org/cmake/help/v3.0/variable/CMAKE_PREFIX_PATH.html) so `cmake` can find it. + +### Build + +Assuming this repository is at `./HIP`: -**Ubuntu 14.04**: ```shell -wget http://llvm.org/releases/3.8.0/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz -tar xvfJ clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz -``` -**Ubuntu 16.04**: -```shell -wget http://llvm.org/releases/3.8.0/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz -tar xvfJ clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz -``` +mkdir build inst -- Enable build of hipify-clang and specify path to LLVM. - -Note HIPIFY_CLANG_LLVM_DIR must be a full absolute path to the location extracted above. Here's an example assuming we extract the clang 3.8 package into ~/HIP/clang+llvm-3.8.0/ -```shell -cd HIP -mkdir build cd build -cmake -DHIPIFY_CLANG_LLVM_DIR=~/HIP/clang+llvm-3.8.0/ -DCMAKE_BUILD_TYPE=Release .. -make -make install +cmake \ + -DCMAKE_INSTALL_PREFIX=../inst \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_HIPIFY_CLANG=ON \ + ../HIP + +make -j install ``` -### Running and using hipify-clang +The binary can then be found at `./inst/bin/hipify-clang`. -`hipify-clang` performs an initial compile of the CUDA source code into a "symbol tree", and thus needs access to the appropriate header files. +### Test -In the case when `hipify-clang` doesn't find cuda headers, it reports various errors about unknown keywords (e.g. '\__global\__'), API function names (e.g. 'cudaMalloc'), syntax (e.g. 'foo<<<1,n>>>(...)'), etc. +`hipify-clang` has unit tests using LLVM [`lit`](https://llvm.org/docs/CommandGuide/lit.html)/[`FileCheck`](https://llvm.org/docs/CommandGuide/FileCheck.html). -To install CUDA headers, download the "deb(network)" variant of the target installer. +To run it: +1. Ensure `lit` and `FileCheck` are installed - these are distributed with LLVM. +2. Ensure `socat` is installed - your distro almost certainly has a package for this. +3. Build with the `HIPIFY_CLANG_TESTS` option turned on. +4. `make test-hipify` -**Ubuntu 14.04**: -```shell -wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_7.5-18_amd64.deb -sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb -sudo apt-get update && sudo apt-get install cuda-minimal-build-7-5 cuda-curand-dev-7-5 -``` -**Ubuntu 16.04**: -```shell -wget http://archive.ubuntu.com/ubuntu/pool/multiverse/n/nvidia-cuda-toolkit/nvidia-cuda-toolkit_7.5.18-0ubuntu1_amd64.deb -sudo dpkg -i nvidia-cuda-toolkit_7.5.18-0ubuntu1_amd64.deb -sudo apt-get update && sudo apt-get install cuda-minimal-build-7-5 cuda-curand-dev-7-5 -``` -To set additional options like Language Selection (only "-x cuda" is supported), Preprocessor Definition (-D), Include Path (-I), etc., options delimiter "--" should be used before them, for instance: +## Running and using hipify-clang + +To process a file, `hipify-clang` needs access to the same headers that would be needed to compile it with clang. + +For example: ```shell -./hipify-clang -print-stats sort_kernel.cu -- -x cuda -I/srv/git/HIP/include -I/usr/local/cuda-7.5/include -DX=1 +hipify-clang square.cu -- \ + -x cuda \ + --cuda-path=/opt/cuda \ + --cuda-gpu-arch=sm_30 \ + -isystem /opt/cuda/samples/common/inc ``` -Delimiter "--" is used to separate hipify-clang options (before the delimiter) from clang options (after the delimiter). It is strongly recommended to always specify the delimiter, even if there are no clang specific options at all, in order to avoid possible errors regarding compilation database; in such case delimeter should be the last option in hipify-clang's command line. +`hipify-clang` arguments are given first, followed by a separator, and then the arguments you'd pass to `clang` if you +were compiling the input file. The [Clang manual for compiling CUDA](https://llvm.org/docs/CompileCudaWithLLVM.html#compiling-cuda-code) +may be useful. -Option "-x cuda" is also worth specifying in order to convert source CUDA files with extensions other than standard extensions (*.cu, *.cuh). +For a list of `hipify-clang` options, run `hipify-clang --help`. ## Disclaimer From 57c7a336740c1aa77b4e1171e6a7c39bd2d56b80 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Fri, 27 Oct 2017 16:17:24 +0100 Subject: [PATCH 9/9] Describe the LLVM we found [ROCm/clr commit: c909c00494dc52e6a90f0ac2878b4df1267144d3] --- projects/clr/hipamd/hipify-clang/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/projects/clr/hipamd/hipify-clang/CMakeLists.txt b/projects/clr/hipamd/hipify-clang/CMakeLists.txt index 0dc1affec5..da6eaeaa99 100644 --- a/projects/clr/hipamd/hipify-clang/CMakeLists.txt +++ b/projects/clr/hipamd/hipify-clang/CMakeLists.txt @@ -2,7 +2,10 @@ cmake_minimum_required(VERSION 2.8.8) project(hipify-clang) find_package(LLVM REQUIRED) -message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") +message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}:") +message(STATUS " - CMake module path: ${LLVM_CMAKE_DIR}") +message(STATUS " - Include path : ${LLVM_INCLUDE_DIRS}") +message(STATUS " - Binary path : ${LLVM_TOOLS_BINARY_DIR}") option(HIPIFY_CLANG_TESTS "Build the tests for hipify-clang, if lit is installed" OFF)