Skip to content

Commit 1fd1d63

Browse files
committed
[MLIR][OpenMP] Add a new AutomapToTargetData conversion pass in FIR (#153048)
Add a new AutomapToTargetData pass. This gathers the declare target enter variables which have the AUTOMAP modifier. And adds omp.declare_target_enter/exit mapping directives for fir.alloca and fir.free oeprations on the AUTOMAP enabled variables. Automap Ref: OpenMP 6.0 section 7.9.7.
1 parent 09267f6 commit 1fd1d63

File tree

6 files changed

+271
-6
lines changed

6 files changed

+271
-6
lines changed

flang/include/flang/Optimizer/OpenMP/Passes.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,15 @@ def SimdOnlyPass : Pass<"omp-simd-only", "mlir::ModuleOp"> {
117117
let dependentDialects = ["mlir::omp::OpenMPDialect"];
118118
}
119119

120+
def AutomapToTargetDataPass
121+
: Pass<"omp-automap-to-target-data", "::mlir::ModuleOp"> {
122+
let summary = "Insert OpenMP target data operations for AUTOMAP variables";
123+
let description = [{
124+
Inserts `omp.target_enter_data` and `omp.target_exit_data` operations to
125+
map variables marked with the `AUTOMAP` modifier when their allocation
126+
or deallocation is detected in the FIR.
127+
}];
128+
let dependentDialects = ["mlir::omp::OpenMPDialect"];
129+
}
130+
120131
#endif //FORTRAN_OPTIMIZER_OPENMP_PASSES
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//===- AutomapToTargetData.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 "flang/Optimizer/Builder/DirectivesCommon.h"
10+
#include "flang/Optimizer/Builder/FIRBuilder.h"
11+
#include "flang/Optimizer/Builder/HLFIRTools.h"
12+
#include "flang/Optimizer/Dialect/FIROps.h"
13+
#include "flang/Optimizer/Dialect/FIRType.h"
14+
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
15+
#include "flang/Optimizer/HLFIR/HLFIROps.h"
16+
17+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
18+
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
19+
#include "mlir/IR/BuiltinAttributes.h"
20+
#include "mlir/IR/Operation.h"
21+
#include "mlir/Pass/Pass.h"
22+
23+
#include "llvm/Frontend/OpenMP/OMPConstants.h"
24+
25+
namespace flangomp {
26+
#define GEN_PASS_DEF_AUTOMAPTOTARGETDATAPASS
27+
#include "flang/Optimizer/OpenMP/Passes.h.inc"
28+
} // namespace flangomp
29+
30+
using namespace mlir;
31+
32+
namespace {
33+
class AutomapToTargetDataPass
34+
: public flangomp::impl::AutomapToTargetDataPassBase<
35+
AutomapToTargetDataPass> {
36+
37+
// Returns true if the variable has a dynamic size and therefore requires
38+
// bounds operations to describe its extents.
39+
inline bool needsBoundsOps(mlir::Value var) {
40+
assert(mlir::isa<mlir::omp::PointerLikeType>(var.getType()) &&
41+
"only pointer like types expected");
42+
mlir::Type t = fir::unwrapRefType(var.getType());
43+
if (mlir::Type inner = fir::dyn_cast_ptrOrBoxEleTy(t))
44+
return fir::hasDynamicSize(inner);
45+
return fir::hasDynamicSize(t);
46+
}
47+
48+
// Generate MapBoundsOp operations for the variable if required.
49+
inline void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
50+
llvm::SmallVectorImpl<mlir::Value> &boundsOps) {
51+
mlir::Location loc = var.getLoc();
52+
fir::factory::AddrAndBoundsInfo info =
53+
fir::factory::getDataOperandBaseAddr(builder, var,
54+
/*isOptional=*/false, loc);
55+
fir::ExtendedValue exv =
56+
hlfir::translateToExtendedValue(loc, builder, hlfir::Entity{info.addr},
57+
/*contiguousHint=*/true)
58+
.first;
59+
llvm::SmallVector<mlir::Value> tmp =
60+
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
61+
mlir::omp::MapBoundsType>(
62+
builder, info, exv, /*dataExvIsAssumedSize=*/false, loc);
63+
llvm::append_range(boundsOps, tmp);
64+
}
65+
66+
void findRelatedAllocmemFreemem(fir::AddrOfOp addressOfOp,
67+
llvm::DenseSet<fir::StoreOp> &allocmems,
68+
llvm::DenseSet<fir::LoadOp> &freemems) {
69+
assert(addressOfOp->hasOneUse() && "op must have single use");
70+
71+
auto declaredRef =
72+
cast<hlfir::DeclareOp>(*addressOfOp->getUsers().begin())->getResult(0);
73+
74+
for (Operation *refUser : declaredRef.getUsers()) {
75+
if (auto storeOp = dyn_cast<fir::StoreOp>(refUser))
76+
if (auto emboxOp = storeOp.getValue().getDefiningOp<fir::EmboxOp>())
77+
if (auto allocmemOp =
78+
emboxOp.getOperand(0).getDefiningOp<fir::AllocMemOp>())
79+
allocmems.insert(storeOp);
80+
81+
if (auto loadOp = dyn_cast<fir::LoadOp>(refUser))
82+
for (Operation *loadUser : loadOp.getResult().getUsers())
83+
if (auto boxAddrOp = dyn_cast<fir::BoxAddrOp>(loadUser))
84+
for (Operation *boxAddrUser : boxAddrOp.getResult().getUsers())
85+
if (auto freememOp = dyn_cast<fir::FreeMemOp>(boxAddrUser))
86+
freemems.insert(loadOp);
87+
}
88+
}
89+
90+
void runOnOperation() override {
91+
ModuleOp module = getOperation()->getParentOfType<ModuleOp>();
92+
if (!module)
93+
module = dyn_cast<ModuleOp>(getOperation());
94+
if (!module)
95+
return;
96+
97+
// Build FIR builder for helper utilities.
98+
fir::KindMapping kindMap = fir::getKindMapping(module);
99+
fir::FirOpBuilder builder{module, std::move(kindMap)};
100+
101+
// Collect global variables with AUTOMAP flag.
102+
llvm::DenseSet<fir::GlobalOp> automapGlobals;
103+
module.walk([&](fir::GlobalOp globalOp) {
104+
if (auto iface =
105+
dyn_cast<omp::DeclareTargetInterface>(globalOp.getOperation()))
106+
if (iface.isDeclareTarget() && iface.getDeclareTargetAutomap() &&
107+
iface.getDeclareTargetDeviceType() !=
108+
omp::DeclareTargetDeviceType::host)
109+
automapGlobals.insert(globalOp);
110+
});
111+
112+
auto addMapInfo = [&](auto globalOp, auto memOp) {
113+
builder.setInsertionPointAfter(memOp);
114+
SmallVector<Value> bounds;
115+
if (needsBoundsOps(memOp.getMemref()))
116+
genBoundsOps(builder, memOp.getMemref(), bounds);
117+
118+
omp::TargetEnterExitUpdateDataOperands clauses;
119+
mlir::omp::MapInfoOp mapInfo = mlir::omp::MapInfoOp::create(
120+
builder, memOp.getLoc(), memOp.getMemref().getType(),
121+
memOp.getMemref(),
122+
TypeAttr::get(fir::unwrapRefType(memOp.getMemref().getType())),
123+
builder.getIntegerAttr(
124+
builder.getIntegerType(64, false),
125+
static_cast<unsigned>(
126+
isa<fir::StoreOp>(memOp)
127+
? llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO
128+
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_DELETE)),
129+
builder.getAttr<omp::VariableCaptureKindAttr>(
130+
omp::VariableCaptureKind::ByCopy),
131+
/*var_ptr_ptr=*/mlir::Value{},
132+
/*members=*/SmallVector<Value>{},
133+
/*members_index=*/ArrayAttr{}, bounds,
134+
/*mapperId=*/mlir::FlatSymbolRefAttr(), globalOp.getSymNameAttr(),
135+
builder.getBoolAttr(false));
136+
clauses.mapVars.push_back(mapInfo);
137+
isa<fir::StoreOp>(memOp)
138+
? builder.create<omp::TargetEnterDataOp>(memOp.getLoc(), clauses)
139+
: builder.create<omp::TargetExitDataOp>(memOp.getLoc(), clauses);
140+
};
141+
142+
for (fir::GlobalOp globalOp : automapGlobals) {
143+
if (auto uses = globalOp.getSymbolUses(module.getOperation())) {
144+
llvm::DenseSet<fir::StoreOp> allocmemStores;
145+
llvm::DenseSet<fir::LoadOp> freememLoads;
146+
for (auto &x : *uses)
147+
if (auto addrOp = dyn_cast<fir::AddrOfOp>(x.getUser()))
148+
findRelatedAllocmemFreemem(addrOp, allocmemStores, freememLoads);
149+
150+
for (auto storeOp : allocmemStores)
151+
addMapInfo(globalOp, storeOp);
152+
153+
for (auto loadOp : freememLoads)
154+
addMapInfo(globalOp, loadOp);
155+
}
156+
}
157+
}
158+
};
159+
} // namespace

