Skip to content

Take libsais from the Bazel Central Registry - #1313

Merged
taku910 merged 1 commit into
google:masterfrom
BYVoid:claude/libsais-bcr
Aug 31, 2026
Merged

taku910 merged 1 commit into
google:masterfrom
BYVoid:claude/libsais-bcr

Conversation

@BYVoid

@BYVoid BYVoid commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

The Bazel build depends on the libsais BCR module instead of compiling third_party/libsais/libsais.c. The CMake build is unchanged and still compiles the vendored copy.

2.10.4.bcr.2 vs. third_party/libsais

Both are upstream v2.10.4 — third_party/libsais/libsais.h declares LIBSAIS_VERSION_STRING "2.10.4", and the module fetches the v2.10.4 tarball. I diffed the vendored sources against that tarball:

result
libsais.h identical once comments and formatting are normalised
libsais.c identical once comments, formatting, include order and one clang-format line continuation are normalised

So unlike third_party/darts_clone, the vendored libsais carries no local patches — it is upstream v2.10.4 after this repo's IWYU/format pass. The module needs no patch to the library sources either, so it can be taken at a plain version with nothing overridden on this side. .bcr.1 and .bcr.2 are registry-side revisions of the same upstream source: .bcr.1 adds the OpenMP toggle described below, and .bcr.2 patches out the archive's VERSION file, without which MSVC resolves #include <version> to it on a case-insensitive file system (bazelbuild/bazel-central-registry#10291, reported from this PR's Windows jobs).

SentencePiece calls libsais, libsais_omp, libsais_plcp, libsais_plcp_omp, libsais_lcp and libsais_lcp_omp, all from unigram_model_trainer.cc. All six are in the module's @libsais//:libsais target (uint8_t input, int32_t suffix array); the 16/64/16x64 variants are unused.

OpenMP

2.10.4.bcr.1 and later expose an --@libsais//:openmp flag, the counterpart of libsais' own LIBSAIS_USE_OPENMP CMake option. It adds -fopenmp (or /openmp), and sets LIBSAIS_OPENMP as a defines, so the macro reaches dependents and unigram_model_trainer.cc compiles its _omp branch — the same way the CMake build's add_compile_definitions(LIBSAIS_OPENMP) reaches it.

It is off by default, which matches both libsais' own CMake default and the //third_party:libsais target this replaces, so this change does not alter what the Bazel build does today: it still takes the serial path. This PR does not turn it on, because -fopenmp is unsupported by Apple clang, so enabling it in .bazelrc would break the macOS Bazel job.

Checked with a toolchain that does support it (--repo_env=CC=/opt/homebrew/opt/llvm/bin/clang), building -c opt with and without the flag and timing the seed-extraction phase from the trainer's own log over an 8.7 MB corpus, 6 runs each, interleaved:

seed extraction trainer log line
default 0.446 s Extracting seed sentencepieces (libsais)
--@libsais//:openmp 0.396 s Extracting seed sentencepieces (libsais_omp, 8 threads)

So the flag works end to end and is worth roughly 1.13x here — suffix array construction does not parallelise dramatically at this corpus size. Mentioned only to show the path is wired correctly; the default is unchanged.

Optimisation flags

//third_party:libsais set copts = ["-O3"], which the module does not. Per bazel aquery:

before after
-c opt -O3 -O2
-c fastbuild -O3 none

I measured what that actually costs, on macOS/arm64 with Apple clang 21, two ways.

Isolated. A driver running the same libsais → libsais_plcp → libsais_lcp sequence as ExtractSeedSentencePieces, over an 8.7 MB corpus, with only libsais.c's optimisation level varied (driver fixed at -O2; median of 24 runs):

libsais.c per pass
-O0 0.368 s
-O2 0.146 s
-O3 0.147 s

End to end. spm_train --model_type=unigram --vocab_size=8000 over the same corpus, timing the seed-extraction phase from the trainer's log lines (6 runs each, interleaved order):

build seed extraction
-c opt, module as-is (-O2) 0.439 s
-c opt + --per_file_copt=…libsais…@-O3 0.438 s

-O3 buys nothing over -O2 for this code — the two agree to within 1% in both measurements — so dropping the copts costs no measurable performance in -c opt. I originally assumed this would be the one real regression in the change; it measures as a non-issue, so I am not proposing a .bazelrc workaround unless you want one.

