OP_ACCESS_CHAIN = 65,
OP_DECORATE = 71,
OP_MEMBER_DECORATE = 72,
+ OP_SELECTION_MERGE = 247,
OP_LABEL = 248,
OP_BRANCH = 249,
OP_BRANCH_CONDITIONAL = 250,
+ OP_SWITCH = 251,
+ OP_KILL = 252,
+ OP_RETURN = 253,
+ OP_RETURN_VALUE = 254,
+ OP_UNREACHABLE = 255,
DECO_SPEC_ID = 1,
DECO_ARRAY_STRIDE = 6,
}
}
+SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
+{
+ vector<uint8_t> flags(code[3], 1);
+
+ std::map<unsigned, int> spec_values_by_id;
+ for(const Constant &c: spec_constants)
+ {
+ auto i = spec_values.find(c.name);
+ if(i!=spec_values.end())
+ {
+ flags[c.id] = (i->second ? 5 : 3);
+ spec_values_by_id[c.constant_id] = i->second;
+ }
+ }
+
+ for(const Variable &v: variables)
+ flags[v.id] = 0;
+ for(const InstructionBlock &b: blocks)
+ flags[b.id] = 0;
+ for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
+ {
+ flags[b->id] = 1;
+ for(const Variable *v: b->accessed_variables)
+ flags[v->id] = 1;
+ }
+
+ std::vector<uint32_t> new_code;
+ new_code.reserve(code.size());
+
+ auto op = code.begin()+5;
+ new_code.insert(new_code.begin(), code.begin(), op);
+
+ bool skip_block = false;
+ while(op!=code.end())
+ {
+ unsigned word_count = *op>>16;
+ unsigned opcode = *op&0xFFFF;
+
+ bool copy = !skip_block;
+ if(skip_block)
+ {
+ skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
+ opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
+ }
+ else
+ {
+ if(opcode==OP_NAME)
+ copy = flags[*(op+1)];
+ else if(opcode==OP_ENTRY_POINT)
+ {
+ unsigned start = new_code.size();
+ new_code.push_back(opcode);
+ new_code.push_back(*(op+1));
+ new_code.push_back(*(op+2));
+
+ unsigned i=3;
+ while(i<word_count)
+ {
+ unsigned word = *(op+i++);
+ new_code.push_back(word);
+ if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
+ break;
+ }
+
+ for(; i<word_count; ++i)
+ {
+ unsigned id = *(op+i);
+ if(flags[id])
+ new_code.push_back(id);
+ }
+
+ new_code[start] |= (new_code.size()-start)<<16;
+
+ copy = false;
+ }
+ else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
+ {
+ unsigned id = *(op+2);
+ if(flags[id]&2)
+ {
+ new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
+ new_code.push_back(*(op+1));
+ new_code.push_back(id);
+
+ copy = false;
+ }
+ }
+ else if(opcode==OP_VARIABLE)
+ copy = flags[*(op+2)];
+ else if(opcode==OP_DECORATE)
+ {
+ unsigned id = *(op+1);
+ copy = flags[id];
+ if(copy && *(op+2)==DECO_SPEC_ID)
+ copy = !(flags[id]&2);
+ }
+ else if(opcode==OP_LABEL)
+ {
+ copy = flags[*(op+1)];
+ skip_block = !copy;
+ }
+ else if(opcode==OP_SELECTION_MERGE)
+ {
+ unsigned next_opcode = *(op+word_count)&0xFFFF;
+ if(next_opcode==OP_BRANCH_CONDITIONAL)
+ {
+ unsigned true_id = *(op+word_count+2);
+ unsigned false_id = *(op+word_count+3);
+ if(!flags[true_id] || !flags[false_id])
+ {
+ new_code.push_back(0x20000 | OP_BRANCH);
+ new_code.push_back(flags[true_id] ? true_id : false_id);
+ copy = false;
+
+ /* Skip the branch instruction when it's encountered on the
+ next iteration */
+ skip_block = true;
+ }
+ }
+ }
+ }
+
+ if(copy)
+ {
+ for(unsigned i=0; i<word_count; ++i)
+ new_code.push_back(*(op+i));
+ }
+
+ op += word_count;
+ }
+
+ SpirVModule *spec_mod = new SpirVModule;
+ spec_mod->code = move(new_code);
+ spec_mod->reflect();
+ spec_mod->create();
+
+ return spec_mod;
+}
+
+vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
+{
+ vector<uint8_t> visited(blocks.size(), 4);
+ for(unsigned i=0; i<blocks.size(); ++i)
+ {
+ const InstructionBlock &b = blocks[i];
+
+ bool cond = true;
+ if(b.condition)
+ {
+ cond = b.condition->i_value;
+ auto j = spec_values.find(b.condition->constant_id);
+ if(j!=spec_values.end())
+ cond = j->second;
+ if(b.negate_condition)
+ cond = !cond;
+ }
+
+ visited[i] |= cond*2;
+ for(const InstructionBlock *s: b.successors)
+ visited[s-blocks.data()] &= 3;
+ }
+
+ for(unsigned i=0; i<blocks.size(); ++i)
+ if(visited[i]&4)
+ collect_visited_blocks(i, visited);
+
+ vector<const SpirVModule::InstructionBlock *> result;
+ for(unsigned i=0; i<blocks.size(); ++i)
+ if(visited[i]&1)
+ result.push_back(&blocks[i]);
+
+ return result;
+}
+
+void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
+{
+ visited[i] |= 1;
+ for(const InstructionBlock *s: blocks[i].successors)
+ {
+ unsigned j = s-blocks.data();
+ if((visited[j]&3)==2)
+ collect_visited_blocks(j, visited);
+ }
+}
+
bool SpirVModule::Variable::operator==(const Variable &other) const
{
void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
{
CodeIterator op_end = get_op_end(op);
- EntryPoint &entry = entry_points[*(op+2)];
+ unsigned id = *(op+2);
+ EntryPoint &entry = entry_points[id];
+ entry.id = id;
entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
op += 3;
entry.name = read_string(op, op_end);
unsigned id = *(op+1);
Structure &strct = structs[id];
strct.name = names[id];
+ strct.id = id;
types[id].struct_type = &strct;
op += 2;
unsigned id = *(op+2);
Constant &cnst = constants[id];
cnst.name = names[id];
+ cnst.id = id;
cnst.type = types[*(op+1)].type;
if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
cnst.i_value = true;
unsigned id = *(op+2);
Variable &var = variables[id];
var.name = names[id];
+ var.id = id;
const TypeInfo &type = types[*(op+1)];
var.storage = type.storage;
var.type = type.type;
void SpirVModule::Reflection::reflect_label(CodeIterator op)
{
- current_block = &blocks[*(op+1)];
+ unsigned id = *(op+1);
+ current_block = &blocks[id];
+ current_block->id = id;
}
void SpirVModule::Reflection::reflect_branch(CodeIterator op)