https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116269

            Bug ID: 116269
           Summary: [OpenACC] acc_on_device – compile-time optimization
                    fails
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: missed-optimization, openacc
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
  Target Milestone: ---

For C, both __builtin_acc_on_device and acc_on_device is defined,
permitting compile-time optimization.

In principle, something like the following would work - except that the API
between C and Fortran differs: C takes a by-value int and Fortran takes a
by-reference integer. Thus, the Fortran call looks like:

    static integer(kind=4) C.4288 = 2;
    _1 = acc_on_device_h (&C.4288);

and not like:

    _1 = acc_on_device_h (2);

and that's unsurprisingly not handled.


--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -2214,0 +2214,7 @@ gfc_get_extern_function_decl (gfc_symbol * sym, 
+  if (flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
+      && !strcmp (sym->name, "acc_on_device"))
+    {
+      sym->backend_decl = builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
+      return sym->backend_decl;
+    }
+

--------------
! { dg-do link }

! Check whether 'acc_on_device()' is properly compile-time optimized. */

! { dg-additional-options "-fdump-tree-gimple -fdump-tree-optimized" }
! { dg-additional-options -foffload-options=-fdump-tree-optimized { target {
offload_device_nvptx || offload_target_amdgcn } } }

! { dg-final { scan-tree-dump-times "acc_on_device" 1 "gimple" } }

! { dg-final { scan-tree-dump-not "acc_on_device" "optimized" } }

! { dg-final { only_for_offload_target amdgcn-amdhsa scan-offload-tree-dump-not
"acc_on_device" "optimized" { target offload_target_amdgcn } } }
! { dg-final { only_for_offload_target nvptx-none scan-offload-tree-dump-not
"acc_on_device" "optimized" { target offload_target_nvptx } } }

module m
   integer :: xxxx
   !$acc declare device_resident(xxxx)
contains
  subroutine set_var
    !$acc routine
    use openacc
    implicit none (type, external)
    if (acc_on_device(acc_device_host)) then
      xxxx = 1234
    else
      xxxx = 4242
    end if
  end
end module m


program main
  use m
  call set_var
  !$acc serial  ! { dg-warning "using ‘vector_length \\(32\\)’, ignoring 1" }
     call set_var
  !$acc end serial
end

Reply via email to