File size: 34,412 Bytes
c65f48d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
from ctypes import (c_bool, c_char_p, c_int, c_size_t, c_uint, Structure, byref,
POINTER)
from collections import namedtuple
from enum import IntFlag
from llvmlite.binding import ffi
import os
from tempfile import mkstemp
from llvmlite.binding.common import _encode_string
_prunestats = namedtuple('PruneStats',
('basicblock diamond fanout fanout_raise'))
class PruneStats(_prunestats):
""" Holds statistics from reference count pruning.
"""
def __add__(self, other):
if not isinstance(other, PruneStats):
msg = 'PruneStats can only be added to another PruneStats, got {}.'
raise TypeError(msg.format(type(other)))
return PruneStats(self.basicblock + other.basicblock,
self.diamond + other.diamond,
self.fanout + other.fanout,
self.fanout_raise + other.fanout_raise)
def __sub__(self, other):
if not isinstance(other, PruneStats):
msg = ('PruneStats can only be subtracted from another PruneStats, '
'got {}.')
raise TypeError(msg.format(type(other)))
return PruneStats(self.basicblock - other.basicblock,
self.diamond - other.diamond,
self.fanout - other.fanout,
self.fanout_raise - other.fanout_raise)
class _c_PruneStats(Structure):
_fields_ = [
('basicblock', c_size_t),
('diamond', c_size_t),
('fanout', c_size_t),
('fanout_raise', c_size_t)]
def dump_refprune_stats(printout=False):
""" Returns a namedtuple containing the current values for the refop pruning
statistics. If kwarg `printout` is True the stats are printed to stderr,
default is False.
"""
stats = _c_PruneStats(0, 0, 0, 0)
do_print = c_bool(printout)
ffi.lib.LLVMPY_DumpRefPruneStats(byref(stats), do_print)
return PruneStats(stats.basicblock, stats.diamond, stats.fanout,
stats.fanout_raise)
def set_time_passes(enable):
"""Enable or disable the pass timers.
Parameters
----------
enable : bool
Set to True to enable the pass timers.
Set to False to disable the pass timers.
"""
ffi.lib.LLVMPY_SetTimePasses(c_bool(enable))
def report_and_reset_timings():
"""Returns the pass timings report and resets the LLVM internal timers.
Pass timers are enabled by ``set_time_passes()``. If the timers are not
enabled, this function will return an empty string.
Returns
-------
res : str
LLVM generated timing report.
"""
with ffi.OutputString() as buf:
ffi.lib.LLVMPY_ReportAndResetTimings(buf)
return str(buf)
def create_module_pass_manager():
return ModulePassManager()
def create_function_pass_manager(module):
return FunctionPassManager(module)
class RefPruneSubpasses(IntFlag):
PER_BB = 0b0001 # noqa: E221
DIAMOND = 0b0010 # noqa: E221
FANOUT = 0b0100 # noqa: E221
FANOUT_RAISE = 0b1000
ALL = PER_BB | DIAMOND | FANOUT | FANOUT_RAISE
class PassManager(ffi.ObjectRef):
"""PassManager
"""
def _dispose(self):
self._capi.LLVMPY_DisposePassManager(self)
def add_aa_eval_pass(self):
"""
See https://llvm.org/docs/Passes.html#aa-eval-exhaustive-alias-analysis-precision-evaluator
LLVM 14: `llvm::createAAEvalPass`
""" # noqa E501
ffi.lib.LLVMPY_AddAAEvalPass(self)
def add_basic_aa_pass(self):
"""
See https://llvm.org/docs/Passes.html#basic-aa-basic-alias-analysis-stateless-aa-impl
LLVM 14: `llvm::createBasicAAWrapperPass`
""" # noqa E501
ffi.lib.LLVMPY_AddBasicAAWrapperPass(self)
def add_constant_merge_pass(self):
"""
See http://llvm.org/docs/Passes.html#constmerge-merge-duplicate-global-constants
LLVM 14: `LLVMAddConstantMergePass`
""" # noqa E501
ffi.lib.LLVMPY_AddConstantMergePass(self)
def add_dead_arg_elimination_pass(self):
"""
See http://llvm.org/docs/Passes.html#deadargelim-dead-argument-elimination
LLVM 14: `LLVMAddDeadArgEliminationPass`
""" # noqa E501
ffi.lib.LLVMPY_AddDeadArgEliminationPass(self)
def add_dependence_analysis_pass(self):
"""
See https://llvm.org/docs/Passes.html#da-dependence-analysis
LLVM 14: `llvm::createDependenceAnalysisWrapperPass`
""" # noqa E501
ffi.lib.LLVMPY_AddDependenceAnalysisPass(self)
def add_dot_call_graph_pass(self):
"""
See https://llvm.org/docs/Passes.html#dot-callgraph-print-call-graph-to-dot-file
LLVM 14: `llvm::createCallGraphDOTPrinterPass`
""" # noqa E501
ffi.lib.LLVMPY_AddCallGraphDOTPrinterPass(self)
def add_dot_cfg_printer_pass(self):
"""
See https://llvm.org/docs/Passes.html#dot-cfg-print-cfg-of-function-to-dot-file
LLVM 14: `llvm::createCFGPrinterLegacyPassPass`
""" # noqa E501
ffi.lib.LLVMPY_AddCFGPrinterPass(self)
def add_dot_dom_printer_pass(self, show_body=False):
"""
See https://llvm.org/docs/Passes.html#dot-dom-print-dominance-tree-of-function-to-dot-file
LLVM 14: `llvm::createDomPrinterPass` and `llvm::createDomOnlyPrinterPass`
""" # noqa E501
ffi.lib.LLVMPY_AddDotDomPrinterPass(self, show_body)
def add_dot_postdom_printer_pass(self, show_body=False):
"""
See https://llvm.org/docs/Passes.html#dot-postdom-print-postdominance-tree-of-function-to-dot-file
LLVM 14: `llvm::createPostDomPrinterPass` and `llvm::createPostDomOnlyPrinterPass`
""" # noqa E501
ffi.lib.LLVMPY_AddDotPostDomPrinterPass(self, show_body)
def add_globals_mod_ref_aa_pass(self):
"""
See https://llvm.org/docs/Passes.html#globalsmodref-aa-simple-mod-ref-analysis-for-globals
LLVM 14: `llvm::createGlobalsAAWrapperPass`
""" # noqa E501
ffi.lib.LLVMPY_AddGlobalsModRefAAPass(self)
def add_iv_users_pass(self):
"""
See https://llvm.org/docs/Passes.html#iv-users-induction-variable-users
LLVM 14: `llvm::createIVUsersPass`
""" # noqa E501
ffi.lib.LLVMPY_AddIVUsersPass(self)
def add_lint_pass(self):
"""
See https://llvm.org/docs/Passes.html#lint-statically-lint-checks-llvm-ir
LLVM 14: `llvm::createLintLegacyPassPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLintPass(self)
def add_lazy_value_info_pass(self):
"""
See https://llvm.org/docs/Passes.html#lazy-value-info-lazy-value-information-analysis
LLVM 14: `llvm::createLazyValueInfoPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLazyValueInfoPass(self)
def add_module_debug_info_pass(self):
"""
See https://llvm.org/docs/Passes.html#module-debuginfo-decodes-module-level-debug-info
LLVM 14: `llvm::createModuleDebugInfoPrinterPass`
""" # noqa E501
ffi.lib.LLVMPY_AddModuleDebugInfoPrinterPass(self)
def add_region_info_pass(self):
"""
See https://llvm.org/docs/Passes.html#regions-detect-single-entry-single-exit-regions
LLVM 14: `llvm::createRegionInfoPass`
""" # noqa E501
ffi.lib.LLVMPY_AddRegionInfoPass(self)
def add_scalar_evolution_aa_pass(self):
"""
See https://llvm.org/docs/Passes.html#scev-aa-scalarevolution-based-alias-analysis
LLVM 14: `llvm::createSCEVAAWrapperPass`
""" # noqa E501
ffi.lib.LLVMPY_AddScalarEvolutionAAPass(self)
def add_aggressive_dead_code_elimination_pass(self):
"""
See https://llvm.org/docs/Passes.html#adce-aggressive-dead-code-elimination
LLVM 14: `llvm::createAggressiveDCEPass`
""" # noqa E501
ffi.lib.LLVMPY_AddAggressiveDCEPass(self)
def add_always_inliner_pass(self, insert_lifetime=True):
"""
See https://llvm.org/docs/Passes.html#always-inline-inliner-for-always-inline-functions
LLVM 14: `llvm::createAlwaysInlinerLegacyPass`
""" # noqa E501
ffi.lib.LLVMPY_AddAlwaysInlinerPass(self, insert_lifetime)
def add_arg_promotion_pass(self, max_elements=3):
"""
See https://llvm.org/docs/Passes.html#argpromotion-promote-by-reference-arguments-to-scalars
LLVM 14: `llvm::createArgumentPromotionPass`
""" # noqa E501
ffi.lib.LLVMPY_AddArgPromotionPass(self, max_elements)
def add_break_critical_edges_pass(self):
"""
See https://llvm.org/docs/Passes.html#break-crit-edges-break-critical-edges-in-cfg
LLVM 14: `llvm::createBreakCriticalEdgesPass`
""" # noqa E501
ffi.lib.LLVMPY_AddBreakCriticalEdgesPass(self)
def add_dead_store_elimination_pass(self):
"""
See https://llvm.org/docs/Passes.html#dse-dead-store-elimination
LLVM 14: `llvm::createDeadStoreEliminationPass`
""" # noqa E501
ffi.lib.LLVMPY_AddDeadStoreEliminationPass(self)
def add_reverse_post_order_function_attrs_pass(self):
"""
See https://llvm.org/docs/Passes.html#function-attrs-deduce-function-attributes
LLVM 14: `llvm::createReversePostOrderFunctionAttrsPass`
""" # noqa E501
ffi.lib.LLVMPY_AddReversePostOrderFunctionAttrsPass(self)
def add_function_attrs_pass(self):
"""
See http://llvm.org/docs/Passes.html#functionattrs-deduce-function-attributes
LLVM 14: `LLVMAddFunctionAttrsPass`
""" # noqa E501
ffi.lib.LLVMPY_AddFunctionAttrsPass(self)
def add_function_inlining_pass(self, threshold):
"""
See http://llvm.org/docs/Passes.html#inline-function-integration-inlining
LLVM 14: `createFunctionInliningPass`
""" # noqa E501
ffi.lib.LLVMPY_AddFunctionInliningPass(self, threshold)
def add_global_dce_pass(self):
"""
See http://llvm.org/docs/Passes.html#globaldce-dead-global-elimination
LLVM 14: `LLVMAddGlobalDCEPass`
""" # noqa E501
ffi.lib.LLVMPY_AddGlobalDCEPass(self)
def add_global_optimizer_pass(self):
"""
See http://llvm.org/docs/Passes.html#globalopt-global-variable-optimizer
LLVM 14: `LLVMAddGlobalOptimizerPass`
""" # noqa E501
ffi.lib.LLVMPY_AddGlobalOptimizerPass(self)
def add_ipsccp_pass(self):
"""
See http://llvm.org/docs/Passes.html#ipsccp-interprocedural-sparse-conditional-constant-propagation
LLVM 14: `LLVMAddIPSCCPPass`
""" # noqa E501
ffi.lib.LLVMPY_AddIPSCCPPass(self)
def add_dead_code_elimination_pass(self):
"""
See http://llvm.org/docs/Passes.html#dce-dead-code-elimination
LLVM 14: `llvm::createDeadCodeEliminationPass`
"""
ffi.lib.LLVMPY_AddDeadCodeEliminationPass(self)
def add_aggressive_instruction_combining_pass(self):
"""
See https://llvm.org/docs/Passes.html#aggressive-instcombine-combine-expression-patterns
LLVM 14: `llvm::createAggressiveInstCombinerPass`
""" # noqa E501
ffi.lib.LLVMPY_AddAggressiveInstructionCombiningPass(self)
def add_internalize_pass(self):
"""
See https://llvm.org/docs/Passes.html#internalize-internalize-global-symbols
LLVM 14: `llvm::createInternalizePass`
""" # noqa E501
ffi.lib.LLVMPY_AddInternalizePass(self)
def add_cfg_simplification_pass(self):
"""
See http://llvm.org/docs/Passes.html#simplifycfg-simplify-the-cfg
LLVM 14: `LLVMAddCFGSimplificationPass`
"""
ffi.lib.LLVMPY_AddCFGSimplificationPass(self)
def add_jump_threading_pass(self, threshold=-1):
"""
See https://llvm.org/docs/Passes.html#jump-threading-jump-threading
LLVM 14: `llvm::createJumpThreadingPass`
""" # noqa E501
ffi.lib.LLVMPY_AddJumpThreadingPass(self, threshold)
def add_lcssa_pass(self):
"""
See https://llvm.org/docs/Passes.html#lcssa-loop-closed-ssa-form-pass
LLVM 14: `llvm::createLCSSAPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLCSSAPass(self)
def add_gvn_pass(self):
"""
See http://llvm.org/docs/Passes.html#gvn-global-value-numbering
LLVM 14: `LLVMAddGVNPass`
"""
ffi.lib.LLVMPY_AddGVNPass(self)
def add_instruction_combining_pass(self):
"""
See http://llvm.org/docs/Passes.html#passes-instcombine
LLVM 14: `LLVMAddInstructionCombiningPass`
"""
ffi.lib.LLVMPY_AddInstructionCombiningPass(self)
def add_licm_pass(self):
"""
See http://llvm.org/docs/Passes.html#licm-loop-invariant-code-motion
LLVM 14: `LLVMAddLICMPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLICMPass(self)
def add_loop_deletion_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-deletion-delete-dead-loops
LLVM 14: `llvm::createLoopDeletionPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopDeletionPass(self)
def add_loop_extractor_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-extract-extract-loops-into-new-functions
LLVM 14: `llvm::createLoopExtractorPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopExtractorPass(self)
def add_single_loop_extractor_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-extract-single-extract-at-most-one-loop-into-a-new-function
LLVM 14: `llvm::createSingleLoopExtractorPass`
""" # noqa E501
ffi.lib.LLVMPY_AddSingleLoopExtractorPass(self)
def add_sccp_pass(self):
"""
See http://llvm.org/docs/Passes.html#sccp-sparse-conditional-constant-propagation
LLVM 14: `LLVMAddSCCPPass`
""" # noqa E501
ffi.lib.LLVMPY_AddSCCPPass(self)
def add_loop_strength_reduce_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-reduce-loop-strength-reduction
LLVM 14: `llvm::createLoopStrengthReducePass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopStrengthReducePass(self)
def add_loop_simplification_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-simplify-canonicalize-natural-loops
LLVM 14: `llvm::createLoopSimplifyPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopSimplificationPass(self)
def add_loop_unroll_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-unroll-unroll-loops
LLVM 14: `LLVMAddLoopUnrollPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopUnrollPass(self)
def add_loop_unroll_and_jam_pass(self):
"""
See https://llvm.org/docs/Passes.html#loop-unroll-and-jam-unroll-and-jam-loops
LLVM 14: `LLVMAddLoopUnrollAndJamPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopUnrollAndJamPass(self)
def add_loop_unswitch_pass(self,
optimize_for_size=False,
has_branch_divergence=False):
"""
See https://llvm.org/docs/Passes.html#loop-unswitch-unswitch-loops
LLVM 14: `llvm::createLoopUnswitchPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLoopUnswitchPass(self,
optimize_for_size,
has_branch_divergence)
def add_lower_atomic_pass(self):
"""
See https://llvm.org/docs/Passes.html#loweratomic-lower-atomic-intrinsics-to-non-atomic-form
LLVM 14: `llvm::createLowerAtomicPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLowerAtomicPass(self)
def add_lower_invoke_pass(self):
"""
See https://llvm.org/docs/Passes.html#lowerinvoke-lower-invokes-to-calls-for-unwindless-code-generators
LLVM 14: `llvm::createLowerInvokePass`
""" # noqa E501
ffi.lib.LLVMPY_AddLowerInvokePass(self)
def add_lower_switch_pass(self):
"""
See https://llvm.org/docs/Passes.html#lowerswitch-lower-switchinsts-to-branches
LLVM 14: `llvm::createLowerSwitchPass`
""" # noqa E501
ffi.lib.LLVMPY_AddLowerSwitchPass(self)
def add_memcpy_optimization_pass(self):
"""
See https://llvm.org/docs/Passes.html#memcpyopt-memcpy-optimization
LLVM 14: `llvm::createMemCpyOptPass`
""" # noqa E501
ffi.lib.LLVMPY_AddMemCpyOptimizationPass(self)
def add_merge_functions_pass(self):
"""
See https://llvm.org/docs/Passes.html#mergefunc-merge-functions
LLVM 14: `llvm::createMergeFunctionsPass`
""" # noqa E501
ffi.lib.LLVMPY_AddMergeFunctionsPass(self)
def add_merge_returns_pass(self):
"""
See https://llvm.org/docs/Passes.html#mergereturn-unify-function-exit-nodes
LLVM 14: `llvm::createUnifyFunctionExitNodesPass`
""" # noqa E501
ffi.lib.LLVMPY_AddMergeReturnsPass(self)
def add_partial_inlining_pass(self):
"""
See https://llvm.org/docs/Passes.html#partial-inliner-partial-inliner
LLVM 14: `llvm::createPartialInliningPass`
""" # noqa E501
ffi.lib.LLVMPY_AddPartialInliningPass(self)
def add_prune_exception_handling_pass(self):
"""
See https://llvm.org/docs/Passes.html#prune-eh-remove-unused-exception-handling-info
LLVM 14: `llvm::createPruneEHPass`
""" # noqa E501
ffi.lib.LLVMPY_AddPruneExceptionHandlingPass(self)
def add_reassociate_expressions_pass(self):
"""
See https://llvm.org/docs/Passes.html#reassociate-reassociate-expressions
LLVM 14: `llvm::createReassociatePass`
""" # noqa E501
ffi.lib.LLVMPY_AddReassociatePass(self)
def add_demote_register_to_memory_pass(self):
"""
See https://llvm.org/docs/Passes.html#rel-lookup-table-converter-relative-lookup-table-converter
LLVM 14: `llvm::createDemoteRegisterToMemoryPass`
""" # noqa E501
ffi.lib.LLVMPY_AddDemoteRegisterToMemoryPass(self)
def add_sroa_pass(self):
"""
See http://llvm.org/docs/Passes.html#scalarrepl-scalar-replacement-of-aggregates-dt
Note that this pass corresponds to the ``opt -sroa`` command-line option,
despite the link above.
LLVM 14: `llvm::createSROAPass`
""" # noqa E501
ffi.lib.LLVMPY_AddSROAPass(self)
def add_sink_pass(self):
"""
See https://llvm.org/docs/Passes.html#sink-code-sinking
LLVM 14: `llvm::createSinkingPass`
""" # noqa E501
ffi.lib.LLVMPY_AddSinkPass(self)
def add_strip_symbols_pass(self, only_debug=False):
"""
See https://llvm.org/docs/Passes.html#strip-strip-all-symbols-from-a-module
LLVM 14: `llvm::createStripSymbolsPass`
""" # noqa E501
ffi.lib.LLVMPY_AddStripSymbolsPass(self, only_debug)
def add_strip_dead_debug_info_pass(self):
"""
See https://llvm.org/docs/Passes.html#strip-dead-debug-info-strip-debug-info-for-unused-symbols
LLVM 14: `llvm::createStripDeadDebugInfoPass`
""" # noqa E501
ffi.lib.LLVMPY_AddStripDeadDebugInfoPass(self)
def add_strip_dead_prototypes_pass(self):
"""
See https://llvm.org/docs/Passes.html#strip-dead-prototypes-strip-unused-function-prototypes
LLVM 14: `llvm::createStripDeadPrototypesPass`
""" # noqa E501
ffi.lib.LLVMPY_AddStripDeadPrototypesPass(self)
def add_strip_debug_declare_pass(self):
"""
See https://llvm.org/docs/Passes.html#strip-debug-declare-strip-all-llvm-dbg-declare-intrinsics
LLVM 14: `llvm::createStripDebugDeclarePass`
""" # noqa E501
ffi.lib.LLVMPY_AddStripDebugDeclarePrototypesPass(self)
def add_strip_nondebug_symbols_pass(self):
"""
See https://llvm.org/docs/Passes.html#strip-nondebug-strip-all-symbols-except-dbg-symbols-from-a-module
LLVM 14: `llvm::createStripNonDebugSymbolsPass`
""" # noqa E501
ffi.lib.LLVMPY_AddStripNondebugSymbolsPass(self)
def add_tail_call_elimination_pass(self):
"""
See https://llvm.org/docs/Passes.html#tailcallelim-tail-call-elimination
LLVM 14: `llvm::createTailCallEliminationPass`
""" # noqa E501
ffi.lib.LLVMPY_AddTailCallEliminationPass(self)
def add_type_based_alias_analysis_pass(self):
"""
LLVM 14: `LLVMAddTypeBasedAliasAnalysisPass`
""" # noqa E501
ffi.lib.LLVMPY_AddTypeBasedAliasAnalysisPass(self)
def add_basic_alias_analysis_pass(self):
"""
See http://llvm.org/docs/AliasAnalysis.html#the-basicaa-pass
LLVM 14: `LLVMAddBasicAliasAnalysisPass`
"""
ffi.lib.LLVMPY_AddBasicAliasAnalysisPass(self)
def add_loop_rotate_pass(self):
"""http://llvm.org/docs/Passes.html#loop-rotate-rotate-loops."""
ffi.lib.LLVMPY_LLVMAddLoopRotatePass(self)
def add_target_library_info(self, triple):
ffi.lib.LLVMPY_AddTargetLibraryInfoPass(self, _encode_string(triple))
# Non-standard LLVM passes
def add_refprune_pass(self, subpasses_flags=RefPruneSubpasses.ALL,
subgraph_limit=1000):
"""Add Numba specific Reference count pruning pass.
Parameters
----------
subpasses_flags : RefPruneSubpasses
A bitmask to control the subpasses to be enabled.
subgraph_limit : int
Limit the fanout pruners to working on a subgraph no bigger than
this number of basic-blocks to avoid spending too much time in very
large graphs. Default is 1000. Subject to change in future
versions.
"""
iflags = RefPruneSubpasses(subpasses_flags)
ffi.lib.LLVMPY_AddRefPrunePass(self, iflags, subgraph_limit)
class ModulePassManager(PassManager):
def __init__(self, ptr=None):
if ptr is None:
ptr = ffi.lib.LLVMPY_CreatePassManager()
PassManager.__init__(self, ptr)
def run(self, module, remarks_file=None, remarks_format='yaml',
remarks_filter=''):
"""
Run optimization passes on the given module.
Parameters
----------
module : llvmlite.binding.ModuleRef
The module to be optimized inplace
remarks_file : str; optional
If not `None`, it is the file to store the optimization remarks.
remarks_format : str; optional
The format to write; YAML is default
remarks_filter : str; optional
The filter that should be applied to the remarks output.
"""
if remarks_file is None:
return ffi.lib.LLVMPY_RunPassManager(self, module)
else:
r = ffi.lib.LLVMPY_RunPassManagerWithRemarks(
self, module, _encode_string(remarks_format),
_encode_string(remarks_filter),
_encode_string(remarks_file))
if r == -1:
raise IOError("Failed to initialize remarks file.")
return r > 0
def run_with_remarks(self, module, remarks_format='yaml',
remarks_filter=''):
"""
Run optimization passes on the given module and returns the result and
the remarks data.
Parameters
----------
module : llvmlite.binding.ModuleRef
The module to be optimized
remarks_format : str
The remarks output; YAML is the default
remarks_filter : str; optional
The filter that should be applied to the remarks output.
"""
remarkdesc, remarkfile = mkstemp()
try:
with os.fdopen(remarkdesc, 'r'):
pass
r = self.run(module, remarkfile, remarks_format, remarks_filter)
if r == -1:
raise IOError("Failed to initialize remarks file.")
with open(remarkfile) as f:
return bool(r), f.read()
finally:
os.unlink(remarkfile)
class FunctionPassManager(PassManager):
def __init__(self, module):
ptr = ffi.lib.LLVMPY_CreateFunctionPassManager(module)
self._module = module
module._owned = True
PassManager.__init__(self, ptr)
def initialize(self):
"""
Initialize the FunctionPassManager. Returns True if it produced
any changes (?).
"""
return ffi.lib.LLVMPY_InitializeFunctionPassManager(self)
def finalize(self):
"""
Finalize the FunctionPassManager. Returns True if it produced
any changes (?).
"""
return ffi.lib.LLVMPY_FinalizeFunctionPassManager(self)
def run(self, function, remarks_file=None, remarks_format='yaml',
remarks_filter=''):
"""
Run optimization passes on the given function.
Parameters
----------
function : llvmlite.binding.FunctionRef
The function to be optimized inplace
remarks_file : str; optional
If not `None`, it is the file to store the optimization remarks.
remarks_format : str; optional
The format of the remarks file; the default is YAML
remarks_filter : str; optional
The filter that should be applied to the remarks output.
"""
if remarks_file is None:
return ffi.lib.LLVMPY_RunFunctionPassManager(self, function)
else:
r = ffi.lib.LLVMPY_RunFunctionPassManagerWithRemarks(
self, function, _encode_string(remarks_format),
_encode_string(remarks_filter),
_encode_string(remarks_file))
if r == -1:
raise IOError("Failed to initialize remarks file.")
return bool(r)
def run_with_remarks(self, function, remarks_format='yaml',
remarks_filter=''):
"""
Run optimization passes on the given function and returns the result
and the remarks data.
Parameters
----------
function : llvmlite.binding.FunctionRef
The function to be optimized inplace
remarks_format : str; optional
The format of the remarks file; the default is YAML
remarks_filter : str; optional
The filter that should be applied to the remarks output.
"""
# LLVM is going to need to close this file and then reopen it, so we
# can't use an unlinked temporary file.
remarkdesc, remarkfile = mkstemp()
try:
# We get an open handle, but we need LLVM to write first, so close
# it.
with os.fdopen(remarkdesc, 'r'):
pass
r = self.run(function, remarkfile, remarks_format, remarks_filter)
if r == -1:
raise IOError("Failed to initialize remarks file.")
with open(remarkfile) as f:
return bool(r), f.read()
finally:
os.unlink(remarkfile)
# ============================================================================
# FFI
ffi.lib.LLVMPY_CreatePassManager.restype = ffi.LLVMPassManagerRef
ffi.lib.LLVMPY_CreateFunctionPassManager.argtypes = [ffi.LLVMModuleRef]
ffi.lib.LLVMPY_CreateFunctionPassManager.restype = ffi.LLVMPassManagerRef
ffi.lib.LLVMPY_DisposePassManager.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_RunPassManager.argtypes = [ffi.LLVMPassManagerRef,
ffi.LLVMModuleRef]
ffi.lib.LLVMPY_RunPassManager.restype = c_bool
ffi.lib.LLVMPY_RunPassManagerWithRemarks.argtypes = [ffi.LLVMPassManagerRef,
ffi.LLVMModuleRef,
c_char_p,
c_char_p,
c_char_p]
ffi.lib.LLVMPY_RunPassManagerWithRemarks.restype = c_int
ffi.lib.LLVMPY_InitializeFunctionPassManager.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_InitializeFunctionPassManager.restype = c_bool
ffi.lib.LLVMPY_FinalizeFunctionPassManager.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_FinalizeFunctionPassManager.restype = c_bool
ffi.lib.LLVMPY_RunFunctionPassManager.argtypes = [ffi.LLVMPassManagerRef,
ffi.LLVMValueRef]
ffi.lib.LLVMPY_RunFunctionPassManager.restype = c_bool
ffi.lib.LLVMPY_RunFunctionPassManagerWithRemarks.argtypes = [
ffi.LLVMPassManagerRef, ffi.LLVMValueRef, c_char_p, c_char_p, c_char_p
]
ffi.lib.LLVMPY_RunFunctionPassManagerWithRemarks.restype = c_int
ffi.lib.LLVMPY_AddAAEvalPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddBasicAAWrapperPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddConstantMergePass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddDeadArgEliminationPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddDependenceAnalysisPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddCallGraphDOTPrinterPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddCFGPrinterPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddDotDomPrinterPass.argtypes = [ffi.LLVMPassManagerRef, c_bool]
ffi.lib.LLVMPY_AddDotPostDomPrinterPass.argtypes = [
ffi.LLVMPassManagerRef,
c_bool]
ffi.lib.LLVMPY_AddGlobalsModRefAAPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddInstructionCountPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddIVUsersPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLazyValueInfoPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLintPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddModuleDebugInfoPrinterPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddRegionInfoPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddScalarEvolutionAAPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddAggressiveDCEPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddAlwaysInlinerPass.argtypes = [ffi.LLVMPassManagerRef, c_bool]
ffi.lib.LLVMPY_AddArgPromotionPass.argtypes = [ffi.LLVMPassManagerRef, c_uint]
ffi.lib.LLVMPY_AddBreakCriticalEdgesPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddDeadStoreEliminationPass.argtypes = [
ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddReversePostOrderFunctionAttrsPass.argtypes = [
ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddAggressiveInstructionCombiningPass.argtypes = [
ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddInternalizePass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLCSSAPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopDeletionPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopExtractorPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddSingleLoopExtractorPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopStrengthReducePass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopSimplificationPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopUnrollPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopUnrollAndJamPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLoopUnswitchPass.argtypes = [
ffi.LLVMPassManagerRef,
c_bool,
c_bool]
ffi.lib.LLVMPY_AddLowerAtomicPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLowerInvokePass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLowerSwitchPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddMemCpyOptimizationPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddMergeFunctionsPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddMergeReturnsPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddPartialInliningPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddPruneExceptionHandlingPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddReassociatePass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddDemoteRegisterToMemoryPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddSinkPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddStripSymbolsPass.argtypes = [ffi.LLVMPassManagerRef, c_bool]
ffi.lib.LLVMPY_AddStripDeadDebugInfoPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddStripDeadPrototypesPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddStripDebugDeclarePrototypesPass.argtypes = [
ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddStripNondebugSymbolsPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddTailCallEliminationPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddJumpThreadingPass.argtypes = [ffi.LLVMPassManagerRef, c_int]
ffi.lib.LLVMPY_AddFunctionAttrsPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddFunctionInliningPass.argtypes = [
ffi.LLVMPassManagerRef, c_int]
ffi.lib.LLVMPY_AddGlobalDCEPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddGlobalOptimizerPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddIPSCCPPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddDeadCodeEliminationPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddCFGSimplificationPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddGVNPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddInstructionCombiningPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddLICMPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddSCCPPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddSROAPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddTypeBasedAliasAnalysisPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddBasicAliasAnalysisPass.argtypes = [ffi.LLVMPassManagerRef]
ffi.lib.LLVMPY_AddTargetLibraryInfoPass.argtypes = [ffi.LLVMPassManagerRef,
c_char_p]
ffi.lib.LLVMPY_AddRefPrunePass.argtypes = [ffi.LLVMPassManagerRef, c_int,
c_size_t]
ffi.lib.LLVMPY_DumpRefPruneStats.argtypes = [POINTER(_c_PruneStats), c_bool]
|