This is an archive of the discontinued LLVM Phabricator instance.

[OpenMP] Try to Infer target triples using the offloading architecture
ClosedPublic

Authored by jhuber6 on May 5 2022, 3:15 PM.

Details

Summary

Currently we require the -fopenmp-targets= option to specify the
triple to use for the offloading toolchains, and the -Xopenmp-target=
option to specify architectures to a specific toolchain. The changes
made in D124721 allowed us to use --offload-arch= to specify multiple
target architectures. However, this can become combersome with many
different architectures. This patch introduces functinality that
attempts to deduce the target triple and architectures from the
offloading action. Currently we will deduce known GPU architectures when
only -fopenmp is specified.

This required a bit of a hack to cache the deduced architectures,
without this we would've just thrown an error when we tried to look up
the architecture again when generating the job. Normally we require the
user to manually specify the toolchain arguments, but here they would
confict unless we overrode them.

Depends on: D124721

Diff Detail

Event Timeline

jhuber6 created this revision.May 5 2022, 3:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2022, 3:15 PM
Herald added a subscriber: guansong. · View Herald Transcript
jhuber6 requested review of this revision.May 5 2022, 3:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2022, 3:15 PM
tra added a subscriber: tra.May 5 2022, 4:10 PM

Just a drive-by style review. I didn't look at the functionality of the changes much.

clang/lib/Driver/Driver.cpp
788–789

So, specifying just -fopenmp will result in IsOpenMP=false? This seems odd.
Perhaps the variable should be called IsOpenMPOffload ?

821–822

This seems a bit too restrictive as it would fail if we've failed to get either of those triples. We may be able to proceed with only one of them in many cases.
I'd remove this check and would make the loop below more forgiving.

845
if (NvidiaTriple && IsNVIDIAGpuArch(...))
...
else if (AMDTriple && IsAMDGpuArch(...))
...
else {
   Diag(); 
   return;
}
jhuber6 updated this revision to Diff 427496.May 5 2022, 4:49 PM

Address comments.

jhuber6 marked 3 inline comments as done.May 5 2022, 4:49 PM
jhuber6 added inline comments.
clang/lib/Driver/Driver.cpp
788–789

Will do.

Looks good to me.

Will it work with -fno-openmp? Sometimes -fno-openmp is used by the end-user to override system provided -fopenmp flag for some translation units.

Please have a look at following examples:

// 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'
jhuber6 marked an inline comment as done.May 6 2022, 4:40 AM

Looks good to me.

Will it work with -fno-openmp? Sometimes -fno-openmp is used by the end-user to override system provided -fopenmp flag for some translation units.

Please have a look at following examples:

// 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'

It should, I could add a simple test for that.

jhuber6 updated this revision to Diff 427599.May 6 2022, 4:42 AM

Add test for disabling openmp with -fno-openmp

saiislam accepted this revision.May 6 2022, 9:46 AM

Thanks!
LGTM.

This revision is now accepted and ready to land.May 6 2022, 9:46 AM
yaxunl added inline comments.May 6 2022, 10:41 AM
clang/lib/Driver/Driver.cpp
789

probably should be

C.getInputArgs().hasArg(options::OPT_offload_arch_EQ) && !IsHIP && !IsCUDA

otherwise a HIP program may get both HIP offloading and OpenMP offloading at the same time. Currently it is not supported.

could you please add a test to https://github.com/llvm/llvm-project/blob/main/clang/test/Driver/hip-offload-arch.hip

to make sure when -fopenmp is specified, no openmp offloading is performed? Thanks.

jhuber6 added inline comments.May 6 2022, 11:01 AM
clang/lib/Driver/Driver.cpp
789

I do that below, line 815. The logic states that we only take --offloading-arch to imply OpenMP offloading if it's not currently CUDA or HIP. I'm pretty sure there's an existing test that covers this as it used to fail one of those HIP-interop tests until I added !IsHIP && !IsCUDA below.