[llvm] [InferAddressSpaces] Handle unconverted ptrmask (PR #140802)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 24 20:46:37 PDT 2025
================
@@ -14,11 +15,40 @@ define i8 @ptrmask_cast_local_to_flat(ptr addrspace(3) %src.ptr, i64 %mask) {
ret i8 %load
}
+; Casting null does not necessarily result in null again ...
+define i8 @ptrmask_cast_local_null_to_flat(i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_local_null_to_flat(
+; CHECK-NEXT: [[MASKED:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr addrspacecast (ptr addrspace(3) null to ptr), i64 [[MASK:%.*]])
+; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast ptr [[MASKED]] to ptr addrspace(3)
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr addrspace(3) [[TMP1]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast ptr addrspace(3) null to ptr
+ %masked = call ptr @llvm.ptrmask.p0.i64(ptr %cast, i64 %mask)
+ %load = load i8, ptr %masked
+ ret i8 %load
+}
+
+; ... exception: addrspace 0 casted to a smaller addrspace (by default we assume
+; that casting to a smaller addrspace = truncating)
+define i8 @ptrmask_cast_flat_null_to_local(i32 %mask) {
+; CHECK-LABEL: @ptrmask_cast_flat_null_to_local(
+; CHECK-NEXT: [[MASKED:%.*]] = call ptr addrspace(3) @llvm.ptrmask.p3.i32(ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), i32 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr addrspace(3) [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast ptr null to ptr addrspace(3)
+ %masked = call ptr addrspace(3) @llvm.ptrmask.p3.i32(ptr addrspace(3) %cast, i32 %mask)
----------------
arsenm wrote:
```suggestion
%masked = call ptr addrspace(3) @llvm.ptrmask.p3.i32(ptr addrspace(3) addrspacecast (ptr null to ptr addrspace(3)), i32 %mask)
```
Write as the constantexpr this will fold to; this expanded as an instruction is a degenerate special case
https://github.com/llvm/llvm-project/pull/140802
More information about the llvm-commits
mailing list