Skip to content

Commit de0ea40

Browse files
committed
[Flang][OpenMP] Fix allocatable -ve bounds mapping issue found in SWDEV-548635
This should fix the problem in SWDEV-548635 and seems fairly stable from the POV of my local tests, but, need to see how it fairs against the nightly testing. The issue was primarily that the previous intermediate map generation for allocatable members wasn't quite handling negative bounds acccesses correctly, it seems to require slightly more complicated access using shape_shift/dimension information. So this more closely mimics what Flang generates in other cases now. There is still a path for non-Box types to go down the old route for the moment, so it is possible we may still have issues with negative bounds in these cases. But, that's better in another PR if we come across it, instead of too much change in this one.
1 parent 039d374 commit de0ea40

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

flang/lib/Lower/OpenMP/Utils.cpp

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <flang/Lower/OpenMP/Utils.h>
1414

1515
#include "ClauseFinder.h"
16-
#include "flang/Lower/OpenMP/Clauses.h"
1716
#include <flang/Lower/AbstractConverter.h>
1817
#include <flang/Lower/ConvertExprToHLFIR.h>
1918
#include <flang/Lower/ConvertType.h>
@@ -196,16 +195,11 @@ static void generateArrayIndices(lower::AbstractConverter &converter,
196195
for (auto v : arr->subscript()) {
197196
if (std::holds_alternative<Triplet>(v.u))
198197
TODO(clauseLocation, "Triplet indexing in map clause is unsupported");
199-
200198
auto expr = std::get<Fortran::evaluate::IndirectSubscriptIntegerExpr>(v.u);
201199
mlir::Value subscript =
202200
fir::getBase(converter.genExprValue(toEvExpr(expr.value()), stmtCtx));
203-
mlir::Value one = firOpBuilder.createIntegerConstant(
204-
clauseLocation, firOpBuilder.getIndexType(), 1);
205-
subscript = firOpBuilder.createConvert(
206-
clauseLocation, firOpBuilder.getIndexType(), subscript);
207-
indices.push_back(mlir::arith::SubIOp::create(firOpBuilder, clauseLocation,
208-
subscript, one));
201+
indices.push_back(firOpBuilder.createConvert(
202+
clauseLocation, firOpBuilder.getIndexType(), subscript));
209203
}
210204
}
211205

@@ -338,10 +332,41 @@ mlir::Value createParentSymAndGenIntermediateMaps(
338332
subscriptIndices, objectList[i]);
339333
assert(!subscriptIndices.empty() &&
340334
"missing expected indices for map clause");
341-
curValue = fir::CoordinateOp::create(
342-
firOpBuilder, clauseLocation,
343-
firOpBuilder.getRefType(arrType.getEleTy()), curValue,
344-
subscriptIndices);
335+
if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(curValue.getType())) {
336+
fir::ExtendedValue exv =
337+
hlfir::translateToExtendedValue(clauseLocation, firOpBuilder,
338+
hlfir::Entity{curValue},
339+
/*contiguousHint=*/
340+
true)
341+
.first;
342+
343+
mlir::Type idxTy = firOpBuilder.getIndexType();
344+
llvm::SmallVector<mlir::Value> shiftOperands;
345+
for (unsigned dim = 0; dim < exv.rank(); ++dim) {
346+
mlir::Value d =
347+
firOpBuilder.createIntegerConstant(clauseLocation, idxTy, dim);
348+
auto dimInfo = fir::BoxDimsOp::create(
349+
firOpBuilder, clauseLocation, idxTy, idxTy, idxTy, curValue, d);
350+
shiftOperands.push_back(dimInfo.getLowerBound());
351+
shiftOperands.push_back(dimInfo.getExtent());
352+
}
353+
auto shapeShiftType =
354+
fir::ShapeShiftType::get(firOpBuilder.getContext(), exv.rank());
355+
mlir::Value shapeShift = fir::ShapeShiftOp::create(
356+
firOpBuilder, clauseLocation, shapeShiftType, shiftOperands);
357+
auto addrOp =
358+
fir::BoxAddrOp::create(firOpBuilder, clauseLocation, curValue);
359+
curValue = fir::ArrayCoorOp::create(
360+
firOpBuilder, clauseLocation,
361+
firOpBuilder.getRefType(arrType.getEleTy()), addrOp, shapeShift,
362+
/*slice=*/mlir::Value{}, subscriptIndices,
363+
/*typeparms=*/mlir::ValueRange{});
364+
} else {
365+
curValue = fir::CoordinateOp::create(
366+
firOpBuilder, clauseLocation,
367+
firOpBuilder.getRefType(arrType.getEleTy()), curValue,
368+
subscriptIndices);
369+
}
345370
}
346371
}
347372

@@ -431,7 +456,6 @@ mlir::Value createParentSymAndGenIntermediateMaps(
431456
currentIndicesIdx++;
432457
}
433458
}
434-
435459
return curValue;
436460
}
437461

