| 1433 | } |
| 1434 | |
| 1435 | std::unordered_map<instruction_ref, std::string> |
| 1436 | module::print_cpp(std::ostream& os, |
| 1437 | const std::string& mname, |
| 1438 | std::unordered_map<instruction_ref, std::string> names) const |
| 1439 | { |
| 1440 | // cppcheck-suppress variableScope |
| 1441 | unsigned long seed = names.size(); |
| 1442 | auto last = std::prev(this->end()); |
| 1443 | names = this->print( |
| 1444 | [&](auto ins, auto ins_names) { |
| 1445 | std::vector<std::string> input_vars; |
| 1446 | std::transform(ins->inputs().begin(), |
| 1447 | ins->inputs().end(), |
| 1448 | std::back_inserter(input_vars), |
| 1449 | [&](auto input) { return cpp_var_name(ins_names.at(input)); }); |
| 1450 | if(ins != last) |
| 1451 | os << "auto " << cpp_var_name(ins_names.at(ins)) << " = "; |
| 1452 | if(ins->name() == "@literal") |
| 1453 | { |
| 1454 | os << mname << "->add_literal("; |
| 1455 | bool use_abs = false; |
| 1456 | ins->get_literal().visit([&](auto v) { |
| 1457 | use_abs = std::none_of(v.begin(), v.end(), [](auto x) { return x < 0; }); |
| 1458 | }); |
| 1459 | if(use_abs) |
| 1460 | os << "migraphx::abs("; |
| 1461 | os << "migraphx::generate_literal("; |
| 1462 | print_cpp_shape(os, ins->get_shape()); |
| 1463 | os << ", " << seed << ")"; |
| 1464 | if(use_abs) |
| 1465 | os << ")"; |
| 1466 | os << ");" << std::endl; |
| 1467 | seed++; |
| 1468 | } |
| 1469 | else if(ins->name() == "@param") |
| 1470 | { |
| 1471 | std::string name = any_cast<builtin::param>(ins->get_operator()).parameter; |
| 1472 | os << mname << "->add_parameter(" << enclose_name(name) << ","; |
| 1473 | print_cpp_shape(os, ins->get_shape()); |
| 1474 | os << ");" << std::endl; |
| 1475 | } |
| 1476 | else if(ins->name() == "@return") |
| 1477 | { |
| 1478 | os << mname << "->add_return({"; |
| 1479 | os << join_strings(input_vars, ", "); |
| 1480 | os << "});" << std::endl; |
| 1481 | } |
| 1482 | else |
| 1483 | { |
| 1484 | assert(ins->name().front() != '@'); |
| 1485 | os << mname << "->add_instruction("; |
| 1486 | print_make_op(os, ins->get_operator()); |
| 1487 | os << ", " << join_strings(input_vars, ", "); |
| 1488 | os << ");" << std::endl; |
| 1489 | } |
| 1490 | }, |
| 1491 | names); |
| 1492 | |