Skip to content

Commit

Permalink
add an environment variable to control enhanced error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbaug committed May 26, 2024
1 parent 25c682b commit dea6098
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
98 changes: 98 additions & 0 deletions include/getenv_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
// Copyright (c) 2022-2024 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#pragma once

#include <stdlib.h>
#include <string.h>

#include <string>

#if defined(_WIN32)

#include <windows.h>

#define GETENV( _name, _value ) _dupenv_s( &_value, NULL, _name )
#define FREEENV( _value ) free( _value )

#else

#define GETENV( _name, _value ) _value = getenv(_name)
#define FREEENV( _value ) (void)_value

#endif

static inline bool getControlFromEnvironment(
const char* name,
void* pValue,
size_t size )
{
char* envVal = NULL;
GETENV( name, envVal );

if( envVal != NULL )
{
if( size == sizeof(unsigned int) )
{
unsigned int* puVal = (unsigned int*)pValue;
*puVal = atoi(envVal);
}
else if( strlen(envVal) < size )
{
char* pStr = (char*)pValue;
strcpy( pStr, envVal );
}

FREEENV( envVal );
return true;
}

return false;
}

template <class T>
static bool getControl(
const char* name,
T& value )
{
unsigned int readValue = 0;
bool success = getControlFromEnvironment( name, &readValue, sizeof(readValue) );
if( success )
{
value = readValue;
}

return success;
}

template <>
bool getControl<bool>(
const char* name,
bool& value )
{
unsigned int readValue = 0;
bool success = getControlFromEnvironment( name, &readValue, sizeof(readValue) );
if( success )
{
value = ( readValue != 0 );
}

return success;
}

template <>
bool getControl<std::string>(
const char* name,
std::string& value )
{
char readValue[256] = "";
bool success = getControlFromEnvironment( name, readValue, sizeof(readValue) );
if( success )
{
value = readValue;
}

return success;
}
10 changes: 9 additions & 1 deletion layers/10_cmdbufemu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ clGetExtensionFunctionAddressForPlatform
clInitLayer
```

## Optional Controls

The following environment variables can modify the behavior of the command buffer emulation layer:

| Environment Variable | Behavior | Example Format |
|----------------------|----------|-----------------|
| `CMDBUFEMU_EnhancedErrorChecking` | Enables additional error checking when commands are added to a command buffer using a command buffer "test queue". By default, the additional error checking is disabled. | `export CMDBUFEMU_EnhancedErrorChecking=1`<br/><br/>`set CMDBUFEMU_EnhancedErrorChecking=1` |

## Known Limitations

This section describes some of the limitations of the emulated `cl_khr_command_buffer` functionality:

* Many error conditions are not properly checked for and returned.
* Some error conditions are not properly checked for and returned.
* Many functions are not thread safe.
2 changes: 1 addition & 1 deletion layers/10_cmdbufemu/emulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ typedef struct _cl_command_buffer_khr

void setupTestQueue(cl_command_queue src)
{
if( g_cEnhancedErrorChecking )
if( g_EnhancedErrorChecking )
{
cl_command_queue testQueue = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion layers/10_cmdbufemu/emulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <map>

extern const bool g_cEnhancedErrorChecking;
extern bool g_EnhancedErrorChecking;

extern const struct _cl_icd_dispatch* g_pNextDispatch;

Expand Down
7 changes: 4 additions & 3 deletions layers/10_cmdbufemu/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
#include <cstring>
#include <cstdio>

#include "getenv_util.hpp"
#include "layer_util.hpp"

#include "emulate.h"

// Enhanced error checking can be used to catch additional errors when
// commands are recorded into a command buffer, but relies on tricky
// use of user events that may not work properly with some implementations.
// Disabling enhanced error checking may enable command buffer emulation
// to function properly on more implementations.

const bool g_cEnhancedErrorChecking = true;
bool g_EnhancedErrorChecking = false;

const struct _cl_icd_dispatch* g_pNextDispatch = NULL;

Expand Down Expand Up @@ -285,6 +284,8 @@ CL_API_ENTRY cl_int CL_API_CALL clInitLayer(

_init_dispatch();

getControl("CMDBUFEMU_EnhancedErrorChecking", g_EnhancedErrorChecking);

g_pNextDispatch = target_dispatch;

*layer_dispatch_ret = &dispatch;
Expand Down

0 comments on commit dea6098

Please sign in to comment.