Skip to content

[SYCL][XPTI] Enable code location info when NDEBUG is not defined #5023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions sycl/include/CL/sycl/detail/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,46 @@
__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

#if !defined(NDEBUG) && (_MSC_VER > 1929 || __has_builtin(__builtin_FILE))
#define __CODELOC_FILE_NAME __builtin_FILE()
#else
#define __CODELOC_FILE_NAME nullptr
#endif

#if _MSC_VER > 1929 || __has_builtin(__builtin_FUNCTION)
#define __CODELOC_FUNCTION __builtin_FUNCTION()
#else
#define __CODELOC_FUNCTION nullptr
#endif

#if _MSC_VER > 1929 || __has_builtin(__builtin_LINE)
#define __CODELOC_LINE __builtin_LINE()
#else
#define __CODELOC_LINE 0
#endif

#if _MSC_VER > 1929 || __has_builtin(__builtin_LINE)
#define __CODELOC_COLUMN __builtin_COLUMN()
#else
#define __CODELOC_COLUMN 0
#endif

// Data structure that captures the user code location information using the
// builtin capabilities of the compiler
struct code_location {
#ifdef _MSC_VER
// Since MSVC does not support the required builtins, we
// implement the version with "unknown"s which is handled
// correctly by the instrumentation
static constexpr code_location current(const char *fileName = nullptr,
const char *funcName = nullptr,
unsigned long lineNo = 0,
unsigned long columnNo = 0) noexcept {
return code_location(fileName, funcName, lineNo, columnNo);
}
#else
// FIXME Having a nullptr for fileName here is a short-term solution to
// workaround leak of full paths in builds
static constexpr code_location
current(const char *fileName = nullptr,
const char *funcName = __builtin_FUNCTION(),
unsigned long lineNo = __builtin_LINE(),
unsigned long columnNo = 0) noexcept {
current(const char *fileName = __CODELOC_FILE_NAME,
const char *funcName = __CODELOC_FUNCTION,
unsigned long lineNo = __CODELOC_LINE,
unsigned long columnNo = __CODELOC_COLUMN) noexcept {
return code_location(fileName, funcName, lineNo, columnNo);
}
#endif

#undef __CODELOC_FILE_NAME
#undef __CODELOC_FUNCTION
#undef __CODELOC_LINE
#undef __CODELOC_COLUMN

constexpr code_location(const char *file, const char *func, int line,
int col) noexcept
Expand Down
37 changes: 37 additions & 0 deletions sycl/test/basic_tests/code_location.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %clangxx -fsycl -DNDEBUG %s -o %t.out
// RUN: %t.out
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %t.out

#include <CL/sycl.hpp>
#include <cassert>
#include <iostream>

int main() {
auto code_loc = sycl::detail::code_location::current();
const char *funcName = "main";
#ifdef NDEBUG
if (code_loc.fileName() != nullptr)
return 1;
if (code_loc.functionName() != funcName)
return 1;
if (code_loc.lineNumber() != 11)
return 1;
if (code_loc.columnNumber() != 19)
return 1;
#else
assert((code_loc.fileName() != nullptr));
std::string str = code_loc.fileName();
assert(((str.find("code_location.cpp") != std::string::npos) &&
"Filename is wrong"));
assert((code_loc.functionName() != nullptr));
str = code_loc.functionName();
assert(
((str.find(funcName) != std::string::npos) && "Function name is wrong"));
assert((code_loc.lineNumber() != 0));
assert(((code_loc.lineNumber() == 11) && "Line number is wrong"));
assert((code_loc.columnNumber() != 0));
assert(((code_loc.columnNumber() == 19) && "Column number is wrong"));
#endif
return 0;
}