[llvm-dev] Contributing Bazel BUILD files similar to gn
Mehdi AMINI via llvm-dev
llvm-dev at lists.llvm.org
Thu Oct 29 13:04:37 PDT 2020
(following up on the discussion on Discord)
On Thu, Oct 29, 2020 at 8:23 AM Tom Stellard via llvm-dev <
llvm-dev at lists.llvm.org> wrote:
> On 10/28/20 7:18 PM, Geoffrey Martin-Noble via llvm-dev wrote:
> > Hi all,
> >
> > tl;dr: We'd like to contribute Bazel BUILD files for LLVM and MLIR in a
> > side-directory in the monorepo, similar to the gn build.
> >
> > Some of us have been working on open-source Bazel BUILD files for the
> > LLVM Project. You may have seen us hanging out in the #build-systems
> > discord channel. As you may know, Google uses Bazel internally and has
> > maintained a Bazel BUILD of LLVM for years. Especially with the
> > introduction of MLIR, we've got more and more OSS projects with a Bazel
> > BUILD depending on LLVM (e.g. IREE <https://github.com/google/iree>and
> > TensorFlow <https://github.com/tensorflow/tensorflow>). We're also not
> > the only ones using Bazel: e.g. PlaidML also has a Bazel BUILD of LLVM
> > that they've borrowed from TF
> > <https://github.com/plaidml/plaidml/blob/master/vendor/llvm/llvm.BUILD>.
>
> > Each of these projects has to jump through some weird hoops to keep
> > their version of the Bazel BUILD files in sync with the code, which
> > requires some fragile combination of scripts and human intervention.
> > Instead, we'd like to move general-purpose Bazel BUILD files into the
> > LLVM Project monorepo. We expect to follow the model of the GN build
> > where these will be maintained by interested contributors rather than
> > expecting the general community to maintain them.
> >
> > To facilitate and test this we've been developing a standalone
> > repository that just has the Bazel BUILD files. It symlinks together the
> > directory trees on top of a submodule as we would need in the monorepo
> > to to avoid in-tree BUILD files. The configuration is at
> > https://github.com/google/llvm-bazel. We now have those in a good place
> > and think they would be useful upstream.
> >
>
> Can you explain some of the benefits to using Bazel instead of CMake?
>
The question can be taken with multiple angles:
1) The benefit for the LLVM project to switch from CMake to Bazel: it isn't
clear even that it would be practical/feasible or serve all the users, this
is *not* *the proposal here*.* Nothing changes for CMake.*
2) The benefit for an LLVM developer to use Bazel instead of CMake: I think
it is minor, unless you have access to a remote build farm in your
environment maybe?
3) The benefit for a project that is willing to use LLVM to use Bazel
instead of CMake: this is a tricky topic because there are many variables.
The Bazel website is a good starting point I think: https://bazel.build
Some of the practical thing I perceive as interesting with Bazel over CMake
personally:
- Strict checking of the dependencies: this seems strange at first that a
missing library dependency won't allow you to include a header which is
present on the filesystem, but this is actually very powerful: this is what
allow Bazel to have correctness with incremental build and what enables
caching at all. This is also quite fundamental to the "distributed build"
mode in Bazel: you can farm-out the build to a large distributed cluster
with remote cachine.
- Declarative approach: this enables static analysis of the BUILD
configuration/graphs, and transformations as well. For example because of
the above, the tooling can figure out unused library dependencies, or
adding missing dependencies as well.
These two aspects are impossible to achieve in a principled way in CMake.
Note that it does not imply that I would (or wouldn't) pick Bazel from my
next open-source project, or that I would recommend LLVM to adopt it as a
primary build system!
I can also miss some reasons why some other projects are using Bazel, but
they got >400 attendees at the Bazel conference last year apparently:
https://blog.bazel.build/2019/12/20/bazelcon-2019.html
Ultimately I don't judge why other projects are picking Bazel, we're just
proposing making their life easier if they start being interested to
include some of LLVM in their project.
In particular, putting on my MLIR ecosystem hat, it'd be great if other
projects using Bazel out-there who may have a possible use for MLIR and
LLVM could have an easier integration path: right now in practice they may
use the Bazel configuration that are shipped inside TensorFlow and try to
adjust them to BUILD LLVM and send us patches to integrate in TensorFlow.
This is difficult because they may want to improve support for the Bazel
config on platforms that TensorFlow does not support: it is just not the
right place for people interested in building with Bazel to collaborate.
I'm a little concerned about having two 'unsupported' buildsystems
> living in tree, and I'm not sure what would stop us from continuing to
> add more.
Following up on some concerns from Tom (and others) explained on Discord
here:
a) Commit Mailing list traffic for updating these build files. This is a
valid point with the GN bot today. It also shows up when I `git log llvm/`
in the monorepo annoyingly.
The proposal would be to have the Bazel and `gn` files in a separate folder
at the top level of the monorepo: that way no commits email would be sent
to any mailing list, and no update would show up in the `git log llvm/`.
b) CI systems picking up more commits to build when not needed: similarly
as above, isolating these in a separate part of the tree will exclude these
from bots tracking the llvm/ or clang/ paths.
c) Investing in developing Bazel support means less investment in CMake. It
is true that engineers fixing Bazel configs are spending time there instead
of in CMake, however the situation is that downstream projects that picked
Bazel (for their own reasons, I don't judge) who start using LLVM are
spending the time to maintain these Bazel files out-of-tree. We're not
making the situation worse by allowing the maintainer of these projects
(who are also frequently upstream contributors) to just collaborate on
their set of patches in a more coordinated way upstream. Also no public
LLVM bots builds with an unsupported build system, any feature is expected
to build with CMake on every supported platform I believe. It seems like
this worked out well with `gn` in practice?
> I would feel better if we had a set of guidelines to define
> the criteria for adding a new buildsytem and also criteria for when we
> can remove them.
>
> Would you be able to amend this proposal to include some general
> guidelines for adding/removing new buildsystems, so that we can discuss
> that too?
>
>
That's an excellent point as well! Could we take inspiration from the
experimental (or non-experimental) backends? For example if `gn` does not
build anymore, send an email to LLVM-dev@ proposing to remove it and see if
any maintainer steps up? Without a community to maintain it we should
remove these easily and quickly?
--
Mehdi
> Thanks,
> Tom
>
>
> >
> > # Details
> >
> > ## What
> >
> > Bazel BUILD files for the LLVM, MLIR, and Clang (PR out for review
> > <https://github.com/google/llvm-bazel/pull/72>) subprojects,
> potentially
> > expanding to others, as needed. Basically everything currently at
> > https://github.com/google/llvm-bazel.
> >
> >
> > ## Where
> >
> > In https://github.com/google/llvm-bazelthe BUILD files live in a single
> > directory tree matching the structure of the overall llvm-project
> > directory. For users, @llvm-project is a single Bazel repository
> > <https://docs.bazel.build/versions/master/build-ref.html#repositories>that
>
> > includes both LLVM and MLIR subprojects. To maintain this structure, we
> > would probably want to put a `bazel` directory in the monorepo's utils
> > directory <https://github.com/llvm/llvm-project/tree/master/utils>,
> > which currently only contains a directory for arcanist. This is
> > different from gn, which is under the LLVM subproject's utils directory
> > <https://github.com/llvm/llvm-project/tree/master/llvm/utils/gn>. We
> > could similarly put the Bazel BUILD files under llvm/utils/bazel but
> > have them be for the entire llvm project (the subsets that are
> > supported). This seems like an odd structure to me, but I know that the
> > CMake build for LLVM also builds the other subprojects
> > <
> https://github.com/llvm/llvm-project/blob/529ac33197f6/llvm/tools/CMakeLists.txt#L34-L41>,
>
> > so maybe this would be preferable.
> >
> > Alternatively we could split each subproject into a separate Bazel
> > repository and put the Bazel build files under each subproject. I think
> > this fragments the configuration of the BUILD without much benefit.
> >
> >
> > ## Configurations
> >
> > We currently have configurations for Linux GCC and Clang, MacOS GCC and
> > Clang, and Windows MSVC. Support for other configurations can be added
> > as-desired, but supporting all possible LLVM build configurations is not
> > the goal.
> >
> >
> > ## Support
> >
> > Support would be similar to the gn build. Contributors could optionally
> > update the Bazel BUILD files as part of their patches, but would be
> > under no obligation to do so.
> >
> >
> > ## Preserving History
> >
> > I don't *think* the history of llvm-bazel is interesting enough to try
> > to merge it into the monorepo and I was planning to submit this as a
> > single patch, but please let me know if you disagree.
> >
> >
> > ## Benefits to the community
> >
> > *
> >
> > Projects that depend on LLVM and use the Bazel build system can
> > avoid duplicating fragile effort. We'll spend more time contributing
> > to LLVM instead :-D
> >
> > *
> >
> > Bazel is stricter than CMake in many ways (e.g. it requires that
> > even header dependencies be declared) and can catch layering issues
> > very easily. There's even an optional layering_check feature we
> > could turn on if its use would benefit the community. (though
> > currently the existing problematic layering makes it a burden to
> > maintain on our own). Even without that additional check, as I've
> > been keeping the Bazel build green, I've found and fixed a number of
> > layering issues in the past couple weeks (e.g.
> > https://reviews.llvm.org/rGb49787df9a
> > <https://reviews.llvm.org/rGb49787df9a535f03761c340dca7ec3ec1155133d
> >and
> > https://reviews.llvm.org/rGc17ae2916c
> > <https://reviews.llvm.org/rGc17ae2916ccf45a0c1717bd5f11598cc4fff342a
> >).
> >
> >
> > Here's a patch <https://reviews.llvm.org/D90352>adding the Bazel build
> > system. It's basically just `cp -r llvm-bazel/llvm-bazel
> > llvm-project/utils/bazel`.
> >
> > _______________________________________________
> > LLVM Developers mailing list
> > llvm-dev at lists.llvm.org
> > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
> >
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20201029/9dc357bd/attachment.html>
More information about the llvm-dev
mailing list