[all-commits] [llvm/llvm-project] 8a1ca2: [mlir] Add a conversion pass between PDL and the ...

River Riddle via All-commits all-commits at lists.llvm.org
Mon Oct 26 18:05:30 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 8a1ca2cd347cf80b377b36d1a7af3f31ec5b575e
      https://github.com/llvm/llvm-project/commit/8a1ca2cd347cf80b377b36d1a7af3f31ec5b575e
  Author: River Riddle <riddleriver at gmail.com>
  Date:   2020-10-26 (Mon, 26 Oct 2020)

  Changed paths:
    A mlir/include/mlir/Conversion/PDLToPDLInterp/PDLToPDLInterp.h
    M mlir/include/mlir/Conversion/Passes.h
    M mlir/include/mlir/Conversion/Passes.td
    M mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td
    M mlir/include/mlir/Support/StorageUniquer.h
    M mlir/lib/Conversion/CMakeLists.txt
    A mlir/lib/Conversion/PDLToPDLInterp/CMakeLists.txt
    A mlir/lib/Conversion/PDLToPDLInterp/PDLToPDLInterp.cpp
    A mlir/lib/Conversion/PDLToPDLInterp/Predicate.cpp
    A mlir/lib/Conversion/PDLToPDLInterp/Predicate.h
    A mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.cpp
    A mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.h
    M mlir/lib/Conversion/PassDetail.h
    A mlir/test/Conversion/PDLToPDLInterp/pdl-to-pdl-interp-matcher.mlir
    A mlir/test/Conversion/PDLToPDLInterp/pdl-to-pdl-interp-rewriter.mlir

  Log Message:
  -----------
  [mlir] Add a conversion pass between PDL and the  PDL Interpreter Dialect

The conversion between PDL and the interpreter is split into several different parts.
** The Matcher:

The matching section of all incoming pdl.pattern operations is converted into a predicate tree and merged. Each pattern is first converted into an ordered list of predicates starting from the root operation. A predicate is composed of three distinct parts:
* Position
  - A position refers to a specific location on the input DAG, i.e. an
    existing MLIR entity being matched. These can be attributes, operands,
    operations, results, and types. Each position also defines a relation to
    its parent. For example, the operand `[0] -> 1` has a parent operation
    position `[0]` (the root).
* Question
  - A question refers to a query on a specific positional value. For
  example, an operation name question checks the name of an operation
  position.
* Answer
  - An answer is the expected result of a question. For example, when
  matching an operation with the name "foo.op". The question would be an
  operation name question, with an expected answer of "foo.op".

After the predicate lists have been created and ordered(based on occurrence of common predicates and other factors), they are formed into a tree of nodes that represent the branching flow of a pattern match. This structure allows for efficient construction and merging of the input patterns. There are currently only 4 simple nodes in the tree:
* ExitNode: Represents the termination of a match
* SuccessNode: Represents a successful match of a specific pattern
* BoolNode/SwitchNode: Branch to a specific child node based on the expected answer to a predicate question.

Once the matcher tree has been generated, this tree is walked to generate the corresponding interpreter operations.

 ** The Rewriter:
The rewriter portion of a pattern is generated in a very straightforward manor, similarly to lowerings in other dialects. Each PDL operation that may exist within a rewrite has a mapping into the interpreter dialect. The code for the rewriter is generated within a FuncOp, that is invoked by the interpreter on a successful pattern match. Referenced values defined in the matcher become inputs the generated rewriter function.

An example lowering is shown below:

