| 388 | } |
| 389 | |
| 390 | bool TransformationAddFunction::TryToAddLoopLimiters( |
| 391 | opt::IRContext* ir_context, opt::Function* added_function) const { |
| 392 | // Collect up all the loop headers so that we can subsequently add loop |
| 393 | // limiting logic. |
| 394 | std::vector<opt::BasicBlock*> loop_headers; |
| 395 | for (auto& block : *added_function) { |
| 396 | if (block.IsLoopHeader()) { |
| 397 | loop_headers.push_back(&block); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (loop_headers.empty()) { |
| 402 | // There are no loops, so no need to add any loop limiters. |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | // Check that the module contains appropriate ingredients for declaring and |
| 407 | // manipulating a loop limiter. |
| 408 | |
| 409 | auto loop_limit_constant_id_instr = |
| 410 | ir_context->get_def_use_mgr()->GetDef(message_.loop_limit_constant_id()); |
| 411 | if (!loop_limit_constant_id_instr || |
| 412 | loop_limit_constant_id_instr->opcode() != spv::Op::OpConstant) { |
| 413 | // The loop limit constant id instruction must exist and have an |
| 414 | // appropriate opcode. |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | auto loop_limit_type = ir_context->get_def_use_mgr()->GetDef( |
| 419 | loop_limit_constant_id_instr->type_id()); |
| 420 | if (loop_limit_type->opcode() != spv::Op::OpTypeInt || |
| 421 | loop_limit_type->GetSingleWordInOperand(0) != 32) { |
| 422 | // The type of the loop limit constant must be 32-bit integer. It |
| 423 | // doesn't actually matter whether the integer is signed or not. |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | // Find the id of the "unsigned int" type. |
| 428 | opt::analysis::Integer unsigned_int_type(32, false); |
| 429 | uint32_t unsigned_int_type_id = |
| 430 | ir_context->get_type_mgr()->GetId(&unsigned_int_type); |
| 431 | if (!unsigned_int_type_id) { |
| 432 | // Unsigned int is not available; we need this type in order to add loop |
| 433 | // limiters. |
| 434 | return false; |
| 435 | } |
| 436 | auto registered_unsigned_int_type = |
| 437 | ir_context->get_type_mgr()->GetRegisteredType(&unsigned_int_type); |
| 438 | |
| 439 | // Look for 0 of type unsigned int. |
| 440 | opt::analysis::IntConstant zero(registered_unsigned_int_type->AsInteger(), |
| 441 | {0}); |
| 442 | auto registered_zero = ir_context->get_constant_mgr()->FindConstant(&zero); |
| 443 | if (!registered_zero) { |
| 444 | // We need 0 in order to be able to initialize loop limiters. |
| 445 | return false; |
| 446 | } |
| 447 | uint32_t zero_id = ir_context->get_constant_mgr() |
nothing calls this directly
no test coverage detected