+
+ map<const InstructionBlock *, unsigned> block_indices;
+ blocks.reserve(reflection.blocks.size());
+ for(const auto &kvp: reflection.blocks)
+ {
+ block_indices[&kvp.second] = blocks.size();
+ blocks.push_back(kvp.second);
+ }
+
+ for(InstructionBlock &b: blocks)
+ {
+ auto i = spec_indices.find(b.condition);
+ b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
+
+ for(const Variable *&v: b.accessed_variables)
+ {
+ auto j = var_indices.find(v);
+ v = (j!=var_indices.end() ? &variables[j->second] : 0);
+ }
+
+ for(const InstructionBlock *&s: b.successors)
+ {
+ auto j = block_indices.find(s);
+ s = (j!=block_indices.end() ? &blocks[j->second] : 0);
+ }
+ }
+}
+
+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);
+ }