|
14 | 14 | import pytest |
15 | 15 | from linker.instructions.cinst import CLoad, CStore |
16 | 16 | from linker.instructions.minst import MLoad, MStore |
17 | | -from linker.kern_trace.kern_remap import remap_cinstrs_vars_hbm, remap_dinstrs_vars, remap_m_c_instrs_vars |
| 17 | +from linker.instructions.xinst import Mac |
| 18 | +from linker.kern_trace.kern_remap import remap_cinstrs_vars_hbm, remap_dinstrs_vars, remap_m_c_instrs_vars, remap_xinstrs_vars |
18 | 19 | from linker.kern_trace.kern_var import KernVar |
19 | 20 | from linker.kern_trace.kernel_op import KernelOp |
20 | 21 |
|
@@ -353,3 +354,306 @@ def test_invalid_var_name(self): |
353 | 354 | # Assert |
354 | 355 | assert mock_instr.var_name == "0" # Unchanged |
355 | 356 | assert mock_instr.comment == "Store new_dest" |
| 357 | + |
| 358 | + |
| 359 | +class TestRemapCinstrsVarsHbm: |
| 360 | + """ |
| 361 | + @class TestRemapCinstrsVarsHbm |
| 362 | + @brief Test cases for the remap_cinstrs_vars_hbm function |
| 363 | + """ |
| 364 | + |
| 365 | + def _create_remap_dict(self): |
| 366 | + """ |
| 367 | + @brief Helper method to create a remap dictionary |
| 368 | + """ |
| 369 | + return {"old_var": "new_var", "input_data": "remapped_input"} |
| 370 | + |
| 371 | + def test_remap_cload_comment(self): |
| 372 | + """ |
| 373 | + @brief Test remapping variables in CLoad instruction comments |
| 374 | + """ |
| 375 | + # Arrange |
| 376 | + mock_instr = MagicMock(spec=CLoad) |
| 377 | + mock_instr.comment = "Load from old_var" |
| 378 | + |
| 379 | + kernel_instrs = [mock_instr] |
| 380 | + hbm_remap_dict = self._create_remap_dict() |
| 381 | + |
| 382 | + # Act |
| 383 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 384 | + |
| 385 | + # Assert |
| 386 | + assert mock_instr.comment == "Load from new_var" |
| 387 | + |
| 388 | + def test_remap_cstore_comment(self): |
| 389 | + """ |
| 390 | + @brief Test remapping variables in CStore instruction comments |
| 391 | + """ |
| 392 | + # Arrange |
| 393 | + mock_instr = MagicMock(spec=CStore) |
| 394 | + mock_instr.comment = "Store to input_data buffer" |
| 395 | + |
| 396 | + kernel_instrs = [mock_instr] |
| 397 | + hbm_remap_dict = self._create_remap_dict() |
| 398 | + |
| 399 | + # Act |
| 400 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 401 | + |
| 402 | + # Assert |
| 403 | + assert mock_instr.comment == "Store to remapped_input buffer" |
| 404 | + |
| 405 | + def test_remap_after_first_match(self): |
| 406 | + """ |
| 407 | + @brief Test that remapping even after first match (break statement) |
| 408 | + """ |
| 409 | + # Arrange |
| 410 | + mock_instr = MagicMock(spec=CLoad) |
| 411 | + mock_instr.comment = "old_var and old_var again" |
| 412 | + |
| 413 | + kernel_instrs = [mock_instr] |
| 414 | + # Dict with multiple keys that could match |
| 415 | + hbm_remap_dict = {"old_var": "new_var", "again": "never"} |
| 416 | + |
| 417 | + # Act |
| 418 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 419 | + |
| 420 | + # Assert |
| 421 | + # Only first match should be replaced due to break |
| 422 | + assert mock_instr.comment == "new_var and new_var again" |
| 423 | + assert "never" not in mock_instr.comment |
| 424 | + |
| 425 | + def test_skip_unmapped_variables(self): |
| 426 | + """ |
| 427 | + @brief Test that variables not in remap dict are unchanged |
| 428 | + """ |
| 429 | + # Arrange |
| 430 | + mock_instr = MagicMock(spec=CLoad) |
| 431 | + mock_instr.comment = "Load unmapped_var" |
| 432 | + |
| 433 | + kernel_instrs = [mock_instr] |
| 434 | + hbm_remap_dict = self._create_remap_dict() |
| 435 | + |
| 436 | + # Act |
| 437 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 438 | + |
| 439 | + # Assert |
| 440 | + assert mock_instr.comment == "Load unmapped_var" # Unchanged |
| 441 | + |
| 442 | + def test_empty_remap_dict(self): |
| 443 | + """ |
| 444 | + @brief Test with empty remap dictionary |
| 445 | + """ |
| 446 | + # Arrange |
| 447 | + mock_instr = MagicMock(spec=CStore) |
| 448 | + mock_instr.comment = "Original comment" |
| 449 | + |
| 450 | + kernel_instrs = [mock_instr] |
| 451 | + hbm_remap_dict = {} |
| 452 | + |
| 453 | + # Act |
| 454 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 455 | + |
| 456 | + # Assert |
| 457 | + assert mock_instr.comment == "Original comment" |
| 458 | + |
| 459 | + def test_invalid_instruction_type(self): |
| 460 | + """ |
| 461 | + @brief Test error when instruction is not a CInstruction |
| 462 | + """ |
| 463 | + # Arrange |
| 464 | + mock_instr = MagicMock() # Not a CInstruction |
| 465 | + |
| 466 | + kernel_instrs = [mock_instr] |
| 467 | + hbm_remap_dict = self._create_remap_dict() |
| 468 | + |
| 469 | + # Act & Assert |
| 470 | + with pytest.raises(TypeError, match="not a valid CInstruction"): |
| 471 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 472 | + |
| 473 | + def test_multiple_instructions(self): |
| 474 | + """ |
| 475 | + @brief Test remapping across multiple instructions |
| 476 | + """ |
| 477 | + # Arrange |
| 478 | + mock_cload = MagicMock(spec=CLoad) |
| 479 | + mock_cload.comment = "Load old_var" |
| 480 | + |
| 481 | + mock_cstore = MagicMock(spec=CStore) |
| 482 | + mock_cstore.comment = "Store input_data" |
| 483 | + |
| 484 | + kernel_instrs = [mock_cload, mock_cstore] |
| 485 | + hbm_remap_dict = self._create_remap_dict() |
| 486 | + |
| 487 | + # Act |
| 488 | + remap_cinstrs_vars_hbm(kernel_instrs, hbm_remap_dict) |
| 489 | + |
| 490 | + # Assert |
| 491 | + assert mock_cload.comment == "Load new_var" |
| 492 | + assert mock_cstore.comment == "Store remapped_input" |
| 493 | + |
| 494 | + |
| 495 | +class TestRemapXinstrsVars: |
| 496 | + """ |
| 497 | + @class TestRemapXinstrsVars |
| 498 | + @brief Test cases for the remap_xinstrs_vars function |
| 499 | + """ |
| 500 | + |
| 501 | + def _create_remap_dict(self): |
| 502 | + """ |
| 503 | + @brief Helper method to create a remap dictionary |
| 504 | + """ |
| 505 | + return {"source_var": "remapped_source", "dest_var": "remapped_dest"} |
| 506 | + |
| 507 | + def test_remap_move_instruction_comment(self): |
| 508 | + """ |
| 509 | + @brief Test remapping variables in Move instruction comments |
| 510 | + """ |
| 511 | + # Arrange |
| 512 | + from linker.instructions.xinst import Move |
| 513 | + |
| 514 | + mock_move = MagicMock(spec=Move) |
| 515 | + mock_move.comment = "Move from source_var" |
| 516 | + |
| 517 | + kernel_xinstrs = [mock_move] |
| 518 | + hbm_remap_dict = self._create_remap_dict() |
| 519 | + |
| 520 | + # Act |
| 521 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 522 | + |
| 523 | + # Assert |
| 524 | + assert mock_move.comment == "Move from remapped_source" |
| 525 | + |
| 526 | + def test_remap_xstore_instruction_comment(self): |
| 527 | + """ |
| 528 | + @brief Test remapping variables in XStore instruction comments |
| 529 | + """ |
| 530 | + # Arrange |
| 531 | + from linker.instructions.xinst import XStore |
| 532 | + |
| 533 | + mock_xstore = MagicMock(spec=XStore) |
| 534 | + mock_xstore.comment = "Store to dest_var" |
| 535 | + |
| 536 | + kernel_xinstrs = [mock_xstore] |
| 537 | + hbm_remap_dict = self._create_remap_dict() |
| 538 | + |
| 539 | + # Act |
| 540 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 541 | + |
| 542 | + # Assert |
| 543 | + assert mock_xstore.comment == "Store to remapped_dest" |
| 544 | + |
| 545 | + def test_remap_after_first_match(self): |
| 546 | + """ |
| 547 | + @brief Test that remapping even after first match (break statement) |
| 548 | + """ |
| 549 | + # Arrange |
| 550 | + from linker.instructions.xinst import Move |
| 551 | + |
| 552 | + mock_instr = MagicMock(spec=Move) |
| 553 | + mock_instr.comment = "source_var and source_var repeated" |
| 554 | + |
| 555 | + kernel_xinstrs = [mock_instr] |
| 556 | + hbm_remap_dict = {"source_var": "new_src", "repeated": "never_used"} |
| 557 | + |
| 558 | + # Act |
| 559 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 560 | + |
| 561 | + # Assert |
| 562 | + # Only first match replaced due to break |
| 563 | + assert mock_instr.comment == "new_src and new_src repeated" |
| 564 | + assert "never_used" not in mock_instr.comment |
| 565 | + |
| 566 | + def test_skip_unmapped_variables(self): |
| 567 | + """ |
| 568 | + @brief Test that unmapped variables remain unchanged |
| 569 | + """ |
| 570 | + # Arrange |
| 571 | + from linker.instructions.xinst import Move |
| 572 | + |
| 573 | + mock_instr = MagicMock(spec=Move) |
| 574 | + mock_instr.comment = "Move unmapped_var" |
| 575 | + |
| 576 | + kernel_xinstrs = [mock_instr] |
| 577 | + hbm_remap_dict = self._create_remap_dict() |
| 578 | + |
| 579 | + # Act |
| 580 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 581 | + |
| 582 | + # Assert |
| 583 | + assert mock_instr.comment == "Move unmapped_var" |
| 584 | + |
| 585 | + def test_empty_remap_dict(self): |
| 586 | + """ |
| 587 | + @brief Test with empty remap dictionary |
| 588 | + """ |
| 589 | + # Arrange |
| 590 | + from linker.instructions.xinst import XStore |
| 591 | + |
| 592 | + mock_instr = MagicMock(spec=XStore) |
| 593 | + mock_instr.comment = "Original XStore comment" |
| 594 | + |
| 595 | + kernel_xinstrs = [mock_instr] |
| 596 | + hbm_remap_dict = {} |
| 597 | + |
| 598 | + # Act |
| 599 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 600 | + |
| 601 | + # Assert |
| 602 | + assert mock_instr.comment == "Original XStore comment" |
| 603 | + |
| 604 | + def test_invalid_instruction_type(self): |
| 605 | + """ |
| 606 | + @brief Test error when instruction is not an XInstruction |
| 607 | + """ |
| 608 | + # Arrange |
| 609 | + mock_instr = MagicMock() # Not an XInstruction |
| 610 | + |
| 611 | + kernel_xinstrs = [mock_instr] |
| 612 | + hbm_remap_dict = self._create_remap_dict() |
| 613 | + |
| 614 | + # Act & Assert |
| 615 | + with pytest.raises(TypeError, match="not a valid X Instruction"): |
| 616 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 617 | + |
| 618 | + def test_multiple_instructions(self): |
| 619 | + """ |
| 620 | + @brief Test remapping across multiple X instructions |
| 621 | + """ |
| 622 | + # Arrange |
| 623 | + from linker.instructions.xinst import Move, XStore |
| 624 | + |
| 625 | + mock_move = MagicMock(spec=Move) |
| 626 | + mock_move.comment = "Move source_var" |
| 627 | + |
| 628 | + mock_xstore = MagicMock(spec=XStore) |
| 629 | + mock_xstore.comment = "Store dest_var" |
| 630 | + |
| 631 | + kernel_xinstrs = [mock_move, mock_xstore] |
| 632 | + hbm_remap_dict = self._create_remap_dict() |
| 633 | + |
| 634 | + # Act |
| 635 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 636 | + |
| 637 | + # Assert |
| 638 | + assert mock_move.comment == "Move remapped_source" |
| 639 | + assert mock_xstore.comment == "Store remapped_dest" |
| 640 | + |
| 641 | + def test_non_move_xstore_instructions_ignored(self): |
| 642 | + """ |
| 643 | + @brief Test that non-Move/XStore X instructions are not processed |
| 644 | + """ |
| 645 | + # Create a mock XInstruction that's not Move or XStore |
| 646 | + mock_other = MagicMock(spec=Mac) |
| 647 | + # Remove Move and XStore from isinstance check |
| 648 | + type(mock_other).__name__ = "OtherXInst" |
| 649 | + mock_other.comment = "source_var reference" |
| 650 | + |
| 651 | + kernel_xinstrs = [mock_other] |
| 652 | + hbm_remap_dict = self._create_remap_dict() |
| 653 | + |
| 654 | + # Act |
| 655 | + remap_xinstrs_vars(kernel_xinstrs, hbm_remap_dict) |
| 656 | + |
| 657 | + # Assert |
| 658 | + # Comment should be unchanged since it's not Move or XStore |
| 659 | + assert mock_other.comment == "source_var reference" |
0 commit comments