Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 556b741

Browse files
authored
[mlir] Add isStatic* size check for ShapedTypes. NFCI. (#147085)
The motivation is to avoid having to negate `isDynamic*` checks, avoid double negations, and allow for `ShapedType::isStaticDim` to be used in ADT functions without having to wrap it in a lambda performing the negation. Also add the new functions to C and Python bindings.
1 parent 9b02b80 commit 556b741

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

mlir/include/mlir-c/BuiltinTypes.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,12 @@ MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetRank(MlirType type);
289289
/// Checks whether the given shaped type has a static shape.
290290
MLIR_CAPI_EXPORTED bool mlirShapedTypeHasStaticShape(MlirType type);
291291

292-
/// Checks wither the dim-th dimension of the given shaped type is dynamic.
292+
/// Checks whether the dim-th dimension of the given shaped type is dynamic.
293293
MLIR_CAPI_EXPORTED bool mlirShapedTypeIsDynamicDim(MlirType type, intptr_t dim);
294294

295+
/// Checks whether the dim-th dimension of the given shaped type is static.
296+
MLIR_CAPI_EXPORTED bool mlirShapedTypeIsStaticDim(MlirType type, intptr_t dim);
297+
295298
/// Returns the dim-th dimension of the given ranked shaped type.
296299
MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetDimSize(MlirType type,
297300
intptr_t dim);
@@ -300,17 +303,25 @@ MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetDimSize(MlirType type,
300303
/// in shaped types.
301304
MLIR_CAPI_EXPORTED bool mlirShapedTypeIsDynamicSize(int64_t size);
302305

306+
/// Checks whether the given shaped type dimension value is statically-sized.
307+
MLIR_CAPI_EXPORTED bool mlirShapedTypeIsStaticSize(int64_t size);
308+
303309
/// Returns the value indicating a dynamic size in a shaped type. Prefer
304-
/// mlirShapedTypeIsDynamicSize to direct comparisons with this value.
310+
/// mlirShapedTypeIsDynamicSize and mlirShapedTypeIsStaticSize to direct
311+
/// comparisons with this value.
305312
MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetDynamicSize(void);
306313

307314
/// Checks whether the given value is used as a placeholder for dynamic strides
308315
/// and offsets in shaped types.
309316
MLIR_CAPI_EXPORTED bool mlirShapedTypeIsDynamicStrideOrOffset(int64_t val);
310317

318+
/// Checks whether the given dimension value of a stride or an offset is
319+
/// statically-sized.
320+
MLIR_CAPI_EXPORTED bool mlirShapedTypeIsStaticStrideOrOffset(int64_t val);
321+
311322
/// Returns the value indicating a dynamic stride or offset in a shaped type.
312-
/// Prefer mlirShapedTypeGetDynamicStrideOrOffset to direct comparisons with
313-
/// this value.
323+
/// Prefer mlirShapedTypeIsDynamicStrideOrOffset and
324+
/// mlirShapedTypeIsStaticStrideOrOffset to direct comparisons with this value.
314325
MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetDynamicStrideOrOffset(void);
315326

316327
//===----------------------------------------------------------------------===//

mlir/lib/Bindings/Python/IRTypes.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,15 @@ void mlir::PyShapedType::bindDerived(ClassTy &c) {
544544
nb::arg("dim"),
545545
"Returns whether the dim-th dimension of the given shaped type is "
546546
"dynamic.");
547+
c.def(
548+
"is_static_dim",
549+
[](PyShapedType &self, intptr_t dim) -> bool {
550+
self.requireHasRank();
551+
return mlirShapedTypeIsStaticDim(self, dim);
552+
},
553+
nb::arg("dim"),
554+
"Returns whether the dim-th dimension of the given shaped type is "
555+
"static.");
547556
c.def(
548557
"get_dim_size",
549558
[](PyShapedType &self, intptr_t dim) {
@@ -558,6 +567,12 @@ void mlir::PyShapedType::bindDerived(ClassTy &c) {
558567
nb::arg("dim_size"),
559568
"Returns whether the given dimension size indicates a dynamic "
560569
"dimension.");
570+
c.def_static(
571+
"is_static_size",
572+
[](int64_t size) -> bool { return mlirShapedTypeIsStaticSize(size); },
573+
nb::arg("dim_size"),
574+
"Returns whether the given dimension size indicates a static "
575+
"dimension.");
561576
c.def(
562577
"is_dynamic_stride_or_offset",
563578
[](PyShapedType &self, int64_t val) -> bool {
@@ -567,6 +582,15 @@ void mlir::PyShapedType::bindDerived(ClassTy &c) {
567582
nb::arg("dim_size"),
568583
"Returns whether the given value is used as a placeholder for dynamic "
569584
"strides and offsets in shaped types.");
585+
c.def(
586+
"is_static_stride_or_offset",
587+
[](PyShapedType &self, int64_t val) -> bool {
588+
self.requireHasRank();
589+
return mlirShapedTypeIsStaticStrideOrOffset(val);
590+
},
591+
nb::arg("dim_size"),
592+
"Returns whether the given shaped type stride or offset value is "
593+
"statically-sized.");
570594
c.def_prop_ro(
571595
"shape",
572596
[](PyShapedType &self) {

mlir/lib/CAPI/IR/BuiltinTypes.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ bool mlirShapedTypeIsDynamicDim(MlirType type, intptr_t dim) {
332332
.isDynamicDim(static_cast<unsigned>(dim));
333333
}
334334

335+
bool mlirShapedTypeIsStaticDim(MlirType type, intptr_t dim) {
336+
return llvm::cast<ShapedType>(unwrap(type))
337+
.isStaticDim(static_cast<unsigned>(dim));
338+
}
339+
335340
int64_t mlirShapedTypeGetDimSize(MlirType type, intptr_t dim) {
336341
return llvm::cast<ShapedType>(unwrap(type))
337342
.getDimSize(static_cast<unsigned>(dim));
@@ -343,10 +348,18 @@ bool mlirShapedTypeIsDynamicSize(int64_t size) {
343348
return ShapedType::isDynamic(size);
344349
}
345350

351+
bool mlirShapedTypeIsStaticSize(int64_t size) {
352+
return ShapedType::isStatic(size);
353+
}
354+
346355
bool mlirShapedTypeIsDynamicStrideOrOffset(int64_t val) {
347356
return ShapedType::isDynamic(val);
348357
}
349358

359+
bool mlirShapedTypeIsStaticStrideOrOffset(int64_t val) {
360+
return ShapedType::isStatic(val);
361+
}
362+
350363
int64_t mlirShapedTypeGetDynamicStrideOrOffset() {
351364
return ShapedType::kDynamic;
352365
}

mlir/python/mlir/_mlir_libs/_mlir/ir.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,11 @@ class ShapedType(Type):
24972497
Returns whether the given dimension size indicates a dynamic dimension.
24982498
"""
24992499
@staticmethod
2500+
def is_static_size(dim_size: int) -> bool:
2501+
"""
2502+
Returns whether the given dimension size indicates a static dimension.
2503+
"""
2504+
@staticmethod
25002505
def isinstance(other: Type) -> bool: ...
25012506
def __init__(self, cast_from_type: Type) -> None: ...
25022507
def get_dim_size(self, dim: int) -> int:
@@ -2507,10 +2512,18 @@ class ShapedType(Type):
25072512
"""
25082513
Returns whether the dim-th dimension of the given shaped type is dynamic.
25092514
"""
2515+
def is_static_dim(self, dim: int) -> bool:
2516+
"""
2517+
Returns whether the dim-th dimension of the given shaped type is static.
2518+
"""
25102519
def is_dynamic_stride_or_offset(self, dim_size: int) -> bool:
25112520
"""
25122521
Returns whether the given value is used as a placeholder for dynamic strides and offsets in shaped types.
25132522
"""
2523+
def is_static_stride_or_offset(self, dim_size: int) -> bool:
2524+
"""
2525+
Returns whether the given shaped type stride or offset value is statically-sized.
2526+
"""
25142527
@property
25152528
def element_type(self) -> Type:
25162529
"""

0 commit comments

Comments
 (0)