[clang] [CIR][NFC] Add test coverage for static member call side effects (PR #239291)
Aman Maurya via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 23:26:36 PST 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/239291
>From 84ae12a44d60bb35cdcc306d8544a64983df63cc Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Thu, 5 Nov 2026 22:13:28 +0530
Subject: [PATCH 1/4] [CIR] Add test coverage for static member call side
effects
Add tests in clang/test/CIR/CodeGenCXX/static-member-call.cpp to verify that invoking a static member function through an instance or pointer expression emits the expected CIR and evaluates base expressions for side effects.
---
.../CIR/CodeGenCXX/static-member-call.cpp | 68 +++++++++++++++++++
1 file changed, 68 insertions(+)
create mode 100644 clang/test/CIR/CodeGenCXX/static-member-call.cpp
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..a6495c8974095d
--- /dev/null
+++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
@@ -0,0 +1,68 @@
+// 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);
+};
+
+S get_s();
+
+// 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);
+}
+
+// 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);
+}
>From 447ceeb7b013a174eea055578286b57df2544404 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Fri, 6 Nov 2026 01:47:29 +0530
Subject: [PATCH 2/4] [CIR] Add LLVM IR checks and verify store for static
member call side effects
---
.../CIR/CodeGenCXX/static-member-call.cpp | 30 +++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/clang/test/CIR/CodeGenCXX/static-member-call.cpp b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
index a6495c8974095d..7b50c9163050ab 100644
--- a/clang/test/CIR/CodeGenCXX/static-member-call.cpp
+++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
@@ -1,5 +1,7 @@
// 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 -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
// 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
@@ -10,6 +12,7 @@ struct S {
S get_s();
// CIR-LABEL: cir.func {{.*}} @_Z18test_instance_call1S
+// LLVM-LABEL: define {{.*}} @_Z18test_instance_call1S
// OGCG-LABEL: define {{.*}} @_Z18test_instance_call1S
int test_instance_call(S s) {
// CIR: %[[ARG:.*]] = cir.const #cir.int<42> : !s32i
@@ -18,12 +21,16 @@ int test_instance_call(S s) {
// CIR: %[[RES:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i
// CIR: cir.return %[[RES]] : !s32i
+ // LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}42)
+ // LLVM: ret i32 %[[CALL_LLVM]]
+
// 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
+// LLVM-LABEL: define {{.*}} @_Z15test_arrow_callP1S
// OGCG-LABEL: define {{.*}} @_Z15test_arrow_callP1S
int test_arrow_call(S *p) {
// CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i
@@ -32,6 +39,9 @@ int test_arrow_call(S *p) {
// CIR: %[[RES:.*]] = cir.load %[[RET]] : !cir.ptr<!s32i>, !s32i
// CIR: cir.return %[[RES]] : !s32i
+ // LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}100)
+ // LLVM: ret i32 %[[CALL_LLVM]]
+
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100)
// OGCG: ret i32 %[[CALL_OGCG]]
return p->static_func(100);
@@ -39,6 +49,7 @@ int test_arrow_call(S *p) {
// Verify that the instance expression is evaluated for side effects:
// CIR-LABEL: cir.func {{.*}} @_Z16test_side_effectv
+// LLVM-LABEL: define {{.*}} @_Z16test_side_effectv
// OGCG-LABEL: define {{.*}} @_Z16test_side_effectv
int test_side_effect() {
// CIR: cir.call @_Z5get_sv()
@@ -46,6 +57,10 @@ int test_side_effect() {
// CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
// CIR: cir.return %{{.*}} : !s32i
+ // LLVM: call void @_Z5get_sv()
+ // LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}42)
+ // LLVM: ret i32 %[[CALL_LLVM]]
+
// OGCG: call void @_Z5get_sv()
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42)
// OGCG: ret i32 %[[CALL_OGCG]]
@@ -54,14 +69,25 @@ int test_side_effect() {
// Verify that pointer expressions are evaluated for side effects:
// CIR-LABEL: cir.func {{.*}} @_Z20test_ptr_side_effectP1S
+// LLVM-LABEL: define {{.*}} @_Z20test_ptr_side_effectP1S
// OGCG-LABEL: define {{.*}} @_Z20test_ptr_side_effectP1S
int test_ptr_side_effect(S *p) {
- // CIR: cir.ptr_stride
+ // CIR: %[[P_ADDR:.*]] = cir.alloca "p"
+ // CIR: %[[P_INC:.*]] = cir.ptr_stride
+ // CIR: cir.store %[[P_INC]], %[[P_ADDR]]
// CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i
// CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
// CIR: cir.return %{{.*}} : !s32i
- // OGCG: getelementptr
+ // LLVM: %[[P_ADDR:.*]] = alloca ptr
+ // LLVM: %[[P_INC:.*]] = getelementptr {{.*}}
+ // LLVM: store ptr %[[P_INC]], ptr %[[P_ADDR]]
+ // LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}100)
+ // LLVM: ret i32 %[[CALL_LLVM]]
+
+ // OGCG: %[[P_ADDR:.*]] = alloca ptr
+ // OGCG: %[[P_INC:.*]] = getelementptr inbounds %struct.S, ptr %{{.*}}, i32 1
+ // OGCG: store ptr %[[P_INC]], ptr %[[P_ADDR]]
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100)
// OGCG: ret i32 %[[CALL_OGCG]]
return (p++)->static_func(100);
>From a9a9d79b5659b6a28e4b22da1f647dfa1e09a63b Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Fri, 6 Nov 2026 05:14:09 +0530
Subject: [PATCH 3/4] [CIR] Fix FileCheck directives for cir.store and LLVM IR
returns
---
clang/test/CIR/CodeGenCXX/static-member-call.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/test/CIR/CodeGenCXX/static-member-call.cpp b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
index 7b50c9163050ab..3d3b91cb6ba4dc 100644
--- a/clang/test/CIR/CodeGenCXX/static-member-call.cpp
+++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
@@ -22,7 +22,7 @@ int test_instance_call(S s) {
// CIR: cir.return %[[RES]] : !s32i
// LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}42)
- // LLVM: ret i32 %[[CALL_LLVM]]
+ // LLVM: ret i32 %{{.*}}
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42)
// OGCG: ret i32 %[[CALL_OGCG]]
@@ -40,7 +40,7 @@ int test_arrow_call(S *p) {
// CIR: cir.return %[[RES]] : !s32i
// LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}100)
- // LLVM: ret i32 %[[CALL_LLVM]]
+ // LLVM: ret i32 %{{.*}}
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100)
// OGCG: ret i32 %[[CALL_OGCG]]
@@ -59,7 +59,7 @@ int test_side_effect() {
// LLVM: call void @_Z5get_sv()
// LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}42)
- // LLVM: ret i32 %[[CALL_LLVM]]
+ // LLVM: ret i32 %{{.*}}
// OGCG: call void @_Z5get_sv()
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 42)
@@ -74,7 +74,7 @@ int test_side_effect() {
int test_ptr_side_effect(S *p) {
// CIR: %[[P_ADDR:.*]] = cir.alloca "p"
// CIR: %[[P_INC:.*]] = cir.ptr_stride
- // CIR: cir.store %[[P_INC]], %[[P_ADDR]]
+ // CIR: cir.store{{.*}} %[[P_INC]], %[[P_ADDR]]
// CIR: %[[ARG:.*]] = cir.const #cir.int<100> : !s32i
// CIR: %[[CALL:.*]] = cir.call @_ZN1S11static_funcEi(%[[ARG]]) : (!s32i {{.*}}) -> (!s32i {{.*}})
// CIR: cir.return %{{.*}} : !s32i
@@ -83,7 +83,7 @@ int test_ptr_side_effect(S *p) {
// LLVM: %[[P_INC:.*]] = getelementptr {{.*}}
// LLVM: store ptr %[[P_INC]], ptr %[[P_ADDR]]
// LLVM: %[[CALL_LLVM:.*]] = call {{.*}}i32 @_ZN1S11static_funcEi(i32 {{.*}}100)
- // LLVM: ret i32 %[[CALL_LLVM]]
+ // LLVM: ret i32 %{{.*}}
// OGCG: %[[P_ADDR:.*]] = alloca ptr
// OGCG: %[[P_INC:.*]] = getelementptr inbounds %struct.S, ptr %{{.*}}, i32 1
>From f67f806cc74284c87199377ff0cf3150c201f3c8 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 7 Nov 2026 12:56:01 +0530
Subject: [PATCH 4/4] [CIR] Update OGCG getelementptr check pattern in
static-member-call.cpp
---
clang/test/CIR/CodeGenCXX/static-member-call.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CIR/CodeGenCXX/static-member-call.cpp b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
index 3d3b91cb6ba4dc..de9a81dd01b849 100644
--- a/clang/test/CIR/CodeGenCXX/static-member-call.cpp
+++ b/clang/test/CIR/CodeGenCXX/static-member-call.cpp
@@ -86,7 +86,7 @@ int test_ptr_side_effect(S *p) {
// LLVM: ret i32 %{{.*}}
// OGCG: %[[P_ADDR:.*]] = alloca ptr
- // OGCG: %[[P_INC:.*]] = getelementptr inbounds %struct.S, ptr %{{.*}}, i32 1
+ // OGCG: %[[P_INC:.*]] = getelementptr inbounds {{.*}}%struct.S, ptr %{{.*}}, i32 1
// OGCG: store ptr %[[P_INC]], ptr %[[P_ADDR]]
// OGCG: %[[CALL_OGCG:.*]] = call noundef i32 @_ZN1S11static_funcEi(i32 noundef 100)
// OGCG: ret i32 %[[CALL_OGCG]]
More information about the cfe-commits
mailing list