changeset 24238:e622b58d78a2

jit: Get rid of residual calls to OCT_MAKE_DECL_LIST and OCT_ITERATE_MACRO * libinterp/parse-tree/jit-ir.h: Get rid of residual calls to OCT_MAKE_DECL_LIST and OCT_ITERATE_MACRO. * libinterp/parse-tree/jit-typeinfo.h: Idem. * libinterp/parse-tree/jit-util.h: Idem. * libinterp/parse-tree/pt-jit.h: Idem.
author Julien Bect <jbect@users.sourceforge.net>
date Sat, 07 Oct 2017 09:59:16 +0200
parents 8d8e4d8e6fdc
children 51e408a7d38f
files libinterp/parse-tree/jit-ir.h libinterp/parse-tree/jit-typeinfo.h libinterp/parse-tree/jit-util.h libinterp/parse-tree/pt-jit.h
diffstat 4 files changed, 147 insertions(+), 149 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/jit-ir.h	Tue Oct 17 07:13:28 2017 +0200
+++ b/libinterp/parse-tree/jit-ir.h	Sat Oct 07 09:59:16 2017 +0200
@@ -109,33 +109,14 @@
 
   const value_list& constants (void) const { return mconstants; }
 
-  template <typename T>
-  T * create (void)
+  template <typename T, typename ...Args>
+  T * create (const Args&... args)
   {
-    T *ret = new T ();
+    T *ret = new T (args...);
     track_value (ret);
     return ret;
   }
 
-#define DECL_ARG(n) const ARG ## n& arg ## n
-
-#define JIT_CREATE(N)                                           \
-  template <typename T, OCT_MAKE_DECL_LIST (typename, ARG, N)>  \
-  T * create (OCT_MAKE_LIST (DECL_ARG, N))                      \
-  {                                                             \
-    T *ret = new T (OCT_MAKE_ARG_LIST (arg, N));                \
-    track_value (ret);                                          \
-    return ret;                                                 \
-  }
-
-  JIT_CREATE (1)
-  JIT_CREATE (2)
-  JIT_CREATE (3)
-  JIT_CREATE (4)
-
-#undef JIT_CREATE
-#undef DECL_ARG
-
 private:
 
   void track_value (jit_value *v);
@@ -344,23 +325,15 @@
     marguments.reserve (nargs);
   }
 
-#define STASH_ARG(i) stash_argument (i, arg ## i);
-
-#define JIT_INSTRUCTION_CTOR(N)                                         \
-  jit_instruction (OCT_MAKE_DECL_LIST (jit_value *, arg, N))            \
-  : already_infered (N), marguments (N), mid (next_id ()), mparent (0)  \
-  {                                                                     \
-    OCT_ITERATE_MACRO (STASH_ARG, N);                                   \
+  template <typename ...Args>
+  jit_instruction (jit_value * arg1, Args... other_args)
+    : already_infered (1 + sizeof... (other_args)),
+      marguments (1 + sizeof... (other_args)),
+      mid (next_id ()), mparent (nullptr)
+  {
+    stash_argument (0, arg1, other_args...);
   }
 
-  JIT_INSTRUCTION_CTOR(1)
-  JIT_INSTRUCTION_CTOR(2)
-  JIT_INSTRUCTION_CTOR(3)
-  JIT_INSTRUCTION_CTOR(4)
-
-#undef STASH_ARG
-#undef JIT_INSTRUCTION_CTOR
-
   jit_instruction (const std::vector<jit_value *>& aarguments)
     : already_infered (aarguments.size ()), marguments (aarguments.size ()),
       mid (next_id ()), mparent (0)
@@ -404,11 +377,18 @@
       return os << "NULL";
   }
 
-  void stash_argument (size_t i, jit_value *arg)
+  void stash_argument (size_t i, jit_value * arg)
   {
     marguments[i].stash_value (arg, this, i);
   }
 
