Skip to content

Commit 2918a47

Browse files
authored
[mlir][OpenMP] Annotate private vars with map_idx when needed (#116770)
This PR extends the MLIR representation for `omp.target` ops by adding a `map_idx` to `private` vars. This annotation stores the index of the map info operand corresponding to the private var. If the variable does not have a map operand, the `map_idx` attribute is either not present at all or its value is `-1`. This makes matching the private variable to its map info op easier (see #116576 for usage).
1 parent 81f544d commit 2918a47

File tree

5 files changed

+166
-40
lines changed

5 files changed

+166
-40
lines changed

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//===- MapsForPrivatizedSymbols.cpp
2-
//-----------------------------------------===//
1+
//===- MapsForPrivatizedSymbols.cpp ---------------------------------------===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
@@ -28,6 +27,7 @@
2827
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
2928
#include "flang/Optimizer/HLFIR/HLFIROps.h"
3029
#include "flang/Optimizer/OpenMP/Passes.h"
30+
3131
#include "mlir/Dialect/Func/IR/FuncOps.h"
3232
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
3333
#include "mlir/IR/BuiltinAttributes.h"
@@ -124,6 +124,8 @@ class MapsForPrivatizedSymbolsPass
124124
if (targetOp.getPrivateVars().empty())
125125
return;
126126
OperandRange privVars = targetOp.getPrivateVars();
127+
llvm::SmallVector<int64_t> privVarMapIdx;
128+
127129
std::optional<ArrayAttr> privSyms = targetOp.getPrivateSyms();
128130
SmallVector<omp::MapInfoOp, 4> mapInfoOps;
129131
for (auto [privVar, privSym] : llvm::zip_equal(privVars, *privSyms)) {
@@ -133,17 +135,25 @@ class MapsForPrivatizedSymbolsPass
133135
SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(
134136
targetOp, privatizerName);
135137
if (!privatizerNeedsMap(privatizer)) {
138+
privVarMapIdx.push_back(-1);
136139
continue;
137140
}
141+
142+
privVarMapIdx.push_back(targetOp.getMapVars().size() +
143+
mapInfoOps.size());
144+
138145
builder.setInsertionPoint(targetOp);
139146
Location loc = targetOp.getLoc();
140147
omp::MapInfoOp mapInfoOp = createMapInfo(loc, privVar, builder);
141148
mapInfoOps.push_back(mapInfoOp);
149+
142150
LLVM_DEBUG(llvm::dbgs() << "MapsForPrivatizedSymbolsPass created ->\n");
143151
LLVM_DEBUG(mapInfoOp.dump());
144152
}
145153
if (!mapInfoOps.empty()) {
146154
mapInfoOpsForTarget.insert({targetOp.getOperation(), mapInfoOps});
155+
targetOp.setPrivateMapsAttr(
156+
mlir::DenseI64ArrayAttr::get(targetOp.getContext(), privVarMapIdx));
147157
}
148158
});
149159
if (!mapInfoOpsForTarget.empty()) {

flang/test/Lower/OpenMP/DelayedPrivatization/target-private-multiple-variables.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ end subroutine target_allocatable
171171
! CHECK_SAME %[[CHAR_VAR_DESC_MAP]] -> %[[MAPPED_ARG3:.[^,]+]] :
172172
! CHECK-SAME !fir.ref<i32>, !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.ref<!fir.boxchar<1>>)
173173
! CHECK-SAME: private(
174-
! CHECK-SAME: @[[ALLOC_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[ALLOC_ARG:[^,]+]],
174+
! CHECK-SAME: @[[ALLOC_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[ALLOC_ARG:[^,]+]] [map_idx=1],
175175
! CHECK-SAME: @[[REAL_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[REAL_ARG:[^,]+]],
176176
! CHECK-SAME: @[[LB_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[LB_ARG:[^,]+]],
177-
! CHECK-SAME: @[[ARR_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[ARR_ARG:[^,]+]],
177+
! CHECK-SAME: @[[ARR_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[ARR_ARG:[^,]+]] [map_idx=2],
178178
! CHECK-SAME: @[[COMP_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[COMP_ARG:[^,]+]],
179-
! CHECK-SAME: @[[CHAR_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[CHAR_ARG:[^,]+]] :
179+
! CHECK-SAME: @[[CHAR_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[CHAR_ARG:[^,]+]] [map_idx=3] :
180180
! CHECK-SAME: !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<f32>, !fir.ref<i64>, !fir.box<!fir.array<?xf32>>, !fir.ref<complex<f32>>, !fir.boxchar<1>) {
181181
! CHECK-NOT: fir.alloca
182182
! CHECK: hlfir.declare %[[ALLOC_ARG]]

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,21 +1225,46 @@ def TargetOp : OpenMP_Op<"target", traits = [
12251225
The optional `if_expr` parameter specifies a boolean result of a conditional
12261226
check. If this value is 1 or is not provided then the target region runs on
12271227
a device, if it is 0 then the target region is executed on the host device.
1228+
1229+
The `private_maps` attribute connects `private` operands to their corresponding
1230+
`map` operands. For `private` operands that require a map, the value of the
1231+
corresponding element in the attribute is the index of the `map` operand
1232+
(relative to other `map` operands not the whole operands of the operation). For
1233+
`private` opernads that do not require a map, this value is -1 (which is omitted
1234+
from the assembly foramt printing).
12281235
}] # clausesDescription;
12291236

1237+
let arguments = !con(clausesArgs,
1238+
(ins OptionalAttr<DenseI64ArrayAttr>:$private_maps));
1239+
12301240
let builders = [
12311241
OpBuilder<(ins CArg<"const TargetOperands &">:$clauses)>
12321242
];
12331243

12341244
let extraClassDeclaration = [{
12351245
unsigned numMapBlockArgs() { return getMapVars().size(); }
1246+
1247+
mlir::Value getMappedValueForPrivateVar(unsigned privVarIdx) {
1248+
std::optional<DenseI64ArrayAttr> privateMapIdices = getPrivateMapsAttr();
1249+
1250+
if (!privateMapIdices.has_value())
1251+
return {};
1252+
1253+
int64_t mapInfoOpIdx = (*privateMapIdices)[privVarIdx];
1254+
1255+
if (mapInfoOpIdx == -1)
1256+
return {};
1257+
1258+
return getMapVars()[mapInfoOpIdx];
1259+
}
12361260
}] # clausesExtraClassDeclaration;
12371261

12381262
let assemblyFormat = clausesAssemblyFormat # [{
12391263
custom<InReductionMapPrivateRegion>(
12401264
$region, $in_reduction_vars, type($in_reduction_vars),
12411265
$in_reduction_byref, $in_reduction_syms, $map_vars, type($map_vars),
1242-
$private_vars, type($private_vars), $private_syms) attr-dict
1266+
$private_vars, type($private_vars), $private_syms, $private_maps)
1267+
attr-dict
12431268
}];
12441269

12451270
let hasVerifier = 1;

0 commit comments

Comments
 (0)