[clang] [CIR] Support calling static member functions via instance in emitCXXMemberCallExpr (PR #237420)
Aman Maurya via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 13:54:23 PST 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/237420
>From 5a41f4cbece204ba22fa90b0e3cd4e8601535fbc Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 31 Oct 2026 03:58:58 +0530
Subject: [PATCH 1/2] [CIR] Support calling static member functions via
instance in emitCXXMemberCallExpr
When a member call expression targets a static method (e.g. obj.static_func()), emit the base object expression for side effects, retrieve the direct callee, and emit the call without passing a 'this' pointer.
Fixes #237402
---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 6 ++--
.../CIR/CodeGenCXX/static-member-call.cpp | 36 +++++++++++++++++++
2 files changed, 40 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CIR/CodeGenCXX/static-member-call.cpp
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 2637266cf3feed..5764e14fd81223 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2843,8 +2843,10 @@ RValue CIRGenFunction::emitCXXMemberCallExpr(const CXXMemberCallExpr *ce,
const auto *md = cast<CXXMethodDecl>(me->getMemberDecl());
if (md->isStatic()) {
- cgm.errorNYI(ce->getSourceRange(), "emitCXXMemberCallExpr: static method");
- return RValue::get(nullptr);
+ emitIgnoredExpr(me->getBase());
+ CIRGenCallee callee = emitDirectCallee(md);
+ return emitCall(getContext().getPointerType(md->getType()), callee, ce,
+ returnValue);
}
bool hasQualifier = me->hasQualifier();
diff --git a/clang/test/CIR/CodeGenCXX/static-member-call.cpp b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
new file mode 100644
index 00000000000000..5dc5ffe7d252c0
--- /dev/null
+++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -std=c++20 -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++20 -triple x86_64-unknown-linux-gnu -emit-llvm -disable-llvm-passes %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG
+
+struct S {
+ static int static_func(int a);
+};
+
+// CIR-LABEL: cir.func {{.*}} @_Z18test_instance_call1S
+// OGCG-LABEL: define {{.*}} @_Z18test_instance_call1S
+int test_instance_call(S s) {
+ // CIR: %[[ARG:.*]] = cir.const #cir.int<42> : !s32i
+ // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
+ // CIR: cir.store %[[CALL]], %[[RET:.*]] : !s32i, !cir.ptr<!s32i>
+ // CIR: %[[RES:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i
+ // CIR: cir.return %[[RES]] : !s32i
+
+ // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42)
+ // OGCG: ret i32 %[[CALL_OGCG]]
+ return s.static_func(42);
+}
+
+// CIR-LABEL: cir.func {{.*}} @_Z15test_arrow_callP1S
+// OGCG-LABEL: define {{.*}} @_Z15test_arrow_callP1S
+int test_arrow_call(S *p) {
+ // CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i
+ // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
+ // CIR: cir.store %[[CALL]], %[[RET:.*]] : !s32i, !cir.ptr<!s32i>
+ // CIR: %[[RES:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i
+ // CIR: cir.return %[[RES]] : !s32i
+
+ // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100)
+ // OGCG: ret i32 %[[CALL_OGCG]]
+ return p->static_func(100);
+}
>From 6a2fb738ffaa18973f3b77c5cc424371c40420d0 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Wed, 4 Nov 2026 03:22:43 +0530
Subject: [PATCH 2/2] [CIR] Add defensive parity comment in
emitCXXMemberCallExpr and side-effect tests
---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 3 ++
.../CIR/CodeGenCXX/static-member-call.cpp | 32 +++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 5764e14fd81223..078ddf1b95334f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -2842,6 +2842,9 @@ RValue CIRGenFunction::emitCXXMemberCallExpr(const CXXMemberCallExpr *ce,
const auto *me = cast<MemberExpr>(callee);
const auto *md = cast<CXXMethodDecl>(me->getMemberDecl());
+ // Although Sema normally represents static method calls as ordinary
+ // CallExpr nodes, retain defensive handling here for parity with classic
+ // Clang (CGExprCXX.cpp:200) in case a CXXMemberCallExpr node is encountered.
if (md->isStatic()) {
emitIgnoredExpr(me->getBase());
CIRGenCallee callee = emitDirectCallee(md);
diff --git a/clang/test/CIR/CodeGenCXX/static-member-call.cpp b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
index 5dc5ffe7d252c0..a6495c8974095d 100644
--- a/clang/test/CIR/CodeGenCXX/static-member-call.cpp
+++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
@@ -7,6 +7,8 @@ struct S {
static int static_func(int a);
};
+S get_s();
+
// CIR-LABEL: cir.func {{.*}} @_Z18test_instance_call1S
// OGCG-LABEL: define {{.*}} @_Z18test_instance_call1S
int test_instance_call(S s) {
@@ -34,3 +36,33 @@ int test_arrow_call(S *p) {
// OGCG: ret i32 %[[CALL_OGCG]]
return p->static_func(100);
}
+
+// Verify that the instance expression is evaluated for side effects:
+// CIR-LABEL: cir.func {{.*}} @_Z16test_side_effectv
+// OGCG-LABEL: define {{.*}} @_Z16test_side_effectv
+int test_side_effect() {
+ // CIR: cir.call @_Z5get_sv()
+ // CIR: %[[ARG:.*]] = cir.const #cir.int<42> : !s32i
+ // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
+ // CIR: cir.return %{{.*}} : !s32i
+
+ // OGCG: call void @_Z5get_sv()
+ // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42)
+ // OGCG: ret i32 %[[CALL_OGCG]]
+ return get_s().static_func(42);
+}
+
+// Verify that pointer expressions are evaluated for side effects:
+// CIR-LABEL: cir.func {{.*}} @_Z20test_ptr_side_effectP1S
+// OGCG-LABEL: define {{.*}} @_Z20test_ptr_side_effectP1S
+int test_ptr_side_effect(S *p) {
+ // CIR: cir.ptr_stride
+ // CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i
+ // CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
+ // CIR: cir.return %{{.*}} : !s32i
+
+ // OGCG: getelementptr
+ // OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100)
+ // OGCG: ret i32 %[[CALL_OGCG]]
+ return (p++)->static_func(100);
+}
More information about the cfe-commits
mailing list