Skip to content

Commit 4dcc12d

Browse files
authored
SPIRV: Add more utility functions to build some opcodes (KhronosGroup#2398)
Add more builder functions to OpExecutionMode, OpExecutionModeId, OpDecorateString, OpMemberDecorateString. According to SPIR-V, OpExecutionMode and OpExecutionModeId could take variable extra operands. Current implementation doesn't support this and assumes at most 3 operands are extra operands. It is not true. Similarly, OpDecorateString and OpMemberDecorateString could support multiple strings either as literal strings or as string operands. Further, OpDecorate and OpDecorateId have the same problem, taking variable extra operands.
1 parent 9eaa69c commit 4dcc12d

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

SPIRV/SpvBuilder.cpp

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,28 @@ void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, int val
11831183
executionModes.push_back(std::unique_ptr<Instruction>(instr));
11841184
}
11851185

1186+
void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, const std::vector<unsigned>& literals)
1187+
{
1188+
Instruction* instr = new Instruction(OpExecutionMode);
1189+
instr->addIdOperand(entryPoint->getId());
1190+
instr->addImmediateOperand(mode);
1191+
for (auto literal : literals)
1192+
instr->addImmediateOperand(literal);
1193+
1194+
executionModes.push_back(std::unique_ptr<Instruction>(instr));
1195+
}
1196+
1197+
void Builder::addExecutionModeId(Function* entryPoint, ExecutionMode mode, const std::vector<Id>& operandIds)
1198+
{
1199+
Instruction* instr = new Instruction(OpExecutionModeId);
1200+
instr->addIdOperand(entryPoint->getId());
1201+
instr->addImmediateOperand(mode);
1202+
for (auto operandId : operandIds)
1203+
instr->addIdOperand(operandId);
1204+
1205+
executionModes.push_back(std::unique_ptr<Instruction>(instr));
1206+
}
1207+
11861208
void Builder::addName(Id id, const char* string)
11871209
{
11881210
Instruction* name = new Instruction(OpName);
@@ -1221,14 +1243,42 @@ void Builder::addDecoration(Id id, Decoration decoration, const char* s)
12211243
if (decoration == spv::DecorationMax)
12221244
return;
12231245

1224-
Instruction* dec = new Instruction(OpDecorateStringGOOGLE);
1246+
Instruction* dec = new Instruction(OpDecorateString);
12251247
dec->addIdOperand(id);
12261248
dec->addImmediateOperand(decoration);
12271249
dec->addStringOperand(s);
12281250

12291251
decorations.push_back(std::unique_ptr<Instruction>(dec));
12301252
}
12311253

1254+
void Builder::addDecoration(Id id, Decoration decoration, const std::vector<unsigned>& literals)
1255+
{
1256+
if (decoration == spv::DecorationMax)
1257+
return;
1258+
1259+
Instruction* dec = new Instruction(OpDecorate);
1260+
dec->addIdOperand(id);
1261+
dec->addImmediateOperand(decoration);
1262+
for (auto literal : literals)
1263+
dec->addImmediateOperand(literal);
1264+
1265+
decorations.push_back(std::unique_ptr<Instruction>(dec));
1266+
}
1267+
1268+
void Builder::addDecoration(Id id, Decoration decoration, const std::vector<const char*>& strings)
1269+
{
1270+
if (decoration == spv::DecorationMax)
1271+
return;
1272+
1273+
Instruction* dec = new Instruction(OpDecorateString);
1274+
dec->addIdOperand(id);
1275+
dec->addImmediateOperand(decoration);
1276+
for (auto string : strings)
1277+
dec->addStringOperand(string);
1278+
1279+
decorations.push_back(std::unique_ptr<Instruction>(dec));
1280+
}
1281+
12321282
void Builder::addDecorationId(Id id, Decoration decoration, Id idDecoration)
12331283
{
12341284
if (decoration == spv::DecorationMax)
@@ -1242,6 +1292,21 @@ void Builder::addDecorationId(Id id, Decoration decoration, Id idDecoration)
12421292
decorations.push_back(std::unique_ptr<Instruction>(dec));
12431293
}
12441294