flang/test/Integration/OpenMP/map-types-and-sizes.f90

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -786,15 +786,20 @@ end subroutine mapType_common_block_members
786786
!CHECK: %[[BOUNDS_CALC:.*]] = sub i64 %[[BOUNDS_LD_2]], 1
787787
!CHECK: %[[OFF_PTR_CALC_0:.*]] = sub i64 %[[BOUNDS_LD]], 1
788788
!CHECK: %[[OFF_PTR_2:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]], ptr, [1 x i64] }, ptr %[[OFF_PTR_1]], i32 0, i32 0
789+
!CHECK: %[[GEP_LB:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]], ptr, [1 x i64] }, ptr %[[ALLOCA_0]], i32 0, i32 7, i64 0, i32 0
790+
!CHECK: %[[LOAD_LB:.*]] = load i64, ptr %[[GEP_LB]], align 8
791+
!CHECK: %[[GEP_UB:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]], ptr, [1 x i64] }, ptr %[[ALLOCA_0]], i32 0, i32 7, i64 0, i32 1
792+
!CHECK: %[[LOAD_UB:.*]] = load i64, ptr %[[GEP_UB]], align 8
789793
!CHECK: %[[GEP_DESC_PTR:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]], ptr, [1 x i64] }, ptr %[[ALLOCA_0]], i32 0, i32 0
790-
!CHECK: %[[LOAD_DESC_PTR:.*]] = load ptr, ptr %[[GEP_DESC_PTR]], align 8
791-
!CHECK: %[[SZ_CALC_1:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]], ptr, [1 x i64] }, ptr %[[ALLOCA_0]], i32 0, i32 7, i32 0, i32 2
792-
!CHECK: %[[SZ_CALC_2:.*]] = load i64, ptr %[[SZ_CALC_1]], align 8
793-
!CHECK: %[[SZ_CALC_3:.*]] = mul nsw i64 1, %[[SZ_CALC_2]]
794-
!CHECK: %[[SZ_CALC_4:.*]] = add nsw i64 %[[SZ_CALC_3]], 0
795-
!CHECK: %[[SZ_CALC_5:.*]] = getelementptr i8, ptr %[[LOAD_DESC_PTR]], i64 %[[SZ_CALC_4]]
796-
!CHECK: %[[SZ_CALC_6:.*]] = getelementptr %_QFmaptype_nested_derived_type_member_idxTvertexes, ptr %[[SZ_CALC_5]], i32 0, i32 2
797-
!CHECK: %[[OFF_PTR_4:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %[[SZ_CALC_6]], i32 0, i32 0
794+
!CHECK: %[[SZ_CALC_1:.*]] = load ptr, ptr %[[GEP_DESC_PTR]], align 8
795+
!CHECK: %[[SZ_CALC_2:.*]] = sub nsw i64 2, %[[LOAD_LB]]
796+
!CHECK: %[[SZ_CALC_3:.*]] = mul nsw i64 %[[SZ_CALC_2]], 1
797+
!CHECK: %[[SZ_CALC_4:.*]] = mul nsw i64 %[[SZ_CALC_3]], 1
798+
!CHECK: %[[SZ_CALC_5:.*]] = add nsw i64 %[[SZ_CALC_4]], 0
799+
!CHECK: %[[SZ_CALC_6:.*]] = mul nsw i64 1, %[[LOAD_UB]]
800+
!CHECK: %[[SZ_CALC_7:.*]] = getelementptr %_QFmaptype_nested_derived_type_member_idxTvertexes, ptr %[[SZ_CALC_1]], i64 %[[SZ_CALC_5]]
801+
!CHECK: %[[SZ_CALC_8:.*]] = getelementptr %_QFmaptype_nested_derived_type_member_idxTvertexes, ptr %[[SZ_CALC_7]], i32 0, i32 2
802+
!CHECK: %[[OFF_PTR_4:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %[[SZ_CALC_8]], i32 0, i32 0
798803
!CHECK: %[[OFF_PTR_CALC_1:.*]] = sub i64 %[[OFF_PTR_CALC_0]], 0
799804
!CHECK: %[[OFF_PTR_CALC_2:.*]] = add i64 %[[OFF_PTR_CALC_1]], 1
800805
!CHECK: %[[OFF_PTR_CALC_3:.*]] = mul i64 1, %[[OFF_PTR_CALC_2]]
@@ -839,7 +844,7 @@ end subroutine mapType_common_block_members
839844
!CHECK: %[[BASE_PTR_ARR:.*]] = getelementptr inbounds [7 x ptr], ptr %.offload_baseptrs, i32 0, i32 4
840845
!CHECK: store ptr %[[BASE_PTR_1]], ptr %[[BASE_PTR_ARR]], align 8
841846
!CHECK: %[[OFFLOAD_PTR_ARR:.*]] = getelementptr inbounds [7 x ptr], ptr %.offload_ptrs, i32 0, i32 4
842-
!CHECK: store ptr %[[SZ_CALC_6]], ptr %[[OFFLOAD_PTR_ARR]], align 8
847+
!CHECK: store ptr %[[SZ_CALC_8]], ptr %[[OFFLOAD_PTR_ARR]], align 8
843848
!CHECK: %[[BASE_PTR_ARR:.*]] = getelementptr inbounds [7 x ptr], ptr %.offload_baseptrs, i32 0, i32 5
844849
!CHECK: store ptr %[[BASE_PTR_1]], ptr %[[BASE_PTR_ARR]], align 8
845850
!CHECK: %[[OFFLOAD_PTR_ARR:.*]] = getelementptr inbounds [7 x ptr], ptr %.offload_ptrs, i32 0, i32 5

0 commit comments

Comments
 (0)