The fastbuild row is the larger change: libsais goes from -O3 to unoptimised, which the isolated numbers put at ~2.5x slower for seed extraction. That only affects training run from a fastbuild binary, which was already an unoptimised build of everything else in the trainer.

Two caveats. This is one machine and one compiler; GCC on Linux may weigh -O3 differently. And total spm_train wall time did shift by ~15% between the two binaries, but that difference sits entirely in the EM iterations, which do not call libsais and whose objects are byte-identical between the two builds — aquery confirms exactly one compile action changes. It is binary layout, not codegen, which is why the numbers above time the seed-extraction phase specifically rather than the whole run.

Why the module rather than the vendored copy

Upgrades become a version bump. libsais is a single 400 KB .c file. Moving it forward today means re-dropping that file and re-running the format pass over it; with the module it is one line in MODULE.bazel.

Known provenance. The module pins the upstream v2.10.4 tarball by SHA-256. The vendored copy is a reformatted snapshot whose correspondence to a specific upstream release can only be established by diffing, as above.

Visible in dependency tooling. A bazel_dep with a version appears in MODULE.bazel.lock, bazel mod graph, and any SBOM generated from them. A checked-in .c file does not.

The vendored copy is not deleted: the CMake build still compiles it, so this reduces the number of builds depending on the unlabelled snapshot from two to one rather than to zero.

Include path

The module exports the header as libsais.h (includes = ["include"]), so the one #include "third_party/libsais/libsais.h" in unigram_model_trainer.cc loses the prefix. src/CMakeLists.txt gains third_party/libsais on its include path so the CMake build resolves the same spelling against the vendored copy. libsais.h is not reachable from any installed header, so the target_include_directories(... PUBLIC ...) blocks need no change.

Verified

macOS/arm64:

  • bazel test //... — 22 of 22 tests pass
  • cmake --build — sentencepiece_train-static builds, compiling both unigram_model_trainer.cc and the vendored libsais.c

Note

This touches third_party/BUILD.bazel, as does #1312, so whichever lands second will need a trivial conflict resolution there. The two changes are otherwise independent.

@BYVoid
BYVoid marked this pull request as draft August 27, 2026 20:56
@BYVoid
BYVoid force-pushed the claude/libsais-bcr branch from 306075e to 19cb60a Compare August 28, 2026 04:00
@BYVoid
BYVoid marked this pull request as ready for review August 28, 2026 05:07
@BYVoid
BYVoid force-pushed the claude/libsais-bcr branch from 19cb60a to 90839dc Compare August 28, 2026 05:07
@taku910

taku910 commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks for working on this! The Bazel Bzlmod migration looks great and tests pass cleanly.

One minor suggestion for consistency with #1312:
In #1312, $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/darts_clone> was added to target_include_directories in src/CMakeLists.txt.

To keep the CMake targets consistent and adhere to modern target-centric CMake practices, could we also add third_party/libsais to target_include_directories for sentencepiece_train-static (and sentencepiece_train under SPM_ENABLE_SHARED)?

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ca29f81..xxxxxxx 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -217,6 +217,7 @@ target_include_directories(sentencepiece_train-static PUBLIC
   $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
   $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party>
   $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/darts_clone>
+  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/libsais>
   $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
 )
 
@@ -249,6 +250,7 @@ if (SPM_ENABLE_SHARED)
     $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
     $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party>
     $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/darts_clone>
+    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/libsais>
     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
   )

@BYVoid

BYVoid commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I would like to push back gently on this one, and I have not changed the CMake yet — I would rather agree on the scope with you first.

The consistency argument holds for #1312 but not, I think, here, because the two headers sit at different levels:

included from
darts.h model_interface.h, normalizer.h, unigram_model.h — headers
libsais.h unigram_model_trainer.cc only — no header

PUBLIC is PRIVATE plus INTERFACE. The INTERFACE half exists so that a target which includes one of our headers can resolve what that header pulls in. For darts.h that is real: anything in-tree including normalizer.h transitively includes darts.h and needs the directory, so PUBLIC in #1312 is correct. For libsais.h there is nothing to carry — it is reached only from a .cc, so the INTERFACE half would advertise a requirement that no dependent has.

