Skip to content

[bazel] cross language LTO #32368

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

Merged
merged 3 commits into from
Apr 29, 2025
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 .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ build:release --compilation_mode=opt
build:release-lto --copt=-flto=thin
build:release-lto --linkopt=-flto=thin
build:release-lto --@rules_rust//rust/settings:lto=thin
build:release-lto --@//misc/bazel/platforms:xlang_lto=True

# Builds from `main` or tagged builds.
#
Expand Down
5 changes: 4 additions & 1 deletion misc/bazel/cargo-gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = ["-Copt-level=3"],
rustc_flags = ["-Copt-level=3"] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [":cargo_gazelle"] + all_crate_deps(normal = True),
)
Expand Down
19 changes: 19 additions & 0 deletions misc/bazel/cargo-gazelle/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::collections::BTreeMap;
use guppy::graph::PackageMetadata;
use std::sync::LazyLock;

use crate::ToBazelDefinition;
use crate::targets::{AdditiveContent, RustTestSize};

const KEY_NAME: &str = "cargo-gazelle";
Expand Down Expand Up @@ -315,3 +316,21 @@ impl CommonConfig {
&self.rustc_env
}
}

/// Values that are mirrored in `/misc/bazel/platforms/BUILD.bazel`.
#[derive(Debug, Copy, Clone)]
pub enum ConfigSettingGroup {
XlangLtoEnabled,
}

impl ToBazelDefinition for ConfigSettingGroup {
fn format(&self, w: &mut dyn std::fmt::Write) -> Result<(), std::fmt::Error> {
match self {
ConfigSettingGroup::XlangLtoEnabled => {
write!(w, "\"@//misc/bazel/platforms:xlang_lto_enabled\"")?
}
}

Ok(())
}
}
12 changes: 6 additions & 6 deletions misc/bazel/cargo-gazelle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::collections::{BTreeMap, VecDeque};
use std::fmt::{self, Debug, Write};
use std::rc::Rc;

use crate::platforms::PlatformVariant;
use crate::targets::RustTarget;

pub mod args;
Expand Down Expand Up @@ -510,20 +509,21 @@ impl ToBazelDefinition for Glob {
/// ```
#[derive(Debug)]
pub struct Select<T> {
entries: BTreeMap<PlatformVariant, T>,
entries: BTreeMap<String, T>,
default: T,
}

impl<T> Select<T> {
pub fn new<E, I>(entires: I, default: E) -> Select<T>
pub fn new<E, I, J>(entires: I, default: E) -> Select<T>
where
E: Into<T>,
I: IntoIterator<Item = (PlatformVariant, E)>,
J: ToBazelDefinition,
I: IntoIterator<Item = (J, E)>,
{
Select {
entries: entires
.into_iter()
.map(|(variant, entry)| (variant, entry.into()))
.map(|(variant, entry)| (variant.to_bazel_definition(), entry.into()))
.collect(),
default: default.into(),
}
Expand All @@ -538,7 +538,7 @@ impl<T: ToBazelDefinition> ToBazelDefinition for Select<T> {
{
let mut w = w.indent();
for (variant, entry) in &self.entries {
variant.format(&mut w)?;
write!(w, "{variant}")?;
write!(w, ": ")?;
entry.format(&mut w)?;
writeln!(w, ",")?;
Expand Down
13 changes: 11 additions & 2 deletions misc/bazel/cargo-gazelle/src/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Write};
use std::str::FromStr;

use crate::config::{CrateConfig, GlobalConfig};
use crate::config::{ConfigSettingGroup, CrateConfig, GlobalConfig};
use crate::context::CrateContext;
use crate::platforms::PlatformVariant;
use crate::rules::Rule;
Expand Down Expand Up @@ -382,8 +382,17 @@ impl RustBinary {
let (paths, globs) = bin_config.common().compile_data();
let compile_data = List::new(paths).concat_other(globs.map(Glob::new));

let xlang_lto_select: Select<List<QuotedString>> = Select::new(
[(
ConfigSettingGroup::XlangLtoEnabled,
vec![QuotedString::new("-Clinker-plugin-lto")],
)],
vec![],
);

let env = Dict::new(bin_config.env());
let rustc_flags = List::new(bin_config.common().rustc_flags());
let rustc_flags =
List::new(bin_config.common().rustc_flags()).concat_other(xlang_lto_select);
let rustc_env = Dict::new(bin_config.common().rustc_env());

Ok(Some(RustBinary {
Expand Down
24 changes: 23 additions & 1 deletion misc/bazel/platforms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ support building Materialize for.
"""

load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")

# A flag that we can specify on the command line to configure whether or not we
# build with a sanitizer.
Expand Down Expand Up @@ -45,6 +45,28 @@ config_setting(
flag_values = {":sanitizer": "hwaddress"},
)

bool_flag(
name = "xlang_lto",
build_setting_default = False,
)

config_setting(
name = "use_xlang_lto",
flag_values = {":xlang_lto": "True"},
)

# With our current toolchain setup, cross language LTO is only supported when building for Linux.
#
# See <https://github.com/rust-lang/rust/issues/60059> for macOS support.
selects.config_setting_group(
name = "xlang_lto_enabled",
match_all = [
"@platforms//os:linux",
":use_xlang_lto",
],
visibility = ["//visibility:public"],
)

# We only want to use jemalloc if we're building for Linux and we're not using sanitizers.
selects.config_setting_group(
name = "use_jemalloc",
Expand Down
5 changes: 4 additions & 1 deletion src/balancerd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
":mz_balancerd",
Expand Down
5 changes: 4 additions & 1 deletion src/catalog-debug/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
"//src/adapter:mz_adapter",
Expand Down
5 changes: 4 additions & 1 deletion src/clusterd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
":mz_clusterd",
Expand Down
5 changes: 4 additions & 1 deletion src/environmentd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
":mz_environmentd",
Expand Down
5 changes: 4 additions & 1 deletion src/fivetran-destination/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
":mz_fivetran_destination",
Expand Down
5 changes: 4 additions & 1 deletion src/frontegg-mock/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
":mz_frontegg_mock",
Expand Down
5 changes: 4 additions & 1 deletion src/interchange/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
":mz_interchange",
Expand Down
5 changes: 4 additions & 1 deletion src/kafka-util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
":mz_kafka_util",
Expand Down
5 changes: 4 additions & 1 deletion src/lsp-server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.3.0",
deps = [
":mz_lsp_server",
Expand Down
5 changes: 4 additions & 1 deletion src/materialized/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
"//src/clusterd:mz_clusterd",
Expand Down
5 changes: 4 additions & 1 deletion src/mz-debug/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.1.0",
deps = [
"//src/build-info:mz_build_info",
Expand Down
5 changes: 4 additions & 1 deletion src/mz/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.3.0",
deps = [
":mz",
Expand Down
5 changes: 4 additions & 1 deletion src/orchestratord/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
":mz_orchestratord",
Expand Down
5 changes: 4 additions & 1 deletion src/persist-cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
"//src/http-util:mz_http_util",
Expand Down
5 changes: 4 additions & 1 deletion src/pgtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
":mz_pgtest",
Expand Down
5 changes: 4 additions & 1 deletion src/s3-datagen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
"//src/aws-util:mz_aws_util",
Expand Down
5 changes: 4 additions & 1 deletion src/sqllogictest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.1",
deps = [
":mz_sqllogictest",
Expand Down
5 changes: 4 additions & 1 deletion src/testdrive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.144.0-dev.0",
deps = [
":mz_testdrive",
Expand Down
5 changes: 4 additions & 1 deletion test/metabase/smoketest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ rust_binary(
lint_config = ":lints",
proc_macro_deps = [] + all_crate_deps(proc_macro = True),
rustc_env = {},
rustc_flags = [],
rustc_flags = [] + select({
"@//misc/bazel/platforms:xlang_lto_enabled": ["-Clinker-plugin-lto"],
"//conditions:default": [],
}),
version = "0.0.0",
deps = [
"//src/metabase:mz_metabase",
Expand Down