Skip to content

Commit 0054f04

Browse files
committed
Add FP tests
1 parent 2b4b002 commit 0054f04

File tree

6 files changed

+58
-29
lines changed

6 files changed

+58
-29
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ struct MissingFeatures {
8383
static bool opFuncSetComdat() { return false; }
8484

8585
// CallOp handling
86-
static bool opCallBuiltinFunc() { return false; }
8786
static bool opCallPseudoDtor() { return false; }
8887
static bool opCallAggregateArgs() { return false; }
8988
static bool opCallPaddingArgs() { return false; }

clang/lib/CIR/CodeGen/CIRGenBuilder.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
5050

5151
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc,
5252
llvm::APInt intVal) {
53-
unsigned width = intVal.getBitWidth();
54-
cir::IntType t = getUIntNTy(width);
55-
return getConstInt(loc, t, intVal.getZExtValue());
53+
return getConstInt(loc, llvm::APSInt(intVal));
5654
}
5755

5856
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t,

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "Address.h"
1313
#include "CIRGenTypeCache.h"
14+
#include "clang/CIR/Interfaces/CIRFPTypeInterface.h"
1415
#include "clang/CIR/MissingFeatures.h"
1516

1617
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
@@ -236,28 +237,11 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
236237

237238
cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t, uint64_t c);
238239

239-
mlir::Type getFPType(llvm::fltSemantics const &sem) {
240-
switch (llvm::APFloat::SemanticsToEnum(sem)) {
241-
case llvm::APFloat::S_IEEEhalf:
242-
return cir::FP16Type::get(getContext(), typeCache.FP16Ty);
243-
case llvm::APFloat::S_BFloat:
244-
return cir::BF16Type::get(getContext(), typeCache.BFloat16Ty);
245-
case llvm::APFloat::S_IEEEsingle:
246-
return cir::SingleType::get(getContext(), typeCache.FloatTy);
247-
case llvm::APFloat::S_IEEEdouble:
248-
return cir::DoubleType::get(getContext(), typeCache.DoubleTy);
249-
case llvm::APFloat::S_IEEEquad:
250-
return cir::FP128Type::get(getContext(), typeCache.FP128Ty);
251-
case llvm::APFloat::S_x87DoubleExtended:
252-
return cir::FP80Type::get(getContext(), typeCache.FP80Ty);
253-
default:
254-
llvm_unreachable("Unrecognized floating semantics");
255-
}
256-
}
257-
258-
cir::ConstantOp getConstFP(mlir::Location loc, llvm::APFloat fpVal) {
259-
mlir::Type type = getFPType(fpVal.getSemantics());
260-
return create<cir::ConstantOp>(loc, getAttr<cir::FPAttr>(type, fpVal));
240+
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t,
241+
llvm::APFloat fpVal) {
242+
assert(mlir::isa<cir::CIRFPTypeInterface>(t) &&
243+
"expected floating point type");
244+
return create<cir::ConstantOp>(loc, getAttr<cir::FPAttr>(t, fpVal));
261245
}
262246

