Skip to content

bazel_to_cmake avoid CONFIGURE_DEPENDS in glob #1083

Closed

Description

@benvanik

Change #622 made it skip tablegen It really shouldn't. It's instead using file CONFIGURE_DEPENDS which slows down every build. We have explicit deps defined and should use them. If there's any issue linking in a *Gen target it's something that should be fixed in iree_tablegen_library such that the CONFIGURE_DEPENDS is not required.

Activity

  1. added
    infrastructure
    on Mar 14, 2020
  2. GMNGeoffrey commented on Mar 14, 2020

    @GMNGeoffrey
    Member

    I'm confused about the connection you're drawing between CONFIGURE_DEPENDS and skipping tablegen deps. The generated deps are skipped because they are directly included as source files, which I think confuses cmake. That workaround harcoding based on the target name is gross for sure, so I'd love to fix that and actually list all our deps. CONFIGURE_DEPENDS is used by glob and isn't really related AFAICT. Maybe you're talking about the globs for td_files? That's just because of filegroups that have globs in them.

  3. benvanik commented on Mar 14, 2020

    @benvanik
    CollaboratorAuthor

    CONFIGURE_DEPENDS is not used by glob, but something glob can do:

    If the CONFIGURE_DEPENDS flag is specified, CMake will add logic to the main build system check
    target to rerun the flagged GLOB commands at build time. If any of the outputs change, CMake will
    regenerate the build system.
    

    This then shows at build time (on every build) as:

    [0/2] Re-checking globbed directories...
    

    It slows down each build by needing to recheck all dirs with globs instead of only performing the glob action during configuration (53 within the project, so far). That's not good. From the docs:

    Note We do not recommend using GLOB to collect a list of source files from your source tree. If no
    CMakeLists.txt file changes when a source is added or removed then the generated build system 
    cannot know when to ask CMake to regenerate. The CONFIGURE_DEPENDS flag may not work 
    reliably on all generators, or if a new generator is added in the future that cannot support it, 
    projects using it will be stuck. Even if CONFIGURE_DEPENDS works reliably, there is still a cost to 
    perform the check on every rebuild.
    

    Visual Studio (full) and xcode don't support this mode well/at all (you basically get your entire workspace reloaded if you touch/add a td file and try to run).
    You probably haven't noticed this or the performance issues because you aren't doing incremental builds. I am :)

    As for the relation with *Gen targets: I noticed dependency tracking issues with ninja not rerunning tblgen in all cases. I suppose the CONFIGURE_DEPENDS was working around that because it was watching for any td change and reconfiguring (which then forces a rebuild), whereas without the CONFIGURE_DEPENDS on *.td it sometimes doesn't. If the dependencies were all correct and the glob with CONFIGURE_DEPENDS *.td wasn't related then it should be a no-op and have no purpose and should just be removed. I suspect it was added for a reason, though ¯_(ツ)_/¯

  4. GMNGeoffrey commented on Mar 15, 2020

    @GMNGeoffrey
    Member

    The CONFIGURE_DEPENDS is used by the implementation of bazel glob we generate for CMake.

    The variables for td files globs are actually completely unused. Those are only used in filegroups and we pass on actually implementing the filegroup because they're never used as dependencies in CMake (even before bazel to cmake). Maybe that needs to be re-evaluated. Because they were unused, I hardcoded a no-op for td_files filegroups. That allowed us to run bazel_to_cmake on a bunch more files to keep them in sync. The glob variables only exist because of the way bazel to cmake works by execing the BUILD file. The glob function argument gets evaluated regardless of whether or not it's used and creates the glob variable. If it's actually load bearing that's definitely a bug, and if it's slowing down your builds let's see if we can kill it.

    For handling globs in bazel_to_cmake, we have a few options:

    1. Evaluate the glob at conversion time and expand it to explicit files in the CMakeLists.txt file. This is what I did originally. The problem was that with that implementation non BUILD-file updates require running the converter, which meant that people kept not doing it. Maybe if we get internal enforcement of bazel_to_cmake, then this wouldn't be as much of an issue, but internal enforcement would also be much more difficult if it conversion of a single file needs to touch the filesystem. We now don't need to touch it at all and are just dependent on the BUILD file (and the existing CMakeLists.txt file for the license year and DO_NOT_EDIT pragmas).
    2. Create a CMake glob with CONFIGURE_DEPENDS. This seemed like the closest analog for bazel globs, so when I switched to using a CMake glob that's what I went with.
    3. Use a CMake glob without CONFIGURE_DEPENDS. If I understand that warning correctly, then CMake doesn't recommend using globs at all for source files (what else would you use it for?). Let's go through our actual uses of glob and see whether it matters. Without configure depends you have to manually run configure again if you add a source file that is in a glob, is that right?
    4. Didn't actually think of this one at the time, but we could ban globs in our bazel files. We use them in two places: lit tests and exporting td files. https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/google/iree%24+f:/BUILD%24+glob%5C%28+-f:experimental/&patternType=regexp

    I think globs are generally a bad idea in build systems because they encourage excessive dependency coupling and make configuration less explicit. The one exception, IMO, is for globbing tests: for a dependency, you'll quickly figure out that you forgot to add it to a build file, but for a test you might very well check in a no-op. So I would vote for not globbing td files at all and making those explicit lists of source files. That at least kills a bunch of the globs. For the mlir file tests though, I think a glob in bazel is a good idea. If we do a glob in cmake with CONFIGURE_DEPENDS, that means you need to reconfigure if you add a new test source file, is that right?

  5. GMNGeoffrey commented on Mar 15, 2020

    @GMNGeoffrey
    Member

    So the flakey build issue you're seeing is because CMake isn't seeing the connection between the td source files and e.g. the dialect cc library, right? I think the dependency between the *Gen targets is already modeled by listing the gen files in the SRCS attribute of the cc library; that's why #622 drops them from the dependencies. They're actually compiled in as sources not linked in. I think the issue then is that the iree_tablegen_library CMake function doesn't model the dependency of tablegen on the included td files at all. It just looks at the single source file. Do you have a reproducer for the issue?

  6. marbre commented on Mar 24, 2020

    @marbre
    Member

    I think the dependency between the *Gen targets is already modeled by listing the gen files in the SRCS attribute of the cc library; that's why #622 drops them from the dependencies.

    That was the intention. If these targets are added as deps, e.g. to IR targets which are marked ALWAYSLINK, linkage fails. So one could add those explicitly with CMake's add_dependency(), but without linking them. Anyway, including these files in SRCS should be sufficient.

  7. GMNGeoffrey commented on Mar 28, 2020

    @GMNGeoffrey
    Member

    As I've been reading more about cmake in attempts to get its dependency graph to work, I think we shouldn't need "CONFIGURE DEPENDS" on the mlir globs at all. That's only for source files being fed to some compilation. As for td files, that glob is currently unused. I would love to properly model the dependencies here and I think we can tell cmake the files are generated, which might help. I'll investigate more, but for now I think we should drop "CONFIGURE DEPENDS" from the bazel_to_cmake glob implementation.

  8. GMNGeoffrey commented on Mar 29, 2020

    @GMNGeoffrey
    Member

    Although actually with the check runner (and #1289) we will probably soon be globbing files that are a source to a build step (though not a C++ build step), so maybe that's the case where CONFIGURE DEPENDS is necessary? We could reconsider doing the globs at bazel_to_cmake translation time, although I think I'd want upstream enforcement before we did that.

  9. changed the title [-]bazel_to_cmake is ignoring *Gen targets and shouldn't[/-] [+]bazel_to_cmake avoid CONFIGURE_DEPENDS in glob[/+] on Apr 9, 2020
  10. GMNGeoffrey commented on Apr 9, 2020

    @GMNGeoffrey
    Member

    Another thought. We could use a glob in bazel that requires also listing the files explicitly. Basically just a build-time check in bazel. Then you can't forget to update the build file with a test, the cmake file is only dependent on the build file, and we're not doing any globbing in cmake.

    Something like:

    def enforce_glob(include, exclude = [], files):
        actual = glob(include, exclude)
        if actual != files:
            fail("File list does not match glob")
        return files
    
  11. added this to the Infrastructure Fixit milestone on Apr 15, 2020
  12. marbre commented on Jun 16, 2020

    @marbre
    Member

    Sorry, missed your latest comment. Well as this allows to avoid globbing in CMake, this idea is nice. So how do we proceed?

  13. GMNGeoffrey commented on Jun 16, 2020

    @GMNGeoffrey
    Member

    I was waiting for someone to respond :-D (and then I forgot about it). How much harm is CONFIGURE DEPENDS causing? What exactly would happen if we just dropped it in our globs? Does that only cause issues if the files are used in a later build step?

  14. benvanik commented on Nov 22, 2020

    @benvanik
    CollaboratorAuthor

    Almost all of these are iree/compiler/ only; they'll leak across the compiler boundary if users choose to install the test suite, but that's fine - not requiring users to run bazel-to-cmake if they are adding a test is 👍

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

Metadata

Metadata

Assignees

Labels

infrastructure

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions