Skip to content

Conversation

@dzbarsky
Copy link
Contributor

@dzbarskydzbarsky commented Dec 11, 2025

Few things going on here:

  • I think we should be able to use the blake3 x86-64 asm on windows with some slight adjustments (sorry it's still named *_unix.S, renaming seemed like a bit bigger project)
  • genrule is really evil because it bakes the path to the host bash into the command, which fails spectacularly when running on a non-windows remote executor. Swap to write_file/run_binary rules to mitigate it
  • The existing windows linkopts do not work correctly for clang in mingw mode

With this set of changes (as well as another one that rewrites bundle_resources.py into C) I am able to hermetically cross-build clang/lld/etc from a windows host to a (mingw) windows target on a linux executor, and use the resulting compilers to compile C binaries/tests (see https://github.com/dzbarsky/toolchains_llvm_bootstrapped/actions/runs/20121686105/job/57743214313?pr=1 and dzbarsky/toolchains_llvm_bootstrapped#1)

@llvmbotllvmbot added llvm:support bazel "Peripheral" support tier build system: utils/bazel labels Dec 11, 2025
@llvmbot
Copy link
Member

@llvm/pr-subscribers-llvm-support

Author: David Zbarsky (dzbarsky)

Changes

Few things going on here:

  • I think we should be able to use the blake3 x86-64 asm on windows with some slight adjustments (sorry it's still named *_unix.S, renaming seemed like a bit bigger project)
  • genrule is really evil because it bakes that path to the host bash into the command, which fails spectacularly when running on a non-windows remote executor. Swap to write_file/run_binary rules to mitigate it
  • The existing windows linkopts do not work correctly for clang in mingw mode

With this set of changes (as well as another one that rewrites bundle_resources.py into C) I am able to hermetically cross-build clang/lld/etc from a windows host to a (mingw) windows target on a linux executor, and use the resulting compilers to compile C binaries/tests


Full diff: https://github.com/llvm/llvm-project/pull/171761.diff

7 Files Affected:

  • (modified) llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S (+7-5)
  • (modified) llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S (+13-11)
  • (modified) llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S (+11-9)
  • (modified) llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S (+11-9)
  • (modified) utils/bazel/llvm-project-overlay/clang/BUILD.bazel (+29-23)
  • (modified) utils/bazel/llvm-project-overlay/lld/BUILD.bazel (+12-7)
  • (modified) utils/bazel/llvm-project-overlay/llvm/BUILD.bazel (+40-9)
diff --git a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S index e98893c7ef8b8..d6fa51638da39 100644 --- a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S+++ b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S@@ -16,15 +16,17 @@ #define _CET_ENDBR #endif -#ifdef __APPLE__-#define HIDDEN .private_extern+#if defined(__APPLE__)+#define HIDDEN(symbol) .private_extern symbol+#elif defined(__ELF__)+#define HIDDEN(symbol) .hidden symbol #else -#define HIDDEN .hidden+#define HIDDEN(symbol) #endif .intel_syntax noprefix -HIDDEN _blake3_hash_many_avx2-HIDDEN blake3_hash_many_avx2+HIDDEN(_blake3_hash_many_avx2)+HIDDEN(blake3_hash_many_avx2) .global _blake3_hash_many_avx2 .global blake3_hash_many_avx2 #ifdef __APPLE__ diff --git a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S index b4b14946de10e..cf2c88329af5c 100644 --- a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S+++ b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S@@ -16,21 +16,23 @@ #define _CET_ENDBR #endif -#ifdef __APPLE__-#define HIDDEN .private_extern+#if defined(__APPLE__)+#define HIDDEN(symbol) .private_extern symbol+#elif defined(__ELF__)+#define HIDDEN(symbol) .hidden symbol #else -#define HIDDEN .hidden+#define HIDDEN(symbol) #endif .intel_syntax noprefix -HIDDEN _blake3_hash_many_avx512-HIDDEN blake3_hash_many_avx512-HIDDEN blake3_compress_in_place_avx512-HIDDEN _blake3_compress_in_place_avx512-HIDDEN blake3_compress_xof_avx512-HIDDEN _blake3_compress_xof_avx512-HIDDEN blake3_xof_many_avx512-HIDDEN _blake3_xof_many_avx512+HIDDEN(_blake3_hash_many_avx512)+HIDDEN(blake3_hash_many_avx512)+HIDDEN(blake3_compress_in_place_avx512)+HIDDEN(_blake3_compress_in_place_avx512)+HIDDEN(blake3_compress_xof_avx512)+HIDDEN(_blake3_compress_xof_avx512)+HIDDEN(blake3_xof_many_avx512)+HIDDEN(_blake3_xof_many_avx512) .global _blake3_hash_many_avx512 .global blake3_hash_many_avx512 .global blake3_compress_in_place_avx512 diff --git a/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S index d69a1706fefe7..5563fc3b5ba09 100644 --- a/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S+++ b/llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S@@ -16,19 +16,21 @@ #define _CET_ENDBR #endif -#ifdef __APPLE__-#define HIDDEN .private_extern+#if defined(__APPLE__)+#define HIDDEN(symbol) .private_extern symbol+#elif defined(__ELF__)+#define HIDDEN(symbol) .hidden symbol #else -#define HIDDEN .hidden+#define HIDDEN(symbol) #endif .intel_syntax noprefix -HIDDEN blake3_hash_many_sse2-HIDDEN _blake3_hash_many_sse2-HIDDEN blake3_compress_in_place_sse2-HIDDEN _blake3_compress_in_place_sse2-HIDDEN blake3_compress_xof_sse2-HIDDEN _blake3_compress_xof_sse2+HIDDEN(blake3_hash_many_sse2)+HIDDEN(_blake3_hash_many_sse2)+HIDDEN(blake3_compress_in_place_sse2)+HIDDEN(_blake3_compress_in_place_sse2)+HIDDEN(blake3_compress_xof_sse2)+HIDDEN(_blake3_compress_xof_sse2) .global blake3_hash_many_sse2 .global _blake3_hash_many_sse2 .global blake3_compress_in_place_sse2 diff --git a/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S b/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S index c5b103af61c4f..5866bfb7ae461 100644 --- a/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S+++ b/llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S@@ -16,19 +16,21 @@ #define _CET_ENDBR #endif -#ifdef __APPLE__-#define HIDDEN .private_extern+#if defined(__APPLE__)+#define HIDDEN(symbol) .private_extern symbol+#elif defined(__ELF__)+#define HIDDEN(symbol) .hidden symbol #else -#define HIDDEN .hidden+#define HIDDEN(symbol) #endif .intel_syntax noprefix -HIDDEN blake3_hash_many_sse41-HIDDEN _blake3_hash_many_sse41-HIDDEN blake3_compress_in_place_sse41-HIDDEN _blake3_compress_in_place_sse41-HIDDEN blake3_compress_xof_sse41-HIDDEN _blake3_compress_xof_sse41+HIDDEN(blake3_hash_many_sse41)+HIDDEN(_blake3_hash_many_sse41)+HIDDEN(blake3_compress_in_place_sse41)+HIDDEN(_blake3_compress_in_place_sse41)+HIDDEN(blake3_compress_xof_sse41)+HIDDEN(_blake3_compress_xof_sse41) .global blake3_hash_many_sse41 .global _blake3_hash_many_sse41 .global blake3_compress_in_place_sse41 diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel index d3bade579f003..0d1c4a800c9df 100644 --- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel@@ -4,6 +4,8 @@ load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") load("@rules_python//python:defs.bzl", "py_binary") +load("@bazel_lib//lib:run_binary.bzl", "run_binary")+load("@bazel_skylib//rules:write_file.bzl", "write_file") load( "//:vars.bzl", "LLVM_VERSION_MAJOR", @@ -577,23 +579,18 @@ exports_files( glob(["include/**/*.td"]), ) -genrule(+write_file( name = "basic_version_gen", - outs = ["include/clang/Basic/Version.inc"],- cmd = (- "echo '#define CLANG_VERSION{vers}' >> $@\n" +- "echo '#define CLANG_VERSION_MAJOR{major}' >> $@\n" +- "echo '#define CLANG_VERSION_MAJOR_STRING \"{major}\"' >> $@\n" +- "echo '#define CLANG_VERSION_MINOR{minor}' >> $@\n" +- "echo '#define CLANG_VERSION_PATCHLEVEL{patch}' >> $@\n" +- "echo '#define MAX_CLANG_ABI_COMPAT_VERSION{major}' >> $@\n" +- "echo '#define CLANG_VERSION_STRING \"{vers}\"' >> $@\n"- ).format(- major = LLVM_VERSION_MAJOR,- minor = LLVM_VERSION_MINOR,- patch = LLVM_VERSION_PATCH,- vers = PACKAGE_VERSION,- ),+ out = "include/clang/Basic/Version.inc",+ content = [+ "#define CLANG_VERSION{}".format(PACKAGE_VERSION),+ "#define CLANG_VERSION_MAJOR{}".format(LLVM_VERSION_MAJOR),+ "#define CLANG_VERSION_MAJOR_STRING \"{}\"".format(LLVM_VERSION_MAJOR),+ "#define CLANG_VERSION_MINOR{}".format(LLVM_VERSION_MINOR),+ "#define CLANG_VERSION_PATCHLEVEL{}".format(LLVM_VERSION_PATCH),+ "#define MAX_CLANG_ABI_COMPAT_VERSION{}".format(LLVM_VERSION_MAJOR),+ "#define CLANG_VERSION_STRING \"{}\"".format(PACKAGE_VERSION),+ ], ) cc_library( @@ -611,13 +608,15 @@ cc_library( # TODO: This should get replaced with something that actually generates the # correct version number. -genrule(+write_file( name = "vcs_version_gen", # This should be under lib/Basic, but because of how the include paths # are passed through bazel, it's easier to drop generated files next to # the other includes. - outs = ["include/VCSVersion.inc"],- cmd = "echo '#undef CLANG_REVISION' > $@",+ out = "include/VCSVersion.inc",+ content = [+ "#undef CLANG_REVISION",+ ], ) py_binary( @@ -979,16 +978,21 @@ cc_library( ], ) -genrule(+run_binary( name = "analysis_htmllogger_gen", + tool = ":bundle_resources", srcs = [ "lib/Analysis/FlowSensitive/HTMLLogger.html", "lib/Analysis/FlowSensitive/HTMLLogger.css", "lib/Analysis/FlowSensitive/HTMLLogger.js", ], outs = ["lib/Analysis/FlowSensitive/HTMLLogger.inc"], - cmd = "$(location :bundle_resources) $@ $(SRCS)",- tools = [":bundle_resources"],+ args = [+ "$@",+ "$(execpath lib/Analysis/FlowSensitive/HTMLLogger.html)",+ "$(execpath lib/Analysis/FlowSensitive/HTMLLogger.css)",+ "$(execpath lib/Analysis/FlowSensitive/HTMLLogger.js)",+ ], ) cc_library( @@ -1549,7 +1553,9 @@ cc_library( "lib/Driver", ], linkopts = select({- "@platforms//os:windows": ["version.lib"],+ "//llvm:is_windows_clang_mingw": ["-lversion"],+ "//llvm:is_windows_clang_cl": ["version.lib"],+ "//llvm:is_windows_msvc": ["version.lib"], "//conditions:default": [], }), textual_hdrs = glob([ diff --git a/utils/bazel/llvm-project-overlay/lld/BUILD.bazel b/utils/bazel/llvm-project-overlay/lld/BUILD.bazel index 1118f5e81f7ed..6f830ba72a0a2 100644 --- a/utils/bazel/llvm-project-overlay/lld/BUILD.bazel+++ b/utils/bazel/llvm-project-overlay/lld/BUILD.bazel@@ -10,6 +10,7 @@ load( load("//llvm:binary_alias.bzl", "binary_alias") load("//llvm:driver.bzl", "llvm_driver_cc_binary") load("//mlir:tblgen.bzl", "gentbl_cc_library") +load("@bazel_skylib//rules:write_file.bzl", "write_file") package( default_visibility = ["//visibility:public"], @@ -19,17 +20,21 @@ package( licenses(["notice"]) # TODO: Actually compute version info -genrule(+write_file( name = "config_version_gen", - outs = ["include/lld/Common/Version.inc"],- cmd = "echo '#define LLD_VERSION_STRING \"{}\"' > $@".format(LLVM_VERSION),+ out = "include/lld/Common/Version.inc",+ content = [+ "#define LLD_VERSION_STRING \"{}\"".format(LLVM_VERSION),+ ], ) -genrule(+write_file( name = "vcs_version_gen", - outs = ["Common/VCSVersion.inc"],- cmd = "echo '#undef LLD_REVISION' >> $@\n" +- "echo '#undef LLD_REPOSITORY' >> $@\n",+ out = "Common/VCSVersion.inc",+ content = [+ "#undef LLD_REVISION",+ "#undef LLD_REPOSITORY",+ ], ) # See https://github.com/bazelbuild/bazel/issues/13803 diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel index 127bd7968ef0a..98d0cfe55f44e 100644 --- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel+++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel@@ -4,6 +4,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "string_flag") load("@bazel_skylib//rules:expand_template.bzl", "expand_template") +load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") load("@rules_python//python:defs.bzl", "py_binary") load("@rules_shell//shell:sh_binary.bzl", "sh_binary") @@ -208,18 +209,40 @@ cc_library( deps = [":config"], ) -genrule(+write_file( name = "generate_vcs_revision", - outs = ["include/llvm/Support/VCSRevision.h"],- cmd = "echo '#undef LLVM_REVISION' >> $@\n" +- "echo '#undef LLVM_REPOSITORY' >> $@\n",+ out = "include/llvm/Support/VCSRevision.h",+ content = [+ "#undef LLVM_REVISION",+ "#undef LLVM_REPOSITORY",+ ], ) -genrule(+write_file( name = "generate_static_extension_registry", - outs = ["include/llvm/Support/Extension.def"],- cmd = "echo -e '// extension handlers' >> $@\n" +- "echo -e '#undef HANDLE_EXTENSION' >> $@\n",+ out = "include/llvm/Support/Extension.def",+ content = [+ "// extension handlers",+ "#undef HANDLE_EXTENSION",+ ],+)++config_setting(+ name = "is_windows_clang_mingw",+ constraint_values = ["@platforms//os:windows"],+ flag_values ={"@rules_cc//cc/compiler:compiler": "clang"},+)++config_setting(+ name = "is_windows_clang_cl",+ constraint_values = ["@platforms//os:windows"],+ flag_values ={"@rules_cc//cc/compiler:compiler": "clang-cl"},+)++config_setting(+ name = "is_windows_msvc",+ constraint_values = ["@platforms//os:windows"],+ flag_values ={"@rules_cc//cc/compiler:compiler": "msvc-cl"}, ) cc_library( @@ -297,7 +320,15 @@ cc_library( }), includes = ["include"], linkopts = select({- "@platforms//os:windows": [+ ":is_windows_clang_mingw": [+ "-lws2_32",+ "-lntdll",+ ],+ ":is_windows_clang_cl": [+ "ws2_32.lib",+ "ntdll.lib",+ ],+ ":is_windows_msvc": [ "ws2_32.lib", "ntdll.lib", ], 

Copy link
Member

@keithkeith left a comment

Choose a reason for hiding this comment

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

bazel changes look good, we should give it a bit to see if anyone with more context can review the blake3 changes with

# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("@bazel_lib//lib:run_binary.bzl", "run_binary")
Copy link
Collaborator

Choose a reason for hiding this comment

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

CI is failing here. You'll need to add this to MODULE.bazel, or use the version from skylib instead if that's sufficient

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Yep, thanks, addressing!

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

the skylib version doesn't support $@ but i'll just expand the output file and use it, not worth a new dep

linkopts=select({
"@platforms//os:windows": [
":is_windows_clang_mingw": [
"-lole32",
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

note ole32 and uuid were missing here but are passed in https://github.com/llvm/llvm-project/blob/fa1dceb67f6fef652c2bf91c070ede64eadc49c6/llvm/lib/Support/CMakeLists.txt#L44C48-L44C53

the other libs are either added by default already or actually unused.

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

Labels

bazel"Peripheral" support tier build system: utils/bazelllvm:support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@dzbarsky@llvmbot@keith@rupprecht