[1/2] intel/compiler: Add a placement version of emit to the builders

Submitted by Neil Roberts on Sept. 22, 2017, 3:09 p.m.

Details

Message ID 20170922150940.16039-1-bpeeluk@yahoo.co.uk
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Neil Roberts Sept. 22, 2017, 3:09 p.m.
Adds “emit_emplace” to emit an instruction that is constructed inplace
with the parameters given. This will be used instead of the pattern of
doing emit(instruction(…)) because that ends up calling emit(const
instruction&) which implies doing a copy.

The “emplace” terminology comes from std::vector::emplace_back which
does a very similar thing.
---

Sorry if this email ends up getting sent twice. It didn’t seem to work
the first time.

 src/intel/compiler/brw_fs_builder.h   | 14 ++++++++++++++
 src/intel/compiler/brw_vec4_builder.h | 14 ++++++++++++++
 2 files changed, 28 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/intel/compiler/brw_fs_builder.h b/src/intel/compiler/brw_fs_builder.h
index 87394bc..90d8285 100644
--- a/src/intel/compiler/brw_fs_builder.h
+++ b/src/intel/compiler/brw_fs_builder.h
@@ -25,6 +25,7 @@ 
 #ifndef BRW_FS_BUILDER_H
 #define BRW_FS_BUILDER_H
 
+#include <utility>
 #include "brw_ir_fs.h"
 #include "brw_shader.h"
 
@@ -256,6 +257,19 @@  namespace brw {
       }
 
       /**
+       * Insert an instruction by constructing it directly inplace. This uses
+       * perfect forwarding magic from C++11 to forward the arguments to the
+       * constructor.
+       */
+      template<typename... Args>
+      instruction *
+      emit_emplace(Args&&... args) const
+      {
+         return emit(new(shader->mem_ctx)
+                     instruction(std::forward<Args>(args)...));
+      }
+
+      /**
        * Create and insert a nullary control instruction into the program.
        */
       instruction *
diff --git a/src/intel/compiler/brw_vec4_builder.h b/src/intel/compiler/brw_vec4_builder.h
index 4c3efe8..413b27c 100644
--- a/src/intel/compiler/brw_vec4_builder.h
+++ b/src/intel/compiler/brw_vec4_builder.h
@@ -25,6 +25,7 @@ 
 #ifndef BRW_VEC4_BUILDER_H
 #define BRW_VEC4_BUILDER_H
 
+#include <utility>
 #include "brw_ir_vec4.h"
 #include "brw_ir_allocator.h"
 
@@ -221,6 +222,19 @@  namespace brw {
       }
 
       /**
+       * Insert an instruction by constructing it directly inplace. This uses
+       * perfect forwarding magic from C++11 to forward the arguments to the
+       * constructor.
+       */
+      template<typename... Args>
+      instruction *
+      emit_emplace(Args&&... args) const
+      {
+         return emit(new(shader->mem_ctx)
+                     instruction(std::forward<Args>(args)...));
+      }
+
+      /**
        * Create and insert a nullary control instruction into the program.
        */
       instruction *