Skip to content

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 22, 2025

Stack from ghstack (oldest at bottom):

This commit adds the foundational support for generating C++ thunks
needed for multiple inheritance, particularly for virtual function calls
through base class pointers. The implementation closely follows CodeGen.

Key changes:

  1. Fix VTable double-insertion bug in createGlobalOp/createCIRFunction:

    • Clear insertion point before calling create() to prevent auto-insertion
    • Manually insert operations at correct location
    • This was a pre-existing bug blocking VTable generation during thunk emission
  2. Implement thunk generation infrastructure matching CodeGen:

    • Add startThunk() matching CodeGen's StartThunk()
    • Add generateThunk() matching CodeGen's generateThunk()
    • Add emitCallAndReturnForThunk() stub matching CodeGen's EmitCallAndReturnForThunk()
    • Add emitMustTailThunk() stub matching CodeGen's EmitMustTailThunk()
    • Implement finishThunk() matching CodeGen's FinishThunk() for cleanup
    • Enable thunk handling in addVTableComponent() for VTable emission
    • Call generateThunk() from maybeEmitThunk()
  3. Make StartFunction null-safe for thunks:

    • Add null checks for GlobalDecl in XRay attributes
    • Guard function body access for thunks (which have no AST body)
  4. Handle thunk attributes in setFunctionAttributes:

    • Remove assertion blocking thunks
    • Allow thunks to proceed through attribute setting
  5. Add comprehensive test coverage:

    • clang/test/CIR/CodeGen/vtable-thunk.cpp - CIR output test with specific offset verification
    • clang/test/CIR/Lowering/vtable-thunk.cpp - LLVM lowering test with correct offset
    • clang/test/CIR/CodeGen/vtable-thunk-compare-codegen.cpp - CodeGen comparison test (XFAIL until thunk bodies implemented)

Note: Thunk bodies are marked with llvm_unreachable("NYI") placeholders.
Full thunk body emission (this adjustment, call forwarding, return adjustment)
will be implemented in follow-up commits.

[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 22, 2025
This commit adds the foundational support for generating C++ thunks
needed for multiple inheritance, particularly for virtual function calls
through base class pointers. The implementation closely follows CodeGen.

Key changes:

1. Fix VTable double-insertion bug in createGlobalOp/createCIRFunction:
   - Clear insertion point before calling create() to prevent auto-insertion
   - Manually insert operations at correct location
   - This was a pre-existing bug blocking VTable generation during thunk emission

2. Implement thunk generation infrastructure matching CodeGen:
   - Add startThunk() matching CodeGen's StartThunk()
   - Add generateThunk() matching CodeGen's generateThunk()
   - Add emitCallAndReturnForThunk() stub matching CodeGen's EmitCallAndReturnForThunk()
   - Add emitMustTailThunk() stub matching CodeGen's EmitMustTailThunk()
   - Implement finishThunk() matching CodeGen's FinishThunk() for cleanup
   - Enable thunk handling in addVTableComponent() for VTable emission
   - Call generateThunk() from maybeEmitThunk()

3. Make StartFunction null-safe for thunks:
   - Add null checks for GlobalDecl in XRay attributes
   - Guard function body access for thunks (which have no AST body)

4. Handle thunk attributes in setFunctionAttributes:
   - Remove assertion blocking thunks
   - Allow thunks to proceed through attribute setting

5. Add comprehensive test coverage:
   - clang/test/CIR/CodeGen/vtable-thunk.cpp - CIR output test with specific offset verification
   - clang/test/CIR/Lowering/vtable-thunk.cpp - LLVM lowering test with correct offset
   - clang/test/CIR/CodeGen/vtable-thunk-compare-codegen.cpp - CodeGen comparison test (XFAIL until thunk bodies implemented)

Note: Thunk bodies are marked with llvm_unreachable("NYI") placeholders.
Full thunk body emission (this adjustment, call forwarding, return adjustment)
will be implemented in follow-up commits.


ghstack-source-id: 933ad05
Pull-Request: #2003
@lanza lanza closed this Nov 23, 2025
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