Skip to content

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 23, 2025

Stack from ghstack (oldest at bottom):

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.

[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
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: eaf1735
Pull-Request: #2006
[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
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
[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
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: 62c915d
Pull-Request: #2006
[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
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: d6515e1
Pull-Request: #2006
[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 23, 2025
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: ae6fa6a
Pull-Request: #2006
[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 24, 2025
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: eb03968
Pull-Request: #2006
[ghstack-poisoned]
@lanza lanza marked this pull request as draft November 24, 2025 18:39
lanza added a commit that referenced this pull request Nov 25, 2025
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: eb03968
Pull-Request: #2006
lanza added a commit that referenced this pull request Nov 25, 2025
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: eb03968
Pull-Request: #2006
lanza added a commit that referenced this pull request Nov 26, 2025
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: eb03968
Pull-Request: #2006
lanza added a commit that referenced this pull request Nov 26, 2025
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: eb03968
Pull-Request: #2006
[ghstack-poisoned]
lanza added a commit that referenced this pull request Nov 26, 2025
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.

ghstack-source-id: a80ef29
Pull-Request: #2006
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