This repository was archived by the owner on Feb 21, 2026. It is now read-only.
Commit 4c54818
committed
[CIR] Add support for GNU ifunc attribute
This patch implements support for the GNU indirect function (ifunc)
attribute in ClangIR, enabling runtime CPU feature detection and
function dispatch.
Background:
The ifunc attribute is a GNU extension that allows selecting function
implementations at runtime based on CPU capabilities. A resolver
function is called at program startup to return a pointer to the
appropriate implementation.
Implementation:
- Added cir.func.ifunc operation to CIROps.td to represent ifunc
declarations in CIR with a resolver function reference
- Implemented emitIFuncDefinition() in CIRGenModule to generate
cir.func.ifunc operations from AST IFuncDecls
- Extended GetOrCreateCIRFunction() to handle ifunc lookups and
create/retrieve IFuncOp operations
- Updated ReplaceUsesOfNonProtoTypeWithRealFunction() to support
replacing ifunc operations when prototypes change
- Modified emitDirectCallee() to allow calls through IFuncOp
- Added CallOp verifier support to accept IFuncOp as valid callee
- Implemented CIRToLLVMIFuncOpLowering to lower cir.func.ifunc to
LLVM dialect IFuncOp, using ptr type for resolver_type parameter
The implementation closely follows the original CodeGen approach for
generating ifunc declarations, adapted to MLIR's operation-based model.
Testing:
Added comprehensive test coverage in clang/test/CIR/CodeGen/ifunc.c
with three scenarios:
1. Basic ifunc with simple resolver
2. Multiple implementations with function pointer typedef
3. Extern declaration followed by ifunc definition
Tests verify:
- CIR emission produces correct cir.func.ifunc operations
- Direct-to-LLVM lowering generates proper ifunc declarations
- Output matches original CodeGen behavior
Test Plan:
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/ifunc.c
PASS: Clang :: CIR/CodeGen/ifunc.c (1 of 1)
ghstack-source-id: 3c37345
Pull-Request: #20121 parent cd117db commit 4c54818
File tree
10 files changed
+327
-16
lines changed- clang
- include/clang/CIR/Dialect
- Builder
- IR
- lib/CIR
- CodeGen
- Dialect/IR
- Lowering/DirectToLLVM
- test/CIR/CodeGen
10 files changed
+327
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
719 | 719 | | |
720 | 720 | | |
721 | 721 | | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
722 | 732 | | |
723 | 733 | | |
724 | 734 | | |
| |||
775 | 785 | | |
776 | 786 | | |
777 | 787 | | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
778 | 799 | | |
779 | 800 | | |
780 | 801 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2596 | 2596 | | |
2597 | 2597 | | |
2598 | 2598 | | |
| 2599 | + | |
| 2600 | + | |
| 2601 | + | |
| 2602 | + | |
| 2603 | + | |
| 2604 | + | |
| 2605 | + | |
| 2606 | + | |
| 2607 | + | |
| 2608 | + | |
| 2609 | + | |
| 2610 | + | |
| 2611 | + | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
2599 | 2646 | | |
2600 | 2647 | | |
2601 | 2648 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
324 | | - | |
| 324 | + | |
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
381 | 384 | | |
| 385 | + | |
382 | 386 | | |
383 | | - | |
| 387 | + | |
384 | 388 | | |
385 | 389 | | |
386 | 390 | | |
| |||
405 | 409 | | |
406 | 410 | | |
407 | 411 | | |
408 | | - | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
409 | 418 | | |
410 | 419 | | |
411 | 420 | | |
| |||
620 | 629 | | |
621 | 630 | | |
622 | 631 | | |
623 | | - | |
| 632 | + | |
624 | 633 | | |
625 | 634 | | |
626 | | - | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
627 | 638 | | |
628 | 639 | | |
629 | 640 | | |
630 | 641 | | |
631 | 642 | | |
632 | 643 | | |
633 | 644 | | |
634 | | - | |
635 | | - | |
| 645 | + | |
| 646 | + | |
636 | 647 | | |
637 | 648 | | |
638 | 649 | | |
| |||
648 | 659 | | |
649 | 660 | | |
650 | 661 | | |
651 | | - | |
| 662 | + | |
652 | 663 | | |
653 | 664 | | |
654 | 665 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
576 | 576 | | |
577 | 577 | | |
578 | 578 | | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
579 | 588 | | |
580 | 589 | | |
581 | 590 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
598 | 598 | | |
599 | 599 | | |
600 | 600 | | |
601 | | - | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
602 | 605 | | |
603 | 606 | | |
604 | 607 | | |
| |||
722 | 725 | | |
723 | 726 | | |
724 | 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 | + | |
725 | 799 | | |
726 | 800 | | |
727 | 801 | | |
| |||
2432 | 2506 | | |
2433 | 2507 | | |
2434 | 2508 | | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
2435 | 2513 | | |
2436 | 2514 | | |
2437 | 2515 | | |
| |||
3139 | 3217 | | |
3140 | 3218 | | |
3141 | 3219 | | |
| 3220 | + | |
| 3221 | + | |
| 3222 | + | |
| 3223 | + | |
| 3224 | + | |
3142 | 3225 | | |
3143 | 3226 | | |
3144 | 3227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
783 | 783 | | |
784 | 784 | | |
785 | 785 | | |
| 786 | + | |
786 | 787 | | |
787 | 788 | | |
788 | 789 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3134 | 3134 | | |
3135 | 3135 | | |
3136 | 3136 | | |
| 3137 | + | |
3137 | 3138 | | |
3138 | | - | |
| 3139 | + | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
3139 | 3143 | | |
3140 | 3144 | | |
| 3145 | + | |
3141 | 3146 | | |
3142 | 3147 | | |
3143 | 3148 | | |
| 3149 | + | |
| 3150 | + | |
| 3151 | + | |
3144 | 3152 | | |
3145 | 3153 | | |
3146 | | - | |
3147 | | - | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
3148 | 3157 | | |
3149 | 3158 | | |
3150 | 3159 | | |
| |||
3161 | 3170 | | |
3162 | 3171 | | |
3163 | 3172 | | |
3164 | | - | |
3165 | | - | |
| 3173 | + | |
| 3174 | + | |
| 3175 | + | |
3166 | 3176 | | |
3167 | 3177 | | |
3168 | 3178 | | |
| |||
0 commit comments