From 758b6e11cd2ce44368b37e28d1254db766d9d9f3 Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Tue, 19 Nov 2024 16:04:25 -0800 Subject: [PATCH] Set -O3 when building python wheels in opt mode. See https://github.com/sgkit-dev/vcf-zarr-publication/issues/161 The default bazel -c opt build mode is -O2, vs. the default cmake Release optimization mode of -O3. It turns out that gcc doesn't unroll the underlying blosc loop at -O3, so this sets the level to -O3 when building wheels. PiperOrigin-RevId: 698176524 Change-Id: Iea9c47e1cb96d40169d2b28af93cf53b33e6991c --- setup.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 291c1f690..b4c939aee 100644 --- a/setup.py +++ b/setup.py @@ -234,9 +234,6 @@ def run(self): bazelisk = os.getenv('TENSORSTORE_BAZELISK', 'bazelisk.py') # Controlled via `setup.py build_ext --debug` flag. default_compilation_mode = 'dbg' if self.debug else 'opt' - compilation_mode = os.getenv( - 'TENSORSTORE_BAZEL_COMPILATION_MODE', default_compilation_mode - ) startup_options = shlex.split( os.getenv('TENSORSTORE_BAZEL_STARTUP_OPTIONS', '') ) @@ -244,13 +241,24 @@ def run(self): os.getenv('TENSORSTORE_BAZEL_BUILD_OPTIONS', '') ) + # Build with a specific compilation mode. + # When in opt mode also set -O3 optimizations to override bazel -O2. + compilation_mode = os.getenv( + 'TENSORSTORE_BAZEL_COMPILATION_MODE', default_compilation_mode + ) + build_flags = ['build', '-c', compilation_mode] + if compilation_mode == 'opt': + if 'win32' in sys.platform: + # Assumes MSVC compiler. + build_flags.append('--copt=/Ox') + else: + build_flags.append('--copt=-O3') + build_command = ( [sys.executable, '-u', bazelisk] + startup_options + + build_flags + [ - 'build', - '-c', - compilation_mode, '//python/tensorstore:_tensorstore__shared_objects', '--verbose_failures', # Bazel does not seem to download these files by default when