[clang] [CIR] Emit memset when zero-initalizing a big struct (PR #244282)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 19 16:56:56 PST 2026
================
@@ -0,0 +1,87 @@
+// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVMCIR
+// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+// Large, mostly zero aggregate initializers are emitted as a memset of the
+// whole object followed by stores of the non-zero parts. This also zeroes the
+// padding.
+
+struct Big {
+ int a; // followed by 4 bytes of padding
+ long b[32];
+ int c; // followed by 4 bytes of padding
+};
+
+void assign_empty(Big *p) { *p = {}; }
+
+// CIR-LABEL: cir.func {{.*}} @_Z12assign_emptyP3Big
+// CIR: %[[TMP:.*]] = cir.alloca "ref.tmp0"
+// CIR: %[[VOID:.*]] = cir.cast bitcast %[[TMP]] : !cir.ptr<!rec_Big> -> !cir.ptr<!void>
+// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !u8i
+// CIR-NEXT: %[[SIZE:.*]] = cir.const #cir.int<272> : !u64i
+// CIR-NEXT: cir.libc.memset %[[SIZE]] bytes at %[[VOID]] align(8) to %[[ZERO]]
+// CIR-NOT: cir.store
+// CIR: cir.call @_ZN3BigaSEOS_({{.*}}, %[[TMP]])
+
+// LLVM-LABEL: define {{.*}} void @_Z12assign_emptyP3Big
+// LLVM: call void @llvm.memset.p0.i64(ptr align 8 %[[TMP:.*]], i8 0, i64 272, i1 false)
+// LLVM-NOT: store
+// LLVMCIR: call {{.*}} @_ZN3BigaSEOS_({{.*}}, ptr {{.*}} %[[TMP]])
----------------
andykaylor wrote:
A comment here saying why CIR doesn't replace the trivial copy would be useful.
https://github.com/llvm/llvm-project/pull/244282
More information about the cfe-commits
mailing list