Skip to content

Commit 1a84aa1

Browse files
authored
Make wheel usable from Python and register dialects (#5)
These dialects appear in Wave output and passes are useful to simplify IR before processing or printing.
1 parent 27d9b8c commit 1a84aa1

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ A Python wheel containing the `water-opt` binary can be produced using the
5959
following commands:
6060

6161
```sh
62-
cd build_tools
62+
cd build_tools/wheel
6363
WATER_MLIR_DIR=$BUILD_DIR/lib/cmake/mlir python -m pip wheel .
6464
```
6565

File renamed without changes.

build_tools/setup.py renamed to build_tools/wheel/setup.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# See https://llvm.org/LICENSE.txt for license information.
55
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66

7-
from setuptools import setup, Extension
7+
from setuptools import setup, Extension, find_packages
88
import subprocess
99
import os
1010
import shutil
@@ -43,8 +43,10 @@ def prepare_installation():
4343
"-DBUILD_SHARED_LIBS=Off",
4444
f"-DCMAKE_BUILD_TYPE={build_type}",
4545
]
46-
source_dir = os.path.dirname(SETUPPY_DIR)
47-
os.makedirs(BINARY_DIR, exist_ok=True)
46+
source_dir = os.path.dirname(os.path.dirname(SETUPPY_DIR))
47+
if os.path.exists(BINARY_DIR):
48+
shutil.rmtree(BINARY_DIR)
49+
os.makedirs(BINARY_DIR)
4850
subprocess.check_call(["cmake", source_dir, *cmake_args], cwd=BINARY_DIR)
4951
subprocess.check_call(
5052
["cmake", "--build", ".", "--target", "water-opt"], cwd=BINARY_DIR
@@ -55,12 +57,13 @@ class CMakeBuildPy(_build_py):
5557
"""Pretends to be building but actually just copies pre-built binaries."""
5658

5759
def run(self):
58-
target_dir = os.path.abspath(self.build_lib)
60+
target_dir = os.path.join(os.path.abspath(self.build_lib), "water_mlir")
5961
print(f"Building in target dir: {target_dir}", file=sys.stderr)
6062
if os.path.exists(target_dir):
6163
shutil.rmtree(target_dir)
6264
os.makedirs(target_dir)
6365
shutil.copy(os.path.join(BINARY_DIR, "bin", "water-opt"), target_dir)
66+
shutil.copy(os.path.join(SETUPPY_DIR, "tools", "binaries.py"), target_dir)
6467

6568

6669
# This is needed to create at least some binary understood by Python.
@@ -107,7 +110,8 @@ def run(self):
107110
setup(
108111
name="water_mlir",
109112
version="0.1.0",
110-
ext_modules=[CMakeExtension("water_mlir")],
113+
packages=find_packages(include=["tools"]),
114+
ext_modules=[CMakeExtension("water_mlir_ext")],
111115
cmdclass={
112116
"build": CustomBuild,
113117
"built_ext": NoopBuildExtension,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright 2025 The Water Authors
2+
#
3+
# Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 The Water Authors
2+
#
3+
# Licensed under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
7+
import os
8+
9+
10+
def find_binary(name: str) -> str:
11+
this_path = os.path.dirname(__file__)
12+
tool_path = os.path.join(this_path, name)
13+
assert os.path.isfile(tool_path) and os.access(
14+
tool_path, os.X_OK
15+
), f"Could not find the {name} executable in package."
16+
return tool_path

water-opt/water-opt.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "mlir/Conversion/Passes.h"
10+
#include "mlir/Dialect/Arith/Transforms/Passes.h"
911
#include "mlir/IR/MLIRContext.h"
1012
#include "mlir/InitAllDialects.h"
1113
#include "mlir/InitAllPasses.h"
1214
#include "mlir/Support/FileUtilities.h"
1315
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
1416

17+
#include "mlir/Transforms/Passes.h"
1518
#include "water/Transforms/Passes.h"
1619

1720
// Forward-declare test passes so we don't have a dependency on the test
@@ -24,9 +27,17 @@ int main(int argc, char **argv) {
2427
mlir::water::registerPasses();
2528
mlir::water::test::registerAllPasses();
2629

30+
mlir::arith::registerArithIntRangeOptsPass();
31+
mlir::registerCanonicalizerPass();
32+
mlir::registerCSEPass();
33+
mlir::registerLoopInvariantCodeMotionPass();
34+
mlir::registerLowerAffinePass();
35+
2736
mlir::DialectRegistry registry;
28-
registry.insert<mlir::arith::ArithDialect, mlir::cf::ControlFlowDialect,
29-
mlir::func::FuncDialect, mlir::memref::MemRefDialect,
37+
registry.insert<mlir::affine::AffineDialect, mlir::amdgpu::AMDGPUDialect,
38+
mlir::arith::ArithDialect, mlir::cf::ControlFlowDialect,
39+
mlir::func::FuncDialect, mlir::gpu::GPUDialect,
40+
mlir::LLVM::LLVMDialect, mlir::memref::MemRefDialect,
3041
mlir::scf::SCFDialect, mlir::vector::VectorDialect>();
3142

3243
return mlir::asMainReturnCode(

0 commit comments

Comments
 (0)