Skip to content

Commit 11568f0

Browse files
Merge pull request #2823 from j2kun:pypi-build-size
PiperOrigin-RevId: 891705622
2 parents 2878ccc + d9e72d0 commit 11568f0

4 files changed

Lines changed: 65 additions & 9 deletions

File tree

frontend/heir/entrypoints.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Entrypoints for shimming heir-opt and heir-translate in the Python path"""
2+
3+
import os
4+
from pathlib import Path
5+
import sys
6+
7+
8+
def _run_binary(binary_name):
9+
"""Locate and execute a bundled binary."""
10+
# This file is at frontend/heir/entrypoints.py
11+
# The binaries are installed in the 'heir' package directory (frontend/heir/)
12+
binary_path = Path(__file__).parent / binary_name
13+
if not binary_path.exists():
14+
# Fallback for some environments where binaries might be in the parent
15+
binary_path = Path(__file__).parent.parent / binary_name
16+
17+
if not binary_path.exists():
18+
print(
19+
f"Error: {binary_name} not found in package at {binary_path}",
20+
file=sys.stderr,
21+
)
22+
sys.exit(1)
23+
24+
# os.execv replaces the current process with the binary
25+
os.execv(binary_path, [str(binary_path)] + sys.argv[1:])
26+
27+
28+
def run_heir_opt():
29+
_run_binary("heir-opt")
30+
31+
32+
def run_heir_translate():
33+
_run_binary("heir-translate")

lib/Pipelines/BooleanPipelineRegistration.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lib/Dialect/Debug/Transforms/ValidateNames.h"
1414
#include "lib/Dialect/Secret/Conversions/SecretToCGGI/SecretToCGGI.h"
1515
#include "lib/Dialect/Secret/Transforms/DistributeGeneric.h"
16+
#include "lib/Pipelines/PipelineRegistration.h"
1617
#include "lib/Transforms/BooleanVectorizer/BooleanVectorizer.h"
1718
#include "lib/Transforms/FoldConstantTensors/FoldConstantTensors.h"
1819
#include "lib/Transforms/ForwardInsertToExtract/ForwardInsertToExtract.h"
@@ -22,6 +23,7 @@
2223
#include "lib/Transforms/MemrefToArith/MemrefToArith.h"
2324
#include "lib/Transforms/Secretize/Passes.h"
2425
#include "lib/Transforms/TensorLinalgToAffineLoops/TensorLinalgToAffineLoops.h"
26+
#include "lib/Transforms/UnusedMemRef/UnusedMemRef.h"
2527
#include "llvm/include/llvm/ADT/SmallVector.h" // from @llvm-project
2628
#include "mlir/include/mlir/Conversion/TensorToLinalg/TensorToLinalgPass.h" // from @llvm-project
2729
#include "mlir/include/mlir/Dialect/Affine/Transforms/Passes.h" // from @llvm-project

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ dev = [
5959
Homepage = "https://heir.dev"
6060
Issues = "https://github.com/google/heir/issues"
6161

62+
[project.scripts]
63+
heir-opt = "heir.entrypoints:run_heir_opt"
64+
heir-translate = "heir.entrypoints:run_heir_translate"
65+
6266
[tool.setuptools]
6367
package-dir = { "" = "frontend" }
6468
zip-safe = false

setup.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(
101101
target_file: str,
102102
is_binary: bool = False,
103103
copy_include_files: bool = False,
104+
aggressive_strip: bool = False,
104105
**kwargs: Any,
105106
):
106107
super().__init__(name=name, sources=[], **kwargs)
@@ -115,6 +116,11 @@ def __init__(
115116
self.is_binary = is_binary
116117
self.copy_include_files = copy_include_files
117118

119+
# Determines whether to `strip-all` when building a target for PyPI.
120+
# Critically, this should only be used for cc_binary targets that are run as
121+
# a subprocess, not cc_library targets that are loaded into Python.
122+
self.aggressive_strip = aggressive_strip
123+
118124

119125
class BuildBazelExtension(build_ext.build_ext):
120126
"""A command that runs Bazel to build a C/C++ extension."""
@@ -226,10 +232,17 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
226232
"--ui_event_filters=ERROR",
227233
f"--symlink_prefix={temp_path / 'bazel-'}",
228234
"--compilation_mode=opt",
235+
"--strip=always",
229236
f"--cxxopt={'/std:c++20' if IS_WINDOWS else '-std=c++20'}",
230237
f"--@rules_python//python/config_settings:python_version={python_version}",
231238
]
232239

240+
if ext.aggressive_strip:
241+
if IS_LINUX:
242+
bazel_argv.append("--stripopt=--strip-all")
243+
elif IS_MAC:
244+
bazel_argv.append("--stripopt=-S")
245+
233246
if is_cibuildwheel() and IS_LINUX:
234247
# TODO(#2249): OpenMP is disabled for manylinux, as libomp is not on the allowed libraries list,
235248
# and statically bundling OpenMP is non-trivial and left as future work.
@@ -288,9 +301,10 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
288301
| stat.S_IXOTH,
289302
)
290303

291-
# Also copy binaries to project root so they can be included in data_files
304+
# Also copy binaries to project root so they are visible when running from
305+
# Python as a subprocess.
292306
root_path = Path(ext.target_file)
293-
print(f"Copying {srcdir_path} to {root_path} for data_files")
307+
print(f"Copying {srcdir_path} to {root_path}")
294308
shutil.copyfile(srcdir_path, root_path)
295309
os.chmod(
296310
root_path,
@@ -313,27 +327,30 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
313327
ext_modules=[
314328
BazelExtension(
315329
name="heir_py._heir_opt",
316-
bazel_target="//tools:heir-opt",
317-
generated_so_file=Path("tools") / "heir-opt",
330+
bazel_target="//tools:heir-opt.stripped",
331+
generated_so_file=Path("tools") / "heir-opt.stripped",
318332
target_file="heir-opt",
319333
py_limited_api=py_limited_api,
320334
is_binary=True,
335+
aggressive_strip=is_cibuildwheel(),
321336
),
322337
BazelExtension(
323338
name="heir_py._heir_translate",
324-
bazel_target="//tools:heir-translate",
325-
generated_so_file=Path("tools") / "heir-translate",
339+
bazel_target="//tools:heir-translate.stripped",
340+
generated_so_file=Path("tools") / "heir-translate.stripped",
326341
target_file="heir-translate",
327342
py_limited_api=py_limited_api,
328343
is_binary=True,
344+
aggressive_strip=is_cibuildwheel(),
329345
),
330346
BazelExtension(
331347
name="heir_py._abc",
332-
bazel_target="@abc//:abc_bin",
333-
generated_so_file=Path("external") / "abc+" / "abc_bin",
348+
bazel_target="@abc//:abc_bin.stripped",
349+
generated_so_file=Path("external") / "abc+" / "abc_bin.stripped",
334350
target_file="abc_bin",
335351
py_limited_api=py_limited_api,
336352
is_binary=True,
353+
aggressive_strip=is_cibuildwheel(),
337354
),
338355
BazelExtension(
339356
name="heir_py._libopenfhe",
@@ -342,8 +359,8 @@ def bazel_build(self, ext: BazelExtension) -> None: # noqa: C901
342359
target_file="libopenfhe.so",
343360
py_limited_api=py_limited_api,
344361
copy_include_files=True,
362+
aggressive_strip=False,
345363
),
346364
],
347-
data_files=[("bin", ["heir-opt", "heir-translate"])],
348365
options=options,
349366
)

0 commit comments

Comments
 (0)