Skip to content

[flang][cuda] Fix allocation of descriptor for cray pointer #103474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions flang/include/flang/Optimizer/Builder/FIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,20 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
/// Create a temporary using `fir.alloca`. This function does not hoist.
/// It is the callers responsibility to set the insertion point if
/// hoisting is required.
mlir::Value
createTemporaryAlloc(mlir::Location loc, mlir::Type type,
llvm::StringRef name, mlir::ValueRange lenParams = {},
mlir::ValueRange shape = {},
llvm::ArrayRef<mlir::NamedAttribute> attrs = {});
mlir::Value createTemporaryAlloc(
mlir::Location loc, mlir::Type type, llvm::StringRef name,
mlir::ValueRange lenParams = {}, mlir::ValueRange shape = {},
llvm::ArrayRef<mlir::NamedAttribute> attrs = {},
std::optional<Fortran::common::CUDADataAttr> cudaAttr = std::nullopt);

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

/// Create an unnamed and untracked temporary on the stack.
mlir::Value createTemporary(mlir::Location loc, mlir::Type type,
Expand Down
5 changes: 4 additions & 1 deletion flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,10 @@ static void genDeclareSymbol(Fortran::lower::AbstractConverter &converter,
if (sym.test(Fortran::semantics::Symbol::Flag::CrayPointee)) {
mlir::Type ptrBoxType =
Fortran::lower::getCrayPointeeBoxType(base.getType());
mlir::Value boxAlloc = builder.createTemporary(loc, ptrBoxType);
mlir::Value boxAlloc = builder.createTemporary(
loc, ptrBoxType,
/*name=*/{}, /*shape=*/{}, /*lenParams=*/{}, /*attrs=*/{},
Fortran::semantics::GetCUDADataAttr(&sym.GetUltimate()));

// Declare a local pointer variable.
auto newBase = builder.create<hlfir::DeclareOp>(
Expand Down
4 changes: 4 additions & 0 deletions flang/lib/Optimizer/Builder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ add_flang_library(FIRBuilder
TemporaryStorage.cpp

DEPENDS
CUFAttrs
CUFDialect
FIRDialect
HLFIRDialect
${dialect_libs}
${extension_libs}

LINK_LIBS
CUFAttrs
CUFDialect
FIRDialect
FIRDialectSupport
FIRSupport
Expand Down
30 changes: 18 additions & 12 deletions flang/lib/Optimizer/Builder/FIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "flang/Optimizer/Builder/Runtime/Assign.h"
#include "flang/Optimizer/Builder/Runtime/Derived.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
#include "flang/Optimizer/Dialect/FIRAttr.h"
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Dialect/FIRType.h"
Expand Down Expand Up @@ -270,25 +271,30 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
mlir::Value fir::FirOpBuilder::createTemporaryAlloc(
mlir::Location loc, mlir::Type type, llvm::StringRef name,
mlir::ValueRange lenParams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
llvm::ArrayRef<mlir::NamedAttribute> attrs,
std::optional<Fortran::common::CUDADataAttr> cudaAttr) {
assert(!mlir::isa<fir::ReferenceType>(type) && "cannot be a reference");
// If the alloca is inside an OpenMP Op which will be outlined then pin
// the alloca here.
const bool pinned =
getRegion().getParentOfType<mlir::omp::OutlineableOpenMPOpInterface>();
mlir::Value temp =
create<fir::AllocaOp>(loc, type, /*unique_name=*/llvm::StringRef{}, name,
pinned, lenParams, shape, attrs);
return temp;
if (cudaAttr) {
cuf::DataAttributeAttr attr = cuf::getDataAttribute(getContext(), cudaAttr);
return create<cuf::AllocOp>(loc, type, /*unique_name=*/llvm::StringRef{},
name, attr, lenParams, shape, attrs);
} else {
return create<fir::AllocaOp>(loc, type, /*unique_name=*/llvm::StringRef{},
name, pinned, lenParams, shape, attrs);
}
}

/// Create a temporary variable on the stack. Anonymous temporaries have no
/// `name` value. Temporaries do not require a uniqued name.
mlir::Value
fir::FirOpBuilder::createTemporary(mlir::Location loc, mlir::Type type,
llvm::StringRef name, mlir::ValueRange shape,
mlir::ValueRange lenParams,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
mlir::Value fir::FirOpBuilder::createTemporary(
mlir::Location loc, mlir::Type type, llvm::StringRef name,
mlir::ValueRange shape, mlir::ValueRange lenParams,
llvm::ArrayRef<mlir::NamedAttribute> attrs,
std::optional<Fortran::common::CUDADataAttr> cudaAttr) {
llvm::SmallVector<mlir::Value> dynamicShape =
fir::factory::elideExtentsAlreadyInType(type, shape);
llvm::SmallVector<mlir::Value> dynamicLength =
Expand All @@ -300,8 +306,8 @@ fir::FirOpBuilder::createTemporary(mlir::Location loc, mlir::Type type,
setInsertionPointToStart(getAllocaBlock());
}

mlir::Value ae =
createTemporaryAlloc(loc, type, name, dynamicLength, dynamicShape, attrs);
mlir::Value ae = createTemporaryAlloc(loc, type, name, dynamicLength,
dynamicShape, attrs, cudaAttr);

if (hoistAlloc)
restoreInsertionPoint(insPt);
Expand Down
5 changes: 5 additions & 0 deletions flang/test/Lower/CUDA/cuda-data-attribute.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ end subroutine

end module

subroutine craypointer()
real, device :: x(*); pointer(px, x)
end

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