Skip to content

Commit 1c93a46

Browse files
committed
[OpenMP] Update description for target_allocmem and target_freemem.
1 parent 2d10c1e commit 1c93a46

File tree

5 files changed

+93
-56
lines changed

5 files changed

+93
-56
lines changed

flang/include/flang/Optimizer/Support/Utils.h

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -224,34 +224,9 @@ mlir::Value computeElementDistance(mlir::Location loc,
224224
// We do this for arrays without a constant interior or arrays of character with
225225
// dynamic length arrays, since those are the only ones that get decayed to a
226226
// pointer to the element type.
227-
template <typename OP>
228-
inline mlir::Value
229-
genAllocationScaleSize(OP op, mlir::Type ity,
230-
mlir::ConversionPatternRewriter &rewriter) {
231-
mlir::Location loc = op.getLoc();
232-
mlir::Type dataTy = op.getInType();
233-
auto seqTy = mlir::dyn_cast<fir::SequenceType>(dataTy);
234-
fir::SequenceType::Extent constSize = 1;
235-
if (seqTy) {
236-
int constRows = seqTy.getConstantRows();
237-
const fir::SequenceType::ShapeRef &shape = seqTy.getShape();
238-
if (constRows != static_cast<int>(shape.size())) {
239-
for (auto extent : shape) {
240-
if (constRows-- > 0)
241-
continue;
242-
if (extent != fir::SequenceType::getUnknownExtent())
243-
constSize *= extent;
244-
}
245-
}
246-
}
247-
248-
if (constSize != 1) {
249-
mlir::Value constVal{
250-
fir::genConstantIndex(loc, ity, rewriter, constSize).getResult()};
251-
return constVal;
252-
}
253-
return nullptr;
254-
}
227+
mlir::Value genAllocationScaleSize(mlir::Location loc, mlir::Type dataTy,
228+
mlir::Type ity,
229+
mlir::ConversionPatternRewriter &rewriter);
255230

256231
/// Perform an extension or truncation as needed on an integer value. Lowering
257232
/// to the specific target may involve some sign-extending or truncation of

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ struct AllocaOpConversion : public fir::FIROpConversion<fir::AllocaOp> {
266266
<< scalarType << " with type parameters";
267267
}
268268
}
269-
if (auto scaleSize = fir::genAllocationScaleSize(alloc, ity, rewriter))
269+
if (auto scaleSize = fir::genAllocationScaleSize(
270+
alloc.getLoc(), alloc.getInType(), ity, rewriter))
270271
size =
271272
rewriter.createOrFold<mlir::LLVM::MulOp>(loc, ity, size, scaleSize);
272273
if (alloc.hasShapeOperands()) {
@@ -1068,7 +1069,8 @@ struct AllocMemOpConversion : public fir::FIROpConversion<fir::AllocMemOp> {
10681069
if (fir::isRecordWithTypeParameters(fir::unwrapSequenceType(dataTy)))
10691070
TODO(loc, "fir.allocmem codegen of derived type with length parameters");
10701071
mlir::Value size = genTypeSizeInBytes(loc, ity, rewriter, llvmObjectTy);
1071-
if (auto scaleSize = fir::genAllocationScaleSize(heap, ity, rewriter))
1072+
if (auto scaleSize =
1073+
fir::genAllocationScaleSize(loc, heap.getInType(), ity, rewriter))
10721074
size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, scaleSize);
10731075
for (mlir::Value opnd : adaptor.getOperands())
10741076
size = mlir::LLVM::MulOp::create(rewriter, loc, ity, size,
@@ -4310,20 +4312,22 @@ void fir::populateFIRToLLVMConversionPatterns(
43104312
BoxTypeCodeOpConversion, BoxTypeDescOpConversion, CallOpConversion,
43114313
CmpcOpConversion, VolatileCastOpConversion, ConvertOpConversion,
43124314
CoordinateOpConversion, CopyOpConversion, DTEntryOpConversion,
4313-
DeclareOpConversion, DivcOpConversion, EmboxOpConversion,
4314-
EmboxCharOpConversion, EmboxProcOpConversion, ExtractValueOpConversion,
4315-
FieldIndexOpConversion, FirEndOpConversion, FreeMemOpConversion,
4316-
GlobalLenOpConversion, GlobalOpConversion, InsertOnRangeOpConversion,
4317-
IsPresentOpConversion, LenParamIndexOpConversion, LoadOpConversion,
4318-
LocalitySpecifierOpConversion, MulcOpConversion, NegcOpConversion,
4319-
NoReassocOpConversion, SelectCaseOpConversion, SelectOpConversion,
4320-
SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion,
4321-
ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion,
4322-
StoreOpConversion, StringLitOpConversion, SubcOpConversion,
4323-
TypeDescOpConversion, TypeInfoOpConversion, UnboxCharOpConversion,
4324-
UnboxProcOpConversion, UndefOpConversion, UnreachableOpConversion,
4325-
XArrayCoorOpConversion, XEmboxOpConversion, XReboxOpConversion,
4326-
ZeroOpConversion>(converter, options);
4315+
DeclareOpConversion,
4316+
DoConcurrentSpecifierOpConversion<fir::LocalitySpecifierOp>,
4317+
DoConcurrentSpecifierOpConversion<fir::DeclareReductionOp>,
4318+
DivcOpConversion, EmboxOpConversion, EmboxCharOpConversion,
4319+
EmboxProcOpConversion, ExtractValueOpConversion, FieldIndexOpConversion,
4320+
FirEndOpConversion, FreeMemOpConversion, GlobalLenOpConversion,
4321+
GlobalOpConversion, InsertOnRangeOpConversion, IsPresentOpConversion,
4322+
LenParamIndexOpConversion, LoadOpConversion, MulcOpConversion,
4323+
NegcOpConversion, NoReassocOpConversion, SelectCaseOpConversion,
4324+
SelectOpConversion, SelectRankOpConversion, SelectTypeOpConversion,
4325+
ShapeOpConversion, ShapeShiftOpConversion, ShiftOpConversion,
4326+
SliceOpConversion, StoreOpConversion, StringLitOpConversion,
4327+
SubcOpConversion, TypeDescOpConversion, TypeInfoOpConversion,
4328+
UnboxCharOpConversion, UnboxProcOpConversion, UndefOpConversion,
4329+
UnreachableOpConversion, XArrayCoorOpConversion, XEmboxOpConversion,
4330+
XReboxOpConversion, ZeroOpConversion>(converter, options);
43274331

43284332
// Patterns that are populated without a type converter do not trigger
43294333
// target materializations for the operands of the root op.

flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ struct TargetAllocMemOpConversion
145145
"parameters");
146146
mlir::Value size = fir::computeElementDistance(
147147
loc, llvmObjectTy, ity, rewriter, lowerTy().getDataLayout());
148-
if (auto scaleSize = fir::genAllocationScaleSize(allocmemOp, ity, rewriter))
148+
if (auto scaleSize = fir::genAllocationScaleSize(
149+
loc, allocmemOp.getInType(), ity, rewriter))
149150
size = rewriter.create<mlir::LLVM::MulOp>(loc, ity, size, scaleSize);
150151
for (mlir::Value opnd : adaptor.getOperands().drop_front())
151152
size = rewriter.create<mlir::LLVM::MulOp>(

flang/lib/Optimizer/Support/Utils.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ fir::computeElementDistance(mlir::Location loc, mlir::Type llvmObjectType,
7777
return fir::genConstantIndex(loc, idxTy, rewriter, distance);
7878
}
7979

80+
mlir::Value
81+
fir::genAllocationScaleSize(mlir::Location loc, mlir::Type dataTy,
82+
mlir::Type ity,
83+
mlir::ConversionPatternRewriter &rewriter) {
84+
auto seqTy = mlir::dyn_cast<fir::SequenceType>(dataTy);
85+
fir::SequenceType::Extent constSize = 1;
86+
if (seqTy) {
87+
int constRows = seqTy.getConstantRows();
88+
const fir::SequenceType::ShapeRef &shape = seqTy.getShape();
89+
if (constRows != static_cast<int>(shape.size())) {
90+
for (auto extent : shape) {
91+
if (constRows-- > 0)
92+
continue;
93+
if (extent != fir::SequenceType::getUnknownExtent())
94+
constSize *= extent;
95+
}
96+
}
97+
}
98+
99+
if (constSize != 1) {
100+
mlir::Value constVal{
101+
fir::genConstantIndex(loc, ity, rewriter, constSize).getResult()};
102+
return constVal;
103+
}
104+
return nullptr;
105+
}
106+
80107
mlir::Value fir::integerCast(const fir::LLVMTypeConverter &converter,
81108
mlir::Location loc,
82109
mlir::ConversionPatternRewriter &rewriter,

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,10 +2129,31 @@ def TargetAllocMemOp : OpenMP_Op<"target_allocmem",
21292129
The memory is uninitialized after allocation. Operations must be paired with
21302130
`omp.target_freemem` to avoid memory leaks.
21312131

2132-
```mlir
2133-
%device = arith.constant 0 : i32
2134-
%ptr = omp.target_allocmem %device : i32, vector<3x3xi32>
2135-
```
2132+
* `$device`: The integer ID of the OpenMP device where the memory will be allocated.
2133+
* `$in_type`: The type of the object for which memory is being allocated.
2134+
For arrays, this can be a static or dynamic array type.
2135+
* `$uniq_name`: An optional unique name for the allocated memory.
2136+
* `$bindc_name`: An optional name used for C interoperability.
2137+
* `$typeparams`: Runtime type parameters for polymorphic or parameterized types.
2138+
These are typically integer values that define aspects of a type not fixed at compile time.
2139+
* `$shape`: Runtime shape operands for dynamic arrays.
2140+
Each operand is an integer value representing the extent of a specific dimension.
2141+
2142+
```mlir
2143+
// Allocate a static 3x3 integer vector on device 0
2144+
%device_0 = arith.constant 0 : i32
2145+
%ptr_static = omp.target_allocmem %device_0 : i32, vector<3x3xi32>
2146+
// ... use %ptr_static ...
2147+
omp.target_freemem %device_0, %ptr_static : i32, i64
2148+
2149+
// Allocate a dynamic 2D Fortran array (fir.array) on device 1
2150+
%device_1 = arith.constant 1 : i32
2151+
%rows = arith.constant 10 : index
2152+
%cols = arith.constant 20 : index
2153+
%ptr_dynamic = omp.target_allocmem %device_1 : i32, !fir.array<?x?xf32>, %rows, %cols : index, index
2154+
// ... use %ptr_dynamic ...
2155+
omp.target_freemem %device_1, %ptr_dynamic : i32, i64
2156+
```
21362157
}];
21372158

21382159
let arguments = (ins
@@ -2162,13 +2183,22 @@ def TargetFreeMemOp : OpenMP_Op<"target_freemem",
21622183
let summary = "free memory on an openmp device";
21632184

21642185
let description = [{
2165-
Deallocates memory on the specified OpenMP device that was previously
2166-
allocated by an `omp.target_allocmem` operation. The memory is placed
2167-
in an undefined state after deallocation.
2168-
```
2169-
%device = arith.constant 0 : i32
2170-
%ptr = omp.target_allocmem %device : i32, vector<3x3xi32>
2171-
omp.target_freemem %device, %ptr : i32, i64
2186+
Deallocates memory on the specified OpenMP device that was previously
2187+
allocated by an `omp.target_allocmem` operation. After this operation, the
2188+
deallocated memory is in an undefined state and should not be accessed.
2189+
It is crucial to ensure that all accesses to the memory region are completed
2190+
before `omp.target_freemem` is called to avoid undefined behavior.
2191+
2192+
* `$device`: The integer ID of the OpenMP device from which the memory will be freed.
2193+
* `$heapref`: The integer value representing the device pointer to the memory
2194+
to be deallocated, which was previously returned by `omp.target_allocmem`.
2195+
2196+
```mlir
2197+
// Example of allocating and freeing memory on an OpenMP device
2198+
%device_id = arith.constant 0 : i32
2199+
%allocated_ptr = omp.target_allocmem %device_id : i32, vector<3x3xi32>
2200+
// ... operations using %allocated_ptr on the device ...
2201+
omp.target_freemem %device_id, %allocated_ptr : i32, i64
21722202
```
21732203
}];
21742204

0 commit comments

Comments
 (0)