Skip to content

Commit 2ba4626

Browse files
committed
[mlir][ArmSVE] Add -arm-sve-legalize-vector-storage pass
This patch adds a pass that ensures that loads, stores, and allocations of SVE vector types will be legal in the LLVM backend. It does this at the memref level, so this pass must be applied before lowering all the way to LLVM. This pass currently fixes two issues. It is only legal to load/store predicate types equal to (or greater than) a full predicate register, which in MLIR is `vector<[16]xi1>`. Smaller predicate types (`vector<[1|2|4|8]xi1>`) must be converted to/from a full predicate type (referred to as a `svbool`) before and after storing and loading respectively. This pass does this by widening allocations and inserting conversion intrinsics. For example: ```mlir %alloca = memref.alloca() : memref<vector<[4]xi1>> %mask = vector.constant_mask [4] : vector<[4]xi1> memref.store %mask, %alloca[] : memref<vector<[4]xi1>> %reload = memref.load %alloca[] : memref<vector<[4]xi1>> ``` Becomes: ```mlir %alloca = memref.alloca() {alignment = 1 : i64} : memref<vector<[16]xi1>> %mask = vector.constant_mask [4] : vector<[4]xi1> %svbool = arm_sve.convert_to_svbool %mask : vector<[4]xi1> memref.store %svbool, %alloca[] : memref<vector<[16]xi1>> %reload_svbool = memref.load %alloca[] : memref<vector<[16]xi1>> %reload = arm_sve.convert_from_svbool %reload_svbool : vector<[4]xi1> ``` The storage for SVE vector types only needs to have an alignment that matches the element type (for example 4 byte alignment for `f32`s). However, the LLVM backend currently defaults to aligning to `base size` x `element size` bytes. For non-legal vector types like `vector<[8]xf32>` this results in 8 x 4 = 32-byte alignment, but the backend only supports up to 16-byte alignment for SVE vectors on the stack. Explicitly setting a smaller alignment prevents this issue.
1 parent 97f1db2 commit 2ba4626

File tree

9 files changed

+701
-0
lines changed

9 files changed

