@@ -32,6 +32,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
32
32
return create<cir::ConstantOp>(loc, attr.getType (), attr);
33
33
}
34
34
35
+ // Creates constant null value for integral type ty.
36
+ cir::ConstantOp getNullValue (mlir::Type ty, mlir::Location loc) {
37
+ return create<cir::ConstantOp>(loc, ty, getZeroInitAttr (ty));
38
+ }
39
+
35
40
cir::ConstantOp getBool (bool state, mlir::Location loc) {
36
41
return create<cir::ConstantOp>(loc, getBoolTy (), getCIRBoolAttr (state));
37
42
}
@@ -68,6 +73,36 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
68
73
getContext (), mlir::cast<cir::PointerType>(type), valueAttr);
69
74
}
70
75
76
+ mlir::TypedAttr getConstNullPtrAttr (mlir::Type t) {
77
+ assert (mlir::isa<cir::PointerType>(t) && " expected cir.ptr" );
78
+ return getConstPtrAttr (t, 0 );
79
+ }
80
+
81
+ mlir::TypedAttr getZeroAttr (mlir::Type t) {
82
+ return cir::ZeroAttr::get (getContext (), t);
83
+ }
84
+
85
+ mlir::TypedAttr getZeroInitAttr (mlir::Type ty) {
86
+ if (mlir::isa<cir::IntType>(ty))
87
+ return cir::IntAttr::get (ty, 0 );
88
+ if (auto fltType = mlir::dyn_cast<cir::SingleType>(ty))
89
+ return cir::FPAttr::getZero (fltType);
90
+ if (auto fltType = mlir::dyn_cast<cir::DoubleType>(ty))
91
+ return cir::FPAttr::getZero (fltType);
92
+ if (auto fltType = mlir::dyn_cast<cir::FP16Type>(ty))
93
+ return cir::FPAttr::getZero (fltType);
94
+ if (auto fltType = mlir::dyn_cast<cir::BF16Type>(ty))
95
+ return cir::FPAttr::getZero (fltType);
96
+ if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))
97
+ return getZeroAttr (arrTy);
98
+ if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
99
+ return getConstNullPtrAttr (ptrTy);
100
+ if (mlir::isa<cir::BoolType>(ty)) {
101
+ return getCIRBoolAttr (false );
102
+ }
103
+ llvm_unreachable (" Zero initializer for given type is NYI" );
104
+ }
105
+
71
106
mlir::Value createAlloca (mlir::Location loc, cir::PointerType addrType,
72
107
mlir::Type type, llvm::StringRef name,
73
108
mlir::IntegerAttr alignment) {
0 commit comments