Skip to content

Commit 5468bc8

Browse files
jplehrDavid Salinas
authored andcommitted
[OpenMP][USM] Introduces -fopenmp-force-usm flag
This flag forces the compiler to generate code for OpenMP target regions as if the user specified the #pragma omp requires unified_shared_memory in each source file. The option does not have a -fno-* friend since OpenMP requires the unified_shared_memory clause to be present in all source files. Since this flag does no harm if the clause is present, it can be used in conjunction. My understanding is that USM should not be turned off selectively, hence, no -fno- version. This adds a basic test to check the correct generation of double indirect access to declare target globals in USM mode vs non-USM mode. Which I think is the only difference observable in code generation. This runtime test checks for the (non-)occurence of data movement between host and device. It does one run without the flag and one with the flag to also see that both versions behave as expected. In the case w/o the new flag data movement between host and device is expected. In the case with the flag such data movement should not be present / reported. Change-Id: I057fcf59ba2648018780b741cc28c532c50223ea
1 parent 236f860 commit 5468bc8

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ LANGOPT(OpenMPTeamSubscription , 1, 0, "Assume distributed loops do not have mo
268268
LANGOPT(OpenMPNoThreadState , 1, 0, "Assume that no thread in a parallel region will modify an ICV.")
269269
LANGOPT(OpenMPNoNestedParallelism , 1, 0, "Assume that no thread in a parallel region will encounter a parallel region")
270270
LANGOPT(OpenMPOffloadMandatory , 1, 0, "Assert that offloading is mandatory and do not create a host fallback.")
271+
LANGOPT(OpenMPForceUSM , 1, 0, "Enable OpenMP unified shared memory mode via compiler.")
271272
LANGOPT(NoGPULib , 1, 0, "Indicate a build without the standard GPU libraries.")
272273
LANGOPT(RenderScript , 1, 0, "RenderScript")
273274

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3446,7 +3446,6 @@ def fno_openmp_target_fast_reduction : Flag<["-"], "fno-openmp-target-fast-reduc
34463446
Flags<[NoArgumentUnused, HelpHidden]>, Visibility<[ClangOption, CC1Option]>,
34473447
HelpText<"Do not use the fast reduction code generation technique">,
34483448
MarshallingInfoFlag<LangOpts<"OpenMPTargetFastReduction">>;
3449-
def fopenmp_force_usm : Flag<["-"], "fopenmp-force-usm">, Group<f_Group>, Flags<[NoArgumentUnused, HelpHidden]>, Visibility<[ClangOption, CC1Option]>;
34503449

34513450
//===----------------------------------------------------------------------===//
34523451
// Shared cc1 + fc1 OpenMP Target Options
@@ -3525,6 +3524,10 @@ def fopenmp_offload_mandatory : Flag<["-"], "fopenmp-offload-mandatory">, Group<
35253524
Flags<[NoArgumentUnused]>, Visibility<[ClangOption, CC1Option]>,
35263525
HelpText<"Do not create a host fallback if offloading to the device fails.">,
35273526
MarshallingInfoFlag<LangOpts<"OpenMPOffloadMandatory">>;
3527+
def fopenmp_force_usm : Flag<["-"], "fopenmp-force-usm">, Group<f_Group>,
3528+
Flags<[NoArgumentUnused]>, Visibility<[ClangOption, CC1Option]>,
3529+
HelpText<"Force behvaior as if the user specified pragma omp requires unified_shared_memory.">,
3530+
MarshallingInfoFlag<LangOpts<"OpenMPForceUSM">>;
35283531
def fopenmp_target_jit : Flag<["-"], "fopenmp-target-jit">, Group<f_Group>,
35293532
Flags<[NoArgumentUnused]>, Visibility<[ClangOption, CLOption]>,
35303533
HelpText<"Emit code that can be JIT compiled for OpenMP offloading. Implies -foffload-lto=full">;

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,13 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
10651065
? CGM.getLangOpts().OMPHostIRFile
10661066
: StringRef{});
10671067
OMPBuilder.setConfig(Config);
1068+
1069+
// The user forces the compiler to behave as if omp requires
1070+
// unified_shared_memory was given.
1071+
if (CGM.getLangOpts().OpenMPForceUSM) {
1072+
HasRequiresUnifiedSharedMemory = true;
1073+
OMPBuilder.Config.setHasRequiresUnifiedSharedMemory(true);
1074+
}
10681075
}
10691076