+  template <typename ...Args>
+  void stash_argument (size_t i, jit_value * arg1, Args... aargs)
+  {
+    marguments[i].stash_value (arg1, this, i);
+    stash_argument (++i, aargs...);
+  }
+
   void push_argument (jit_value *arg)
   {
     marguments.push_back (jit_use ());
@@ -1029,18 +1009,11 @@
 {
 public:
 
-#define JIT_TERMINATOR_CONST(N)                                 \
-  jit_terminator (size_t asuccessor_count,                      \
-                  OCT_MAKE_DECL_LIST (jit_value *, arg, N))     \
-    : jit_instruction (OCT_MAKE_ARG_LIST (arg, N)),             \
+  template <typename ...Args>
+  jit_terminator (size_t asuccessor_count, Args... args)
+    : jit_instruction (args...),
       malive (asuccessor_count, false) { }
 
-  JIT_TERMINATOR_CONST (1)
-  JIT_TERMINATOR_CONST (2)
-  JIT_TERMINATOR_CONST (3)
-
-#undef JIT_TERMINATOR_CONST
-
   jit_block * successor (size_t idx = 0) const
   {
     return static_cast<jit_block *> (argument (idx));
@@ -1152,22 +1125,17 @@
       stash_type (ol.result ());
   }
 
-#define JIT_CALL_CONST(N)                                               \
-  jit_call (const jit_operation& aoperation,                            \
-            OCT_MAKE_DECL_LIST (jit_value *, arg, N))                   \
-    : jit_instruction (OCT_MAKE_ARG_LIST (arg, N)), moperation (aoperation) { } \
-                                                                        \
-  jit_call (const jit_operation& (*aoperation) (void),                  \
-            OCT_MAKE_DECL_LIST (jit_value *, arg, N))                   \
-    : jit_instruction (OCT_MAKE_ARG_LIST (arg, N)), moperation (aoperation ()) \
+  template <typename ...Args>
+  jit_call (const jit_operation& aoperation,
+            jit_value * arg1, Args... other_args)
+    : jit_instruction (arg1, other_args...), moperation (aoperation)
   { }
 
-  JIT_CALL_CONST (1)
-  JIT_CALL_CONST (2)
-  JIT_CALL_CONST (3)
-  JIT_CALL_CONST (4)
-
-#undef JIT_CALL_CONST
+  template <typename ...Args>
+  jit_call (const jit_operation& (*aoperation) (void),
+            jit_value * arg1, Args... other_args)
+    : jit_instruction (arg1, other_args...), moperation (aoperation ())
+  { }
 
   jit_call (const jit_operation& aoperation,
             const std::vector<jit_value *>& args)
--- a/libinterp/parse-tree/jit-typeinfo.h	Tue Oct 17 07:13:28 2017 +0200
+++ b/libinterp/parse-tree/jit-typeinfo.h	Sat Oct 07 09:59:16 2017 +0200
@@ -255,34 +255,49 @@
   llvm::BasicBlock * new_block (const std::string& aname = "body",
                                 llvm::BasicBlock *insert_before = nullptr);
 
+  typedef std::vector<llvm::Value *> arg_vec;
+
+  llvm::Value * call (llvm::IRBuilderD& builder,
+                      const arg_vec& in_args = arg_vec ()) const;
+
   llvm::Value * call (llvm::IRBuilderD& builder,
                       const std::vector<jit_value *>& in_args) const;
 
-  llvm::Value * call (llvm::IRBuilderD& builder,
-                      const std::vector<llvm::Value *>& in_args
-                      = std::vector<llvm::Value *> ()) const;
+  template <typename ...Args>
+  llvm::Value * call (llvm::IRBuilderD& builder, arg_vec& in_args,
+                      llvm::Value * arg1, Args... other_args) const
+  {
+    in_args.push_back (arg1);
+    return call (builder, in_args, other_args...);
+  }
 
-#define JIT_PARAM_ARGS llvm::IRBuilderD& builder,
-#define JIT_PARAMS builder,
-#define JIT_CALL(N) JIT_EXPAND (llvm::Value *, call, llvm::Value *, const, N)
+  template <typename T, typename ...Args>
+  llvm::Value * call (llvm::IRBuilderD& builder, arg_vec& in_args,
+                      T * arg1, Args... other_args) const
+  {
+    in_args.push_back (arg1->to_llvm ());
+    return call (builder, in_args, other_args...);
+  }
 
-  JIT_CALL (1)
-  JIT_CALL (2)
-  JIT_CALL (3)
-  JIT_CALL (4)
-  JIT_CALL (5)
-
-#undef JIT_CALL
+  template <typename ...Args>
+  llvm::Value * call (llvm::IRBuilderD& builder, llvm::Value * arg1,
+                      Args... other_args) const
+  {
+    arg_vec in_args;
+    in_args.reserve (1 + sizeof... (other_args));
+    in_args.push_back (arg1);
+    return call (builder, in_args, other_args...);
+  }
 
-#define JIT_CALL(N) JIT_EXPAND (llvm::Value *, call, jit_value *, const, N)
-
-  JIT_CALL (1);
-  JIT_CALL (2);
-  JIT_CALL (3);
-
-#undef JIT_CALL
-#undef JIT_PARAMS
-#undef JIT_PARAM_ARGS
+  template <typename T, typename ...Args>
+  llvm::Value * call (llvm::IRBuilderD& builder, T * arg1,
+                      Args... other_args) const
+  {
+    arg_vec in_args;
+    in_args.reserve (1 + sizeof... (other_args));
+    in_args.push_back (arg1->to_llvm ());
+    return call (builder, in_args, other_args...);
+  }
 
   llvm::Value * argument (llvm::IRBuilderD& builder, size_t idx) const;
 
@@ -341,24 +356,45 @@
 
   const jit_function& overload (const signature_vec& types) const;
 
+  template <typename ...Args>
+  const jit_function& overload (signature_vec& args, jit_type * arg1,
+                                Args... other_args) const
+  {
+    args.push_back (arg1);
+    return overload (args, other_args...);
+  }
+
+  template <typename ...Args>
+  const jit_function& overload (jit_type * arg1, Args... other_args) const
+  {
+    signature_vec args;
+    args.reserve (1 + sizeof... (other_args));
+    args.push_back (arg1);
+    return overload (args, other_args...);
+  }
+
   jit_type * result (const signature_vec& types) const
   {
     const jit_function& temp = overload (types);
     return temp.result ();
   }
 
-#define JIT_PARAMS
-#define JIT_PARAM_ARGS
-#define JIT_OVERLOAD(N)                                              \
-  JIT_EXPAND (const jit_function&, overload, jit_type *, const, N)   \
-  JIT_EXPAND (jit_type *, result, jit_type *, const, N)
+  template <typename ...Args>
+  jit_type * result (signature_vec& args, jit_type * arg1,
+                     Args... other_args) const
+  {
+    args.push_back (arg1);
+    return overload (args, other_args...);
+  }
 
-  JIT_OVERLOAD (1);
-  JIT_OVERLOAD (2);
-  JIT_OVERLOAD (3);
-
-#undef JIT_PARAMS
-#undef JIT_PARAM_ARGS
+  template <typename ...Args>
+  jit_type * result (jit_type * arg1, Args... other_args) const
+  {
+    signature_vec args;
+    args.reserve (1 + sizeof... (other_args));
+    args.push_back (arg1);
+    return overload (args, other_args...);
+  }
 
   const std::string& name (void) const { return mname; }
 
@@ -679,13 +715,16 @@
 
   void add_binary_fcmp (jit_type *ty, int op, int llvm_op);
 
+  // type signature vector
+  typedef std::vector<jit_type *> signature_vec;
+
   // create a function with an external calling convention
   // forces the function pointer to be specified
   template <typename T>
   jit_function create_external (llvm::ExecutionEngine *ee, T fn,
                                 const llvm::Twine& name, jit_type *ret,
-                                const std::vector<jit_type *>& args
-                                = std::vector<jit_type *> ())
+                                const signature_vec& args
+                                = signature_vec ())
   {
     jit_function retval = create_function (jit_convention::external, name, ret,
                                            args);
@@ -693,21 +732,26 @@
     return retval;
   }
 
-#define JIT_PARAM_ARGS llvm::ExecutionEngine *ee, T fn, \
-    const llvm::Twine& name, jit_type *ret,
-#define JIT_PARAMS ee, fn, name, ret,
-#define CREATE_FUNCTION(N) JIT_EXPAND(template <typename T> jit_function, \
-                                      create_external,                  \
-                                      jit_type *, /* empty */, N)
+  template <typename T, typename ...Args>
+  jit_function create_external (llvm::ExecutionEngine *ee, T fn,
+                                const llvm::Twine& name, jit_type *ret,
+                                signature_vec& args, jit_type * arg1,
+                                Args... other_args)
+  {
+    args.push_back (arg1);
+    return create_external (ee, fn, name, ret, args, other_args...);
+  }
 
