Files
clang-p2996/flang/test/lib/OpenACC/TestOpenACCInterfaces.cpp
Razvan Lupusoru 01a0d212a6 [flang][acc] Implement MappableType interfaces for fir.box and fir.array (#122495)
The newly introduced MappableType interface in `acc` dialect was
primarily intended to allow variables with non-materialized storage to
be used in acc data clauses (previously everything was required to be
`pointer-like`). One motivator for this was `fir.box` since it is
possible to be passed to functions without a wrapping `fir.ref` and also
it can be generated directly via operations like `fir.embox` - and
unlike other variable representations in FIR, the underlying storage for
it does not get materialized until LLVM codegen.

The new interface is being attached to both `fir.box` and `fir.array`.
Strictly speaking, attaching to the latter is primarily for consistency
since the MappableType interface requires implementation of utilities to
compute byte size - and it made sense that a
`fir.box<fir.array<10xi32>>` and `fir.array<10xi32>` would have a
consistently computable size. This decision may be revisited as
MappableType interface evolves.

The new interface attachments are made in a new library named
`FIROpenACCSupport`. The reason for this is to avoid circular
dependencies since the implementation of this library is reusing code
from lowering of OpenACC. More specifically, the types are defined in
`FIRDialect` and `FortranLower` depends on it. Thus we cannot attach
these interfaces in `FIRDialect`.
2025-01-14 10:42:57 -08:00

86 lines
3.1 KiB
C++

//===- TestOpenACCInterfaces.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Support/LLVM.h"
#include "flang/Optimizer/Support/DataLayout.h"
using namespace mlir;
namespace {
struct TestFIROpenACCInterfaces
: public PassWrapper<TestFIROpenACCInterfaces, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFIROpenACCInterfaces)
StringRef getArgument() const final { return "test-fir-openacc-interfaces"; }
StringRef getDescription() const final {
return "Test FIR implementation of the OpenACC interfaces.";
}
void runOnOperation() override {
mlir::ModuleOp mod = getOperation();
auto datalayout =
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true);
mlir::OpBuilder builder(mod);
getOperation().walk([&](Operation *op) {
if (isa<ACC_DATA_ENTRY_OPS>(op)) {
Type typeOfVar = acc::getVar(op).getType();
llvm::errs() << "Visiting: " << *op << "\n";
auto mappableTy = dyn_cast_if_present<acc::MappableType>(typeOfVar);
if (!mappableTy) {
mappableTy =
dyn_cast_if_present<acc::MappableType>(acc::getVarType(op));
}
if (mappableTy) {
llvm::errs() << "\tMappable: " << mappableTy << "\n";
if (datalayout.has_value()) {
auto size = mappableTy.getSizeInBytes(
acc::getVar(op), acc::getBounds(op), datalayout.value());
if (size) {
llvm::errs() << "\t\tSize: " << size.value() << "\n";
}
auto offset = mappableTy.getOffsetInBytes(
acc::getVar(op), acc::getBounds(op), datalayout.value());
if (offset) {
llvm::errs() << "\t\tOffset: " << offset.value() << "\n";
}
}
builder.setInsertionPoint(op);
auto bounds = mappableTy.generateAccBounds(acc::getVar(op), builder);
if (!bounds.empty()) {
for (auto [idx, bound] : llvm::enumerate(bounds)) {
llvm::errs() << "\t\tBound[" << idx << "]: " << bound << "\n";
}
}
} else {
assert(acc::isPointerLikeType(typeOfVar) &&
"expected to be pointer-like");
llvm::errs() << "\tPointer-like: " << typeOfVar << "\n";
}
}
});
}
};
} // namespace
//===----------------------------------------------------------------------===//
// Pass Registration
//===----------------------------------------------------------------------===//
namespace fir {
namespace test {
void registerTestFIROpenACCInterfacesPass() {
PassRegistration<TestFIROpenACCInterfaces>();
}
} // namespace test
} // namespace fir