Author: Erich Keane Date: 2026-09-22T13:48:13-07:00 New Revision: dc245a0c5efb1fdc5344c1c8964323ae5219cf9c
URL: https://github.com/llvm/llvm-project/commit/dc245a0c5efb1fdc5344c1c8964323ae5219cf9c DIFF: https://github.com/llvm/llvm-project/commit/dc245a0c5efb1fdc5344c1c8964323ae5219cf9c.diff LOG: [CIR] Fixup 'zero' branch of Null Initialization (#225490) The zero branch has a test for an NYI based on whether the thing is a VLA or not. However, classic codegen uses 'dyn_cast_or_null', and we used 'isa', so a nullptr caused an assert. This patch fixes it. Added: Modified: clang/lib/CIR/CodeGen/CIRGenFunction.cpp clang/test/CIR/CodeGen/class.cpp Removed: ################################################################################ diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp index 64572144b5d83..d10c1802be539 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.cpp @@ -1383,7 +1383,7 @@ void CIRGenFunction::emitNullInitialization(mlir::Location loc, Address destPtr, const CharUnits size = getContext().getTypeSizeInChars(ty); if (size.isZero()) { // But note that getTypeInfo returns 0 for a VLA. - if (isa<VariableArrayType>(getContext().getAsArrayType(ty))) { + if (isa_and_nonnull<VariableArrayType>(getContext().getAsArrayType(ty))) { cgm.errorNYI(loc, "emitNullInitialization for zero size VariableArrayType"); } else { diff --git a/clang/test/CIR/CodeGen/class.cpp b/clang/test/CIR/CodeGen/class.cpp index b04899feb29ff..afa5aab3f2d76 100644 --- a/clang/test/CIR/CodeGen/class.cpp +++ b/clang/test/CIR/CodeGen/class.cpp @@ -136,3 +136,67 @@ void usesTemplate() { takesTemplate(&Template<char>::m); } // OGCG: define dso_local void @_Z12usesTemplatev // OGCG: call void @_Z13takesTemplateM8TemplateIcEi( + +struct JustFam { + int m[0]; +}; + +void fam_1() { + JustFam a; + (void)a.m; +} +// CIR: cir.func{{.*}}@_Z5fam_1v +// CIR: %[[A:.*]] = cir.alloca "a" align(4) : !cir.ptr<!rec_JustFam> +// CIR: cir.get_member %[[A]][0] {name = "m"} : !cir.ptr<!rec_JustFam> -> !cir.ptr<!cir.array<!s32i x 0>> + +// LLVM: define{{.*}}@_Z5fam_1v +// LLVM: %[[A:.*]] = alloca %struct.JustFam, align 4 +// LLVM: getelementptr inbounds nuw %struct.JustFam, ptr %[[A]], i32 0, i32 0 + +// OGCG: define{{.*}}@_Z5fam_1v +// OGCG: %[[A:.*]] = alloca %struct.JustFam, align 4 +// OGCG: getelementptr inbounds nuw %struct.JustFam, ptr %[[A]], i32 0, i32 0 + +void fam_2() { + JustFam a = JustFam(); + (void)a.m; +} + +// CIR: cir.func{{.*}}@_Z5fam_2v +// CIR: %[[A:.*]] = cir.alloca "a" align(4) init : !cir.ptr<!rec_JustFam> +// CIR: cir.get_member %[[A]][0] {name = "m"} : !cir.ptr<!rec_JustFam> -> !cir.ptr<!cir.array<!s32i x 0>> + +// LLVM: define{{.*}}@_Z5fam_2v +// LLVM: %[[A:.*]] = alloca %struct.JustFam, align 4 +// LLVM: getelementptr inbounds nuw %struct.JustFam, ptr %[[A]], i32 0, i32 0 + +// OGCG: define{{.*}}@_Z5fam_2v +// OGCG: %[[A:.*]] = alloca %struct.JustFam, align 4 +// OGCG: getelementptr inbounds nuw %struct.JustFam, ptr %[[A]], i32 0, i32 0 + +void fam_3() { + JustFam *a = new JustFam(); + (void)a->m; +} +// CIR: cir.func{{.*}}@_Z5fam_3v +// CIR: %[[A:.*]] = cir.alloca "a" align(8) init : !cir.ptr<!cir.ptr<!rec_JustFam>> +// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !u64i +// CIR: %[[NEW:.*]] = cir.call @_Znwm(%[[ZERO]]) {allocsize = array<i32: 0>, builtin} +// CIR: %[[NEW_TO_A:.*]] = cir.cast bitcast %[[NEW]] : !cir.ptr<!void> -> !cir.ptr<!rec_JustFam> +// CIR: cir.store align(8) %[[NEW_TO_A]], %[[A]] : !cir.ptr<!rec_JustFam>, !cir.ptr<!cir.ptr<!rec_JustFam>> +// CIR: %[[LOAD_A:.*]] = cir.load align(8) %[[A]] : !cir.ptr<!cir.ptr<!rec_JustFam>>, !cir.ptr<!rec_JustFam> +// CIR: cir.get_member %[[LOAD_A]][0] {name = "m"} : !cir.ptr<!rec_JustFam> -> !cir.ptr<!cir.array<!s32i x 0>> + +// LLVM: define{{.*}}@_Z5fam_3v +// LLVM: %[[A:.*]] = alloca ptr, align 8 +// LLVM: %[[NEW:.*]] = call noundef nonnull ptr @_Znwm(i64 noundef 0) +// LLVM: store ptr %[[NEW]], ptr %[[A]], align 8 +// LLVM: %[[LOAD_A:.*]] = load ptr, ptr %[[A]], align 8 +// LLVM: getelementptr inbounds nuw %struct.JustFam, ptr %[[LOAD_A]], i32 0, i32 0 + +// OGCG: define{{.*}}@_Z5fam_3v +// OGCG: %[[A:.*]] = alloca ptr, align 8 +// OGCG: %[[NEW:.*]] = call noalias noundef nonnull ptr @_Znwm(i64 noundef 0) +// OGCG: store ptr %[[NEW]], ptr %[[A]], align 8 +// OGCG: %[[LOAD_A:.*]] = load ptr, ptr %[[A]], align 8 +// OGCG: getelementptr inbounds nuw %struct.JustFam, ptr %[[LOAD_A]], i32 0, i32 0 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
