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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
//===- IndexToLLVM.cpp - Index to LLVM dialect conversion -------*- 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/Conversion/IndexToLLVM/IndexToLLVM.h"
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Dialect/Index/IR/IndexAttrs.h"
#include "mlir/Dialect/Index/IR/IndexDialect.h"
#include "mlir/Dialect/Index/IR/IndexOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
using namespace index;
namespace {
//===----------------------------------------------------------------------===//
// ConvertIndexCeilDivS
//===----------------------------------------------------------------------===//
/// Convert `ceildivs(n, m)` into `x = m > 0 ? -1 : 1` and then
/// `n*m > 0 ? (n+x)/m + 1 : -(-n/m)`.
struct ConvertIndexCeilDivS : mlir::ConvertOpToLLVMPattern<CeilDivSOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(CeilDivSOp op, CeilDivSOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Value n = adaptor.getLhs();
Value m = adaptor.getRhs();
Value zero = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 0);
Value posOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 1);
Value negOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), -1);
// Compute `x`.
Value mPos =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::sgt, m, zero);
Value x = LLVM::SelectOp::create(rewriter, loc, mPos, negOne, posOne);
// Compute the positive result.
Value nPlusX = LLVM::AddOp::create(rewriter, loc, n, x);
Value nPlusXDivM = LLVM::SDivOp::create(rewriter, loc, nPlusX, m);
Value posRes = LLVM::AddOp::create(rewriter, loc, nPlusXDivM, posOne);
// Compute the negative result.
Value negN = LLVM::SubOp::create(rewriter, loc, zero, n);
Value negNDivM = LLVM::SDivOp::create(rewriter, loc, negN, m);
Value negRes = LLVM::SubOp::create(rewriter, loc, zero, negNDivM);
// Pick the positive result if `n` and `m` have the same sign and `n` is
// non-zero, i.e. `(n > 0) == (m > 0) && n != 0`.
Value nPos =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::sgt, n, zero);
Value sameSign = LLVM::ICmpOp::create(rewriter, loc,
LLVM::ICmpPredicate::eq, nPos, mPos);
Value nNonZero =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::ne, n, zero);
Value cmp = LLVM::AndOp::create(rewriter, loc, sameSign, nNonZero);
rewriter.replaceOpWithNewOp<LLVM::SelectOp>(op, cmp, posRes, negRes);
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexCeilDivU
//===----------------------------------------------------------------------===//
/// Convert `ceildivu(n, m)` into `n == 0 ? 0 : (n-1)/m + 1`.
struct ConvertIndexCeilDivU : mlir::ConvertOpToLLVMPattern<CeilDivUOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(CeilDivUOp op, CeilDivUOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Value n = adaptor.getLhs();
Value m = adaptor.getRhs();
Value zero = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 0);
Value one = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 1);
// Compute the non-zero result.
Value minusOne = LLVM::SubOp::create(rewriter, loc, n, one);
Value quotient = LLVM::UDivOp::create(rewriter, loc, minusOne, m);
Value plusOne = LLVM::AddOp::create(rewriter, loc, quotient, one);
// Pick the result.
Value cmp =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::eq, n, zero);
rewriter.replaceOpWithNewOp<LLVM::SelectOp>(op, cmp, zero, plusOne);
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexFloorDivS
//===----------------------------------------------------------------------===//
/// Convert `floordivs(n, m)` into `x = m < 0 ? 1 : -1` and then
/// `n*m < 0 ? -1 - (x-n)/m : n/m`.
struct ConvertIndexFloorDivS : mlir::ConvertOpToLLVMPattern<FloorDivSOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(FloorDivSOp op, FloorDivSOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Location loc = op.getLoc();
Value n = adaptor.getLhs();
Value m = adaptor.getRhs();
Value zero = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 0);
Value posOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 1);
Value negOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), -1);
// Compute `x`.
Value mNeg =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::slt, m, zero);
Value x = LLVM::SelectOp::create(rewriter, loc, mNeg, posOne, negOne);
// Compute the negative result.
Value xMinusN = LLVM::SubOp::create(rewriter, loc, x, n);
Value xMinusNDivM = LLVM::SDivOp::create(rewriter, loc, xMinusN, m);
Value negRes = LLVM::SubOp::create(rewriter, loc, negOne, xMinusNDivM);
// Compute the positive result.
Value posRes = LLVM::SDivOp::create(rewriter, loc, n, m);
// Pick the negative result if `n` and `m` have different signs and `n` is
// non-zero, i.e. `(n < 0) != (m < 0) && n != 0`.
Value nNeg =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::slt, n, zero);
Value diffSign = LLVM::ICmpOp::create(rewriter, loc,
LLVM::ICmpPredicate::ne, nNeg, mNeg);
Value nNonZero =
LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::ne, n, zero);
Value cmp = LLVM::AndOp::create(rewriter, loc, diffSign, nNonZero);
rewriter.replaceOpWithNewOp<LLVM::SelectOp>(op, cmp, negRes, posRes);
return success();
}
};
//===----------------------------------------------------------------------===//
// CovnertIndexCast
//===----------------------------------------------------------------------===//
/// Convert a cast op. If the materialized index type is the same as the other
/// type, fold away the op. Otherwise, truncate or extend the op as appropriate.
/// Signed casts sign extend when the result bitwidth is larger. Unsigned casts
/// zero extend when the result bitwidth is larger.
template <typename CastOp, typename ExtOp>
struct ConvertIndexCast : public mlir::ConvertOpToLLVMPattern<CastOp> {
using mlir::ConvertOpToLLVMPattern<CastOp>::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(CastOp op, typename CastOp::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Type in = adaptor.getInput().getType();
Type out = this->getTypeConverter()->convertType(op.getType());
if (in == out)
rewriter.replaceOp(op, adaptor.getInput());
else if (in.getIntOrFloatBitWidth() > out.getIntOrFloatBitWidth())
rewriter.replaceOpWithNewOp<LLVM::TruncOp>(op, out, adaptor.getInput());
else
rewriter.replaceOpWithNewOp<ExtOp>(op, out, adaptor.getInput());
return success();
}
};
using ConvertIndexCastS = ConvertIndexCast<CastSOp, LLVM::SExtOp>;
using ConvertIndexCastU = ConvertIndexCast<CastUOp, LLVM::ZExtOp>;
//===----------------------------------------------------------------------===//
// ConvertIndexCmp
//===----------------------------------------------------------------------===//
/// Assert that the LLVM comparison enum lines up with index's enum.
static constexpr bool checkPredicates(LLVM::ICmpPredicate lhs,
IndexCmpPredicate rhs) {
return static_cast<int>(lhs) == static_cast<int>(rhs);
}
static_assert(
LLVM::getMaxEnumValForICmpPredicate() ==
getMaxEnumValForIndexCmpPredicate() &&
checkPredicates(LLVM::ICmpPredicate::eq, IndexCmpPredicate::EQ) &&
checkPredicates(LLVM::ICmpPredicate::ne, IndexCmpPredicate::NE) &&
checkPredicates(LLVM::ICmpPredicate::sge, IndexCmpPredicate::SGE) &&
checkPredicates(LLVM::ICmpPredicate::sgt, IndexCmpPredicate::SGT) &&
checkPredicates(LLVM::ICmpPredicate::sle, IndexCmpPredicate::SLE) &&
checkPredicates(LLVM::ICmpPredicate::slt, IndexCmpPredicate::SLT) &&
checkPredicates(LLVM::ICmpPredicate::uge, IndexCmpPredicate::UGE) &&
checkPredicates(LLVM::ICmpPredicate::ugt, IndexCmpPredicate::UGT) &&
checkPredicates(LLVM::ICmpPredicate::ule, IndexCmpPredicate::ULE) &&
checkPredicates(LLVM::ICmpPredicate::ult, IndexCmpPredicate::ULT),
"LLVM ICmpPredicate mismatches IndexCmpPredicate");
struct ConvertIndexCmp : public mlir::ConvertOpToLLVMPattern<CmpOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(CmpOp op, CmpOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// The LLVM enum has the same values as the index predicate enums.
rewriter.replaceOpWithNewOp<LLVM::ICmpOp>(
op, *LLVM::symbolizeICmpPredicate(static_cast<uint32_t>(op.getPred())),
adaptor.getLhs(), adaptor.getRhs());
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexSizeOf
//===----------------------------------------------------------------------===//
/// Lower `index.sizeof` to a constant with the value of the index bitwidth.
struct ConvertIndexSizeOf : public mlir::ConvertOpToLLVMPattern<SizeOfOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(SizeOfOp op, SizeOfOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(
op, getTypeConverter()->getIndexType(),
getTypeConverter()->getIndexTypeBitwidth());
return success();
}
};
//===----------------------------------------------------------------------===//
// ConvertIndexConstant
//===----------------------------------------------------------------------===//
/// Convert an index constant. Truncate the value as appropriate.
struct ConvertIndexConstant : public mlir::ConvertOpToLLVMPattern<ConstantOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
LogicalResult
matchAndRewrite(ConstantOp op, ConstantOpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Type type = getTypeConverter()->getIndexType();
APInt value = op.getValue().trunc(type.getIntOrFloatBitWidth());
rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(
op, type, IntegerAttr::get(type, value));
return success();
}
};
//===----------------------------------------------------------------------===//
// Trivial Conversions
//===----------------------------------------------------------------------===//
using ConvertIndexAdd = mlir::OneToOneConvertToLLVMPattern<AddOp, LLVM::AddOp>;
using ConvertIndexSub = mlir::OneToOneConvertToLLVMPattern<SubOp, LLVM::SubOp>;
using ConvertIndexMul = mlir::OneToOneConvertToLLVMPattern<MulOp, LLVM::MulOp>;
using ConvertIndexDivS =
mlir::OneToOneConvertToLLVMPattern<DivSOp, LLVM::SDivOp>;
using ConvertIndexDivU =
mlir::OneToOneConvertToLLVMPattern<DivUOp, LLVM::UDivOp>;
using ConvertIndexRemS =
mlir::OneToOneConvertToLLVMPattern<RemSOp, LLVM::SRemOp>;
using ConvertIndexRemU =
mlir::OneToOneConvertToLLVMPattern<RemUOp, LLVM::URemOp>;
using ConvertIndexMaxS =
mlir::OneToOneConvertToLLVMPattern<MaxSOp, LLVM::SMaxOp>;
using ConvertIndexMaxU =
mlir::OneToOneConvertToLLVMPattern<MaxUOp, LLVM::UMaxOp>;
using ConvertIndexMinS =
mlir::OneToOneConvertToLLVMPattern<MinSOp, LLVM::SMinOp>;
using ConvertIndexMinU =
mlir::OneToOneConvertToLLVMPattern<MinUOp, LLVM::UMinOp>;
using ConvertIndexShl = mlir::OneToOneConvertToLLVMPattern<ShlOp, LLVM::ShlOp>;
using ConvertIndexShrS =
mlir::OneToOneConvertToLLVMPattern<ShrSOp, LLVM::AShrOp>;
using ConvertIndexShrU =
mlir::OneToOneConvertToLLVMPattern<ShrUOp, LLVM::LShrOp>;
using ConvertIndexAnd = mlir::OneToOneConvertToLLVMPattern<AndOp, LLVM::AndOp>;
using ConvertIndexOr = mlir::OneToOneConvertToLLVMPattern<OrOp, LLVM::OrOp>;
using ConvertIndexXor = mlir::OneToOneConvertToLLVMPattern<XOrOp, LLVM::XOrOp>;
using ConvertIndexBoolConstant =
mlir::OneToOneConvertToLLVMPattern<BoolConstantOp, LLVM::ConstantOp>;
} // namespace
//===----------------------------------------------------------------------===//
// Pattern Population
//===----------------------------------------------------------------------===//
void index::populateIndexToLLVMConversionPatterns(
const LLVMTypeConverter &typeConverter, RewritePatternSet &patterns) {
patterns.insert<
// clang-format off
ConvertIndexAdd,
ConvertIndexSub,
ConvertIndexMul,
ConvertIndexDivS,
ConvertIndexDivU,
ConvertIndexRemS,
ConvertIndexRemU,
ConvertIndexMaxS,
ConvertIndexMaxU,
ConvertIndexMinS,
ConvertIndexMinU,
ConvertIndexShl,
ConvertIndexShrS,
ConvertIndexShrU,
ConvertIndexAnd,
ConvertIndexOr,
ConvertIndexXor,
ConvertIndexCeilDivS,
ConvertIndexCeilDivU,
ConvertIndexFloorDivS,
ConvertIndexCastS,
ConvertIndexCastU,
ConvertIndexCmp,
ConvertIndexSizeOf,
ConvertIndexConstant,
ConvertIndexBoolConstant
// clang-format on
>(typeConverter);
}
//===----------------------------------------------------------------------===//
// ODS-Generated Definitions
//===----------------------------------------------------------------------===//
namespace mlir {
#define GEN_PASS_DEF_CONVERTINDEXTOLLVMPASS
#include "mlir/Conversion/Passes.h.inc"
} // namespace mlir
//===----------------------------------------------------------------------===//
// Pass Definition
//===----------------------------------------------------------------------===//
namespace {
struct ConvertIndexToLLVMPass
: public impl::ConvertIndexToLLVMPassBase<ConvertIndexToLLVMPass> {
using Base::Base;
void runOnOperation() override;
};
} // namespace
void ConvertIndexToLLVMPass::runOnOperation() {
// Configure dialect conversion.
ConversionTarget target(getContext());
target.addIllegalDialect<IndexDialect>();
target.addLegalDialect<LLVM::LLVMDialect>();
// Set LLVM lowering options.
LowerToLLVMOptions options(&getContext());
if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
options.overrideIndexBitwidth(indexBitwidth);
LLVMTypeConverter typeConverter(&getContext(), options);
// Populate patterns and run the conversion.
RewritePatternSet patterns(&getContext());
populateIndexToLLVMConversionPatterns(typeConverter, patterns);
if (failed(
applyPartialConversion(getOperation(), target, std::move(patterns))))
return signalPassFailure();
}
//===----------------------------------------------------------------------===//
// ConvertToLLVMPatternInterface implementation
//===----------------------------------------------------------------------===//
namespace {
/// Implement the interface to convert Index to LLVM.
struct IndexToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface;
void loadDependentDialects(MLIRContext *context) const final {
context->loadDialect<LLVM::LLVMDialect>();
}
/// Hook for derived dialect interface to provide conversion patterns
/// and mark dialect legal for the conversion target.
void populateConvertToLLVMConversionPatterns(
ConversionTarget &target, LLVMTypeConverter &typeConverter,
RewritePatternSet &patterns) const final {
populateIndexToLLVMConversionPatterns(typeConverter, patterns);
}
};
} // namespace
void mlir::index::registerConvertIndexToLLVMInterface(
DialectRegistry ®istry) {
registry.addExtension(+[](MLIRContext *ctx, index::IndexDialect *dialect) {
dialect->addInterfaces<IndexToLLVMDialectInterface>();
});
}
|