My Project
DependenceAnalysis.h
Go to the documentation of this file.
1 //===- DependenceAnalysis.h - Dependence analysis on SSA views --*- C++ -*-===//
2 //
3 // Part of the MLIR 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 #ifndef MLIR_DIALECT_LINALG_ANALYSIS_DEPENDENCEANALYSIS_H_
10 #define MLIR_DIALECT_LINALG_ANALYSIS_DEPENDENCEANALYSIS_H_
11 
12 #include "mlir/IR/Builders.h"
13 #include "mlir/IR/OpDefinition.h"
14 
15 namespace mlir {
16 class FuncOp;
17 
18 namespace linalg {
19 
20 class LinalgOp;
21 
28 class Aliases {
29 public:
31  bool alias(Value v1, Value v2) { return find(v1) == find(v2); }
32 
33 private:
37  Value find(Value v);
38 
39  DenseMap<Value, Value> aliases;
40 };
41 
45 public:
46  struct LinalgOpView {
49  };
51  // dependentOpView may be either:
52  // 1. src in the case of dependencesIntoGraphs.
53  // 2. dst in the case of dependencesFromDstGraphs.
55  // View in the op that is used to index in the graph:
56  // 1. src in the case of dependencesFromDstGraphs.
57  // 2. dst in the case of dependencesIntoGraphs.
59  };
62  using dependence_iterator = LinalgDependences::const_iterator;
64 
65  enum DependenceType { RAR = 0, RAW, WAR, WAW, NumTypes };
66 
67  // Builds a linalg dependence graph for the ops of type LinalgOp under `f`.
68  static LinalgDependenceGraph buildDependenceGraph(Aliases &aliases, FuncOp f);
70 
72  dependence_range getDependencesFrom(Operation *src, DependenceType dt) const;
73  dependence_range getDependencesFrom(LinalgOp src, DependenceType dt) const;
74 
76  dependence_range getDependencesInto(Operation *dst, DependenceType dt) const;
77  dependence_range getDependencesInto(LinalgOp dst, DependenceType dt) const;
78 
84  findCoveringDependences(LinalgOp srcLinalgOp, LinalgOp dstLinalgOp) const;
85 
89  SmallVector<Operation *, 8> findCoveringReads(LinalgOp srcLinalgOp,
90  LinalgOp dstLinalgOp,
91  Value view) const;
92 
96  SmallVector<Operation *, 8> findCoveringWrites(LinalgOp srcLinalgOp,
97  LinalgOp dstLinalgOp,
98  Value view) const;
99 
100 private:
101  // Keep dependences in both directions, this is not just a performance gain
102  // but it also reduces usage errors.
103  // Dependence information is stored as a map of:
104  // (source operation -> LinalgDependenceGraphElem)
105  DependenceGraph dependencesFromGraphs[DependenceType::NumTypes];
106  // Reverse dependence information is stored as a map of:
107  // (destination operation -> LinalgDependenceGraphElem)
108  DependenceGraph dependencesIntoGraphs[DependenceType::NumTypes];
109 
112  void addDependencesBetween(LinalgOp src, LinalgOp dst);
113 
114  // Adds an new dependence unit in the proper graph.
115  // Uses std::pair to keep operations and view together and avoid usage errors
116  // related to src/dst and producer/consumer terminology in the context of
117  // dependences.
118  void addDependenceElem(DependenceType dt, LinalgOpView indexingOpView,
119  LinalgOpView dependentOpView);
120 
123  findOperationsWithCoveringDependences(LinalgOp srcLinalgOp,
124  LinalgOp dstLinalgOp, Value view,
125  ArrayRef<DependenceType> types) const;
126 
127  Aliases &aliases;
128  SmallVector<Operation *, 8> linalgOps;
129  DenseMap<Operation *, unsigned> linalgOpPositions;
130 };
131 } // namespace linalg
132 } // namespace mlir
133 
134 #endif // MLIR_DIALECT_LINALG_ANALYSIS_DEPENDENCEANALYSIS_H_
Definition: InferTypeOpInterface.cpp:20
Definition: Operation.h:27
Definition: LLVM.h:48
Definition: DependenceAnalysis.h:46
Definition: LLVM.h:37
Definition: Function.h:32
LinalgDependences::const_iterator dependence_iterator
Definition: DependenceAnalysis.h:62
Definition: DependenceAnalysis.h:44
Definition: DependenceAnalysis.h:28
Operation * op
Definition: DependenceAnalysis.h:47
Definition: Value.h:38
Definition: LLVM.h:35
Definition: LLVM.h:50
Definition: DependenceAnalysis.h:65
DependenceType
Definition: DependenceAnalysis.h:65
ValueBuilder< ViewOp > view
Definition: Intrinsics.h:215
Value view
Definition: DependenceAnalysis.h:48
Value indexingView
Definition: DependenceAnalysis.h:58
bool alias(Value v1, Value v2)
Returns true if v1 and v2 alias.
Definition: DependenceAnalysis.h:31
LinalgOpView dependentOpView
Definition: DependenceAnalysis.h:54