diff options
author | Peter Hawkins <phawkins@google.com> | 2025-01-22 09:21:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-22 06:21:46 -0800 |
commit | f4125e0226e03295f73c4b2dd9cfa540d872d162 (patch) | |
tree | 0e1131bcd5d1b5b47e34bae195e0f3ff27586a7f /mlir/lib/Bindings/Python/IRModule.h | |
parent | 43177b524ee06dfc09cbc357ff277d4f53f5dc15 (diff) | |
download | llvm-f4125e0226e03295f73c4b2dd9cfa540d872d162.zip llvm-f4125e0226e03295f73c4b2dd9cfa540d872d162.tar.gz llvm-f4125e0226e03295f73c4b2dd9cfa540d872d162.tar.bz2 |
[mlir python] Change PyOpView constructor to construct operations. (#123777)
Previously ODS-generated Python operations had code like this:
```
super().__init__(self.build_generic(attributes=attributes, operands=operands, successors=_ods_successors, regions=regions, loc=loc, ip=ip))
```
we change it to:
```
super().__init__(self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS, attributes=attributes, operands=operands, successors=_ods_successors, regions=regions, loc=loc, ip=ip)
```
This:
a) avoids an extra call dispatch (to `build_generic`), and
b) passes the class attributes directly to the constructor. Benchmarks
show that it is faster to pass these as arguments rather than having the
C++ code look up attributes on the class.
This PR improves the timing of the following benchmark on my workstation
from 5.3s to 4.5s:
```
def main(_):
with ir.Context(), ir.Location.unknown():
typ = ir.IntegerType.get_signless(32)
m = ir.Module.create()
with ir.InsertionPoint(m.body):
start = time.time()
for i in range(1000000):
arith.ConstantOp(typ, i)
end = time.time()
print(f"time: {end - start}")
```
Since this change adds an additional overload to the constructor and
does not alter any existing behaviors, it should be backwards
compatible.
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r-- | mlir/lib/Bindings/Python/IRModule.h | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h index d1fb430..2228b55 100644 --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -685,7 +685,7 @@ public: /// Creates an operation. See corresponding python docstring. static nanobind::object - create(const std::string &name, std::optional<std::vector<PyType *>> results, + create(std::string_view name, std::optional<std::vector<PyType *>> results, std::optional<std::vector<PyValue *>> operands, std::optional<nanobind::dict> attributes, std::optional<std::vector<PyBlock *>> successors, int regions, @@ -739,12 +739,16 @@ public: nanobind::object getOperationObject() { return operationObject; } - static nanobind::object buildGeneric( - const nanobind::object &cls, std::optional<nanobind::list> resultTypeList, - nanobind::list operandList, std::optional<nanobind::dict> attributes, - std::optional<std::vector<PyBlock *>> successors, - std::optional<int> regions, DefaultingPyLocation location, - const nanobind::object &maybeIp); + static nanobind::object + buildGeneric(std::string_view name, std::tuple<int, bool> opRegionSpec, + nanobind::object operandSegmentSpecObj, + nanobind::object resultSegmentSpecObj, + std::optional<nanobind::list> resultTypeList, + nanobind::list operandList, + std::optional<nanobind::dict> attributes, + std::optional<std::vector<PyBlock *>> successors, + std::optional<int> regions, DefaultingPyLocation location, + const nanobind::object &maybeIp); /// Construct an instance of a class deriving from OpView, bypassing its /// `__init__` method. The derived class will typically define a constructor |