-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InferAddressSpaces] Mark ConstantAggregateZero as safe to cast to a ConstantExpr addrspacecast #159695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[InferAddressSpaces] Mark ConstantAggregateZero as safe to cast to a ConstantExpr addrspacecast #159695
Conversation
…ConstantExpr addrspacecast This PR extends isSafeToCastConstAddrSpace to treat ConstantAggregateZero like ConstantPointerNull. Tests shows an extra addrspacecast instruction is removed and icmp pointer vector operand's address space is now inferred. This change is motivated by inspecting the test in commit f7629f5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR extends the InferAddressSpaces optimization to treat ConstantAggregateZero similarly to ConstantPointerNull when determining if it's safe to cast constants across address spaces. This allows the optimizer to infer address spaces for pointer vector comparisons with zero-initialized values and remove unnecessary addrspacecast instructions.
Key changes:
- Extends
isSafeToCastConstAddrSpace
to include ConstantAggregateZero alongside ConstantPointerNull - Enables address space inference for icmp operations with vector pointer operands against zero-initialized constants
- Eliminates redundant addrspacecast instructions in the optimized output
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
File | Description |
---|---|
llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp | Modified condition to treat ConstantAggregateZero as safe to cast |
llvm/test/Transforms/InferAddressSpaces/AMDGPU/phi-poison.ll | Updated test expectations showing removal of addrspacecast and proper address space inference |
llvm/test/Transforms/InferAddressSpaces/AMDGPU/issue110433.ll | Updated test expectations showing removal of redundant addrspacecast instruction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-backend-amdgpu Author: Wenju He (wenju-he) ChangesThis PR extends isSafeToCastConstAddrSpace to treat ConstantAggregateZero like ConstantPointerNull. This change is motivated by inspecting the test in commit f7629f5. Full diff: https://github.com/llvm/llvm-project/pull/159695.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index 7363ec38a4e3a..3ad87545953ff 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -1170,7 +1170,7 @@ bool InferAddressSpacesImpl::isSafeToCastConstAddrSpace(Constant *C,
if (SrcAS != FlatAddrSpace && NewAS != FlatAddrSpace)
return false;
- if (isa<ConstantPointerNull>(C))
+ if (isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C))
return true;
if (auto *Op = dyn_cast<Operator>(C)) {
diff --git a/llvm/test/Transforms/InferAddressSpaces/AMDGPU/issue110433.ll b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/issue110433.ll
index 4297d1c27639e..1928bb98cd2a7 100644
--- a/llvm/test/Transforms/InferAddressSpaces/AMDGPU/issue110433.ll
+++ b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/issue110433.ll
@@ -6,8 +6,7 @@ define <8 x i1> @load_vector_of_flat_ptr_from_constant(ptr addrspace(4) %ptr) {
; CHECK-SAME: ptr addrspace(4) [[PTR:%.*]]) {
; CHECK-NEXT: [[LD:%.*]] = load <8 x ptr>, ptr addrspace(4) [[PTR]], align 128
; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast <8 x ptr> [[LD]] to <8 x ptr addrspace(1)>
-; CHECK-NEXT: [[TMP2:%.*]] = addrspacecast <8 x ptr addrspace(1)> [[TMP1]] to <8 x ptr>
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq <8 x ptr> [[TMP2]], zeroinitializer
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq <8 x ptr addrspace(1)> [[TMP1]], <ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1))>
; CHECK-NEXT: ret <8 x i1> [[CMP]]
;
%ld = load <8 x ptr>, ptr addrspace(4) %ptr, align 128
diff --git a/llvm/test/Transforms/InferAddressSpaces/AMDGPU/phi-poison.ll b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/phi-poison.ll
index 6c60d9748ebc7..0ccf7e3df8af9 100644
--- a/llvm/test/Transforms/InferAddressSpaces/AMDGPU/phi-poison.ll
+++ b/llvm/test/Transforms/InferAddressSpaces/AMDGPU/phi-poison.ll
@@ -11,9 +11,8 @@ define void @phi_poison(ptr addrspace(1) %arg, <2 x ptr addrspace(1)> %arg1) {
; CHECK: merge:
; CHECK-NEXT: [[I:%.*]] = phi ptr addrspace(1) [ [[ARG:%.*]], [[LEADER]] ], [ poison, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[I2:%.*]] = phi <2 x ptr addrspace(1)> [ [[ARG1:%.*]], [[LEADER]] ], [ poison, [[ENTRY]] ]
-; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast <2 x ptr addrspace(1)> [[I2]] to <2 x ptr>
; CHECK-NEXT: [[J:%.*]] = icmp eq ptr addrspace(1) [[I]], addrspacecast (ptr null to ptr addrspace(1))
-; CHECK-NEXT: [[J1:%.*]] = icmp eq <2 x ptr> [[TMP1]], zeroinitializer
+; CHECK-NEXT: [[J1:%.*]] = icmp eq <2 x ptr addrspace(1)> [[I2]], <ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1)), ptr addrspace(1) addrspacecast (ptr null to ptr addrspace(1))>
; CHECK-NEXT: ret void
;
entry:
|
This PR extends isSafeToCastConstAddrSpace to treat ConstantAggregateZero like ConstantPointerNull.
Tests shows an extra addrspacecast instruction is removed and icmp pointer vector operand's address space is now inferred.
This change is motivated by inspecting the test in commit f7629f5.