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
[CIR] Implement non-virtual thunk generation for multiple inheritance
This patch implements complete support for generating non-virtual thunks
in ClangIR for multiple inheritance scenarios. Thunks are small adapter
functions that adjust the 'this' pointer and forward calls to the actual
target function when virtual functions are overridden in derived classes
with multiple base classes.
Implementation details:
1. **CIRGenVTables.cpp**:
- Implemented `generateThunk()` to create thunk function bodies with
proper scope management (SymTableScopeTy + LexicalScope)
- Implemented `emitCallAndReturnForThunk()` to emit the call sequence
- Implemented `setThunkProperties()` to set linkage and properties
- Fixed VTable double-insertion bug in vtable emission
2. **CIRGenCXXABI.h/cpp**:
- Added virtual methods `performThisAdjustment()` and
`performReturnAdjustment()` to base class
- Implemented `EmitReturnFromThunk()` default implementation
3. **CIRGenItaniumCXXABI.cpp**:
- Implemented `performThisAdjustment()` for non-virtual adjustments
using cir.ptr_stride operations
- Implemented `performReturnAdjustment()` for covariant return types
- Virtual adjustments marked as NYI with llvm_unreachable
4. **CIRGenFunction.h/cpp**:
- Added `startThunk()`, `generateThunk()`, `emitCallAndReturnForThunk()`,
`finishThunk()`, and `emitMustTailThunk()` methods
- Implemented `finishThunk()` to properly cleanup after thunk generation
5. **CIRGenModule.cpp**:
- Removed assertion blocking thunks in `setFunctionAttributes()`
- Fixed insertion point management in vtable creation
6. **CIRGenCXX.cpp**:
- Added null checks for GlobalDecl in XRay attributes for thunks
7. **Test Updates**:
- Added `clang/test/CIR/CodeGen/vtable-thunk.cpp` for CIR generation
- Added `clang/test/CIR/Lowering/vtable-thunk.cpp` for LLVM lowering
- Fixed offset expectations (16 bytes for x86_64 layout)
- Reordered FileCheck patterns to match actual CIR output order
This implementation closely follows CodeGen's approach in
CodeGenVTables.cpp and CodeGenItaniumCXXABI.cpp, with adaptations for
CIR's MLIR-based infrastructure (e.g., location requirements, operation
builders, scope management).
Limitations:
- Virtual adjustments (vcall offsets) not yet implemented
- Musttail thunks for variadic functions not yet implemented
- Return adjustments implemented but not extensively tested
Test Plan:
```
bin/llvm-lit -v ../../clang/test/CIR/CodeGen/vtable-thunk.cpp
bin/llvm-lit -v ../../clang/test/CIR/Lowering/vtable-thunk.cpp
```
Both tests pass, verifying:
- CIR generation with correct thunk mangling and operations
- Vtable contains thunk references
- Lowering to LLVM IR produces correct getelementptr operations
ghstack-source-id: 16477e8
Pull-Request: #2006
0 commit comments