[PATCH 141/144] add testcase for llvm segfault (issue #889)
authorAndreas Beckmann <anbe@debian.org>
Wed, 24 Nov 2021 21:39:43 +0000 (22:39 +0100)
committerAndreas Beckmann <anbe@debian.org>
Fri, 7 Jan 2022 23:55:22 +0000 (23:55 +0000)
The segmentation fault can be observed with llvm-10, llvm-11 and llvm-12
and seems to be fixed in llvm-13. It happens on the architectures armhf
and armel (both 32-bit) always and on x86_64 sporadically.
The test segfaults only on the first run (i.e. the kernel is not yet in
pocl's kernel cache) while it passes on subsequent execution (with
something already in the kernel cache), emitting only some llvm
diagnostics:

inlinable function call in a function with debug info must have a !dbg location
  %11 = call i32 @_Z12get_local_idj(i32 0)
inlinable function call in a function with debug info must have a !dbg location
  %19 = call i32 @_Z12get_local_idj(i32 1)
inlinable function call in a function with debug info must have a !dbg location
  %27 = call i32 @_Z12get_local_idj(i32 2)

The backtrace of the segmentation fault as observed with llvm-10 and pocl 1.6:
 #0  getEmissionKind () at .../llvm/include/llvm/IR/DebugInfoMetadata.h:1244
 #1  initialize () at .../llvm/lib/CodeGen/LexicalScopes.cpp:53
 #2  0xb14102f0 in computeIntervals () at .../llvm/lib/CodeGen/LiveDebugVariables.cpp:979
 #3  runOnMachineFunction () at .../llvm/lib/CodeGen/LiveDebugVariables.cpp:996
 #4  runOnMachineFunction () at .../llvm/lib/CodeGen/LiveDebugVariables.cpp:1023
 #5  0xb14856c8 in runOnFunction () at .../llvm/lib/CodeGen/MachineFunctionPass.cpp:73
 #6  0xb12ff494 in runOnFunction () at .../llvm/lib/IR/LegacyPassManager.cpp:1481
 #7  0xb12ff750 in runOnModule () at .../llvm/lib/IR/LegacyPassManager.cpp:1517
 #8  0xb12ffba8 in runOnModule () at .../llvm/lib/IR/LegacyPassManager.cpp:1582
 #9  run () at .../llvm/lib/IR/LegacyPassManager.cpp:1694
 #10 0xb6e64c82 in pocl_llvm_codegen (Device=Device@entry=0xdb0010, Modp=0x1361838, Output=Output@entry=0xbefde86c, OutputSize=OutputSize@entry=0xbefde880) at ./lib/CL/pocl_llvm_wg.cc:624
 #11 0xb6e291de in llvm_codegen (output=output@entry=0xdeb898 "...BMDHA/Sdot_kernel/0-0-0/Sdot_kernel.so", device_i=device_i@entry=0, kernel=kernel@entry=0xbefe0240,
     device=0xdb0010, command=command@entry=0xbefe0278, specialize=specialize@entry=0) at ./lib/CL/devices/common.c:158
 #12 0xb6e2ae44 in pocl_check_kernel_disk_cache (command=command@entry=0xbefe0278, specialized=specialized@entry=0) at ./lib/CL/devices/common.c:958
 #13 0xb6e2b262 in pocl_check_kernel_dlhandle_cache (command=0xbefe0278, initial_refcount=0, specialize=0) at ./lib/CL/devices/common.c:1081
 #14 0xb6e033d4 in program_compile_dynamic_wg_binaries (program=program@entry=0xd8ab88) at ./lib/CL/pocl_build.c:179
 #15 0xb6e13f20 in get_binary_sizes (sizes=0xbefe0384, program=0xd8ab88) at ./lib/CL/clGetProgramInfo.c:36
 #16 POclGetProgramInfo (program=0xd8ab88, param_name=4453, param_value_size=128, param_value=0xbefe0384, param_value_size_ret=0xbefe0380) at ./lib/CL/clGetProgramInfo.c:115
 #17 0x00473070 in main () at 975931.c:238

