MLIR  20.0.0git
DataLayoutAnalysis.cpp
Go to the documentation of this file.
1 //===- DataLayoutAnalysis.cpp ---------------------------------------------===//
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 
10 #include "mlir/IR/BuiltinOps.h"
11 #include "mlir/IR/Operation.h"
13 #include "mlir/Support/LLVM.h"
14 #include <memory>
15 
16 using namespace mlir;
17 
19  : defaultLayout(std::make_unique<DataLayout>(DataLayoutOpInterface())) {
20  // Construct a DataLayout if possible from the op.
21  auto computeLayout = [this](Operation *op) {
22  if (auto iface = dyn_cast<DataLayoutOpInterface>(op))
23  layouts[op] = std::make_unique<DataLayout>(iface);
24  if (auto module = dyn_cast<ModuleOp>(op))
25  layouts[op] = std::make_unique<DataLayout>(module);
26  };
27 
28  // Compute layouts for both ancestors and descendants.
29  root->walk(computeLayout);
30  for (Operation *ancestor = root->getParentOp(); ancestor != nullptr;
31  ancestor = ancestor->getParentOp()) {
32  computeLayout(ancestor);
33  }
34 }
35 
37  for (Operation *ancestor = operation->getParentOp(); ancestor != nullptr;
38  ancestor = ancestor->getParentOp()) {
39  auto it = layouts.find(ancestor);
40  if (it != layouts.end())
41  return *it->getSecond();
42  }
43 
44  // Fallback to the default layout.
45  return *defaultLayout;
46 }
47 
49  auto it = layouts.find(operation);
50  if (it != layouts.end())
51  return *it->getSecond();
52  return getAbove(operation);
53 }
const DataLayout & getAtOrAbove(Operation *operation) const
Returns the data layout specified by the given operation or its closest ancestor that can specify one...
const DataLayout & getAbove(Operation *operation) const
Returns the data layout active at the given operation, that is the data layout specified by the close...
DataLayoutAnalysis(Operation *root)
Constructs the data layouts.
The main mechanism for performing data layout queries.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition: Operation.h:793
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition: Operation.h:234
Include the generated interface declarations.