Skip to content

Commit

Permalink
[Clang][OpenMP] Fix behavior when -fopenmp is not present on command …
Browse files Browse the repository at this point in the history
…line

When -fopenmp is not present, or gets hidden with -fno-openmp, compiler
shouldn't give up parsing the command line.

This will allow HPC users to pass -fno-openmp as override flag for
subset of their code which doesn't contain OpenMP code, without
writing new command lines by removing -fopenmp-targets, -Xopenmp-target,
-march, etc.

Fixes: SWDEV-294358 and SWDEV-323292

Change-Id: I82288adef2327fa590fd16d822acbf8cc9a1b541
(cherry picked from commit b266d5c)
  • Loading branch information
saiislam authored and Subodh Gupta committed Mar 16, 2022
1 parent 75bed70 commit b9a335a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ def err_drv_omp_host_ir_file_not_found : Error<
"target regions but cannot be found">;
def err_drv_omp_host_target_not_supported : Error<
"target '%0' is not a supported OpenMP host target">;
def err_drv_expecting_fopenmp_with_fopenmp_targets : Error<
def warn_drv_expecting_fopenmp_with_fopenmp_targets : Warning<
"'-fopenmp-targets' must be used in conjunction with a '-fopenmp' option "
"compatible with offloading; e.g., '-fopenmp=libomp' or '-fopenmp=libiomp5'">;
"compatible with offloading; e.g., '-fopenmp=libomp' or '-fopenmp=libiomp5'">, InGroup<OpenMPTarget>;
def err_drv_omp_offload_target_missingbcruntime : Error<
"no library '%0' found in the default clang lib directory or in LIBRARY_PATH"
"; use '--libomptarget-%1-bc-path' to specify %1 bitcode library">;
Expand Down
44 changes: 28 additions & 16 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,34 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
// may now contain targetid value. This value
// may include features that would result in different and potentially
// multiple offload images.

bool HasValidOpenMPRuntime =
C.getInputArgs().hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
options::OPT_fno_openmp, false);
if (HasValidOpenMPRuntime) {
OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs());
HasValidOpenMPRuntime =
OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5;
}

bool HasOpenMPTargets =
C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ);
if (!HasValidOpenMPRuntime && HasOpenMPTargets) {
// We expect that an offload target is always used in conjunction with
// option -fopenmp specifying a valid runtime with offloading support,
// i.e. libomp or libiomp.
Diag(clang::diag::warn_drv_expecting_fopenmp_with_fopenmp_targets);
return;
} else if (!HasValidOpenMPRuntime && !HasOpenMPTargets) {
return;
}

bool HasOffloadArch = C.getInputArgs().hasFlag(
options::OPT_offload_arch_EQ, options::OPT_no_offload_arch_EQ, false);
if (HasValidOpenMPRuntime && !HasOpenMPTargets && !HasOffloadArch) {
return;
}

std::set<std::string> OffloadArchs;

if (Arg *OpenMPTargets =
Expand Down Expand Up @@ -950,22 +978,6 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
}
}

// We expect that an offload target is always used in conjunction with
// option -fopenmp specifying a valid runtime with offloading support,
// i.e. libomp or libiomp.
bool HasValidOpenMPRuntime = C.getInputArgs().hasFlag(
options::OPT_fopenmp, options::OPT_fopenmp_EQ,
options::OPT_fno_openmp, false);
if (HasValidOpenMPRuntime) {
OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs());
HasValidOpenMPRuntime =
OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5;
}
if (!HasValidOpenMPRuntime) {
Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
return;
}

llvm::StringMap<const char *> FoundNormalizedTriples;
for (auto &Target : OffloadArchs) {
size_t find_loc = Target.find('^');
Expand Down
40 changes: 40 additions & 0 deletions clang/test/Driver/openmp-offload-fnoopenmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// REQUIRES: clang-driver
// REQUIRES: x86-registered-target
// REQUIRES: amdgpu-registered-target

// RUN: %clang -### -target x86_64-linux-gnu \
// RUN: --offload-arch=gfx906 \
// RUN: %s 2>&1 | FileCheck -check-prefix=OFFLOAD %s
// OFFLOAD: warning: argument unused during compilation: '--offload-arch=gfx906'

// RUN: %clang -### -target x86_64-linux-gnu -fopenmp\
// RUN: --offload-arch=gfx906 \
// RUN: -fno-openmp \
// RUN: %s 2>&1 | FileCheck -check-prefix=OFFLOAD1 %s
// OFFLOAD1: warning: argument unused during compilation: '--offload-arch=gfx906'

// RUN: %clang -### -target x86_64-linux-gnu -fopenmp\
// RUN: -fopenmp-targets=amdgcn-amd-amdhsa,amdgcn-amd-amdhsa \
// RUN: -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906 \
// RUN: -fno-openmp \
// RUN: %s 2>&1 | FileCheck -check-prefix=LEGACY %s
// LEGACY: warning: '-fopenmp-targets' must be used in conjunction with a '-fopenmp' option compatible with offloading; e.g., '-fopenmp=libomp' or '-fopenmp=libiomp5'
// LEGACY-NEXT: warning: argument unused during compilation: '-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906'

// RUN: %clang -### -target x86_64-linux-gnu -fopenmp\
// RUN: --offload-arch=gfx906 \
// RUN: --offload-arch=gfx908 \
// RUN: -fno-openmp \
// RUN: %s 2>&1 | FileCheck -check-prefix=MOFFLOAD %s
// MOFFLOAD: warning: argument unused during compilation: '--offload-arch=gfx906'
// MOFFLOAD-NEXT: warning: argument unused during compilation: '--offload-arch=gfx908'

// RUN: %clang -### -target x86_64-linux-gnu -fopenmp\
// RUN: -fopenmp-targets=amdgcn-amd-amdhsa,amdgcn-amd-amdhsa \
// RUN: -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906 \
// RUN: -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908 \
// RUN: -fno-openmp \
// RUN: %s 2>&1 | FileCheck -check-prefix=MLEGACY %s
// MLEGACY: warning: '-fopenmp-targets' must be used in conjunction with a '-fopenmp' option compatible with offloading; e.g., '-fopenmp=libomp' or '-fopenmp=libiomp5'
// MLEGACY: warning: argument unused during compilation: '-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx906'
// MLEGACY: warning: argument unused during compilation: '-Xopenmp-target=amdgcn-amd-amdhsa -march=gfx908'

0 comments on commit b9a335a

Please sign in to comment.