Skip to content

Commit 96e040a

Browse files
authored
[mlir][ArmSVE] Add -arm-sve-legalize-vector-storage pass (#68794)
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. ## Loading and storing predicate types 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> ``` ## Relax alignments for SVE vector allocas 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. Depends on: #68586 and #68695 (for testing)
1 parent eb737d6 commit 96e040a

File tree

9 files changed

+772
-0
lines changed

9 files changed

+772
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
add_subdirectory(IR)
2+
add_subdirectory(Transforms)
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 Arm SVE vector storage.
21+
std::unique_ptr<Pass> createLegalizeVectorStoragePass();
22+
23+
/// Collect a set of patterns to legalize Arm SVE vector storage.
24+
void populateLegalizeVectorStoragePatterns(RewritePatternSet &patterns);
25+
26+
//===----------------------------------------------------------------------===//
27+
// Registration
28+
//===----------------------------------------------------------------------===//
29+
30+
/// Generate the code for registering passes.
31+
#define GEN_PASS_REGISTRATION
32+
#include "mlir/Dialect/ArmSVE/Transforms/Passes.h.inc"
33+
34+
} // namespace mlir::arm_sve
35+
36+
#endif // MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 addresses 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. Note: Non-powers-of-two masks (e.g.
32+
`vector<[7]xi1>`), which are not SVE predicates, are ignored.
33+
34+
For example:
35+
36+
```mlir
37+
%alloca = memref.alloca() : memref<vector<[4]xi1>>
38+
%mask = vector.constant_mask [4] : vector<[4]xi1>
39+
memref.store %mask, %alloca[] : memref<vector<[4]xi1>>
40+
%reload = memref.load %alloca[] : memref<vector<[4]xi1>>
41+
```
42+
Becomes:
43+
```mlir
44+
%alloca = memref.alloca() {alignment = 1 : i64} : memref<vector<[16]xi1>>
45+
%mask = vector.constant_mask [4] : vector<[4]xi1>
46+
%svbool = arm_sve.convert_to_svbool %mask : vector<[4]xi1>
47+
memref.store %svbool, %alloca[] : memref<vector<[16]xi1>>
48+
%reload_svbool = memref.load %alloca[] : memref<vector<[16]xi1>>
49+
%reload = arm_sve.convert_from_svbool %reload_svbool : vector<[4]xi1>
50+
```
51+
52+
## Relax alignments for SVE vector allocas
53+
54+
The storage for SVE vector types only needs to have an alignment that
55+
matches the element type (for example 4 byte alignment for `f32`s). However,
56+
the LLVM backend currently defaults to aligning to `base size` x
57+
`element size` bytes. For non-legal vector types like `vector<[8]xf32>` this
58+
results in 8 x 4 = 32-byte alignment, but the backend only supports up to
59+
16-byte alignment for SVE vectors on the stack. Explicitly setting a smaller
60+
alignment prevents this issue.
61+
}];
62+
let constructor = "mlir::arm_sve::createLegalizeVectorStoragePass()";
63+
let dependentDialects = ["func::FuncDialect",
64+
"memref::MemRefDialect", "vector::VectorDialect",
65+
"arm_sve::ArmSVEDialect"];
66+
}
67+
68+
#endif // MLIR_DIALECT_ARMSVE_TRANSFORMS_PASSES_TD

mlir/include/mlir/InitAllPasses.h

+2
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

+2
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)