Skip to content

Commit a1ba5b8

Browse files
[Flang-RT] Changes required for embedded GPU LLVM IR Flang runtime
1 parent 726b6f0 commit a1ba5b8

File tree

17 files changed

+81
-15
lines changed

17 files changed

+81
-15
lines changed

flang-rt/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ if (NOT "${FLANG_RT_LIBCXX_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
152152
endif ()
153153

154154
option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." ON)
155+
option(FLANG_RT_EMBED_GPU_LLVM_IR "Build Flang-RT as GPU LLVM IR library" ON)
156+
155157
if (WIN32)
156158
# Windows DLL currently not implemented.
157159
set(FLANG_RT_ENABLE_SHARED OFF)
@@ -327,7 +329,9 @@ endif ()
327329

328330
if (FLANG_RT_INCLUDE_TESTS)
329331
add_subdirectory(test)
330-
add_subdirectory(unittests)
332+
if (NOT "${LLVM_RUNTIMES_TARGET}" MATCHES "^amdgcn|^nvptx")
333+
add_subdirectory(unittests)
334+
endif()
331335
else ()
332336
add_custom_target(check-flang-rt)
333337
endif()

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ function (add_flangrt_library name)
219219
# Minimum required C++ version for Flang-RT, even if CMAKE_CXX_STANDARD is defined to something else.
220220
target_compile_features(${tgtname} PRIVATE cxx_std_17)
221221

222+
# Determine which version of GPU Flang-RT we want to build:
223+
# flang_rt.hostdevice or the implicitly linked device flang_rt.runtime.
224+
if (FLANG_RT_EMBED_GPU_LLVM_IR)
225+
target_compile_definitions(${tgtname} PRIVATE EMBED_FLANG_RT_GPU_LLVM_IR)
226+
endif ()
227+
222228
# When building the flang runtime if LTO is enabled the archive file
223229
# contains LLVM IR rather than object code. Currently flang is not
224230
# LTO aware so cannot link this file to compiled Fortran code.

flang-rt/include/flang-rt/runtime/lock.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#endif
2424

2525
#if USE_PTHREADS
26+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined(EMBED_FLANG_RT_GPU_LLVM_IR)
2627
#include <pthread.h>
28+
#endif
2729
#elif defined(_WIN32)
2830
#include "flang/Common/windows-include.h"
2931
#else
@@ -45,6 +47,7 @@ class Lock {
4547
RT_API_ATTRS void Drop() {}
4648
RT_API_ATTRS bool TakeIfNoDeadlock() { return true; }
4749
#elif USE_PTHREADS
50+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined(EMBED_FLANG_RT_GPU_LLVM_IR)
4851
Lock() { pthread_mutex_init(&mutex_, nullptr); }
4952
~Lock() { pthread_mutex_destroy(&mutex_); }
5053
void Take() {
@@ -68,6 +71,14 @@ class Lock {
6871
isBusy_ = false;
6972
pthread_mutex_unlock(&mutex_);
7073
}
74+
#else
75+
RT_API_ATTRS void Take(){}
76+
RT_API_ATTRS bool TakeIfNoDeadlock() {return true;}
77+
RT_API_ATTRS bool Try() {return true;}
78+
RT_API_ATTRS void Drop() {}
79+
Lock() {}
80+
~Lock() {}
81+
#endif
7182
#elif defined(_WIN32)
7283
Lock() { InitializeCriticalSection(&cs_); }
7384
~Lock() { DeleteCriticalSection(&cs_); }
@@ -91,9 +102,11 @@ class Lock {
91102
#if RT_USE_PSEUDO_FILE_UNIT
92103
// No state.
93104
#elif USE_PTHREADS
105+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined(EMBED_FLANG_RT_GPU_LLVM_IR)
94106
pthread_mutex_t mutex_{};
95107
volatile bool isBusy_{false};
96108
volatile pthread_t holder_;
109+
#endif
97110
#elif defined(_WIN32)
98111
CRITICAL_SECTION cs_;
99112
#else

flang-rt/include/flang-rt/runtime/tools.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
#define RT_USE_PSEUDO_FILE_UNIT 1
4343
#endif
4444

45+
#if (defined(__AMDGPU__) || defined(__NVPTX__)) && defined(EMBED_FLANG_RT_GPU_LLVM_IR)
46+
// Use the pseudo lock and pseudo file unit implementations
47+
// for the device.
48+
#define RT_USE_PSEUDO_LOCK 1
49+
#define RT_USE_PSEUDO_FILE_UNIT 1
50+
#endif
51+
4552
namespace Fortran::runtime {
4653

4754
class Terminator;

flang-rt/lib/runtime/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ else ()
178178
endif ()
179179

180180
if ("${LLVM_RUNTIMES_TARGET}" MATCHES "^amdgcn|^nvptx")
181-
set(sources ${gpu_sources})
181+
if (FLANG_RT_EMBED_GPU_LLVM_IR)
182+
set(sources ${supported_sources} ${gpu_sources})
183+
else ()
184+
set(sources ${gpu_sources})
185+
endif ()
182186
elseif(FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")
183187
set(sources ${supported_sources})
184188
else ()

flang-rt/lib/runtime/assign.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "flang/Runtime/assign.h"
10+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined(EMBED_FLANG_RT_GPU_LLVM_IR)
1011
#include "flang/Runtime/stop.h"
12+
#endif
1113
#include "flang-rt/runtime/assign-impl.h"
1214
#include "flang-rt/runtime/derived.h"
1315
#include "flang-rt/runtime/descriptor.h"
@@ -862,17 +864,6 @@ void RTDEF(AssignPolymorphic)(Descriptor &to, const Descriptor &from,
862864
PolymorphicLHS);
863865
}
864866

865-
#if defined(OMP_OFFLOAD_BUILD)
866-
// To support a recently added use of variant in the OpenMP offload build,
867-
// added an abort wrapper which calls the flang-rt FortranAAbort.
868-
// Avoids the following linker error:
869-
// ld.lld: error: undefined symbol: abort
870-
// >>> referenced by /tmp/device_aassign.amdgcn.gfx90a-34a7ed.img.lto.o:(std::__throw_bad_variant_access(char const*))
871-
extern "C" void abort(void) {
872-
RTNAME(Abort)();
873-
}
874-
#endif
875-
876867
RT_EXT_API_GROUP_END
877868
} // extern "C"
878869
} // namespace Fortran::runtime

flang-rt/lib/runtime/descriptor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#include "flang-rt/runtime/descriptor.h"
1010
#include "ISO_Fortran_util.h"
11+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined(EMBED_FLANG_RT_GPU_LLVM_IR)
1112
#include "memory.h"
13+
#endif
1214
#include "flang-rt/runtime/allocator-registry.h"
1315
#include "flang-rt/runtime/derived.h"
1416
#include "flang-rt/runtime/stat.h"

flang-rt/lib/runtime/edit-input.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,11 @@ static RT_API_ATTRS void RaiseFPExceptions(
569569
#ifdef feraisexcept // a macro in some environments; omit std::
570570
#define RAISE feraiseexcept
571571
#else
572+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined (EMBED_FLANG_RT_GPU_LLVM_IR)
572573
#define RAISE std::feraiseexcept
574+
#else
575+
#define RAISE
576+
#endif
573577
#endif
574578
#endif // !defined(RT_DEVICE_COMPILATION)
575579

flang-rt/lib/runtime/environment.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#if (not defined (__AMDGPU__) && not defined(__NVPTX__)) || not defined (EMBED_FLANG_RT_GPU_LLVM_IR)
910
#include "flang-rt/runtime/environment.h"
1011
#include "environment-default-list.h"
1112
#include "memory.h"
@@ -334,3 +335,4 @@ bool RTNAME(RegisterConfigureEnv)(
334335
} // extern "C"
335336

336337
} // namespace Fortran::runtime
338+
#endif

flang-rt/lib/runtime/file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#if (not defined(__AMDGPU__) && not defined(__NVPTX__)) || not defined(EMBED_FLANG_RT_GPU_LLVM_IR)
910
#include "flang-rt/runtime/file.h"
1011
#include "flang-rt/runtime/memory.h"
1112
#include "flang-rt/runtime/tools.h"
@@ -486,3 +487,4 @@ RT_API_ATTRS std::int64_t SizeInBytes(const char *path) {
486487
#endif // defined(RT_DEVICE_COMPILATION)
487488

488489
} // namespace Fortran::runtime::io
490+
#endif

0 commit comments

Comments
 (0)