Skip to content

Commit 5bb379f

Browse files
authored
[flang][cuda] Fix allocation of descriptor for cray pointer (#103474)
The cray pointee descriptor with device attribute was not allocated with cuf.alloc so it leads to error on deallocation with cuf.free.
1 parent b719ab4 commit 5bb379f

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

flang/include/flang/Optimizer/Builder/FIRBuilder.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,20 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
214214
/// Create a temporary using `fir.alloca`. This function does not hoist.
215215
/// It is the callers responsibility to set the insertion point if
216216
/// hoisting is required.
217-
mlir::Value
218-
createTemporaryAlloc(mlir::Location loc, mlir::Type type,
219-
llvm::StringRef name, mlir::ValueRange lenParams = {},
220-
mlir::ValueRange shape = {},
221-
llvm::ArrayRef<mlir::NamedAttribute> attrs = {});
217+
mlir::Value createTemporaryAlloc(
218+
mlir::Location loc, mlir::Type type, llvm::StringRef name,
219+
mlir::ValueRange lenParams = {}, mlir::ValueRange shape = {},
220+
llvm::ArrayRef<mlir::NamedAttribute> attrs = {},
221+
std::optional<Fortran::common::CUDADataAttr> cudaAttr = std::nullopt);
222222

223223
/// Create a temporary. A temp is allocated using `fir.alloca` and can be read
224224
/// and written using `fir.load` and `fir.store`, resp. The temporary can be
225225
/// given a name via a front-end `Symbol` or a `StringRef`.
226-
mlir::Value createTemporary(mlir::Location loc, mlir::Type type,
227-
llvm::StringRef name = {},
228-
mlir::ValueRange shape = {},
229-
mlir::ValueRange lenParams = {},
230-
llvm::ArrayRef<mlir::NamedAttribute> attrs = {});
226+
mlir::Value createTemporary(
227+
mlir::Location loc, mlir::Type type, llvm::StringRef name = {},
228+
mlir::ValueRange shape = {}, mlir::ValueRange lenParams = {},
229+
llvm::ArrayRef<mlir::NamedAttribute> attrs = {},
230+
std::optional<Fortran::common::CUDADataAttr> cudaAttr = std::nullopt);
231231

232232
/// Create an unnamed and untracked temporary on the stack.
233233
mlir::Value createTemporary(mlir::Location loc, mlir::Type type,

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,10 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
16981698
if (sym.test(Fortran::semantics::Symbol::Flag::CrayPointee)) {
16991699
mlir::Type ptrBoxType =
17001700
Fortran::lower::getCrayPointeeBoxType(base.getType());
1701-
mlir::Value boxAlloc = builder.createTemporary(loc, ptrBoxType);
1701+
mlir::Value boxAlloc = builder.createTemporary(
1702+
loc, ptrBoxType,
1703+
/*name=*/{}, /*shape=*/{}, /*lenParams=*/{}, /*attrs=*/{},
1704+
Fortran::semantics::GetCUDADataAttr(&sym.GetUltimate()));
17021705

17031706
// Declare a local pointer variable.
17041707
auto newBase = builder.create<hlfir::DeclareOp>(

flang/lib/Optimizer/Builder/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ add_flang_library(FIRBuilder
3535
TemporaryStorage.cpp
3636

3737
DEPENDS
38+
CUFAttrs
39+
CUFDialect
3840
FIRDialect
3941
HLFIRDialect
4042
${dialect_libs}
4143
${extension_libs}
4244

4345
LINK_LIBS
46+
CUFAttrs
47+
CUFDialect
4448
FIRDialect
4549
FIRDialectSupport
4650
FIRSupport

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "flang/Optimizer/Builder/Runtime/Assign.h"
1515
#include "flang/Optimizer/Builder/Runtime/Derived.h"
1616
#include "flang/Optimizer/Builder/Todo.h"
17+
#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
1718
#include "flang/Optimizer/Dialect/FIRAttr.h"
1819
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
1920
#include "flang/Optimizer/Dialect/FIRType.h"
@@ -270,25 +271,30 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
270271
mlir::Value fir::FirOpBuilder::createTemporaryAlloc(
271272
mlir::Location loc, mlir::Type type, llvm::StringRef name,
272273
mlir::ValueRange lenParams, mlir::ValueRange shape,
273-
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
274+
llvm::ArrayRef<mlir::NamedAttribute> attrs,
275+
std::optional<Fortran::common::CUDADataAttr> cudaAttr) {
274276
assert(!mlir::isa<fir::ReferenceType>(type) && "cannot be a reference");
275277
// If the alloca is inside an OpenMP Op which will be outlined then pin
276278
// the alloca here.
277279
const bool pinned =
278280
getRegion().getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>();
279-
mlir::Value temp =
280-
create<fir::AllocaOp>(loc, type, /*unique_name=*/llvm::StringRef{}, name,
281-
pinned, lenParams, shape, attrs);
282-
return temp;
281+
if (cudaAttr) {
282+
cuf::DataAttributeAttr attr = cuf::getDataAttribute(getContext(), cudaAttr);
283+
return create<cuf::AllocOp>(loc, type, /*unique_name=*/llvm::StringRef{},
284+
name, attr, lenParams, shape, attrs);
285+
} else {
286+
return create<fir::AllocaOp>(loc, type, /*unique_name=*/llvm::StringRef{},
287+
name, pinned, lenParams, shape, attrs);
288+
}
283289
}
284290

285291
/// Create a temporary variable on the stack. Anonymous temporaries have no
286292
/// `name` value. Temporaries do not require a uniqued name.
287-
mlir::Value
288-
fir::FirOpBuilder::createTemporary(mlir::Location loc, mlir::Type type,
289-
llvm::StringRef name, mlir::ValueRange shape,
290-
mlir::ValueRange lenParams,
291-
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
293+
mlir::Value fir::FirOpBuilder::createTemporary(
294+
mlir::Location loc, mlir::Type type, llvm::StringRef name,
295+
mlir::ValueRange shape, mlir::ValueRange lenParams,
296+
llvm::ArrayRef<mlir::NamedAttribute> attrs,
297+
std::optional<Fortran::common::CUDADataAttr> cudaAttr) {
292298
llvm::SmallVector<mlir::Value> dynamicShape =
293299
fir::factory::elideExtentsAlreadyInType(type, shape);
294300
llvm::SmallVector<mlir::Value> dynamicLength =
@@ -300,8 +306,8 @@ fir::FirOpBuilder::createTemporary(mlir::Location loc, mlir::Type type,
300306
setInsertionPointToStart(getAllocaBlock());
301307
}
302308

303-
mlir::Value ae =
304-
createTemporaryAlloc(loc, type, name, dynamicLength, dynamicShape, attrs);
309+
mlir::Value ae = createTemporaryAlloc(loc, type, name, dynamicLength,
310+
dynamicShape, attrs, cudaAttr);
305311

306312
if (hoistAlloc)
307313
restoreInsertionPoint(insPt);

flang/test/Lower/CUDA/cuda-data-attribute.cuf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ end subroutine
109109

110110
end module
111111

112+
subroutine craypointer()
113+
real, device :: x(*); pointer(px, x)
114+
end
112115

116+
! CHECK-LABEL: func.func @_QPcraypointer()
117+
! CHECK: %{{.*}} = cuf.alloc !fir.box<!fir.ptr<!fir.array<?xf32>>> {data_attr = #cuf.cuda<device>} -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>

0 commit comments

Comments
 (0)