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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#include "slang-ir-layout-on-types.h"
#include "slang-ir-insts.h"
#include "slang-ir.h"
namespace Slang
{
struct TypeTypeLayout
{
IRType* type;
IRTypeLayout* layout;
};
template<typename Struct, typename StructuredBuffer, typename Array, typename Base>
static void zipPreorderTypeAndTypeLayout(
Struct struct_,
StructuredBuffer structuredBuffer,
Array array,
Base base,
IRType* type,
IRTypeLayout* layout)
{
auto go = [&](auto& go, IRType* type, IRLayout* layout) -> void{
if(const auto structTypeLayout = as<IRStructTypeLayout>(layout))
{
const auto structType = as<IRStructType>(type);
SLANG_ASSERT(structType);
struct_(structType, structTypeLayout);
Index i = 0;
for(const auto field : structType->getFields())
{
const auto fieldVarLayout = structTypeLayout->getFieldLayout(i);
const auto fieldTypeLayout = fieldVarLayout->getTypeLayout();
const auto fieldType = field->getFieldType();
go(go, fieldType, fieldTypeLayout);
++i;
}
}
else if(const auto structuredBufferTypeLayout = as<IRStructuredBufferTypeLayout>(layout))
{
const auto structuredBufferType = as<IRHLSLStructuredBufferTypeBase>(type);
SLANG_ASSERT(structuredBufferType);
structuredBuffer(structuredBufferType, structuredBufferTypeLayout);
go(go, structuredBufferType->getElementType(), structuredBufferTypeLayout->getElementTypeLayout());
}
else if(const auto arrayTypeLayout = as<IRArrayTypeLayout>(layout))
{
const auto arrayType = as<IRArrayTypeBase>(type);
SLANG_ASSERT(arrayType);
array(arrayType , arrayTypeLayout);
go(go, arrayType->getElementType(), arrayTypeLayout->getElementTypeLayout());
}
else if(const auto existentialTypeLayout = as<IRExistentialTypeLayout>(layout))
{
// TODO: To resolve this we should
// - Record the interface width in each IRExistentialTypeLayout
// - To allow us to replace them with IRTupleTypeLayout when we lower existentials
// - To allow us to replace them with IRStructTypeLayout when we lower tuples
SLANG_UNEXPECTED("Existentials (or their layouts) have not been erased before SPIR-V emit");
}
else
{
base((IRType*)type, (IRTypeLayout*)layout);
}
};
go(go, type, layout);
}
static Dictionary<IRType*, IRTypeLayout*> associateTypesWithLayouts(IRModule* module)
{
List<TypeTypeLayout> worklist;
for(auto globalInst : module->getGlobalInsts())
{
if(const auto varLayout = as<IRVarLayout>(globalInst))
{
auto typeLayout = varLayout->getTypeLayout();
List<IRInst*> vars;
traverseUsers(varLayout, [&](IRInst* varLayoutUser){
if(const auto dec = as<IRLayoutDecoration>(varLayoutUser))
{
if(const auto globalParam = as<IRGlobalParam>(dec->getParent()))
vars.add(globalParam);
else
{
// todo
}
}
else if(as<IREntryPointLayout>(varLayoutUser))
{
// todo
}
else if(as<IRStructFieldLayoutAttr>(varLayoutUser))
{
// todo
}
else if(as<IRParameterGroupTypeLayout>(varLayoutUser))
{
// todo
}
else
SLANG_UNEXPECTED("Var layout was used somewhere unexpected");
});
if(vars.getCount() == 1)
{
auto type = vars[0]->getDataType();
worklist.add({type, typeLayout});
}
else if(vars.getCount() > 2)
{
SLANG_UNIMPLEMENTED_X("vars with different layouts");
}
}
}
Dictionary<IRType*, IRTypeLayout*> ret;
while(!worklist.getCount() == 0)
{
const auto ttl = worklist.getLast();
worklist.removeLast();
const auto add = [&](IRType* type, IRTypeLayout* typeLayout){
const auto* otherTypeLayout = ret.tryGetValueOrAdd(type, typeLayout);
if(otherTypeLayout)
SLANG_ASSERT(*otherTypeLayout == typeLayout);
};
zipPreorderTypeAndTypeLayout(add, add, add, add, ttl.type, ttl.layout);
}
return ret;
}
static void decorateTypesWithLayouts(IRModule* module, const Dictionary<IRType*, IRTypeLayout*>& assocs)
{
IRBuilder builder(module);
for(const auto& [type, layout] : assocs)
{
builder.setInsertBefore(type);
// TODO: types with more than one decoration (needs deduplicating)
builder.addLayoutDecoration(type, layout);
}
}
void placeTypeLayoutsOnTypes(IRModule* module, CodeGenContext* codeGenContext)
{
SLANG_ASSERT(module);
SLANG_ASSERT(codeGenContext);
const auto assocs = associateTypesWithLayouts(module);
decorateTypesWithLayouts(module, assocs);
}
}
|