forked from microsoft/triton-shared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructuredToMemrefPass.cpp
183 lines (153 loc) · 6.74 KB
/
StructuredToMemrefPass.cpp
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
//===----------------------------------------------------------------------===//
//
// Copyright (c) Microsoft Corporation, Meta Platforms.
// Licensed under the MIT license.
//
//===----------------------------------------------------------------------===//
#include "triton/Dialect/Triton/IR/Dialect.h"
#include "triton-shared/Conversion/StructuredToMemref/StructuredToMemref.h"
#include "triton-shared/Dialect/TritonStructured/IR/TritonStructuredDialect.h"
#include "triton-shared/Dialect/TritonTilingExt/IR/TritonTilingExtDialect.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/SCF/Transforms/Patterns.h"
#include "mlir/Pass/PassManager.h"
#include "triton/Dialect/Triton/IR/Types.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Casting.h"
#include <optional>
#define DEBUG_TYPE "structured-to-memref"
using namespace mlir;
using namespace triton;
namespace mlir {
namespace triton {
#define GEN_PASS_DEF_STRUCTUREDTOMEMREF
#include "triton-shared/Conversion/StructuredToMemref/Passes.h.inc"
} // namespace triton
} // namespace mlir
namespace {
class LoopTypeConverter : public TypeConverter {
public:
LoopTypeConverter(MLIRContext *context) {
// The order of type conversion is important: later ones are tried earlier.
addConversion([](Type type) { return type; });
// addConversion([context](triton::PointerType ptrType) {
// SmallVector<int64_t> strides{1};
// auto layout =
// StridedLayoutAttr::get(context, ShapedType::kDynamic, strides);
// auto elemType = ptrType.getPointeeType();
// auto memrefType = MemRefType::get({1}, elemType, layout);
// return memrefType;
// });
// A tensor of pointers can be passed in as scf.for's init-args, in such
// cases, we convert the type to a memref with dynamic offsets and
// strides.
addConversion(
[context](RankedTensorType tensorType) -> std::optional<MemRefType> {
if (auto ptrType = llvm::dyn_cast<triton::PointerType>(
tensorType.getElementType())) {
auto layout = StridedLayoutAttr::get(
context, ShapedType::kDynamic,
SmallVector<int64_t>(tensorType.getRank(),
ShapedType::kDynamic));
Type elemType = ptrType.getPointeeType();
return MemRefType::get(tensorType.getShape(), elemType, layout);
}
return std::nullopt;
});
// Convert the current memref type to a memref type with dynamic offsets and
// strides through another reinterpret_cast with the same offsets.
// Canonicalization will simplify this sequence by removing the inital
// reinterpret_cast.
addTargetMaterialization([&](OpBuilder &builder, MemRefType memrefType,
ValueRange inputs,
Location loc) -> Value {
auto reinterpretCast =
inputs[0].getDefiningOp<memref::ReinterpretCastOp>();
if (!reinterpretCast) {
return builder
.create<UnrealizedConversionCastOp>(loc, memrefType, inputs)
.getResult(0);
}
return builder.create<memref::ReinterpretCastOp>(
loc, memrefType, inputs[0], reinterpretCast.getMixedOffsets()[0],
reinterpretCast.getMixedSizes(), reinterpretCast.getMixedStrides());
});
addSourceMaterialization([&](OpBuilder &builder, Type resultType,
ValueRange inputs,
Location loc) -> Value {
return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
.getResult(0);
});
addArgumentMaterialization([&](OpBuilder &builder, Type resultType,
ValueRange inputs,
Location loc) -> Value {
return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
.getResult(0);
});
}
};
class PtrToUnrankedMemrefConverter : public TypeConverter {
public:
PtrToUnrankedMemrefConverter() {
addConversion([](Type type) { return type; });
addConversion([](triton::PointerType ptrType) {
return UnrankedMemRefType::get(ptrType.getPointeeType(), 0);
});
addTargetMaterialization([&](OpBuilder &builder,
UnrankedMemRefType resultType,
ValueRange inputs,
Location loc) -> Value {
return builder.create<UnrealizedConversionCastOp>(loc, resultType, inputs)
.getResult(0);
});
}
};
class StructuredToMemrefPass
: public triton::impl::StructuredToMemrefBase<StructuredToMemrefPass> {
using StructuredToMemrefBase<StructuredToMemrefPass>::StructuredToMemrefBase;
public:
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<func::FuncDialect, arith::ArithDialect, math::MathDialect,
linalg::LinalgDialect, affine::AffineDialect,
scf::SCFDialect, tensor::TensorDialect,
bufferization::BufferizationDialect, triton::TritonDialect,
ttx::TritonTilingExtDialect, memref::MemRefDialect>();
}
void runOnOperation() override {
auto moduleOp = getOperation();
RewritePatternSet patterns(&getContext());
ConversionTarget target(getContext());
target.addLegalDialect<
func::FuncDialect, arith::ArithDialect, math::MathDialect,
linalg::LinalgDialect, affine::AffineDialect, scf::SCFDialect,
cf::ControlFlowDialect, tensor::TensorDialect,
bufferization::BufferizationDialect, ttx::TritonTilingExtDialect,
memref::MemRefDialect>();
target.addIllegalOp<tts::LoadOp, tts::StoreOp, tts::MakeTensorPtrOp>();
target.addLegalOp<UnrealizedConversionCastOp>();
PtrToUnrankedMemrefConverter typeConverter;
triton::populateStructuredToMemrefConversionPatterns(patterns,
typeConverter);
LoopTypeConverter loopTypeConverter(patterns.getContext());
mlir::scf::populateSCFStructuralTypeConversionsAndLegality(
loopTypeConverter, patterns, target);
if (failed(applyPartialConversion(moduleOp, target, std::move(patterns)))) {
signalPassFailure();
}
}
};
} // namespace
std::unique_ptr<OperationPass<ModuleOp>>
triton::createStructuredToMemrefPass() {
return std::make_unique<StructuredToMemrefPass>();
}