Skip to content

Conversation

wenju-he
Copy link
Contributor

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.

…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.
Copy link
Contributor

@Copilot Copilot AI left a 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

@wenju-he wenju-he requested a review from Copilot September 19, 2025 02:41
Copy link
Contributor

@Copilot Copilot AI left a 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.

@llvmbot
Copy link
Member

llvmbot commented Sep 19, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-backend-amdgpu

Author: Wenju He (wenju-he)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/159695.diff

3 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp (+1-1)
  • (modified) llvm/test/Transforms/InferAddressSpaces/AMDGPU/issue110433.ll (+1-2)
  • (modified) llvm/test/Transforms/InferAddressSpaces/AMDGPU/phi-poison.ll (+1-2)
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:

@wenju-he wenju-he merged commit dd669c3 into llvm:main Sep 19, 2025
12 checks passed
@wenju-he wenju-he deleted the InferAddressSpaces-isSafeToCastConstAddrSpace-ConstantAggregateZero branch September 19, 2025 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants