Skip to content

Commit

Permalink
[test] [ir] Add a simple unit test for IRBuilder (#2214)
Browse files Browse the repository at this point in the history
* [test] [ir] Add basic IR unit test

* comments
  • Loading branch information
k-ye authored Mar 16, 2021
1 parent 569623b commit 588fc8f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
41 changes: 33 additions & 8 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,45 @@ if (TI_WITH_CC)
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_CC_SOURCE})
endif()

# We don't mean to create a dedicated library just for common. Instead, we are
# trying to identify, refactor and group the core files into a smaller lib. This
# lib should be mostly self-contained and completely Taichi focused, free from
# the "application" layer such as pybind11 or GUI.
file(GLOB TAICHI_TESTABLE_SRC "taichi/common/*.cpp" "taichi/common/*.h")
# This compiles all the libraries with -fPIC, which is critical to link a static
# library into a shared lib.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# The short-term goal is to have a sub-library that is mostly Taichi-focused,
# free from the "application" layer such as pybind11 or GUI. At a minimum, we
# must decouple from pybind11/python-environment. This sub-lib will then be
# unit testtable.
# TODO(#2198): Long-term speaking, we should create a separate library for each
# sub-module. This way we can guarantee that the lib dependencies form a DAG.
file(GLOB TAICHI_TESTABLE_SRC
"taichi/common/*.cpp"
"taichi/common/*.h"
"taichi/ir/ir_builder.*"
"taichi/ir/ir.*"
"taichi/ir/offloaded_task_type.*"
"taichi/ir/snode_types.*"
"taichi/ir/snode.*"
"taichi/ir/statements.*"
"taichi/ir/type_factory.*"
"taichi/ir/type_utils.*"
"taichi/ir/type.*"
"taichi/transforms/statement_usage_replace.cpp"
"taichi/program/arch.*"
"taichi/program/compile_config.*"
"taichi/system/timer.*"
"taichi/system/profiler.*"
)

# TODO(#2196): Maybe we can do the following renaming in the end?
# taichi_core --> taichi_pylib (this requires python-side refactoring...)
# taichi_testable_lib --> taichi_core
set(TAICHI_TESTABLE_LIB taichi_testable_lib)
add_library(${TAICHI_TESTABLE_LIB} OBJECT ${TAICHI_TESTABLE_SRC})
set_property(TARGET ${TAICHI_TESTABLE_LIB} PROPERTY POSITION_INDEPENDENT_CODE ON)
add_library(${TAICHI_TESTABLE_LIB} STATIC ${TAICHI_TESTABLE_SRC})

list(REMOVE_ITEM TAICHI_CORE_SOURCE ${TAICHI_TESTABLE_SRC})
add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_CORE_SOURCE} ${PROJECT_SOURCES} $<TARGET_OBJECTS:${TAICHI_TESTABLE_LIB}>)

add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_CORE_SOURCE})
target_link_libraries(${CORE_LIBRARY_NAME} ${TAICHI_TESTABLE_LIB})

if (APPLE)
# Ask OS X to minic Linux dynamic linking behavior
Expand Down
3 changes: 2 additions & 1 deletion cmake/TaichiTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ include_directories(
${PROJECT_SOURCE_DIR},
)

add_executable(${TESTS_NAME} ${TAICHI_TESTS_SOURCE} $<TARGET_OBJECTS:taichi_testable_lib>)
add_executable(${TESTS_NAME} ${TAICHI_TESTS_SOURCE})
target_link_libraries(${TESTS_NAME} taichi_testable_lib)
target_link_libraries(${TESTS_NAME} gtest_main)

add_test(NAME ${TESTS_NAME} COMMAND ${TESTS_NAME})
22 changes: 22 additions & 0 deletions tests/cpp_new/ir/ir_builder_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "gtest/gtest.h"

#include "taichi/ir/ir_builder.h"
#include "taichi/ir/statements.h"

namespace taichi {
namespace lang {

TEST(IRBuilder, Basic) {
IRBuilder builder;
auto *lhs = builder.get_int32(40);
auto *rhs = builder.get_int32(2);
auto *add = builder.create_add(lhs, rhs);
ASSERT_TRUE(add->is<BinaryOpStmt>());
auto *addc = add->cast<BinaryOpStmt>();
EXPECT_EQ(addc->lhs, lhs);
EXPECT_EQ(addc->rhs, rhs);
EXPECT_EQ(addc->op_type, BinaryOpType::add);
}

} // namespace lang
} // namespace taichi

0 comments on commit 588fc8f

Please sign in to comment.