-  CREATE_FUNCTION(1);
-  CREATE_FUNCTION(2);
-  CREATE_FUNCTION(3);
-  CREATE_FUNCTION(4);
-
-#undef JIT_PARAM_ARGS
-#undef JIT_PARAMS
-#undef CREATE_FUNCTION
+  template <typename T, typename ...Args>
+  jit_function create_external (llvm::ExecutionEngine *ee, T fn,
+                                const llvm::Twine& name, jit_type *ret,
+                                jit_type * arg1, Args... other_args)
+  {
+    signature_vec args;
+    args.reserve (1 + sizeof... (other_args));
+    args.push_back (arg1);
+    return create_external (ee, fn, name, ret, args, other_args...);
+  }
 
   // use create_external or create_internal directly
   jit_function create_function (jit_convention::type cc,
@@ -717,25 +761,30 @@
 
   // create an internal calling convention (a function defined in llvm)
   jit_function create_internal (const llvm::Twine& name, jit_type *ret,
-                                const std::vector<jit_type *>& args
-                                = std::vector<jit_type *> ())
+                                const signature_vec& args
+                                = signature_vec ())
   {
     return create_function (jit_convention::internal, name, ret, args);
   }
 
-#define JIT_PARAM_ARGS const llvm::Twine& name, jit_type *ret,
-#define JIT_PARAMS name, ret,
-#define CREATE_FUNCTION(N) JIT_EXPAND(jit_function, create_internal,    \
-                                      jit_type *, /* empty */, N)
+  template <typename ...Args>
+  jit_function create_internal (const llvm::Twine& name, jit_type *ret,
+                                signature_vec& args,
+                                jit_type * arg1, Args... other_args)
+  {
+    args.push_back (arg1);
+    return create_internal (name, ret, args, other_args...);
+  }
 
