From 4e13c90944e9dfdc6b429054092b94666f06fcb2 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Mon, 13 Jul 2020 00:35:58 -0600 Subject: [PATCH] Fix #2329: don't use invalid initializers. --- SPIRV/GlslangToSpv.cpp | 2 +- SPIRV/SpvBuilder.cpp | 2 +- SPIRV/SpvBuilder.h | 7 + Test/baseResults/spv.ext.World3x4.rahit.out | 145 ++++++++++++++++++++ Test/spv.ext.World3x4.rahit | 21 +++ gtests/Spv.FromFile.cpp | 1 + 6 files changed, 176 insertions(+), 2 deletions(-) create mode 100755 Test/baseResults/spv.ext.World3x4.rahit.out create mode 100644 Test/spv.ext.World3x4.rahit diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp index c3b48d6c..ba60b82d 100644 --- a/SPIRV/GlslangToSpv.cpp +++ b/SPIRV/GlslangToSpv.cpp @@ -1724,7 +1724,7 @@ void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol) spv::StorageClass sc = builder.getStorageClass(id); // Before SPIR-V 1.4, we only want to include Input and Output. // Starting with SPIR-V 1.4, we want all globals. - if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) || + if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) || (sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) { iOSet.insert(id); } diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp index fba26ad7..151e9358 100644 --- a/SPIRV/SpvBuilder.cpp +++ b/SPIRV/SpvBuilder.cpp @@ -2722,7 +2722,7 @@ Id Builder::accessChainLoad(Decoration precision, Decoration nonUniform, Id resu setPrecision(id, precision); } else { Id lValue = NoResult; - if (spvVersion >= Spv_1_4) { + if (spvVersion >= Spv_1_4 && isValidInitializer(accessChain.base)) { // make a new function variable for this r-value, using an initializer, // and mark it as NonWritable so that downstream it can be detected as a lookup // table diff --git a/SPIRV/SpvBuilder.h b/SPIRV/SpvBuilder.h index ca126d3a..83a7116a 100644 --- a/SPIRV/SpvBuilder.h +++ b/SPIRV/SpvBuilder.h @@ -248,6 +248,13 @@ public: { return module.getInstruction(resultId)->getImmediateOperand(0); } StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); } + bool isVariableOpCode(Op opcode) const { return opcode == OpVariable; } + bool isVariable(Id resultId) const { return isVariableOpCode(getOpCode(resultId)); } + bool isGlobalStorage(Id resultId) const { return getStorageClass(resultId) != StorageClassFunction; } + bool isGlobalVariable(Id resultId) const { return isVariable(resultId) && isGlobalStorage(resultId); } + // See if a resultId is valid for use as an initializer. + bool isValidInitializer(Id resultId) const { return isConstant(resultId) || isGlobalVariable(resultId); } + int getScalarTypeWidth(Id typeId) const { Id scalarTypeId = getScalarTypeId(typeId); diff --git a/Test/baseResults/spv.ext.World3x4.rahit.out b/Test/baseResults/spv.ext.World3x4.rahit.out new file mode 100755 index 00000000..c689c4dd --- /dev/null +++ b/Test/baseResults/spv.ext.World3x4.rahit.out @@ -0,0 +1,145 @@ +spv.ext.World3x4.rahit +// Module Version 10400 +// Generated by (magic number): 8000a +// Id's are bound by 90 + + Capability RayTracingProvisionalKHR + Extension "SPV_KHR_ray_tracing" + 1: ExtInstImport "GLSL.std.450" + MemoryModel Logical GLSL450 + EntryPoint AnyHitKHR 4 "main" 43 60 78 87 89 + Source GLSL 460 + SourceExtension "GL_EXT_ray_tracing" + Name 4 "main" + Name 8 "k" + Name 12 "ndx" + Name 14 "col" + Name 25 "row" + Name 39 "p" + Name 43 "gl_LaunchIDEXT" + Name 53 "r" + Name 60 "gl_WorldToObject3x4EXT" + Name 66 "indexable" + Name 72 "c" + Name 78 "result" + Name 87 "attribs" + Name 89 "hitValue" + Decorate 43(gl_LaunchIDEXT) BuiltIn LaunchIdKHR + Decorate 60(gl_WorldToObject3x4EXT) BuiltIn WorldToObjectKHR + Decorate 78(result) DescriptorSet 0 + Decorate 78(result) Binding 0 + Decorate 89(hitValue) Location 0 + 2: TypeVoid + 3: TypeFunction 2 + 6: TypeFloat 32 + 7: TypePointer Function 6(float) + 9: 6(float) Constant 1233125376 + 10: TypeInt 32 1 + 11: TypePointer Function 10(int) + 13: 10(int) Constant 4294967295 + 15: 10(int) Constant 0 + 22: 10(int) Constant 3 + 23: TypeBool + 32: 10(int) Constant 4 + 35: 10(int) Constant 1 + 37: TypeVector 10(int) 3 + 38: TypePointer Function 37(ivec3) + 40: TypeInt 32 0 + 41: TypeVector 40(int) 3 + 42: TypePointer Input 41(ivec3) +43(gl_LaunchIDEXT): 42(ptr) Variable Input + 44: TypeVector 40(int) 2 + 47: TypeVector 10(int) 2 + 55: TypeVector 6(float) 4 + 56: TypeMatrix 55(fvec4) 3 + 57: TypeVector 6(float) 3 + 58: TypeMatrix 57(fvec3) 4 + 59: TypePointer Input 58 +60(gl_WorldToObject3x4EXT): 59(ptr) Variable Input + 65: TypePointer Function 56 + 70: TypeVector 10(int) 4 + 71: TypePointer Function 70(ivec4) + 76: TypeImage 10(int) 3D nonsampled format:R32i + 77: TypePointer UniformConstant 76 + 78(result): 77(ptr) Variable UniformConstant + 86: TypePointer HitAttributeKHR 57(fvec3) + 87(attribs): 86(ptr) Variable HitAttributeKHR + 88: TypePointer IncomingRayPayloadKHR 57(fvec3) + 89(hitValue): 88(ptr) Variable IncomingRayPayloadKHR + 4(main): 2 Function None 3 + 5: Label + 8(k): 7(ptr) Variable Function + 12(ndx): 11(ptr) Variable Function + 14(col): 11(ptr) Variable Function + 25(row): 11(ptr) Variable Function + 39(p): 38(ptr) Variable Function + 53(r): 7(ptr) Variable Function + 66(indexable): 65(ptr) Variable Function + 72(c): 71(ptr) Variable Function + Store 8(k) 9 + Store 12(ndx) 13 + Store 14(col) 15 + Branch 16 + 16: Label + LoopMerge 18 19 None + Branch 20 + 20: Label + 21: 10(int) Load 14(col) + 24: 23(bool) SLessThan 21 22 + BranchConditional 24 17 18 + 17: Label + Store 25(row) 15 + Branch 26 + 26: Label + LoopMerge 28 29 None + Branch 30 + 30: Label + 31: 10(int) Load 25(row) + 33: 23(bool) SLessThan 31 32 + BranchConditional 33 27 28 + 27: Label + 34: 10(int) Load 12(ndx) + 36: 10(int) IAdd 34 35 + Store 12(ndx) 36 + 45: 41(ivec3) Load 43(gl_LaunchIDEXT) + 46: 44(ivec2) VectorShuffle 45 45 0 1 + 48: 47(ivec2) Bitcast 46 + 49: 10(int) Load 12(ndx) + 50: 10(int) CompositeExtract 48 0 + 51: 10(int) CompositeExtract 48 1 + 52: 37(ivec3) CompositeConstruct 50 51 49 + Store 39(p) 52 + 54: 6(float) Load 8(k) + 61: 58 Load 60(gl_WorldToObject3x4EXT) + 62: 56 Transpose 61 + 63: 10(int) Load 14(col) + 64: 10(int) Load 25(row) + Store 66(indexable) 62 + 67: 7(ptr) AccessChain 66(indexable) 63 64 + 68: 6(float) Load 67 + 69: 6(float) FMul 54 68 + Store 53(r) 69 + 73: 6(float) Load 53(r) + 74: 10(int) ConvertFToS 73 + 75: 70(ivec4) CompositeConstruct 74 15 15 35 + Store 72(c) 75 + 79: 76 Load 78(result) + 80: 37(ivec3) Load 39(p) + 81: 70(ivec4) Load 72(c) + ImageWrite 79 80 81 SignExtend + Branch 29 + 29: Label + 82: 10(int) Load 25(row) + 83: 10(int) IAdd 82 35 + Store 25(row) 83 + Branch 26 + 28: Label + Branch 19 + 19: Label + 84: 10(int) Load 14(col) + 85: 10(int) IAdd 84 35 + Store 14(col) 85 + Branch 16 + 18: Label + Return + FunctionEnd diff --git a/Test/spv.ext.World3x4.rahit b/Test/spv.ext.World3x4.rahit new file mode 100644 index 00000000..4f541f82 --- /dev/null +++ b/Test/spv.ext.World3x4.rahit @@ -0,0 +1,21 @@ +#version 460 +#extension GL_EXT_ray_tracing : enable + +hitAttributeEXT vec3 attribs; +layout(location = 0) rayPayloadInEXT vec3 hitValue; +layout(set = 0, binding = 0, r32i) uniform iimage3D result; + +void main() +{ + float k = 1048576.0f; + int ndx = -1; + for (int col = 0; col < 3; col++) + for (int row = 0; row < 4; row++) + { + ndx++; + ivec3 p = ivec3(gl_LaunchIDEXT.xy, ndx); + float r = k * gl_WorldToObject3x4EXT[col][row]; + ivec4 c = ivec4(int(r),0,0,1); + imageStore(result, p, c); + } +} diff --git a/gtests/Spv.FromFile.cpp b/gtests/Spv.FromFile.cpp index 96342623..714ef3c5 100644 --- a/gtests/Spv.FromFile.cpp +++ b/gtests/Spv.FromFile.cpp @@ -560,6 +560,7 @@ INSTANTIATE_TEST_CASE_P( "spv.ext.RayGenShader.rgen", "spv.ext.RayGenShader11.rgen", "spv.ext.RayGenShaderArray.rgen", + "spv.ext.World3x4.rahit", })), FileNameAsCustomTestSuffix ); -- 2.34.1