Index: flang/CMakeLists.txt =================================================================== --- flang/CMakeLists.txt +++ flang/CMakeLists.txt @@ -120,8 +120,40 @@ option(FLANG_INCLUDE_TESTS "Generate build targets for the Flang unit tests." - ON) - add_custom_target(check-all DEPENDS check-flang) + ${LLVM_INCLUDE_TESTS}) + +#Handle unittests when out-of-tree +#LLVM_BUILD_MAIN_SRC_DIR - Path to llvm source when out-of-tree. + set(UNITTEST_DIR ${LLVM_BUILD_MAIN_SRC_DIR}/utils/unittest) + if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h) + if (TARGET gtest) + # LLVM Doesn't export gtest's include directorys, so do that here + set_target_properties(gtest + PROPERTIES INTERFACE_INCLUDE_DIRECTORIES + "${UNITTEST_DIR}/googletest/include;${UNITTEST_DIR}/googlemock/include" + ) + else() + add_library(gtest + ${UNITTEST_DIR}/googletest/src/gtest-all.cc + ${UNITTEST_DIR}/googlemock/src/gmock-all.cc + ) + target_include_directories(gtest + PUBLIC + "${UNITTEST_DIR}/googletest/include" + "${UNITTEST_DIR}/googlemock/include" + + PRIVATE + "${UNITTEST_DIR}/googletest" + "${UNITTEST_DIR}/googlemock" + ) + target_link_libraries(gtest PUBLIC -lpthread) + + add_library(gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp) + target_link_libraries(gtest_main PUBLIC gtest) + endif() + endif() + + add_custom_target(check-all DEPENDS check-flang FlangUnitTests) if (LLVM_BUILD_DOCS) add_custom_target(doxygen ALL) endif() Index: flang/README.md =================================================================== --- flang/README.md +++ flang/README.md @@ -140,8 +140,13 @@ cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src make ``` -### How to Run the Regression Tests +# How to Run Tests +Flang supports 2 different categories of tests +1. Regression tests (https://www.llvm.org/docs/TestingGuide.html#regression-tests) +2. Unit tests (https://www.llvm.org/docs/TestingGuide.html#unit-tests) + +## For out of tree builds To run all tests: ``` cd ~/flang/build @@ -157,6 +162,37 @@ --param flang_site_config=/test-lit/lit.site.cfg.py \ --param flang_config=/test-lit/lit.cfg.py \ + +``` + +Unit tests: + +If flang was built with `-DFLANG_INCLUDE_TESTS=On` (By default depends on `-DLLVM_INCLUDE_TESTS`), it is possible to +generate unittests. +There are various ways to run unit-tests. + +``` + +1. make check-flang-unit +2. make check-all or make check-flang +3. /llvm-lit \ + test/Unit +4. Invoking tests from /unittests/ + +``` + + +## For in tree builds +If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`On` by default), it is possible to +generate unittests. + +To run all of the flang unit tests use the `check-flang-unit` target: +``` +make check-flang-unit +``` +To run all of the flang regression tests use the `check-flang` target: +``` +make check-flang ``` # How to Generate Documentation @@ -180,3 +216,4 @@ /tools/flang/docs/doxygen/html # for flang docs ``` + Index: flang/include/flang/Optimizer/Support/InternalNames.h =================================================================== --- flang/include/flang/Optimizer/Support/InternalNames.h +++ flang/include/flang/Optimizer/Support/InternalNames.h @@ -65,6 +65,7 @@ /// Unique a (global) constant name std::string doConstant(llvm::ArrayRef modules, + llvm::Optional host, llvm::StringRef name); /// Unique a dispatch table name Index: flang/lib/Optimizer/Support/InternalNames.cpp =================================================================== --- flang/lib/Optimizer/Support/InternalNames.cpp +++ flang/lib/Optimizer/Support/InternalNames.cpp @@ -99,9 +99,11 @@ std::string fir::NameUniquer::doConstant(llvm::ArrayRef modules, + llvm::Optional host, llvm::StringRef name) { std::string result = prefix(); - return result.append(doModules(modules)).append("EC").append(toLower(name)); + result.append(doModulesHost(modules, host)).append("EC"); + return result.append(toLower(name)); } std::string Index: flang/runtime/io-error.cpp =================================================================== --- flang/runtime/io-error.cpp +++ flang/runtime/io-error.cpp @@ -7,7 +7,6 @@ //===----------------------------------------------------------------------===// #include "io-error.h" -#include "config.h" #include "magic-numbers.h" #include "tools.h" #include Index: flang/test/CMakeLists.txt =================================================================== --- flang/test/CMakeLists.txt +++ flang/test/CMakeLists.txt @@ -12,6 +12,13 @@ ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py ) +configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in + ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py + MAIN_CONFIG + ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py +) + set(FLANG_TEST_PARAMS flang_site_config=${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py) @@ -23,6 +30,10 @@ list(APPEND FLANG_TEST_DEPENDS tco) endif() +if (FLANG_INCLUDE_TESTS) + list(APPEND FLANG_TEST_DEPENDS FlangUnitTests) +endif() + add_custom_target(flang-test-depends DEPENDS ${FLANG_TEST_DEPENDS}) add_lit_testsuite(check-flang "Running the Flang regression tests" Index: flang/test/Unit/lit.cfg.py =================================================================== --- /dev/null +++ flang/test/Unit/lit.cfg.py @@ -0,0 +1,33 @@ +# -*- Python -*- + +# Configuration file for the 'lit' test runner. + +import os + +import lit.formats + +# name: The name of this test suite. +config.name = 'flang-Unit' + +# suffixes: A list of file extensions to treat as test files. +config.suffixes = [] + +# test_source_root: The root path where unit test binaries are located. +# test_exec_root: The root path where tests should be run. +config.test_source_root = os.path.join(config.flang_obj_root, 'unittests') +config.test_exec_root = config.test_source_root + +# testFormat: The test format to use to interpret tests. +config.test_format = lit.formats.GoogleTest(config.llvm_build_mode, 'Tests') + +# Tweak the PATH to include the tools dir. +path = os.path.pathsep.join((config.flang_tools_dir, config.llvm_tools_dir, config.environment['PATH'])) +config.environment['PATH'] = path + +path = os.path.pathsep.join((config.flang_libs_dir, config.llvm_libs_dir, + config.environment.get('LD_LIBRARY_PATH',''))) +config.environment['LD_LIBRARY_PATH'] = path + +# Propagate PYTHON_EXECUTABLE into the environment +#config.environment['PYTHON_EXECUTABLE'] = sys.executable + Index: flang/test/Unit/lit.site.cfg.py.in =================================================================== --- /dev/null +++ flang/test/Unit/lit.site.cfg.py.in @@ -0,0 +1,27 @@ +@LIT_SITE_CFG_IN_HEADER@ + +config.llvm_src_root = "@LLVM_SOURCE_DIR@" +config.llvm_obj_root = "@LLVM_BINARY_DIR@" +config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" +config.llvm_libs_dir = "@LLVM_LIBS_DIR@" +config.llvm_build_mode = "@LLVM_BUILD_MODE@" +config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" +config.flang_obj_root = "@FLANG_BINARY_DIR@" +config.flang_src_root = "@FLANG_SOURCE_DIR@" +config.flang_libs_dir = "@LLVM_LIBRARY_OUTPUT_INTDIR@" +config.flang_tools_dir = "@LLVM_RUNTIME_OUTPUT_INTDIR@" +config.target_triple = "@TARGET_TRIPLE@" +config.python_executable = "@Python3_EXECUTABLE@" + +# Support substitution of the tools and libs dirs with user parameters. This is +# used when we can't determine the tool dir at configuration time. +try: + config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params + config.llvm_libs_dir = config.llvm_libs_dir % lit_config.params + config.llvm_build_mode = config.llvm_build_mode % lit_config.params +except KeyError as e: + key, = e.args + lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) + +# Let the main config do the real work. +lit_config.load_config(config, "@FLANG_SOURCE_DIR@/test/Unit/lit.cfg.py") Index: flang/unittests/CMakeLists.txt =================================================================== --- flang/unittests/CMakeLists.txt +++ flang/unittests/CMakeLists.txt @@ -1,3 +1,11 @@ +add_custom_target(FlangUnitTests) +set_target_properties(FlangUnitTests PROPERTIES FOLDER "Flang Unit Tests") + +function(add_flang_unittest test_dirname) + add_unittest(FlangUnitTests ${test_dirname} ${ARGN}) +endfunction() + +add_subdirectory(Optimizer) add_subdirectory(Decimal) add_subdirectory(Evaluate) add_subdirectory(Runtime) Index: flang/unittests/Optimizer/CMakeLists.txt =================================================================== --- /dev/null +++ flang/unittests/Optimizer/CMakeLists.txt @@ -0,0 +1,13 @@ +get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) + +set(LIBS + FIRDialect + ${dialect_libs} +) + +add_flang_unittest(FlangOptimizerTests + InternalNamesTest.cpp +) +target_link_libraries(FlangOptimizerTests + PRIVATE + ${LIBS}) Index: flang/unittests/Optimizer/InternalNamesTest.cpp =================================================================== --- /dev/null +++ flang/unittests/Optimizer/InternalNamesTest.cpp @@ -0,0 +1,20 @@ +//===- Basic.cpp - InternalNames unit tests ---------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "flang/Optimizer/Support/InternalNames.h" +#include "gtest/gtest.h" + +using namespace fir; +using namespace llvm; + +TEST(genericName, MyTest) { + NameUniquer obj; + std::string val = obj.doCommonBlock("hello"); + std::string val2 = "_QBhello"; + EXPECT_EQ(val, val2); +}