MLIR  19.0.0git
DecomposeCallGraphTypes.h
Go to the documentation of this file.
1 //===- DecomposeCallGraphTypes.h - CG type decompositions -------*- C++ -*-===//
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 // Conversion patterns for decomposing types along call graph edges. That is,
10 // decomposing types for calls, returns, and function args.
11 //
12 // TODO: Make this handle dialect-defined functions, calls, and returns.
13 // Currently, the generic interfaces aren't sophisticated enough for the
14 // types of mutations that we are doing here.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef MLIR_DIALECT_FUNC_TRANSFORMS_DECOMPOSECALLGRAPHTYPES_H
19 #define MLIR_DIALECT_FUNC_TRANSFORMS_DECOMPOSECALLGRAPHTYPES_H
20 
22 #include <optional>
23 
24 namespace mlir {
25 
26 /// This class provides a hook that expands one Value into multiple Value's,
27 /// with a TypeConverter-inspired callback registration mechanism.
28 ///
29 /// For folks that are familiar with the dialect conversion framework /
30 /// TypeConverter, this is effectively the inverse of a source/argument
31 /// materialization. A target materialization is not what we want here because
32 /// it always produces a single Value, but in this case the whole point is to
33 /// decompose a Value into multiple Value's.
34 ///
35 /// The reason we need this inverse is easily understood by looking at what we
36 /// need to do for decomposing types for a return op. When converting a return
37 /// op, the dialect conversion framework will give the list of converted
38 /// operands, and will ensure that each converted operand, even if it expanded
39 /// into multiple types, is materialized as a single result. We then need to
40 /// undo that materialization to a single result, which we do with the
41 /// decomposeValue hooks registered on this object.
42 ///
43 /// TODO: Eventually, the type conversion infra should have this hook built-in.
44 /// See
45 /// https://llvm.discourse.group/t/extending-type-conversion-infrastructure/779/2
47 public:
48  /// This method tries to decompose a value of a certain type using provided
49  /// decompose callback functions. If it is unable to do so, the original value
50  /// is returned.
53 
54  /// This method registers a callback function that will be called to decompose
55  /// a value of a certain type into 0, 1, or multiple values.
56  template <typename FnT, typename T = typename llvm::function_traits<
57  std::decay_t<FnT>>::template arg_t<2>>
58  void addDecomposeValueConversion(FnT &&callback) {
59  decomposeValueConversions.emplace_back(
60  wrapDecomposeValueConversionCallback<T>(std::forward<FnT>(callback)));
61  }
62 
63 private:
64  using DecomposeValueConversionCallFn =
65  std::function<std::optional<LogicalResult>(
67 
68  /// Generate a wrapper for the given decompose value conversion callback.
69  template <typename T, typename FnT>
70  DecomposeValueConversionCallFn
71  wrapDecomposeValueConversionCallback(FnT &&callback) {
72  return
73  [callback = std::forward<FnT>(callback)](
74  OpBuilder &builder, Location loc, Type type, Value value,
75  SmallVectorImpl<Value> &newValues) -> std::optional<LogicalResult> {
76  if (T derivedType = dyn_cast<T>(type))
77  return callback(builder, loc, derivedType, value, newValues);
78  return std::nullopt;
79  };
80  }
81 
82  SmallVector<DecomposeValueConversionCallFn, 2> decomposeValueConversions;
83 };
84 
85 /// Populates the patterns needed to drive the conversion process for
86 /// decomposing call graph types with the given `ValueDecomposer`.
87 void populateDecomposeCallGraphTypesPatterns(MLIRContext *context,
88  TypeConverter &typeConverter,
89  ValueDecomposer &decomposer,
90  RewritePatternSet &patterns);
91 
92 } // namespace mlir
93 
94 #endif // MLIR_DIALECT_FUNC_TRANSFORMS_DECOMPOSECALLGRAPHTYPES_H
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class helps build Operations.
Definition: Builders.h:209
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides a hook that expands one Value into multiple Value's, with a TypeConverter-inspire...
void addDecomposeValueConversion(FnT &&callback)
This method registers a callback function that will be called to decompose a value of a certain type ...
void decomposeValue(OpBuilder &, Location, Type, Value, SmallVectorImpl< Value > &)
This method tries to decompose a value of a certain type using provided decompose callback functions.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Include the generated interface declarations.
void populateDecomposeCallGraphTypesPatterns(MLIRContext *context, TypeConverter &typeConverter, ValueDecomposer &decomposer, RewritePatternSet &patterns)
Populates the patterns needed to drive the conversion process for decomposing call graph types with t...