-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang][cuda] Set PINNED variable to false in ALLOCATE #121593
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
Conversation
@llvm/pr-subscribers-flang-fir-hlfir Author: Valentin Clement (バレンタイン クレメン) (clementval) ChangesWhen Full diff: https://github.com/llvm/llvm-project/pull/121593.diff 2 Files Affected:
diff --git a/flang/lib/Lower/Allocatable.cpp b/flang/lib/Lower/Allocatable.cpp
index 4c64870675816e..531056abfc0113 100644
--- a/flang/lib/Lower/Allocatable.cpp
+++ b/flang/lib/Lower/Allocatable.cpp
@@ -454,6 +454,19 @@ class AllocateStmtHelper {
alloc.getSymbol());
}
+ void setPinnedToFalse() {
+ if (!pinnedExpr)
+ return;
+ Fortran::lower::StatementContext stmtCtx;
+ mlir::Value pinned =
+ fir::getBase(converter.genExprAddr(loc, *pinnedExpr, stmtCtx));
+ mlir::Location loc = pinned.getLoc();
+ mlir::Value falseValue = builder.createBool(loc, false);
+ mlir::Value falseConv = builder.createConvert(
+ loc, fir::unwrapRefType(pinned.getType()), falseValue);
+ builder.create<fir::StoreOp>(loc, falseConv, pinned);
+ }
+
void genSimpleAllocation(const Allocation &alloc,
const fir::MutableBoxValue &box) {
bool isCudaSymbol = Fortran::semantics::HasCUDAAttr(alloc.getSymbol());
@@ -469,6 +482,7 @@ class AllocateStmtHelper {
// can be validated.
genInlinedAllocation(alloc, box);
postAllocationAction(alloc);
+ setPinnedToFalse();
return;
}
@@ -482,11 +496,13 @@ class AllocateStmtHelper {
genSetDeferredLengthParameters(alloc, box);
genAllocateObjectBounds(alloc, box);
mlir::Value stat;
- if (!isCudaSymbol)
+ if (!isCudaSymbol) {
stat = genRuntimeAllocate(builder, loc, box, errorManager);
- else
+ setPinnedToFalse();
+ } else {
stat =
genCudaAllocate(builder, loc, box, errorManager, alloc.getSymbol());
+ }
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
postAllocationAction(alloc);
errorManager.assignStat(builder, loc, stat);
@@ -616,13 +632,16 @@ class AllocateStmtHelper {
genSetDeferredLengthParameters(alloc, box);
genAllocateObjectBounds(alloc, box);
mlir::Value stat;
- if (Fortran::semantics::HasCUDAAttr(alloc.getSymbol()))
+ if (Fortran::semantics::HasCUDAAttr(alloc.getSymbol())) {
stat =
genCudaAllocate(builder, loc, box, errorManager, alloc.getSymbol());
- else if (isSource)
- stat = genRuntimeAllocateSource(builder, loc, box, exv, errorManager);
- else
- stat = genRuntimeAllocate(builder, loc, box, errorManager);
+ } else {
+ if (isSource)
+ stat = genRuntimeAllocateSource(builder, loc, box, exv, errorManager);
+ else
+ stat = genRuntimeAllocate(builder, loc, box, errorManager);
+ setPinnedToFalse();
+ }
fir::factory::syncMutableBoxFromIRBox(builder, loc, box);
postAllocationAction(alloc);
errorManager.assignStat(builder, loc, stat);
diff --git a/flang/test/Lower/CUDA/cuda-allocatable.cuf b/flang/test/Lower/CUDA/cuda-allocatable.cuf
index 6479425c58d8be..8b287f859aa76b 100644
--- a/flang/test/Lower/CUDA/cuda-allocatable.cuf
+++ b/flang/test/Lower/CUDA/cuda-allocatable.cuf
@@ -196,3 +196,30 @@ end subroutine
! CHECK: %[[BOX:.*]] = fir.load %[[A]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
! CHECK: %[[BOXADDR:.*]] = fir.box_addr %[[BOX]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>) -> !fir.heap<!fir.array<?xf32>>
! CHECK: fir.freemem %[[BOXADDR]] : !fir.heap<!fir.array<?xf32>>
+
+subroutine setpinned()
+ integer, allocatable :: i(:)
+ logical :: plog
+ allocate(i(10), pinned=plog)
+end
+
+! CHECK-LABEL: func.func @_QPsetpinned()
+! CHECK: %[[PLOG:.*]] = fir.alloca !fir.logical<4> {bindc_name = "plog", uniq_name = "_QFsetpinnedEplog"}
+! CHECK: %[[PLOG_DECL:.*]]:2 = hlfir.declare %[[PLOG]] {uniq_name = "_QFsetpinnedEplog"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
+! CHECK: %[[FALSE:.*]] = arith.constant false
+! CHECK: %[[FLASE_CONV:.*]] = fir.convert %[[FALSE]] : (i1) -> !fir.logical<4>
+! CHECK: fir.store %[[FLASE_CONV]] to %[[PLOG_DECL]]#1 : !fir.ref<!fir.logical<4>>
+
+subroutine setpinnedpointer()
+ integer, pointer :: i(:)
+ logical :: plog
+ allocate(i(10), pinned=plog)
+end
+
+! CHECK-LABEL: func.func @_QPsetpinnedpointer()
+! CHECK: %[[PLOG:.*]] = fir.alloca !fir.logical<4> {bindc_name = "plog", uniq_name = "_QFsetpinnedpointerEplog"}
+! CHECK: %[[PLOG_DECL:.*]]:2 = hlfir.declare %[[PLOG]] {uniq_name = "_QFsetpinnedpointerEplog"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
+! CHECK: fir.call @_FortranAPointerAllocate
+! CHECK: %[[FALSE:.*]] = arith.constant false
+! CHECK: %[[FLASE_CONV:.*]] = fir.convert %[[FALSE]] : (i1) -> !fir.logical<4>
+! CHECK: fir.store %[[FLASE_CONV]] to %[[PLOG_DECL]]#1 : !fir.ref<!fir.logical<4>>
|
Can you also add a test that when |
When the variable has the |
When
PINNED=
is used with variables that don't have thePINNED
attribute, the logical value must be set to false when host allocation is performed.