[clang] [Clang] Add float type support to __builtin_reduce_add and __builtin_reduce_multipy (PR #120367)
Farzon Lotfi via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 17 22:59:39 PST 2024
================
@@ -1754,6 +1755,17 @@ static bool interp__builtin_vector_reduce(InterpState &S, CodePtr OpPC,
PrimType ElemT = *S.getContext().classify(ElemType);
unsigned NumElems = Arg.getNumElems();
+ if (ElemType->isRealFloatingType()) {
+ if (ID != Builtin::BI__builtin_reduce_add &&
+ ID != Builtin::BI__builtin_reduce_mul)
+ llvm_unreachable("Only reduce_add and reduce_mul are supported for "
+ "floating-point types.");
+ // Floating-point arithmetic is not valid for constant expression
+ // initialization. Returning false defers checks to integral constant
+ // expression validation, preventing a bad deref of Floating as an integer.
+ return false;
----------------
farzonl wrote:
I wanted to do something like this:
```cpp
if(ElemType->isRealFloatingType()) {
FPOptions FPO = Call->getFPFeaturesInEffect(S.getASTContext().getLangOpts());
llvm::RoundingMode RM = getRoundingMode(FPO);
Floating Result = Arg.atIndex(0).deref<Floating>();
APFloat currResult = Result.getAPFloat();
for (unsigned I = 1; I != NumElems; ++I) {
Floating Elem = Arg.atIndex(I).deref<Floating>();
if (ID == Builtin::BI__builtin_reduce_add) {
if(APFloat::opStatus::opOK != currResult.add(Elem.getAPFloat(),RM))
return false;
} else if (ID == Builtin::BI__builtin_reduce_mul) {
if(APFloat::opStatus::opOK != currResult.multiply(Elem.getAPFloat(),RM))
return false;
} else
llvm_unreachable("Only reduce_add and reduce_mul are supported for floating-point types.");
}
S.Stk.push<Floating>(Floating(currResult));
return true;
}
```
However there are checks further for `isIntegralPointer()` that make any code here to compute the reduction not make sense.
https://github.com/llvm/llvm-project/pull/120367
More information about the cfe-commits
mailing list