Skip to content

Commit ed6292f

Browse files
committed
[TBAA] Emit int TBAA metadata on FP math libcalls
Base on the discussion https://discourse.llvm.org/t/fp-can-we-add-pure-attribute-for-math-library-functions-default/79459, math libcalls set errno, so it should emit "int" TBAA metadata on FP libcalls to solve the alias issue. Fix #86635
1 parent 3c8f3b9 commit ed6292f

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,38 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
707707
const CallExpr *E, llvm::Constant *calleeValue) {
708708
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
709709
CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
710-
return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
710+
RValue Call =
711+
CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
712+
713+
// Check the supported intrinsic.
714+
if (unsigned BuiltinID = FD->getBuiltinID()) {
715+
auto IntrinsicID = [&]() -> unsigned {
716+
switch (BuiltinID) {
717+
case Builtin::BIexpf:
718+
case Builtin::BI__builtin_expf:
719+
case Builtin::BI__builtin_expf128:
720+
return true;
721+
}
722+
// TODO: support more FP math libcalls
723+
return false;
724+
}();
725+
726+
if (IntrinsicID) {
727+
llvm::MDBuilder MDHelper(CGF.getLLVMContext());
728+
MDNode *RootMD;
729+
if (CGF.getLangOpts().CPlusPlus)
730+
RootMD = MDHelper.createTBAARoot("Simple C++ TBAA");
731+
else
732+
RootMD = MDHelper.createTBAARoot("Simple C/C++ TBAA");
733+
// Emit "int" TBAA metadata on FP math libcalls.
734+
MDNode *AliasType = MDHelper.createTBAANode("int", RootMD);
735+
MDNode *MDInt = MDHelper.createTBAAStructTagNode(AliasType, AliasType, 0);
736+
737+
Value *Val = Call.getScalarVal();
738+
cast<llvm::Instruction>(Val)->setMetadata(LLVMContext::MD_tbaa, MDInt);
739+
}
740+
}
741+
return Call;
711742
}
712743

713744
/// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2
2+
// RUN: %clang -S -O3 -emit-llvm -o - -x c++ %s | FileCheck %s -check-prefixes=CHECK
3+
4+
#include <math.h>
5+
6+
7+
// Emit int TBAA metadata on FP math libcalls, which is useful for alias analysis
8+
9+
// CHECK-LABEL: define dso_local noundef float @_Z3fooPffi
10+
// CHECK-SAME: (ptr nocapture noundef readonly [[NUM:%.*]], float noundef [[R2INV:%.*]], i32 noundef [[N:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
11+
// CHECK-NEXT: entry:
12+
// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i8, ptr [[NUM]], i64 40
13+
// CHECK-NEXT: [[TMP0:%.*]] = load float, ptr [[ARRAYIDX]], align 4, !tbaa [[TBAA6:![0-9]+]]
14+
// CHECK-NEXT: [[CALL_I:%.*]] = tail call noundef float @expf(float noundef [[TMP0]]) #[[ATTR2:[0-9]+]], !tbaa [[TBAA10:![0-9]+]]
15+
// CHECK-NEXT: [[MUL:%.*]] = fmul float [[TMP0]], [[CALL_I]]
16+
// CHECK-NEXT: ret float [[MUL]]
17+
//
18+
float foo (float num[], float r2inv, int n) {
19+
const float expm2 = std::exp(num[10]); // Emit TBAA metadata on @expf
20+
float tmp = expm2 * num[10];
21+
return tmp;
22+
}

0 commit comments

Comments
 (0)