@@ -6621,10 +6621,27 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
6621
6621
}
6622
6622
}
6623
6623
6624
+ // These will be emitted as Intrinsic later.
6625
+ auto NeedsDeviceOverload = [&](unsigned BuiltinID) {
6626
+ if (getTarget().getTriple().isAMDGCN()) {
6627
+ switch (BuiltinID) {
6628
+ default:
6629
+ return false;
6630
+ case Builtin::BIlogb:
6631
+ case Builtin::BI__builtin_logb:
6632
+ case Builtin::BIscalbn:
6633
+ case Builtin::BI__builtin_scalbn:
6634
+ return true;
6635
+ }
6636
+ }
6637
+ return false;
6638
+ };
6639
+
6624
6640
// If this is an alias for a lib function (e.g. __builtin_sin), emit
6625
6641
// the call using the normal call path, but using the unmangled
6626
6642
// version of the function name.
6627
- if (getContext().BuiltinInfo.isLibFunction(BuiltinID))
6643
+ if (!NeedsDeviceOverload(BuiltinID) &&
6644
+ getContext().BuiltinInfo.isLibFunction(BuiltinID))
6628
6645
return emitLibraryCall(*this, FD, E,
6629
6646
CGM.getBuiltinLibFunction(FD, BuiltinID));
6630
6647
@@ -20910,6 +20927,30 @@ Value *CodeGenFunction::EmitAMDGPUBuiltinExpr(unsigned BuiltinID,
20910
20927
case AMDGPU::BI__builtin_amdgcn_s_prefetch_data:
20911
20928
return emitBuiltinWithOneOverloadedType<2>(
20912
20929
*this, E, Intrinsic::amdgcn_s_prefetch_data);
20930
+ case Builtin::BIlogb:
20931
+ case Builtin::BI__builtin_logb: {
20932
+ auto Src0 = EmitScalarExpr(E->getArg(0));
20933
+ auto FrExpFunc = CGM.getIntrinsic(Intrinsic::amdgcn_frexp_exp,
20934
+ {Builder.getInt32Ty(), Src0->getType()});
20935
+ auto FrExp = Builder.CreateCall(FrExpFunc, Src0);
20936
+ auto Add = Builder.CreateAdd(
20937
+ FrExp, ConstantInt::getSigned(FrExp->getType(), -1), "", false, true);
20938
+ auto SIToFP = Builder.CreateSIToFP(Add, Builder.getDoubleTy());
20939
+ auto Fabs = emitBuiltinWithOneOverloadedType<1>(*this, E, Intrinsic::fabs);
20940
+ auto FCmpONE = Builder.CreateFCmpONE(
20941
+ Fabs, ConstantFP::getInfinity(Builder.getDoubleTy()));
20942
+ auto Sel1 = Builder.CreateSelect(FCmpONE, SIToFP, Fabs);
20943
+ auto FCmpOEQ =
20944
+ Builder.CreateFCmpOEQ(Src0, ConstantFP::getZero(Builder.getDoubleTy()));
20945
+ auto Sel2 = Builder.CreateSelect(
20946
+ FCmpOEQ, ConstantFP::getInfinity(Builder.getDoubleTy(), /*Neg*/ true),
20947
+ Sel1);
20948
+ return Sel2;
20949
+ }
20950
+ case Builtin::BIscalbn:
20951
+ case Builtin::BI__builtin_scalbn:
20952
+ return emitBinaryExpMaybeConstrainedFPBuiltin(
20953
+ *this, E, Intrinsic::ldexp, Intrinsic::experimental_constrained_ldexp);
20913
20954
default:
20914
20955
return nullptr;
20915
20956
}
0 commit comments