This is an archive of the discontinued LLVM Phabricator instance.

[Clang][OpenMP] Allow host call to nohost function with host variant
ClosedPublic

Authored by doru1004 on Dec 15 2022, 12:41 PM.

Details

Summary

In OpenMP 5.2 when a nohost function is called from the host, the host variant of that function is called if one is declared.

Diff Detail

Event Timeline

doru1004 created this revision.Dec 15 2022, 12:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 15 2022, 12:41 PM
doru1004 requested review of this revision.Dec 15 2022, 12:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 15 2022, 12:41 PM
doru1004 retitled this revision from [OpenMP] Allow host call to nohost function with host variant to [Clang][OpenMP] Allow host call to nohost function with host variant.Dec 15 2022, 1:38 PM
doru1004 updated this revision to Diff 483325.Dec 15 2022, 1:44 PM
ABataev added inline comments.Dec 15 2022, 1:48 PM
clang/lib/Sema/SemaOpenMP.cpp
2697–2706

It is recommended to implement such loops as functions/lauto &ambdas:

auto HasHostAttr = []() {
  for (const OMPDeclareVariantAttr *A :
    Callee->specific_attrs<OMPDeclareVariantAttr>()) {
    auto *DeclRefVariant = cast<DeclRefExpr>(A->getVariantFuncRef());
    auto *VariantFD = cast<FunctionDecl>(DeclRefVariant->getDecl());
    Optional<OMPDeclareTargetDeclAttr::DevTypeTy> DevTy =
        OMPDeclareTargetDeclAttr::getDeviceType(
            VariantFD->getMostRecentDecl());
    if (!DevTy || *DevTy == OMPDeclareTargetDeclAttr::DT_Host)
      return true;
  }
  return false;
};
if (HasHostAttr())
  return;
2703

Why the most recent decl?

2704

Isuppose it must be DevTy && *DevTy == OMPDeclareTargetDeclAttr::DT_Host, no?

doru1004 added inline comments.Dec 15 2022, 2:06 PM
clang/lib/Sema/SemaOpenMP.cpp
2703

I believe that would retrieve the full information about the function since that's where the function is defined?

2704

Correct me if I'm wrong but if there's no device type associated with the function then it is a host function? For the second part of the condition, if there exists a DevTy associated with the function then it has to be DT_Host.

doru1004 added inline comments.Dec 15 2022, 2:12 PM
clang/lib/Sema/SemaOpenMP.cpp
2697–2706

I will rewrite this.

ABataev added inline comments.Dec 15 2022, 2:14 PM
clang/lib/Sema/SemaOpenMP.cpp
2704

Ah, I missed it. Can we have just declare variant without device type? Can you add a test for this case?

doru1004 added inline comments.Dec 15 2022, 2:28 PM
clang/lib/Sema/SemaOpenMP.cpp
2704

You mean when the variant function definition does not have a device type declared? That's the case that's currently tested:

#pragma omp declare variant(host_function) match(device={kind(host)})
void fun() {}
void host_function() {}

But I think you're right I should add another test which tests the other part of the condition when the host_function has a device type.

doru1004 updated this revision to Diff 483627.Dec 16 2022, 12:04 PM
ABataev added inline comments.Dec 16 2022, 12:08 PM
clang/lib/Sema/SemaOpenMP.cpp
2671

Do you have a test for !DevTy case? Maybe I'm missing it.

doru1004 marked an inline comment as done.Dec 16 2022, 12:24 PM
doru1004 added inline comments.
clang/lib/Sema/SemaOpenMP.cpp
2671

Yes it was the first test I added. The second one tests the second part of the condition.

doru1004 updated this revision to Diff 483639.Dec 16 2022, 12:25 PM
doru1004 marked an inline comment as done.
ABataev added inline comments.Dec 16 2022, 12:32 PM
clang/lib/Sema/SemaOpenMP.cpp
2671

Hmm, but in this test we have *DevTy == OMPDeclareTargetDeclAttr::DT_Host, no?

doru1004 updated this revision to Diff 483643.Dec 16 2022, 12:44 PM
doru1004 added inline comments.Dec 16 2022, 12:46 PM
clang/lib/Sema/SemaOpenMP.cpp
2671

I added a positive check of that condition too. No error messages to check for in that case.

