MLIR 24.0.0git
IndexToLLVM.cpp
Go to the documentation of this file.
1//===- IndexToLLVM.cpp - Index to LLVM dialect conversion -------*- 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
10
21#include "mlir/Pass/Pass.h"
22
23using namespace mlir;
24using namespace index;
25
26namespace {
27
28//===----------------------------------------------------------------------===//
29// ConvertIndexCeilDivS
30//===----------------------------------------------------------------------===//
31
32/// Convert `ceildivs(n, m)` into `x = m > 0 ? -1 : 1` and then
33/// `n*m > 0 ? (n+x)/m + 1 : -(-n/m)`.
34struct ConvertIndexCeilDivS : mlir::ConvertOpToLLVMPattern<CeilDivSOp> {
36
37 LogicalResult
38 matchAndRewrite(CeilDivSOp op, CeilDivSOpAdaptor adaptor,
39 ConversionPatternRewriter &rewriter) const override {
40 Location loc = op.getLoc();
41 Value n = adaptor.getLhs();
42 Value m = adaptor.getRhs();
43 Value zero = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 0);
44 Value posOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 1);
45 Value negOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), -1);
46
47 // Compute `x`.
48 Value mPos =
49 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::sgt, m, zero);
50 Value x = LLVM::SelectOp::create(rewriter, loc, mPos, negOne, posOne);
51
52 // Compute the positive result.
53 Value nPlusX = LLVM::AddOp::create(rewriter, loc, n, x);
54 Value nPlusXDivM = LLVM::SDivOp::create(rewriter, loc, nPlusX, m);
55 Value posRes = LLVM::AddOp::create(rewriter, loc, nPlusXDivM, posOne);
56
57 // Compute the negative result.
58 Value negN = LLVM::SubOp::create(rewriter, loc, zero, n);
59 Value negNDivM = LLVM::SDivOp::create(rewriter, loc, negN, m);
60 Value negRes = LLVM::SubOp::create(rewriter, loc, zero, negNDivM);
61
62 // Pick the positive result if `n` and `m` have the same sign and `n` is
63 // non-zero, i.e. `(n > 0) == (m > 0) && n != 0`.
64 Value nPos =
65 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::sgt, n, zero);
66 Value sameSign = LLVM::ICmpOp::create(rewriter, loc,
67 LLVM::ICmpPredicate::eq, nPos, mPos);
68 Value nNonZero =
69 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::ne, n, zero);
70 Value cmp = LLVM::AndOp::create(rewriter, loc, sameSign, nNonZero);
71 rewriter.replaceOpWithNewOp<LLVM::SelectOp>(op, cmp, posRes, negRes);
72 return success();
73 }
74};
75
76//===----------------------------------------------------------------------===//
77// ConvertIndexCeilDivU
78//===----------------------------------------------------------------------===//
79
80/// Convert `ceildivu(n, m)` into `n == 0 ? 0 : (n-1)/m + 1`.
81struct ConvertIndexCeilDivU : mlir::ConvertOpToLLVMPattern<CeilDivUOp> {
83
84 LogicalResult
85 matchAndRewrite(CeilDivUOp op, CeilDivUOpAdaptor adaptor,
86 ConversionPatternRewriter &rewriter) const override {
87 Location loc = op.getLoc();
88 Value n = adaptor.getLhs();
89 Value m = adaptor.getRhs();
90 Value zero = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 0);
91 Value one = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 1);
92
93 // Compute the non-zero result.
94 Value minusOne = LLVM::SubOp::create(rewriter, loc, n, one);
95 Value quotient = LLVM::UDivOp::create(rewriter, loc, minusOne, m);
96 Value plusOne = LLVM::AddOp::create(rewriter, loc, quotient, one);
97
98 // Pick the result.
99 Value cmp =
100 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::eq, n, zero);
101 rewriter.replaceOpWithNewOp<LLVM::SelectOp>(op, cmp, zero, plusOne);
102 return success();
103 }
104};
105
106//===----------------------------------------------------------------------===//
107// ConvertIndexFloorDivS
108//===----------------------------------------------------------------------===//
109
110/// Convert `floordivs(n, m)` into `x = m < 0 ? 1 : -1` and then
111/// `n*m < 0 ? -1 - (x-n)/m : n/m`.
112struct ConvertIndexFloorDivS : mlir::ConvertOpToLLVMPattern<FloorDivSOp> {
114
115 LogicalResult
116 matchAndRewrite(FloorDivSOp op, FloorDivSOpAdaptor adaptor,
117 ConversionPatternRewriter &rewriter) const override {
118 Location loc = op.getLoc();
119 Value n = adaptor.getLhs();
120 Value m = adaptor.getRhs();
121 Value zero = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 0);
122 Value posOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), 1);
123 Value negOne = LLVM::ConstantOp::create(rewriter, loc, n.getType(), -1);
124
125 // Compute `x`.
126 Value mNeg =
127 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::slt, m, zero);
128 Value x = LLVM::SelectOp::create(rewriter, loc, mNeg, posOne, negOne);
129
130 // Compute the negative result.
131 Value xMinusN = LLVM::SubOp::create(rewriter, loc, x, n);
132 Value xMinusNDivM = LLVM::SDivOp::create(rewriter, loc, xMinusN, m);
133 Value negRes = LLVM::SubOp::create(rewriter, loc, negOne, xMinusNDivM);
134
135 // Compute the positive result.
136 Value posRes = LLVM::SDivOp::create(rewriter, loc, n, m);
137
138 // Pick the negative result if `n` and `m` have different signs and `n` is
139 // non-zero, i.e. `(n < 0) != (m < 0) && n != 0`.
140 Value nNeg =
141 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::slt, n, zero);
142 Value diffSign = LLVM::ICmpOp::create(rewriter, loc,
143 LLVM::ICmpPredicate::ne, nNeg, mNeg);
144 Value nNonZero =
145 LLVM::ICmpOp::create(rewriter, loc, LLVM::ICmpPredicate::ne, n, zero);
146 Value cmp = LLVM::AndOp::create(rewriter, loc, diffSign, nNonZero);
147 rewriter.replaceOpWithNewOp<LLVM::SelectOp>(op, cmp, negRes, posRes);
148 return success();
149 }
150};
151
152//===----------------------------------------------------------------------===//
153// CovnertIndexCast
154//===----------------------------------------------------------------------===//
155
156/// Convert a cast op. If the materialized index type is the same as the other
157/// type, fold away the op. Otherwise, truncate or extend the op as appropriate.
158/// Signed casts sign extend when the result bitwidth is larger. Unsigned casts
159/// zero extend when the result bitwidth is larger.
160template <typename CastOp, typename ExtOp>
161struct ConvertIndexCast : public mlir::ConvertOpToLLVMPattern<CastOp> {
162 using mlir::ConvertOpToLLVMPattern<CastOp>::ConvertOpToLLVMPattern;
163
164 LogicalResult
165 matchAndRewrite(CastOp op, typename CastOp::Adaptor adaptor,
166 ConversionPatternRewriter &rewriter) const override {
167 Type in = adaptor.getInput().getType();
168 Type out = this->getTypeConverter()->convertType(op.getType());
169 if (in == out)
170 rewriter.replaceOp(op, adaptor.getInput());
171 else if (in.getIntOrFloatBitWidth() > out.getIntOrFloatBitWidth())
172 rewriter.replaceOpWithNewOp<LLVM::TruncOp>(op, out, adaptor.getInput());
173 else
174 rewriter.replaceOpWithNewOp<ExtOp>(op, out, adaptor.getInput());
175 return success();
176 }
177};
178
179using ConvertIndexCastS = ConvertIndexCast<CastSOp, LLVM::SExtOp>;
180using ConvertIndexCastU = ConvertIndexCast<CastUOp, LLVM::ZExtOp>;
181
182//===----------------------------------------------------------------------===//
183// ConvertIndexCmp
184//===----------------------------------------------------------------------===//
185
186/// Assert that the LLVM comparison enum lines up with index's enum.
187static constexpr bool checkPredicates(LLVM::ICmpPredicate lhs,
188 IndexCmpPredicate rhs) {
189 return static_cast<int>(lhs) == static_cast<int>(rhs);
190}
191
192static_assert(
193 LLVM::getMaxEnumValForICmpPredicate() ==
194 getMaxEnumValForIndexCmpPredicate() &&
195 checkPredicates(LLVM::ICmpPredicate::eq, IndexCmpPredicate::EQ) &&
196 checkPredicates(LLVM::ICmpPredicate::ne, IndexCmpPredicate::NE) &&
197 checkPredicates(LLVM::ICmpPredicate::sge, IndexCmpPredicate::SGE) &&
198 checkPredicates(LLVM::ICmpPredicate::sgt, IndexCmpPredicate::SGT) &&
199 checkPredicates(LLVM::ICmpPredicate::sle, IndexCmpPredicate::SLE) &&
200 checkPredicates(LLVM::ICmpPredicate::slt, IndexCmpPredicate::SLT) &&
201 checkPredicates(LLVM::ICmpPredicate::uge, IndexCmpPredicate::UGE) &&
202 checkPredicates(LLVM::ICmpPredicate::ugt, IndexCmpPredicate::UGT) &&
203 checkPredicates(LLVM::ICmpPredicate::ule, IndexCmpPredicate::ULE) &&
204 checkPredicates(LLVM::ICmpPredicate::ult, IndexCmpPredicate::ULT),
205 "LLVM ICmpPredicate mismatches IndexCmpPredicate");
206
207struct ConvertIndexCmp : public mlir::ConvertOpToLLVMPattern<CmpOp> {
209
210 LogicalResult
211 matchAndRewrite(CmpOp op, CmpOpAdaptor adaptor,
212 ConversionPatternRewriter &rewriter) const override {
213 // The LLVM enum has the same values as the index predicate enums.
214 rewriter.replaceOpWithNewOp<LLVM::ICmpOp>(
215 op, *LLVM::symbolizeICmpPredicate(static_cast<uint32_t>(op.getPred())),
216 adaptor.getLhs(), adaptor.getRhs());
217 return success();
218 }
219};
220
221//===----------------------------------------------------------------------===//
222// ConvertIndexSizeOf
223//===----------------------------------------------------------------------===//
224
225/// Lower `index.sizeof` to a constant with the value of the index bitwidth.
226struct ConvertIndexSizeOf : public mlir::ConvertOpToLLVMPattern<SizeOfOp> {
228
229 LogicalResult
230 matchAndRewrite(SizeOfOp op, SizeOfOpAdaptor adaptor,
231 ConversionPatternRewriter &rewriter) const override {
232 rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(
233 op, getTypeConverter()->getIndexType(),
234 getTypeConverter()->getIndexTypeBitwidth());
235 return success();
236 }
237};
238
239//===----------------------------------------------------------------------===//
240// ConvertIndexConstant
241//===----------------------------------------------------------------------===//
242
243/// Convert an index constant. Truncate the value as appropriate.
244struct ConvertIndexConstant : public mlir::ConvertOpToLLVMPattern<ConstantOp> {
246
247 LogicalResult
248 matchAndRewrite(ConstantOp op, ConstantOpAdaptor adaptor,
249 ConversionPatternRewriter &rewriter) const override {
250 Type type = getTypeConverter()->getIndexType();
251 APInt value = op.getValue().trunc(type.getIntOrFloatBitWidth());
252 rewriter.replaceOpWithNewOp<LLVM::ConstantOp>(
253 op, type, IntegerAttr::get(type, value));
254 return success();
255 }
256};
257
258//===----------------------------------------------------------------------===//
259// Trivial Conversions
260//===----------------------------------------------------------------------===//
261
265using ConvertIndexDivS =
267using ConvertIndexDivU =
269using ConvertIndexRemS =
271using ConvertIndexRemU =
273using ConvertIndexMaxS =
275using ConvertIndexMaxU =
277using ConvertIndexMinS =
279using ConvertIndexMinU =
282using ConvertIndexShrS =
284using ConvertIndexShrU =
289using ConvertIndexBoolConstant =
291
292} // namespace
293
294//===----------------------------------------------------------------------===//
295// Pattern Population
296//===----------------------------------------------------------------------===//
297
299 const LLVMTypeConverter &typeConverter, RewritePatternSet &patterns) {
300 patterns.insert<
301 // clang-format off
302 ConvertIndexAdd,
303 ConvertIndexSub,
304 ConvertIndexMul,
305 ConvertIndexDivS,
306 ConvertIndexDivU,
307 ConvertIndexRemS,
308 ConvertIndexRemU,
309 ConvertIndexMaxS,
310 ConvertIndexMaxU,
311 ConvertIndexMinS,
312 ConvertIndexMinU,
313 ConvertIndexShl,
314 ConvertIndexShrS,
315 ConvertIndexShrU,
316 ConvertIndexAnd,
317 ConvertIndexOr,
318 ConvertIndexXor,
319 ConvertIndexCeilDivS,
320 ConvertIndexCeilDivU,
321 ConvertIndexFloorDivS,
322 ConvertIndexCastS,
323 ConvertIndexCastU,
324 ConvertIndexCmp,
325 ConvertIndexSizeOf,
326 ConvertIndexConstant,
327 ConvertIndexBoolConstant
328 // clang-format on
329 >(typeConverter);
330}
331
332//===----------------------------------------------------------------------===//
333// ODS-Generated Definitions
334//===----------------------------------------------------------------------===//
335
336namespace mlir {
337#define GEN_PASS_DEF_CONVERTINDEXTOLLVMPASS
338#include "mlir/Conversion/Passes.h.inc"
339} // namespace mlir
340
341//===----------------------------------------------------------------------===//
342// Pass Definition
343//===----------------------------------------------------------------------===//
344
345namespace {
346struct ConvertIndexToLLVMPass
347 : public impl::ConvertIndexToLLVMPassBase<ConvertIndexToLLVMPass> {
348 using Base::Base;
349
350 void runOnOperation() override;
351};
352} // namespace
353
354void ConvertIndexToLLVMPass::runOnOperation() {
355 // Configure dialect conversion.
356 ConversionTarget target(getContext());
357 target.addIllegalDialect<IndexDialect>();
358 target.addLegalDialect<LLVM::LLVMDialect>();
359
360 // Set LLVM lowering options.
361 const auto &dataLayoutAnalysis = getAnalysis<DataLayoutAnalysis>();
362 LowerToLLVMOptions options(&getContext(),
363 dataLayoutAnalysis.getAtOrAbove(getOperation()));
364 if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
365 options.overrideIndexBitwidth(indexBitwidth);
366 LLVMTypeConverter typeConverter(&getContext(), options, &dataLayoutAnalysis);
367
368 // Populate patterns and run the conversion.
369 RewritePatternSet patterns(&getContext());
370 populateIndexToLLVMConversionPatterns(typeConverter, patterns);
371
372 if (failed(
373 applyPartialConversion(getOperation(), target, std::move(patterns))))
374 return signalPassFailure();
375}
376
377//===----------------------------------------------------------------------===//
378// ConvertToLLVMPatternInterface implementation
379//===----------------------------------------------------------------------===//
380
381namespace {
382/// Implement the interface to convert Index to LLVM.
383struct IndexToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
384 IndexToLLVMDialectInterface(Dialect *dialect)
385 : ConvertToLLVMPatternInterface(dialect) {}
386
387 void loadDependentDialects(MLIRContext *context) const final {
388 context->loadDialect<LLVM::LLVMDialect>();
389 }
390
391 /// Hook for derived dialect interface to provide conversion patterns
392 /// and mark dialect legal for the conversion target.
393 void populateConvertToLLVMConversionPatterns(
394 ConversionTarget &target, LLVMTypeConverter &typeConverter,
395 RewritePatternSet &patterns) const final {
396 populateIndexToLLVMConversionPatterns(typeConverter, patterns);
397 }
398};
399} // namespace
400
402 DialectRegistry &registry) {
403 registry.addExtension(+[](MLIRContext *ctx, index::IndexDialect *dialect) {
404 dialect->addInterfaces<IndexToLLVMDialectInterface>();
405 });
406}
return success()
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
Definition Pattern.h:233
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.h:239
Type getIndexType() const
Gets the MLIR type wrapping the LLVM integer type whose bit width is defined by the used type convert...
Definition Pattern.cpp:46
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Conversion from types to the LLVM IR dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:71
Generic implementation of one-to-one conversion from "SourceOp" to "TargetOp" where the latter belong...
Definition Pattern.h:336
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
Type getType() const
Return the type of this value.
Definition Value.h:105
void populateIndexToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns)
void registerConvertIndexToLLVMInterface(DialectRegistry &registry)
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.
static constexpr unsigned kDeriveIndexBitwidthFromDataLayout
Value to pass as bitwidth for the index type when the converter is expected to derive the bitwidth fr...