144 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir/Dialect/Arith/IR/Arith.h"10#include "mlir/Dialect/DLTI/DLTI.h"11#include "mlir/Dialect/OpenACC/OpenACC.h"12#include "mlir/IR/Builders.h"13#include "mlir/IR/BuiltinOps.h"14#include "mlir/Pass/Pass.h"15#include "mlir/Support/LLVM.h"16#include "flang/Optimizer/Dialect/FIRDialect.h"17#include "flang/Optimizer/HLFIR/HLFIRDialect.h"18#include "flang/Optimizer/HLFIR/HLFIROps.h"19#include "flang/Optimizer/Support/DataLayout.h"20 21using namespace mlir;22 23namespace {24 25struct TestFIROpenACCInterfaces26 : public PassWrapper<TestFIROpenACCInterfaces, OperationPass<ModuleOp>> {27 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFIROpenACCInterfaces)28 29 StringRef getArgument() const final { return "test-fir-openacc-interfaces"; }30 StringRef getDescription() const final {31 return "Test FIR implementation of the OpenACC interfaces.";32 }33 void getDependentDialects(::mlir::DialectRegistry ®istry) const override {34 registry.insert<fir::FIROpsDialect, hlfir::hlfirDialect,35 mlir::arith::ArithDialect, mlir::acc::OpenACCDialect,36 mlir::DLTIDialect>();37 }38 void runOnOperation() override {39 mlir::ModuleOp mod = getOperation();40 auto datalayout =41 fir::support::getOrSetMLIRDataLayout(mod, /*allowDefaultLayout=*/true);42 mlir::OpBuilder builder(mod);43 getOperation().walk([&](Operation *op) {44 if (isa<ACC_DATA_ENTRY_OPS>(op)) {45 Value var = acc::getVar(op);46 Type typeOfVar = var.getType();47 48 // Attempt to determine if the variable is mappable-like or if49 // the pointee itself is mappable-like. For example, if the variable is50 // of type !fir.ref<!fir.box<>>, we want to print both the details about51 // the !fir.ref since it is pointer-like, and about !fir.box since it52 // is mappable.53 auto mappableTy = dyn_cast_if_present<acc::MappableType>(typeOfVar);54 if (!mappableTy) {55 mappableTy =56 dyn_cast_if_present<acc::MappableType>(acc::getVarType(op));57 }58 59 llvm::errs() << "Visiting: " << *op << "\n";60 llvm::errs() << "\tVar: " << var << "\n";61 62 if (mlir::isa<acc::PointerLikeType>(typeOfVar) &&63 mlir::isa<acc::MappableType>(typeOfVar)) {64 llvm::errs() << "\tPointer-like and Mappable: " << typeOfVar << "\n";65 } else if (mlir::isa<acc::PointerLikeType>(typeOfVar)) {66 llvm::errs() << "\tPointer-like: " << typeOfVar << "\n";67 } else {68 assert(69 mlir::isa<acc::MappableType>(typeOfVar) && "expected mappable");70 llvm::errs() << "\tMappable: " << typeOfVar << "\n";71 }72 73 if (auto ptrTy = dyn_cast_if_present<acc::PointerLikeType>(typeOfVar)) {74 // If the pointee is not mappable, print details about it. Otherwise,75 // we defer to the mappable printing below to print those details.76 if (!mappableTy) {77 acc::VariableTypeCategory typeCategory =78 ptrTy.getPointeeTypeCategory(79 cast<TypedValue<acc::PointerLikeType>>(var),80 acc::getVarType(op));81 llvm::errs() << "\t\tType category: " << typeCategory << "\n";82 }83 }84 85 if (mappableTy) {86 acc::VariableTypeCategory typeCategory =87 mappableTy.getTypeCategory(var);88 llvm::errs() << "\t\tType category: " << typeCategory << "\n";89 90 if (datalayout.has_value()) {91 auto size = mappableTy.getSizeInBytes(92 acc::getVar(op), acc::getBounds(op), datalayout.value());93 if (size) {94 llvm::errs() << "\t\tSize: " << size.value() << "\n";95 }96 auto offset = mappableTy.getOffsetInBytes(97 acc::getVar(op), acc::getBounds(op), datalayout.value());98 if (offset) {99 llvm::errs() << "\t\tOffset: " << offset.value() << "\n";100 }101 }102 103 llvm::errs() << "\t\tHas unknown dimensions: "104 << (mappableTy.hasUnknownDimensions() ? "true" : "false")105 << "\n";106 107 if (auto declareOp =108 dyn_cast_if_present<hlfir::DeclareOp>(var.getDefiningOp())) {109 llvm::errs() << "\t\tShape: " << declareOp.getShape() << "\n";110 }111 112 builder.setInsertionPoint(op);113 auto bounds = mappableTy.generateAccBounds(acc::getVar(op), builder);114 if (!bounds.empty()) {115 for (auto [idx, bound] : llvm::enumerate(bounds)) {116 if (auto boundOp = dyn_cast_if_present<acc::DataBoundsOp>(117 bound.getDefiningOp())) {118 llvm::errs() << "\t\tBound[" << idx << "]: " << bound << "\n";119 llvm::errs()120 << "\t\tLower bound: " << boundOp.getLowerbound() << "\n";121 llvm::errs()122 << "\t\tUpper bound: " << boundOp.getUpperbound() << "\n";123 }124 }125 }126 }127 }128 });129 }130};131} // namespace132 133//===----------------------------------------------------------------------===//134// Pass Registration135//===----------------------------------------------------------------------===//136 137namespace fir {138namespace test {139void registerTestFIROpenACCInterfacesPass() {140 PassRegistration<TestFIROpenACCInterfaces>();141}142} // namespace test143} // namespace fir144