Commits

ebo committed 4de5fee

Made tracing more efficient to restrict loop search to opcodes where it can happen.

  • Participants
  • Parent commits db56ab4

Comments (0)

Files changed (1)

 diff --git a/JIT/global_jit_data.h b/JIT/global_jit_data.h
 --- a/JIT/global_jit_data.h
 +++ b/JIT/global_jit_data.h
-@@ -91,6 +91,8 @@
+@@ -81,17 +81,22 @@
+   {
+     active_ = true;
+     suspended_ = false;
++    merge_point_ = false;
+     unsuspend();
+   }
+   void off()
+   {
+     active_ = false;
+     suspended_ = true;
++    merge_point_ = false;
+   }
  
    void unsuspend()
    {
 +    assert(PyThreadState_GET()->interp != NULL);
 +    assert(PyThreadState_GET()->interp->global_jit_data != NULL);
      suspended_ = false;
++    merge_point_ = false;
      trace_.clear();
    }
-@@ -105,6 +107,8 @@
+ 
+@@ -101,10 +106,19 @@
+   }
+ 
+   void trace(PyCodeObject *code, int pc, int opcode, int oparg);
++  void insertMergePoint()
++  {
++    if (!active_ || suspended_)
++      return;
++    merge_point_ = true;
++  }
++
+   void setFlag(TRACE flag)
    {
      if (!active_ || suspended_)
        return;
      TraceEntry &entry = trace_.back();
      entry.flag = flag;
    }
-@@ -113,12 +117,19 @@
+@@ -113,12 +127,19 @@
    {
      if (!active_ || suspended_)
        return;
      }
    }
  
+@@ -129,6 +150,7 @@
+ private:
+   bool active_;
+   bool suspended_;
++  bool merge_point_;
+ 
+   std::vector<TraceEntry> trace_;
+ };
 diff --git a/JIT/global_jit_data_fwd.h b/JIT/global_jit_data_fwd.h
 --- a/JIT/global_jit_data_fwd.h
 +++ b/JIT/global_jit_data_fwd.h
 diff --git a/JIT/jit_tracer.cc b/JIT/jit_tracer.cc
 --- a/JIT/jit_tracer.cc
 +++ b/JIT/jit_tracer.cc
+@@ -52,7 +52,7 @@
+ }
+ 
+ PyJITTracer::PyJITTracer()
+-  : active_(false), suspended_(true)
++  : active_(false), suspended_(true), merge_point_(false)
+ {
+ }
+ 
 @@ -66,7 +66,7 @@
    std::vector<TraceEntry> cut_trace(trace_.begin() + start, trace_.end());
    bool verified = true;
      return;
    }
  
-@@ -144,7 +149,7 @@
- 
-   jit::JITTraceAnalysis analysis;
-   analysis.analyseTrace(cut_trace);
--  // analysis.dump();
-+  analysis.dump();
+@@ -148,7 +153,7 @@
    jit::JITFunctionState state(PyGlobalJITData::Get());
    analysis.emit(state);
    PyGlobalJITData::Get()->optimize(state.function());
