MLIR 23.0.0git
OpenACCUtilsCG.cpp
Go to the documentation of this file.
1//===- OpenACCUtilsCG.cpp - OpenACC Code Generation Utilities -------------===//
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// This file implements utility functions for OpenACC code generation.
10//
11//===----------------------------------------------------------------------===//
12
14#include "mlir/IR/BuiltinOps.h"
15
16namespace mlir {
17namespace acc {
18
19std::optional<DataLayout> getDataLayout(Operation *op, bool allowDefault) {
20 if (!op)
21 return std::nullopt;
22
23 // Walk up the parent chain to find the nearest operation with an explicit
24 // data layout spec. Check ModuleOp explicitly since it does not actually
25 // implement DataLayoutOpInterface as a trait (it just has the same methods).
26 Operation *current = op;
27 while (current) {
28 // Check for ModuleOp with explicit data layout spec
29 if (auto mod = llvm::dyn_cast<ModuleOp>(current)) {
30 if (mod.getDataLayoutSpec())
31 return DataLayout(mod);
32 } else if (auto dataLayoutOp =
33 llvm::dyn_cast<DataLayoutOpInterface>(current)) {
34 // Check other DataLayoutOpInterface implementations
35 if (dataLayoutOp.getDataLayoutSpec())
36 return DataLayout(dataLayoutOp);
37 }
38 current = current->getParentOp();
39 }
40
41 // No explicit data layout found; return default if allowed
42 if (allowDefault) {
43 // Check if op itself is a ModuleOp
44 if (auto mod = llvm::dyn_cast<ModuleOp>(op))
45 return DataLayout(mod);
46 // Otherwise check parents
47 if (auto mod = op->getParentOfType<ModuleOp>())
48 return DataLayout(mod);
49 }
50
51 return std::nullopt;
52}
53
54} // namespace acc
55} // namespace mlir
The main mechanism for performing data layout queries.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:234
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:238
std::optional< DataLayout > getDataLayout(Operation *op, bool allowDefault=true)
Get the data layout for an operation.
Include the generated interface declarations.