Skip to content

Commit 9077522

Browse files
[acc][flang] Define hasUnknownDimensions in MappableType (#165794)
The MappableType interface currently defines a `generateAccBounds` method which examines a variable and generates `acc.bounds` operations that encode its dimensions. The implementation can extract bounds information in various ways: either from the MLIR type itself or by analyzing the IR to find dimension information from defining operations. However, we need to distinguish between cases where dimensional information is not directly available from the type itself. This new `hasUnknownDimensions` API returns true when the MLIR type does not encode dimensional information and there is no associated descriptor or metadata that would make this information extractable from the visible ssa value the represents the variable. The expected use case is calling `generateAccBounds` only when this returns true, as it indicates that bounds must be extracted from the IR (by walking back from current variable to its defining spots or its descriptor). This supports cases such as raw references to arrays with non-constant bounds (e.g., explicit-shape arrays in Fortran where bounds are passed as arguments). This functionality could also be leveraged for CIR VLA support in the future. For FIR types: - Box types return false (descriptor encodes dimensions) - Reference types check if the pointee has dynamic size using fir::hasDynamicSize()
1 parent f7a21a8 commit 9077522

File tree

6 files changed

+48
-1
lines changed

6 files changed

+48
-1
lines changed

flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ struct OpenACCMappableModel
6060
getOffsetInBytes(mlir::Type type, mlir::Value var, mlir::ValueRange accBounds,
6161
const mlir::DataLayout &dataLayout) const;
6262

63+
bool hasUnknownDimensions(mlir::Type type) const;
64+
6365
llvm::SmallVector<mlir::Value>
6466
generateAccBounds(mlir::Type type, mlir::Value var,
6567
mlir::OpBuilder &builder) const;

flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ OpenACCMappableModel<fir::PointerType>::getOffsetInBytes(
193193
mlir::Type type, mlir::Value var, mlir::ValueRange accBounds,
194194
const mlir::DataLayout &dataLayout) const;
195195

196+
template <typename Ty>
197+
bool OpenACCMappableModel<Ty>::hasUnknownDimensions(mlir::Type type) const {
198+
assert(fir::isa_ref_type(type) && "expected FIR reference type");
199+
return fir::hasDynamicSize(fir::unwrapRefType(type));
200+
}
201+
202+
template bool OpenACCMappableModel<fir::ReferenceType>::hasUnknownDimensions(
203+
mlir::Type type) const;
204+
205+
template bool OpenACCMappableModel<fir::HeapType>::hasUnknownDimensions(
206+
mlir::Type type) const;
207+
208+
template bool OpenACCMappableModel<fir::PointerType>::hasUnknownDimensions(
209+
mlir::Type type) const;
210+
211+
template <>
212+
bool OpenACCMappableModel<fir::BaseBoxType>::hasUnknownDimensions(
213+
mlir::Type type) const {
214+
// Descriptor-based entities have dimensions encoded.
215+
return false;
216+
}
217+
196218
static llvm::SmallVector<mlir::Value>
197219
generateSeqTyAccBounds(fir::SequenceType seqType, mlir::Value var,
198220
mlir::OpBuilder &builder) {

flang/test/Fir/OpenACC/openacc-mappable.fir

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<f16 = dense<16> : vector<2xi64>,
2121
// CHECK: Mappable: !fir.box<!fir.array<10xf32>>
2222
// CHECK: Type category: array
2323
// CHECK: Size: 40
24+
// CHECK: Has unknown dimensions: false
2425

2526
// CHECK: Visiting: %{{.*}} = acc.copyin varPtr(%{{.*}} : !fir.ref<!fir.array<10xf32>>) -> !fir.ref<!fir.array<10xf32>> {name = "arr", structured = false}
2627
// CHECK: Pointer-like and Mappable: !fir.ref<!fir.array<10xf32>>
2728
// CHECK: Type category: array
2829
// CHECK: Size: 40
30+
// CHECK: Has unknown dimensions: false
2931

3032
// This second test exercises argument of explicit-shape arrays in following forms:
3133
// `real :: arr1(nn), arr2(2:nn), arr3(10)`
@@ -62,6 +64,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<f16 = dense<16> : vector<2xi64>,
6264
// CHECK: Visiting: %{{.*}} = acc.copyin varPtr(%{{.*}} : !fir.ref<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>> {name = "arr1", structured = false}
6365
// CHECK: Pointer-like and Mappable: !fir.ref<!fir.array<?xf32>>
6466
// CHECK: Type category: array
67+
// CHECK: Has unknown dimensions: true
6568
// CHECK: Shape: %{{.*}} = fir.shape %[[EXTENT1:.*]] : (index) -> !fir.shape<1>
6669
// CHECK: Bound[0]: %{{.*}} = acc.bounds lowerbound(%[[LB1:.*]] : index) upperbound(%[[UB1:.*]] : index) extent(%{{.*}} : index) stride(%c1{{.*}} : index) startIdx(%c1{{.*}} : index)
6770
// CHECK: Lower bound: %[[LB1]] = arith.constant 0 : index
@@ -70,6 +73,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<f16 = dense<16> : vector<2xi64>,
7073
// CHECK: Visiting: %{{.*}} = acc.copyin varPtr(%{{.*}} : !fir.ref<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>> {name = "arr2", structured = false}
7174
// CHECK: Pointer-like and Mappable: !fir.ref<!fir.array<?xf32>>
7275
// CHECK: Type category: array
76+
// CHECK: Has unknown dimensions: true
7377
// CHECK: Shape: %{{.*}} = fir.shape_shift %c2{{.*}}, %[[EXTENT2:.*]] : (index, index) -> !fir.shapeshift<1>
7478
// CHECK: Bound[0]: %{{.*}} = acc.bounds lowerbound(%[[LB2:.*]] : index) upperbound(%[[UB2:.*]] : index) extent(%{{.*}} : index) stride(%c1{{.*}} : index) startIdx(%c2{{.*}} : index)
7579
// CHECK: Lower bound: %[[LB2]] = arith.constant 0 : index
@@ -80,6 +84,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<f16 = dense<16> : vector<2xi64>,
8084
// CHECK: Type category: array
8185
// CHECK: Size: 40
8286
// CHECK: Offset: 0
87+
// CHECK: Has unknown dimensions: false
8388
// CHECK: Shape: %{{.*}} = fir.shape %[[EXTENT3:.*]] : (index) -> !fir.shape<1>
8489
// CHECK: Bound[0]: %{{.*}} = acc.bounds lowerbound(%[[LB3:.*]] : index) upperbound(%[[UB3:.*]] : index) extent(%c10{{.*}} : index) stride(%c1{{.*}} : index) startIdx(%c1{{.*}} : index)
8590
// CHECK: Lower bound: %[[LB3]] = arith.constant 0 : index

flang/test/lib/OpenACC/TestOpenACCInterfaces.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ struct TestFIROpenACCInterfaces
100100
}
101101
}
102102

