[all-commits] [llvm/llvm-project] 3d8b90: [PDLL] Add support for single line lambda-like pat...

River Riddle via All-commits all-commits at lists.llvm.org
Thu Feb 10 12:49:26 PST 2022

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 3d8b90601211914b0d4690fa603e4b5c43e5c9ac
  Author: River Riddle <riddleriver at gmail.com>
  Date:   2022-02-10 (Thu, 10 Feb 2022)

  Changed paths:
    M mlir/lib/Tools/PDLL/Parser/Parser.cpp
    M mlir/test/mlir-pdll/Parser/pattern-failure.pdll
    M mlir/test/mlir-pdll/Parser/pattern.pdll

  Log Message:
  [PDLL] Add support for single line lambda-like patterns

This allows for defining simple patterns in a single line. The lambda
body of a Pattern expects a single operation rewrite statement:

Pattern => replace op<my_dialect.foo>(operands: ValueRange) with operands;

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

  Commit: faf42264e5401a1dfca95b701e5c2bf951d7f8a7
  Author: River Riddle <riddleriver at gmail.com>
  Date:   2022-02-10 (Thu, 10 Feb 2022)

  Changed paths:
    M mlir/include/mlir/Tools/PDLL/AST/Nodes.h
    M mlir/include/mlir/Tools/PDLL/AST/Types.h
    M mlir/lib/Tools/PDLL/AST/Context.cpp
    M mlir/lib/Tools/PDLL/AST/NodePrinter.cpp
    M mlir/lib/Tools/PDLL/AST/Nodes.cpp
    M mlir/lib/Tools/PDLL/AST/TypeDetail.h
    M mlir/lib/Tools/PDLL/AST/Types.cpp
    M mlir/lib/Tools/PDLL/Parser/Lexer.cpp
    M mlir/lib/Tools/PDLL/Parser/Lexer.h
    M mlir/lib/Tools/PDLL/Parser/Parser.cpp
    A mlir/test/mlir-pdll/Parser/constraint-failure.pdll
    A mlir/test/mlir-pdll/Parser/constraint.pdll
    M mlir/test/mlir-pdll/Parser/expr-failure.pdll
    M mlir/test/mlir-pdll/Parser/expr.pdll
    M mlir/test/mlir-pdll/Parser/pattern-failure.pdll
    A mlir/test/mlir-pdll/Parser/rewrite-failure.pdll
    A mlir/test/mlir-pdll/Parser/rewrite.pdll
    M mlir/test/mlir-pdll/Parser/stmt-failure.pdll

  Log Message:
  [PDLL] Add support for user defined constraint and rewrite functions

These functions allow for defining pattern fragments usable within the `match` and `rewrite` sections of a pattern. The main structure of Constraints and Rewrites functions are the same, and are similar to functions in other languages; they contain a signature (i.e. name, argument list, result list) and a body:

// Constraint that takes a value as an input, and produces a value:
Constraint Cst(arg: Value) -> Value { ... }

// Constraint that returns multiple values:
Constraint Cst() -> (result1: Value, result2: ValueRange);

When returning multiple results, each result can be optionally be named (the result of a Constraint/Rewrite in the case of multiple results is a tuple).

These body of a Constraint/Rewrite functions can be specified in several ways:

* Externally
In this case we are importing an external function (registered by the user outside of PDLL):

Constraint Foo(op: Op);
Rewrite Bar();

* In PDLL (using PDLL constructs)
In this case, the body is defined using PDLL constructs:

Rewrite BuildFooOp() {
  // The result type of the Rewrite is inferred from the return.
  return op<my_dialect.foo>;
// Constraints/Rewrites can also implement a lambda/expression
// body for simple one line bodies.
Rewrite BuildFooOp() => op<my_dialect.foo>;

* In PDLL (using a native/C++ code block)
In this case the body is specified using a C++(or potentially other language at some point) code block. When building PDLL in AOT mode this will generate a native constraint/rewrite and register it with the PDL bytecode.

Rewrite BuildFooOp() -> Op<my_dialect.foo> [{
  return rewriter.create<my_dialect::FooOp>(...);

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

Compare: https://github.com/llvm/llvm-project/compare/8cffea061afc...faf42264e540

More information about the All-commits mailing list