+701
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
add_subdirectory(IR)
2+
add_subdirectory(Transforms)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(LLVM_TARGET_DEFINITIONS Passes.td)
2+
mlir_tablegen(Passes.h.inc -gen-pass-decls -name ArmSVE)
3+
add_public_tablegen_target(MLIRArmSVEPassIncGen)
4+
5+
add_mlir_doc(Passes ArmSVEPasses ./ -gen-pass-doc)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===- Passes.h - Pass Entrypoints ------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_H
10+
#define MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_H
11+
12+
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
13+
#include "mlir/Pass/Pass.h"
14+
15+
namespace mlir::arm_sve {
16+
17+
#define GEN_PASS_DECL
18+
#include "mlir/Dialect/ArmSVE/Transforms/Passes.h.inc"
19+
20+
/// Pass to legalize the types of mask stores.
21+
std::unique_ptr<Pass> createLegalizeVectorStoragePass();
22+
23+
//===----------------------------------------------------------------------===//
24+
// Registration
25+
//===----------------------------------------------------------------------===//
26+
27+
/// Generate the code for registering passes.
28+
#define GEN_PASS_REGISTRATION
29+
#include "mlir/Dialect/ArmSVE/Transforms/Passes.h.inc"
30+
31+
} // namespace mlir::arm_sve
32+
33+
#endif // MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_H
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===-- Passes.td - ArmSVE pass definition file ------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_TD
10+
#define MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_TD
11+
12+
include "mlir/Pass/PassBase.td"
13+
14+
def LegalizeVectorStorage
15+
: Pass<"arm-sve-legalize-vector-storage", "mlir::func::FuncOp"> {
16+
let summary = "Ensures stores of SVE vector types will be legal";
17+
let description = [{
18+
This pass ensures that loads, stores, and allocations of SVE vector types
19+
will be legal in the LLVM backend. It does this at the memref level, so this
20+
pass must be applied before lowering all the way to LLVM.
21+
22+
This pass currently fixes two issues.
23+
24+
## Loading and storing predicate types
25+
26+
It is only legal to load/store predicate types equal to (or greater than) a
27+
full predicate register, which in MLIR is `vector<[16]xi1>`. Smaller
28+
predicate types (`vector<[1|2|4|8]xi1>`) must be converted to/from a full
29+
predicate type (referred to as a `svbool`) before and after storing and
30+
loading respectively. This pass does this by widening allocations and
31+
inserting conversion intrinsics.
32+
33+
For example:
34+
35+
```mlir
36+
%alloca = memref.alloca() : memref<vector<[4]xi1>>
37+
%mask = vector.constant_mask [4] : vector<[4]xi1>
38+
memref.store %mask, %alloca[] : memref<vector<[4]xi1>>
39+
%reload = memref.load %alloca[] : memref<vector<[4]xi1>>
40+
```
41+
Becomes:
42+
```mlir
43+
%alloca = memref.alloca() {alignment = 1 : i64} : memref<vector<[16]xi1>>
44+
%mask = vector.constant_mask [4] : vector<[4]xi1>
45+
%svbool = arm_sve.convert_to_svbool %mask : vector<[4]xi1>
46+
memref.store %svbool, %alloca[] : memref<vector<[16]xi1>>
47+
%reload_svbool = memref.load %alloca[] : memref<vector<[16]xi1>>
48+
%reload = arm_sve.convert_from_svbool %reload_svbool : vector<[4]xi1>
49+
```
50+
51+
## Relax alignments for SVE vector allocas
52+
53+
The storage for SVE vector types only needs to have an alignment that
54+
matches the element type (for example 4 byte alignment for `f32`s). However,
55+
the LLVM backend currently defaults to aligning to `base size` x
56+
`element size` bytes. For non-legal vector types like `vector<[8]xf32>` this
57+
results in 8 x 4 = 32-byte alignment, but the backend only supports up to
58+
16-byte alignment for SVE vectors on the stack. Explicitly setting a smaller
59+
alignment prevents this issue.
60+
}];
61+
let constructor = "mlir::arm_sve::createLegalizeVectorStoragePass()";
62+
let dependentDialects = ["func::FuncDialect",
63+
"memref::MemRefDialect", "vector::VectorDialect",
64+
"arm_sve::ArmSVEDialect"];
65+
}
66+
67+
#endif // MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_TD

mlir/include/mlir/InitAllPasses.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mlir/Dialect/Affine/Passes.h"
2020
#include "mlir/Dialect/Arith/Transforms/Passes.h"
2121
#include "mlir/Dialect/ArmSME/Transforms/Passes.h"
22+
#include "mlir/Dialect/ArmSVE/Transforms/Passes.h"
2223
#include "mlir/Dialect/Async/Passes.h"
2324
#include "mlir/Dialect/Bufferization/Pipelines/Passes.h"
2425
#include "mlir/Dialect/Bufferization/Transforms/Passes.h"
@@ -82,6 +83,7 @@ inline void registerAllPasses() {
8283
transform::registerTransformPasses();
8384
vector::registerVectorPasses();
8485
arm_sme::registerArmSMEPasses();
86+
arm_sve::registerArmSVEPasses();
8587

8688
// Dialect pipelines
8789
bufferization::registerBufferizationPipelines();

mlir/lib/Dialect/ArmSVE/Transforms/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
add_mlir_dialect_library(MLIRArmSVETransforms
22
LegalizeForLLVMExport.cpp
3+
LegalizeVectorStorage.cpp
34

45
DEPENDS
56
MLIRArmSVEConversionsIncGen
7+
MLIRArmSVEPassIncGen
68

79
LINK_LIBS PUBLIC
810
MLIRArmSVEDialect

0 commit comments

Comments
 (0)