Need Float16 in some derivative tests
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / spirv_assembly / vktSpvAsmInstructionTests.cpp
index 055d21c..bfa7d9e 100644 (file)
@@ -48,6 +48,7 @@
 #include "deStringUtil.hpp"
 #include "deUniquePtr.hpp"
 #include "deMath.h"
+#include "deRandom.hpp"
 #include "tcuStringTemplate.hpp"
 
 #include "vktSpvAsmCrossStageInterfaceTests.hpp"
@@ -72,6 +73,7 @@
 #include "vktSpvAsmVaryingNameTests.hpp"
 #include "vktSpvAsmWorkgroupMemoryTests.hpp"
 #include "vktSpvAsmSignedIntCompareTests.hpp"
+#include "vktSpvAsmPtrAccessChainTests.hpp"
 
 #include <cmath>
 #include <limits>
@@ -111,7 +113,7 @@ static void fillRandomScalars (de::Random& rnd, T minValue, T maxValue, void* ds
 {
        T* const typedPtr = (T*)dst;
        for (int ndx = 0; ndx < numValues; ndx++)
-               typedPtr[offset + ndx] = randomScalar<T>(rnd, minValue, maxValue);
+               typedPtr[offset + ndx] = de::randomScalar<T>(rnd, minValue, maxValue);
 }
 
 // Filter is a function that returns true if a value should pass, false otherwise.
@@ -123,7 +125,7 @@ static void fillRandomScalars (de::Random& rnd, T minValue, T maxValue, void* ds
        for (int ndx = 0; ndx < numValues; ndx++)
        {
                do
-                       value = randomScalar<T>(rnd, minValue, maxValue);
+                       value = de::randomScalar<T>(rnd, minValue, maxValue);
                while (!filter(value));
 
                typedPtr[offset + ndx] = value;
@@ -378,6 +380,128 @@ tcu::TestCaseGroup* createOpNopGroup (tcu::TestContext& testCtx)
        return group.release();
 }
 
+tcu::TestCaseGroup* createUnusedVariableComputeTests (tcu::TestContext& testCtx)
+{
+       de::MovePtr<tcu::TestCaseGroup> group                   (new tcu::TestCaseGroup(testCtx, "unused_variables", "Compute shaders with unused variables"));
+       de::Random                                              rnd                             (deStringHash(group->getName()));
+       const int                                               numElements             = 100;
+       vector<float>                                   positiveFloats  (numElements, 0);
+       vector<float>                                   negativeFloats  (numElements, 0);
+
+       fillRandomScalars(rnd, 1.f, 100.f, &positiveFloats[0], numElements);
+
+       for (size_t ndx = 0; ndx < numElements; ++ndx)
+               negativeFloats[ndx] = -positiveFloats[ndx];
+
+       const VariableLocation                  testLocations[] =
+       {
+               // Set          Binding
+               { 0,            5                       },
+               { 5,            5                       },
+       };
+
+       for (size_t locationNdx = 0; locationNdx < DE_LENGTH_OF_ARRAY(testLocations); ++locationNdx)
+       {
+               const VariableLocation& location = testLocations[locationNdx];
+
+               // Unused variable.
+               {
+                       ComputeShaderSpec                               spec;
+
+                       spec.assembly =
+                               string(getComputeAsmShaderPreamble()) +
+
+                               "OpDecorate %id BuiltIn GlobalInvocationId\n"
+
+                               + getUnusedDecorations(location)
+
+                               + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes())
+
+                               + getUnusedTypesAndConstants()
+
+                               + string(getComputeAsmInputOutputBuffer())
+
+                               + getUnusedBuffer() +
+
+                               "%id        = OpVariable %uvec3ptr Input\n"
+                               "%zero      = OpConstant %i32 0\n"
+
+                               "%main      = OpFunction %void None %voidf\n"
+                               "%label     = OpLabel\n"
+                               "%idval     = OpLoad %uvec3 %id\n"
+                               "%x         = OpCompositeExtract %u32 %idval 0\n"
+
+                               "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
+                               "%inval     = OpLoad %f32 %inloc\n"
+                               "%neg       = OpFNegate %f32 %inval\n"
+                               "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
+                               "             OpStore %outloc %neg\n"
+                               "             OpReturn\n"
+                               "             OpFunctionEnd\n";
+                       spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
+                       spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
+                       spec.numWorkGroups = IVec3(numElements, 1, 1);
+
+                       std::string testName            = "variable_" + location.toString();
+                       std::string testDescription     = "Unused variable test with " + location.toDescription();
+
+                       group->addChild(new SpvAsmComputeShaderCase(testCtx, testName.c_str(), testDescription.c_str(), spec));
+               }
+
+               // Unused function.
+               {
+                       ComputeShaderSpec                               spec;
+
+                       spec.assembly =
+                               string(getComputeAsmShaderPreamble("", "", "", getUnusedEntryPoint())) +
+
+                               "OpDecorate %id BuiltIn GlobalInvocationId\n"
+
+                               + getUnusedDecorations(location)
+
+                               + string(getComputeAsmInputOutputBufferTraits()) + string(getComputeAsmCommonTypes())
+
+                               + getUnusedTypesAndConstants() +
+
+                               "%c_i32_0 = OpConstant %i32 0\n"
+                               "%c_i32_1 = OpConstant %i32 1\n"
+
+                               + string(getComputeAsmInputOutputBuffer())
+
+                               + getUnusedBuffer() +
+
+                               "%id        = OpVariable %uvec3ptr Input\n"
+                               "%zero      = OpConstant %i32 0\n"
+
+                               "%main      = OpFunction %void None %voidf\n"
+                               "%label     = OpLabel\n"
+                               "%idval     = OpLoad %uvec3 %id\n"
+                               "%x         = OpCompositeExtract %u32 %idval 0\n"
+
+                               "%inloc     = OpAccessChain %f32ptr %indata %zero %x\n"
+                               "%inval     = OpLoad %f32 %inloc\n"
+                               "%neg       = OpFNegate %f32 %inval\n"
+                               "%outloc    = OpAccessChain %f32ptr %outdata %zero %x\n"
+                               "             OpStore %outloc %neg\n"
+                               "             OpReturn\n"
+                               "             OpFunctionEnd\n"
+
+                               + getUnusedFunctionBody();
+
+                       spec.inputs.push_back(BufferSp(new Float32Buffer(positiveFloats)));
+                       spec.outputs.push_back(BufferSp(new Float32Buffer(negativeFloats)));
+                       spec.numWorkGroups = IVec3(numElements, 1, 1);
+
+                       std::string testName            = "function_" + location.toString();
+                       std::string testDescription     = "Unused function test with " + location.toDescription();
+
+                       group->addChild(new SpvAsmComputeShaderCase(testCtx, testName.c_str(), testDescription.c_str(), spec));
+               }
+       }
+
+       return group.release();
+}
+
 template<bool nanSupported>
 bool compareFUnord (const std::vector<Resource>& inputs, const vector<AllocationSp>& outputAllocs, const std::vector<Resource>& expectedOutputs, TestLog& log)
 {
@@ -3017,9 +3141,9 @@ void createOpPhiVartypeTests (de::MovePtr<tcu::TestCaseGroup>& group, tcu::TestC
 
                "%id       = OpVariable %uvec3ptr Input\n"
                "%zero     = OpConstant %i32 0\n"
-               "%float_0  = OpConstant %f16 0.0\n"
-               "%float_1  = OpConstant %f16 1.0\n"
-               "%float_n1 = OpConstant %f16 -1.0\n"
+               "%float_0  = OpConstant %f32 0.0\n"
+               "%float_1  = OpConstant %f32 1.0\n"
+               "%float_n1 = OpConstant %f32 -1.0\n"
 
                "%main     = OpFunction %void None %voidf\n"
                "%entry    = OpLabel\n"
@@ -3027,8 +3151,9 @@ void createOpPhiVartypeTests (de::MovePtr<tcu::TestCaseGroup>& group, tcu::TestC
                "%x        = OpCompositeExtract %u32 %idval 0\n"
                "%inloc    = OpAccessChain %f16ptr %indata %zero %x\n"
                "%inval    = OpLoad %f16 %inloc\n"
+               "%f32_inval = OpFConvert %f32 %inval\n"
 
-               "%comp     = OpFOrdGreaterThan %bool %inval %float_0\n"
+               "%comp     = OpFOrdGreaterThan %bool %f32_inval %float_0\n"
                "            OpSelectionMerge %cm None\n"
                "            OpBranchConditional %comp %tb %fb\n"
                "%tb       = OpLabel\n"
@@ -3036,10 +3161,11 @@ void createOpPhiVartypeTests (de::MovePtr<tcu::TestCaseGroup>& group, tcu::TestC
                "%fb       = OpLabel\n"
                "            OpBranch %cm\n"
                "%cm       = OpLabel\n"
-               "%res      = OpPhi %f16 %float_1 %tb %float_n1 %fb\n"
+               "%res      = OpPhi %f32 %float_1 %tb %float_n1 %fb\n"
+               "%f16_res  = OpFConvert %f16 %res\n"
 
                "%outloc   = OpAccessChain %f16ptr %outdata %zero %x\n"
-               "            OpStore %outloc %res\n"
+               "            OpStore %outloc %f16_res\n"
                "            OpReturn\n"
 
                "            OpFunctionEnd\n";
@@ -5125,7 +5251,6 @@ tcu::TestCaseGroup* createLoopControlGroup (tcu::TestContext& testCtx)
        cases.push_back(CaseParameter("none",                           "None"));
        cases.push_back(CaseParameter("unroll",                         "Unroll"));
        cases.push_back(CaseParameter("dont_unroll",            "DontUnroll"));
-       cases.push_back(CaseParameter("unroll_dont_unroll",     "Unroll|DontUnroll"));
 
        fillRandomScalars(rnd, -100.f, 100.f, &inputFloats[0], numElements);
 
@@ -8197,6 +8322,93 @@ tcu::TestCaseGroup* createModuleTests(tcu::TestContext& testCtx)
        return moduleTests.release();
 }
 
+std::string getUnusedVarTestNamePiece(const std::string& prefix, ShaderTask task)
+{
+       switch (task)
+       {
+               case SHADER_TASK_NONE:                  return "";
+               case SHADER_TASK_NORMAL:                return prefix + "_normal";
+               case SHADER_TASK_UNUSED_VAR:    return prefix + "_unused_var";
+               case SHADER_TASK_UNUSED_FUNC:   return prefix + "_unused_func";
+               default:                                                DE_ASSERT(DE_FALSE);
+       }
+       // unreachable
+       return "";
+}
+
+std::string getShaderTaskIndexName(ShaderTaskIndex index)
+{
+       switch (index)
+       {
+       case SHADER_TASK_INDEX_VERTEX:                  return "vertex";
+       case SHADER_TASK_INDEX_GEOMETRY:                return "geom";
+       case SHADER_TASK_INDEX_TESS_CONTROL:    return "tessc";
+       case SHADER_TASK_INDEX_TESS_EVAL:               return "tesse";
+       case SHADER_TASK_INDEX_FRAGMENT:                return "frag";
+       default:                                                                DE_ASSERT(DE_FALSE);
+       }
+       // unreachable
+       return "";
+}
+
+std::string getUnusedVarTestName(const ShaderTaskArray& shaderTasks, const VariableLocation& location)
+{
+       std::string testName = location.toString();
+
+       for (size_t i = 0; i < DE_LENGTH_OF_ARRAY(shaderTasks); ++i)
+       {
+               if (shaderTasks[i] != SHADER_TASK_NONE)
+               {
+                       testName += "_" + getUnusedVarTestNamePiece(getShaderTaskIndexName((ShaderTaskIndex)i), shaderTasks[i]);
+               }
+       }
+
+       return testName;
+}
+
+tcu::TestCaseGroup* createUnusedVariableTests(tcu::TestContext& testCtx)
+{
+       de::MovePtr<tcu::TestCaseGroup>         moduleTests                             (new tcu::TestCaseGroup(testCtx, "unused_variables", "Graphics shaders with unused variables"));
+
+       ShaderTaskArray                                         shaderCombinations[]    =
+       {
+               // Vertex                                       Geometry                                        Tess. Control                           Tess. Evaluation                        Fragment
+               { SHADER_TASK_UNUSED_VAR,       SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NORMAL      },
+               { SHADER_TASK_UNUSED_FUNC,      SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NORMAL      },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_UNUSED_VAR  },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_UNUSED_FUNC },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_UNUSED_VAR,         SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NORMAL      },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_UNUSED_FUNC,        SHADER_TASK_NONE,                       SHADER_TASK_NONE,                       SHADER_TASK_NORMAL      },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_NONE,                       SHADER_TASK_UNUSED_VAR,         SHADER_TASK_NORMAL,                     SHADER_TASK_NORMAL      },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_NONE,                       SHADER_TASK_UNUSED_FUNC,        SHADER_TASK_NORMAL,                     SHADER_TASK_NORMAL      },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_NONE,                       SHADER_TASK_NORMAL,                     SHADER_TASK_UNUSED_VAR,         SHADER_TASK_NORMAL      },
+               { SHADER_TASK_NORMAL,           SHADER_TASK_NONE,                       SHADER_TASK_NORMAL,                     SHADER_TASK_UNUSED_FUNC,        SHADER_TASK_NORMAL      }
+       };
+
+       const VariableLocation                          testLocations[] =
+       {
+               // Set          Binding
+               { 0,            5                       },
+               { 5,            5                       },
+       };
+
+       for (size_t combNdx = 0; combNdx < DE_LENGTH_OF_ARRAY(shaderCombinations); ++combNdx)
+       {
+               for (size_t locationNdx = 0; locationNdx < DE_LENGTH_OF_ARRAY(testLocations); ++locationNdx)
+               {
+                       const ShaderTaskArray&  shaderTasks             = shaderCombinations[combNdx];
+                       const VariableLocation& location                = testLocations[locationNdx];
+                       std::string                             testName                = getUnusedVarTestName(shaderTasks, location);
+
+                       addFunctionCaseWithPrograms<UnusedVariableContext>(
+                               moduleTests.get(), testName, "", createUnusedVariableModules, runAndVerifyUnusedVariablePipeline,
+                               createUnusedVariableContext(shaderTasks, location));
+               }
+       }
+
+       return moduleTests.release();
+}
+
 tcu::TestCaseGroup* createLoopTests(tcu::TestContext& testCtx)
 {
        de::MovePtr<tcu::TestCaseGroup> testGroup(new tcu::TestCaseGroup(testCtx, "loop", "Looping control flow"));
@@ -9190,7 +9402,7 @@ struct ConvertCase
 
                        if (instruction == "OpUConvert" || instruction == "OpSConvert" || instruction == "OpFConvert")
                        {
-                               // Conversions between 16 and 32 bit are provided by SPV_KHR_16bit_storage. The rest requires explicit Int16
+                               // Width-only conversions between 16 and 32 bit are provided by SPV_KHR_16bit_storage. The rest requires explicit Int16
                                if (usesInt32(from, to) || usesFloat32(from, to))
                                        requiresInt16Capability = false;
                        }
@@ -9208,8 +9420,8 @@ struct ConvertCase
                {
                        decl += "%f16        = OpTypeFloat 16\n";
 
-                       // Conversions between 16 and 32 bit are provided by SPV_KHR_16bit_storage. The rest requires explicit Float16
-                       if (!(usesInt32(from, to) || usesFloat32(from, to)))
+                       // Width-only conversions between 16 and 32 bit are provided by SPV_KHR_16bit_storage. The rest requires explicit Float16
+                       if (!usesFloat32(from, to))
                                caps += "OpCapability Float16\n";
                }
 
@@ -10755,7 +10967,7 @@ tcu::TestCaseGroup* createDerivativeTests (tcu::TestContext& testCtx)
                specs["type_decls"]                     = testType.typeDecls;
 
                fragments["extension"]          = "OpExtension \"SPV_KHR_16bit_storage\"";
-               fragments["capability"]         = "OpCapability DerivativeControl\nOpCapability StorageUniformBufferBlock16\n";
+               fragments["capability"]         = "OpCapability DerivativeControl\nOpCapability StorageUniformBufferBlock16\nOpCapability Float16\n";
                fragments["decoration"]         = decoration.specialize(specs);
                fragments["pre_main"]           = preMain.specialize(specs);
                fragments["testfun"]            = testFun.specialize(specs);
@@ -13747,6 +13959,12 @@ struct fp16SmoothStep : public fp16PerComponent
 
 struct fp16Fma : public fp16PerComponent
 {
+       fp16Fma()
+       {
+               flavorNames.push_back("DoubleCalc");
+               flavorNames.push_back("EmulatingFP16");
+       }
+
        virtual double getULPs(vector<const deFloat16*>& in)
        {
                DE_UNREF(in);
@@ -13766,10 +13984,30 @@ struct fp16Fma : public fp16PerComponent
                const fp16type  a               (*in[0]);
                const fp16type  b               (*in[1]);
                const fp16type  c               (*in[2]);
-               const double    ad              (a.asDouble());
-               const double    bd              (b.asDouble());
-               const double    cd              (c.asDouble());
-               const double    result  (deMadd(ad, bd, cd));
+               double                  result  (0.0);
+
+               if (getFlavor() == 0)
+               {
+                       const double    ad      (a.asDouble());
+                       const double    bd      (b.asDouble());
+                       const double    cd      (c.asDouble());
+
+                       result  = deMadd(ad, bd, cd);
+               }
+               else if (getFlavor() == 1)
+               {
+                       const double    ad      (a.asDouble());
+                       const double    bd      (b.asDouble());
+                       const double    cd      (c.asDouble());
+                       const fp16type  ab      (ad * bd);
+                       const fp16type  r       (ab.asDouble() + cd);
+
+                       result  = r.asDouble();
+               }
+               else
+               {
+                       TCU_THROW(InternalError, "Unknown flavor");
+               }
 
                out[0] = fp16type(result).bits();
                min[0] = getMin(result, getULPs(in));
@@ -18423,6 +18661,8 @@ tcu::TestCaseGroup* createInstructionTests (tcu::TestContext& testCtx)
        computeTests->addChild(createWorkgroupMemoryComputeGroup(testCtx));
        computeTests->addChild(createSpirvIdsAbuseGroup(testCtx));
        computeTests->addChild(createSignedIntCompareGroup(testCtx));
+       computeTests->addChild(createUnusedVariableComputeTests(testCtx));
+       computeTests->addChild(createPtrAccessChainGroup(testCtx));
 
        graphicsTests->addChild(createCrossStageInterfaceTests(testCtx));
        graphicsTests->addChild(createSpivVersionCheckTests(testCtx, !testComputePipeline));
@@ -18438,6 +18678,7 @@ tcu::TestCaseGroup* createInstructionTests (tcu::TestContext& testCtx)
        graphicsTests->addChild(createOpUndefTests(testCtx));
        graphicsTests->addChild(createSelectionBlockOrderTests(testCtx));
        graphicsTests->addChild(createModuleTests(testCtx));
+       graphicsTests->addChild(createUnusedVariableTests(testCtx));
        graphicsTests->addChild(createSwitchBlockOrderTests(testCtx));
        graphicsTests->addChild(createOpPhiTests(testCtx));
        graphicsTests->addChild(createNoContractionTests(testCtx));