summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-specialize-arrays.cpp
blob: 4a4a72ee95fd40a94a84d13c4a22f442ac2d38b6 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// slang-ir-specialize-arrays.cpp
#include "slang-ir-specialize-arrays.h"

#include "slang-ir-insts.h"
#include "slang-ir-specialize-function-call.h"
#include "slang-ir.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.
    // It will also specialize functions with unsized array parameters into
    // sized arrays, if the function is called with an argument that has a
    // sized array type.
    //
    bool isStructTypeWithArray(IRType* type)
    {
        if (auto structType = as<IRStructType>(type))
        {
            for (auto field : structType->getFields())
            {
                if (const 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);
        if (isKhronosTarget(codeGenContext->getTargetReq()))
            return isStructTypeWithArray(param->getDataType());
        return false;
    }

    bool doesParamTypeWantSpecialization(IRParam* param, IRInst* arg)
    {
        auto paramType = param->getDataType();
        auto argType = arg->getDataType();
        if (auto outTypeBase = as<IROutTypeBase>(paramType))
        {
            paramType = outTypeBase->getValueType();
            SLANG_ASSERT(as<IRPtrTypeBase>(argType));
            argType = as<IRPtrTypeBase>(argType)->getValueType();
        }
        else if (auto refType = as<IRRefType>(paramType))
        {
            paramType = refType->getValueType();
            SLANG_ASSERT(as<IRPtrTypeBase>(argType));
            argType = as<IRPtrTypeBase>(argType)->getValueType();
        }
        else if (auto constRefType = as<IRConstRefType>(paramType))
        {
            paramType = constRefType->getValueType();
            SLANG_ASSERT(as<IRPtrTypeBase>(argType));
            argType = as<IRPtrTypeBase>(argType)->getValueType();
        }
        auto arrayType = as<IRUnsizedArrayType>(paramType);
        if (!arrayType)
            return false;
        auto argArrayType = as<IRArrayType>(argType);
        if (!argArrayType)
            return false;
        if (as<IRIntLit>(argArrayType->getElementCount()))
        {
            return true;
        }
        return false;
    }

    CodeGenContext* codeGenContext = nullptr;
};

void specializeArrayParameters(CodeGenContext* codeGenContext, IRModule* module)
{
    ArrayParameterSpecializationCondition condition;
    condition.codeGenContext = codeGenContext;
    specializeFunctionCalls(codeGenContext, module, &condition);
}

} // namespace Slang