#!/bin/sh
# pocl-workgroup - Parallelize a kernel from an OpenCL bytecode file.
# 
# Copyright (c) 2011 Universidad Rey Juan Carlos
#               2011-2013 Pekka Jääskeläinen / Tampere University of Technology
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

#echo 0=$0 @=$@
#set -x
#set -e

target=i686-redhat-linux-gnu

while getopts k:x:y:z:t:o: o
do
    case "$o" in
	k)   kernel="${OPTARG}";;
	x)   size_x="${OPTARG}";;
	y)   size_y="${OPTARG}";;
	z)   size_z="${OPTARG}";;
	t)   target="${OPTARG}";;
	o)   output_file="${OPTARG}";;
	[?]) echo >&2 "Usage: $0 -k <kernel> [-x <size>] [-y <size>] [-z <size>] [-t <target_arch>] -o <output_file> <input_file>" && exit 1;;
    esac
done
shift $((${OPTIND}-1))

if [ "x$kernel" = x ]
then
    echo >&2 "Usage: $0 -k <kernel> [-x <size>] [-y <size>] [-z <size>] [-t <target_arch>] -o <output_file> <input_file>" && exit 1
fi

if [ "x$size_x" = x ]
then
    size_x=1
fi

if [ "x$size_y" = x ]
then
    size_y=1
fi

if [ "x$size_z" = x ]
then
    size_z=1
fi

if [ "x$output_file" = x ]
then
    echo >&2 "Usage: $0 -k <kernel> [-x <size>] [-y <size>] [-z <size>] -o <output_file> <input_file>" && exit 1
fi

linked_bc="`dirname $1`/`basename $1 .bc`_linked.bc"
linked_out="${linked_bc}.out"

target_dir=${target}
case $target in
  arm*)        target_dir="arm";;
  cellspu-*)   target_dir="cellspu";;
  powerpc-*)   target_dir="powerpc";;
  powerpc64-*) target_dir="powerpc64";;
  tce-*)       target_dir="tce";;
  i?86*)       target_dir="x86_64";;
  x86_64-*)    target_dir="x86_64";;
esac

full_target_dir="/usr/lib/pocl/${target_dir}"

/usr/bin/llvm-link -o ${linked_bc}  $1 "$full_target_dir"/kernel-$target.bc 

header="`dirname $1`/`basename $1 .bc`_header.h"

pocl_lib=/usr/lib/pocl/llvmopencl.so

OPT_SWITCH="-O3"

if test "x$POCL_KERNEL_COMPILER_OPT_SWITCH" != "x";
then
OPT_SWITCH=$POCL_KERNEL_COMPILER_OPT_SWITCH
fi

EXTRA_OPTS=""
if test "x$POCL_VECTORIZE_WORK_GROUPS" = "x1";
then
OPT_SWITCH="-O3"
EXTRA_OPTS="-wi-vectorize"
fi

if test "x$POCL_VECTORIZE_VECTOR_WIDTH" != "x";
then
EXTRA_OPTS="$EXTRA_OPTS -wi-vectorize-vector-width="${POCL_VECTORIZE_VECTOR_WIDTH}
fi

if test "x$POCL_VECTORIZE_NO_FP" = "x1";
then
EXTRA_OPTS="$EXTRA_OPTS -wi-vectorize-no-fp"
fi

if test "x$POCL_VECTORIZE_MEM_ONLY" = "x1";
then
EXTRA_OPTS="$EXTRA_OPTS -wi-vectorize-mem-ops-only"
fi

#EXTRA_OPTS+="-vectorize-loops"

WG_VECTORIZER=""
if test "x$POCL_WORK_GROUP_METHOD" = "xloopvec";
then
export POCL_WORK_GROUP_METHOD=loops
export POCL_WILOOPS_MAX_UNROLL_COUNT=1
WG_VECTORIZER="-vectorize-loops -vectorizer-min-trip-count 1"
fi

#set -x

# -disable-simplify-libcalls was added because of TCE (it doesn't have
# a runtime linker which could link the libs later on), but it might
# also be harmful for wg-vectorization where we want to try to vectorize
# the code it wants to convert e.g. to a memset or a memcpy

/usr/bin/opt -load=${pocl_lib} -domtree -workitem-handler-chooser -break-constgeps -automatic-locals -flatten -always-inline \
    -globaldce -simplifycfg -loop-simplify -phistoallocas -isolate-regions -uniformity -implicit-loop-barriers -loop-barriers \
    -barriertails -barriers -isolate-regions -add-wi-metadata -wi-aa -workitemrepl -workitemloops  \
    -allocastoentry -workgroup -kernel=${kernel} -local-size=${size_x} ${size_y} ${size_z} -disable-simplify-libcalls \
    -target-address-spaces \
    ${OPT_SWITCH} ${WG_VECTORIZER} ${EXTRA_OPTS} -instcombine -o ${output_file} ${linked_bc}

#set +x
