Skip to content
Permalink
Browse files

Change system() calls in llvm_codegen() to vfork() + execv().

Add a new subroutine pocl_run_command in pocl-util.c,
and make both pocl-hsa and pocl-llvm-codegen use it.
  • Loading branch information
franz committed Apr 21, 2017
1 parent 3a52674 commit aa07f9e1827a10e707ef4975b31297b8cf06b6fc
Showing with 96 additions and 59 deletions.
  1. +8 −0 CMakeLists.txt
  2. +5 −0 config2.h.in.cmake
  3. +24 −17 lib/CL/devices/common.c
  4. +2 −37 lib/CL/devices/hsa/pocl-hsa.c
  5. +54 −5 lib/CL/pocl_util.c
  6. +3 −0 lib/CL/pocl_util.h
@@ -685,6 +685,10 @@ set(EXTRA_HOST_LLC_FLAGS "" CACHE STRING "Extra parameters to llc for code gener

set(HOST_AS_FLAGS "${DEFAULT_HOST_AS_FLAGS} ${EXTRA_HOST_AS_FLAGS}")
set(HOST_LD_FLAGS "${DEFAULT_HOST_LD_FLAGS} ${EXTRA_HOST_LD_FLAGS}" )
string(STRIP "${HOST_LD_FLAGS}" HOST_LD_FLAGS_STRIPPED)
string(REGEX REPLACE "[\r\n\t ]+" "\", \"" HOST_LD_FLAGS_ARRAY "${HOST_LD_FLAGS_STRIPPED}")
# string(REPLACE "###, ###" " oo \", \" oo " HOST_LD_FLAGS_ARRAY "${HOST_LD_FLAGS_ARRAY_1}")

set(HOST_CLANG_FLAGS "${DEFAULT_HOST_CLANG_FLAGS} ${EXTRA_HOST_CLANG_FLAGS}")
set(HOST_LLC_FLAGS "${DEFAULT_HOST_LLC_FLAGS} ${EXTRA_HOST_LLC_FLAGS}")

@@ -974,6 +978,10 @@ set(PACKAGE_VERSION "${POCL_VERSION}")

configure_file("config.h.in.cmake" "config.h.new" ESCAPE_QUOTES)
rename_if_different("${CMAKE_BINARY_DIR}/config.h.new" "${CMAKE_BINARY_DIR}/config.h")

configure_file("config2.h.in.cmake" "config2.h.new")
rename_if_different("${CMAKE_BINARY_DIR}/config2.h.new" "${CMAKE_BINARY_DIR}/config2.h")

include_directories("${CMAKE_BINARY_DIR}")

# autotools compat
@@ -0,0 +1,5 @@
/* this config file is for values NOT escaped for C/C++
* required e.g. for values with doublequotes, like C string arrays */

#define HOST_LD_FLAGS_ARRAY "@HOST_LD_FLAGS_ARRAY@"

@@ -40,21 +40,23 @@
#endif

#include "config.h"
#include "pocl_image_util.h"
#include "pocl_file_util.h"
#include "pocl_util.h"
#include "pocl_cache.h"
#include "config2.h"
#include "devices.h"
#include "pocl_cache.h"
#include "pocl_debug.h"
#include "pocl_file_util.h"
#include "pocl_image_util.h"
#include "pocl_mem_management.h"
#include "pocl_runtime_config.h"
#include "pocl_debug.h"
#include "pocl_util.h"

#ifdef OCS_AVAILABLE
#include "pocl_llvm.h"
#endif

#include "_kernel_constants.h"


#define COMMAND_LENGTH 2048

/**
@@ -73,7 +75,6 @@ llvm_codegen (const char* tmpdir, cl_kernel kernel, cl_device_id device,
size_t local_x, size_t local_y, size_t local_z)
{

char command[COMMAND_LENGTH];
char bytecode[POCL_FILENAME_LENGTH];
char objfile[POCL_FILENAME_LENGTH];
/* strlen of / .so 4+1 */
@@ -120,22 +121,28 @@ llvm_codegen (const char* tmpdir, cl_kernel kernel, cl_device_id device,
assert (error == 0);

/* clang is used as the linker driver in LINK_CMD */
error = snprintf (command, COMMAND_LENGTH,

POCL_MSG_PRINT_INFO ("Linking final module\n");
char *const args1[]
#ifndef POCL_ANDROID
LINK_COMMAND " " HOST_LD_FLAGS " -o %s %s",
= { LINK_COMMAND,
HOST_LD_FLAGS_ARRAY,
"-o",
tmp_module,
objfile,
NULL };
#else
POCL_ANDROID_PREFIX"/bin/ld " HOST_LD_FLAGS " -o %s %s ",
= { POCL_ANDROID_PREFIX "/bin/ld",
HOST_LD_FLAGS_ARRAY,
"-o",
tmp_module,
objfile,
NULL };
#endif
tmp_module, objfile);
assert (error >= 0);

POCL_MSG_PRINT_INFO ("executing [%s]\n", command);
error = system (command);
error = pocl_run_command (args1);
assert (error == 0);

error = snprintf (command, COMMAND_LENGTH, "mv %s %s", tmp_module, module);
assert (error >= 0);
error = system (command);
error = pocl_rename (tmp_module, module);
assert (error == 0);

/* Save space in kernel cache */
@@ -941,41 +941,6 @@ setup_kernel_args (pocl_hsa_device_data_t *d,
#endif
}

/*
* This replaces a simple system(), because system() was causing issues
* (gpu lockups) when compiling code (via compile_parallel_bc_to_brig)
* with OpenCL 2.0 atomics (like CalcPie from AMD SDK).
* The reason of lockups is unknown (yet).
*/
static int
run_command(char* args[])
{
POCL_MSG_PRINT_INFO("Launching: %s\n", args[0]);
#ifdef HAVE_VFORK
pid_t p = vfork();
#elif defined(HAVE_FORK)
pid_t p = fork();
#else
#error Must have fork() or vfork() system calls for HSA
#endif
if (p == 0)
{
return execv(args[0], args);
}
else
{
if (p < 0)
return -1;
int status;
if (waitpid(p, &status, 0) < 0)
POCL_ABORT("pocl-hsa: waitpid() itself failed.\n");
if (WIFEXITED(status))
return WEXITSTATUS(status);
else
return -2;
}
}

static int
compile_parallel_bc_to_brig(char* brigfile, cl_kernel kernel,
cl_device_id device) {
@@ -1005,14 +970,14 @@ compile_parallel_bc_to_brig(char* brigfile, cl_kernel kernel,

char* args1[] = { LLVM_LLC, "-O2", "-march=hsail64", "-filetype=asm",
"-o", hsailfile, parallel_bc_path, NULL };
if ((error = run_command(args1)))
if ((error = pocl_run_command (args1)))
{
POCL_MSG_PRINT_INFO("pocl-hsa: llc exit status %i\n", error);
return error;
}

char* args2[] = { HSAIL_ASM, "-o", brigfile, hsailfile, NULL };
if ((error = run_command(args2)))
if ((error = pocl_run_command (args2)))
{
POCL_MSG_PRINT_INFO("pocl-hsa: HSAILasm exit status %i\n", error);
return error;
@@ -26,14 +26,18 @@
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

#ifndef _MSC_VER
# include <dirent.h>
# include <unistd.h>
# include <utime.h>
#include <dirent.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <unistd.h>
#include <utime.h>
#else
# include "vccompat.hpp"
#endif
@@ -855,3 +859,48 @@ pocl_command_to_str (cl_command_type cmd)

return "unknown";
}

/*
* This replaces a simple system(), because:
*
* 1) system() was causing issues (gpu lockups) with HSA when
* compiling code (via compile_parallel_bc_to_brig)
* with OpenCL 2.0 atomics (like CalcPie from AMD SDK).
* The reason of lockups is unknown (yet).
*
* 2) system() uses fork() which copies page table maps, and runs
* out of AS when pocl has already allocated huge buffers in memory.
* this happened in llvm_codegen()
*
* vfork() does not copy pagetables.
*/
int
pocl_run_command (char *const *args)
{
POCL_MSG_PRINT_INFO ("Launching: %s\n", args[0]);
#ifdef HAVE_VFORK
pid_t p = vfork ();
#elif defined(HAVE_FORK)
pid_t p = fork ();
#else
#error Must have fork() or vfork() system calls for HSA
#endif
if (p == 0)
{
return execv (args[0], args);
}
else
{
if (p < 0)
return EXIT_FAILURE;
int status;
if (waitpid (p, &status, 0) < 0)
POCL_ABORT ("pocl: waitpid() failed.\n");
if (WIFEXITED (status))
return WEXITSTATUS (status);
else if (WIFSIGNALED (status))
return WTERMSIG (status);
else
return EXIT_FAILURE;
}
}
@@ -151,6 +151,9 @@ pocl_status_to_str (int status);
const char *
pocl_command_to_str (cl_command_type cmd);

int
pocl_run_command(char * const *args);

#ifdef __cplusplus
}
#endif

0 comments on commit aa07f9e

Please sign in to comment.
You can’t perform that action at this time.