Skip to content

Commit a56ca1a

Browse files
authored
[clang][codegen] Fix possible crash when setting TBAA metadata on FP math libcalls (#108575)
There's currently no code path that can reach this crash, but: ``` Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal()); ``` fails if the call returns `void`. This could happen if a builtin for something like `void sincos(double, double*, double*)` is added to clang. Instead, use the `llvm::CallBase` returned from `EmitCall()` to set the TBAA metadata, which should exist no matter the return type.
1 parent c49a1ae commit a56ca1a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,10 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
690690
const CallExpr *E, llvm::Constant *calleeValue) {
691691
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
692692
CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
693+
llvm::CallBase *callOrInvoke = nullptr;
693694
RValue Call =
694-
CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
695+
CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot(),
696+
/*Chain=*/nullptr, &callOrInvoke);
695697

696698
if (unsigned BuiltinID = FD->getBuiltinID()) {
697699
// Check whether a FP math builtin function, such as BI__builtin_expf
@@ -705,8 +707,7 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
705707
// Emit "int" TBAA metadata on FP math libcalls.
706708
clang::QualType IntTy = Context.IntTy;
707709
TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
708-
Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal());
709-
CGF.CGM.DecorateInstructionWithTBAA(Inst, TBAAInfo);
710+
CGF.CGM.DecorateInstructionWithTBAA(callOrInvoke, TBAAInfo);
710711
}
711712
}
712713
return Call;

0 commit comments

Comments
 (0)