#/bin/bash

MYGCC=$HOME/projects/gcc-trunk-offload/bin/gcc

cat <<EOF > off.c
#include <omp.h>
int main() {
#pragma omp target
        __builtin_printf("%d\n", omp_is_initial_device());
        return 0;

}
EOF

for MARCH in "" fiji gfx900 gfx906 gfx908 gfx90a gfx1030 gfx1100; do
  [[ -n "$MARCH" ]] && MARCH="-march=$MARCH"
  for XNACK in "" "-mxnack=off" "-mxnack=any" "-mxnack=on"; do
    for ECC in "" "-msram-ecc=off" "-msram-ecc=any" "-msram-ecc=on"; do
      OPTIONS=()
      [[ -n "$MARCH" ]] && OPTIONS+=("-foffload-options=$MARCH")
      [[ -n "$XNACK" ]] && OPTIONS+=("-foffload-options=$XNACK")
      [[ -n "$ECC" ]] && OPTIONS+=("-foffload-options=$ECC")
      echo
      echo "------------------------------------------------------------------------------------"
      echo "gcc -fopenmp -foffload=amdgcn-amdhsa" "${OPTIONS[@]}"
      echo "------------------------------------------------------------------------------------"
      "$MYGCC" -fopenmp -foffload=amdgcn-amdhsa "${OPTIONS[@]}" \
	       off.c
      RES=$?
      "$MYGCC" -fopenmp -foffload=amdgcn-amdhsa "${OPTIONS[@]}" \
	       off.c -v -foffload-options=-Wa,-v -save-temps -v 2>&1 \
        | grep  'amdgcn-amdhsa/as' \
	| sed -e 's@.*amdgcn-amdhsa/as@amdgcn-amdhsa/as@' -e 's/ -o .*//'
      if [[ $RES = 0 ]]; then
          echo "$(readelf -h a.xamdgcn-amdhsa.mkoffload.hsaco | grep Flags)  - a.xamdgcn-amdhsa.mkoffload.hsaco"
	 "$MYGCC" -fopenmp -foffload=amdgcn-amdhsa "${OPTIONS[@]}" \
		  off.c -g -save-temps -gdwarf-4
	 # read elf
	 echo "$(readelf -h a.xamdgcn-amdhsa.mkoffload.dbg0.o 2> /dev/null | grep Flags)  - a.xamdgcn-amdhsa.mkoffload.dbg0.o"
      fi
      rm -f a-off.* a.xamdgcn-amdhsa.*
    done      
  done      
done      
