Skip to content

Commit

Permalink
Proof-of-concept cl_ext_device_side_abort impl. for CPU devices
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaaskel committed May 14, 2022
1 parent 72b4fe4 commit d5e88c7
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ endif()
# These are mandatory for OpenCL 1.1 (except for 3D writes but those are CPU-independent)
set(HOST_DEVICE_EXTENSIONS "cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics \
cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics \
cl_khr_local_int32_extended_atomics cl_khr_3d_image_writes")
cl_khr_local_int32_extended_atomics cl_khr_3d_image_writes cl_ext_device_side_abort")

set(HOST_DEVICE_FEATURES_30 "__opencl_c_3d_image_writes __opencl_c_images \
__opencl_c_atomic_order_acq_rel __opencl_c_atomic_order_seq_cst \
Expand Down
9 changes: 9 additions & 0 deletions include/_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,12 @@
#include "pocl_image_types.h"

#pragma OPENCL EXTENSION all : disable

/****************************************************************************/
/* PoCL-implemented extensions */

#ifdef cl_ext_device_side_abort

void abort_platform ();

#endif
3 changes: 3 additions & 0 deletions lib/CL/devices/basic/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ pocl_basic_init (unsigned j, cl_device_id device, const char* parameters)
device->version_of_latest_passed_cts = HOST_DEVICE_LATEST_CTS_PASS;
device->extensions = HOST_DEVICE_EXTENSIONS;

static const char *dev_aux_funcs[] = { "__pocl_flush_printf_buffer", NULL };
device->device_aux_functions = dev_aux_funcs;

#if (HOST_DEVICE_CL_VERSION_MAJOR >= 3)
device->features = HOST_DEVICE_FEATURES_30;

Expand Down
2 changes: 2 additions & 0 deletions lib/CL/devices/pthread/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ pocl_pthread_init (unsigned j, cl_device_id device, const char* parameters)

device->version_of_latest_passed_cts = HOST_DEVICE_LATEST_CTS_PASS;
device->extensions = HOST_DEVICE_EXTENSIONS;
static const char *dev_aux_funcs[] = { "__pocl_flush_printf_buffer", NULL };
device->device_aux_functions = dev_aux_funcs;

#if (HOST_DEVICE_CL_VERSION_MAJOR >= 3)
device->features = HOST_DEVICE_FEATURES_30;
Expand Down
31 changes: 31 additions & 0 deletions lib/kernel/abort_platform.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* cl_ext_device_side_abort for CPU/host devices.
Copyright (c) 2022 Pekka Jääskeläinen / Parmance
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.
*/

#include <stdlib.h>

void
abort_platform ()
{
__pocl_flush_printf ();
abort ();
}
4 changes: 3 additions & 1 deletion lib/kernel/host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#=============================================================================
# CMake build system files
#
# Copyright (c) 2014 pocl developers
# Copyright (c) 2014-2022 pocl developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -294,6 +294,8 @@ else()
endif()
endif()

list(APPEND KERNEL_SOURCES abort_platform.c)

set(HOST_DEVICE_CL_VERSION_3DIGIT "${HOST_DEVICE_CL_VERSION_MAJOR}${HOST_DEVICE_CL_VERSION_MINOR}0")
set(HOST_DEVICE_CL_VERSION_STD "${HOST_DEVICE_CL_VERSION_MAJOR}.${HOST_DEVICE_CL_VERSION_MINOR}")

Expand Down
13 changes: 13 additions & 0 deletions lib/kernel/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "printf_base.h"

#include <stdarg.h>
#include <unistd.h>

#define OCL_C_AS

Expand Down Expand Up @@ -711,6 +712,17 @@ extern char *_printf_buffer;
extern uint32_t *_printf_buffer_position;
extern uint32_t _printf_buffer_capacity;

void
__pocl_flush_printf_buffer (char *restrict buffer, uint32_t *buffer_index,
uint32_t buffer_capacity)
{
if (*buffer_index > 0 && buffer_capacity > 0)
{
write (STDOUT_FILENO, buffer, *buffer_index);
*buffer_index = 0;
}
}

/* This is a placeholder printf function that will be replaced by calls
* to __pocl_printf(), after an LLVM pass handles the hidden arguments.
* both __pocl_printf and __pocl_printf_format_simple must be referenced
Expand All @@ -734,6 +746,7 @@ printf (const PRINTF_FMT_STR_AS char *restrict fmt, ...)
_printf_buffer_capacity, NULL);

*_printf_buffer_position = p.printf_buffer_index;

return r;
}

Expand Down
30 changes: 21 additions & 9 deletions lib/llvmopencl/Workgroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ static bool callsPrintf(Function *F) {
return true;
if (callee->getName().equals("__pocl_printf"))
return true;
if (callee->getName().equals("__pocl_flush_printf"))
return true;
if (callsPrintf(callee))
return true;
}
Expand Down Expand Up @@ -516,10 +518,12 @@ static Function *cloneFunctionWithPrintfArgs(Value *pb, Value *pbp, Value *pbc,
return NewF;
}

// Recursively replace _cl_printf calls with _pocl_printf calls, while
// propagating the required pocl_context->printf_buffer arguments.
// Recursively replace _cl_printf calls with _pocl_printf calls,
// and __pocl_flush_printf with __pocl_flush_printf_buffer calls
// while propagating the required pocl_context->printf_buffer arguments.
static void replacePrintfCalls(Value *pb, Value *pbp, Value *pbc, bool isKernel,
Function *poclPrintf, Module &M, Function *L,
Function *poclPrintf, Function *poclFlushPrintf,
Module &M, Function *L,
FunctionMapping &printfCache) {

// If none of the kernels use printf(), it will not be linked into the
Expand Down Expand Up @@ -558,7 +562,8 @@ static void replacePrintfCalls(Value *pb, Value *pbp, Value *pbc, bool isKernel,
if (oldF == nullptr)
continue;

if (oldF->getName().equals("printf")) {
if (oldF->getName().equals("printf") ||
oldF->getName().equals("__pocl_flush_printf")) {
ops.clear();
ops.push_back(pb);
ops.push_back(pbp);
Expand All @@ -582,8 +587,10 @@ static void replacePrintfCalls(Value *pb, Value *pbp, Value *pbc, bool isKernel,
ops.push_back(Operand);
}

CallInst *NewCI = CallInst::Create(poclPrintf, ops);
NewCI->setCallingConv(poclPrintf->getCallingConv());
Function *F =
oldF->getName().equals("printf") ? poclPrintf : poclFlushPrintf;
CallInst *NewCI = CallInst::Create(F, ops);
NewCI->setCallingConv(F->getCallingConv());
#ifdef LLVM_OLDER_THAN_11_0
CallSite CS(CallInstr);
NewCI->setTailCall(CS.isTailCall());
Expand Down Expand Up @@ -635,8 +642,8 @@ static void replacePrintfCalls(Value *pb, Value *pbp, Value *pbc, bool isKernel,
needsPrintf = callsPrintf(oldF);
if (needsPrintf) {
newF = cloneFunctionWithPrintfArgs(pb, pbp, pbc, oldF, &M);
replacePrintfCalls(nullptr, nullptr, nullptr, false, poclPrintf, M,
newF, printfCache);
replacePrintfCalls(nullptr, nullptr, nullptr, false, poclPrintf,
poclFlushPrintf, M, newF, printfCache);

printfCache.insert(
std::pair<llvm::Function *, llvm::Function *>(oldF, newF));
Expand Down Expand Up @@ -796,7 +803,12 @@ Workgroup::createWrapper(Function *F, FunctionMapping &printfCache) {

if (DeviceSidePrintf) {
Function *poclPrintf = M->getFunction("__pocl_printf");
replacePrintfCalls(pb, pbp, pbc, true, poclPrintf, *M, L, printfCache);
Function *poclFlushPrintf = M->getFunction("__pocl_flush_printf_buffer");
if (poclFlushPrintf == nullptr) {
M->dump();
}
replacePrintfCalls(pb, pbp, pbc, true, poclPrintf, poclFlushPrintf, *M, L,
printfCache);
}

L->setSubprogram(F->getSubprogram());
Expand Down
18 changes: 18 additions & 0 deletions tests/kernel/test_abort_platform.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
void
subfunction ()
{
printf ("Aborting from subfunction!\n");
#ifdef cl_ext_device_side_abort
abort_platform ();
#else

#error cl_ext_device_side_abort not supported by the device!

#endif
}

kernel void
test_abort_platform ()
{
subfunction ();
}

0 comments on commit d5e88c7

Please # to comment.