blob: 6f749f24223f98dc495fa232c64620f1131e05ab (
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
|
#include "slang-ir-array-reg-to-mem.h"
#include "slang-ir.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
namespace Slang
{
bool eliminateArrayTypeParameters(IRFunc* func)
{
IRBuilder builder(func);
bool changed = false;
List<UInt> arrayParamIds;
UInt idx = 0;
List<IRParam*> paramWorkList;
for (auto param : func->getParams())
{
if (auto arrayType = as<IRArrayTypeBase>(param->getFullType()))
{
paramWorkList.add(param);
arrayParamIds.add(idx);
}
idx++;
}
for (auto param : paramWorkList)
{
// We have an array type parameter, so we need to replace it with a pointer to the array
// type.
//
// We will also need to insert a `load` instruction at the start of the function body
// to load the actual pointer value from the parameter.
//
if (auto arrayType = as<IRArrayTypeBase>(param->getFullType()))
{
changed = true;
builder.setInsertBefore(param);
auto ptrArrayType = builder.getPtrType(arrayType);
auto newParam = builder.emitParam(ptrArrayType);
setInsertAfterOrdinaryInst(&builder, param);
auto regVal = builder.emitLoad(newParam);
param->replaceUsesWith(regVal);
param->removeAndDeallocate();
}
}
if (changed)
{
// The function is modified, we need to also update its type.
List<IRType*> paramTypes;
for (auto param : func->getParams())
{
paramTypes.add(param->getFullType());
}
auto newFuncType = builder.getFuncType((UInt)paramTypes.getCount(), paramTypes.getBuffer(), func->getResultType());
func->setFullType(newFuncType);
// Update all the call sites to pass the arrays by pointer.
traverseUses(func, [&](IRUse* use)
{
if (const auto call = as<IRCall>(use->getUser()))
{
builder.setInsertBefore(call);
for (auto paramId : arrayParamIds)
{
auto arg = call->getArg(paramId);
auto var = builder.emitVar(as<IRPtrTypeBase>(paramTypes[paramId])->getValueType());
builder.emitStore(var, arg);
call->setArg(paramId, var);
}
}
});
}
return changed;
}
bool eliminateArrayTypeSSARegisters(IRModule* module)
{
bool changed = false;
for (auto inst : module->getGlobalInsts())
{
if (auto func = as<IRFunc>(inst))
{
changed |= eliminateArrayTypeParameters(func);
}
}
return changed;
}
}
|