1//===- ToyCombine.td - Pattern Match Optimizations for Toy -*- tablegen -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// Defines language-specific pattern match optimizations for Toy using 10// Declarative Rewrite Rules (DRR) specified using TableGen records. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef TOY_COMBINE 15#define TOY_COMBINE 16 17include "toy/Ops.td" 18 19/// Note: The DRR definition used for defining patterns is shown below: 20/// 21/// class Pattern< 22/// dag sourcePattern, list<dag> resultPatterns, 23/// list<dag> additionalConstraints = [], 24/// dag benefitsAdded = (addBenefit 0) 25/// >; 26 27//===----------------------------------------------------------------------===// 28// Basic Pattern-Match and Rewrite 29//===----------------------------------------------------------------------===// 30 31// Reshape(Reshape(x)) = Reshape(x) 32def ReshapeReshapeOptPattern : Pat<(ReshapeOp(ReshapeOp $arg)), 33 (ReshapeOp $arg)>; 34 35//===----------------------------------------------------------------------===// 36// Pattern-Match and Rewrite using Native Code Call 37//===----------------------------------------------------------------------===// 38 39// Native Code Calls may be used for more complex transformations using inline 40// C++ and C++ helper functions. 41 42// Reshape(Constant(x)) = x' 43def ReshapeConstant : 44 NativeCodeCall<"$0.reshape(($1.getType()).cast<ShapedType>())">; 45def FoldConstantReshapeOptPattern : Pat< 46 (ReshapeOp:$res (ConstantOp $arg)), 47 (ConstantOp (ReshapeConstant $arg, $res))>; 48 49//===----------------------------------------------------------------------===// 50// Pattern-Match and Rewrite with Constraints 51//===----------------------------------------------------------------------===// 52 53// DRR allows for constraint checking when the transformation is conditional 54// on operand properties. 55 56// Reshape(x) = x, where input and output shapes are identical 57def TypesAreIdentical : Constraint<CPred<"$0.getType() == $1.getType()">>; 58def RedundantReshapeOptPattern : Pat< 59 (ReshapeOp:$res $arg), (replaceWithValue $arg), 60 [(TypesAreIdentical $res, $arg)]>; 61 62#endif // TOY_COMBINE 63