9#ifndef MLIR_IR_PDLPATTERNMATCH_H
10#define MLIR_IR_PDLPATTERNMATCH_H
12#include "mlir/Config/mlir-config.h"
14#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
17#include "llvm/ADT/TypeSwitch.h"
35 PDLValue(
const PDLValue &other) =
default;
36 PDLValue(std::nullptr_t =
nullptr) {}
37 PDLValue(Attribute value)
38 : value(value.getAsOpaquePointer()), kind(
Kind::Attribute) {}
39 PDLValue(Operation *value) : value(value), kind(
Kind::Operation) {}
40 PDLValue(Type value) : value(value.getAsOpaquePointer()), kind(
Kind::Type) {}
43 : value(value.getAsOpaquePointer()), kind(
Kind::Value) {}
49 assert(value &&
"isa<> used on a null value");
50 return kind == getKindOf<T>();
56 typename ResultT = std::conditional_t<
57 std::is_constructible_v<bool, T>, T, std::optional<T>>>
58 ResultT dyn_cast()
const {
59 return isa<T>() ? castImpl<T>() : ResultT();
66 assert(isa<T>() &&
"expected value to be of type `T`");
71 const void *getAsOpaquePointer()
const {
return value; }
74 explicit operator bool()
const {
return value; }
77 Kind getKind()
const {
return kind; }
80 void print(raw_ostream &os)
const;
83 static void print(raw_ostream &os, Kind kind);
87 template <
typename...>
89 template <
typename T,
typename... R>
90 struct index_of_t<T, T, R...> : std::integral_constant<size_t, 0> {};
91 template <
typename T,
typename F,
typename... R>
92 struct index_of_t<T, F, R...>
93 : std::integral_constant<size_t, 1 + index_of_t<T, R...>::value> {};
97 static Kind getKindOf() {
98 return static_cast<Kind>(index_of_t<T, Attribute, Operation *, Type,
104 template <
typename T>
105 std::enable_if_t<llvm::is_one_of<T, Attribute, Type, Value>::value, T>
107 return T::getFromOpaquePointer(value);
109 template <
typename T>
110 std::enable_if_t<llvm::is_one_of<T, TypeRange, ValueRange>::value, T>
112 return *
reinterpret_cast<T *
>(
const_cast<void *
>(value));
114 template <
typename T>
115 std::enable_if_t<std::is_pointer<T>::value, T> castImpl()
const {
116 return reinterpret_cast<T
>(
const_cast<void *
>(value));
120 const void *value{
nullptr};
122 Kind kind{Kind::Attribute};
131 PDLValue::print(os, kind);
144 void push_back(Attribute value) { results.push_back(value); }
147 void push_back(Operation *value) { results.push_back(value); }
150 void push_back(Type value) { results.push_back(value); }
156 allocatedTypeRanges.emplace_back(value.begin(), value.end());
157 typeRanges.push_back(allocatedTypeRanges.back());
158 results.push_back(&typeRanges.back());
160 void push_back(ValueTypeRange<OperandRange> value) {
161 typeRanges.push_back(value);
162 results.push_back(&typeRanges.back());
164 void push_back(ValueTypeRange<ResultRange> value) {
165 typeRanges.push_back(value);
166 results.push_back(&typeRanges.back());
170 void push_back(Value value) { results.push_back(value); }
176 allocatedValueRanges.emplace_back(value.begin(), value.end());
177 valueRanges.push_back(allocatedValueRanges.back());
178 results.push_back(&valueRanges.back());
181 valueRanges.push_back(value);
182 results.push_back(&valueRanges.back());
185 valueRanges.push_back(value);
186 results.push_back(&valueRanges.back());
191 PDLResultList(
unsigned maxNumResults) {
195 typeRanges.reserve(maxNumResults);
196 valueRanges.reserve(maxNumResults);
200 SmallVector<PDLValue> results;
202 SmallVector<TypeRange> typeRanges;
203 SmallVector<ValueRange> valueRanges;
206 SmallVector<std::vector<Type>> allocatedTypeRanges;
207 SmallVector<std::vector<Value>> allocatedValueRanges;
217class PDLPatternConfig {
219 virtual ~PDLPatternConfig() =
default;
224 virtual void notifyRewriteBegin(PatternRewriter &rewriter) {}
225 virtual void notifyRewriteEnd(PatternRewriter &rewriter) {}
228 TypeID getTypeID()
const {
return id; }
231 PDLPatternConfig(TypeID
id) : id(id) {}
240class PDLPatternConfigBase :
public PDLPatternConfig {
243 static bool classof(
const PDLPatternConfig *config) {
244 return config->getTypeID() == getConfigID();
248 static TypeID getConfigID() {
return TypeID::get<T>(); }
251 PDLPatternConfigBase() : PDLPatternConfig(getConfigID()) {}
257class PDLPatternConfigSet {
259 PDLPatternConfigSet() =
default;
262 template <
typename... ConfigsT>
263 PDLPatternConfigSet(ConfigsT &&...configs) {
264 (addConfig(std::forward<ConfigsT>(configs)), ...);
269 template <
typename T>
270 const T &
get()
const {
271 const T *config = tryGet<T>();
272 assert(config &&
"configuration not found");
278 template <
typename T>
279 const T *tryGet()
const {
280 for (
const auto &configIt : configs)
281 if (
const T *config = dyn_cast<T>(configIt.get()))
288 void notifyRewriteBegin(PatternRewriter &rewriter) {
289 for (
const auto &config : configs)
290 config->notifyRewriteBegin(rewriter);
292 void notifyRewriteEnd(PatternRewriter &rewriter) {
293 for (
const auto &config : configs)
294 config->notifyRewriteEnd(rewriter);
299 template <
typename T>
300 void addConfig(T &&config) {
301 assert(!tryGet<std::decay_t<T>>() &&
"configuration already exists");
302 configs.emplace_back(
303 std::make_unique<std::decay_t<T>>(std::forward<T>(config)));
309 SmallVector<std::unique_ptr<PDLPatternConfig>> configs;
318using PDLConstraintFunction = std::function<LogicalResult(
327using PDLRewriteFunction = std::function<LogicalResult(
331namespace pdl_function_builder {
343constexpr bool always_false =
false;
378template <
typename T,
typename Enable =
void>
379struct ProcessPDLValue;
398template <
typename T,
typename BaseT>
399struct ProcessPDLValueBasedOn {
401 verifyAsArg(function_ref<LogicalResult(
const Twine &)> errorFn,
402 PDLValue pdlValue,
size_t argIdx) {
404 if (
failed(ProcessPDLValue<BaseT>::verifyAsArg(errorFn, pdlValue, argIdx)))
406 return ProcessPDLValue<T>::verifyAsArg(
407 errorFn, ProcessPDLValue<BaseT>::processAsArg(pdlValue), argIdx);
409 static T processAsArg(PDLValue pdlValue) {
410 return ProcessPDLValue<T>::processAsArg(
411 ProcessPDLValue<BaseT>::processAsArg(pdlValue));
418 verifyAsArg(function_ref<LogicalResult(
const Twine &)> errorFn, BaseT value,
422 static T processAsArg(BaseT baseValue);
429struct ProcessBuiltinPDLValue {
431 verifyAsArg(function_ref<LogicalResult(
const Twine &)> errorFn,
432 PDLValue pdlValue,
size_t argIdx) {
435 return errorFn(
"expected a non-null value for argument " + Twine(argIdx) +
436 " of type: " + llvm::getTypeName<T>());
439 static T processAsArg(PDLValue pdlValue) {
return pdlValue.cast<T>(); }
440 static void processAsResult(PatternRewriter &, PDLResultList &results,
442 results.push_back(value);
450template <
typename T,
typename BaseT>
451struct ProcessDerivedPDLValue :
public ProcessPDLValueBasedOn<T, BaseT> {
453 verifyAsArg(function_ref<LogicalResult(
const Twine &)> errorFn,
454 BaseT baseValue,
size_t argIdx) {
455 return TypeSwitch<BaseT, LogicalResult>(baseValue)
456 .Case([&](T) {
return success(); })
457 .Default([&](BaseT) {
458 return errorFn(
"expected argument " + Twine(argIdx) +
459 " to be of type: " + llvm::getTypeName<T>());
462 using ProcessPDLValueBasedOn<T, BaseT>::verifyAsArg;
464 static T processAsArg(BaseT baseValue) {
465 return baseValue.template cast<T>();
467 using ProcessPDLValueBasedOn<T, BaseT>::processAsArg;
469 static void processAsResult(PatternRewriter &, PDLResultList &results,
471 results.push_back(value);
479struct ProcessPDLValue<Attribute> :
public ProcessBuiltinPDLValue<Attribute> {};
481struct ProcessPDLValue<T,
482 std::enable_if_t<std::is_base_of<Attribute, T>::value>>
483 :
public ProcessDerivedPDLValue<T, Attribute> {};
487struct ProcessPDLValue<StringRef>
488 :
public ProcessPDLValueBasedOn<StringRef, StringAttr> {
489 static StringRef processAsArg(StringAttr value) {
return value.getValue(); }
490 using ProcessPDLValueBasedOn<StringRef, StringAttr>::processAsArg;
492 static void processAsResult(PatternRewriter &rewriter, PDLResultList &results,
494 results.push_back(rewriter.getStringAttr(value));
498struct ProcessPDLValue<std::string>
499 :
public ProcessPDLValueBasedOn<std::string, StringAttr> {
500 template <
typename T>
501 static std::string processAsArg(T value) {
502 static_assert(always_false<T>,
503 "`std::string` arguments require a string copy, use "
504 "`StringRef` for string-like arguments instead");
507 static void processAsResult(PatternRewriter &rewriter, PDLResultList &results,
509 results.push_back(rewriter.getStringAttr(value));
517struct ProcessPDLValue<Operation *>
518 :
public ProcessBuiltinPDLValue<Operation *> {};
520struct ProcessPDLValue<T, std::enable_if_t<std::is_base_of<OpState, T>::value>>
521 :
public ProcessDerivedPDLValue<T, Operation *> {
522 static T processAsArg(Operation *value) {
return cast<T>(value); }
523 using ProcessDerivedPDLValue<T, Operation *>::processAsArg;
530struct ProcessPDLValue<Type> :
public ProcessBuiltinPDLValue<Type> {};
532struct ProcessPDLValue<T, std::enable_if_t<std::is_base_of<Type, T>::value>>
533 :
public ProcessDerivedPDLValue<T, Type> {};
539struct ProcessPDLValue<
TypeRange> :
public ProcessBuiltinPDLValue<TypeRange> {};
541struct ProcessPDLValue<ValueTypeRange<OperandRange>> {
542 static void processAsResult(PatternRewriter &, PDLResultList &results,
543 ValueTypeRange<OperandRange> types) {
544 results.push_back(types);
548struct ProcessPDLValue<ValueTypeRange<ResultRange>> {
549 static void processAsResult(PatternRewriter &, PDLResultList &results,
550 ValueTypeRange<ResultRange> types) {
551 results.push_back(types);
555struct ProcessPDLValue<SmallVector<Type, N>> {
556 static void processAsResult(PatternRewriter &, PDLResultList &results,
557 SmallVector<Type, N> values) {
566struct ProcessPDLValue<Value> :
public ProcessBuiltinPDLValue<Value> {};
572struct ProcessPDLValue<
ValueRange> :
public ProcessBuiltinPDLValue<ValueRange> {
575struct ProcessPDLValue<OperandRange> {
576 static void processAsResult(PatternRewriter &, PDLResultList &results,
577 OperandRange values) {
578 results.push_back(values);
582struct ProcessPDLValue<ResultRange> {
583 static void processAsResult(PatternRewriter &, PDLResultList &results,
584 ResultRange values) {
585 results.push_back(values);
589struct ProcessPDLValue<SmallVector<Value, N>> {
590 static void processAsResult(PatternRewriter &, PDLResultList &results,
591 SmallVector<Value, N> values) {
605template <
typename PDLFnT, std::size_t... I>
606LogicalResult verifyAsArgs(PatternRewriter &rewriter, ArrayRef<PDLValue> values,
607 std::index_sequence<I...>) {
608 using FnTraitsT = llvm::function_traits<PDLFnT>;
610 auto errorFn = [&](
const Twine &msg) {
611 return rewriter.notifyMatchFailure(rewriter.getUnknownLoc(), msg);
614 (succeeded(ProcessPDLValue<
typename FnTraitsT::template arg_t<I + 1>>::
615 verifyAsArg(errorFn, values[I], I)) &&
622template <
typename PDLFnT, std::size_t... I>
623void assertArgs(PatternRewriter &rewriter, ArrayRef<PDLValue> values,
624 std::index_sequence<I...>) {
627 using FnTraitsT = llvm::function_traits<PDLFnT>;
628 auto errorFn = [&](
const Twine &msg) -> LogicalResult {
629 llvm::report_fatal_error(msg);
632 assert((succeeded(ProcessPDLValue<
typename FnTraitsT::template arg_t<I + 1>>::
633 verifyAsArg(errorFn, values[I], I)) &&
645LogicalResult processResults(PatternRewriter &rewriter, PDLResultList &results,
647 ProcessPDLValue<T>::processAsResult(rewriter, results,
648 std::forward<T>(value));
653template <
typename T1,
typename T2>
654LogicalResult processResults(PatternRewriter &rewriter, PDLResultList &results,
655 std::pair<T1, T2> &&pair) {
656 if (
failed(processResults(rewriter, results, std::move(pair.first))) ||
657 failed(processResults(rewriter, results, std::move(pair.second))))
663template <
typename... Ts>
664LogicalResult processResults(PatternRewriter &rewriter, PDLResultList &results,
665 std::tuple<Ts...> &&tuple) {
666 auto applyFn = [&](
auto &&...args) {
667 return (succeeded(processResults(rewriter, results, std::move(args))) &&
670 return success(std::apply(applyFn, std::move(tuple)));
674inline LogicalResult processResults(PatternRewriter &rewriter,
675 PDLResultList &results,
680LogicalResult processResults(PatternRewriter &rewriter, PDLResultList &results,
684 return processResults(rewriter, results, std::move(*
result));
692template <
typename PDLFnT, std::size_t... I,
693 typename FnTraitsT = llvm::function_traits<PDLFnT>>
694typename FnTraitsT::result_t
695processArgsAndInvokeConstraint(PDLFnT &fn, PatternRewriter &rewriter,
696 ArrayRef<PDLValue> values,
697 std::index_sequence<I...>) {
700 (ProcessPDLValue<
typename FnTraitsT::template arg_t<I + 1>>::processAsArg(
710template <
typename Constra
intFnT>
712 std::is_convertible<ConstraintFnT, PDLConstraintFunction>::value,
713 PDLConstraintFunction>
714buildConstraintFn(ConstraintFnT &&constraintFn) {
715 return std::forward<ConstraintFnT>(constraintFn);
719template <
typename Constra
intFnT>
721 !std::is_convertible<ConstraintFnT, PDLConstraintFunction>::value,
722 PDLConstraintFunction>
723buildConstraintFn(ConstraintFnT &&constraintFn) {
724 return [constraintFn = std::forward<ConstraintFnT>(constraintFn)](
725 PatternRewriter &rewriter, PDLResultList &,
726 ArrayRef<PDLValue> values) -> LogicalResult {
727 auto argIndices = std::make_index_sequence<
728 llvm::function_traits<ConstraintFnT>::num_args - 1>();
729 if (
failed(verifyAsArgs<ConstraintFnT>(rewriter, values, argIndices)))
731 return processArgsAndInvokeConstraint(constraintFn, rewriter, values,
742template <
typename PDLFnT, std::size_t... I,
743 typename FnTraitsT = llvm::function_traits<PDLFnT>>
744std::enable_if_t<std::is_same<typename FnTraitsT::result_t, void>::value,
746processArgsAndInvokeRewrite(PDLFnT &fn, PatternRewriter &rewriter,
747 PDLResultList &, ArrayRef<PDLValue> values,
748 std::index_sequence<I...>) {
750 (ProcessPDLValue<
typename FnTraitsT::template arg_t<I + 1>>::processAsArg(
756template <
typename PDLFnT, std::size_t... I,
757 typename FnTraitsT = llvm::function_traits<PDLFnT>>
758std::enable_if_t<!std::is_same<typename FnTraitsT::result_t, void>::value,
760processArgsAndInvokeRewrite(PDLFnT &fn, PatternRewriter &rewriter,
761 PDLResultList &results, ArrayRef<PDLValue> values,
762 std::index_sequence<I...>) {
763 return processResults(
765 fn(rewriter, (ProcessPDLValue<
typename FnTraitsT::template arg_t<I + 1>>::
766 processAsArg(values[I]))...));
776template <
typename RewriteFnT>
777std::enable_if_t<std::is_convertible<RewriteFnT, PDLRewriteFunction>::value,
779buildRewriteFn(RewriteFnT &&rewriteFn) {
780 return std::forward<RewriteFnT>(rewriteFn);
784template <
typename RewriteFnT>
785std::enable_if_t<!std::is_convertible<RewriteFnT, PDLRewriteFunction>::value,
787buildRewriteFn(RewriteFnT &&rewriteFn) {
788 return [rewriteFn = std::forward<RewriteFnT>(rewriteFn)](
789 PatternRewriter &rewriter, PDLResultList &results,
790 ArrayRef<PDLValue> values) {
792 std::make_index_sequence<llvm::function_traits<RewriteFnT>::num_args -
794 assertArgs<RewriteFnT>(rewriter, values, argIndices);
795 return processArgsAndInvokeRewrite(rewriteFn, rewriter, results, values,
810class PDLPatternModule {
812 PDLPatternModule() =
default;
815 PDLPatternModule(OwningOpRef<ModuleOp> module)
816 : pdlModule(std::move(module)) {}
817 template <
typename... ConfigsT>
818 PDLPatternModule(OwningOpRef<ModuleOp> module, ConfigsT &&...patternConfigs)
819 : PDLPatternModule(std::move(module)) {
820 auto configSet = std::make_unique<PDLPatternConfigSet>(
821 std::forward<ConfigsT>(patternConfigs)...);
822 attachConfigToPatterns(*pdlModule, *configSet);
823 configs.emplace_back(std::move(configSet));
827 void mergeIn(PDLPatternModule &&other);
830 ModuleOp getModule() {
return pdlModule.get(); }
833 MLIRContext *
getContext() {
return getModule()->getContext(); }
860 void registerConstraintFunction(StringRef name,
861 PDLConstraintFunction constraintFn);
862 template <
typename Constra
intFnT>
863 void registerConstraintFunction(StringRef name,
864 ConstraintFnT &&constraintFn) {
865 registerConstraintFunction(name,
866 detail::pdl_function_builder::buildConstraintFn(
867 std::forward<ConstraintFnT>(constraintFn)));
894 void registerRewriteFunction(StringRef name, PDLRewriteFunction rewriteFn);
895 template <
typename RewriteFnT>
896 void registerRewriteFunction(StringRef name, RewriteFnT &&rewriteFn) {
897 registerRewriteFunction(name, detail::pdl_function_builder::buildRewriteFn(
898 std::forward<RewriteFnT>(rewriteFn)));
902 const llvm::StringMap<PDLConstraintFunction> &getConstraintFunctions()
const {
903 return constraintFunctions;
905 llvm::StringMap<PDLConstraintFunction> takeConstraintFunctions() {
906 return constraintFunctions;
909 const llvm::StringMap<PDLRewriteFunction> &getRewriteFunctions()
const {
910 return rewriteFunctions;
912 llvm::StringMap<PDLRewriteFunction> takeRewriteFunctions() {
913 return rewriteFunctions;
917 SmallVector<std::unique_ptr<PDLPatternConfigSet>> takeConfigs() {
918 return std::move(configs);
920 DenseMap<Operation *, PDLPatternConfigSet *> takeConfigMap() {
921 return std::move(configMap);
927 constraintFunctions.clear();
928 rewriteFunctions.clear();
934 void attachConfigToPatterns(ModuleOp module, PDLPatternConfigSet &configSet);
937 OwningOpRef<ModuleOp> pdlModule;
940 SmallVector<std::unique_ptr<PDLPatternConfigSet>> configs;
941 DenseMap<Operation *, PDLPatternConfigSet *> configMap;
944 llvm::StringMap<PDLConstraintFunction> constraintFunctions;
945 llvm::StringMap<PDLRewriteFunction> rewriteFunctions;
956 template <
typename T>
961class PDLResultList {};
962using PDLConstraintFunction = std::function<LogicalResult(
964using PDLRewriteFunction = std::function<LogicalResult(
967class PDLPatternModule {
969 PDLPatternModule() =
default;
971 PDLPatternModule(OwningOpRef<ModuleOp> ) {}
973 llvm_unreachable(
"Error: PDL for rewrites when PDL is not enabled");
975 void mergeIn(PDLPatternModule &&other) {}
977 template <
typename Constra
intFnT>
978 void registerConstraintFunction(StringRef name,
979 ConstraintFnT &&constraintFn) {}
980 void registerRewriteFunction(StringRef name, PDLRewriteFunction rewriteFn) {}
981 template <
typename RewriteFnT>
982 void registerRewriteFunction(StringRef name, RewriteFnT &&rewriteFn) {}
983 const llvm::StringMap<PDLConstraintFunction> &getConstraintFunctions()
const {
984 return constraintFunctions;
988 llvm::StringMap<PDLConstraintFunction> constraintFunctions;
memberIdxs push_back(ArrayAttr::get(parser.getContext(), values))
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Kind
An enumeration of the kinds of predicates.
Include the generated interface declarations.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)