Skip to content

Commit 7fcee5f

Browse files
authored
[WebAssembly] Add support for avgr_u in loops (llvm#153252)
Fixes llvm#150550. With the test case ``` void f(unsigned char *x, unsigned char *y, int n) { // should have been vectorized into avgr_u instead of seperated vectorized add and logical right shift for (int i = 0; i < n; i++) x[i] = (x[i] + y[i] + 1) / 2; } ``` the backend failed to recognize that this can be reduced to avgr_u since the loop vectorizer doesn't transform into the existing pattern in tablegen. This PR sets AVGCEIL_U as legal for v8i16 and v16i8 and selects it to avgr_u in the tablegen file.
1 parent e12689e commit 7fcee5f

File tree

3 files changed

+546
-0
lines changed

3 files changed

+546
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
246246
MVT::v2f64})
247247
setOperationAction(ISD::SPLAT_VECTOR, T, Legal);
248248

249+
setOperationAction(ISD::AVGCEILU, {MVT::v8i16, MVT::v16i8}, Legal);
250+
249251
// Custom lowering since wasm shifts must have a scalar shift amount
250252
for (auto Op : {ISD::SHL, ISD::SRA, ISD::SRL})
251253
for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64})

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,9 @@ def : Pat<(wasm_shr_u
11441144
(vec.splat (i32 1))),
11451145
(i32 1)),
11461146
(inst $lhs, $rhs)>;
1147+
1148+
def : Pat<(vec.vt (avgceilu (vec.vt V128:$lhs), (vec.vt V128:$rhs))),
1149+
(inst $lhs, $rhs)>;
11471150
}
11481151

11491152
// Widening dot product: i32x4.dot_i16x8_s

0 commit comments

Comments
 (0)