It would not break anything, so if you prefer the uniform spelling I will just add it. But my suggestion is the other direction: replace the directory-scope include_directories() I currently have — which is the older style your comment is steering away from — with a target-scope PRIVATE entry on the targets that actually compile unigram_model_trainer.cc. That gets the target-centric form you are asking for without exporting a path no dependent uses, and keeps one fewer directory on the search path for everything that links the trainer.

Which would you like? I am happy either way; I just did not want to widen the scope silently.

Separately, on the red Windows jobs: those are not caused by this change as such. The libsais archive has a 7-byte VERSION file at its root, Bazel hands a dependency's repository root to dependents as a quote-include, and since MSVC has no -iquote it becomes a plain /I. On a case-insensitive file system #include <version> then resolves to that file instead of the C++ standard header. bazelbuild/bazel-central-registry#10291 publishes 2.10.4.bcr.2, which patches the file away and adds a regression test; once it lands I will bump this PR to that version and the Windows jobs should go green.

UebelAndre pushed a commit to bazelbuild/bazel-central-registry that referenced this pull request Aug 28, 2026
Fixes a Windows-only build break in libsais 2.10.4 and 2.10.4.bcr.1,
reported through a downstream failure in google/sentencepiece#1313:

```
external/libsais+\version(1): error C2059: syntax error: 'constant'
C:\...\MSVC\...\include\bitset(31): error C2143: syntax error: missing ';' before '{'
external/abseil-cpp+\absl/base/casts.h(50): error C2065: 'bit_cast': undeclared identifier
```

The root of the upstream archive contains a 7-byte file named `VERSION`,
whose content is `2.10.4`. Bazel passes the repository root of a
dependency to its dependents as a quote-include directory, and since
MSVC has no `-iquote`, it lands there as a plain `/I` include directory.
On the case-insensitive Windows file system, `#include <version>` then
resolves to that file rather than the C++ standard header, so every
dependent that reaches `<version>` — directly, or through `<bitset>`,
`<utility>` and friends — fails to compile. Only Windows consumers are
affected: on Linux and macOS the directory is passed as `-iquote`, which
angle-bracket includes never search.

This version adds `patches/remove_version_file.patch`, which deletes
that file. Nothing reads it: the CMake build hardcodes the version in
`CMakeLists.txt`, and the Bazel build does not use it either.

To keep this from regressing silently, the overlay adds
`test/version_header_test.cc`, a C++ test that includes `<bitset>` and
`<version>` alongside `libsais.h`. It is part of `@libsais//:all`, so
the existing `verify_targets` and `verify_targets_openmp` tasks run it
on Windows as well.

### Verification

- With the patch, `bazel test @libsais//...` passes against a local
registry, and the fetched repository no longer contains `VERSION`.
- The regression test was checked against the failure mode by compiling
it with the repository root on the plain include path, which is what
MSVC gets: against the unpatched tree it reproduces the reported error
(`version:1:1: error: expected unqualified-id` on the `2.10.4` line),
and against the patched tree it compiles. Windows itself is covered by
presubmit rather than locally.

Everything else — the archive, the four targets, the OpenMP flag — is
unchanged from 2.10.4.bcr.1.

@bazel-io skip_check unstable_url
The Bazel build depends on the libsais module instead of compiling
third_party/libsais/libsais.c. The CMake build is unchanged and still
compiles the vendored copy.

libsais 2.10.4.bcr.2 in the registry is upstream v2.10.4, which is the
same release third_party/libsais is a snapshot of: ignoring comments,
formatting, include order and one clang-format line continuation, the
vendored sources are byte-identical to the upstream tarball, so the
vendored copy carries no local patches. The registry revision patches
out the archive's VERSION file, which MSVC would otherwise pick up for
#include <version> on a case-insensitive file system.

The module leaves OpenMP off by default, matching both libsais' own
CMake option and the //third_party:libsais target this replaces, so the
Bazel build keeps taking the serial path. --@libsais//:openmp turns it
on where the toolchain supports -fopenmp.

libsais.h is now included unqualified, so src/CMakeLists.txt gains
third_party/libsais on its include path to resolve the same spelling
against the vendored copy.
@BYVoid
BYVoid force-pushed the claude/libsais-bcr branch from 90839dc to af9812a Compare August 28, 2026 20:40
@taku910
taku910 merged commit ac0f71d into google:master Aug 31, 2026
36 of 37 checks passed
@BYVoid
BYVoid deleted the claude/libsais-bcr branch August 31, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants