llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Callback builders in `scf` do not allow for roll back in rewrite engine

Open frgossen opened this issue 6 years ago • 3 comments

Bugzilla Link 46731
Resolution FIXED
Resolved on Jul 21, 2020 20:50
Version unspecified
OS All
CC @joker-eph,@jpienaar,@River707
Fixed by commit(s) b98f414a04e19202669a4273e620bc12b5054413

Extended Description

When using the callback builders in scf potential rollbacks of conversion patterns may fail. This problem occurs when values are used within the callback that exist only in the OperationState at that point.

For example, the scf.for loop creates its body region, body block, and induction variable in the operation state. When the induction variable is then used within the body, its using operation is created in the actual builder (the ConversionPatternRewriter in our case). The embracing loop (including the induction variable) only exists in the operation state at this point. Later, the operation state is used to build the actual scf.for operation, hence creating the loop after its body. (See mlir/lib/Dialect/SCF/SCF.cpp:74)

MLIR does support the creation of nested regions/blocks before embracing ones. It ensures that these cases are dealt with during rollback. (See mlir/lib/Transforms/DialectConversion.cpp:730) When a value's use is created before its declaration, however, the rollback mechanism fails.

In these cases, the rollback will erase newly created operations in reverse order to ensure that uses are erased before declarations. (See mlir/lib/Transforms/DialectConversion.cpp:826) The nesting structure is detached to avoid double-frees, the value usage structure, however, remains. (See mlir/lib/Transforms/DialectConversion.cpp:730) The mechanism eventually fails when asserting that a value has no uses when it is deleted. (See mlir/include/mlir/IR/UseDefLists.h:37) Removing this assertion does not help.

I am not sure if this is an incorrect use of the MLIR infrastructure by the callback builders in scf or if it is an avoidable limitation of the MLIR rollback mechanism.

Small reproducing example (See https://github.com/llvm/llvm-project/blob/00e3a1ddec95c0b48ce216220d7e3481dab3bc78/mlir/lib/Conversion/ShapeToSCF/ShapeToSCF.cpp#L85):

LogicalResult ShapeOfOpConverter::matchAndRewrite(ShapeOfOp op, ArrayRef<Value> operands, ConversionPatternRewriter &rewriter) const {

...

rewriter.create<scf::ForOp>(loc, zero, rank, one, llvm::None,
    [&](OpBuilder &b, Location loc, Value iv, ValueRange args) {
      b.create<DimOp>(loc, tensor, iv);
      b.create<scf::YieldOp>(loc);
    });                                                                                                                                                        

...

}

...

target.addIllegalOp<DimOp>();

frgossen avatar Jul 15 '20 15:07 frgossen

It works now. Thank you!

frgossen avatar Jul 16 '20 09:07 frgossen

Can you try again at head? I submitted a fix for an identical case to this yesterday afternoon.

River707 avatar Jul 15 '20 17:07 River707

assigned to @ftynse

frgossen avatar Jul 15 '20 15:07 frgossen