[clang] [CIR] Implement load of volatile reference (PR #246782)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 16:44:40 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Sachit-Juneja
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/246782.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenExpr.cpp (+1-5)
- (modified) clang/test/CIR/CodeGen/volatile.cpp (+23)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 2b2684c0781eb..fad9426ad720c 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 ffe40aa12a7ba..82aa63069f0c5 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]]
``````````
</details>
https://github.com/llvm/llvm-project/pull/246782
More information about the cfe-commits
mailing list