Skip to content

[SelectionDAG] Use EVT::getIntegerVT in getBitcastedAnyExtOrTrunc #96658

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

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

shiltian
Copy link
Contributor

SelectionDAG::getBitcastedAnyExtOrTrunc assumes that there is always a valid
integer type corresponding to another type, which is not always true when it
comes to vector type. For example, <3 x i8> doesn't have a corresponding
integer type.

Fix SWDEV-464698.

@llvmbot llvmbot added backend:AMDGPU llvm:SelectionDAG SelectionDAGISel as well labels Jun 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2024

@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-selectiondag

Author: Shilei Tian (shiltian)

Changes

SelectionDAG::getBitcastedAnyExtOrTrunc assumes that there is always a valid
integer type corresponding to another type, which is not always true when it
comes to vector type. For example, &lt;3 x i8&gt; doesn't have a corresponding
integer type.

Fix SWDEV-464698.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+19-1)
  • (added) llvm/test/CodeGen/AMDGPU/no-corresponding-integer-type.ll (+13)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 8463e94d7f933..1d474543c2d3d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -1468,13 +1468,31 @@ SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
 }
 
 SDValue SelectionDAG::getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL,
-                                                 EVT VT) {
+                                                EVT VT) {
   assert(!VT.isVector());
   auto Type = Op.getValueType();
   SDValue DestOp;
   if (Type == VT)
     return Op;
   auto Size = Op.getValueSizeInBits();
+  auto IntTy = MVT::getIntegerVT(Size);
+
+  if (!IntTy.isValid()) {
+    // We assume integers of "weird" size have already been legalized here.
+    assert(Type.isVector());
+    unsigned NumElements = Type.getVectorNumElements();
+    unsigned ExtSize = VT.getScalarSizeInBits();
+    EVT ElementType = Type.getVectorElementType();
+    unsigned ExtNumElements = ExtSize / ElementType.getScalarSizeInBits();
+    assert(NumElements < ExtNumElements);
+    MVT ExtType = MVT::getVectorVT(ElementType.getSimpleVT(), ExtNumElements);
+    SmallVector<SDValue, 4> Ops(ExtNumElements, getUNDEF(ElementType));
+    SDValue ExtVec = getNode(ISD::BUILD_VECTOR, DL, ExtType, Ops);
+    DestOp = getNode(ISD::INSERT_SUBVECTOR, DL, ExtType, ExtVec, Op,
+                     getVectorIdxConstant(0, DL));
+    return getBitcast(VT, DestOp);
+  }
+
   DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
   if (DestOp.getValueType() == VT)
     return DestOp;
diff --git a/llvm/test/CodeGen/AMDGPU/no-corresponding-integer-type.ll b/llvm/test/CodeGen/AMDGPU/no-corresponding-integer-type.ll
new file mode 100644
index 0000000000000..16e17b7f25a3a
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/no-corresponding-integer-type.ll
@@ -0,0 +1,13 @@
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx942 %s -o -
+
+define void @no_corresponding_integer_type(i8 %arg, ptr addrspace(1) %ptr) {
+entry:
+  %load = load <3 x i8>, ptr addrspace(1) %ptr, align 1
+  %elt0 = extractelement <3 x i8> %load, i64 0
+  %mul0 = mul i8 %elt0, %arg
+  %or = or i8 %mul0, 1
+  %mul1 = mul i8 %arg, %arg
+  %add = add i8 %mul1, %or
+  store i8 %add, ptr addrspace(1) %ptr, align 1
+  ret void
+}

@shiltian shiltian requested review from arsenm and jrbyrnes June 25, 2024 15:47
@shiltian
Copy link
Contributor Author

gentle ping

@shiltian
Copy link
Contributor Author

gentle ping +1

…id integer type corresponding to a vector type

`SelectionDAG::getBitcastedAnyExtOrTrunc` assumes that there is always a valid
integer type corresponding to another type, which is not always true when it
comes to vector type. For example, `<3 x i8>` doesn't have a corresponding
integer type.

Fix SWDEV-464698.
@shiltian shiltian changed the title [SelectionDAG] Fix a false assumption that there will always be a valid integer type corresponding to a vector type [SelectionDAG] Use EVT::getIntegerVT in getBitcastedAnyExtOrTrunc Jul 1, 2024
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM

@shiltian shiltian merged commit 9a4f57e into llvm:main Jul 1, 2024
5 of 7 checks passed
@shiltian shiltian deleted the SWDEV-464698 branch July 1, 2024 19:11
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…llvm#96658)

`SelectionDAG::getBitcastedAnyExtOrTrunc` assumes that there is always a
valid
integer type corresponding to another type, which is not always true
when it
comes to vector type. For example, `<3 x i8>` doesn't have a
corresponding
integer type.

Fix SWDEV-464698.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
…llvm#96658)

`SelectionDAG::getBitcastedAnyExtOrTrunc` assumes that there is always a
valid
integer type corresponding to another type, which is not always true
when it
comes to vector type. For example, `<3 x i8>` doesn't have a
corresponding
integer type.

Fix SWDEV-464698.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AMDGPU llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants