[clang] [clang][codegen] Fix possible crash when setting TBAA metadata on FP math libcalls (PR #108575)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 07:43:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Benjamin Maxwell (MacDue)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/108575.diff


1 Files Affected:

- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+4-3) 


``````````diff
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 27abeba92999b3..d4c7eea3d20b24 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -690,8 +690,10 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
                               const CallExpr *E, llvm::Constant *calleeValue) {
   CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
   CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
+  llvm::CallBase *callOrInvoke = nullptr;
   RValue Call =
-      CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
+      CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot(),
+                   /*Chain=*/nullptr, &callOrInvoke);
 
   if (unsigned BuiltinID = FD->getBuiltinID()) {
     // Check whether a FP math builtin function, such as BI__builtin_expf
@@ -705,8 +707,7 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
       // Emit "int" TBAA metadata on FP math libcalls.
       clang::QualType IntTy = Context.IntTy;
       TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
-      Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal());
-      CGF.CGM.DecorateInstructionWithTBAA(Inst, TBAAInfo);
+      CGF.CGM.DecorateInstructionWithTBAA(callOrInvoke, TBAAInfo);
     }
   }
   return Call;

``````````

</details>


https://github.com/llvm/llvm-project/pull/108575


More information about the cfe-commits mailing list