Skip to content

Commit 72f15f3

Browse files
committed
[MLIR][Python] enable precise registration
1 parent cac0635 commit 72f15f3

38 files changed

+546
-24
lines changed

mlir/examples/standalone/pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ cmake.source-dir = "."
3737
# This is for installing/distributing the python bindings target and only the python bindings target.
3838
build.targets = ["StandalonePythonModules"]
3939
install.components = ["StandalonePythonModules"]
40+
# The default is true but make it explicit to highlight that this option exists (turn off for debug symbols).
41+
install.strip = true
4042

4143
[tool.scikit-build.cmake.define]
4244
# Optional
@@ -51,6 +53,9 @@ LLVM_USE_LINKER = { env = "LLVM_USE_LINKER", default = "" }
5153
CMAKE_VISIBILITY_INLINES_HIDDEN = "ON"
5254
CMAKE_C_VISIBILITY_PRESET = "hidden"
5355
CMAKE_CXX_VISIBILITY_PRESET = "hidden"
56+
# Disables generation of "version soname" (i.e. libFoo.so.<version>),
57+
# which causes pure duplication of various shlibs for Python wheels.
58+
CMAKE_PLATFORM_NO_VERSIONED_SONAME = "ON"
5459

5560
# Non-optional (alternatively you could use CMAKE_PREFIX_PATH here).
5661
MLIR_DIR = { env = "MLIR_DIR", default = "" }

mlir/examples/standalone/python/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ add_mlir_python_common_capi_library(StandalonePythonCAPI
6565
DECLARED_SOURCES
6666
StandalonePythonSources
6767
MLIRPythonSources.Core
68-
MLIRPythonSources.Dialects.builtin
68+
EMBED_LIBS
69+
MLIRCAPIQuant
6970
)
7071

7172
################################################################################
@@ -138,6 +139,8 @@ set(_declared_sources
138139
StandalonePythonSources
139140
MLIRPythonSources.Core
140141
MLIRPythonSources.Dialects.builtin
142+
MLIRPythonSources.Dialects.arith
143+
MLIRPythonSources.Dialects.quant
141144
)
142145
# For an external projects build, the MLIRPythonExtension.Core.type_stub_gen
143146
# target already exists and can just be added to DECLARED_SOURCES.

mlir/examples/standalone/python/StandaloneExtensionNanobind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ NB_MODULE(_standaloneDialectsNanobind, m) {
3232
mlirDialectHandleRegisterDialect(standaloneHandle, context);
3333
if (load) {
3434
mlirDialectHandleLoadDialect(arithHandle, context);
35-
mlirDialectHandleRegisterDialect(standaloneHandle, context);
35+
mlirDialectHandleLoadDialect(standaloneHandle, context);
3636
}
3737
},
3838
nb::arg("context").none() = nb::none(), nb::arg("load") = true,

mlir/examples/standalone/python/StandaloneExtensionPybind11.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PYBIND11_MODULE(_standaloneDialectsPybind11, m) {
3131
mlirDialectHandleRegisterDialect(standaloneHandle, context);
3232
if (load) {
3333
mlirDialectHandleLoadDialect(arithHandle, context);
34-
mlirDialectHandleRegisterDialect(standaloneHandle, context);
34+
mlirDialectHandleLoadDialect(standaloneHandle, context);
3535
}
3636
},
3737
py::arg("context") = py::none(), py::arg("load") = true);

mlir/examples/standalone/python/mlir_standalone/dialects/standalone_nanobind.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44

55
from ._standalone_ops_gen import *
66
from .._mlir_libs._standaloneDialectsNanobind.standalone import *
7+
8+
from .._mlir_libs import get_dialect_registry as _get_dialect_registry
9+
from .._mlir_libs._capi import register_dialect as _register_dialect
10+
11+
_dialect_registry = _get_dialect_registry()
12+
if "mlirGetDialectHandle__quant__" not in _dialect_registry.dialect_names:
13+
_register_dialect("mlirGetDialectHandle__quant__", _dialect_registry)

mlir/examples/standalone/python/mlir_standalone/dialects/standalone_pybind11.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@
44

55
from ._standalone_ops_gen import *
66
from .._mlir_libs._standaloneDialectsPybind11.standalone import *
7+
8+
from .._mlir_libs import get_dialect_registry as _get_dialect_registry
9+
from .._mlir_libs._capi import register_dialect as _register_dialect
10+
11+
_dialect_registry = _get_dialect_registry()
12+
if "mlirGetDialectHandle__quant__" not in _dialect_registry.dialect_names:
13+
_register_dialect("mlirGetDialectHandle__quant__", _dialect_registry)