https://github.com/pocl/pocl/issues/889
https://bugs.debian.org/975931

Gbp-Pq: Name 0141-add-testcase-for-llvm-segfault-issue-889.patch

tests/regression/CMakeLists.txt
tests/regression/test_llvm_segfault_issue_889.c [new file with mode: 0644]

index 2bdb1fcad8cc13e697b85214454079d7ddb8a347..76c98510fc0c2121c7cff5f3ed8cdaf189e62673 100644 (file)
@@ -26,7 +26,9 @@
 
 set(C_PROGRAMS_TO_BUILD test_assign_loop_variable_to_privvar_makes_it_local
      test_program_from_binary_with_local_1_1_1
-     test_assign_loop_variable_to_privvar_makes_it_local_2)
+     test_assign_loop_variable_to_privvar_makes_it_local_2
+  test_llvm_segfault_issue_889
+)
 foreach(PROG ${C_PROGRAMS_TO_BUILD})
   if(MSVC)
     set_source_files_properties( "${PROG}.c" PROPERTIES LANGUAGE CXX )
@@ -76,6 +78,8 @@ add_test_pocl(NAME "regression/test_issue_577" COMMAND "test_issue_577")
 
 add_test_pocl(NAME "regression/test_issue_757" COMMAND "test_issue_757")
 
+add_test_pocl(NAME "regression/test_llvm_segfault_issue_889" COMMAND "test_llvm_segfault_issue_889")
+
 add_test_pocl(NAME "regression/test_issue_893" COMMAND "test_issue_893")
 
 add_test_pocl(NAME "regression/test_flatten_barrier_subs" COMMAND "test_flatten_barrier_subs" EXPECTED_OUTPUT "test_flatten_barrier_subs.output")
