https://github.com/Sachit-Juneja created 
https://github.com/llvm/llvm-project/pull/246782

In C++, when loading a reference member from a volatile object, the load of the 
reference pointer itself must be a volatile load. CIR codegen previously 
diagnosed an NYI error for this construct.

This patch replaces the 'errorNYI' bail-out in emitLoadOfReference with 
'builder.createLoad' passing 'refLVal.isVolatile()', matching classic Clang 
codegen.

>From d65151bf13fd89e8ffabc8f0a15fdda311fca89d Mon Sep 17 00:00:00 2001
From: Sachit Juneja <[email protected]>
Date: Fri, 27 Nov 2026 16:22:17 -0500
Subject: [PATCH] [CIR] Implement load of volatile reference

---
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp |  6 +-----
 clang/test/CIR/CodeGen/volatile.cpp  | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 2b2684c0781eb4..fad9426ad720c4 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -3032,12 +3032,8 @@ RValue CIRGenFunction::emitReferenceBindingToExpr(const 
Expr *e) {
 
 Address CIRGenFunction::emitLoadOfReference(LValue refLVal, mlir::Location loc,
                                             LValueBaseInfo *pointeeBaseInfo) {
-  if (refLVal.isVolatile())
-    cgm.errorNYI(loc, "load of volatile reference");
-
   cir::LoadOp load =
-      cir::LoadOp::create(builder, loc, refLVal.getAddress().getElementType(),
-                          refLVal.getAddress().getPointer());
+      builder.createLoad(loc, refLVal.getAddress(), refLVal.isVolatile());
 
   assert(!cir::MissingFeatures::opTBAA());
 
diff --git a/clang/test/CIR/CodeGen/volatile.cpp 
b/clang/test/CIR/CodeGen/volatile.cpp
index ffe40aa12a7ba4..82aa63069f0c51 100644
--- a/clang/test/CIR/CodeGen/volatile.cpp
+++ b/clang/test/CIR/CodeGen/volatile.cpp
@@ -182,3 +182,26 @@ int A::get_x() volatile {
 // OGCG: define {{.*}} i32 @_ZNV1A5get_xEv
 // OGCG:   %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.A, ptr 
%{{.*}}, i32 0, i32 0
 // OGCG:   %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]
+
+struct VolatileRef {
+  int &r;
+};
+
+int test_load_volatile_reference(volatile VolatileRef *ptr) {
+  return ptr->r;
+}
+
+// CIR: cir.func {{.*}} @_Z28test_load_volatile_referencePV11VolatileRef
+// CIR:   %[[MEMBER_ADDR:.*]] = cir.get_member %{{.*}}[0] {name = "r"}
+// CIR:   %[[REF_ADDR:.*]] = cir.load volatile align(8) %[[MEMBER_ADDR]]
+// CIR:   cir.load align(4) %[[REF_ADDR]]
+
+// LLVM: define {{.*}} i32 @_Z28test_load_volatile_referencePV11VolatileRef
+// LLVM:   %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw 
%struct.VolatileRef, ptr %{{.*}}, i32 0, i32 0
+// LLVM:   %[[REF_ADDR:.*]] = load volatile ptr, ptr %[[MEMBER_ADDR]]
+// LLVM:   %{{.*}} = load i32, ptr %[[REF_ADDR]]
+
+// OGCG: define {{.*}} i32 @_Z28test_load_volatile_referencePV11VolatileRef
+// OGCG:   %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw 
%struct.VolatileRef, ptr %{{.*}}, i32 0, i32 0
+// OGCG:   %[[REF_ADDR:.*]] = load volatile ptr, ptr %[[MEMBER_ADDR]]
+// OGCG:   %{{.*}} = load i32, ptr %[[REF_ADDR]]

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to