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

[test] [ir] Add a simple unit test for IRBuilder #2214

Merged
merged 2 commits into from
Mar 16, 2021
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
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) {
Copy link
Member Author

Choose a reason for hiding this comment

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

googletest is pretty easy to learn: https://google.github.io/googletest/primer.html. By having these files unit tested, we should be able to finalize our IR/passes API, while still iterating and improving the implementations.

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