From ee21fc9081de8b54f5d1199325598c71eead1ef6 Mon Sep 17 00:00:00 2001
From: John Kessenich <cepheus@frii.com>
Date: Mon, 21 Sep 2015 21:50:29 -0600
Subject: [PATCH] SPV: Fix missing 'Member' operand to OpArrayLength.

---
 SPIRV/GlslangToSpv.cpp            |  5 +++--
 SPIRV/SpvBuilder.cpp              | 10 ++++++++++
 SPIRV/SpvBuilder.h                |  3 +++
 Test/baseResults/spv.310.comp.out |  2 +-
 glslang/Include/revision.h        |  2 +-
 5 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp
index 86e6ac3a8..90cb2c0a5 100755
--- a/SPIRV/GlslangToSpv.cpp
+++ b/SPIRV/GlslangToSpv.cpp
@@ -741,11 +741,12 @@ bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit /* visit */, glslang::TI
 
         // Normal .length() would have been constant folded by the front-end.
         // So, this has to be block.lastMember.length().
-        // SPV wants "block" as the operand, go get it.
+        // SPV wants "block" and member number as the operands, go get them.
         assert(node->getOperand()->getType().isRuntimeSizedArray());
         glslang::TIntermTyped* block = node->getOperand()->getAsBinaryNode()->getLeft();
         block->traverse(this);
-        spv::Id length = builder.createUnaryOp(spv::OpArrayLength, builder.makeIntType(32), builder.accessChainGetLValue());
+        unsigned int member = node->getOperand()->getAsBinaryNode()->getRight()->getAsConstantUnion()->getConstArray()[0].getUConst();
+        spv::Id length = builder.createArrayLength(builder.accessChainGetLValue(), member);
 
         builder.clearAccessChain();
         builder.setAccessChainRValue(length);
diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp
index 9d8288a2c..8b209b623 100755
--- a/SPIRV/SpvBuilder.cpp
+++ b/SPIRV/SpvBuilder.cpp
@@ -890,6 +890,16 @@ Id Builder::createAccessChain(StorageClass storageClass, Id base, std::vector<Id
     return chain->getResultId();
 }
 
+Id Builder::createArrayLength(Id base, unsigned int member)
+{
+    Instruction* length = new Instruction(getUniqueId(), makeIntType(32), OpArrayLength);
+    length->addIdOperand(base);
+    length->addImmediateOperand(member);
+    buildPoint->addInstruction(length);
+
+    return length->getResultId();
+}
+
 Id Builder::createCompositeExtract(Id composite, Id typeId, unsigned index)
 {
     Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
diff --git a/SPIRV/SpvBuilder.h b/SPIRV/SpvBuilder.h
index 23b9bc2c0..1a6be6ad8 100755
--- a/SPIRV/SpvBuilder.h
+++ b/SPIRV/SpvBuilder.h
@@ -225,6 +225,9 @@ public:
     // Create an OpAccessChain instruction
     Id createAccessChain(StorageClass, Id base, std::vector<Id>& offsets);
 
+    // Create an OpArrayLength instruction
+    Id createArrayLength(Id base, unsigned int member);
+
     // Create an OpCompositeExtract instruction
     Id createCompositeExtract(Id composite, Id typeId, unsigned index);
     Id createCompositeExtract(Id composite, Id typeId, std::vector<unsigned>& indexes);
diff --git a/Test/baseResults/spv.310.comp.out b/Test/baseResults/spv.310.comp.out
index 9a6a10c74..93dba9762 100644
--- a/Test/baseResults/spv.310.comp.out
+++ b/Test/baseResults/spv.310.comp.out
@@ -113,7 +113,7 @@ Linked compute stage:
               58:   23(fvec4) CompositeConstruct 57 57 57 57
               59:     30(ptr) AccessChain 51(outnames) 27 56
                               Store 59 58
-              60:     16(int) ArrayLength 15(outbname)
+              60:     16(int) ArrayLength 15(outbname) 3
               62:     61(ptr) AccessChain 51(outnames) 17
                               Store 62 60
                               Return
diff --git a/glslang/Include/revision.h b/glslang/Include/revision.h
index f4a5a4c92..3a82635b6 100644
--- a/glslang/Include/revision.h
+++ b/glslang/Include/revision.h
@@ -2,5 +2,5 @@
 // For the version, it uses the latest git tag followed by the number of commits.
 // For the date, it uses the current date (when then script is run).
 
-#define GLSLANG_REVISION "3.0.774"
+#define GLSLANG_REVISION "3.0.775"
 #define GLSLANG_DATE "21-Sep-2015"
-- 
GitLab