[clang] [CIR] Upstream pointer arithmetic support (PR #138041)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 30 16:19:13 PDT 2025
================
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s
+
+// Should generate basic pointer arithmetics.
+void foo(int *iptr, char *cptr, unsigned ustride) {
+ iptr + 2;
+ // CHECK: %[[#STRIDE:]] = cir.const #cir.int<2> : !s32i
+ // CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s32i>, %[[#STRIDE]] : !s32i), !cir.ptr<!s32i>
+ cptr + 3;
+ // CHECK: %[[#STRIDE:]] = cir.const #cir.int<3> : !s32i
+ // CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s8i>, %[[#STRIDE]] : !s32i), !cir.ptr<!s8i>
+ iptr - 2;
+ // CHECK: %[[#STRIDE:]] = cir.const #cir.int<2> : !s32i
+ // CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#STRIDE]]) : !s32i, !s32i
+ // CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s32i>, %[[#NEGSTRIDE]] : !s32i), !cir.ptr<!s32i>
+ cptr - 3;
+ // CHECK: %[[#STRIDE:]] = cir.const #cir.int<3> : !s32i
+ // CHECK: %[[#NEGSTRIDE:]] = cir.unary(minus, %[[#STRIDE]]) : !s32i, !s32i
+ // CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s8i>, %[[#NEGSTRIDE]] : !s32i), !cir.ptr<!s8i>
+ iptr + ustride;
+ // CHECK: %[[#STRIDE:]] = cir.load %{{.+}} : !cir.ptr<!u32i>, !u32i
+ // CHECK: cir.ptr_stride(%{{.+}} : !cir.ptr<!s32i>, %[[#STRIDE]] : !u32i), !cir.ptr<!s32i>
+
+ // Must convert unsigned stride to a signed one.
+ iptr - ustride;
----------------
andykaylor wrote:
The `ptr_diff` operation hasn't been upstreamed yet.
https://github.com/llvm/llvm-project/pull/138041
More information about the cfe-commits
mailing list