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

Add a dedicated iree_c_module CMake module #5214

Merged
merged 2 commits into from
Mar 24, 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
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}
)
Comment on lines +69 to +80
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like it would be just as clear to include the command args in the call to add_custom_command

Suggested change
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}
)
add_custom_command(
OUTPUT "${_RULE_H_FILE_OUTPUT}"
COMMAND
${_TRANSLATE_TOOL_EXECUTABLE}
${_FLAGS}
"${CMAKE_CURRENT_SOURCE_DIR}/${_RULE_SRC}"
"-o" "${_RULE_H_FILE_OUTPUT}"
# Changes to either the translation tool or the input source should
# trigger rebuilding.
DEPENDS ${_TRANSLATE_TOOL_EXECUTABLE} ${_RULE_SRC}
)

Copy link
Member Author

Choose a reason for hiding this comment

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

This is nearly a takeover from https://github.com/google/iree/blob/881de81238971869d6d62cabf8dc0b54a17be704/build_tools/cmake/iree_bytecode_module.cmake#L64%C3%9FL77.
I have no preference, but I think it might be nice to keep it consistent?

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair, though I don't think consistency in this case really buys us much. I don't care too much either way.


set(_GEN_TARGET "${_NAME}_gen")
add_custom_target(
${_GEN_TARGET}
DEPENDS
${_RULE_H_FILE_OUTPUT}
)
Comment on lines +83 to +87
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that if the only thing that depends on a generated file from add_custom_command is a library defined in the same CMakeLists.txt, you can just directly depend on it from that library.

This blog post is from 2015, but I think may still be relevant https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/

I think that interface libraries do support sources (although the documentation is somewhat confused). https://cmake.org/cmake/help/v3.13/command/add_library.html#interface-libraries. We don't appear to use that functionality in iree_cc_library, but I think we maybe should.

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed in discord. What we have here works and other things appear not to. The extra wrinkle of this being a generated header file (and therefore not compiled), means that my suggestion here doesn't work.


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