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