+ 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;