[clang] [CIR] Lower get_element's leading zero with the index's type (PR #242404)

Adam Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 16 10:54:09 PST 2026


================
@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -cl-std=CL2.0 -O0 -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -cl-std=CL2.0 -O0 -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVMCIR
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -cl-std=CL2.0 -O0 -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG
+
+void private_array(int i) {
+  int a[4];
+  a[i] = 1;
+}
+
+// CIR-LABEL: cir.func {{.*}} @private_array(
+// CIR:         %[[A:.*]] = cir.alloca "a" {{.*}} : !cir.ptr<!cir.array<!s32i x 4>, target_address_space(5)>
+// CIR:         %[[IDX:.*]] = cir.cast integral %{{.+}} : !s32i -> !s64i
+// CIR-NEXT:    cir.get_element inbounds %[[A]][%[[IDX]] : !s64i] : !cir.ptr<!cir.array<!s32i x 4>, target_address_space(5)> -> !cir.ptr<!s32i, target_address_space(5)>
+
+// LLVM-LABEL: define {{.*}} void @private_array(i32 noundef %{{.+}})
+// LLVM:         %[[A:.*]] = alloca [4 x i32], align 4, addrspace(5)
+// LLVM:         %[[SEXT:.*]] = sext i32 %{{.+}} to i64
+// LLVMCIR-NEXT: %[[TRUNC:.*]] = trunc i64 %[[SEXT]] to i32
+// LLVMCIR-NEXT: getelementptr inbounds [4 x i32], ptr addrspace(5) %[[A]], i32 0, i32 %[[TRUNC]]
----------------
adams381 wrote:

That comes from `convertToIndexTy`, which the `get_element` and `ptr_stride` lowerings share.  It casts the index to the pointer's index width from the data layout, and AMDGPU's private and local pointers are 32 bits (`p5:32:32`, `p3:32:32`).  So the index is truncated to i32, and the zero takes the same type, which is also what the raw constant gave before this PR.  Classic codegen keeps i64 and leaves the truncation to LLVM.  This PR doesn't change that, and the test pins the current difference.


https://github.com/llvm/llvm-project/pull/242404


More information about the cfe-commits mailing list