1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
//===- TestEmulateNarrowType.cpp - Test Narrow Type Emulation ------*- c++
//-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Arith/Transforms/NarrowTypeEmulationConverter.h"
#include "mlir/Dialect/Arith/Transforms/Passes.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/MemRef/Transforms/Transforms.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
using namespace mlir;
namespace {
struct TestEmulateNarrowTypePass
: public PassWrapper<TestEmulateNarrowTypePass,
OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestEmulateNarrowTypePass)
TestEmulateNarrowTypePass() = default;
TestEmulateNarrowTypePass(const TestEmulateNarrowTypePass &pass)
: PassWrapper(pass) {}
void getDependentDialects(DialectRegistry ®istry) const override {
registry
.insert<arith::ArithDialect, func::FuncDialect, memref::MemRefDialect,
vector::VectorDialect, affine::AffineDialect>();
}
StringRef getArgument() const final { return "test-emulate-narrow-int"; }
StringRef getDescription() const final {
return "Function pass to test Narrow Integer Emulation";
}
void runOnOperation() override {
if (!llvm::isPowerOf2_32(loadStoreEmulateBitwidth) ||
loadStoreEmulateBitwidth < 8) {
signalPassFailure();
return;
}
Operation *op = getOperation();
MLIRContext *ctx = op->getContext();
arith::NarrowTypeEmulationConverter typeConverter(loadStoreEmulateBitwidth);
// Convert scalar type.
typeConverter.addConversion([this](IntegerType ty) -> std::optional<Type> {
unsigned width = ty.getWidth();
if (width >= arithComputeBitwidth)
return ty;
return IntegerType::get(ty.getContext(), arithComputeBitwidth);
});
// Convert vector type.
typeConverter.addConversion([this](VectorType ty) -> std::optional<Type> {
auto intTy = dyn_cast<IntegerType>(ty.getElementType());
if (!intTy)
return ty;
unsigned width = intTy.getWidth();
if (width >= arithComputeBitwidth)
return ty;
return VectorType::get(
to_vector(ty.getShape()),
IntegerType::get(ty.getContext(), arithComputeBitwidth));
});
// With the type converter enabled, we are effectively unable to write
// negative tests. This is a workaround specifically for negative tests.
if (!disableMemrefTypeConversion)
memref::populateMemRefNarrowTypeEmulationConversions(typeConverter);
ConversionTarget target(*ctx);
target.addDynamicallyLegalOp<func::FuncOp>([&typeConverter](Operation *op) {
return typeConverter.isLegal(cast<func::FuncOp>(op).getFunctionType());
});
auto opLegalCallback = [&typeConverter](Operation *op) {
return typeConverter.isLegal(op);
};
target.addDynamicallyLegalOp<func::CallOp, func::ReturnOp>(opLegalCallback);
target.addDynamicallyLegalDialect<
arith::ArithDialect, vector::VectorDialect, memref::MemRefDialect,
affine::AffineDialect>(opLegalCallback);
RewritePatternSet patterns(ctx);
arith::populateArithNarrowTypeEmulationPatterns(typeConverter, patterns);
memref::populateMemRefNarrowTypeEmulationPatterns(typeConverter, patterns);
vector::populateVectorNarrowTypeEmulationPatterns(typeConverter, patterns,
disableAtomicRMW);
if (failed(applyPartialConversion(op, target, std::move(patterns))))
signalPassFailure();
}
Option<unsigned> loadStoreEmulateBitwidth{
*this, "memref-load-bitwidth",
llvm::cl::desc("memref load/store emulation bit width"),
llvm::cl::init(8)};
Option<unsigned> arithComputeBitwidth{
*this, "arith-compute-bitwidth",
llvm::cl::desc("arith computation bit width"), llvm::cl::init(4)};
Option<bool> disableMemrefTypeConversion{
*this, "skip-memref-type-conversion",
llvm::cl::desc("disable memref type conversion (to test failures)"),
llvm::cl::init(false)};
Option<bool> disableAtomicRMW{
*this, "disable-atomic-rmw",
llvm::cl::desc("disable atomic read-modify-write and prefer generating "
"normal sequence"),
llvm::cl::init(false)};
};
struct TestMemRefFlattenAndVectorNarrowTypeEmulationPass
: public PassWrapper<TestMemRefFlattenAndVectorNarrowTypeEmulationPass,
OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(
TestMemRefFlattenAndVectorNarrowTypeEmulationPass)
TestMemRefFlattenAndVectorNarrowTypeEmulationPass() = default;
TestMemRefFlattenAndVectorNarrowTypeEmulationPass(
const TestMemRefFlattenAndVectorNarrowTypeEmulationPass &pass)
: PassWrapper(pass) {}
void getDependentDialects(DialectRegistry ®istry) const override {
registry
.insert<arith::ArithDialect, func::FuncDialect, memref::MemRefDialect,
vector::VectorDialect, affine::AffineDialect>();
}
StringRef getArgument() const final {
return "test-memref-flatten-and-vector-narrow-type-emulation";
}
StringRef getDescription() const final {
return "Test MemRef flattening and vector narrow type emulation patterns";
}
void runOnOperation() override {
Operation *op = getOperation();
MLIRContext *ctx = &getContext();
// Create a type converter for narrow type emulation (8-bit)
arith::NarrowTypeEmulationConverter typeConverter(8);
// Add conversions for memref types with i4 elements
memref::populateMemRefNarrowTypeEmulationConversions(typeConverter);
ConversionTarget target(*ctx);
target.addDynamicallyLegalOp<func::FuncOp>([&typeConverter](Operation *op) {
return typeConverter.isLegal(cast<func::FuncOp>(op).getFunctionType());
});
auto opLegalCallback = [&typeConverter](Operation *op) {
return typeConverter.isLegal(op);
};
target.addDynamicallyLegalOp<func::CallOp, func::ReturnOp>(opLegalCallback);
target.addDynamicallyLegalDialect<
arith::ArithDialect, vector::VectorDialect, memref::MemRefDialect,
affine::AffineDialect>(opLegalCallback);
RewritePatternSet patterns(ctx);
// This is necessary for the purpose of emulating `memref.alloc` and
// function boundaries.
memref::populateMemRefNarrowTypeEmulationPatterns(typeConverter, patterns);
vector::populateMemRefFlattenAndVectorNarrowTypeEmulationPatterns(
typeConverter, patterns);
// Apply partial conversion
if (failed(applyPartialConversion(op, target, std::move(patterns))))
signalPassFailure();
}
};
} // namespace
namespace mlir::test {
void registerTestEmulateNarrowTypePass() {
PassRegistration<TestEmulateNarrowTypePass>();
PassRegistration<TestMemRefFlattenAndVectorNarrowTypeEmulationPass>();
}
} // namespace mlir::test
|