Skip to content

Commit 3bd3395

Browse files
authored
[schedule][x86] Linearization and polynomial approximation (#310)
Adds vector linearization and math polynomial approximation. A collection of upstream patterns to improve SIMD code generation. Particularly, use of math approximation allows LLVM to generate fully vectorized assembly which falls into slow scalar path, otherwise. The approximation is applied by default in x86 pipeline in line with general fast math assumptions.
1 parent 3bd2772 commit 3bd3395

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎lighthouse/pipeline/descriptors/x86_64/lower.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Pipeline:
1111
- schedule: "vectorization.py[gen=vectorize_all]"
1212
- schedule: "vectorization.py[gen=lower_vector_multi_reductions]"
1313
- schedule: "vectorization.py[gen=flatten_vector_ops]"
14+
- schedule: "vectorization.py[gen=linearize_vector_ops]"
15+
- schedule: "math.py[gen=poly_approx]"
1416

1517
## Lower to LLVM
1618
- include: llvm-lowering.yaml

‎lighthouse/schedule/__init__.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from .vectorization import vectorize_linalg
1313
from .vectorization import vectorize_all
1414
from .vectorization import x86_vectorization
15+
from .vectorization import linearize_vector_ops
16+
from .math import poly_approx
1517
from .bufferization import bufferize
1618
from .debug import print_ir
1719
from .asserts import remove_asserts
@@ -26,8 +28,10 @@
2628
"flatten_vector_ops",
2729
"hoist_loops",
2830
"linalg_contract_fold_unit_dims",
31+
"linearize_vector_ops",
2932
"lower_vector_multi_reductions",
3033
"parallelize",
34+
"poly_approx",
3135
"print_ir",
3236
"remove_asserts",
3337
"schedule_boilerplate",

‎lighthouse/schedule/math.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from mlir import ir
2+
from mlir.dialects import transform
3+
from mlir.dialects.transform import math
4+
5+
from lighthouse.schedule.builders import schedule_boilerplate
6+
7+
8+
def poly_approx() -> ir.Module:
9+
"""
10+
Polynomial approximations for math functions.
11+
12+
Returns:
13+
Schedule
14+
"""
15+
with schedule_boilerplate() as (schedule, named_seq):
16+
with ir.InsertionPoint(
17+
transform.ApplyPatternsOp(named_seq.bodyTarget).patterns
18+
):
19+
math.ApplyF32ExpansionPatternsOp()
20+
math.ApplyPolynomialApproximationPatternsOp(enable_avx2=True)
21+
transform.yield_()
22+
return schedule

‎lighthouse/schedule/vectorization.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from mlir import ir
22
from mlir.dialects import transform
3-
from mlir.dialects.transform import structured, vector, tensor
3+
from mlir.dialects.transform import structured, vector, tensor, loop
44

55
from lighthouse.schedule.builders import schedule_boilerplate
66
import lighthouse.transform as lh_transform
@@ -145,3 +145,30 @@ def flatten_vector_ops() -> ir.Module:
145145
lh_transform.cleanup(named_seq.bodyTarget)
146146
transform.yield_()
147147
return schedule
148+
149+
150+
def linearize_vector_ops() -> ir.Module:
151+
"""
152+
ND vector (n >= 2) to 1D vector linearization.
153+
154+
Returns:
155+
Schedule
156+
"""
157+
with schedule_boilerplate() as (schedule, named_seq):
158+
linearize = transform.ApplyConversionPatternsOp(
159+
named_seq.bodyTarget,
160+
num_default_type_converter_region=1,
161+
legal_dialects=["vector"],
162+
partial_conversion=True,
163+
preserve_handles=True,
164+
)
165+
with ir.InsertionPoint(linearize.patterns.blocks.append()):
166+
vector.apply_conversion_patterns_vector_linearize(shuffle_like_ops=False)
167+
loop.apply_conversion_patterns_scf_structural_conversions()
168+
with ir.InsertionPoint(
169+
linearize.default_type_converter_region[0].blocks.append()
170+
):
171+
vector.VectorLinearizeTypeConverterOp()
172+
lh_transform.cleanup(named_seq.bodyTarget)
173+
transform.yield_()
174+
return schedule

0 commit comments

Comments
 (0)