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-specialize-target-switch.h"
#include "slang-capability.h"
#include "slang-compiler.h"
#include "slang-ir-dce.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"
namespace Slang
{
void specializeTargetSwitch(
TargetRequest* target,
IRGlobalValueWithCode* code,
DiagnosticSink* sink)
{
if (auto gen = as<IRGeneric>(code))
{
auto retVal = findGenericReturnVal(gen);
if (auto innerCode = as<IRGlobalValueWithCode>(retVal))
{
specializeTargetSwitch(target, innerCode, sink);
return;
}
}
bool changed = false;
for (auto block : code->getBlocks())
{
bool failedImplies = false;
if (auto targetSwitch = as<IRTargetSwitch>(block->getTerminator()))
{
bool isEqual;
CapabilitySet bestCapSet = CapabilitySet::makeInvalid();
IRBlock* targetBlock = nullptr;
CapabilitySet::ImpliesReturnFlags impliesReturnType =
CapabilitySet::ImpliesReturnFlags::NotImplied;
for (UInt i = 0; i < targetSwitch->getCaseCount(); i++)
{
auto cap = (CapabilityName)getIntVal(targetSwitch->getCaseValue(i));
if (target->getTargetCaps().isIncompatibleWith(cap))
continue;
CapabilitySet capSet;
if (cap == CapabilityName::Invalid) // `default` case
capSet = CapabilitySet::makeEmpty();
else
capSet = CapabilitySet(cap);
bool isBetterForTarget =
capSet.isBetterForTarget(bestCapSet, target->getTargetCaps(), isEqual);
if (isBetterForTarget)
{
impliesReturnType = target->getTargetCaps().atLeastOneSetImpliedInOther(capSet);
bool targetImpliesCapSet =
((int)impliesReturnType & (int)CapabilitySet::ImpliesReturnFlags::Implied ||
capSet.isEmpty());
if (targetImpliesCapSet)
{
// Now check if bestCapSet contains targetCaps. If it does not then this is
// an invalid target
targetBlock = targetSwitch->getCaseBlock(i);
bestCapSet = capSet;
}
else
failedImplies = true;
}
}
IRBuilder builder(targetSwitch);
builder.setInsertBefore(targetSwitch);
if (targetBlock)
{
builder.emitBranch(targetBlock);
}
else
{
// only error if we have the chance of setting a valid target switch, but did not
// due to incompatability within same `target` atom. Otherwise we will have an issue
// when we process a `__target_switch() { case metal: return; }` for glsl targets.
if (failedImplies)
sink->diagnose(
targetSwitch->sourceLoc,
Diagnostics::profileIncompatibleWithTargetSwitch,
target->getTargetCaps());
builder.emitMissingReturn();
}
targetSwitch->removeAndDeallocate();
changed = true;
}
}
if (changed)
{
// Remove unreachable blocks after specialization.
eliminateDeadCode(code);
}
}
void specializeTargetSwitch(TargetRequest* target, IRModule* module, DiagnosticSink* sink)
{
for (auto globalInst : module->getGlobalInsts())
{
if (auto code = as<IRGlobalValueWithCode>(globalInst))
{
specializeTargetSwitch(target, code, sink);
}
}
}
} // namespace Slang
|