[PATCH vkd3d 2/2] vkd3d-shader: Reserve spirv words upfront instead of checking each time.

Rémi Bernon rbernon at codeweavers.com
Thu Oct 3 12:09:33 CDT 2019


Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---

Same thing, the vkd3d_spirv_build_word function shows up in perf report,
and we can instead reserve space upfront.

 libs/vkd3d-shader/spirv.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/libs/vkd3d-shader/spirv.c b/libs/vkd3d-shader/spirv.c
index 75a3a4f3..d74d2d38 100644
--- a/libs/vkd3d-shader/spirv.c
+++ b/libs/vkd3d-shader/spirv.c
@@ -371,12 +371,17 @@ static uint32_t vkd3d_spirv_opcode_word(SpvOp op, unsigned int word_count)
     return (word_count << SpvWordCountShift) | op;
 }

-static void vkd3d_spirv_build_word(struct vkd3d_spirv_stream *stream, uint32_t word)
+static void vkd3d_spirv_reserve_words(struct vkd3d_spirv_stream *stream, uint32_t count)
 {
     if (!vkd3d_array_reserve((void **)&stream->words, &stream->capacity,
-            stream->word_count + 1, sizeof(*stream->words)))
-        return;
+            stream->word_count + count, sizeof(*stream->words)))
+        ERR("Not enough memory to reserve words.\n");
+    assert(stream->capacity >= stream->word_count + count);
+}

+static void vkd3d_spirv_build_word(struct vkd3d_spirv_stream *stream, uint32_t word)
+{
+    assert(stream->capacity >= stream->word_count + 1);
     stream->words[stream->word_count++] = word;
 }

@@ -563,12 +568,14 @@ static uint32_t vkd3d_spirv_build_once7(struct vkd3d_spirv_builder *builder,
  */
 static void vkd3d_spirv_build_op(struct vkd3d_spirv_stream *stream, SpvOp op)
 {
+    vkd3d_spirv_reserve_words(stream, 1);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 1));
 }

 static void vkd3d_spirv_build_op1(struct vkd3d_spirv_stream *stream,
         SpvOp op, uint32_t operand)
 {
+    vkd3d_spirv_reserve_words(stream, 2);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 2));
     vkd3d_spirv_build_word(stream, operand);
 }
@@ -577,6 +584,7 @@ static void vkd3d_spirv_build_op1v(struct vkd3d_spirv_stream *stream,
         SpvOp op, uint32_t operand0, const uint32_t *operands, unsigned int operand_count)
 {
     unsigned int i;
+    vkd3d_spirv_reserve_words(stream, 2 + operand_count);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 2 + operand_count));
     vkd3d_spirv_build_word(stream, operand0);
     for (i = 0; i < operand_count; ++i)
@@ -588,6 +596,7 @@ static void vkd3d_spirv_build_op2v(struct vkd3d_spirv_stream *stream,
         const uint32_t *operands, unsigned int operand_count)
 {
     unsigned int i;
+    vkd3d_spirv_reserve_words(stream, 3 + operand_count);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 3 + operand_count));
     vkd3d_spirv_build_word(stream, operand0);
     vkd3d_spirv_build_word(stream, operand1);
@@ -600,6 +609,7 @@ static void vkd3d_spirv_build_op3v(struct vkd3d_spirv_stream *stream,
         const uint32_t *operands, unsigned int operand_count)
 {
     unsigned int i;
+    vkd3d_spirv_reserve_words(stream, 4 + operand_count);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 4 + operand_count));
     vkd3d_spirv_build_word(stream, operand0);
     vkd3d_spirv_build_word(stream, operand1);
@@ -712,6 +722,7 @@ static uint32_t vkd3d_spirv_build_op_tr2v(struct vkd3d_spirv_builder *builder,
 {
     uint32_t result_id = vkd3d_spirv_alloc_id(builder);
     unsigned int i;
+    vkd3d_spirv_reserve_words(stream, 5 + operand_count);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(op, 5 + operand_count));
     vkd3d_spirv_build_word(stream, result_type);
     vkd3d_spirv_build_word(stream, result_id);
@@ -775,6 +786,7 @@ static void vkd3d_spirv_build_op_extension(struct vkd3d_spirv_stream *stream,
         const char *name)
 {
     unsigned int name_size = vkd3d_spirv_string_word_count(name);
+    vkd3d_spirv_reserve_words(stream, 1 + name_size);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpExtension, 1 + name_size));
     vkd3d_spirv_build_string(stream, name, name_size);
 }
@@ -783,6 +795,7 @@ static void vkd3d_spirv_build_op_ext_inst_import(struct vkd3d_spirv_stream *stre
         uint32_t result_id, const char *name)
 {
     unsigned int name_size = vkd3d_spirv_string_word_count(name);
+    vkd3d_spirv_reserve_words(stream, 2 + name_size);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpExtInstImport, 2 + name_size));
     vkd3d_spirv_build_word(stream, result_id);
     vkd3d_spirv_build_string(stream, name, name_size);
@@ -807,6 +820,7 @@ static void vkd3d_spirv_build_op_entry_point(struct vkd3d_spirv_stream *stream,
         uint32_t *interface_list, unsigned int interface_size)
 {
     unsigned int i, name_size = vkd3d_spirv_string_word_count(name);
+    vkd3d_spirv_reserve_words(stream, 3 + name_size + interface_size);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpEntryPoint, 3 + name_size + interface_size));
     vkd3d_spirv_build_word(stream, model);
     vkd3d_spirv_build_word(stream, function_id);
@@ -835,6 +849,7 @@ static void vkd3d_spirv_build_op_name(struct vkd3d_spirv_builder *builder,
     va_end(args);

     name_size = vkd3d_spirv_string_word_count(name);
+    vkd3d_spirv_reserve_words(stream, 2 + name_size);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpName, 2 + name_size));
     vkd3d_spirv_build_word(stream, id);
     vkd3d_spirv_build_string(stream, name, name_size);
@@ -854,6 +869,7 @@ static void vkd3d_spirv_build_op_member_name(struct vkd3d_spirv_builder *builder
     va_end(args);

     name_size = vkd3d_spirv_string_word_count(name);
+    vkd3d_spirv_reserve_words(stream, 3 + name_size);
     vkd3d_spirv_build_word(stream, vkd3d_spirv_opcode_word(SpvOpMemberName, 3 + name_size));
     vkd3d_spirv_build_word(stream, type_id);
     vkd3d_spirv_build_word(stream, member);
@@ -1699,6 +1715,7 @@ static bool vkd3d_spirv_compile_module(struct vkd3d_spirv_builder *builder,

     vkd3d_spirv_stream_init(&stream);

+    vkd3d_spirv_reserve_words(&stream, 5);
     vkd3d_spirv_build_word(&stream, SpvMagicNumber);
     vkd3d_spirv_build_word(&stream, VKD3D_SPIRV_VERSION);
     vkd3d_spirv_build_word(&stream, VKD3D_SPIRV_GENERATOR_MAGIC);
--
2.23.0




More information about the wine-devel mailing list