Hi! As the testcase shows, we weren't handling kind(host) and kind(nohost) properly in the ACCEL_COMPILERs, the code written in there is valid for the host compiler only, where if we are maybe offloaded, we defer resolution after IPA, otherwise return 0 for kind(nohost) and accept it for kind(host). Note, omp_maybe_offloaded is false after IPA. If ACCEL_COMPILER is defined, it is the other way around, but also we know we are after IPA.
Bootstrapped/regtested on x86_64-linux and i686-linux and tested with offloading to nvptx-none where the new testcase fails without the patch, committed to trunk. 2021-11-24 Jakub Jelinek <ja...@redhat.com> PR middle-end/103384 gcc/ * omp-general.c (omp_context_selector_matches): For ACCEL_COMPILER, return 0 for kind(host) and continue for kind(nohost). libgomp/ * testsuite/libgomp.c/declare-variant-2.c: New test. --- gcc/omp-general.c.jj 2021-10-15 11:59:16.135683948 +0200 +++ gcc/omp-general.c 2021-11-23 15:45:32.761468592 +0100 @@ -1487,16 +1487,22 @@ omp_context_selector_matches (tree ctx) continue; if (!strcmp (prop, "host")) { +#ifdef ACCEL_COMPILER + return 0; +#else if (omp_maybe_offloaded ()) ret = -1; continue; +#endif } if (!strcmp (prop, "nohost")) { +#ifndef ACCEL_COMPILER if (omp_maybe_offloaded ()) ret = -1; else return 0; +#endif continue; } int r = 0; --- libgomp/testsuite/libgomp.c/declare-variant-2.c.jj 2021-11-24 10:14:11.689619756 +0100 +++ libgomp/testsuite/libgomp.c/declare-variant-2.c 2021-11-24 10:20:37.956110726 +0100 @@ -0,0 +1,45 @@ +/* { dg-do run } */ + +#include <omp.h> +#include <stdlib.h> + +void +foo_host (void) +{ + if (!omp_is_initial_device ()) + abort (); +} + +#pragma omp declare variant (foo_host) match (device={kind(host)}) +void +foo (void) +{ + if (omp_is_initial_device ()) + abort (); +} + +void +bar_nohost (void) +{ + if (omp_is_initial_device ()) + abort (); +} + +#pragma omp declare variant (bar_nohost) match (device={kind(nohost)}) +void +bar (void) +{ + if (!omp_is_initial_device ()) + abort (); +} + +int +main () +{ + #pragma omp target + { + foo (); + bar (); + } + return 0; +} Jakub