Debugging aid: If you want to modify a kernel that fails and you cannot
modify the producer of the kernel, this patch allows you to replace a
kernel "on the fly". You just need the original kernel (find it
from the kcache temp dir) and a replacement kernel.

This is useful when debugging pyopencl test cases
etc. which generate kernels on the fly.

diff --git a/lib/CL/clCreateProgramWithSource.c b/lib/CL/clCreateProgramWithSource.c
index 0b34cdc..c80d6d2 100644
--- a/lib/CL/clCreateProgramWithSource.c
+++ b/lib/CL/clCreateProgramWithSource.c
@@ -94,6 +94,29 @@ POname(clCreateProgramWithSource)(cl_context context,
 
   *source = '\0';
 
+  static int first = 1;
+  static char *debug_targeted_kernel;
+  static char *replacement_kernel;
+  if (first) {
+    uint64_t fsize;
+    int err
+      = pocl_read_file ("target.cl",
+                        &debug_targeted_kernel, &fsize);
+    if (err != 0) abort();
+
+    err = pocl_read_file ("replacement.cl",
+                          &replacement_kernel, &fsize);
+    if (err != 0) abort();
+    first = 0;
+  }
+  if (strcmp (program->source, debug_targeted_kernel) == 0)
+  {
+    POCL_MSG_PRINT_INFO ("Detected the kernel of interest, "
+                         "replacing with a modified one...");
+    free (program->source);
+    program->source = strdup (replacement_kernel);
+  }
+
   program->context = context;
   program->num_devices = context->num_devices;
   program->devices = context->devices;
