[clang] [CIR] Add test coverage for static member call side effects (PR #239291)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 09:00:50 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Aman Maurya (amanmaurya92)
<details>
<summary>Changes</summary>
This patch adds tests for calling static member functions through instance and pointer expressions in ClangIR (`clang/test/CIR/CodeGenCXX/static-member-call.cpp`).
Under standard C++ Sema, calling a static member function via an object instance (`s.static_func()`) or pointer (`p->static_func()`) is lowered into a standard `CallExpr` with a `MemberExpr` callee.
This patch adds test coverage verifying that:
1. Direct instance calls (`s.static_func()`) emit expected CIR and OGCG LLVM IR.
2. Pointer arrow calls (`p->static_func()`) emit expected CIR and OGCG LLVM IR.
3. Temporary base object expressions are evaluated for side effects (`get_s().static_func()`).
4. Mutating pointer expressions are evaluated for side effects (`(p++)->static_func()`).
Split from #<!-- -->237420 as requested by @<!-- -->Men-cotton.
---
Full diff: https://github.com/llvm/llvm-project/pull/239291.diff
1 Files Affected:
- (added) clang/test/CIR/CodeGenCXX/static-member-call.cpp (+68)
``````````diff
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 0000000000000..a6495c8974095
--- /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);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/239291
More information about the cfe-commits
mailing list