blob: c0928a2ff4947d3cd4ae9a43a501ce81fb813e15 (
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
95
96
97
98
99
100
101
102
103
104
105
106
|
#include "slang-ir-fix-entrypoint-callsite.h"
#include "slang-ir-clone.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
namespace Slang
{
// If the entrypoint is called by some other function, we need to clone the
// entrypoint and replace the callsites to call the cloned entrypoint instead.
// This is because we will be modifying the signature of the entrypoint during
// entrypoint legalization to rewrite the way system values are passed in.
// By replacing the callsites to call the cloned entrypoint that act as ordinary
// functions, we will no longer need to worry about changing the callsites when we
// legalize the entry-points.
//
void fixEntryPointCallsites(IRFunc* entryPoint)
{
IRFunc* clonedEntryPointForCall = nullptr;
auto ensureClonedEntryPointForCall = [&]() -> IRFunc*
{
if (clonedEntryPointForCall)
return clonedEntryPointForCall;
IRCloneEnv cloneEnv;
IRBuilder builder(entryPoint);
builder.setInsertBefore(entryPoint);
clonedEntryPointForCall = (IRFunc*)cloneInst(&cloneEnv, &builder, entryPoint);
// Remove entrypoint and linkage decorations from the cloned callee.
List<IRInst*> decorsToRemove;
for (auto decor : clonedEntryPointForCall->getDecorations())
{
switch (decor->getOp())
{
case kIROp_ExportDecoration:
case kIROp_UserExternDecoration:
case kIROp_HLSLExportDecoration:
case kIROp_EntryPointDecoration:
case kIROp_LayoutDecoration:
case kIROp_NumThreadsDecoration:
case kIROp_ImportDecoration:
case kIROp_ExternCDecoration:
case kIROp_ExternCppDecoration:
decorsToRemove.add(decor);
break;
}
}
for (auto decor : decorsToRemove)
decor->removeAndDeallocate();
return clonedEntryPointForCall;
};
traverseUses(
entryPoint,
[&](IRUse* use)
{
auto user = use->getUser();
auto call = as<IRCall>(user);
if (!call)
return;
auto callee = ensureClonedEntryPointForCall();
call->setOperand(0, callee);
// Fix up argument types: if the callee entrypoint is expecting a constref
// and the caller is passing a value, we need to wrap the value in a temporary var
// and pass the temporary var.
//
// TODO(tfoley): Wait, what? The situation this code is trying to fix should
// never be allowed to occur in the first place. This code shouldn't be
// trying to defend against the bad input; instead we should be *fixing*
// the source of the problem.
//
auto funcType = as<IRFuncType>(callee->getDataType());
SLANG_ASSERT(funcType);
IRBuilder builder(call);
builder.setInsertBefore(call);
List<IRParam*> params;
for (auto param : callee->getParams())
params.add(param);
if ((UInt)params.getCount() != call->getArgCount())
return;
for (UInt i = 0; i < call->getArgCount(); i++)
{
auto paramType = params[i]->getDataType();
auto arg = call->getArg(i);
if (auto refType = as<IRBorrowInParamType>(paramType))
{
if (!as<IRPtrTypeBase>(arg->getDataType()))
{
auto tempVar = builder.emitVar(refType->getValueType());
builder.emitStore(tempVar, arg);
call->setArg(i, tempVar);
}
}
}
});
}
void fixEntryPointCallsites(IRModule* module)
{
for (auto globalInst : module->getGlobalInsts())
{
if (globalInst->findDecoration<IREntryPointDecoration>())
fixEntryPointCallsites((IRFunc*)globalInst);
}
}
} // namespace Slang
|