```mlir
// The following high level PDL pattern:
pdl.pattern : benefit(1) {
  %resultType = pdl.type
  %inputOperand = pdl.input
  %root, %results = pdl.operation "foo.op"(%inputOperand) -> %resultType
  pdl.rewrite %root {
    pdl.replace %root with (%inputOperand)
  }
}

// is lowered to the following:
module {
  // The matcher function takes the root operation as an input.
  func @matcher(%arg0: !pdl.operation) {
    pdl_interp.check_operation_name of %arg0 is "foo.op" -> ^bb2, ^bb1
  ^bb1:
    pdl_interp.return
  ^bb2:
    pdl_interp.check_operand_count of %arg0 is 1 -> ^bb3, ^bb1
  ^bb3:
    pdl_interp.check_result_count of %arg0 is 1 -> ^bb4, ^bb1
  ^bb4:
    %0 = pdl_interp.get_operand 0 of %arg0
    pdl_interp.is_not_null %0 : !pdl.value -> ^bb5, ^bb1
  ^bb5:
    %1 = pdl_interp.get_result 0 of %arg0
    pdl_interp.is_not_null %1 : !pdl.value -> ^bb6, ^bb1
  ^bb6:
    // This operation corresponds to a successful pattern match.
    pdl_interp.record_match @rewriters::@rewriter(%0, %arg0 : !pdl.value, !pdl.operation) : benefit(1), loc([%arg0]), root("foo.op") -> ^bb1
  }
  module @rewriters {
    // The inputs to the rewriter from the matcher are passed as arguments.
    func @rewriter(%arg0: !pdl.value, %arg1: !pdl.operation) {
      pdl_interp.replace %arg1 with(%arg0)
      pdl_interp.return
    }
  }
}
```

Differential Revision: https://reviews.llvm.org/D84580


  Commit: b99bd771626fbbf8b9b29ce312d4151968796826
      https://github.com/llvm/llvm-project/commit/b99bd771626fbbf8b9b29ce312d4151968796826
  Author: River Riddle <riddleriver at gmail.com>
  Date:   2020-10-26 (Mon, 26 Oct 2020)

  Changed paths:
    M mlir/docs/PatternRewriter.md
    M mlir/include/mlir/IR/PatternMatch.h
    M mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
    M mlir/lib/IR/PatternMatch.cpp
    M mlir/lib/Transforms/DialectConversion.cpp
    M mlir/test/lib/Dialect/Test/TestPatterns.cpp

  Log Message:
  -----------
  [mlir][Pattern] Refactor the Pattern class into a "metadata only" class

The Pattern class was originally intended to be used for solely matching operations, but that use never materialized. All of the pattern infrastructure uses RewritePattern, and the infrastructure for pure matching(Matchers.h) is implemented inline. This means that this class isn't a useful abstraction at the moment, so this revision refactors it to solely encapsulate the "metadata" of a pattern. The metadata includes the various state describing a pattern; benefit, root operation, etc. The API on PatternApplicator is updated to now operate on `Pattern`s as nothing special from `RewritePattern` is necessary.

This refactoring is also necessary for the upcoming use of PDL patterns alongside C++ rewrite patterns.

