Skip to content

Commit ea5bfe3

Browse files
AmrDeveloperlanza
authored andcommitted
[CIR] Backport support zero init attr for all FP types (#1536)
Update getZeroInitAttr to cover more FP types, Backport from (llvm/llvm-project#133100)
1 parent a2fe8b8 commit ea5bfe3

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
135135
mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
136136
if (mlir::isa<cir::IntType>(ty))
137137
return cir::IntAttr::get(ty, 0);
138-
if (auto fltType = mlir::dyn_cast<cir::SingleType>(ty))
139-
return cir::FPAttr::getZero(fltType);
140-
if (auto fltType = mlir::dyn_cast<cir::DoubleType>(ty))
141-
return cir::FPAttr::getZero(fltType);
142-
if (auto fltType = mlir::dyn_cast<cir::FP16Type>(ty))
143-
return cir::FPAttr::getZero(fltType);
144-
if (auto fltType = mlir::dyn_cast<cir::BF16Type>(ty))
145-
return cir::FPAttr::getZero(fltType);
138+
if (cir::isAnyFloatingPointType(ty))
139+
return cir::FPAttr::getZero(ty);
146140
if (auto complexType = mlir::dyn_cast<cir::ComplexType>(ty))
147141
return getZeroAttr(complexType);
148142
if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ mlir::Type CIRGenTypes::convertType(QualType T) {
498498
ResultType = Builder.getLongDoubleTy(astContext.getFloatTypeSemantics(T));
499499
break;
500500
case BuiltinType::Float128:
501+
ResultType = CGM.FP128Ty;
502+
break;
501503
case BuiltinType::Ibm128:
502504
// FIXME: look at astContext.getFloatTypeSemantics(T) and getTypeForFormat
503505
// on LLVM codegen.

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ LongDoubleType::verify(function_ref<InFlightDiagnostic()> emitError,
705705

706706
bool cir::isAnyFloatingPointType(mlir::Type t) {
707707
return isa<cir::SingleType, cir::DoubleType, cir::LongDoubleType,
708-
cir::FP80Type>(t);
708+
cir::FP80Type, cir::BF16Type, cir::FP16Type, cir::FP128Type>(t);
709709
}
710710

711711
bool cir::isScalarType(mlir::Type ty) {

clang/test/CIR/global-var-simple.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ _BitInt(20) sb20;
5858

5959
unsigned _BitInt(48) ub48;
6060
// CHECK: external @ub48 = #cir.int<0> : !u48i
61+
62+
_Float16 f16;
63+
// CHECK: cir.global external @f16 = #cir.fp<0.000000e+00> : !cir.f16
64+
65+
__bf16 bf16;
66+
// CHECK: cir.global external @bf16 = #cir.fp<0.000000e+00> : !cir.bf16
67+
68+
float f;
69+
// CHECK: cir.global external @f = #cir.fp<0.000000e+00> : !cir.float
70+
71+
double d = 1.25;
72+
// CHECK: cir.global external @d = #cir.fp<1.250000e+00> : !cir.double
73+
74+
long double ld;
75+
// CHECK: cir.global external @ld = #cir.fp<0.000000e+00> : !cir.long_double<!cir.f80>
76+
77+
__float128 f128;
78+
// CHECK: cir.global external @f128 = #cir.fp<0.000000e+00> : !cir.f128

0 commit comments

Comments
 (0)