flang/lib/Optimizer/OpenMP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
22

33
add_flang_library(FlangOpenMPTransforms
4+
AutomapToTargetData.cpp
45
DoConcurrentConversion.cpp
56
FunctionFiltering.cpp
67
GenericLoopConversion.cpp

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,13 @@ void createOpenMPFIRPassPipeline(mlir::PassManager &pm,
319319
pm.addPass(flangomp::createDoConcurrentConversionPass(
320320
opts.doConcurrentMappingKind == DoConcurrentMappingKind::DCMK_Device));
321321

322-
// The MapsForPrivatizedSymbols pass needs to run before
323-
// MapInfoFinalizationPass because the former creates new
324-
// MapInfoOp instances, typically for descriptors.
325-
// MapInfoFinalizationPass adds MapInfoOp instances for the descriptors
326-
// underlying data which is necessary to access the data on the offload
327-
// target device.
322+
// The MapsForPrivatizedSymbols and AutomapToTargetDataPass pass need to run
323+
// before MapInfoFinalizationPass because they create new MapInfoOp
324+
// instances, typically for descriptors. MapInfoFinalizationPass adds
325+
// MapInfoOp instances for the descriptors underlying data which is necessary
326+
// to access the data on the offload target device.
328327
pm.addPass(flangomp::createMapsForPrivatizedSymbolsPass());
328+
pm.addPass(flangomp::createAutomapToTargetDataPass());
329329
pm.addPass(flangomp::createMapInfoFinalizationPass());
330330
pm.addPass(flangomp::createMarkDeclareTargetPass());
331331
pm.addPass(flangomp::createGenericLoopConversionPass());
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: fir-opt --omp-automap-to-target-data %s | FileCheck %s
2+
// Test OMP AutomapToTargetData pass.
3+
4+
module {
5+
fir.global
6+
@_QMtestEarr{omp.declare_target = #omp.declaretarget<device_type = (any),
7+
capture_clause = (enter), automap = true>} target
8+
: !fir.box<!fir.heap<!fir.array<?xi32>>>
9+
10+
func.func @automap() {
11+
%c0 = arith.constant 0 : index
12+
%c10 = arith.constant 10 : i32
13+
%addr = fir.address_of(@_QMtestEarr) : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
14+
%decl:2 = hlfir.declare %addr {fortran_attrs = #fir.var_attrs<allocatable, target>, uniq_name = "_QMtestEarr"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>)
15+
%idx = fir.convert %c10 : (i32) -> index
16+
%cond = arith.cmpi sgt, %idx, %c0 : index
17+
%n = arith.select %cond, %idx, %c0 : index
18+
%mem = fir.allocmem !fir.array<?xi32>, %n {fir.must_be_heap = true}
19+
%shape = fir.shape %n : (index) -> !fir.shape<1>
20+
%box = fir.embox %mem(%shape) : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xi32>>>
21+
fir.store %box to %decl#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
22+
%ld = fir.load %decl#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
23+
%base = fir.box_addr %ld : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
24+
fir.freemem %base : !fir.heap<!fir.array<?xi32>>
25+
%undef = fir.zero_bits !fir.heap<!fir.array<?xi32>>
26+
%sh0 = fir.shape %c0 : (index) -> !fir.shape<1>
27+
%empty = fir.embox %undef(%sh0) : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xi32>>>
28+
fir.store %empty to %decl#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
29+
return
30+
}
31+
}
32+
33+
// CHECK: fir.global @[[AUTOMAP:.*]] {{{.*}} automap = true
34+
// CHECK-LABEL: func.func @automap()
35+
// CHECK: %[[AUTOMAP_ADDR:.*]] = fir.address_of(@[[AUTOMAP]])
36+
// CHECK: %[[AUTOMAP_DECL:.*]]:2 = hlfir.declare %[[AUTOMAP_ADDR]]
37+
// CHECK: %[[ALLOC_MEM:.*]] = fir.allocmem
38+
// CHECK-NEXT: fir.shape
39+
// CHECK-NEXT: %[[ARR_BOXED:.*]] = fir.embox %[[ALLOC_MEM]]
40+
// CHECK-NEXT: fir.store %[[ARR_BOXED]]
41+
// CHECK-NEXT: %[[ARR_BOXED_LOADED:.*]] = fir.load %[[AUTOMAP_DECL]]#0
42+
// CHECK-NEXT: %[[ARR_HEAP_PTR:.*]] = fir.box_addr %[[ARR_BOXED_LOADED]]
43+
// CHECK-NEXT: %[[DIM0:.*]] = arith.constant 0 : index
44+
// CHECK-NEXT: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[ARR_BOXED_LOADED]], %[[DIM0]]
45+
// CHECK-NEXT: %[[ONE:.*]] = arith.constant 1 : index
46+
// CHECK-NEXT: %[[ZERO:.*]] = arith.constant 0 : index
47+
// CHECK-NEXT: %[[BOX_DIMS2:.*]]:3 = fir.box_dims %[[ARR_BOXED_LOADED]], %[[ZERO]]
48+
// CHECK-NEXT: %[[LOWER_BOUND:.*]] = arith.constant 0 : index
49+
// CHECK-NEXT: %[[UPPER_BOUND:.*]] = arith.subi %[[BOX_DIMS2]]#1, %[[ONE]] : index
50+
// CHECK-NEXT: omp.map.bounds lower_bound(%[[LOWER_BOUND]] : index) upper_bound(%[[UPPER_BOUND]] : index) extent(%[[BOX_DIMS2]]#1 : index) stride(%[[BOX_DIMS2]]#2 : index) start_idx(%[[BOX_DIMS]]#0 : index) {stride_in_bytes = true}
51+
// CHECK-NEXT: arith.muli %[[BOX_DIMS2]]#2, %[[BOX_DIMS2]]#1 : index
52+
// CHECK-NEXT: %[[MAP_INFO:.*]] = omp.map.info var_ptr(%[[AUTOMAP_DECL]]#0 {{.*}} map_clauses(to) capture(ByCopy)
53+
// CHECK-NEXT: omp.target_enter_data map_entries(%[[MAP_INFO]]
54+
// CHECK: %[[LOAD:.*]] = fir.load %[[AUTOMAP_DECL]]#0
55+
// CHECK: %[[EXIT_MAP:.*]] = omp.map.info var_ptr(%[[AUTOMAP_DECL]]#0 {{.*}} map_clauses(delete) capture(ByCopy)
56+
// CHECK-NEXT: omp.target_exit_data map_entries(%[[EXIT_MAP]]
57+
// CHECK-NEXT: %[[BOXADDR:.*]] = fir.box_addr %[[LOAD]]
58+
// CHECK-NEXT: fir.freemem %[[BOXADDR]]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
!Offloading test for AUTOMAP modifier in declare target enter
2+
! REQUIRES: flang, amdgpu
3+
4+
program automap_program
5+
use iso_c_binding, only: c_loc
6+
use omp_lib, only: omp_get_default_device, omp_target_is_present
7+
integer, parameter :: N = 10
8+
integer :: i
9+
integer, allocatable, target :: automap_array(:)
10+
!$omp declare target enter(automap:automap_array)
11+
12+
! false since the storage is not present even though the descriptor is present
13+
write (*, *) omp_target_is_present(c_loc(automap_array), omp_get_default_device())
14+
! CHECK: 0
15+
16+
allocate (automap_array(N))
17+
! true since the storage should be allocated and reference count incremented by the allocate
18+
write (*, *) omp_target_is_present(c_loc(automap_array), omp_get_default_device())
19+
! CHECK: 1
20+
21+
! since storage is present this should not be a runtime error
22+
!$omp target teams loop
23+
do i = 1, N
24+
automap_array(i) = i
25+
end do
26+
27+
!$omp target update from(automap_array)
28+
write (*, *) automap_array
29+
! CHECK: 1 2 3 4 5 6 7 8 9 10
30+
31+
deallocate (automap_array)
32+
33+
! automap_array should have it's storage unmapped on device here
34+
write (*, *) omp_target_is_present(c_loc(automap_array), omp_get_default_device())
35+
! CHECK: 0
36+
end program

0 commit comments

Comments
 (0)