#!/bin/sh
# pocl-standalone - Run parallelization passes directly on an OpenCL source
#                   file and generate a parallel bytecode and a kernel description
#                   header file.
# 
# Copyright (c) 2011-2012 Carlos Sánchez de La Lama / URJC and
#               2011-2013 Pekka Jääskeläinen / TUT
# 
# 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 h:t:o: o
do
    case "$o" in
	h)   header="${OPTARG}";;
	t)   target="${OPTARG}";;
	o)   output_file="${OPTARG}";;
	[?]) echo >&2 "Usage: $0 -o <output_file> <input_file>" && exit 1;;
    esac
done
shift $((${OPTIND}-1))

if [ "x$header" = x ]
then
    echo >&2 "Usage: $0 [-t target] -h <header> -o <output_file> <input_file>" && exit 1
fi

if [ "x$output_file" = x ]
then
    echo >&2 "Usage: $0 [-t target] -h <header> -o <output_file> <input_file>" && exit 1
fi

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"
        if test "$LLVM_VERSION" != "3.1"
        then
            FP_CONTRACT=-fp-contract=off
        fi
        ;;
  amd64-*|x86*|i?86-*)    target_dir="x86_64";;
esac

case $target in
  i686-redhat-linux-gnu) CLANG_FLAGS="" ;;
  i686-redhat-linux-gnu)   CLANG_FLAGS="" ;;
esac
CLANG_FLAGS="$CLANG_FLAGS -fasm -fsigned-char -Xclang -ffake-address-space-map -target $target "

# With fp-contract we get calls to fma with processors which do not
# have fma instructions. These ruin the performance. Better to have
# the mul+add separated in the IR.
CLANG_FLAGS="$CLANG_FLAGS -ffp-contract=off "

tempdir="`dirname ${output_file}`/.pocl$$"
mkdir ${tempdir}

kernel_bc="${tempdir}/kernel.bc"

/usr/bin/clang ${CLANG_FLAGS} $EXTRA_CLANG_FLAGS -c -emit-llvm  -include /usr/include/pocl/${target_dir}/types.h -include /usr/include/pocl/_kernel.h -o ${kernel_bc} -x cl $1
rm -f ${header}
/usr/bin/opt -load=/usr/lib/pocl/llvmopencl.so -generate-header -disable-output -header=${header} ${kernel_bc}

linked_bc="${tempdir}/linked.bc"
linked_out="${linked_bc}.out"
pocl_lib="/usr/lib/pocl/llvmopencl.so"
full_target_dir="/usr/lib/pocl/${target_dir}"

llvm-link -o ${linked_bc} ${kernel_bc} ${full_target_dir}/kernel*.bc

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" = "x0";
then
OPT_SWITCH="-O3"
EXTRA_OPTS="-wi-vectorize -instcombine"
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

# -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 -generate-header -flatten -always-inline \
    -globaldce -simplifycfg -loop-simplify -phistoallocas -isolate-regions -loop-barriers \
    -barriertails -barriers -isolate-regions -add-wi-metadata -wi-aa -workitemrepl -workitemloops \
    -workgroup -kernel=${kernel} -local-size=1 1 1 -disable-simplify-libcalls ${OPT_SWITCH} \
    ${EXTRA_OPTS} -instcombine -header=/dev/null ${FP_CONTRACT} -o ${output_file} ${linked_bc}


rm -fr ${tempdir}
