Skip to content

Commit 4240a25

Browse files
author
Andres Madrid Ucha
committed
Clang: Fix elementtype attribute mismatch in thunk generation
Previously, CurrentThisTy was assigned from FnInfo, which could contain type information that was not updated. This caused a type mismatch between the function's parameter type and the elementtype attribute, specially in multiple-inheritance cases. Now we extract CurrentThisTy directly from the actual thunk function parameter, so that they are both consistent. We need to make sure that the thunk function has arguments, otherwise there can be issues accessing their type. Only typed pointers can be handled this way because for opaque pointers, the getPointerElementType() is depricated. Therefore, they get handled in the previous way in which we assigned CurrentThisTy
1 parent 8907c03 commit 4240a25

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

clang/lib/CodeGen/CGVTables.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,24 @@ llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
576576
llvm::Function *ThunkFn = cast<llvm::Function>(Thunk->stripPointerCastsSafe());
577577

578578
int thisIndex = ThunkFn->hasStructRetAttr() && !FnInfo.getReturnInfo().isSRetAfterThis() ? 1 : 0;
579-
llvm::Type* CurrentThisTy = CGM.getTypes().ConvertTypeForMem(cast<PointerType>(FnInfo.arg_begin()->type)->getPointeeType());
579+
580+
// CurrentThisTy is the value used for elementtype attribute
581+
// Extract the pointee type from the actual parameter to ensure consistency
582+
// between the function signature and the elementtype attribute
583+
llvm::Type* CurrentThisTy = nullptr;
584+
585+
if (thisIndex < ThunkFn->arg_size()) {
586+
llvm::Argument* ThisArg = ThunkFn->getArg(thisIndex);
587+
llvm::Type* ActualParamType = ThisArg->getType();
588+
auto* PtrTy = dyn_cast<llvm::PointerType>(ActualParamType);
589+
// The getPointerElementType function is depricated for Opaque pointers
590+
if (PtrTy && !PtrTy->isOpaque())
591+
CurrentThisTy = PtrTy->getPointerElementType();
592+
}
593+
594+
if (CurrentThisTy == nullptr)
595+
CurrentThisTy = CGM.getTypes().ConvertTypeForMem(cast<PointerType>(FnInfo.arg_begin()->type)->getPointeeType());
596+
580597

581598
if (!ThunkFn->hasParamAttribute(thisIndex, llvm::Attribute::ElementType))
582599
ThunkFn->addParamAttr(thisIndex, llvm::Attribute::get(CGM.getLLVMContext(), llvm::Attribute::ElementType, CurrentThisTy));

0 commit comments

Comments
 (0)