summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-ir-specialize-arrays.cpp
blob: 1f50a7579897e1ea3d60f78816fb04d427721a7a (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
// 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 unsized array parameter called with a sized-array argument.
    //

    bool doesParamWantSpecialization(IRParam* param, IRInst* arg, IRCall* callInst)
    {
        SLANG_UNUSED(param);
        SLANG_UNUSED(arg);
        SLANG_UNUSED(callInst);
        return false;
    }

    bool doesParamTypeWantSpecialization(IRParam* param, IRInst* arg)
    {
        auto paramType = param->getDataType();
        auto argType = arg->getDataType();
        if (auto outTypeBase = as<IROutParamTypeBase>(paramType))
        {
            paramType = outTypeBase->getValueType();
            IRBuilder builder(paramType);
            argType = tryGetPointedToType(&builder, argType);
            SLANG_ASSERT(argType);
        }
        else if (auto refType = as<IRRefParamType>(paramType))
        {
            paramType = refType->getValueType();
            IRBuilder builder(paramType);
            argType = tryGetPointedToType(&builder, argType);
            SLANG_ASSERT(argType);
        }
        else if (auto constRefType = as<IRBorrowInParamType>(paramType))
        {
            paramType = constRefType->getValueType();
            IRBuilder builder(paramType);
            argType = tryGetPointedToType(&builder, argType);
            SLANG_ASSERT(argType);
        }
        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