summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-specialize-arrays.cpp
blob: 2ed3da47964f414f9a0fc5688d5c957540e631ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// slang-ir-specialize-arrays.cpp
#include "slang-ir-specialize-arrays.h"

#include "slang-ir-specialize-function-call.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"

namespace Slang
{

struct ArrayParameterSpecializationCondition : FunctionCallSpecializeCondition
{
    // This pass is intended to specialize functions
    // with struct parameters that has array fields
    // to avoid performance problems for GLSL targets.

    // Returns true if `type` is an `IRStructType` with array-typed fields.
    bool isStructTypeWithArray(IRType* type)
    {
        if (auto structType = as<IRStructType>(type))
        {
            for (auto field : structType->getFields())
            {
                if (auto arrayType = as<IRArrayType>(field->getFieldType()))
                {
                    return true;
                }
                if (auto subStructType = as<IRStructType>(field->getFieldType()))
                {
                    if (isStructTypeWithArray(subStructType))
                        return true;
                }
            }
        }
        return false;
    }

    bool doesParamWantSpecialization(IRParam* param, IRInst* arg)
    {
        SLANG_UNUSED(arg);
        return isStructTypeWithArray(param->getDataType());
    }
};

void specializeArrayParameters(
    BackEndCompileRequest* compileRequest,
    TargetRequest*  targetRequest,
    IRModule*       module)
{
    ArrayParameterSpecializationCondition condition;
    specializeFunctionCalls(compileRequest, targetRequest, module, &condition);
}

} // namesapce Slang