103+
llvm::errs() << "\t\tHas unknown dimensions: "
104+
<< (mappableTy.hasUnknownDimensions() ? "true" : "false")
105+
<< "\n";
106+
103107
if (auto declareOp =
104108
dyn_cast_if_present<hlfir::DeclareOp>(var.getDefiningOp())) {
105109
llvm::errs() << "\t\tShape: " << declareOp.getShape() << "\n";

mlir/include/mlir/Dialect/OpenACC/OpenACCTypeInterfaces.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ def OpenACC_MappableTypeInterface : TypeInterface<"MappableType"> {
259259
return {};
260260
}]
261261
>,
262+
InterfaceMethod<
263+
/*description=*/[{
264+
Returns true if the dimensions of this type are not known. This occurs
265+
when the MLIR type does not encode dimensional information and there is
266+
no associated descriptor or metadata in the current entity that would
267+
make this information extractable. For example, an opaque pointer type
268+
pointing to an array without dimension information would have unknown
269+
dimensions.
270+
}],
271+
/*retTy=*/"bool",
272+
/*methodName=*/"hasUnknownDimensions"
273+
>,
262274
InterfaceMethod<
263275
/*description=*/[{
264276
Returns explicit `acc.bounds` operations that envelop the whole

mlir/unittests/Dialect/OpenACC/OpenACCOpsTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,9 @@ void testShortDataEntryOpBuildersMappableVar(OpBuilder &b, MLIRContext &context,
766766

767767
struct IntegerOpenACCMappableModel
768768
: public mlir::acc::MappableType::ExternalModel<IntegerOpenACCMappableModel,
769-
IntegerType> {};
769+
IntegerType> {
770+
bool hasUnknownDimensions(mlir::Type type) const { return false; }
771+
};
770772

771773
TEST_F(OpenACCOpsTest, mappableTypeBuilderDataEntry) {
772774
// First, set up the test by attaching MappableInterface to IntegerType.

0 commit comments

Comments
 (0)