From 11c4fc6c4844f9bea694b96b84ad07bde9d39e19 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Sun, 10 May 2020 14:14:10 +0200 Subject: [PATCH] [mlir] Affine dim and symbol checkers: support detached regions Functions checking whether an SSA value is a valid dimension or symbol for affine operations can be called on values defined in a detached region (a region that is not yet attached to an operation), for example, during parsing or operation construction. These functions will attempt to uncondtionally dereference a pointer to the parent operation of a region, which may be null (as fixed by the previous commit, uninitialized before that). Since one cannot know to which operation a region will be attached, conservatively this operation would not be a valid affine scope and act accordingly, instead of crashing. --- mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp index a07c3fa..1766728 100644 --- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp +++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp @@ -88,9 +88,12 @@ Operation *AffineDialect::materializeConstant(OpBuilder &builder, /// op with trait `AffineScope`. A value of index type defined at the top /// level is always a valid symbol. bool mlir::isTopLevelValue(Value value) { - if (auto arg = value.dyn_cast()) - return arg.getOwner()->getParentOp()->hasTrait(); - return value.getDefiningOp()->getParentOp()->hasTrait(); + if (auto arg = value.dyn_cast()) { + Operation *parentOp = arg.getOwner()->getParentOp(); + return parentOp && parentOp->hasTrait(); + } + Operation *parentOp = value.getDefiningOp()->getParentOp(); + return parentOp && parentOp->hasTrait(); } /// A utility function to check if a value is defined at the top level of @@ -132,8 +135,9 @@ bool mlir::isValidDim(Value value) { // This value has to be a block argument for an op that has the // `AffineScope` trait or for an affine.for or affine.parallel. auto *parentOp = value.cast().getOwner()->getParentOp(); - return parentOp->hasTrait() || - isa(parentOp) || isa(parentOp); + return parentOp && + (parentOp->hasTrait() || + isa(parentOp) || isa(parentOp)); } // Value can be used as a dimension id iff it meets one of the following -- 2.7.4