[clang] [CIR] Upstream zero init for global variables (PR #133100)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 27 06:51:13 PDT 2025
================
@@ -67,6 +67,40 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return create<cir::ConstantOp>(loc, attr.getType(), attr);
}
+ mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) {
+ assert(mlir::isa<cir::PointerType>(t) && "expected cir.ptr");
+ return getConstPtrAttr(t, 0);
+ }
+
+ mlir::TypedAttr getZeroAttr(mlir::Type t) {
+ return cir::ZeroAttr::get(getContext(), t);
+ }
+
+ mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
+ if (mlir::isa<cir::IntType>(ty))
+ return cir::IntAttr::get(ty, 0);
+ if (auto fltType = mlir::dyn_cast<cir::SingleType>(ty))
+ return cir::FPAttr::getZero(fltType);
+ if (auto fltType = mlir::dyn_cast<cir::DoubleType>(ty))
+ return cir::FPAttr::getZero(fltType);
+ if (auto fltType = mlir::dyn_cast<cir::LongDoubleType>(ty))
+ return cir::FPAttr::getZero(fltType);
+ if (auto fltType = mlir::dyn_cast<cir::FP16Type>(ty))
+ return cir::FPAttr::getZero(fltType);
+ if (auto fltType = mlir::dyn_cast<cir::FP128Type>(ty))
+ return cir::FPAttr::getZero(fltType);
+ if (auto fltType = mlir::dyn_cast<cir::BF16Type>(ty))
+ return cir::FPAttr::getZero(fltType);
+ if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))
+ return getZeroAttr(arrTy);
+ if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
+ return getConstNullPtrAttr(ptrTy);
+ if (mlir::isa<cir::BoolType>(ty)) {
+ return getCIRBoolAttr(false);
+ }
+ llvm_unreachable("Zero initializer for given type is NYI");
+ }
----------------
AmrDeveloper wrote:
> This was probably done in response to my suggestion that handling the zero-initialization case for arrays would be a way to make the other PR smaller. I didn't realize this piece of it was so small.
I was thinking of extracting the zero attr part into another PR, and if it can be merged first, then I will update the other patch. but I think now it's doesn't matter which one will be merged first it's easy to update the other one
https://github.com/llvm/llvm-project/pull/133100
More information about the cfe-commits
mailing list