263247
bool isInt8Ty(mlir::Type i) {

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
4343
// type. We feel it should be Ok to use expression type because it is
4444
// hard to imagine a builtin function evaluates to a value that
4545
// over/underflows its own defined type.
46-
mlir::Type resTy = convertType(e->getType());
47-
return RValue::get(builder.getConstFP(getLoc(e->getExprLoc()), resTy,
46+
mlir::Type type = convertType(e->getType());
47+
return RValue::get(builder.getConstFP(getLoc(e->getExprLoc()), type,
4848
result.Val.getFloat()));
4949
}
5050
}

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ CIRGenCallee CIRGenFunction::emitDirectCallee(const GlobalDecl &gd) {
10431043
const auto *fd = cast<FunctionDecl>(gd.getDecl());
10441044

10451045
if (unsigned builtinID = fd->getBuiltinID()) {
1046-
if(fd->getAttr<AsmLabelAttr>()) {
1046+
if (fd->getAttr<AsmLabelAttr>()) {
10471047
cgm.errorNYI("AsmLabelAttr");
10481048
}
10491049

clang/test/CIR/CodeGen/builtin_call.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ constexpr extern int cx_var = __builtin_is_constant_evaluated();
1111
// LLVM: @cx_var = {{.*}} i32 1
1212
// OGCG: @cx_var = {{.*}} i32 1
1313

14+
constexpr extern float cx_var_single = __builtin_huge_valf();
15+
16+
// CIR: cir.global {{.*}} @cx_var_single = #cir.fp<0x7F800000> : !cir.float
17+
// LLVM: @cx_var_single = {{.*}} float 0x7FF0000000000000
18+
// OGCG: @cx_var_single = {{.*}} float 0x7FF0000000000000
19+
20+
constexpr extern long double cx_var_ld = __builtin_huge_vall();
21+
22+
// CIR: cir.global {{.*}} @cx_var_ld = #cir.fp<0x7FFF8000000000000000> : !cir.long_double<!cir.f80>
23+
// LLVM: @cx_var_ld = {{.*}} x86_fp80 0xK7FFF8000000000000000
24+
// OGCG: @cx_var_ld = {{.*}} x86_fp80 0xK7FFF8000000000000000
25+
1426
int is_constant_evaluated() {
1527
return __builtin_is_constant_evaluated();
1628
}
@@ -28,3 +40,39 @@ int is_constant_evaluated() {
2840
// OGCG: define {{.*}}i32 @_Z21is_constant_evaluatedv()
2941
// OGCG: ret i32 0
3042
// OGCG: }
43+
44+
long double constant_fp_builtin_ld() {
45+
return __builtin_fabsl(-0.1L);
46+
}
47+
48+
// CIR: cir.func @_Z22constant_fp_builtin_ldv() -> !cir.long_double<!cir.f80>
49+
// CIR: %[[PONE:.+]] = cir.const #cir.fp<1.000000e-01> : !cir.long_double<!cir.f80>
50+
51+
// LLVM: define {{.*}}x86_fp80 @_Z22constant_fp_builtin_ldv()
52+
// LLVM: %[[MEM:.+]] = alloca x86_fp80
53+
// LLVM: store x86_fp80 0xK3FFBCCCCCCCCCCCCCCCD, ptr %[[MEM]]
54+
// LLVM: %[[RETVAL:.+]] = load x86_fp80, ptr %[[MEM]]
55+
// LLVM: ret x86_fp80 %[[RETVAL]]
56+
// LLVM: }
57+
58+
// OGCG: define {{.*}}x86_fp80 @_Z22constant_fp_builtin_ldv()
59+
// OGCG: ret x86_fp80 0xK3FFBCCCCCCCCCCCCCCCD
60+
// OGCG: }
61+
62+
float constant_fp_builtin_single() {
63+
return __builtin_fabsf(-0.1f);
64+
}
65+
66+
// CIR: cir.func @_Z26constant_fp_builtin_singlev() -> !cir.float
67+
// CIR: %[[PONE:.+]] = cir.const #cir.fp<1.000000e-01> : !cir.float
68+
69+
// LLVM: define {{.*}}float @_Z26constant_fp_builtin_singlev()
70+
// LLVM: %[[MEM:.+]] = alloca float
71+
// LLVM: store float 0x3FB99999A0000000, ptr %[[MEM]]
72+
// LLVM: %[[RETVAL:.+]] = load float, ptr %[[MEM]]
73+
// LLVM: ret float %[[RETVAL]]
74+
// LLVM: }
75+
76+
// OGCG: define {{.*}}float @_Z26constant_fp_builtin_singlev()
77+
// OGCG: ret float 0x3FB99999A0000000
78+
// OGCG: }

0 commit comments

Comments
 (0)