Skip to content

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 23, 2025

Stack from ghstack (oldest at bottom):

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-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
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
lanza added a commit that referenced this pull request Nov 24, 2025
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
[ghstack-poisoned]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants