Skip to content

Commit d13edc9

Browse files
More fixes based on some out-of-tree end-to-end tests
1 parent 17a07ca commit d13edc9

File tree

5 files changed

+99
-25
lines changed

5 files changed

+99
-25
lines changed

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
#include <type_traits>
3939

4040
#define DEBUG_TYPE "omp-maps-for-privatized-symbols"
41-
41+
#define PDBGS() (llvm::dbgs() << "[" << DEBUG_TYPE << "]: ")
4242
namespace flangomp {
4343
#define GEN_PASS_DEF_MAPSFORPRIVATIZEDSYMBOLSPASS
4444
#include "flang/Optimizer/OpenMP/Passes.h.inc"
4545
} // namespace flangomp
46+
4647
using namespace mlir;
48+
4749
namespace {
4850
class MapsForPrivatizedSymbolsPass
4951
: public flangomp::impl::MapsForPrivatizedSymbolsPassBase<
@@ -60,14 +62,14 @@ class MapsForPrivatizedSymbolsPass
6062
// We want the first result of the hlfir.declare op because our goal
6163
// is to map the descriptor (fir.box or fir.boxchar) and the first
6264
// result for hlfir.declare is the descriptor if a the symbol being
63-
// decalred needs a descriptor.
65+
// declared needs a descriptor.
6466
// Some types are boxed immediately before privatization. These have other
6567
// operations in between the privatization and the declaration. It is safe
6668
// to use var directly here because they will be boxed anyway.
6769
if (auto declOp = llvm::dyn_cast_if_present<hlfir::DeclareOp>(definingOp))
6870
varPtr = declOp.getBase();
6971

70-
// If we do not have a reference to descritor, but the descriptor itself
72+
// If we do not have a reference to a descriptor but the descriptor itself,
7173
// then we need to store that on the stack so that we can map the
7274
// address of the descriptor.
7375
if (mlir::isa<fir::BaseBoxType>(varPtr.getType()) ||
@@ -81,14 +83,23 @@ class MapsForPrivatizedSymbolsPass
8183
builder.create<fir::StoreOp>(loc, varPtr, alloca);
8284
varPtr = alloca;
8385
}
86+
assert(mlir::isa<omp::PointerLikeType>(varPtr.getType()) &&
87+
"Dealing with a varPtr that is not a PointerLikeType");
88+
89+
// Figure out the bounds because knowing the bounds will help the subsequent
90+
// MapInfoFinalizationPass map the underlying data of the descriptor.
91+
llvm::SmallVector<mlir::Value> boundsOps;
92+
if (needsBoundsOps(varPtr))
93+
genBoundsOps(builder, varPtr, boundsOps);
94+
8495
return builder.create<omp::MapInfoOp>(
8596
loc, varPtr.getType(), varPtr,
8697
TypeAttr::get(llvm::cast<omp::PointerLikeType>(varPtr.getType())
8798
.getElementType()),
8899
/*varPtrPtr=*/Value{},
89100
/*members=*/SmallVector<Value>{},
90101
/*member_index=*/mlir::ArrayAttr{},
91-
/*bounds=*/ValueRange{},
102+
/*bounds=*/boundsOps.empty() ? SmallVector<Value>{} : boundsOps,
92103
builder.getIntegerAttr(builder.getIntegerType(64, /*isSigned=*/false),
93104
mapTypeTo),
94105
/*mapperId*/ mlir::FlatSymbolRefAttr(),
@@ -143,8 +154,8 @@ class MapsForPrivatizedSymbolsPass
143154
omp::MapInfoOp mapInfoOp = createMapInfo(loc, privVar, builder);
144155
mapInfoOps.push_back(mapInfoOp);
145156

146-
LLVM_DEBUG(llvm::dbgs() << "MapsForPrivatizedSymbolsPass created ->\n");
147-
LLVM_DEBUG(mapInfoOp.dump());
157+
LLVM_DEBUG(PDBGS() << "MapsForPrivatizedSymbolsPass created ->\n"
158+
<< mapInfoOp << "\n");
148159
}
149160
if (!mapInfoOps.empty()) {
150161
mapInfoOpsForTarget.insert({targetOp.getOperation(), mapInfoOps});
@@ -158,5 +169,50 @@ class MapsForPrivatizedSymbolsPass
158169
}
159170
}
160171
}
172+
// As the name suggests, this function examines var to determine if
173+
// it has dynamic size. If true, this pass'll have to extract these
174+
// bounds from descriptor of var and add the bounds to the resultant
175+
// MapInfoOp.
176+
bool needsBoundsOps(mlir::Value var) {
177+
assert(mlir::isa<omp::PointerLikeType>(var.getType()) &&
178+
"needsBoundsOps can deal only with pointer types");
179+
mlir::Type t = fir::unwrapRefType(var.getType());
180+
// t could be a box, so look inside the box
181+
auto innerType = fir::dyn_cast_ptrOrBoxEleTy(t);
182+
if (innerType)
183+
return fir::hasDynamicSize(innerType);
184+
return fir::hasDynamicSize(t);
185+
}
186+
void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
187+
llvm::SmallVector<mlir::Value> &boundsOps) {
188+
if (!fir::isBoxAddress(var.getType()))
189+
return;
190+
191+
unsigned int rank = 0;
192+
rank = fir::getBoxRank(fir::unwrapRefType(var.getType()));
193+
mlir::Location loc = var.getLoc();
194+
mlir::Type idxTy = builder.getIndexType();
195+
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
196+
mlir::Type boundTy = builder.getType<omp::MapBoundsType>();
197+
mlir::Value box = builder.create<fir::LoadOp>(loc, var);
198+
for (unsigned int i = 0; i < rank; ++i) {
199+
mlir::Value dimNo = builder.createIntegerConstant(loc, idxTy, i);
200+
auto dimInfo =
201+
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dimNo);
202+
auto normalizedLB = builder.create<mlir::arith::ConstantOp>(
203+
loc, idxTy, builder.getIntegerAttr(idxTy, 0));
204+
mlir::Value lb = dimInfo.getLowerBound();
205+
mlir::Value extent = dimInfo.getExtent();
206+
mlir::Value byteStride = dimInfo.getByteStride();
207+
mlir::Value ub = builder.create<mlir::arith::SubIOp>(loc, extent, one);
208+
209+
mlir::Value boundsOp = builder.create<omp::MapBoundsOp>(
210+
loc, boundTy, /*lower_bound=*/normalizedLB,
211+
/*upper_bound=*/ub, /*extent=*/extent, /*stride=*/byteStride,
212+
/*stride_in_bytes = */ true, /*start_idx=*/lb);
213+
LLVM_DEBUG(PDBGS() << "Created BoundsOp " << boundsOp << "\n");
214+
boundsOps.push_back(boundsOp);
215+
}
216+
}
161217
};
162218
} // namespace

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,14 @@ void createHLFIRToFIRPassPipeline(mlir::PassManager &pm, bool enableOpenMP,
282282
/// \param isTargetDevice - Whether code is being generated for a target device
283283
/// rather than the host device.
284284
void createOpenMPFIRPassPipeline(mlir::PassManager &pm, bool isTargetDevice) {
285-
pm.addPass(flangomp::createMapInfoFinalizationPass());
285+
// The MapsForPrivatizedSymbols pass needs to run before
286+
// MapInfoFinalizationPass because the former creates new
287+
// MapInfoOp instances, typically for descriptors.
288+
// MapInfoFinalizationPass adds MapInfoOp instances for the descriptors
289+
// underlying data which is necessary to access the data on the offload
290+
// target device.
286291
pm.addPass(flangomp::createMapsForPrivatizedSymbolsPass());
292+
pm.addPass(flangomp::createMapInfoFinalizationPass());
287293
pm.addPass(flangomp::createMarkDeclareTargetPass());
288294
pm.addPass(flangomp::createGenericLoopConversionPass());
289295
if (isTargetDevice)

flang/test/Lower/OpenMP/DelayedPrivatization/target-private-allocatable.f90

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ end subroutine target_allocatable
5858
! CHECK: %[[VAR_ALLOC:.*]] = fir.alloca [[DESC_TYPE]]
5959
! CHECK-SAME: {bindc_name = "alloc_var", {{.*}}}
6060
! CHECK: %[[VAR_DECL:.*]]:2 = hlfir.declare %[[VAR_ALLOC]]
61+
! CHECK: %[[BASE_ADDR:.*]] = fir.box_offset %[[VAR_DECL]]#0 base_addr : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> [[MEMBER_TYPE:.*]]
62+
! CHECK: %[[MEMBER:.*]] = omp.map.info var_ptr(%[[VAR_DECL]]#0 : [[TYPE]], i32) var_ptr_ptr(%[[BASE_ADDR]] : [[MEMBER_TYPE:.*]]) map_clauses(to) capture(ByRef) -> {{.*}}
63+
! CHECK: %[[MAP_VAR:.*]] = omp.map.info var_ptr(%[[VAR_DECL]]#0 : [[TYPE]], [[DESC_TYPE]]) map_clauses(to) capture(ByRef) members(%[[MEMBER]] : [0] : !fir.llvm_ptr<!fir.ref<i32>>) -> !fir.ref<!fir.box<!fir.heap<i32>>>
6164

62-
! CHECK: %[[MAP_VAR:.*]] = omp.map.info var_ptr(%[[VAR_DECL]]#0 : [[TYPE]], [[DESC_TYPE]])
63-
! CHECK-SAME: map_clauses(to) capture(ByRef) -> [[TYPE]]
64-
! CHECK: omp.target map_entries(%[[MAP_VAR]] -> %arg0 : [[TYPE]]) private(
65-
! CHECK-SAME: @[[VAR_PRIVATIZER_SYM]] %[[VAR_DECL]]#0 -> %{{.*}} : [[TYPE]]) {
65+
! CHECK: omp.target map_entries(%[[MAP_VAR]] -> %arg0, %[[MEMBER]] -> %arg1 : [[TYPE]], [[MEMBER_TYPE]]) private(
66+
! CHECK-SAME: @[[VAR_PRIVATIZER_SYM]] %[[VAR_DECL]]#0 -> %{{.*}} [map_idx=0] : [[TYPE]]) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,19 @@ end subroutine target_allocatable
140140
! CHECK: %[[REAL_ARR_DECL:.*]]:2 = hlfir.declare %[[REAL_ARR_ALLOC]]({{.*}})
141141
! CHECK: fir.store %[[REAL_ARR_DECL]]#0 to %[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>
142142
! CHECK: %[[MAPPED_MI0:.*]] = omp.map.info var_ptr(%[[MAPPED_DECL]]#1 : !fir.ref<i32>, i32) {{.*}}
143-
! CHECK: %[[ALLOC_VAR_MAP:.*]] = omp.map.info var_ptr(%[[ALLOC_VAR_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.box<!fir.heap<i32>>)
144-
! CHECK: %[[REAL_ARR_DESC_MAP:.*]] = omp.map.info var_ptr(%[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.box<!fir.array<?xf32>>)
143+
! CHECK: %[[ALLOC_VAR_MEMBER:.*]] = omp.map.info var_ptr(%[[ALLOC_VAR_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>, i32)
144+
! CHECK: %[[ALLOC_VAR_MAP:.*]] = omp.map.info var_ptr(%[[ALLOC_VAR_DECL]]#0 : !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.box<!fir.heap<i32>>) {{.*}} members(%[[ALLOC_VAR_MEMBER]] :
145+
! CHECK: %[[REAL_ARR_MEMBER:.*]] = omp.map.info var_ptr(%[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>, f32)
146+
! CHECK: %[[REAL_ARR_DESC_MAP:.*]] = omp.map.info var_ptr(%[[REAL_ARR_DESC_ALLOCA]] : !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.box<!fir.array<?xf32>>) {{.*}} members(%[[REAL_ARR_MEMBER]] :
145147
! CHECK: fir.store %[[CHAR_VAR_DECL]]#0 to %[[CHAR_VAR_DESC_ALLOCA]] : !fir.ref<!fir.boxchar<1>>
146148
! CHECK: %[[CHAR_VAR_DESC_MAP:.*]] = omp.map.info var_ptr(%[[CHAR_VAR_DESC_ALLOCA]] : !fir.ref<!fir.boxchar<1>>, !fir.boxchar<1>)
147149
! CHECK: omp.target
148150
! CHECK-SAME: map_entries(
149151
! CHECK-SAME: %[[MAPPED_MI0]] -> %[[MAPPED_ARG0:[^,]+]],
150152
! CHECK-SAME: %[[ALLOC_VAR_MAP]] -> %[[MAPPED_ARG1:[^,]+]]
151153
! CHECK-SAME: %[[REAL_ARR_DESC_MAP]] -> %[[MAPPED_ARG2:[^,]+]]
152-
! CHECK-SAME: %[[CHAR_VAR_DESC_MAP]] -> %[[MAPPED_ARG3:.[^,]+]] :
153-
! CHECK-SAME: !fir.ref<i32>, !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.ref<!fir.boxchar<1>>)
154+
! CHECK-SAME: %[[CHAR_VAR_DESC_MAP]] -> %[[MAPPED_ARG3:.[^,]+]]
155+
! CHECK-SAME: !fir.ref<i32>, !fir.ref<!fir.box<!fir.heap<i32>>>, !fir.ref<!fir.box<!fir.array<?xf32>>>, !fir.ref<!fir.boxchar<1>>, !fir.llvm_ptr<!fir.ref<i32>>, !fir.llvm_ptr<!fir.ref<!fir.array<?xf32>>>
154156
! CHECK-SAME: private(
155157
! CHECK-SAME: @[[ALLOC_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[ALLOC_ARG:[^,]+]] [map_idx=1],
156158
! CHECK-SAME: @[[REAL_PRIVATIZER_SYM]] %{{[^[:space:]]+}}#0 -> %[[REAL_ARG:[^,]+]],

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,27 +1440,34 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
14401440
assert(allocaTerminator->getNumSuccessors() == 1 &&
14411441
"This is an unconditional branch created by splitBB");
14421442

1443+
llvm::DataLayout dataLayout = builder.GetInsertBlock()->getDataLayout();
14431444
llvm::BasicBlock *afterAllocas = allocaTerminator->getSuccessor(0);
1444-
1445+
const unsigned int allocaAS = dataLayout.getAllocaAddrSpace();
1446+
const unsigned int defaultAS = dataLayout.getProgramAddressSpace();
1447+
bool needAddrSpaceCasts = allocaAS != defaultAS;
14451448
for (auto [privDecl, mlirPrivVar, blockArg] :
14461449
llvm::zip_equal(privateDecls, mlirPrivateVars, privateBlockArgs)) {
14471450
llvm::Type *llvmAllocType =
14481451
moduleTranslation.convertType(privDecl.getType());
14491452
builder.SetInsertPoint(allocaIP.getBlock()->getTerminator());
14501453
llvm::Value *llvmPrivateVar = builder.CreateAlloca(
14511454
llvmAllocType, /*ArraySize=*/nullptr, "omp.private.alloc");
1452-
llvmPrivateVars.push_back(llvmPrivateVar);
1455+
if (needAddrSpaceCasts)
1456+
llvmPrivateVars.push_back(builder.CreateAddrSpaceCast(
1457+
llvmPrivateVar, builder.getPtrTy(defaultAS)));
1458+
else
1459+
llvmPrivateVars.push_back(llvmPrivateVar);
14531460
}
14541461

14551462
return afterAllocas;
14561463
}
14571464

1458-
static LogicalResult
1459-
copyFirstPrivateVars(llvm::IRBuilderBase &builder,
1460-
LLVM::ModuleTranslation &moduleTranslation,
1461-
SmallVectorImpl<mlir::Value> &mlirPrivateVars,
1462-
ArrayRef<llvm::Value *> llvmPrivateVars,
1463-
SmallVectorImpl<omp::PrivateClauseOp> &privateDecls) {
1465+
static LogicalResult copyFirstPrivateVars(
1466+
llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation,
1467+
SmallVectorImpl<mlir::Value> &mlirPrivateVars,
1468+
ArrayRef<llvm::Value *> llvmPrivateVars,
1469+
SmallVectorImpl<omp::PrivateClauseOp> &privateDecls,
1470+
llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
14641471
// Apply copy region for firstprivate.
14651472
bool needsFirstprivate =
14661473
llvm::any_of(privateDecls, [](omp::PrivateClauseOp &privOp) {
@@ -1484,7 +1491,8 @@ copyFirstPrivateVars(llvm::IRBuilderBase &builder,
14841491
Region &copyRegion = decl.getCopyRegion();
14851492

14861493
// map copyRegion rhs arg
1487-
llvm::Value *nonPrivateVar = moduleTranslation.lookupValue(mlirVar);
1494+
llvm::Value *nonPrivateVar = findAssociatedValue(
1495+
mlirVar, builder, moduleTranslation, mappedPrivateVars);
14881496
assert(nonPrivateVar);
14891497
moduleTranslation.mapValue(decl.getCopyMoldArg(), nonPrivateVar);
14901498

@@ -4801,7 +4809,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
48014809
return llvm::make_error<PreviouslyReportedError>();
48024810

48034811
if (failed(copyFirstPrivateVars(builder, moduleTranslation, mlirPrivateVars,
4804-
llvmPrivateVars, privateDecls)))
4812+
llvmPrivateVars, privateDecls,
4813+
&mappedPrivateVars)))
48054814
return llvm::make_error<PreviouslyReportedError>();
48064815

48074816
SmallVector<Region *> privateCleanupRegions;

0 commit comments

Comments
 (0)