Skip to content

Commit

Permalink
Use the ElementType attribute for cheerp_dowcast return type
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri91 committed Oct 17, 2024
1 parent 5cc520a commit ca91b80
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12492,9 +12492,9 @@ Value *CodeGenFunction::EmitCheerpBuiltinExpr(unsigned BuiltinID,
CallBase* CB = Builder.CreateCall(F, Ops);

llvm::Type* elementType = ConvertTypeForMem(E->getArg(0)->getType()->getPointeeType());
assert(Ops[0]->getType()->isOpaquePointerTy() || Ops[0]->getType()->getNonOpaquePointerElementType() == elementType);

llvm::Type* retType = ConvertType(E->getType()->getPointeeType());
CB->addParamAttr(0, llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, elementType));
CB->addRetAttr(llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, retType));
return CB;
}
else if (BuiltinID == Cheerp::BI__builtin_cheerp_pointer_kind) {
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ CodeGenFunction::GenerateDowncast(Address Value,

llvm::CallBase* CB = Builder.CreateCall(intrinsic, {Value.getPointer(), BaseIdOffset});
CB->addParamAttr(0, llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, Value.getElementType()));
CB->addRetAttr(llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, DerivedPtrTy));
return Address(CB, DerivedPtrTy, Value.getAlignment());
}

Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CGException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) {
llvm::Function* intrinsic = llvm::Intrinsic::getDeclaration(&CGM.getModule(), llvm::Intrinsic::cheerp_downcast, Tys);
llvm::CallBase* CB = Builder.CreateCall(intrinsic, {typedAddr.getPointer(), llvm::ConstantInt::get(CGM.Int32Ty, 0)});
CB->addParamAttr(0, llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, ty));
CB->addRetAttr(llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, ty));
typedAddr = Address(CB, ty, typedAddr.getAlignment());
}

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(

llvm::CallBase* CB = Builder.CreateCall(intrinsic, {This, Adj});
CB->addParamAttr(0, llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, ThisAddr.getElementType()));
CB->addRetAttr(llvm::Attribute::get(CB->getContext(), llvm::Attribute::ElementType, CGF.Int8Ty));
llvm::Value* ThisNotZero = Builder.CreateBitCast(CB, This->getType());
Builder.CreateBr(FnNonVirtual);
CGF.EmitBlock(FnNonVirtual);
Expand Down Expand Up @@ -1681,6 +1682,8 @@ llvm::Value *ItaniumCXXABI::EmitDynamicCastCall(
llvm::Value* DynamicDowncast = CGF.Builder.CreateCall(intrinsic, {DynCastObj, Value});
assert(DynCastObj->getType()->isOpaquePointerTy() || DynCastObj->getType()->getNonOpaquePointerElementType() == ThisAddr.getElementType());
cast<llvm::CallBase>(DynamicDowncast)->addParamAttr(0, llvm::Attribute::get(DynamicDowncast->getContext(), llvm::Attribute::ElementType, ThisAddr.getElementType()));
cast<llvm::CallBase>(DynamicDowncast)->addRetAttr(llvm::Attribute::get(DynamicDowncast->getContext(), llvm::Attribute::ElementType, CGF.ConvertType(DestTy->getPointeeType())));

CGF.Builder.CreateBr(EndBB);
// If the returned offset is zero, we can passthrough the value
llvm::BasicBlock *ZeroBB = CGF.createBasicBlock("cheerp_null_downcast");
Expand Down
9 changes: 7 additions & 2 deletions llvm/lib/CheerpUtils/GlobalDepsAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ void GlobalDepsAnalyzer::visitFunction(const Function* F, VisitedSet& visited)
if (F->getIntrinsicID() == Intrinsic::cheerp_downcast)
{
Type* elementType = nullptr;
Type* retType = nullptr;
for (auto* u : F->users())
{
if (const CallBase* CB = dyn_cast<CallBase>(u))
Expand All @@ -1410,12 +1411,16 @@ void GlobalDepsAnalyzer::visitFunction(const Function* F, VisitedSet& visited)
elementType = currElementType;
else
assert(elementType == currElementType);
Type* currRetType = CB->getRetElementType();
if (!retType)
retType = currRetType;
else
assert(retType == currRetType);
}
}
if (!elementType)
if (!elementType || !retType)
return;

Type* retType = F->getReturnType()->getPointerElementType();
// A downcast from a type to i8* is conventially used to support pointers to
// member functions and does not imply that the type needs the downcast array
Type* ty = retType->isIntegerTy(8) ? elementType : retType;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CheerpUtils/TypeOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void TypeOptimizer::gatherAllTypesInfo(const Module& M)
if(II->getIntrinsicID()==Intrinsic::cheerp_downcast)
{
Type* opType = II->getParamElementType(0);
Type* retType = II->getType()->getPointerElementType();
Type* retType = II->getRetElementType();
// In the special case of downcast from i8* to i8* we are dealing with exceptions.
// We collect the types info from another syntetic downcast, so just skip here.
if(opType->isIntegerTy())
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Coroutines/CoroEarly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void Lowerer::lowerCoroPromise(CoroPromiseInst *Intrin) {
ConstantInt* Adj = ConstantInt::get(Builder.getInt32Ty(), offset);
CallBase* CB = Builder.CreateCall(intrinsic, {Operand, Adj});
CB->addParamAttr(0, Attribute::get(Context, Attribute::ElementType, Builder.getInt8Ty()));
CB->addRetAttr(Attribute::get(Context, Attribute::ElementType, Builder.getInt8Ty()));
Replacement = CB;
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Coroutines/CoroFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,7 @@ static void createFramePtr(coro::Shape &Shape) {
ConstantInt* Adj = ConstantInt::get(Builder.getInt32Ty(), 0);
CallBase* DC = Builder.CreateCall(intrinsic, {Shape.FramePtr, Adj});
DC->addParamAttr(0, Attribute::get(Builder.getContext(), Attribute::ElementType, FrameTy));
DC->addRetAttr(Attribute::get(Builder.getContext(), Attribute::ElementType, FrameTy));
Shape.FramePtr = DC;

// CHEERP: Add bases metadata to the Frame type, and consider the promise
Expand Down

0 comments on commit ca91b80

Please sign in to comment.