|
| 1 | +// UNSUPPORTED: hip || cuda |
| 2 | + |
| 3 | +// RUN: %clangxx -fsycl -DNDEBUG %s -o %t.out |
| 4 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER |
| 5 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER |
| 6 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER |
| 7 | + |
| 8 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 9 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER |
| 10 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER |
| 11 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER |
| 12 | + |
| 13 | +/* |
| 14 | + clang++ -fsycl -DNDEBUG -o smyl.bin code_location_e2e.cpp // <<--- NDEBUG |
| 15 | + will suppress fileLocation |
| 16 | +
|
| 17 | + -DNDEBUG <-- not debugging, meaning fileName should not be retrievable (for |
| 18 | + data privacy reasons) |
| 19 | +
|
| 20 | + Between having to test against NDEBUG and test from kernel code, |
| 21 | + using assert/abort type of logic is not available. This test just |
| 22 | + outputs strings that it checks with FileCheck. |
| 23 | +
|
| 24 | + This test uses hardcoded file code location positions. If modified, those |
| 25 | + may change. pass in any value to signal "report only" and not run tests. |
| 26 | +
|
| 27 | +*/ |
| 28 | + |
| 29 | +#include <sycl/sycl.hpp> |
| 30 | +using namespace sycl; |
| 31 | + |
| 32 | +// llvm/sycl/doc/design/DeviceLibExtensions.rst |
| 33 | +// Our devicelib support for <cstring> only includes three memory |
| 34 | +// operations, none of the string ones. So we need to provide |
| 35 | +// our own string comparison for kernel calls. |
| 36 | +bool stringsAreSameP(const char *a, const char *b) { |
| 37 | + // If both are nullptr, then they are the same, |
| 38 | + if ((a == nullptr) && (b == nullptr)) |
| 39 | + return true; |
| 40 | + // but if only one, they are not. |
| 41 | + if ((a == nullptr) || (b == nullptr)) |
| 42 | + return false; |
| 43 | + |
| 44 | + int index = 0; |
| 45 | + while (true) { |
| 46 | + if (a[index] != b[index]) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + if (a[index] == '\0') { |
| 50 | + return true; |
| 51 | + } // If we are on this line we know a[i]==b[i]. |
| 52 | + index++; |
| 53 | + } |
| 54 | + // We will never arrive here. |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +template <typename OS> void report(OS &out, detail::code_location code_loc) { |
| 59 | + out << "function {line:col} => " << code_loc.functionName() << " {" |
| 60 | + << code_loc.lineNumber() << ":" << code_loc.columnNumber() << "}" |
| 61 | + << "\n"; |
| 62 | + |
| 63 | + auto fileName = code_loc.fileName(); |
| 64 | + if (fileName == nullptr) |
| 65 | + out << "nullptr for fileName" |
| 66 | + << "\n"; |
| 67 | + else |
| 68 | + out << "fileName: " << code_loc.fileName() << "\n"; |
| 69 | +} |
| 70 | + |
| 71 | +template <typename OS> |
| 72 | +void test(OS &out, detail::code_location &code_loc, const char *fileName, |
| 73 | + const char *funcName, int line, int col) { |
| 74 | + |
| 75 | + // functionName |
| 76 | + auto funcNameStr = code_loc.functionName(); |
| 77 | + auto fNameResult = |
| 78 | + ((funcNameStr != nullptr) && stringsAreSameP(funcNameStr, funcName)) |
| 79 | + ? "OK" |
| 80 | + : "WRONG"; |
| 81 | + out << "code_location.functionName: " << fNameResult << "\n"; |
| 82 | + |
| 83 | + // lineNumber |
| 84 | + auto lineNumberResult = (code_loc.lineNumber() == line) ? "OK" : "WRONG"; |
| 85 | + out << "code_location.lineNumber: " << lineNumberResult << "\n"; |
| 86 | + |
| 87 | + // columnNumber |
| 88 | + auto colNumberResult = (code_loc.columnNumber() == col) ? "OK" : "WRONG"; |
| 89 | + out << "code_location.columnNumber: " << colNumberResult << "\n"; |
| 90 | + |
| 91 | + // fileName |
| 92 | + auto fileNameStr = code_loc.fileName(); |
| 93 | +#ifdef NDEBUG |
| 94 | + // NDEBUG == not debugging == no fileName (for security). |
| 95 | + auto fileNameResult = |
| 96 | + (fileNameStr == nullptr) |
| 97 | + ? "OK" |
| 98 | + : "WRONG - fileName should not be present when NDEBUG defined"; |
| 99 | +#else |
| 100 | + auto fileNameResult = stringsAreSameP(fileName, fileNameStr) ? "OK" : "WRONG"; |
| 101 | +#endif |
| 102 | + out << "code_location.fileName: " << fileNameResult << "\n"; |
| 103 | +} |
| 104 | + |
| 105 | +int main(int argc, char **argv) { |
| 106 | + bool testing = |
| 107 | + (argc <= 1); // Passing in ANY argument means report only, do not test. |
| 108 | + auto code_loc = sycl::detail::code_location::current(); // <-- |
| 109 | + int EXPECTED_LINE = __LINE__ - 1; |
| 110 | +#ifdef NDEBUG |
| 111 | + std::cout << "NDEBUG, therefore no fileName" << std::endl; |
| 112 | +#else |
| 113 | + std::cout << "NDEBUG NOT DEFINED, make sure file name isn't a full path " |
| 114 | + << std::endl; |
| 115 | + std::cout << "file name: " << code_loc.fileName() << std::endl; |
| 116 | +#endif |
| 117 | + std::cout << "------- host test -------" << std::endl; |
| 118 | + report(std::cout, code_loc); |
| 119 | + |
| 120 | + if (testing) |
| 121 | + test(std::cout, code_loc, "code_location_e2e.cpp", "main", EXPECTED_LINE, |
| 122 | + 19); |
| 123 | + |
| 124 | + std::cout << "------- kernel test -------" << std::endl; |
| 125 | + |
| 126 | + queue q; |
| 127 | + q.submit([testing](handler &cgh) { |
| 128 | + sycl::stream out(2024, 400, cgh); |
| 129 | + cgh.single_task<class KHALID>([=]() { |
| 130 | + auto kernel_loc = sycl::detail::code_location::current(); // <-- |
| 131 | + int EXPECTED_LINE = __LINE__ - 1; |
| 132 | + report(out, kernel_loc); |
| 133 | + |
| 134 | + if (testing) |
| 135 | + test(out, kernel_loc, "code_location_e2e.cpp", "operator()", |
| 136 | + EXPECTED_LINE, 25); |
| 137 | + |
| 138 | + out << "-------" << sycl::endl; |
| 139 | + }); |
| 140 | + }); |
| 141 | + q.wait(); |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +// CHECK: ------- host test ------- |
| 147 | +// CHECK: code_location.functionName: OK |
| 148 | +// CHECK-NEXT: code_location.lineNumber: OK |
| 149 | +// CHECK-NEXT: code_location.columnNumber: OK |
| 150 | +// XHECK-NEXT: code_location.fileName: OK |
| 151 | + |
| 152 | +// CHECK: ------- kernel test ------- |
| 153 | +// CHECK: code_location.functionName: OK |
| 154 | +// CHECK-NEXT: code_location.lineNumber: OK |
| 155 | +// CHECK-NEXT: code_location.columnNumber: OK |
| 156 | +// XHECK-NEXT: code_location.fileName: OK |
0 commit comments