10701077
void CGOpenMPRuntime::clear() {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,6 +6545,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
65456545

65466546
if (Args.hasArg(options::OPT_fopenmp_offload_mandatory))
65476547
CmdArgs.push_back("-fopenmp-offload-mandatory");
6548+
if (Args.hasArg(options::OPT_fopenmp_force_usm))
6549+
CmdArgs.push_back("-fopenmp-force-usm");
65486550
break;
65496551
default:
65506552
// By default, if Clang doesn't know how to generate useful OpenMP code

clang/test/OpenMP/force-usm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --include-generated-funcs --replace-value-regex "__omp_offloading_[0-9a-z]+_[0-9a-z]+" "pl_cond[.].+[.|,]" --prefix-filecheck-ir-name _ --version 3
22
// REQUIRES: amdgpu-registered-target
33

4-
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -include %S/../../lib/Headers/openmp_wrappers/usm/force_usm.h -emit-llvm-bc %s -o %t-ppc-host.bc
5-
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -include %S/../../lib/Headers/openmp_wrappers/usm/force_usm.h -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck -check-prefix=CHECK-USM %s
4+
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-force-usm -emit-llvm-bc %s -o %t-ppc-host.bc
5+
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-force-usm -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck -check-prefix=CHECK-USM %s
66

77
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm-bc %s -o %t-ppc-host.bc
88
// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-is-device -fopenmp-host-ir-file-path %t-ppc-host.bc -o - | FileCheck -check-prefix=CHECK-DEFAULT %s

openmp/libomptarget/test/lit.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ for libomptarget_target in config.libomptarget_all_targets:
200200
"%libomptarget-compile-and-run-" + libomptarget_target))
201201
config.substitutions.append(("%libomptarget-compilexx-generic",
202202
"%libomptarget-compilexx-" + libomptarget_target))
203+
config.substitutions.append(("%libomptarget-compilexxx-generic-force-usm",
204+
"%libomptarget-compilexxx-force-usm-" + libomptarget_target))
203205
config.substitutions.append(("%libomptarget-compile-generic",
204206
"%libomptarget-compile-" + libomptarget_target))
205207
config.substitutions.append(("%libomptarget-compile-fortran-generic",
@@ -257,6 +259,9 @@ for libomptarget_target in config.libomptarget_all_targets:
257259
config.substitutions.append(("%libomptarget-compilexx-" + \
258260
libomptarget_target, \
259261
"%clangxx-" + libomptarget_target + add_libraries(" %s -o %t")))
262+
config.substitutions.append(("%libomptarget-compilexxx-force-usm-" +
263+
libomptarget_target, "%clangxxx-force-usm-" + libomptarget_target + \
264+
add_libraries(" %s -o %t")))
260265
config.substitutions.append(("%libomptarget-compile-" + \
261266
libomptarget_target, \
262267
"%clang-" + libomptarget_target + add_libraries(" %s -o %t")))
@@ -296,6 +301,9 @@ for libomptarget_target in config.libomptarget_all_targets:
296301
remove_suffix_if_present(libomptarget_target) + " -Xopenmp-target=" +\
297302
remove_suffix_if_present(libomptarget_target) + " -march=" +\
298303
get_arch_from_target(libomptarget_target)))
304+
config.substitutions.append(("%clangxxx-force-usm-" + libomptarget_target, \
305+
"%clangxx %openmp_flags -fopenmp-force-usm %cuda_flags %flags %flags_clang -fopenmp-targets=" +\
306+
remove_suffix_if_present(libomptarget_target)))
299307
config.substitutions.append(("%clang-" + libomptarget_target, \
300308
"%clang %openmp_flags %cuda_flags %flags %flags_clang -fopenmp-targets=" +\
301309
remove_suffix_if_present(libomptarget_target) + " -Xopenmp-target=" +\
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// clang-format off
2+
// RUN: %libomptarget-compilexx-generic
3+
// RUN: env LIBOMPTARGET_INFO=32 %libomptarget-run-generic 2>&1 | %fcheck-generic --check-prefix=NO-USM
4+
//
5+
// RUN: %libomptarget-compilexxx-generic-force-usm
6+
// RUN: env HSA_XNACK=1 LIBOMPTARGET_INFO=32 \
7+
// RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic --check-prefix=FORCE-USM
8+
//
9+
// UNSUPPORTED: nvptx64-nvidia-cuda
10+
// UNSUPPORTED: nvptx64-nvidia-cuda-LTO
11+
// clang-format on
12+
13+
#include <cassert>
14+
#include <cstdio>
15+
#include <cstdlib>
16+
17+
int GI;
18+
#pragma omp declare target
19+
int *pGI;
20+
#pragma omp end declare target
21+
22+
int main(void) {
23+
24+
GI = 0;
25+
// Implicit mappings
26+
int alpha = 1;
27+
int beta[3] = {2, 5, 8};
28+
29+
// Require map clauses for non-USM execution
30+
pGI = (int *)malloc(sizeof(int));
31+
*pGI = 42;
32+
33+
#pragma omp target map(pGI[ : 1], GI)
34+
{
35+
GI = 1 * alpha;
36+
*pGI = 2 * beta[1];
37+
}
38+
39+
assert(GI == 1);
40+
assert(*pGI == 10);
41+
42+
printf("SUCCESS\n");
43+
44+
return 0;
45+
}
46+
47+
// clang-format off
48+
// NO-USM: omptarget device 0 info: Copying data from host to device, HstPtr={{.*}}, TgtPtr={{.*}}, Size=4
49+
// NO-USM-NEXT: omptarget device 0 info: Copying data from host to device, HstPtr={{.*}}, TgtPtr={{.*}}, Size=12
50+
// NO-USM-NEXT: omptarget device 0 info: Copying data from host to device, HstPtr={{.*}}, TgtPtr={{.*}}, Size=4
51+
// NO-USM-NEXT: omptarget device 0 info: Copying data from host to device, HstPtr={{.*}}, TgtPtr={{.*}}, Size=8, Name=pGI
52+
// NO-USM-NEXT: omptarget device 0 info: Copying data from device to host, TgtPtr={{.*}}, HstPtr={{.*}}, Size=4
53+
// NO-USM-NEXT: omptarget device 0 info: Copying data from device to host, TgtPtr={{.*}}, HstPtr={{.*}}, Size=12
54+
// NO-USM-NEXT: omptarget device 0 info: Copying data from device to host, TgtPtr={{.*}}, HstPtr={{.*}}, Size=4
55+
// NO-USM-NEXT: SUCCESS
56+
57+
// FORCE-USM: SUCCESS
58+
//
59+
// clang-format on

0 commit comments

Comments
 (0)