Skip to content

Commit 8ef5da7

Browse files
committed
[WebAssembly] Fix crash when selecting 64 bit lane extract operand
The tablegen patterns on vector_extract only match i32 constants, but on wasm64 these come in as i64 constants. In certain situations this would cause crashes whenever it couldn't select an extract_vector_elt instruction. Rather than add duplicate patterns for every instruction, this just canonicalizes the constant to be i32 when lowering. Fixes llvm#57577 Differential Revision: https://reviews.llvm.org/D140205
1 parent 45067d1 commit 8ef5da7

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,11 +2245,16 @@ WebAssemblyTargetLowering::LowerAccessVectorElement(SDValue Op,
22452245
SelectionDAG &DAG) const {
22462246
// Allow constant lane indices, expand variable lane indices
22472247
SDNode *IdxNode = Op.getOperand(Op.getNumOperands() - 1).getNode();
2248-
if (isa<ConstantSDNode>(IdxNode) || IdxNode->isUndef())
2249-
return Op;
2250-
else
2251-
// Perform default expansion
2252-
return SDValue();
2248+
if (isa<ConstantSDNode>(IdxNode) || IdxNode->isUndef()) {
2249+
// Ensure the index type is i32 to match the tablegen patterns
2250+
uint64_t Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
2251+
SmallVector<SDValue, 3> Ops(Op.getNode()->ops());
2252+
Ops[Op.getNumOperands() - 1] =
2253+
DAG.getConstant(Idx, SDLoc(IdxNode), MVT::i32);
2254+
return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(), Ops);
2255+
}
2256+
// Perform default expansion
2257+
return SDValue();
22532258
}
22542259

22552260
static SDValue unrollVectorShift(SDValue Op, SelectionDAG &DAG) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: llc < %s -mattr=+simd128 -verify-machineinstrs -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals
2+
3+
; Regression test for a crash on wasm64 when trying to lower extract_vector_elt
4+
; with a 64 bit constant:
5+
;
6+
; t19: i64 = extract_vector_elt t18, Constant:i64<0>
7+
8+
target triple = "wasm64-unknown-unknown"
9+
10+
define void @foo() {
11+
store <4 x i32> zeroinitializer, ptr poison, align 16
12+
%1 = load <4 x i32>, ptr poison, align 16
13+
%2 = extractelement <4 x i32> %1, i32 0
14+
%3 = insertelement <2 x i32> undef, i32 %2, i32 0
15+
%4 = insertelement <2 x i32> %3, i32 poison, i32 1
16+
store <2 x i32> %4, ptr poison, align 8
17+
unreachable
18+
}

0 commit comments

Comments
 (0)