Skip to content

Commit 987a889

Browse files
committed
[Flang] Add Assign_omp fortran-rt function
This commit is c-p from ivanradanov commit d7e4499
1 parent cd84582 commit 987a889

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

flang-rt/lib/runtime/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(supported_sources
2121
allocatable.cpp
2222
array-constructor.cpp
2323
assign.cpp
24+
assign_omp.cpp
2425
buffer.cpp
2526
character.cpp
2627
connection.cpp
@@ -97,6 +98,7 @@ set(gpu_sources
9798
allocatable.cpp
9899
array-constructor.cpp
99100
assign.cpp
101+
assign_omp.cpp
100102
buffer.cpp
101103
character.cpp
102104
connection.cpp

flang-rt/lib/runtime/assign_omp.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===-- lib/runtime/assign_omp.cpp ----------------------------------*- C++ -*-===//
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+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Runtime/assign.h"
10+
#include "flang-rt/runtime/assign-impl.h"
11+
#include "flang-rt/runtime/derived.h"
12+
#include "flang-rt/runtime/descriptor.h"
13+
#include "flang-rt/runtime/stat.h"
14+
#include "flang-rt/runtime/terminator.h"
15+
#include "flang-rt/runtime/tools.h"
16+
#include "flang-rt/runtime/type-info.h"
17+
18+
#include <omp.h>
19+
20+
namespace Fortran::runtime {
21+
22+
RT_API_ATTRS static void Assign(Descriptor &to, const Descriptor &from,
23+
Terminator &terminator, int flags, int32_t omp_device) {
24+
std::size_t toElementBytes{to.ElementBytes()};
25+
std::size_t fromElementBytes{from.ElementBytes()};
26+
std::size_t toElements{to.Elements()};
27+
std::size_t fromElements{from.Elements()};
28+
29+
if (toElementBytes != fromElementBytes)
30+
terminator.Crash("Assign: toElementBytes != fromElementBytes");
31+
if (toElements != fromElements)
32+
terminator.Crash("Assign: toElements != fromElements");
33+
34+
void *host_to_ptr = to.raw().base_addr;
35+
void *host_from_ptr = from.raw().base_addr;
36+
size_t length = toElements * toElementBytes;
37+
38+
printf("assign length: %zu\n", length);
39+
40+
if (!omp_target_is_present(host_to_ptr, omp_device))
41+
terminator.Crash("Assign: !omp_target_is_present(host_to_ptr, omp_device)");
42+
if (!omp_target_is_present(host_from_ptr, omp_device))
43+
terminator.Crash(
44+
"Assign: !omp_target_is_present(host_from_ptr, omp_device)");
45+
46+
printf("host_to_ptr: %p\n", host_to_ptr);
47+
#pragma omp target data use_device_ptr(host_to_ptr, host_from_ptr) device(omp_device)
48+
{
49+
printf("device_to_ptr: %p\n", host_to_ptr);
50+
// TODO do we need to handle overlapping memory? does this function do that?
51+
omp_target_memcpy(host_to_ptr, host_from_ptr, length, /*dst_offset*/ 0,
52+
/*src_offset*/ 0, /*dst*/ omp_device, /*src*/ omp_device);
53+
}
54+
55+
return;
56+
}
57+
58+
extern "C" {
59+
RT_EXT_API_GROUP_BEGIN
60+
void RTDEF(Assign_omp)(Descriptor &to, const Descriptor &from,
61+
const char *sourceFile, int sourceLine, int32_t omp_device) {
62+
Terminator terminator{sourceFile, sourceLine};
63+
// All top-level defined assignments can be recognized in semantics and
64+
// will have been already been converted to calls, so don't check for
65+
// defined assignment apart from components.
66+
Assign(to, from, terminator,
67+
MaybeReallocate | NeedFinalization | ComponentCanBeDefinedAssignment,
68+
omp_device);
69+
}
70+
} // extern "C"
71+
72+
}

flang/include/flang/Runtime/assign.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ extern "C" {
5555
// API for lowering assignment
5656
void RTDECL(Assign)(Descriptor &to, const Descriptor &from,
5757
const char *sourceFile = nullptr, int sourceLine = 0);
58+
void RTDECL(Assign_omp)(Descriptor &to, const Descriptor &from,
59+
const char *sourceFile = nullptr, int sourceLine = 0, int32_t omp_device = 0);
5860
// This variant has no finalization, defined assignment, or allocatable
5961
// reallocation.
6062
void RTDECL(AssignTemporary)(Descriptor &to, const Descriptor &from,

flang/lib/Optimizer/OpenMP/LowerWorkdistribute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ static void moveToHost(omp::TargetOp targetOp, RewriterBase &rewriter) {
809809
auto module = runtimeCall->getParentOfType<ModuleOp>();
810810
auto callee = cast<func::FuncOp>(
811811
module.lookupSymbol(runtimeCall.getCalleeAttr()));
812-
std::string newCalleeName = (callee.getName()).str();
812+
std::string newCalleeName = (callee.getName() + "_omp").str();
813813
mlir::OpBuilder moduleBuilder(module.getBodyRegion());
814814
func::FuncOp newCallee =
815815
cast_or_null<func::FuncOp>(module.lookupSymbol(newCalleeName));

0 commit comments

Comments
 (0)