+-  state.function()->dump();
++  // state.function()->dump();
+ 
+   typedef int(*pyfunc_t)(PyFrameObject*);
+   pyfunc_t trace_func =
 @@ -160,7 +165,8 @@
  }
  
  {
    if (!active_)
      return;
-@@ -189,6 +195,46 @@
+@@ -170,12 +176,15 @@
+ 
+   TraceEntry entry = TraceEntry(code, pc, opcode, oparg);
+ 
+-  for (size_t i = 0; i < trace_.size(); ++i) {
+-    TraceEntry &old = trace_[i];
+-    if (entry == old) {
+-      suspend();
+-      do_trace(code, i);
+-      return;
++  if (merge_point_) {
++    merge_point_ = false;
++    for (size_t i = 0; i < trace_.size(); ++i) {
++      TraceEntry &old = trace_[i];
++      if (entry == old) {
++        suspend();
++        do_trace(code, i);
++        return;
++      }
+     }
+   }
+ 
+@@ -189,6 +198,46 @@
  
  namespace jit {
  
    JITTraceAnalysis::JITTraceAnalysis()
      : stack_counter_(0)
    {
-@@ -206,37 +252,53 @@
+@@ -206,37 +255,53 @@
      }
    }
  
      for (size_t i = 0; i < trace.size(); ++i) {
        TraceEntry &entry = trace[i];
        int opcode = entry.opcode;
-@@ -249,101 +311,86 @@
+@@ -249,101 +314,86 @@
  
  
        case POP_JUMP_IF_FALSE:
      llvm::Value *v_type =
        builder.CreateLoad(ObjectTy::ob_type(builder, v), "v_type");
      llvm::Value *tp_iternext =
-@@ -351,13 +398,14 @@
+@@ -351,13 +401,14 @@
                           "tp_iternext");
  
      value_ = fbuilder.getState()->CreateCall(tp_iternext, v);
      llvm::Value *localptr = fbuilder.LocalPtr(oparg_);
      llvm::Value *old = builder.CreateLoad(localptr);
      builder.CreateStore(v, localptr);
-@@ -370,6 +418,7 @@
+@@ -370,6 +421,7 @@
      JITFunctionState::BuilderT &builder = fbuilder.builder();
      llvm::Value *localptr = fbuilder.LocalPtr(oparg_);
      value_ = builder.CreateLoad(localptr);
    }
  
    void
-@@ -378,71 +427,34 @@
+@@ -378,71 +430,34 @@
      JITFunctionState::BuilderT &builder = fbuilder.builder();
      llvm::Value *localptr = fbuilder.ConstPtr(oparg_);
      value_ = builder.CreateLoad(localptr);
    }
  
    void
-@@ -452,11 +464,43 @@
+@@ -452,11 +467,43 @@
  
      Value *func = state->GetGlobalFunction<PyObject*(PyObject*, PyObject*, int)>("PyObject_RichCompare");
      value_ = state->CreateCall(func,
    }
  
    void
-@@ -468,13 +512,13 @@
+@@ -468,13 +515,13 @@
      llvm::BasicBlock *trace_cont = state->CreateBasicBlock("trace_cont");
      llvm::BasicBlock *bail = state->CreateBasicBlock("bail");
  
      }
      fbuilder.SetLastI(lasti_);
      fbuilder.SetBailReason(reason_);
-@@ -497,7 +541,7 @@
+@@ -497,7 +544,7 @@
      llvm::BasicBlock *bail_exc = state->CreateBasicBlock("bail_exc");
      llvm::BasicBlock *bail_cont = state->CreateBasicBlock("bail_cont");
  
      Value *py_true = state->EmbedPointer<PyObject*>(&_Py_TrueStruct);
      Value *py_false = state->EmbedPointer<PyObject*>(&_Py_FalseStruct);
  
-@@ -541,7 +585,7 @@
+@@ -541,7 +588,7 @@
  
      state->DecRef(v);
      for (size_t i = 0; i < stack_.size(); ++i) {
      }
      fbuilder.SetLastI(lasti_);
      fbuilder.CreateBailBr();
-@@ -554,7 +598,7 @@
+@@ -554,7 +601,7 @@
    JITTraceEnd::emit(JITFunctionBuilder &fbuilder)
    {
      for (size_t i = 0; i < stack_.size(); ++i) {
      }
      fbuilder.CreateEntryBr();
    }
-@@ -563,7 +607,22 @@
+@@ -563,7 +610,22 @@
    JITIncRef::emit(JITFunctionBuilder &fbuilder)
    {
      JITFunctionState *state = fbuilder.getState();
 diff --git a/JIT/jit_tracer.h b/JIT/jit_tracer.h
 --- a/JIT/jit_tracer.h
 +++ b/JIT/jit_tracer.h
-@@ -9,43 +9,136 @@
+@@ -9,43 +9,138 @@
  #include "JIT/global_jit_data.h"
  #include "JIT/jit_blackhole.h"
  #include "JIT/jit_fbuilder.h"
 +      std::cout << "OPCODE_INFO\n";
 +    }
 +
-+    virtual void emit(JITFunctionBuilder &)
-+    {}
++    virtual void emit(JITFunctionBuilder &fbuilder)
++    {
++      fbuilder.SetLastI(lasti_);
++    }
 +
 +    virtual uintptr_t type()
 +    {
      virtual void dump()
      {
        std::cout << "FOR_ITER\n";
-@@ -53,19 +146,54 @@
+@@ -53,19 +148,54 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      {}
  
      virtual void dump()
-@@ -75,22 +203,28 @@
+@@ -75,22 +205,28 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      virtual void dump()
      {
        std::cout << "LOAD_FAST\n";
-@@ -98,21 +232,61 @@
+@@ -98,21 +234,61 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      virtual void dump()
      {
        std::cout << "LOAD_CONST\n";
-@@ -120,137 +294,128 @@
+@@ -120,137 +296,128 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      virtual void dump()
      {
        std::cout << "COMPARE_OP " << oparg_ << "\n";
-@@ -258,20 +423,55 @@
+@@ -258,20 +425,55 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      {}
  
      virtual void dump()
-@@ -281,21 +481,50 @@
+@@ -281,21 +483,50 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      {}
  
      virtual void dump()
-@@ -305,24 +534,25 @@
+@@ -305,24 +536,25 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      {}
  
      virtual void dump()
-@@ -332,22 +562,23 @@
+@@ -332,22 +564,23 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      {}
  
      virtual void dump()
-@@ -357,24 +588,41 @@
+@@ -357,24 +590,41 @@
  
      virtual void emit(JITFunctionBuilder &builder);
  
      void dump()
      {
        for (size_t i = 0; i < loaded_stack_.size(); ++i) {
-@@ -387,6 +635,15 @@
+@@ -387,6 +637,15 @@
  
      void emit(JITFunctionState &state);
  
      JITForIter *createForIter(JITValue *v)
      {
        JITForIter *r = new JITForIter(v);
-@@ -394,37 +651,9 @@
+@@ -394,37 +653,9 @@
        return r;
      }
  
        trace_.push_back(r);
        return r;
      }
-@@ -436,16 +665,23 @@
+@@ -436,16 +667,23 @@
        return r;
      }
  
        trace_.push_back(r);
        return r;
      }
-@@ -478,19 +714,18 @@
+@@ -478,19 +716,18 @@
        return r;
      }
  
 +          f->f_stacktop = NULL;
 +          next_instr = first_instr + f->f_lasti;
 +
-+          std::cout << "Finished Trace " << f->f_lasti << " " << val << std::endl;
++          // std::cout << "Finished Trace " << f->f_lasti << " " << val << std::endl;
 +
 +          if (val == jit::BH_EXC) {
 +            x = 0;
              DISPATCH();
  
          PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
-@@ -2468,6 +2541,8 @@
+@@ -2468,6 +2541,9 @@
          PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
          TARGET(JUMP_ABSOLUTE)
              JUMPTO(oparg);
++            trace->insertMergePoint();
 +            if (co->co_jit && INSTR_OFFSET() == co->co_jit->opcode)
 +                goto fast_next_opcode;
  #if FAST_LOOPS
              /* Enabling this path speeds-up all while and for-loops by bypassing
                 the per-loop checks for signals.  By default, this should be turned-off
-@@ -2507,11 +2582,14 @@
+@@ -2507,11 +2583,14 @@
              }
              if (PyErr_Occurred()) {
                  if (!PyErr_ExceptionMatches(