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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
--
-- This file contains most of the code generation logic for the ir instructions
--
-- Helper function
-- Find instruction data by struct name
local function findInstData(insts, struct_name)
local function search(tbl)
for _, i in ipairs(tbl) do
local key, value = next(i)
local inst_struct_name = value.struct_name or key
if inst_struct_name == struct_name then
return value
end
-- Recursively search nested instructions
local result = search(value)
if result then
return result
end
end
return nil
end
return search(insts)
end
-- Walk the instruction tree and call a callback for each instruction
local function walk_instructions(insts, callback, parent_struct)
local function walk_insts(tbl, parent)
for _, i in ipairs(tbl) do
local key, value = next(i)
-- Determine struct name
local struct_name = value.struct_name and value.struct_name or key
-- Call the callback
callback(key, value, struct_name, parent)
-- Recursively process nested instructions
walk_insts(value, struct_name)
end
end
-- Start walking from the top-level insts
if insts then
walk_insts(insts, "Inst")
end
end
-- The definitions for leaf instructions
local leafInst = function(name, args)
args = args or {}
local result = args.noIsaImpl and ""
or [[static bool isaImpl(IROp op)
{
return (kIROpMask_OpMask & op) == kIROp_]]
.. name
.. [[;
}
enum { kOp = kIROp_]]
.. name
.. [[ }; ]]
-- Add getter methods if operands are specified
if args.operands then
for i, operand in ipairs(args.operands) do
local operandName = operand[1]
local operandType = operand[2]
local getterName = "get" .. operandName:sub(1,1):upper() .. operandName:sub(2)
local returnType = "IRInst"
if operandType then
returnType = operandType
result = result .. "\n " .. returnType .. "* " .. getterName .. "() { return (" .. returnType .. "*)getOperand(" .. (i-1) .. "); }"
else
result = result .. "\n " .. returnType .. "* " .. getterName .. "() { return getOperand(" .. (i-1) .. "); }"
end
end
end
return result
end
-- The definitions for abstract instruction classes
local baseInst = function(name, args)
args = args or {}
local result = args.noIsaImpl and ""
or [[static bool isaImpl(IROp opIn)
{
const int op = (kIROpMask_OpMask & opIn);
return op >= kIROp_First]]
.. name
.. [[ && op <= kIROp_Last]]
.. name
.. [[;
}]]
-- Add getter methods if operands are specified
if args.operands then
for i, operand in ipairs(args.operands) do
local operandName = operand[1]
local operandType = operand[2]
local getterName = "get" .. operandName:sub(1,1):upper() .. operandName:sub(2)
local returnType = "IRInst"
if operandType then
returnType = operandType
result = result .. "\n " .. returnType .. "* " .. getterName .. "() { return (" .. returnType .. "*)getOperand(" .. (i-1) .. "); }"
else
result = result .. "\n " .. returnType .. "* " .. getterName .. "() { return getOperand(" .. (i-1) .. "); }"
end
end
end
return result
end
-- Generate struct definitions for instructions not defined by the user
local function allOtherInstStructs()
local insts = require("source/slang/slang-ir-insts.lua").insts
local output = {}
walk_instructions(insts, function(key, value, struct_name, parent_struct)
-- If this type already has a definition, skip it
if not Slang["IR" .. struct_name] then
-- Generate struct definition
table.insert(output, string.format("struct IR%s : IR%s", struct_name, parent_struct))
table.insert(output, "{")
table.insert(
output,
string.format(" %s", value.is_leaf and leafInst(struct_name) or baseInst(struct_name))
)
table.insert(output, "};")
table.insert(output, "")
end
end)
return table.concat(output, "\n")
end
-- Forward declarations for everything
local function instStructForwardDecls()
local insts = require("source/slang/slang-ir-insts.lua").insts
local output = {}
walk_instructions(insts, function(key, value, struct_name, parent_struct)
-- Generate struct definition
table.insert(output, string.format("struct IR%s;", struct_name))
end)
return table.concat(output, "\n")
end
-- The info table
local function instInfoEntries()
local insts = require("source/slang/slang-ir-insts.lua").insts
local output = {}
local function constructFlags(value)
local flags = {}
-- Map of field names to their IROpFlags names
local flagMap = {
hoistable = "kIROpFlag_Hoistable",
parent = "kIROpFlag_Parent",
global = "kIROpFlag_Global",
use_other = "kIROpFlag_UseOther",
}
-- Check each flag and add to the list if true
for field, flagName in pairs(flagMap) do
if value[field] then
table.insert(flags, flagName)
end
end
-- Join all flags with " | "
if #flags > 0 then
return table.concat(flags, " | ")
else
return "kIROpFlags_None" -- or return "" if you prefer empty string
end
end
walk_instructions(insts, function(key, value, struct_name, parent_struct)
if value.is_leaf then
-- Calculate operand count from operands array or use min_operands as fallback
local operand_count = 0
if value.operands then
operand_count = #value.operands
elseif value.min_operands then
operand_count = value.min_operands
end
RAW(
"{kIROp_"
.. struct_name
.. ', {"'
.. value.mnemonic
.. '", '
.. tostring(operand_count)
.. ", "
.. constructFlags(value)
.. "}},"
)
end
end)
end
-- The enum table
local function instEnums()
local insts = require("source/slang/slang-ir-insts.lua").insts
local output = {}
-- Walk manually so we can output in postorder
local function traverse(tbl)
local first_child = nil
local last_child = nil
-- First, process all children
for _, i in ipairs(tbl) do
local key, value = next(i)
if value.is_leaf then
-- Leaf instruction
RAW(" kIROp_" .. value.struct_name .. ",")
-- Track first and last child
if first_child == nil then
first_child = "kIROp_" .. value.struct_name
end
last_child = "kIROp_" .. value.struct_name
else
-- Parent instruction - recurse first
local child_first, child_last = traverse(value)
-- Track first and last child across all children
if first_child == nil then
first_child = child_first
end
if child_last then
last_child = child_last
end
-- Then add parent entries
if child_first and child_last then
RAW(" kIROp_First" .. value.struct_name .. " = " .. child_first .. ",")
RAW(" kIROp_Last" .. value.struct_name .. " = " .. child_last .. ",")
end
end
RAW("\n")
end
return first_child, last_child
end
traverse(insts)
return table.concat(output, "\n")
end
return {
leafInst = function(args)
-- Get the current instruction definition from the Lua data
local insts = require("source/slang/slang-ir-insts.lua").insts
local current_name = tostring(fiddle.current_decl):gsub("^IR", "")
local inst_data = findInstData(insts, current_name)
-- Merge the args with the instruction data
local merged_args = args or {}
if inst_data and inst_data.operands then
merged_args.operands = inst_data.operands
end
return leafInst(current_name, merged_args)
end,
baseInst = function(args)
-- Get the current instruction definition from the Lua data
local insts = require("source/slang/slang-ir-insts.lua").insts
local current_name = tostring(fiddle.current_decl):gsub("^IR", "")
local inst_data = findInstData(insts, current_name)
-- Merge the args with the instruction data
local merged_args = args or {}
if inst_data and inst_data.operands then
merged_args.operands = inst_data.operands
end
return baseInst(current_name, merged_args)
end,
allOtherInstStructs = allOtherInstStructs,
instStructForwardDecls = instStructForwardDecls,
instInfoEntries = instInfoEntries,
instEnums = instEnums,
}
|