-  CREATE_FUNCTION(1);
-  CREATE_FUNCTION(2);
-  CREATE_FUNCTION(3);
-  CREATE_FUNCTION(4);
-
-#undef JIT_PARAM_ARGS
-#undef JIT_PARAMS
-#undef CREATE_FUNCTION
+  template <typename ...Args>
+  jit_function create_internal (const llvm::Twine& name, jit_type *ret,
+                                jit_type * arg1, Args... other_args)
+  {
+    signature_vec args;
+    args.reserve (1 + sizeof... (other_args));
+    args.push_back (arg1);
+    return create_internal (name, ret, args, other_args...);
+  }
 
   jit_function create_identity (jit_type *type);
 
--- a/libinterp/parse-tree/jit-util.h	Tue Oct 17 07:13:28 2017 +0200
+++ b/libinterp/parse-tree/jit-util.h	Sat Oct 07 09:59:16 2017 +0200
@@ -206,14 +206,5 @@
   return dynamic_cast<T *> (value);
 }
 
-#define JIT_ASSIGN_ARG(i) the_args[i] = arg ## i;
-#define JIT_EXPAND(ret, fname, type, isconst, N)                        \
-  ret fname (JIT_PARAM_ARGS OCT_MAKE_DECL_LIST (type, arg, N)) isconst  \
-  {                                                                     \
-    std::vector<type> the_args (N);                                     \
-    OCT_ITERATE_MACRO (JIT_ASSIGN_ARG, N);                              \
-    return fname (JIT_PARAMS the_args);                                 \
-  }
-
 #endif
 #endif
--- a/libinterp/parse-tree/pt-jit.h	Tue Oct 17 07:13:28 2017 +0200
+++ b/libinterp/parse-tree/pt-jit.h	Sat Oct 07 09:59:16 2017 +0200
@@ -48,23 +48,13 @@
 
   jit_convert (octave_user_function& fcn, const std::vector<jit_type *>& args);
 
-#define DECL_ARG(n) const ARG ## n& arg ## n
-#define JIT_CREATE_CHECKED(N)                                           \
-  template <OCT_MAKE_DECL_LIST (typename, ARG, N)>                      \
-  jit_call * create_checked (OCT_MAKE_LIST (DECL_ARG, N))               \
-  {                                                                     \
-    jit_call *ret = factory.create<jit_call> (OCT_MAKE_ARG_LIST (arg, N)); \
-    return create_checked_impl (ret);                                   \
+  template <typename ...Args>
+  jit_call * create_checked (const Args&... args)
+  {
+    jit_call *ret = factory.create<jit_call> (args...);
+    return create_checked_impl (ret);
   }
 
-  JIT_CREATE_CHECKED (1)
-  JIT_CREATE_CHECKED (2)
-  JIT_CREATE_CHECKED (3)
-  JIT_CREATE_CHECKED (4)
-
-#undef JIT_CREATE_CHECKED
-#undef DECL_ARG
-
   jit_block_list& get_blocks (void) { return blocks; }
 
   const type_bound_vector& get_bounds (void) const { return bounds; }