@@ -270,6 +274,7 @@ set_tests_properties("regression/setting_a_buffer_argument_to_NULL_causes_a_segf
   "regression/autolocals_in_constexprs" "regression/test_issue_231"
   "regression/test_issue_445" "regression/test_issue_553"
   "regression/test_issue_577" "regression/test_issue_757"
+  "regression/test_llvm_segfault_issue_889"
   "regression/test_issue_893"
   "regression/test_flatten_barrier_subs"
   ${TCE_TESTS}
diff --git a/tests/regression/test_llvm_segfault_issue_889.c b/tests/regression/test_llvm_segfault_issue_889.c
new file mode 100644 (file)
index 0000000..7ed2c1b
--- /dev/null
@@ -0,0 +1,122 @@
+/* This reduced kernel (originally from clblas invoked by libgpuarray
+   tests) triggers a segmentation fault in llvm (10-12) on some platforms.
+   https://bugs.debian.org/975931
+
+   Copyright (c) 2021 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 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 "poclu.h"
+#include <CL/cl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+const char* source =
+"__kernel void Sdot_kernel(__global float *_X, __global float *_Y, __global float *scratchBuff,\n"
+"                          uint N, uint offx, int incx, uint offy, int incy, int doConj)\n"
+"{\n"
+"__global float *X = _X + offx;\n"
+"__global float *Y = _Y + offy;\n"
+"float dotP = (float) 0.0;\n"
+"if ( incx < 0 ) {\n"
+"X = X + (N - 1) * abs(incx);\n"
+"}\n"
+"if ( incy < 0 ) {\n"
+"Y = Y + (N - 1) * abs(incy);\n"
+"}\n"
+"int gOffset;\n"
+"for( gOffset=(get_global_id(0) * 4); (gOffset + 4 - 1)<N; gOffset+=( get_global_size(0) * 4 ) )\n"
+"{\n"
+"float4 vReg1, vReg2, res;\n"
+"vReg1 = (float4)(  (X + (gOffset*incx))[0 + ( incx * 0)],  (X + (gOffset*incx))[0 + ( incx * 1)],  (X + (gOffset*incx))[0 + ( incx * 2)],  (X + (gOffset*incx))[0 + ( incx * 3)]);\n"
+"vReg2 = (float4)(  (Y + (gOffset*incy))[0 + ( incy * 0)],  (Y + (gOffset*incy))[0 + ( incy * 1)],  (Y + (gOffset*incy))[0 + ( incy * 2)],  (Y + (gOffset*incy))[0 + ( incy * 3)]);\n"
+"res =  vReg1 *  vReg2 ;\n"
+"dotP +=  res .S0 +  res .S1 +  res .S2 +  res .S3;\n"
+"}\n"
+"for( ; gOffset<N; gOffset++ )\n"
+"{\n"
+"float sReg1, sReg2, res;\n"
+"sReg1 = X[gOffset * incx];\n"
+"sReg2 = Y[gOffset * incy];\n"
+"res =  sReg1 *  sReg2 ;\n"
+"dotP =  dotP +  res ;\n"
+"}\n"
+"__local float p1753 [ 64 ];\n"
+"uint QKiD0 = get_local_id(0);\n"
+"p1753 [ QKiD0 ] =  dotP ;\n"
+"barrier(CLK_LOCAL_MEM_FENCE);\n"
+"if( QKiD0 < 32 ) {\n"
+"p1753 [ QKiD0 ] = p1753 [ QKiD0 ] + p1753 [ QKiD0 + 32 ];\n"
+"}\n"
+"barrier(CLK_LOCAL_MEM_FENCE);\n"
+"if( QKiD0 < 16 ) {\n"
+"p1753 [ QKiD0 ] = p1753 [ QKiD0 ] + p1753 [ QKiD0 + 16 ];\n"
+"}\n"
+"barrier(CLK_LOCAL_MEM_FENCE);\n"
+"if( QKiD0 < 8 ) {\n"
+"p1753 [ QKiD0 ] = p1753 [ QKiD0 ] + p1753 [ QKiD0 + 8 ];\n"
+"}\n"
+"barrier(CLK_LOCAL_MEM_FENCE);\n"
+"if( QKiD0 < 4 ) {\n"
+"p1753 [ QKiD0 ] = p1753 [ QKiD0 ] + p1753 [ QKiD0 + 4 ];\n"
+"}\n"
+"barrier(CLK_LOCAL_MEM_FENCE);\n"
+"if( QKiD0 < 2 ) {\n"
+"p1753 [ QKiD0 ] = p1753 [ QKiD0 ] + p1753 [ QKiD0 + 2 ];\n"
+"}\n"
+"barrier(CLK_LOCAL_MEM_FENCE);\n"
+"if( QKiD0 == 0 ) {\n"
+"dotP  = p1753 [0] + p1753 [1];\n"
+"}\n"
+"if( (get_local_id(0)) == 0 ) {\n"
+"scratchBuff[ get_group_id(0) ] = dotP;\n"
+"}\n"
+"}\n"
+;
+
+int
+main ()
+{
+  cl_int err;
+  cl_context context;
+  cl_device_id device;
+  cl_command_queue command_queue;
+  poclu_get_any_device (&context, &device, &command_queue);
+
+  cl_program program
+      = clCreateProgramWithSource (context, 1, &source, NULL, &err);
+  CHECK_OPENCL_ERROR_IN ("clCreateProgramWithSource");
+
+  CHECK_CL_ERROR (clBuildProgram (program, 1, &device, "-g", NULL, NULL));
+
+  size_t binsizes[32];
+  size_t nbinaries;
+  CHECK_CL_ERROR (clGetProgramInfo (program, CL_PROGRAM_BINARY_SIZES,
+                                    sizeof (binsizes), binsizes, &nbinaries));
+  for (size_t i = 0; i < nbinaries; ++i)
+    printf ("binary size [%zd]: %zd\n", i, binsizes[i]);
+
+  CHECK_CL_ERROR (clReleaseProgram (program));
+
+  CHECK_CL_ERROR (clReleaseContext (context));
+
+  printf ("OK\n");
+  return EXIT_SUCCESS;
+}