[Beignet] [PATCH 1/5] backend: add support for kernel debugging

Mircea Gherzan mircea.gherzan at intel.com
Fri Jul 8 12:39:35 UTC 2016


The "-debug" option is used to enable the breakpoint bit on the first
instruction of every kernel.

Signed-off-by: Mircea Gherzan <mircea.gherzan at intel.com>
---
 backend/src/backend/context.cpp     |  4 ++--
 backend/src/backend/context.hpp     |  4 ++--
 backend/src/backend/gen_context.cpp | 12 +++++++++++-
 backend/src/backend/gen_context.hpp |  2 +-
 backend/src/backend/gen_program.cpp |  7 +++++--
 backend/src/backend/gen_program.hpp |  8 ++++++--
 backend/src/backend/program.cpp     |  5 ++++-
 7 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/backend/src/backend/context.cpp b/backend/src/backend/context.cpp
index 675dc78..7e18483 100644
--- a/backend/src/backend/context.cpp
+++ b/backend/src/backend/context.cpp
@@ -374,7 +374,7 @@ namespace gbe
     this->JIPs.clear();
   }
 
-  Kernel *Context::compileKernel(void) {
+  Kernel *Context::compileKernel(bool kernelDebug) {
     this->kernel = this->allocateKernel();
     this->kernel->simdWidth = this->simdWidth;
     this->buildArgList();
@@ -386,7 +386,7 @@ namespace gbe
       this->buildJIPs();
     this->buildStack();
     this->handleSLM();
-    if (this->emitCode() == false) {
+    if (this->emitCode(kernelDebug) == false) {
       GBE_DELETE(this->kernel);
       this->kernel = NULL;
     }
diff --git a/backend/src/backend/context.hpp b/backend/src/backend/context.hpp
index 1567bd6..97f3c8e 100644
--- a/backend/src/backend/context.hpp
+++ b/backend/src/backend/context.hpp
@@ -60,7 +60,7 @@ namespace gbe
     /*! start new code generation with specific simd width. */
     void startNewCG(uint32_t simdWidth);
     /*! Compile the code */
-    Kernel *compileKernel(void);
+    Kernel *compileKernel(bool kernelDebug);
     /*! Tells if the labels is used */
     INLINE bool isLabelUsed(ir::LabelIndex index) const {
       return usedLabels.contains(index);
@@ -122,7 +122,7 @@ namespace gbe
     }
   protected:
     /*! Build the instruction stream. Return false if failed */
-    virtual bool emitCode(void) = 0;
+    virtual bool emitCode(bool kernelDebug) = 0;
     /*! Align the scratch size to the device's scratch unit size */
     virtual uint32_t alignScratchSize(uint32_t) = 0;
     /*! Get the device's max srcatch size */
diff --git a/backend/src/backend/gen_context.cpp b/backend/src/backend/gen_context.cpp
index 8802efc..4ca115b 100644
--- a/backend/src/backend/gen_context.cpp
+++ b/backend/src/backend/gen_context.cpp
@@ -3841,7 +3841,7 @@ namespace gbe
 
   BVAR(OCL_OUTPUT_SEL_IR, false);
   BVAR(OCL_OPTIMIZE_SEL_IR, true);
-  bool GenContext::emitCode(void) {
+  bool GenContext::emitCode(bool kernelDebug) {
     GenKernel *genKernel = static_cast<GenKernel*>(this->kernel);
     sel->select();
     if (OCL_OPTIMIZE_SEL_IR)
@@ -3863,6 +3863,16 @@ namespace gbe
     this->emitInstructionStream();
     if (this->patchBranches() == false)
       return false;
+
+    // Set the breakpint bit if kernel debugging is enabled
+    if (kernelDebug) {
+      // Check if the first instruction is a compact one
+      if (((GenCompactInstruction *)&p->store[0])->bits1.cmpt_control)
+        ((GenCompactInstruction *)&p->store[0])->bits1.debug_control = 1;
+      else
+        ((GenNativeInstruction *)&p->store[0])->header.debug_control = 1;
+    }
+
     genKernel->insnNum = p->store.size();
     genKernel->insns = GBE_NEW_ARRAY_NO_ARG(GenInstruction, genKernel->insnNum);
     std::memcpy(genKernel->insns, &p->store[0], genKernel->insnNum * sizeof(GenInstruction));
diff --git a/backend/src/backend/gen_context.hpp b/backend/src/backend/gen_context.hpp
index fb3d4fe..baaf298 100644
--- a/backend/src/backend/gen_context.hpp
+++ b/backend/src/backend/gen_context.hpp
@@ -75,7 +75,7 @@ namespace gbe
     /*! Target device ID*/
     uint32_t deviceID;
     /*! Implements base class */
-    virtual bool emitCode(void);
+    virtual bool emitCode(bool kernelDebug);
     /*! Align the scratch size to the device's scratch unit size */
     virtual uint32_t alignScratchSize(uint32_t size);
     /*! Get the device's max srcatch size */
diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 88010c2..d03e553 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -201,7 +201,7 @@ namespace gbe {
         GBE_ASSERT(0);
       simdFn->setSimdWidth(simdWidth);
       ctx->startNewCG(simdWidth, reservedSpillRegs, limitRegisterPressure);
-      kernel = ctx->compileKernel();
+      kernel = ctx->compileKernel(this->kernel_debug);
       if (kernel != NULL) {
         GBE_ASSERT(ctx->getErrCode() == NO_ERROR);
         break;
@@ -426,11 +426,14 @@ namespace gbe {
   {
     using namespace gbe;
     uint32_t fast_relaxed_math = 0;
+    const bool debug = options && strstr(options, "-debug");
+
     if (options != NULL)
       if (strstr(options, "-cl-fast-relaxed-math") != NULL)
         fast_relaxed_math = 1;
 
-    GenProgram *program = GBE_NEW(GenProgram, deviceID, module, llvm_ctx, asm_file_name, fast_relaxed_math);
+    GenProgram *program = GBE_NEW(GenProgram, deviceID, module, llvm_ctx,
+                                  asm_file_name, fast_relaxed_math, debug);
 #ifdef GBE_COMPILER_AVAILABLE
     std::string error;
     // Try to compile the program
diff --git a/backend/src/backend/gen_program.hpp b/backend/src/backend/gen_program.hpp
index 076f617..fae7cb6 100644
--- a/backend/src/backend/gen_program.hpp
+++ b/backend/src/backend/gen_program.hpp
@@ -60,8 +60,10 @@ namespace gbe
   {
   public:
     /*! Create an empty program */
-    GenProgram(uint32_t deviceID, const void* mod = NULL, const void* ctx = NULL, const char* asm_fname = NULL, uint32_t fast_relaxed_math = 0) :
-      Program(fast_relaxed_math), deviceID(deviceID),module((void*)mod), llvm_ctx((void*)ctx), asm_file_name(asm_fname) {}
+    GenProgram(uint32_t deviceID, const void* mod = NULL, const void* ctx = NULL,
+               const char* asm_fname = NULL, uint32_t fast_relaxed_math = 0, bool k_debug = false) :
+      Program(fast_relaxed_math), deviceID(deviceID), module((void*)mod),
+      llvm_ctx((void*)ctx), asm_file_name(asm_fname), kernel_debug(k_debug) {}
     /*! Current device ID*/
     uint32_t deviceID;
     /*! Destroy the program */
@@ -79,6 +81,8 @@ namespace gbe
     const char* asm_file_name;
     /*! Use custom allocators */
     GBE_CLASS(GenProgram);
+  private:
+    const bool kernel_debug __attribute__((unused));
   };
   /*! decompact GEN ASM if it is in compacted format */
   extern void decompactInstruction(union GenCompactInstruction *p, void *insn);
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index d13cba5..89286d8 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -610,9 +610,12 @@ namespace gbe {
     // Arguments to pass to the clang frontend
     vector<const char *> args;
     bool bFastMath = false;
+    // List of options that are should not be forwarded to Clang
+    static const char privateOptions[] = "-debug";
 
     for (auto &s : options) {
-      args.push_back(s.c_str());
+      if (strstr(privateOptions, s.c_str()) == NULL)
+        args.push_back(s.c_str());
     }
 
     args.push_back("-cl-kernel-arg-info");
-- 
1.8.3.1



More information about the Beignet mailing list