diff --git a/CMakeLists.txt b/CMakeLists.txt index 1938a50b70f8..752b05f4004f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -213,6 +213,7 @@ include(iree_tablegen_library) include(iree_tablegen_doc) include(iree_cc_embed_data) include(iree_bytecode_module) +include(iree_c_module) include(iree_python) include(iree_lit_test) include(iree_add_all_subdirs) diff --git a/build_tools/cmake/iree_bytecode_module.cmake b/build_tools/cmake/iree_bytecode_module.cmake index 54419534d75f..0af567af62f2 100644 --- a/build_tools/cmake/iree_bytecode_module.cmake +++ b/build_tools/cmake/iree_bytecode_module.cmake @@ -21,8 +21,10 @@ include(CMakeParseArguments) # Parameters: # NAME: Name of target (see Note). # SRC: Source file to compile into a bytecode module. -# FLAGS: Flags to pass to the translation tool (list of strings). -# TRANSLATE_TOOL: Translation tool to invoke (CMake target). +# FLAGS: Flags to pass to the translation tool (list of strings). The +# default flag set is "-iree-mlir-to-vm-bytecode-module". +# TRANSLATE_TOOL: Translation tool to invoke (CMake target). The default +# tool is "iree-translate". # CC_NAMESPACE: Wraps everything in a C++ namespace. # PUBLIC: Add this so that this library will be exported under ${PACKAGE}:: # Also in IDE, target will appear in ${PACKAGE} folder while non PUBLIC diff --git a/build_tools/cmake/iree_c_module.cmake b/build_tools/cmake/iree_c_module.cmake new file mode 100644 index 000000000000..3cf7a61eacf6 --- /dev/null +++ b/build_tools/cmake/iree_c_module.cmake @@ -0,0 +1,102 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include(CMakeParseArguments) + +# iree_c_module() +# +# Parameters: +# NAME: Name of target (see Note). +# SRC: MLIR source file to compile into a c module. +# H_FILE_OUTPUT: The H header file to output. +# TRANSLATE_TOOL: Translation tool to invoke (CMake target). The default +# tool is "iree-translate". +# FLAGS: Flags to pass to the translation tool (list of strings). The +# default flag set is "-iree-vm-ir-to-c-module". +# TESTONLY: When added, this target will only be built if user passes +# -DIREE_BUILD_TESTS=ON to CMake. +# +# Note: +# By default, iree_c_module will create a library named ${NAME}, +# and alias target iree::${NAME}. The iree:: form should always be used. +# This is to reduce namespace pollution. +function(iree_c_module) + cmake_parse_arguments( + _RULE + "TESTONLY" + "NAME;SRC;H_FILE_OUTPUT;TRANSLATE_TOOL" + "FLAGS" + ${ARGN} + ) + + if(_RULE_TESTONLY AND NOT IREE_BUILD_TESTS) + return() + endif() + + # Replace dependencies passed by ::name with iree::package::name + iree_package_ns(_PACKAGE_NS) + list(TRANSFORM _RULE_DEPS REPLACE "^::" "${_PACKAGE_NS}::") + + # Prefix the library with the package name, so we get: iree_package_name. + iree_package_name(_PACKAGE_NAME) + set(_NAME "${_PACKAGE_NAME}_${_RULE_NAME}") + + # Set defaults for FLAGS and TRANSLATE_TOOL + if(DEFINED _RULE_FLAGS) + set(_FLAGS ${_RULE_FLAGS}) + else() + set(_FLAGS "-iree-vm-ir-to-c-module") + endif() + if(DEFINED _RULE_TRANSLATE_TOOL) + set(_TRANSLATE_TOOL ${_RULE_TRANSLATE_TOOL}) + else() + set(_TRANSLATE_TOOL "iree-translate") + endif() + + iree_get_executable_path(_TRANSLATE_TOOL_EXECUTABLE ${_TRANSLATE_TOOL}) + + set(_ARGS "${_FLAGS}") + list(APPEND _ARGS "${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_SRC}") + list(APPEND _ARGS "-o") + list(APPEND _ARGS "${_RULE_H_FILE_OUTPUT}") + + add_custom_command( + OUTPUT "${_RULE_H_FILE_OUTPUT}" + COMMAND ${_TRANSLATE_TOOL_EXECUTABLE} ${_ARGS} + # Changes to either the translation tool or the input source should + # trigger rebuilding. + DEPENDS ${_TRANSLATE_TOOL_EXECUTABLE} ${_RULE_SRC} + ) + + set(_GEN_TARGET "${_NAME}_gen") + add_custom_target( + ${_GEN_TARGET} + DEPENDS + ${_RULE_H_FILE_OUTPUT} + ) + + add_library(${_NAME} INTERFACE) + add_dependencies(${_NAME} ${_GEN_TARGET}) + + # Alias the iree_package_name library to iree::package::name. + # This lets us more clearly map to Bazel and makes it possible to + # disambiguate the underscores in paths vs. the separators. + add_library(${_PACKAGE_NS}::${_RULE_NAME} ALIAS ${_NAME}) + iree_package_dir(_PACKAGE_DIR) + if(${_RULE_NAME} STREQUAL ${_PACKAGE_DIR}) + # If the library name matches the package then treat it as a default. + # For example, foo/bar/ library 'bar' would end up as 'foo::bar'. + add_library(${_PACKAGE_NS} ALIAS ${_NAME}) + endif() +endfunction() diff --git a/iree/samples/emitc_modules/CMakeLists.txt b/iree/samples/emitc_modules/CMakeLists.txt index 772fcdee4d75..38c20f67d511 100644 --- a/iree/samples/emitc_modules/CMakeLists.txt +++ b/iree/samples/emitc_modules/CMakeLists.txt @@ -13,18 +13,13 @@ # limitations under the License. if(${IREE_ENABLE_EMITC}) - # TODO(simon-camp): We are only interested in the output of iree-translate, i.e. add_module.vmfb - # TODO(marbre): Use a custom command or rather implement a new CMake function. - iree_bytecode_module( + iree_c_module( NAME add_module SRC "add.mlir" - CC_NAMESPACE - "iree::samples::custom_modules" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "add_module.h" ) iree_cc_test( @@ -33,7 +28,7 @@ if(${IREE_ENABLE_EMITC}) SRCS "add_module_test.cc" DEPS - ::add_module_cc + ::add_module iree::base::api iree::base::status iree::testing::gtest diff --git a/iree/samples/emitc_modules/add_module_test.cc b/iree/samples/emitc_modules/add_module_test.cc index 5159f82aa045..d70be249a173 100644 --- a/iree/samples/emitc_modules/add_module_test.cc +++ b/iree/samples/emitc_modules/add_module_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "iree/samples/emitc_modules/add_module.vmfb" +#include "iree/samples/emitc_modules/add_module.h" #include "iree/base/status.h" #include "iree/testing/gtest.h" diff --git a/iree/vm/test/emitc/CMakeLists.txt b/iree/vm/test/emitc/CMakeLists.txt index c89fc4a6d013..5cd9b8c90b91 100644 --- a/iree/vm/test/emitc/CMakeLists.txt +++ b/iree/vm/test/emitc/CMakeLists.txt @@ -30,162 +30,126 @@ iree_cc_test( iree::vm::cc iree::vm::ops iree::vm::shims - ::arithmetic_ops_cc - ::arithmetic_ops_i64_cc - ::assignment_ops_cc - ::assignment_ops_i64_cc - ::comparison_ops_cc - ::comparison_ops_i64_cc - ::control_flow_ops_cc - ::conversion_ops_cc - ::conversion_ops_i64_cc - ::global_ops_cc - ::shift_ops_cc - ::shift_ops_i64_cc + ::arithmetic_ops + ::arithmetic_ops_i64 + ::assignment_ops + ::assignment_ops_i64 + ::comparison_ops + ::comparison_ops_i64 + ::control_flow_ops + ::conversion_ops + ::conversion_ops_i64 + ::global_ops + ::shift_ops + ::shift_ops_i64 ) -iree_bytecode_module( +iree_c_module( NAME arithmetic_ops SRC "../arithmetic_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "arithmetic_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME arithmetic_ops_i64 SRC "../arithmetic_ops_i64.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "arithmetic_ops_i64.h" ) -iree_bytecode_module( +iree_c_module( NAME assignment_ops SRC "../assignment_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "assignment_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME assignment_ops_i64 SRC "../assignment_ops_i64.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "assignment_ops_i64.h" ) -iree_bytecode_module( +iree_c_module( NAME comparison_ops SRC "../comparison_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "comparison_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME comparison_ops_i64 SRC "../comparison_ops_i64.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "comparison_ops_i64.h" ) -iree_bytecode_module( +iree_c_module( NAME control_flow_ops SRC "../control_flow_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "control_flow_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME conversion_ops SRC "../conversion_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "conversion_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME conversion_ops_i64 SRC "../conversion_ops_i64.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "conversion_ops_i64.h" ) -iree_bytecode_module( +iree_c_module( NAME global_ops SRC "../global_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "global_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME shift_ops SRC "../shift_ops.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "shift_ops.h" ) -iree_bytecode_module( +iree_c_module( NAME shift_ops_i64 SRC "../shift_ops_i64.mlir" - CC_NAMESPACE - "iree::vm::test::emitc" - FLAGS - "-iree-vm-ir-to-c-module" - PUBLIC + H_FILE_OUTPUT + "shift_ops_i64.h" ) endif() diff --git a/iree/vm/test/emitc/module_test.cc b/iree/vm/test/emitc/module_test.cc index bf0c686ebfc2..a487250be362 100644 --- a/iree/vm/test/emitc/module_test.cc +++ b/iree/vm/test/emitc/module_test.cc @@ -18,18 +18,18 @@ #include "iree/base/status.h" #include "iree/testing/gtest.h" #include "iree/vm/api.h" -#include "iree/vm/test/emitc/arithmetic_ops.vmfb" -#include "iree/vm/test/emitc/arithmetic_ops_i64.vmfb" -#include "iree/vm/test/emitc/assignment_ops.vmfb" -#include "iree/vm/test/emitc/assignment_ops_i64.vmfb" -#include "iree/vm/test/emitc/comparison_ops.vmfb" -#include "iree/vm/test/emitc/comparison_ops_i64.vmfb" -#include "iree/vm/test/emitc/control_flow_ops.vmfb" -#include "iree/vm/test/emitc/conversion_ops.vmfb" -#include "iree/vm/test/emitc/conversion_ops_i64.vmfb" -#include "iree/vm/test/emitc/global_ops.vmfb" -#include "iree/vm/test/emitc/shift_ops.vmfb" -#include "iree/vm/test/emitc/shift_ops_i64.vmfb" +#include "iree/vm/test/emitc/arithmetic_ops.h" +#include "iree/vm/test/emitc/arithmetic_ops_i64.h" +#include "iree/vm/test/emitc/assignment_ops.h" +#include "iree/vm/test/emitc/assignment_ops_i64.h" +#include "iree/vm/test/emitc/comparison_ops.h" +#include "iree/vm/test/emitc/comparison_ops_i64.h" +#include "iree/vm/test/emitc/control_flow_ops.h" +#include "iree/vm/test/emitc/conversion_ops.h" +#include "iree/vm/test/emitc/conversion_ops_i64.h" +#include "iree/vm/test/emitc/global_ops.h" +#include "iree/vm/test/emitc/shift_ops.h" +#include "iree/vm/test/emitc/shift_ops_i64.h" namespace {