You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: 8c51a5b
Pull-Request: #2012
0 commit comments