[CI]Added initial framework of CXX unittest (#98)

Based on the discussion in #53 , I just added the initial flow of CXX unittests for this repo, with providing two dummy UTs as placeholder to show the usage, feel free to add your own CXX unittests. 
@Superjomn  @ptillet 

@ptillet , in this PR, I also configure the integration-tests.yml to add the unittest into github CI check. 

Thanks
This commit is contained in:
Jun Yang
2022-09-04 12:50:27 +08:00
committed by GitHub
parent d0b4c67b05
commit ea175f689e
10 changed files with 112 additions and 7 deletions

View File

@@ -62,3 +62,9 @@ jobs:
run: | run: |
cd python/tests cd python/tests
pytest pytest
- name: Run CXX unittests
run: |
cd python/
cd "build/$(ls build)"
ctest

View File

@@ -220,3 +220,5 @@ if(BUILD_PYTHON_MODULE AND NOT WIN32)
endif() endif()
add_subdirectory(test) add_subdirectory(test)
add_subdirectory(unittest)

View File

@@ -20,9 +20,13 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <fstream> #include <fstream>
#if defined __has_include
#if __has_include(<unistd.h>) #if __has_include(<unistd.h>)
#include <unistd.h> #include <unistd.h>
#endif #endif
#endif
#include "triton/driver/dispatch.h" #include "triton/driver/dispatch.h"
#include "triton/driver/error.h" #include "triton/driver/error.h"
#include "triton/driver/llvm.h" #include "triton/driver/llvm.h"
@@ -87,9 +91,11 @@ static bool find_and_replace(std::string &str, const std::string &begin,
const std::string &end, const std::string &end,
const std::string &target) { const std::string &target) {
size_t start_replace = str.find(begin); size_t start_replace = str.find(begin);
size_t end_replace = str.find(end, start_replace);
if (start_replace == std::string::npos) if (start_replace == std::string::npos)
return false; return false;
size_t end_replace = str.find(end, start_replace);
if (end_replace == std::string::npos)
return false;
str.replace(start_replace, end_replace + 1 - start_replace, target); str.replace(start_replace, end_replace + 1 - start_replace, target);
return true; return true;
} }
@@ -104,7 +110,7 @@ std::string path_to_ptxas(int &version) {
ptxas_prefixes.insert(ptxas_prefixes.begin(), triton_ptxas); ptxas_prefixes.insert(ptxas_prefixes.begin(), triton_ptxas);
// see what path for ptxas are valid // see what path for ptxas are valid
std::vector<std::string> working_ptxas; std::vector<std::string> working_ptxas;
for (std::string prefix : ptxas_prefixes) { for (const std::string &prefix : ptxas_prefixes) {
std::string ptxas = prefix + "ptxas"; std::string ptxas = prefix + "ptxas";
bool works = tools::exec(ptxas + " --version 2>&1", ret) == 0; bool works = tools::exec(ptxas + " --version 2>&1", ret) == 0;
if (works) { if (works) {
@@ -124,19 +130,21 @@ std::string path_to_ptxas(int &version) {
bool found = false; bool found = false;
// currently choosing the first ptxas. Other logics can be implemented in // currently choosing the first ptxas. Other logics can be implemented in
// future // future
for (std::string ret : rets) { size_t i = 0;
if (std::regex_search(ret, match, version_regex)) { while (i < rets.size()) {
if (std::regex_search(rets[i], match, version_regex)) {
int major = std::stoi(match[1]); int major = std::stoi(match[1]);
int minor = std::stoi(match[2]); int minor = std::stoi(match[2]);
version = major * 1000 + minor * 10; version = major * 1000 + minor * 10;
found = true; found = true;
break; break;
} }
++i;
} }
if (not found) { if (not found) {
throw std::runtime_error("Error in parsing version"); throw std::runtime_error("Error in parsing version");
} }
return ptxas; return working_ptxas[i];
} }
int vptx(int version) { int vptx(int version) {

View File

@@ -0,0 +1,5 @@
add_triton_ut(
NAME TritonAnalysisTests
SRCS UtilityTest.cpp
LIBS TritonAnalysis
)

View File

@@ -0,0 +1,14 @@
//===- UtilityTest.cpp - Tests for
// Utility----------------------------------===//
//
//===----------------------------------------------------------------------===//
#include "triton/Analysis/Utility.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace mlir {
TEST(UtilityTest, DummyTest) { EXPECT_EQ(true, true); }
} // namespace mlir

27
unittest/CMakeLists.txt Normal file
View File

@@ -0,0 +1,27 @@
include (${CMAKE_CURRENT_SOURCE_DIR}/googletest.cmake)
include(GoogleTest)
enable_testing()
function(add_triton_ut)
set(options)
set(oneValueArgs NAME)
set(multiValueArgs SRCS LIBS)
cmake_parse_arguments(_ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
add_test(NAME ${__NAME}
COMMAND ${__NAME})
add_executable(
${__NAME}
${__SRCS})
target_link_libraries(
${__NAME}
GTest::gtest_main
gmock
${__LIBS})
gtest_discover_tests(${__NAME})
endfunction()
add_subdirectory(Analysis)
add_subdirectory(Conversion)

View File

@@ -0,0 +1 @@
add_subdirectory(TritonGPUToLLVM)

View File

@@ -0,0 +1,5 @@
add_triton_ut(
NAME TritonGPUToLLVMTests
SRCS TritonGPUToLLVMTests.cpp
LIBS TritonGPUToLLVM
)

View File

@@ -0,0 +1,14 @@
//===- TritonGPUToLLVMTests.cpp - Tests for
// TritonGPUToLLVM----------------------------------===//
//
//===----------------------------------------------------------------------===//
#include "triton/Conversion/TritonGPUToLLVM/PtxAsmFormat.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace mlir {
TEST(PtxAsmFormatTest, BasicTest) { EXPECT_EQ(true, true); }
} // namespace mlir

23
unittest/googletest.cmake Normal file
View File

@@ -0,0 +1,23 @@
include(FetchContent)
set(GOOGLETEST_DIR "" CACHE STRING "Location of local GoogleTest repo to build against")
if(GOOGLETEST_DIR)
set(FETCHCONTENT_SOURCE_DIR_GOOGLETEST ${GOOGLETEST_DIR} CACHE STRING "GoogleTest source directory override")
endif()
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)
FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
FetchContent_Populate(googletest)
if (MSVC)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()