This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Support slicing for operands in results in Python bindings
ClosedPublic

Authored by ftynse on Nov 6 2020, 6:13 AM.

Details

Summary

Slicing, that is element access with [being:end:step] structure, is
a common Python idiom for sequence-like containers. It is also necessary
to support custom accessor for operations with variadic operands and
results (an operation an return a slice of its operands that correspond
to the given variadic group).

Add generic utility to support slicing in Python bindings and use it
for operation operands and results.

Depends On D90923

Diff Detail

Event Timeline

ftynse created this revision.Nov 6 2020, 6:13 AM
ftynse requested review of this revision.Nov 6 2020, 6:13 AM
This revision is now accepted and ready to land.Nov 6 2020, 8:00 AM
mehdi_amini added inline comments.Nov 6 2020, 9:14 AM
mlir/lib/Bindings/Python/PybindUtils.h
231

Can negative index be larger than the length?
(i.e. should this be a while loop?)

253

I'm puzzled by the behavior here, it seems like taking a slice of a slice wouldn't be intuitive: IIUC the slicing would be defined in terms of the underlying container (you could "widen" the slice even?)

ftynse added inline comments.Nov 9 2020, 6:34 AM
mlir/lib/Bindings/Python/PybindUtils.h
231

Standard Python arrays don't loop, so I don't think we should either.

253

I tried to replicate the behavior of Python array slicing. Note that slice.compute (or, equivalently, PySlice_GetIndicesEx) returns positive start/stop values, and truncates length accordingly (dunderLen() returns the length of the current slice). So I don't see how we can widen the slice here: the start index and step are monotonously non-decreasing, the length is monotonously non-decreasing provided slice.compute works as its documentation specifies. We test negative-start slices as well as negative indices and slice chains, and everything seems to work the same way array slicing does.

mehdi_amini added inline comments.Nov 9 2020, 8:04 PM
mlir/lib/Bindings/Python/PybindUtils.h
253

OK!
Seems like I got confused by getNumElements returning the number of element in the container instead of the slice length.

Some of the check above aren't obvious to me though, linearIndex >= static_cast<Derived *>(this)->getNumElements()) on indexing for example looks like something that can be caught on slice creation instead.

mehdi_amini accepted this revision.Nov 9 2020, 8:04 PM
This revision was landed with ongoing or failed builds.Nov 10 2020, 1:46 AM
This revision was automatically updated to reflect the committed changes.