Skip to content

Commit

Permalink
support non-default AS for va_list/va_copy/va_start/va_end
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri91 committed Oct 24, 2024
1 parent c116ac2 commit 18ca23f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
17 changes: 12 additions & 5 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,17 @@ EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> Types) {
}

Value *CodeGenFunction::EmitVAStartEnd(Value *ArgValue, bool IsStart) {
llvm::Type *DestType = Int8PtrTy;
if (ArgValue->getType() != DestType)
llvm::PointerType *DestType = Int8PtrTy;
if (getLangOpts().Cheerp) {
DestType = llvm::PointerType::getWithSamePointeeType(DestType, ArgValue->getType()->getPointerAddressSpace());
}
if (ArgValue->getType() != DestType) {
ArgValue =
Builder.CreateBitCast(ArgValue, DestType, ArgValue->getName().data());
}

Intrinsic::ID inst = IsStart ? Intrinsic::vastart : Intrinsic::vaend;
return Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue);
return Builder.CreateCall(CGM.getIntrinsic(inst, {DestType}), ArgValue);
}

/// Checks if using the result of __builtin_object_size(p, @p From) in place of
Expand Down Expand Up @@ -2548,11 +2552,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
Value *DstPtr = EmitVAListRef(E->getArg(0)).getPointer();
Value *SrcPtr = EmitVAListRef(E->getArg(1)).getPointer();

llvm::Type *Type = Int8PtrTy;
llvm::PointerType *Type = Int8PtrTy;

if (getLangOpts().Cheerp) {
Type = llvm::PointerType::getWithSamePointeeType(Type, SrcPtr->getType()->getPointerAddressSpace());
}
DstPtr = Builder.CreateBitCast(DstPtr, Type);
SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy), {DstPtr, SrcPtr});
Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy, {Type, Type}), {DstPtr, SrcPtr});
return RValue::get(nullptr);
}
case Builtin::BI__builtin_abs:
Expand Down
22 changes: 17 additions & 5 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6455,7 +6455,8 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context,
QualType DeclType = FDecl->getType();
const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(DeclType);

if (!Context.BuiltinInfo.hasPtrArgsOrResult(FDecl->getBuiltinID()) || !FT ||
if (!(Context.BuiltinInfo.hasPtrArgsOrResult(FDecl->getBuiltinID()) ||
Context.BuiltinInfo.hasReferenceArgsOrResult(FDecl->getBuiltinID())) || !FT ||
ArgExprs.size() < FT->getNumParams())
return nullptr;

Expand All @@ -6472,9 +6473,12 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context,
return nullptr;
Expr *Arg = ArgRes.get();
QualType ArgType = Arg->getType();
if (!ParamType->isPointerType() ||
auto isPointerOrRef = [](QualType Ty) {
return Ty->isPointerType() || Ty->isReferenceType();
};
if (!isPointerOrRef(ParamType) ||
ParamType.hasAddressSpace() ||
!ArgType->isPointerType() ||
!isPointerOrRef(ArgType) ||
!ArgType->getPointeeType().hasAddressSpace()) {
OverloadParams.push_back(ParamType);
continue;
Expand All @@ -6488,7 +6492,14 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context,
LangAS AS = ArgType->getPointeeType().getAddressSpace();

PointeeType = Context.getAddrSpaceQualType(PointeeType, AS);
OverloadParams.push_back(Context.getPointerType(PointeeType));
QualType Type;
if (ParamType->isPointerType()) {
Type = Context.getPointerType(PointeeType);
} else {
assert(ParamType->isReferenceType());
Type = Context.getLValueReferenceType(PointeeType);
}
OverloadParams.push_back(Type);
}

if (!NeedsNewDecl)
Expand Down Expand Up @@ -16815,8 +16826,9 @@ ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc,
}
}

QualType ETyNoAS = Context.removeAddrSpaceQualType(E->getType());
if (!IsMS && !E->isTypeDependent() &&
!Context.hasSameType(VaListType, E->getType()))
!Context.hasSameType(VaListType, ETyNoAS))
return ExprError(
Diag(E->getBeginLoc(),
diag::err_first_argument_to_va_arg_not_of_type_va_list)
Expand Down
6 changes: 3 additions & 3 deletions llvm/include/llvm/IR/Intrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@ class MSBuiltin<string name> {
//===--------------- Variable Argument Handling Intrinsics ----------------===//
//

def int_vastart : DefaultAttrsIntrinsic<[], [llvm_ptr_ty], [], "llvm.va_start">;
def int_vacopy : DefaultAttrsIntrinsic<[], [llvm_ptr_ty, llvm_ptr_ty], [],
def int_vastart : DefaultAttrsIntrinsic<[], [llvm_anyptr_ty], [], "llvm.va_start">;
def int_vacopy : DefaultAttrsIntrinsic<[], [llvm_anyptr_ty, llvm_anyptr_ty], [],
"llvm.va_copy">;
def int_vaend : DefaultAttrsIntrinsic<[], [llvm_ptr_ty], [], "llvm.va_end">;
def int_vaend : DefaultAttrsIntrinsic<[], [llvm_anyptr_ty], [], "llvm.va_end">;

//===------------------- Garbage Collection Intrinsics --------------------===//
//
Expand Down

0 comments on commit 18ca23f

Please sign in to comment.