Differential Revision: https://reviews.llvm.org/D86258


  Commit: b6eb26fd0e316b36e3750f7cba7ebdb10219790c
      https://github.com/llvm/llvm-project/commit/b6eb26fd0e316b36e3750f7cba7ebdb10219790c
  Author: River Riddle <riddleriver at gmail.com>
  Date:   2020-10-26 (Mon, 26 Oct 2020)

  Changed paths:
    M mlir/include/mlir/IR/PatternMatch.h
    A mlir/include/mlir/Rewrite/PatternApplicator.h
    A mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
    M mlir/lib/CMakeLists.txt
    M mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp
    M mlir/lib/Conversion/GPUToROCDL/LowerGpuOpsToROCDLOps.cpp
    M mlir/lib/Conversion/ShapeToStandard/ConvertShapeConstraints.cpp
    M mlir/lib/Conversion/StandardToSPIRV/LegalizeStandardForSPIRV.cpp
    M mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
    M mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp
    M mlir/lib/Dialect/Affine/Transforms/AffineDataCopyGeneration.cpp
    M mlir/lib/Dialect/Affine/Transforms/SimplifyAffineStructures.cpp
    M mlir/lib/Dialect/Affine/Utils/Utils.cpp
    M mlir/lib/Dialect/Linalg/Transforms/CodegenStrategy.cpp
    M mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Fusion.cpp
    M mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Loops.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Tiling.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp
    M mlir/lib/Dialect/Quant/Transforms/ConvertConst.cpp
    M mlir/lib/Dialect/Quant/Transforms/ConvertSimQuant.cpp
    M mlir/lib/Dialect/Shape/Transforms/RemoveShapeConstraints.cpp
    M mlir/lib/IR/PatternMatch.cpp
    A mlir/lib/Rewrite/CMakeLists.txt
    A mlir/lib/Rewrite/PatternApplicator.cpp
    M mlir/lib/Transforms/CMakeLists.txt
    M mlir/lib/Transforms/Canonicalizer.cpp
    R mlir/lib/Transforms/DialectConversion.cpp
    M mlir/lib/Transforms/Inliner.cpp
    M mlir/lib/Transforms/Utils/CMakeLists.txt
    A mlir/lib/Transforms/Utils/DialectConversion.cpp
    M mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
    M mlir/lib/Transforms/Utils/LoopUtils.cpp
    M mlir/test/lib/Dialect/Affine/TestAffineDataCopy.cpp
    M mlir/test/lib/Dialect/Test/TestPatterns.cpp
    M mlir/test/lib/Dialect/Test/TestTraits.cpp
    M mlir/test/lib/Transforms/TestConvVectorization.cpp
    M mlir/test/lib/Transforms/TestExpandTanh.cpp
    M mlir/test/lib/Transforms/TestGpuRewrite.cpp
    M mlir/test/lib/Transforms/TestLinalgFusionTransforms.cpp
    M mlir/test/lib/Transforms/TestLinalgTransforms.cpp
    M mlir/test/lib/Transforms/TestVectorTransforms.cpp

  Log Message:
  -----------
  [mlir][NFC] Move around the code related to PatternRewriting to improve layering

There are several pieces of pattern rewriting infra in IR/ that really shouldn't be there. This revision moves those pieces to a better location such that they are easier to evolve in the future(e.g. with PDL). More concretely this revision does the following:

* Create a Transforms/GreedyPatternRewriteDriver.h and move the apply*andFold methods there.
The definitions for these methods are already in Transforms/ so it doesn't make sense for the declarations to be in IR.

* Create a new lib/Rewrite library and move PatternApplicator there.
This new library will be focused on applying rewrites, and will also include compiling rewrites with PDL.