mlir/examples/standalone/test/python/smoketest.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,26 @@
1111
else:
1212
raise ValueError("Expected either pybind11 or nanobind as arguments")
1313

14+
from mlir_standalone.dialects import arith, quant
1415

15-
with Context():
16+
17+
with Context(), Location.unknown():
1618
standalone_d.register_dialects()
17-
module = Module.parse(
18-
"""
19-
%0 = arith.constant 2 : i32
20-
%1 = standalone.foo %0 : i32
21-
"""
19+
f32 = F32Type.get()
20+
i8 = IntegerType.get_signless(8)
21+
i32 = IntegerType.get_signless(32)
22+
uniform = quant.UniformQuantizedType.get(
23+
quant.UniformQuantizedType.FLAG_SIGNED, i8, f32, 0.99872, 127, -8, 7
2224
)
23-
# CHECK: %[[C:.*]] = arith.constant 2 : i32
24-
# CHECK: standalone.foo %[[C]] : i32
25+
26+
module = Module.create()
27+
with InsertionPoint(module.body):
28+
two_i32 = arith.constant(i32, 2)
29+
standalone_d.foo(two_i32)
30+
two_f32 = arith.constant(f32, 2.0)
31+
quant.qcast(uniform, two_f32)
32+
# CHECK: %[[TWOI32:.*]] = arith.constant 2 : i32
33+
# CHECK: standalone.foo %[[TWOI32]] : i32
34+
# CHECK: %[[TWOF32:.*]] = arith.constant 2.000000e+00 : f32
35+
# CHECK: quant.qcast %[[TWOF32]] : f32 to !quant.uniform<i8<-8:7>:f32, 9.987200e-01:127>
2536
print(str(module))

mlir/include/mlir-c/Bindings/Python/Interop.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
#define MLIR_PYTHON_CAPSULE_VALUE MAKE_MLIR_PYTHON_QUALNAME("ir.Value._CAPIPtr")
8585
#define MLIR_PYTHON_CAPSULE_TYPEID \
8686
MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID._CAPIPtr")
87+
#define MLIR_PYTHON_CAPSULE_DIALECT_HANDLE \
88+
MAKE_MLIR_PYTHON_QUALNAME("ir.DialectHandle._CAPIPtr")
8789

8890
/** Attribute on MLIR Python objects that expose their C-API pointer.
8991
* This will be a type-specific capsule created as per one of the helpers
@@ -457,6 +459,13 @@ static inline MlirValue mlirPythonCapsuleToValue(PyObject *capsule) {
457459
return value;
458460
}
459461

462+
static inline MlirDialectHandle
463+
mlirPythonCapsuleToDialectHandle(PyObject *capsule) {
464+
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_DIALECT_HANDLE);
465+
MlirDialectHandle handle = {ptr};
466+
return handle;
467+
}
468+
460469
#ifdef __cplusplus
461470
}
462471
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- mlir-c/Dialect/Affine.h - C API for Affine dialect --------*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This header declares the C interface for registering and accessing the
11+
// Affine dialect. A dialect should be registered with a context to make it
12+
// available to users of the context. These users must load the dialect
13+
// before using any of its attributes, operations or types. Parser and pass
14+
// manager can load registered dialects automatically.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef MLIR_C_DIALECT_AFFINE_H
19+
#define MLIR_C_DIALECT_AFFINE_H
20+
21+
#include "mlir-c/IR.h"
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Affine, affine);
28+
29+
MLIR_CAPI_EXPORTED void
30+
mlirAffineRegisterTransformDialectExtension(MlirDialectRegistry registry);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif // MLIR_C_DIALECT_AFFINE_H
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- mlir-c/Dialect/Bufferization.h - C API for Bufferization dialect --===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This header declares the C interface for registering and accessing the
11+
// Bufferization dialect. A dialect should be registered with a context to make
12+
// it available to users of the context. These users must load the dialect
13+
// before using any of its attributes, operations or types. Parser and pass
14+
// manager can load registered dialects automatically.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef MLIR_C_DIALECT_BUFFERIZATION_H
19+
#define MLIR_C_DIALECT_BUFFERIZATION_H
20+
21+
#include "mlir-c/IR.h"
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Bufferization, bufferization);
28+
29+
MLIR_CAPI_EXPORTED void mlirBufferizationRegisterTransformDialectExtension(
30+
MlirDialectRegistry registry);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif // MLIR_C_DIALECT_BUFFERIZATION_H

0 commit comments

Comments
 (0)