ABataev added inline comments.Dec 16 2022, 12:53 PM
clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
16 ↗(On Diff #483643)

You mean this test case? But it still has kind(host).

doru1004 marked 2 inline comments as done.Dec 16 2022, 12:53 PM
doru1004 added inline comments.Dec 16 2022, 12:55 PM
clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
16 ↗(On Diff #483643)

The condition checks the attribute of the host_function which in this case is host. In the test above the condition is false because the not_a_host_function has a nohost attribute.

doru1004 added inline comments.Dec 16 2022, 1:36 PM
clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
16 ↗(On Diff #483643)

It should always have host there because we are trying to fix the case where we have a nohost function that needs a host variant.

ABataev accepted this revision.Dec 16 2022, 1:48 PM

LG

clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
16 ↗(On Diff #483643)

Ah, I missed that the check was for declare target attribute.

This revision is now accepted and ready to land.Dec 16 2022, 1:48 PM
mgorny reopened this revision.Dec 25 2022, 5:45 AM
mgorny added a subscriber: mgorny.

The added test fails on 32-bit platforms:

FAIL: Clang :: OpenMP/declare_target_nohost_variant_messages.cpp (10230 of 16135)
******************** TEST 'Clang :: OpenMP/declare_target_nohost_variant_messages.cpp' FAILED ********************
Script:
--
: 'RUN: at line 3';   /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
--
Exit Code: 1

Command Output (stderr):
--
+ : 'RUN: at line 3'
+ /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
error: 'error' diagnostics seen but not expected: 
  (frontend): OpenMP target architecture 'amdgcn-amd-amdhsa' pointer size is incompatible with host 'i686-pc-linux-gnu'

--

********************

Please fix, or ideally revert, fix and then commit properly linking to the diff.

This revision is now accepted and ready to land.Dec 25 2022, 5:45 AM

The added test fails on 32-bit platforms:

FAIL: Clang :: OpenMP/declare_target_nohost_variant_messages.cpp (10230 of 16135)
******************** TEST 'Clang :: OpenMP/declare_target_nohost_variant_messages.cpp' FAILED ********************
Script:
--
: 'RUN: at line 3';   /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
--
Exit Code: 1

Command Output (stderr):
--
+ : 'RUN: at line 3'
+ /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
error: 'error' diagnostics seen but not expected: 
  (frontend): OpenMP target architecture 'amdgcn-amd-amdhsa' pointer size is incompatible with host 'i686-pc-linux-gnu'

--

********************

Please fix, or ideally revert, fix and then commit properly linking to the diff.

Should be a simple fix, someone needs to provide --triple= in the test line. A lot of the old tests used 64-bit PowerPC but it's not really important.

The added test fails on 32-bit platforms:

FAIL: Clang :: OpenMP/declare_target_nohost_variant_messages.cpp (10230 of 16135)
******************** TEST 'Clang :: OpenMP/declare_target_nohost_variant_messages.cpp' FAILED ********************
Script:
--
: 'RUN: at line 3';   /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
--
Exit Code: 1

Command Output (stderr):
--
+ : 'RUN: at line 3'
+ /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
error: 'error' diagnostics seen but not expected: 
  (frontend): OpenMP target architecture 'amdgcn-amd-amdhsa' pointer size is incompatible with host 'i686-pc-linux-gnu'

--

********************

Please fix, or ideally revert, fix and then commit properly linking to the diff.

Should be fixed in rGf74e3d2f81d2.

mgorny closed this revision.Dec 26 2022, 1:19 AM

Thank you. I'm going to try if the same solution helps with D139723 too.

The added test fails on 32-bit platforms:

FAIL: Clang :: OpenMP/declare_target_nohost_variant_messages.cpp (10230 of 16135)
******************** TEST 'Clang :: OpenMP/declare_target_nohost_variant_messages.cpp' FAILED ********************
Script:
--
: 'RUN: at line 3';   /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
--
Exit Code: 1

Command Output (stderr):
--
+ : 'RUN: at line 3'
+ /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/clang -cc1 -internal-isystem /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/x/y/clang-abi_x86_32.x86/bin/../../../../lib/clang/16/include -nostdsysteminc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-version=52 -DVERBOSE_MODE=1 -verify=omp52 -fnoopenmp-use-tls -ferror-limit 100 -fopenmp-targets=amdgcn-amd-amdhsa -o - /var/tmp/portage/sys-devel/clang-16.0.0_pre20221225/work/clang/test/OpenMP/declare_target_nohost_variant_messages.cpp
error: 'error' diagnostics seen but not expected: 
  (frontend): OpenMP target architecture 'amdgcn-amd-amdhsa' pointer size is incompatible with host 'i686-pc-linux-gnu'

--

********************

Please fix, or ideally revert, fix and then commit properly linking to the diff.

Should be fixed in rGf74e3d2f81d2.

Thank you for pushing a fix!