Author: Nicolas Vasilache Date: 2020-12-30T16:44:26Z New Revision: 9b5a3d67b496ce92729c61b7b2a99e8dc8d39ca3
URL: https://github.com/llvm/llvm-project/commit/9b5a3d67b496ce92729c61b7b2a99e8dc8d39ca3 DIFF: https://github.com/llvm/llvm-project/commit/9b5a3d67b496ce92729c61b7b2a99e8dc8d39ca3.diff LOG: [mlir] Fix indexing of first offset operand in ops that implement OffsetSizeAndStrideOpInterface OffsetSizeAndStrideOpInterface ops may have a varying number of operands before the first offset operand. This revision adds a method that such ops much implement to properly specify the position of the first offset operand. Differential Revision: https://reviews.llvm.org/D93947 Added: Modified: mlir/include/mlir/Dialect/StandardOps/IR/Ops.td mlir/include/mlir/Interfaces/ViewLikeInterface.td Removed: ################################################################################ diff --git a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td index ba78db68214f..1f7b888167cd 100644 --- a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td +++ b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td @@ -2177,6 +2177,10 @@ def MemRefReinterpretCastOp: unsigned resultRank = getResult().getType().cast<ShapedType>().getRank(); return {1, resultRank, resultRank}; } + + /// Return the number of leading operands before the `offsets`, `sizes` and + /// and `strides` operands. + static unsigned getOffsetSizeAndStrideStartOperandIndex() { return 1; } }]; } @@ -3031,7 +3035,8 @@ def SubIOp : IntArithmeticOp<"subi"> { //===----------------------------------------------------------------------===// def SubViewOp : BaseOpWithOffsetSizesAndStrides< - "subview", [DeclareOpInterfaceMethods<ViewLikeOpInterface>, OffsetSizeAndStrideOpInterface] > { + "subview", [DeclareOpInterfaceMethods<ViewLikeOpInterface>, + OffsetSizeAndStrideOpInterface] > { let summary = "memref subview operation"; let description = [{ The "subview" operation converts a memref type to another memref type @@ -3217,6 +3222,10 @@ def SubViewOp : BaseOpWithOffsetSizesAndStrides< unsigned rank = getSourceType().getRank(); return {rank, rank, rank}; } + + /// Return the number of leading operands before the `offsets`, `sizes` and + /// and `strides` operands. + static unsigned getOffsetSizeAndStrideStartOperandIndex() { return 1; } }]; let hasCanonicalizer = 1; @@ -3227,7 +3236,8 @@ def SubViewOp : BaseOpWithOffsetSizesAndStrides< // SubTensorOp //===----------------------------------------------------------------------===// -def SubTensorOp : BaseOpWithOffsetSizesAndStrides<"subtensor", [OffsetSizeAndStrideOpInterface]> { +def SubTensorOp : BaseOpWithOffsetSizesAndStrides< + "subtensor", [OffsetSizeAndStrideOpInterface]> { let summary = "subtensor operation"; let description = [{ The "subtensor" operation extract a tensor from another tensor as @@ -3279,12 +3289,12 @@ def SubTensorOp : BaseOpWithOffsetSizesAndStrides<"subtensor", [OffsetSizeAndStr let results = (outs AnyRankedTensor:$result); let builders = [ - // Build a SubViewOp with mixed static and dynamic entries. + // Build a SubTensorOp with mixed static and dynamic entries. OpBuilderDAG<(ins "Value":$source, "ArrayRef<int64_t>":$staticOffsets, "ArrayRef<int64_t>":$staticSizes, "ArrayRef<int64_t>":$staticStrides, "ValueRange":$offsets, "ValueRange":$sizes, "ValueRange":$strides, CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)>, - // Build a SubViewOp with all dynamic entries. + // Build a SubTensorOp with all dynamic entries. OpBuilderDAG<(ins "Value":$source, "ValueRange":$offsets, "ValueRange":$sizes, "ValueRange":$strides, CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)> @@ -3315,6 +3325,10 @@ def SubTensorOp : BaseOpWithOffsetSizesAndStrides<"subtensor", [OffsetSizeAndStr unsigned rank = getSourceType().getRank(); return {rank, rank, rank}; } + + /// Return the number of leading operands before the `offsets`, `sizes` and + /// and `strides` operands. + static unsigned getOffsetSizeAndStrideStartOperandIndex() { return 1; } }]; let hasCanonicalizer = 1; @@ -3324,7 +3338,8 @@ def SubTensorOp : BaseOpWithOffsetSizesAndStrides<"subtensor", [OffsetSizeAndStr // SubTensorInsertOp //===----------------------------------------------------------------------===// -def SubTensorInsertOp : BaseOpWithOffsetSizesAndStrides<"subtensor_insert", [OffsetSizeAndStrideOpInterface]> { +def SubTensorInsertOp : BaseOpWithOffsetSizesAndStrides< + "subtensor_insert", [OffsetSizeAndStrideOpInterface]> { let summary = "subtensor_insert operation"; let description = [{ The "subtensor_insert" operation insert a tensor `source` into another @@ -3369,13 +3384,13 @@ def SubTensorInsertOp : BaseOpWithOffsetSizesAndStrides<"subtensor_insert", [Off let results = (outs AnyRankedTensor:$result); let builders = [ - // Build a SubViewOp with mixed static and dynamic entries. + // Build a SubTensorInsertOp with mixed static and dynamic entries. OpBuilderDAG<(ins "Value":$source, "Value":$dest, "ArrayRef<int64_t>":$staticOffsets, "ArrayRef<int64_t>":$staticSizes, "ArrayRef<int64_t>":$staticStrides, "ValueRange":$offsets, "ValueRange":$sizes, "ValueRange":$strides, CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)>, - // Build a SubViewOp with all dynamic entries. + // Build a SubTensorInsertOp with all dynamic entries. OpBuilderDAG<(ins "Value":$source, "Value":$dest, "ValueRange":$offsets, "ValueRange":$sizes, "ValueRange":$strides, CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)> @@ -3398,6 +3413,10 @@ def SubTensorInsertOp : BaseOpWithOffsetSizesAndStrides<"subtensor_insert", [Off unsigned rank = getSourceType().getRank(); return {rank, rank, rank}; } + + /// Return the number of leading operands before the `offsets`, `sizes` and + /// and `strides` operands. + static unsigned getOffsetSizeAndStrideStartOperandIndex() { return 2; } }]; } diff --git a/mlir/include/mlir/Interfaces/ViewLikeInterface.td b/mlir/include/mlir/Interfaces/ViewLikeInterface.td index 31f9bca8d7fb..62c371b2f97d 100644 --- a/mlir/include/mlir/Interfaces/ViewLikeInterface.td +++ b/mlir/include/mlir/Interfaces/ViewLikeInterface.td @@ -40,6 +40,8 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface 2. `offsets`, `sizes` and `strides` variadic operands. 3. `static_offsets`, resp. `static_sizes` and `static_strides` integer array attributes. + 4. `getOffsetSizeAndStrideStartOperandIndex` method that specifies the + starting index of the OffsetSizeAndStrideOpInterface operands The invariants of this interface are: 1. `static_offsets`, `static_sizes` and `static_strides` have length @@ -53,6 +55,8 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface a dynamic offset (resp. size, stride). 4. a variadic `offset` (resp. `sizes`, `strides`) operand must be present for each dynamic offset (resp. size, stride). + 5. `offsets`, `sizes` and `strides` operands are specified in this order + at operand index starting at `getOffsetSizeAndStrideStartOperandIndex`. This interface is useful to factor out common behavior and provide support for carrying or injecting static behavior through the use of the static @@ -62,6 +66,15 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface let cppNamespace = "::mlir"; let methods = [ + InterfaceMethod< + /*desc=*/[{ + Return the number of leading operands before the `offsets`, `sizes` and + and `strides` operands. + }], + /*retTy=*/"unsigned", + /*methodName=*/"getOffsetSizeAndStrideStartOperandIndex", + /*args=*/(ins) + >, InterfaceMethod< /*desc=*/[{ Return the expected rank of each of the`static_offsets`, `static_sizes` @@ -69,11 +82,7 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface }], /*retTy=*/"std::array<unsigned, 3>", /*methodName=*/"getArrayAttrRanks", - /*args=*/(ins), - /*methodBody=*/"", - /*defaultImplementation=*/[{ - return $_op.offsets(); - }] + /*args=*/(ins) >, InterfaceMethod< /*desc=*/[{ @@ -250,9 +259,9 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface assert($_op.isDynamicOffset(idx) && "expected dynamic offset"); auto numDynamic = getNumDynamicEntriesUpToIdx( static_offsets().template cast<ArrayAttr>(), - ShapedType::isDynamicStrideOrOffset, + ShapedType::isDynamicStrideOrOffset, idx); - return 1 + numDynamic; + return $_op.getOffsetSizeAndStrideStartOperandIndex() + numDynamic; }] >, InterfaceMethod< @@ -268,7 +277,8 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface assert($_op.isDynamicSize(idx) && "expected dynamic size"); auto numDynamic = getNumDynamicEntriesUpToIdx( static_sizes().template cast<ArrayAttr>(), ShapedType::isDynamic, idx); - return 1 + offsets().size() + numDynamic; + return $_op.getOffsetSizeAndStrideStartOperandIndex() + + offsets().size() + numDynamic; }] >, InterfaceMethod< @@ -284,9 +294,10 @@ def OffsetSizeAndStrideOpInterface : OpInterface<"OffsetSizeAndStrideOpInterface assert($_op.isDynamicStride(idx) && "expected dynamic stride"); auto numDynamic = getNumDynamicEntriesUpToIdx( static_strides().template cast<ArrayAttr>(), - ShapedType::isDynamicStrideOrOffset, + ShapedType::isDynamicStrideOrOffset, idx); - return 1 + offsets().size() + sizes().size() + numDynamic; + return $_op.getOffsetSizeAndStrideStartOperandIndex() + + offsets().size() + sizes().size() + numDynamic; }] >, InterfaceMethod< _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits