[clang] [Clang] Avoid volatile copy for inactive union init (PR #239870)
Thomas Lively via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 17:15:42 PST 2026
https://github.com/tlively created https://github.com/llvm/llvm-project/pull/239870
In AggExprEmitter::VisitBinAssign, Clang marks the destination slot volatile whenever RecordDecl::hasVolatileMember() is true on the LHS type, even when the RHS is a compound literal that initializes only non-volatile union members.
For example, musl defines pthread_mutex_t as a struct wrapping a union of non-volatile `int __i[...]` and `volatile int __vi[...]` arrays, and defines `PTHREAD_MUTEX_INITIALIZER` as `{{{0}}}`. Assigning a compound literal containing `PTHREAD_MUTEX_INITIALIZER`, such as `*s = (struct S){ .mutex = PTHREAD_MUTEX_INITIALIZER }`, initializes only the first, non-volatile union member `__i`, yet Clang previously treated the entire aggregate assignment as volatile. That emitted a stack temporary plus a volatile `llvm.memcpy`, preventing SROA and store-merging optimizations from eliminating the temporary copy.
Inspect the RHS expression in VisitBinAssign so that when an aggregate is assigned from a compound literal (or nested initializer lists), only the active initialized member of each union is checked for volatility. Non-initializer RHS expressions (such as copying an existing union or struct variable whose active member is unknown statically) continue to conservatively treat any volatile union member as active.
Assisted-By: Gemini
>From 50de15f9ab063c33106a0fd8197d5f9129109f74 Mon Sep 17 00:00:00 2001
From: Thomas Lively <tlively at google.com>
Date: Fri, 6 Nov 2026 16:39:28 -0800
Subject: [PATCH] [Clang] Avoid volatile copy for inactive union init
In AggExprEmitter::VisitBinAssign, Clang marks the destination slot volatile whenever RecordDecl::hasVolatileMember() is true on the LHS type, even when the RHS is a compound literal that initializes only non-volatile union members.
For example, musl defines pthread_mutex_t as a struct wrapping a union of non-volatile `int __i[...]` and `volatile int __vi[...]` arrays, and defines `PTHREAD_MUTEX_INITIALIZER` as `{{{0}}}`. Assigning a compound literal containing `PTHREAD_MUTEX_INITIALIZER`, such as `*s = (struct S){ .mutex = PTHREAD_MUTEX_INITIALIZER }`, initializes only the first, non-volatile union member `__i`, yet Clang previously treated the entire aggregate assignment as volatile. That emitted a stack temporary plus a volatile `llvm.memcpy`, preventing SROA and store-merging optimizations from eliminating the temporary copy.
Inspect the RHS expression in VisitBinAssign so that when an aggregate is assigned from a compound literal (or nested initializer lists), only the active initialized member of each union is checked for volatility. Non-initializer RHS expressions (such as copying an existing union or struct variable whose active member is unknown statically) continue to conservatively treat any volatile union member as active.
Assisted-By: Gemini
---
clang/lib/CodeGen/CGExprAgg.cpp | 61 +++++++-
clang/test/CodeGen/no-opt-volatile-memcpy.c | 152 +++++++++++++++++++-
2 files changed, 210 insertions(+), 3 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 73c6301c5d585..ed9fa3e13b661 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -1299,6 +1299,64 @@ void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
EmitFinalDestCopy(E->getType(), LV);
}
+/// Return true if initializing or assigning \p T from \p E accesses a volatile
+/// member. When \p E is an initializer list (or compound literal), only the
+/// active initialized member of each union is considered. A null \p E
+/// indicates default/value initialization of an omitted subobject.
+static bool hasVolatileMember(QualType T, const Expr *E, ASTContext &Ctx) {
+ if (T.isVolatileQualified())
+ return true;
+
+ const RecordDecl *RD = Ctx.getBaseElementType(T)->getAsRecordDecl();
+ if (!RD || !RD->hasVolatileMember())
+ return false;
+
+ if (E) {
+ E = E->IgnoreParenNoopCasts(Ctx);
+ if (E->getType().isVolatileQualified())
+ return true;
+ if (const auto *CLE = dyn_cast<CompoundLiteralExpr>(E))
+ E = CLE->getInitializer();
+ }
+
+ const auto *ILE = dyn_cast_or_null<InitListExpr>(E);
+ if (E && !ILE && !isa<ImplicitValueInitExpr>(E))
+ return true;
+
+ if (const auto *AT = Ctx.getAsArrayType(T)) {
+ QualType EltTy = AT->getElementType();
+ if (!ILE)
+ return hasVolatileMember(EltTy, nullptr, Ctx);
+ for (const Expr *Init : ILE->inits())
+ if (hasVolatileMember(EltTy, Init, Ctx))
+ return true;
+ return ILE->hasArrayFiller() &&
+ hasVolatileMember(EltTy, ILE->getArrayFiller(), Ctx);
+ }
+
+ if (RD->isUnion() && ILE) {
+ const FieldDecl *FD = ILE->getInitializedFieldInUnion();
+ const Expr *Init = ILE->getNumInits() ? ILE->getInit(0) : nullptr;
+ return FD && hasVolatileMember(FD->getType(), Init, Ctx);
+ }
+
+ unsigned InitIdx = 0;
+ for (const FieldDecl *FD : RD->fields()) {
+ if (FD->isUnnamedBitField())
+ continue;
+ if (FD->getType()->isIncompleteArrayType())
+ break;
+ const Expr *FieldInit = (ILE && InitIdx < ILE->getNumInits())
+ ? ILE->getInit(InitIdx++)
+ : nullptr;
+ if (hasVolatileMember(FD->getType(), FieldInit, Ctx))
+ return true;
+ if (RD->isUnion())
+ return false;
+ }
+ return false;
+}
+
void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
ApplyAtomGroup Grp(CGF.getDebugInfo());
// For an assignment to work, the value on the right has
@@ -1353,7 +1411,8 @@ void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
LHS, AggValueSlot::IsDestructed, needsGC(E->getLHS()->getType()),
AggValueSlot::IsAliased, AggValueSlot::MayOverlap);
// A non-volatile aggregate destination might have volatile member.
- if (!LHSSlot.isVolatile() && CGF.hasVolatileMember(E->getLHS()->getType()))
+ if (!LHSSlot.isVolatile() &&
+ hasVolatileMember(E->getLHS()->getType(), E->getRHS(), CGF.getContext()))
LHSSlot.setVolatile(true);
CGF.EmitAggExpr(E->getRHS(), LHSSlot);
diff --git a/clang/test/CodeGen/no-opt-volatile-memcpy.c b/clang/test/CodeGen/no-opt-volatile-memcpy.c
index 62335c564d3a7..d50fa15628317 100644
--- a/clang/test/CodeGen/no-opt-volatile-memcpy.c
+++ b/clang/test/CodeGen/no-opt-volatile-memcpy.c
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple=x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s
-// RUN: %if cir-enabled %{ %clang_cc1 -fclangir -triple=x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s %}
+// RUN: %clang_cc1 -triple=x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,LLVM
+// RUN: %if cir-enabled %{ %clang_cc1 -fclangir -DCIR -triple=x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s %}
struct s {
char filler [128];
@@ -35,3 +35,151 @@ void fee (void) {
// CHECK: call void @llvm.memcpy.{{.*}}(ptr align 4 @s, ptr align 4 @s, i64 132, i1 true)
// CHECK-NEXT: call void @llvm.memcpy.{{.*}}(ptr align 4 @s, ptr align 4 @gs, i64 132, i1 true)
+union u {
+ int i;
+ volatile int vi;
+};
+
+struct s2 {
+ char filler[128];
+ union u u;
+};
+
+struct s2 gs2;
+union u gu;
+
+void union_zero_init(void) {
+ gs2 = (struct s2){0};
+}
+// LLVM-LABEL: define{{.*}} void @union_zero_init()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 false)
+
+void union_nonvolatile_field(void) {
+ gs2 = (struct s2){ .u.i = 1 };
+}
+// LLVM-LABEL: define{{.*}} void @union_nonvolatile_field()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 false)
+
+void union_nonvolatile_field_from_volatile_load(void) {
+ gs2 = (struct s2){ .u.i = gs.x };
+}
+// LLVM-LABEL: define{{.*}} void @union_nonvolatile_field_from_volatile_load()
+// LLVM: load volatile i32, ptr getelementptr inbounds nuw (i8, ptr @gs, i64 128)
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 false)
+
+void union_nested_compound_literal(void) {
+ gs2 = (struct s2){ .u = (union u){ .i = 1 } };
+}
+// LLVM-LABEL: define{{.*}} void @union_nested_compound_literal()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 false)
+
+// ClangIR does not yet implement volatile aggregate lvalue-to-rvalue casts.
+#ifndef CIR
+void union_volatile_compound_literal(void) {
+ gs2 = (struct s2){ .u = ((volatile union u){0}) };
+}
+// LLVM-LABEL: define{{.*}} void @union_volatile_compound_literal()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 true)
+#endif
+
+void union_volatile_field(void) {
+ gs2 = (struct s2){ .u.vi = 1 };
+}
+// LLVM-LABEL: define{{.*}} void @union_volatile_field()
+// LLVM: store volatile i32 1
+// LLVM-NEXT: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 true)
+
+void union_copy_subobject(void) {
+ gs2 = (struct s2){ .u = gu };
+}
+// LLVM-LABEL: define{{.*}} void @union_copy_subobject()
+// Note: Copying @gu into the temporary is non-volatile because aggregate
+// initialization does not check volatility (see FIXME in EmitInitializationToLValue).
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 {{.*}}, ptr align 4 @gu, i64 4, i1 false)
+// LLVM-NEXT: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 {{.*}}, i64 132, i1 true)
+
+void union_struct_assign(void) {
+ gs2 = gs2;
+}
+// LLVM-LABEL: define{{.*}} void @union_struct_assign()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gs2, ptr align 4 @gs2, i64 132, i1 true)
+
+union u_vfirst {
+ int : 4;
+ volatile int vi;
+ int i;
+};
+
+union u_vfirst guvf;
+
+void union_empty_init_nonvolatile_first(void) {
+ gu = (union u){};
+}
+// LLVM-LABEL: define{{.*}} void @union_empty_init_nonvolatile_first()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gu, ptr align 4 {{.*}}, i64 4, i1 false)
+
+void union_empty_init_volatile_first(void) {
+ guvf = (union u_vfirst){};
+}
+// LLVM-LABEL: define{{.*}} void @union_empty_init_volatile_first()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @guvf, ptr align 4 {{.*}}, i64 4, i1 true)
+
+void union_override_volatile_first(void) {
+ guvf = (union u_vfirst){ .i = 1 };
+}
+// LLVM-LABEL: define{{.*}} void @union_override_volatile_first()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @guvf, ptr align 4 {{.*}}, i64 4, i1 false)
+
+struct s_complex {
+ volatile int : 4;
+ union u u_arr[2];
+ union u_vfirst uvf_arr[2];
+ union u_vfirst uvf;
+ union u trailing_u;
+ volatile int flex[];
+};
+
+struct s_complex gsc;
+
+void complex_nonvolatile(void) {
+ // Unnamed volatile bitfield, omitted non-volatile union array (`!ILE`),
+ // explicit non-volatile union array/field, omitted `trailing_u`, and
+ // trailing volatile flexible array member -> non-volatile memcpy.
+ gsc = (struct s_complex){ .uvf_arr = { { .i = 1 }, { .i = 2 } }, .uvf.i = 3 };
+}
+// LLVM-LABEL: define{{.*}} void @complex_nonvolatile()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gsc, ptr align 4 {{.*}}, i64 28, i1 false)
+
+void complex_omitted_volatile_array(void) {
+ gsc = (struct s_complex){ .uvf.i = 1 };
+}
+// LLVM-LABEL: define{{.*}} void @complex_omitted_volatile_array()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gsc, ptr align 4 {{.*}}, i64 28, i1 true)
+
+void complex_explicit_volatile_array_elt(void) {
+ gsc = (struct s_complex){ .u_arr = { { .i = 1 }, { .vi = 2 } },
+ .uvf_arr = { { .i = 3 }, { .i = 4 } },
+ .uvf.i = 5 };
+}
+// LLVM-LABEL: define{{.*}} void @complex_explicit_volatile_array_elt()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gsc, ptr align 4 {{.*}}, i64 28, i1 true)
+
+void complex_nonvolatile_array_filler(void) {
+ gsc = (struct s_complex){ .u_arr = { { .i = 1 } },
+ .uvf_arr = { { .i = 2 }, { .i = 3 } },
+ .uvf.i = 4 };
+}
+// LLVM-LABEL: define{{.*}} void @complex_nonvolatile_array_filler()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gsc, ptr align 4 {{.*}}, i64 28, i1 false)
+
+void complex_volatile_array_filler(void) {
+ gsc = (struct s_complex){ .uvf_arr = { { .i = 1 } }, .uvf.i = 2 };
+}
+// LLVM-LABEL: define{{.*}} void @complex_volatile_array_filler()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gsc, ptr align 4 {{.*}}, i64 28, i1 true)
+
+void complex_omitted_trailing_volatile_field(void) {
+ gsc = (struct s_complex){ .uvf_arr = { { .i = 1 }, { .i = 2 } } };
+}
+// LLVM-LABEL: define{{.*}} void @complex_omitted_trailing_volatile_field()
+// LLVM: call void @llvm.memcpy.{{.*}}(ptr align 4 @gsc, ptr align 4 {{.*}}, i64 28, i1 true)
More information about the cfe-commits
mailing list