Repository navigation
Add c_embed_data build tool - #5540
Conversation
GMNGeoffrey
left a comment
There was a problem hiding this comment.
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
| flags = ["-iree-mlir-to-vm-bytecode-module"], | ||
| translate_tool = "//iree/tools:iree-translate", | ||
| cc_namespace = None, | ||
| c_output = None, |
| # Embed the module for use in C. | ||
| if c_output: | ||
| c_embed_data( | ||
| name = "%s_cc" % (name), |
There was a problem hiding this comment.
Should have a different name I think?
There was a problem hiding this comment.
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.
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"
| return f.good(); | ||
| } | ||
|
|
||
| int main(int argc, char** argv) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
How different is the API? I'm also a fan of (3)
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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:
| "NAME;SRC;TRANSLATE_TOOL;CC_NAMESPACE;C_OUTPUT" | |
| "NAME;SRC;TRANSLATE_TOOL;CC_NAMESPACE" |
There was a problem hiding this comment.
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" | |||
There was a problem hiding this comment.
| "PUBLIC;TESTONLY" | |
| "PUBLIC;TESTONLY;C_OUTPUT" |
There was a problem hiding this comment.
Done. Thanks for the suggestion.
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. |
96043f5 to
eba61f5
Compare
marbre
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
| if(DEFINED _RULE_C_OUTPUT AND _RULE_C_OUTPUT) | |
| if(_RULE_C_OUTPUT) |
should be sufficient, now that C_OUTPUT is an <options> argument.
There was a problem hiding this comment.
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.
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! :) |
|
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? |
|
I believe at that point the 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. |
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.