Skip to content
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

[InstCombine] Avoid simplifying bitcast of undef to a zeroinitializer vector #108872

Merged

Conversation

AlexMaclean
Copy link
Member

In some cases, if an undef value is the product of another instcombine simplification, a bitcast of undef is simplified to a zeroinitializer vector instead of undef.

@llvmbot
Copy link
Member

llvmbot commented Sep 16, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Alex MacLean (AlexMaclean)

Changes

In some cases, if an undef value is the product of another instcombine simplification, a bitcast of undef is simplified to a zeroinitializer vector instead of undef.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (+5)
  • (modified) llvm/test/Transforms/InstCombine/bitcast.ll (+24)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 5c9faa9449f539..ea51d779045718 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2323,6 +2323,11 @@ static Value *optimizeIntegerToVectorInsertions(BitCastInst &CI,
   auto *DestVecTy = cast<FixedVectorType>(CI.getType());
   Value *IntInput = CI.getOperand(0);
 
+  // if the int input is just an undef value do not try to optimize to vector
+  // insertions as it will prevent undef propagation
+  if (isa<UndefValue>(IntInput))
+    return nullptr;
+
   SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
   if (!collectInsertionElements(IntInput, 0, Elements,
                                 DestVecTy->getElementType(),
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index 4ab24ce7b925dc..ee3bfd8acc2969 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -879,3 +879,27 @@ define half @copysign_idiom_constant_wrong_type2(bfloat %x, i16 %mag) {
   %y = bitcast i16 %res to half
   ret half %y
 }
+
+define void @bitcast_undef_to_vector() {
+; CHECK-LABEL: @bitcast_undef_to_vector(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[END:%.*]]
+; CHECK:       unreachable:
+; CHECK-NEXT:    br label [[END]]
+; CHECK:       end:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %end
+
+unreachable:                                 ; No predecessors!
+  %0 = extractvalue { i32, i32 } zeroinitializer, 1
+  br label %end
+
+end:                                        ; preds = %unreachable, %entry
+  %1 = phi i32 [ %0, %unreachable ], [ undef, %entry ]
+  %2 = bitcast i32 %1 to <2 x i16>
+  %3 = extractelement <2 x i16> %2, i64 0
+  store i16 %3, ptr addrspace(1) null, align 2
+  ret void
+}

%1 = phi i32 [ %0, %unreachable ], [ undef, %entry ]
%2 = bitcast i32 %1 to <2 x i16>
%3 = extractelement <2 x i16> %2, i64 0
store i16 %3, ptr addrspace(1) null, align 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, did you use llvm-reduce to create this test case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

%1 = phi i32 [ %0, %unreachable ], [ undef, %entry ]
%2 = bitcast i32 %1 to <2 x i16>
%3 = extractelement <2 x i16> %2, i64 0
store i16 %3, ptr addrspace(1) null, align 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you ret i16 %3 here instead? Failing that, replace null with a pointer argument, to make this less UB.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, I've updated to a ret i16

@AlexMaclean AlexMaclean force-pushed the dev/amaclean/upstream-ic-undef-vect-cast branch from 55158a1 to d0601e7 Compare September 17, 2024 20:14
@AlexMaclean AlexMaclean merged commit 790f2eb into llvm:main Sep 17, 2024
6 of 7 checks passed
hamphet pushed a commit to hamphet/llvm-project that referenced this pull request Sep 18, 2024
… vector (llvm#108872)

In some cases, if an undef value is the product of another instcombine
simplification, a bitcast of undef is simplified to a zeroinitializer
vector instead of undef.
tmsri pushed a commit to tmsri/llvm-project that referenced this pull request Sep 19, 2024
… vector (llvm#108872)

In some cases, if an undef value is the product of another instcombine
simplification, a bitcast of undef is simplified to a zeroinitializer
vector instead of undef.
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.

4 participants