Skip to content

Commit

Permalink
Add a dedicated iree_c_module CMake module (iree-org#5214)
Browse files Browse the repository at this point in the history
Instead of using `iree_bytecode_module` to generate a C module, this
adds a dedicated `iree_c_module` CMake module.
  • Loading branch information
marbre authored Mar 24, 2021
1 parent 603b208 commit 46e8331
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 108 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions build_tools/cmake/iree_bytecode_module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
102 changes: 102 additions & 0 deletions build_tools/cmake/iree_c_module.cmake
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 4 additions & 9 deletions iree/samples/emitc_modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion iree/samples/emitc_modules/add_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
132 changes: 48 additions & 84 deletions iree/vm/test/emitc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading

0 comments on commit 46e8331

Please sign in to comment.