LAA: improve code in getStrideFromPointer (NFC) (#124780)
[llvm-project.git] / flang / runtime / CUDA / pointer.cpp
blob3252410bd8d2c2c1a1040d70c5adb3404d189d1b
1 //===-- runtime/CUDA/pointer.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "flang/Runtime/CUDA/pointer.h"
10 #include "../assign-impl.h"
11 #include "../stat.h"
12 #include "../terminator.h"
13 #include "flang/Runtime/CUDA/descriptor.h"
14 #include "flang/Runtime/CUDA/memmove-function.h"
15 #include "flang/Runtime/pointer.h"
17 #include "cuda_runtime.h"
19 namespace Fortran::runtime::cuda {
21 extern "C" {
22 RT_EXT_API_GROUP_BEGIN
24 int RTDEF(CUFPointerAllocate)(Descriptor &desc, int64_t stream, bool hasStat,
25 const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
26 if (desc.HasAddendum()) {
27 Terminator terminator{sourceFile, sourceLine};
28 // TODO: This require a bit more work to set the correct type descriptor
29 // address
30 terminator.Crash(
31 "not yet implemented: CUDA descriptor allocation with addendum");
33 // Perform the standard allocation.
34 int stat{
35 RTNAME(PointerAllocate)(desc, hasStat, errMsg, sourceFile, sourceLine)};
36 return stat;
39 int RTDEF(CUFPointerAllocateSync)(Descriptor &desc, int64_t stream,
40 bool hasStat, const Descriptor *errMsg, const char *sourceFile,
41 int sourceLine) {
42 int stat{RTNAME(CUFPointerAllocate)(
43 desc, stream, hasStat, errMsg, sourceFile, sourceLine)};
44 #ifndef RT_DEVICE_COMPILATION
45 // Descriptor synchronization is only done when the allocation is done
46 // from the host.
47 if (stat == StatOk) {
48 void *deviceAddr{
49 RTNAME(CUFGetDeviceAddress)((void *)&desc, sourceFile, sourceLine)};
50 RTNAME(CUFDescriptorSync)
51 ((Descriptor *)deviceAddr, &desc, sourceFile, sourceLine);
53 #endif
54 return stat;
57 int RTDEF(CUFPointerAllocateSource)(Descriptor &pointer,
58 const Descriptor &source, int64_t stream, bool hasStat,
59 const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
60 int stat{RTNAME(CUFPointerAllocate)(
61 pointer, stream, hasStat, errMsg, sourceFile, sourceLine)};
62 if (stat == StatOk) {
63 Terminator terminator{sourceFile, sourceLine};
64 Fortran::runtime::DoFromSourceAssign(
65 pointer, source, terminator, &MemmoveHostToDevice);
67 return stat;
70 int RTDEF(CUFPointerAllocateSourceSync)(Descriptor &pointer,
71 const Descriptor &source, int64_t stream, bool hasStat,
72 const Descriptor *errMsg, const char *sourceFile, int sourceLine) {
73 int stat{RTNAME(CUFPointerAllocateSync)(
74 pointer, stream, hasStat, errMsg, sourceFile, sourceLine)};
75 if (stat == StatOk) {
76 Terminator terminator{sourceFile, sourceLine};
77 Fortran::runtime::DoFromSourceAssign(
78 pointer, source, terminator, &MemmoveHostToDevice);
80 return stat;
83 RT_EXT_API_GROUP_END
85 } // extern "C"
87 } // namespace Fortran::runtime::cuda