Differential Revision: https://reviews.llvm.org/D89103


  Commit: 3fffffa882c0c3702e1ce4c6eaf8a380ac4ab065
      https://github.com/llvm/llvm-project/commit/3fffffa882c0c3702e1ce4c6eaf8a380ac4ab065
  Author: River Riddle <riddleriver at gmail.com>
  Date:   2020-10-26 (Mon, 26 Oct 2020)

  Changed paths:
    M mlir/examples/toy/Ch5/mlir/LowerToAffineLoops.cpp
    M mlir/examples/toy/Ch6/mlir/LowerToAffineLoops.cpp
    M mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp
    M mlir/examples/toy/Ch7/mlir/LowerToAffineLoops.cpp
    M mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp
    M mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
    M mlir/include/mlir/IR/PatternMatch.h
    A mlir/include/mlir/Rewrite/FrozenRewritePatternList.h
    M mlir/include/mlir/Rewrite/PatternApplicator.h
    M mlir/include/mlir/Transforms/DialectConversion.h
    M mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
    M mlir/lib/Conversion/AVX512ToLLVM/ConvertAVX512ToLLVM.cpp
    M mlir/lib/Conversion/AffineToStandard/AffineToStandard.cpp
    M mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp
    M mlir/lib/Conversion/GPUCommon/ConvertLaunchFuncToRuntimeCalls.cpp
    M mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp
    M mlir/lib/Conversion/GPUToROCDL/LowerGpuOpsToROCDLOps.cpp
    M mlir/lib/Conversion/GPUToSPIRV/ConvertGPUToSPIRVPass.cpp
    M mlir/lib/Conversion/LinalgToLLVM/LinalgToLLVM.cpp
    M mlir/lib/Conversion/LinalgToSPIRV/LinalgToSPIRVPass.cpp
    M mlir/lib/Conversion/LinalgToStandard/LinalgToStandard.cpp
    M mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp
    M mlir/lib/Conversion/SCFToGPU/SCFToGPUPass.cpp
    M mlir/lib/Conversion/SCFToStandard/SCFToStandard.cpp
    M mlir/lib/Conversion/SPIRVToLLVM/ConvertLaunchFuncToLLVMCalls.cpp
    M mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVMPass.cpp
    M mlir/lib/Conversion/ShapeToStandard/ShapeToStandard.cpp
    M mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp
    M mlir/lib/Conversion/StandardToSPIRV/ConvertStandardToSPIRVPass.cpp
    M mlir/lib/Conversion/StandardToSPIRV/LegalizeStandardForSPIRV.cpp
    M mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
    M mlir/lib/Conversion/VectorToROCDL/VectorToROCDL.cpp
    M mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp
    M mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRV.cpp
    M mlir/lib/Dialect/Affine/Transforms/AffineDataCopyGeneration.cpp
    M mlir/lib/Dialect/Affine/Transforms/SimplifyAffineStructures.cpp
    M mlir/lib/Dialect/Affine/Utils/Utils.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Bufferize.cpp
    M mlir/lib/Dialect/Linalg/Transforms/CodegenStrategy.cpp
    M mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
    M mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Loops.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Tiling.cpp
    M mlir/lib/Dialect/Linalg/Transforms/Transforms.cpp
    M mlir/lib/Dialect/Quant/Transforms/ConvertConst.cpp
    M mlir/lib/Dialect/Quant/Transforms/ConvertSimQuant.cpp
    M mlir/lib/Dialect/SCF/Transforms/Bufferize.cpp
    M mlir/lib/Dialect/SPIRV/Transforms/DecorateSPIRVCompositeTypeLayoutPass.cpp
    M mlir/lib/Dialect/SPIRV/Transforms/LowerABIAttributesPass.cpp
    M mlir/lib/Dialect/Shape/Transforms/Bufferize.cpp
    M mlir/lib/Dialect/Shape/Transforms/RemoveShapeConstraints.cpp
    M mlir/lib/Dialect/Shape/Transforms/ShapeToShapeLowering.cpp
    M mlir/lib/Dialect/StandardOps/Transforms/Bufferize.cpp
    M mlir/lib/Dialect/StandardOps/Transforms/ExpandAtomic.cpp
    M mlir/lib/Rewrite/CMakeLists.txt
    A mlir/lib/Rewrite/FrozenRewritePatternList.cpp
    M mlir/lib/Rewrite/PatternApplicator.cpp
    M mlir/lib/Transforms/Canonicalizer.cpp
    M mlir/lib/Transforms/Inliner.cpp
    M mlir/lib/Transforms/Utils/DialectConversion.cpp
    M mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
    M mlir/test/lib/Dialect/SPIRV/TestAvailability.cpp
    M mlir/test/lib/Dialect/Test/TestPatterns.cpp
    M mlir/test/lib/Dialect/Test/TestTraits.cpp
    M mlir/test/lib/Transforms/TestBufferPlacement.cpp
    M mlir/test/lib/Transforms/TestConvVectorization.cpp
    M mlir/test/lib/Transforms/TestConvertCallOp.cpp
    M mlir/test/lib/Transforms/TestExpandTanh.cpp
    M mlir/test/lib/Transforms/TestGpuRewrite.cpp
    M mlir/test/lib/Transforms/TestLinalgFusionTransforms.cpp
    M mlir/test/lib/Transforms/TestLinalgTransforms.cpp
    M mlir/test/lib/Transforms/TestVectorTransforms.cpp

  Log Message:
  -----------
  [mlir][Pattern] Add a new FrozenRewritePatternList class

This class represents a rewrite pattern list that has been frozen, and thus immutable. This replaces the uses of OwningRewritePatternList in pattern driver related API, such as dialect conversion. When PDL becomes more prevalent, this API will allow for optimizing a set of patterns once without the need to do this per run of a pass.

Differential Revision: https://reviews.llvm.org/D89104


Compare: https://github.com/llvm/llvm-project/compare/aab50af8c18a...3fffffa882c0


More information about the All-commits mailing list