xref: /llvm-project/flang/test/lib/OpenACC/TestOpenACCInterfaces.cpp (revision 01a0d212a64919205734706d929db37e503c35ce)
1 //===- TestOpenACCInterfaces.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 
9 #include "mlir/Dialect/OpenACC/OpenACC.h"
10 #include "mlir/IR/Builders.h"
11 #include "mlir/IR/BuiltinOps.h"
12 #include "mlir/Pass/Pass.h"
13 #include "mlir/Support/LLVM.h"
14 #include "flang/Optimizer/Support/DataLayout.h"
15 
16 using namespace mlir;
17 
18 namespace {
19 
20 struct TestFIROpenACCInterfaces
21     : public PassWrapper<TestFIROpenACCInterfaces, OperationPass<ModuleOp>> {
22   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFIROpenACCInterfaces)
23 
24   StringRef getArgument() const final { return "test-fir-openacc-interfaces"; }
25   StringRef getDescription() const final {
26     return "Test FIR implementation of the OpenACC interfaces.";
27   }
28   void runOnOperation() override {
29     mlir::ModuleOp mod = getOperation();
30     auto datalayout =
31         fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
32     mlir::OpBuilder builder(mod);
33     getOperation().walk([&](Operation *op) {
34       if (isa<ACC_DATA_ENTRY_OPS>(op)) {
35         Type typeOfVar = acc::getVar(op).getType();
36         llvm::errs() << "Visiting: " << *op << "\n";
37         auto mappableTy = dyn_cast_if_present<acc::MappableType>(typeOfVar);
38         if (!mappableTy) {
39           mappableTy =
40               dyn_cast_if_present<acc::MappableType>(acc::getVarType(op));
41         }
42         if (mappableTy) {
43           llvm::errs() << "\tMappable: " << mappableTy << "\n";
44           if (datalayout.has_value()) {
45             auto size = mappableTy.getSizeInBytes(
46                 acc::getVar(op), acc::getBounds(op), datalayout.value());
47             if (size) {
48               llvm::errs() << "\t\tSize: " << size.value() << "\n";
49             }
50             auto offset = mappableTy.getOffsetInBytes(
51                 acc::getVar(op), acc::getBounds(op), datalayout.value());
52             if (offset) {
53               llvm::errs() << "\t\tOffset: " << offset.value() << "\n";
54             }
55           }
56 
57           builder.setInsertionPoint(op);
58           auto bounds = mappableTy.generateAccBounds(acc::getVar(op), builder);
59           if (!bounds.empty()) {
60             for (auto [idx, bound] : llvm::enumerate(bounds)) {
61               llvm::errs() << "\t\tBound[" << idx << "]: " << bound << "\n";
62             }
63           }
64         } else {
65           assert(acc::isPointerLikeType(typeOfVar) &&
66               "expected to be pointer-like");
67           llvm::errs() << "\tPointer-like: " << typeOfVar << "\n";
68         }
69       }
70     });
71   }
72 };
73 } // namespace
74 
75 //===----------------------------------------------------------------------===//
76 // Pass Registration
77 //===----------------------------------------------------------------------===//
78 
79 namespace fir {
80 namespace test {
81 void registerTestFIROpenACCInterfacesPass() {
82   PassRegistration<TestFIROpenACCInterfaces>();
83 }
84 } // namespace test
85 } // namespace fir
86