1295+
void Builder::addDecorationId(Id id, Decoration decoration, const std::vector<Id>& operandIds)
1296+
{
1297+
if(decoration == spv::DecorationMax)
1298+
return;
1299+
1300+
Instruction* dec = new Instruction(OpDecorateId);
1301+
dec->addIdOperand(id);
1302+
dec->addImmediateOperand(decoration);
1303+
1304+
for (auto operandId : operandIds)
1305+
dec->addIdOperand(operandId);
1306+
1307+
decorations.push_back(std::unique_ptr<Instruction>(dec));
1308+
}
1309+
12451310
void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, int num)
12461311
{
12471312
if (decoration == spv::DecorationMax)
@@ -1271,6 +1336,36 @@ void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decorat
12711336
decorations.push_back(std::unique_ptr<Instruction>(dec));
12721337
}
12731338

1339+
void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const std::vector<unsigned>& literals)
1340+
{
1341+
if (decoration == spv::DecorationMax)
1342+
return;
1343+
1344+
Instruction* dec = new Instruction(OpMemberDecorate);
1345+
dec->addIdOperand(id);
1346+
dec->addImmediateOperand(member);
1347+
dec->addImmediateOperand(decoration);
1348+
for (auto literal : literals)
1349+
dec->addImmediateOperand(literal);
1350+
1351+
decorations.push_back(std::unique_ptr<Instruction>(dec));
1352+
}
1353+
1354+
void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const std::vector<const char*>& strings)
1355+
{
1356+
if (decoration == spv::DecorationMax)
1357+
return;
1358+
1359+
Instruction* dec = new Instruction(OpMemberDecorateString);
1360+
dec->addIdOperand(id);
1361+
dec->addImmediateOperand(member);
1362+
dec->addImmediateOperand(decoration);
1363+
for (auto string : strings)
1364+
dec->addStringOperand(string);
1365+
1366+
decorations.push_back(std::unique_ptr<Instruction>(dec));
1367+
}
1368+
12741369
// Comments in header
12751370
Function* Builder::makeEntryPoint(const char* entryPoint)
12761371
{

SPIRV/SpvBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,20 @@ class Builder {
321321
// Methods for adding information outside the CFG.
322322
Instruction* addEntryPoint(ExecutionModel, Function*, const char* name);
323323
void addExecutionMode(Function*, ExecutionMode mode, int value1 = -1, int value2 = -1, int value3 = -1);
324+
void addExecutionMode(Function*, ExecutionMode mode, const std::vector<unsigned>& literals);
325+
void addExecutionModeId(Function*, ExecutionMode mode, const std::vector<Id>& operandIds);
324326
void addName(Id, const char* name);
325327
void addMemberName(Id, int member, const char* name);
326328
void addDecoration(Id, Decoration, int num = -1);
327329
void addDecoration(Id, Decoration, const char*);
330+
void addDecoration(Id, Decoration, const std::vector<unsigned>& literals);
331+
void addDecoration(Id, Decoration, const std::vector<const char*>& strings);
328332
void addDecorationId(Id id, Decoration, Id idDecoration);
333+
void addDecorationId(Id id, Decoration, const std::vector<Id>& operandIds);
329334
void addMemberDecoration(Id, unsigned int member, Decoration, int num = -1);
330335
void addMemberDecoration(Id, unsigned int member, Decoration, const char*);
336+
void addMemberDecoration(Id, unsigned int member, Decoration, const std::vector<unsigned>& literals);
337+
void addMemberDecoration(Id, unsigned int member, Decoration, const std::vector<const char*>& strings);
331338

332339
// At the end of what block do the next create*() instructions go?
333340
void setBuildPoint(Block* bp) { buildPoint = bp; }

0 commit comments

Comments
 (0)