Skip to content

Commit 9697f4b

Browse files
authored
[WebAssembly][FastISel] Bail out on meeting non-integer type in selectTrunc (llvm#167165)
Fixes llvm#165438 With `simd128` enabled, we may meet vector type truncation in FastISel. To respect llvm#138479, this patch merely bails out on non-integer IR types, though I prefer bailing out for all non-simple types as most targets (X86, AArch64) do.
1 parent 1e467e4 commit 9697f4b

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -988,20 +988,36 @@ bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
988988
bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
989989
const auto *Trunc = cast<TruncInst>(I);
990990

991-
Register Reg = getRegForValue(Trunc->getOperand(0));
992-
if (Reg == 0)
991+
const Value *Op = Trunc->getOperand(0);
992+
MVT::SimpleValueType From = getSimpleType(Op->getType());
993+
MVT::SimpleValueType To = getLegalType(getSimpleType(Trunc->getType()));
994+
Register In = getRegForValue(Op);
995+
if (In == 0)
993996
return false;
994997

995-
unsigned FromBitWidth = Trunc->getOperand(0)->getType()->getIntegerBitWidth();
996-
unsigned ToBitWidth = Trunc->getType()->getIntegerBitWidth();
998+
auto Truncate = [&](Register Reg) -> unsigned {
999+
if (From == MVT::i64) {
1000+
if (To == MVT::i64)
1001+
return copyValue(Reg);
1002+
1003+
if (To == MVT::i1 || To == MVT::i8 || To == MVT::i16 || To == MVT::i32) {
1004+
Register Result = createResultReg(&WebAssembly::I32RegClass);
1005+
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1006+
TII.get(WebAssembly::I32_WRAP_I64), Result)
1007+
.addReg(Reg);
1008+
return Result;
1009+
}
1010+
}
9971011

998-
if (ToBitWidth <= 32 && (32 < FromBitWidth && FromBitWidth <= 64)) {
999-
Register Result = createResultReg(&WebAssembly::I32RegClass);
1000-
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1001-
TII.get(WebAssembly::I32_WRAP_I64), Result)
1002-
.addReg(Reg);
1003-
Reg = Result;
1004-
}
1012+
if (From == MVT::i32)
1013+
return copyValue(Reg);
1014+
1015+
return 0;
1016+
};
1017+
1018+
unsigned Reg = Truncate(In);
1019+
if (Reg == 0)
1020+
return false;
10051021

10061022
updateValueMap(Trunc, Reg);
10071023
return true;

llvm/test/CodeGen/WebAssembly/fast-isel-pr138479.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=1 -verify-machineinstrs | FileCheck %s
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
2+
; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=0 -verify-machineinstrs | FileCheck %s
23

34
target triple = "wasm32-unknown-unknown"
45

@@ -13,3 +14,5 @@ define void @call_trunc_i64_to_i48(i64 %x) {
1314
call void @extern48(i48 %x48)
1415
ret void
1516
}
17+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
18+
; CHECK: {{.*}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
2+
; RUN: llc < %s -fast-isel -fast-isel-abort=0 -mattr=+simd128 -verify-machineinstrs | FileCheck %s
3+
4+
target triple = "wasm32-unknown-unknown"
5+
6+
define i8 @pr165438(<4 x i32> %0) {
7+
; CHECK-LABEL: pr165438:
8+
; CHECK: .functype pr165438 (v128) -> (i32)
9+
; CHECK-NEXT: # %bb.0: # %entry
10+
; CHECK-NEXT: local.get 0
11+
; CHECK-NEXT: local.get 0
12+
; CHECK-NEXT: i8x16.shuffle 0, 4, 8, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
13+
; CHECK-NEXT: i8x16.extract_lane_u 0
14+
; CHECK-NEXT: # fallthrough-return
15+
entry:
16+
%conv = trunc <4 x i32> %0 to <4 x i8>
17+
br label %cond.true
18+
19+
20+
cond.true: ; preds = %entry
21+
%vecext = extractelement <4 x i8> %conv, i32 0
22+
ret i8 %vecext
23+
}

0 commit comments

Comments
 (0)