Skip to content

Add c_embed_data build tool - #5540

Merged
hcindyl merged 4 commits into
iree-org:mainfrom
hcindyl:c_embedding
Apr 22, 2021
Merged

hcindyl merged 4 commits into
iree-org:mainfrom
hcindyl:c_embedding

Conversation

@hcindyl

@hcindyl hcindyl commented Apr 21, 2021

Copy link
Copy Markdown
Contributor

Add the build tool to generate c embed data for testing. The code is
used to bypass fileIO and it can be used with the C API.

Change the build rule of iree_bytecode_module to include the c embed
codegen. The user needs to assign a non-empty value in the "c_output"
field.

@GMNGeoffrey GMNGeoffrey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Before I review the implementation (which I think may have to wait till tomorrow), I'd note that I really dislike that iree_bytecode_module as currently written outputs a secret extra target. It means you have to know the macro's implementation to use it (and is against the style guide https://docs.bazel.build/versions/master/skylark/bzl-style.html#macros). It also makes it a pain to implement in CMake because it behaves very poorly with generated file dependencies. I'd much prefer that the embed data didn't get bundled up with running IREE compilation, or if that was such a common pattern that we wanted a single function to do it, we could have that function create the bytecode module it wanted behind the scenes. I don't want to block you because you happen to be touching a part of our codebase I don't like though :-D

Comment thread iree/tools/compilation.bzl Outdated
flags = ["-iree-mlir-to-vm-bytecode-module"],
translate_tool = "//iree/tools:iree-translate",
cc_namespace = None,
c_output = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Default to False instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread iree/tools/compilation.bzl Outdated
# Embed the module for use in C.
if c_output:
c_embed_data(
name = "%s_cc" % (name),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should have a different name I think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I notice that when I test the bazel flow. Fixed in the new patch.

Add the build tool to generate c embed data for testing. The code is
used to bypass fileIO and it can be used with the C API.

Change the build rule of iree_bytecode_module to include the c embed
codegen. The user needs to assign a non-empty value in the "c_output"
field.
@hcindyl

hcindyl commented Apr 21, 2021

Copy link
Copy Markdown
Contributor Author

Before I review the implementation (which I think may have to wait till tomorrow), I'd note that I really dislike that iree_bytecode_module as currently written outputs a secret extra target. It means you have to know the macro's implementation to use it (and is against the style guide https://docs.bazel.build/versions/master/skylark/bzl-style.html#macros). It also makes it a pain to implement in CMake because it behaves very poorly with generated file dependencies. I'd much prefer that the embed data didn't get bundled up with running IREE compilation, or if that was such a common pattern that we wanted a single function to do it, we could have that function create the bytecode module it wanted behind the scenes. I don't want to block you because you happen to be touching a part of our codebase I don't like though :-D

Understood. The extra target behavior already exists (cc_namespace to generate the cc_embed_data). Unfortunately, I piggyback the c_embed_data to make it produce one more target optionally. I tested the cmake flow and it had the correct embedding output generated.

Set the default to "False" instead of "None"
@hcindyl
hcindyl marked this pull request as ready for review April 21, 2021 03:31
@hcindyl hcindyl linked an issue Apr 21, 2021 that may be closed by this pull request
8 tasks done
@hcindyl
hcindyl requested a review from GMNGeoffrey April 21, 2021 04:19
Comment thread build_tools/embed_data/build_defs.bzl Resolved
Comment thread build_tools/embed_data/build_defs.bzl Resolved
return f.good();
}

int main(int argc, char** argv) {

@ScottTodd ScottTodd Apr 21, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could merge generate_c_embed_data with generate_cc_embed data to avoid duplicating this generation code. I normally wouldn't be too concerned, but this PR is +500 LOC across multiple files, when it could be leaner.

We could (options 1-3, so one option isn't "C" :P)
(1) Generate two separate .h files for the same .c implementation
(2) Generate one .h file that includes both interfaces
(3) Drop the C++ part entirely and change all uses to C

Following the general trend of rewriting core code in C, I'm personally a fan of (3), but (2) looks easier to start with

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How different is the API? I'm also a fan of (3)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Chat with @ScottTodd offline. We will go with option (4), same gen tool but output different .h and .c. (2) is really tricky because .c could be used by .cc, so we can't use tricks like #if __cplusplus. (3) would require a great clean up of all the namespace usage in the existing test/sample code. We can do that in a followup patch.

_RULE
"PUBLIC;TESTONLY"
"NAME;SRC;TRANSLATE_TOOL;CC_NAMESPACE"
"NAME;SRC;TRANSLATE_TOOL;CC_NAMESPACE;C_OUTPUT"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think C_OUPUT is rather an <options> argument and not an <one_value_keywords> argument as suggested here, see https://cmake.org/cmake/help/latest/command/cmake_parse_arguments.html.

Suggested change together is the one above:

Suggested change
"NAME;SRC;TRANSLATE_TOOL;CC_NAMESPACE;C_OUTPUT"
"NAME;SRC;TRANSLATE_TOOL;CC_NAMESPACE"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. Also update the build_tools/bazel_to_cmake/bazel_to_cmake_converter.py accordingly.

@@ -40,7 +41,7 @@ function(iree_bytecode_module)
cmake_parse_arguments(
_RULE
"PUBLIC;TESTONLY"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"PUBLIC;TESTONLY"
"PUBLIC;TESTONLY;C_OUTPUT"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for the suggestion.

@GMNGeoffrey

Copy link
Copy Markdown
Member

Understood. The extra target behavior already exists (cc_namespace to generate the cc_embed_data). Unfortunately, I piggyback the c_embed_data to make it produce one more target optionally. I tested the cmake flow and it had the correct embedding output generated.

Yeah that's what I meant about not blocking you. You're not doing anything here we weren't already doing. Note that the CMake generated file stuff is quite tricky though. It has to do with correctly rerunning targets when some input changes and it can get upset if multiple targets depend on the same custom command. It's really a whole mess that I have to read about again every time I have to touch it and is really hard to ensure you got right because it's nondeterministic behavior.

@hcindyl
hcindyl requested review from ScottTodd and marbre April 21, 2021 21:46
@hcindyl
hcindyl force-pushed the c_embedding branch 2 times, most recently from 96043f5 to eba61f5 Compare April 22, 2021 02:03

@marbre marbre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just one additional NIT. Beside that, maybe one should check and disallow if both _RULE_CC_NAMESPACE and _RULE_C_OUTPUT are set iniree_bytecode_module? Or is it intended to have the option to generate both?

)
endif()

if(DEFINED _RULE_C_OUTPUT AND _RULE_C_OUTPUT)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if(DEFINED _RULE_C_OUTPUT AND _RULE_C_OUTPUT)
if(_RULE_C_OUTPUT)

should be sufficient, now that C_OUTPUT is an <options> argument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks for catching this.

build_tools/embed_data/generate_embed_data_main.cc now can produce
either c code or c++ code, controlled by --c_output flag.

Address other comments from reviewers.
@hcindyl

hcindyl commented Apr 22, 2021

Copy link
Copy Markdown
Contributor Author

Just one additional NIT. Beside that, maybe one should check and disallow if both _RULE_CC_NAMESPACE and _RULE_C_OUTPUT are set iniree_bytecode_module? Or is it intended to have the option to generate both?

I intentionally allow iree_bytecode_module to generate both, for the target may be referred in different target (one example will be #5524). Later if we have the cleanup patch to remove all the cc_embed_data (and the namespace in the test/sample code), we can change the logic.

@marbre

marbre commented Apr 22, 2021

Copy link
Copy Markdown
Member

Just one additional NIT. Beside that, maybe one should check and disallow if both _RULE_CC_NAMESPACE and _RULE_C_OUTPUT are set iniree_bytecode_module? Or is it intended to have the option to generate both?

I intentionally allow iree_bytecode_module to generate both, for the target may be referred in different target (one example will be #5524). Later if we have the cleanup patch to remove all the cc_embed_data (and the namespace in the test/sample code), we can change the logic.

Sounds good! :)

@hcindyl
hcindyl merged commit 40ed02d into iree-org:main Apr 22, 2021
@hcindyl
hcindyl deleted the c_embedding branch April 26, 2021 21:27
@GMNGeoffrey GMNGeoffrey added the infrastructure Relating to build systems, CI, or testing label May 11, 2021
@GMNGeoffrey

Copy link
Copy Markdown
Member

So I was just looking at our BUILD files and noticed that we only run the c_embed_data tests in Bazel, not C++. I'm surprised we didn't discuss that on this PR. @hcindyl do you recall the reason?

@hcindyl

hcindyl commented Nov 14, 2022

Copy link
Copy Markdown
Contributor Author

I believe at that point the cc_test was only at build_tools/embed_data/BUILD but was never at build_tools/embed_data/CMakeLists.txt. The PR was following the same binary/test target structure as its parent of https://github.com/iree-org/iree/blob/9f382ba5a08810fb12b26864bb4bb72e277852e0/build_tools/embed_data/

Looking at the code now there's no reason why the test code should not work in CMake. If you want I can add a CMake test target.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

infrastructure Relating to build systems, CI, or testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants