@@ -166,10 +166,14 @@ class InstructionBuilder {
166166 for (size_t i = 0 ; i < operands.size (); i++) {
167167 ops.push_back ({SPV_OPERAND_TYPE_ID, {operands[i]}});
168168 }
169- // TODO(1841): Handle id overflow.
170- std::unique_ptr<Instruction> new_inst (new Instruction (
171- GetContext (), opcode, type_id,
172- result != 0 ? result : GetContext ()->TakeNextId (), ops));
169+ if (result == 0 ) {
170+ result = GetContext ()->TakeNextId ();
171+ if (result == 0 ) {
172+ return nullptr ;
173+ }
174+ }
175+ std::unique_ptr<Instruction> new_inst (
176+ new Instruction (GetContext (), opcode, type_id, result, ops));
173177 return AddInstruction (std::move (new_inst));
174178 }
175179
@@ -297,9 +301,12 @@ class InstructionBuilder {
297301 // The id |op1| is the left hand side of the operation.
298302 // The id |op2| is the right hand side of the operation.
299303 Instruction* AddIAdd (uint32_t type, uint32_t op1, uint32_t op2) {
300- // TODO(1841): Handle id overflow.
304+ uint32_t result_id = GetContext ()->TakeNextId ();
305+ if (result_id == 0 ) {
306+ return nullptr ;
307+ }
301308 std::unique_ptr<Instruction> inst (new Instruction (
302- GetContext (), spv::Op::OpIAdd, type, GetContext ()-> TakeNextId () ,
309+ GetContext (), spv::Op::OpIAdd, type, result_id ,
303310 {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}}));
304311 return AddInstruction (std::move (inst));
305312 }
@@ -311,9 +318,12 @@ class InstructionBuilder {
311318 Instruction* AddULessThan (uint32_t op1, uint32_t op2) {
312319 analysis::Bool bool_type;
313320 uint32_t type = GetContext ()->get_type_mgr ()->GetId (&bool_type);
314- // TODO(1841): Handle id overflow.
321+ uint32_t result_id = GetContext ()->TakeNextId ();
322+ if (result_id == 0 ) {
323+ return nullptr ;
324+ }
315325 std::unique_ptr<Instruction> inst (new Instruction (
316- GetContext (), spv::Op::OpULessThan, type, GetContext ()-> TakeNextId () ,
326+ GetContext (), spv::Op::OpULessThan, type, result_id ,
317327 {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}}));
318328 return AddInstruction (std::move (inst));
319329 }
@@ -325,9 +335,12 @@ class InstructionBuilder {
325335 Instruction* AddSLessThan (uint32_t op1, uint32_t op2) {
326336 analysis::Bool bool_type;
327337 uint32_t type = GetContext ()->get_type_mgr ()->GetId (&bool_type);
328- // TODO(1841): Handle id overflow.
338+ uint32_t result_id = GetContext ()->TakeNextId ();
339+ if (result_id == 0 ) {
340+ return nullptr ;
341+ }
329342 std::unique_ptr<Instruction> inst (new Instruction (
330- GetContext (), spv::Op::OpSLessThan, type, GetContext ()-> TakeNextId () ,
343+ GetContext (), spv::Op::OpSLessThan, type, result_id ,
331344 {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}}));
332345 return AddInstruction (std::move (inst));
333346 }
@@ -355,9 +368,12 @@ class InstructionBuilder {
355368 // bool) for |type|.
356369 Instruction* AddSelect (uint32_t type, uint32_t cond, uint32_t true_value,
357370 uint32_t false_value) {
358- // TODO(1841): Handle id overflow.
371+ uint32_t result_id = GetContext ()->TakeNextId ();
372+ if (result_id == 0 ) {
373+ return nullptr ;
374+ }
359375 std::unique_ptr<Instruction> select (new Instruction (
360- GetContext (), spv::Op::OpSelect, type, GetContext ()-> TakeNextId () ,
376+ GetContext (), spv::Op::OpSelect, type, result_id ,
361377 std::initializer_list<Operand>{{SPV_OPERAND_TYPE_ID, {cond}},
362378 {SPV_OPERAND_TYPE_ID, {true_value}},
363379 {SPV_OPERAND_TYPE_ID, {false_value}}}));
@@ -381,10 +397,12 @@ class InstructionBuilder {
381397 ops.emplace_back (SPV_OPERAND_TYPE_ID,
382398 std::initializer_list<uint32_t >{id});
383399 }
384- // TODO(1841): Handle id overflow.
385- std::unique_ptr<Instruction> construct (
386- new Instruction (GetContext (), spv::Op::OpCompositeConstruct, type,
387- GetContext ()->TakeNextId (), ops));
400+ uint32_t result_id = GetContext ()->TakeNextId ();
401+ if (result_id == 0 ) {
402+ return nullptr ;
403+ }
404+ std::unique_ptr<Instruction> construct (new Instruction (
405+ GetContext (), spv::Op::OpCompositeConstruct, type, result_id, ops));
388406 return AddInstruction (std::move (construct));
389407 }
390408
@@ -466,10 +484,12 @@ class InstructionBuilder {
466484 operands.push_back ({SPV_OPERAND_TYPE_LITERAL_INTEGER, {index}});
467485 }
468486
469- // TODO(1841): Handle id overflow.
470- std::unique_ptr<Instruction> new_inst (
471- new Instruction (GetContext (), spv::Op::OpCompositeExtract, type,
472- GetContext ()->TakeNextId (), operands));
487+ uint32_t result_id = GetContext ()->TakeNextId ();
488+ if (result_id == 0 ) {
489+ return nullptr ;
490+ }
491+ std::unique_ptr<Instruction> new_inst (new Instruction (
492+ GetContext (), spv::Op::OpCompositeExtract, type, result_id, operands));
473493 return AddInstruction (std::move (new_inst));
474494 }
475495
@@ -493,9 +513,12 @@ class InstructionBuilder {
493513 operands.push_back ({SPV_OPERAND_TYPE_ID, {index_id}});
494514 }
495515
496- // TODO(1841): Handle id overflow.
497- std::unique_ptr<Instruction> new_inst (new Instruction (
498- GetContext (), opcode, type_id, GetContext ()->TakeNextId (), operands));
516+ uint32_t result_id = GetContext ()->TakeNextId ();
517+ if (result_id == 0 ) {
518+ return nullptr ;
519+ }
520+ std::unique_ptr<Instruction> new_inst (
521+ new Instruction (GetContext (), opcode, type_id, result_id, operands));
499522 return AddInstruction (std::move (new_inst));
500523 }
501524
@@ -521,29 +544,36 @@ class InstructionBuilder {
521544 operands.push_back ({SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, {alignment}});
522545 }
523546
524- // TODO(1841): Handle id overflow.
525- std::unique_ptr<Instruction> new_inst (
526- new Instruction (GetContext (), spv::Op::OpLoad, type_id,
527- GetContext ()->TakeNextId (), operands));
547+ uint32_t result_id = GetContext ()->TakeNextId ();
548+ if (result_id == 0 ) {
549+ return nullptr ;
550+ }
551+ std::unique_ptr<Instruction> new_inst (new Instruction (
552+ GetContext (), spv::Op::OpLoad, type_id, result_id, operands));
528553 return AddInstruction (std::move (new_inst));
529554 }
530555
531556 Instruction* AddCopyObject (uint32_t type_id, uint32_t value_id) {
532557 std::vector<Operand> operands{{SPV_OPERAND_TYPE_ID, {value_id}}};
533558
534- // TODO(1841): Handle id overflow.
535- std::unique_ptr<Instruction> new_inst (
536- new Instruction (GetContext (), spv::Op::OpCopyObject, type_id,
537- GetContext ()->TakeNextId (), operands));
559+ uint32_t result_id = GetContext ()->TakeNextId ();
560+ if (result_id == 0 ) {
561+ return nullptr ;
562+ }
563+ std::unique_ptr<Instruction> new_inst (new Instruction (
564+ GetContext (), spv::Op::OpCopyObject, type_id, result_id, operands));
538565 return AddInstruction (std::move (new_inst));
539566 }
540567
541568 Instruction* AddVariable (uint32_t type_id, uint32_t storage_class) {
542569 std::vector<Operand> operands;
543570 operands.push_back ({SPV_OPERAND_TYPE_STORAGE_CLASS, {storage_class}});
544- std::unique_ptr<Instruction> new_inst (
545- new Instruction (GetContext (), spv::Op::OpVariable, type_id,
546- GetContext ()->TakeNextId (), operands));
571+ uint32_t result_id = GetContext ()->TakeNextId ();
572+ if (result_id == 0 ) {
573+ return nullptr ;
574+ }
575+ std::unique_ptr<Instruction> new_inst (new Instruction (
576+ GetContext (), spv::Op::OpVariable, type_id, result_id, operands));
547577 return AddInstruction (std::move (new_inst));
548578 }
549579
0 commit comments