From 7947e406c7398dacfe0e76728ed43e890a202f73 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/hip commit: a4ecd4eb3124d5cc7e414178b5a56709d8cea547] --- projects/hip/CMakeLists.txt | 9 ++++++++- projects/hip/hipify-clang/CMakeLists.txt | 6 +----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/projects/hip/CMakeLists.txt b/projects/hip/CMakeLists.txt index 4c89c93668..1304e9f7d7 100644 --- a/projects/hip/CMakeLists.txt +++ b/projects/hip/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/hip/hipify-clang/CMakeLists.txt b/projects/hip/hipify-clang/CMakeLists.txt index cb8354157b..7b43f2beb7 100644 --- a/projects/hip/hipify-clang/CMakeLists.txt +++ b/projects/hip/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 b61c4a241fd860ea9da69a016dc7ca9b82261f9c 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/hip commit: 56b422204397c1858b17ca396d80be5c4ea25f93] --- projects/hip/CMakeLists.txt | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/projects/hip/CMakeLists.txt b/projects/hip/CMakeLists.txt index 1304e9f7d7..dd8d604dd2 100644 --- a/projects/hip/CMakeLists.txt +++ b/projects/hip/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 d92f3c8d764629401d361ef51724ee520a52ee41 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/hip commit: 921ff4c8a3dd8994bdb31de8af75bc90c121791c] --- projects/hip/hipify-clang/CMakeLists.txt | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/projects/hip/hipify-clang/CMakeLists.txt b/projects/hip/hipify-clang/CMakeLists.txt index 7b43f2beb7..13abd287c8 100644 --- a/projects/hip/hipify-clang/CMakeLists.txt +++ b/projects/hip/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 cc1bc495a0ef2a7372f940f5ed2a8e3b63282d93 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/hip commit: c60c8d417ee19b901df125778ca80b1950e00f28] --- projects/hip/hipify-clang/CMakeLists.txt | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/projects/hip/hipify-clang/CMakeLists.txt b/projects/hip/hipify-clang/CMakeLists.txt index 13abd287c8..c8d205f0f4 100644 --- a/projects/hip/hipify-clang/CMakeLists.txt +++ b/projects/hip/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 1461c69757dade16bcfd4d45160d1c47a8966b1c 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/hip commit: 92c90a70685f17ef21eabe528251a12c66b9272b] --- projects/hip/hipify-clang/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/projects/hip/hipify-clang/CMakeLists.txt b/projects/hip/hipify-clang/CMakeLists.txt index c8d205f0f4..e9709a6f6e 100644 --- a/projects/hip/hipify-clang/CMakeLists.txt +++ b/projects/hip/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 fd656a0fbbbe0e65a31c305dd0bc00c0cb1e8ac7 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/hip commit: 8fefc6a2b789739dfad064cd32d1e57bf9ef03ff] --- projects/hip/CMakeLists.txt | 7 ------- projects/hip/hipify-clang/CMakeLists.txt | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/projects/hip/CMakeLists.txt b/projects/hip/CMakeLists.txt index dd8d604dd2..1c8f640afb 100644 --- a/projects/hip/CMakeLists.txt +++ b/projects/hip/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/hip/hipify-clang/CMakeLists.txt b/projects/hip/hipify-clang/CMakeLists.txt index e9709a6f6e..0dc1affec5 100644 --- a/projects/hip/hipify-clang/CMakeLists.txt +++ b/projects/hip/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 e3338fe5a4646f93184d740b656fa5eeca6c8f7d 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/hip commit: b412802c66b25e6dbc83cfc222c7f23f6f6c1861] --- projects/hip/hipify-clang/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/projects/hip/hipify-clang/README.md b/projects/hip/hipify-clang/README.md index 20456f3bff..bb2ffe55b5 100644 --- a/projects/hip/hipify-clang/README.md +++ b/projects/hip/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 7a0eedabbebdba5d33fb409518050a4526e75625 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/hip commit: 82d05ee6f4236dea2c1bc1ac889f8b1f5b1f5fd4] --- projects/hip/hipify-clang/README.md | 88 +++++++++++++++-------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/projects/hip/hipify-clang/README.md b/projects/hip/hipify-clang/README.md index bb2ffe55b5..563440e709 100644 --- a/projects/hip/hipify-clang/README.md +++ b/projects/hip/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 22854820754f6678e966261b22e6e3d55ccbd61e 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/hip commit: 69e67fe25a8475ebc9dbc0fe30d23769352135f8] --- projects/hip/hipify-clang/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/projects/hip/hipify-clang/CMakeLists.txt b/projects/hip/hipify-clang/CMakeLists.txt index 0dc1affec5..da6eaeaa99 100644 --- a/projects/hip/hipify-clang/CMakeLists.txt +++ b/projects/hip/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)