-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathir.hpp
199 lines (170 loc) · 3.85 KB
/
ir.hpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright (c) 2019-2022 Hans-Kristian Arntzen for Valve Corporation
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "thread_local_allocator.hpp"
#include "spirv.hpp"
#include <assert.h>
#include <initializer_list>
#include <stdint.h>
// A simple IR representation which allows the CFGStructurizer to do some simple rewrites of blocks,
// PHI nodes in particular.
namespace dxil_spv
{
enum class MergeType
{
None,
Loop,
Selection
};
struct CFGNode;
struct MergeInfo
{
MergeType merge_type = MergeType::None;
CFGNode *merge_block = nullptr;
CFGNode *continue_block = nullptr;
spv::LoopControlMask loop_control_mask = spv::LoopControlMaskNone;
spv::SelectionControlMask selection_control_mask = spv::SelectionControlMaskNone;
};
struct IncomingValue
{
CFGNode *block = nullptr;
uint32_t id = 0;
};
struct PHI
{
uint32_t id = 0;
uint32_t type_id = 0;
bool relaxed = false;
Vector<IncomingValue> incoming;
};
struct IDArgument
{
explicit IDArgument(spv::Id id_)
: id(id_)
{
}
spv::Id id;
};
struct LiteralArgument
{
explicit LiteralArgument(uint32_t lit_)
: lit(lit_)
{
}
uint32_t lit;
};
struct Operation
{
enum
{
MaxArguments = 11
};
Operation() = default;
explicit Operation(spv::Op op_)
: op(op_)
{
}
Operation(spv::Op op_, spv::Id id_, spv::Id type_id_)
: op(op_)
, id(id_)
, type_id(type_id_)
{
}
void add_id(spv::Id arg)
{
assert(num_arguments < MaxArguments);
assert(arg != 0);
arguments[num_arguments++] = arg;
}
void add_ids(const std::initializer_list<spv::Id> &args)
{
for (auto &arg : args)
{
assert(arg != 0);
add_id(arg);
}
}
void add_literal(uint32_t lit)
{
assert(num_arguments < MaxArguments);
literal_mask |= 1u << num_arguments;
arguments[num_arguments++] = lit;
}
const spv::Id *begin() const
{
return arguments;
}
const spv::Id *end() const
{
return arguments + num_arguments;
}
uint8_t get_literal_mask() const
{
return literal_mask;
}
spv::Op op = spv::OpNop;
spv::Id id = 0;
spv::Id type_id = 0;
spv::Id arguments[MaxArguments];
unsigned num_arguments = 0;
uint8_t literal_mask = 0;
};
struct Terminator
{
enum class Type
{
Unreachable,
Branch,
Condition,
Switch,
Return,
Kill
};
uint32_t conditional_id = 0;
Type type = Type::Unreachable;
CFGNode *direct_block = nullptr;
CFGNode *true_block = nullptr;
CFGNode *false_block = nullptr;
struct Case
{
CFGNode *node = nullptr;
uint64_t global_order = 0;
uint32_t value = 0;
bool is_default = false;
};
Vector<Case> cases;
uint32_t return_value = 0;
bool force_unroll = false;
bool force_loop = false;
bool force_flatten = false;
bool force_branch = false;
};
struct IRBlock
{
Vector<PHI> phi;
Vector<Operation *> operations;
MergeInfo merge_info;
Terminator terminator;
};
} // namespace dxil_spv