1//===- ArithToEmitC.cpp - Arith to EmitC Patterns ---------------*- 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// This file implements patterns to convert the Arith dialect to the EmitC
10// dialect.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Conversion/ArithToEmitC/ArithToEmitC.h"
15
16#include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h"
17#include "mlir/Dialect/Arith/IR/Arith.h"
18#include "mlir/Dialect/EmitC/IR/EmitC.h"
19#include "mlir/Dialect/EmitC/Transforms/TypeConversions.h"
20#include "mlir/IR/BuiltinAttributes.h"
21#include "mlir/IR/BuiltinTypes.h"
22#include "mlir/Transforms/DialectConversion.h"
23
24using namespace mlir;
25
26namespace {
27/// Implement the interface to convert Arith to EmitC.
28struct ArithToEmitCDialectInterface : public ConvertToEmitCPatternInterface {
29 ArithToEmitCDialectInterface(Dialect *dialect)
30 : ConvertToEmitCPatternInterface(dialect) {}
31
32 /// Hook for derived dialect interface to provide conversion patterns
33 /// and mark dialect legal for the conversion target.
34 void populateConvertToEmitCConversionPatterns(
35 ConversionTarget &target, TypeConverter &typeConverter,
36 RewritePatternSet &patterns, std::optional<bool> lowerToCpp) const final {
37 populateArithToEmitCPatterns(typeConverter, patterns);
38 }
39};
40} // namespace
41
42void mlir::registerConvertArithToEmitCInterface(DialectRegistry &registry) {
43 registry.addExtension(extensionFn: +[](MLIRContext *ctx, arith::ArithDialect *dialect) {
44 dialect->addInterfaces<ArithToEmitCDialectInterface>();
45 });
46}
47
48//===----------------------------------------------------------------------===//
49// Conversion Patterns
50//===----------------------------------------------------------------------===//
51
52namespace {
53class ArithConstantOpConversionPattern
54 : public OpConversionPattern<arith::ConstantOp> {
55public:
56 using Base::Base;
57
58 LogicalResult
59 matchAndRewrite(arith::ConstantOp arithConst,
60 arith::ConstantOp::Adaptor adaptor,
61 ConversionPatternRewriter &rewriter) const override {
62 if (isa<MemRefType>(Val: arithConst.getType()))
63 return rewriter.notifyMatchFailure(arg&: arithConst,
64 msg: "memref constants are not supported");
65 Type newTy = this->getTypeConverter()->convertType(t: arithConst.getType());
66 if (!newTy)
67 return rewriter.notifyMatchFailure(arg&: arithConst, msg: "type conversion failed");
68 rewriter.replaceOpWithNewOp<emitc::ConstantOp>(op: arithConst, args&: newTy,
69 args: adaptor.getValue());
70 return success();
71 }
72};
73
74/// Get the signed or unsigned type corresponding to \p ty.
75Type adaptIntegralTypeSignedness(Type ty, bool needsUnsigned) {
76 if (isa<IntegerType>(Val: ty)) {
77 if (ty.isUnsignedInteger() != needsUnsigned) {
78 auto signedness = needsUnsigned
79 ? IntegerType::SignednessSemantics::Unsigned
80 : IntegerType::SignednessSemantics::Signed;
81 return IntegerType::get(context: ty.getContext(), width: ty.getIntOrFloatBitWidth(),
82 signedness);
83 }
84 } else if (emitc::isPointerWideType(type: ty)) {
85 if (isa<emitc::SizeTType>(Val: ty) != needsUnsigned) {
86 if (needsUnsigned)
87 return emitc::SizeTType::get(ctx: ty.getContext());
88 return emitc::PtrDiffTType::get(ctx: ty.getContext());
89 }
90 }
91 return ty;
92}
93
94/// Insert a cast operation to type \p ty if \p val does not have this type.
95Value adaptValueType(Value val, ConversionPatternRewriter &rewriter, Type ty) {
96 return rewriter.createOrFold<emitc::CastOp>(location: val.getLoc(), args&: ty, args&: val);
97}
98
99class CmpFOpConversion : public OpConversionPattern<arith::CmpFOp> {
100public:
101 using Base::Base;
102
103 LogicalResult
104 matchAndRewrite(arith::CmpFOp op, OpAdaptor adaptor,
105 ConversionPatternRewriter &rewriter) const override {
106
107 if (!isa<FloatType>(Val: adaptor.getRhs().getType())) {
108 return rewriter.notifyMatchFailure(arg: op.getLoc(),
109 msg: "cmpf currently only supported on "
110 "floats, not tensors/vectors thereof");
111 }
112
113 bool unordered = false;
114 emitc::CmpPredicate predicate;
115 switch (op.getPredicate()) {
116 case arith::CmpFPredicate::AlwaysFalse: {
117 auto constant =
118 emitc::ConstantOp::create(builder&: rewriter, location: op.getLoc(), resultType0: rewriter.getI1Type(),
119 value: rewriter.getBoolAttr(/*value=*/false));
120 rewriter.replaceOp(op, newOp: constant);
121 return success();
122 }
123 case arith::CmpFPredicate::OEQ:
124 unordered = false;
125 predicate = emitc::CmpPredicate::eq;
126 break;
127 case arith::CmpFPredicate::OGT:
128 unordered = false;
129 predicate = emitc::CmpPredicate::gt;
130 break;
131 case arith::CmpFPredicate::OGE:
132 unordered = false;
133 predicate = emitc::CmpPredicate::ge;
134 break;
135 case arith::CmpFPredicate::OLT:
136 unordered = false;
137 predicate = emitc::CmpPredicate::lt;
138 break;
139 case arith::CmpFPredicate::OLE:
140 unordered = false;
141 predicate = emitc::CmpPredicate::le;
142 break;
143 case arith::CmpFPredicate::ONE:
144 unordered = false;
145 predicate = emitc::CmpPredicate::ne;
146 break;
147 case arith::CmpFPredicate::ORD: {
148 // ordered, i.e. none of the operands is NaN
149 auto cmp = createCheckIsOrdered(rewriter, loc: op.getLoc(), first: adaptor.getLhs(),
150 second: adaptor.getRhs());
151 rewriter.replaceOp(op, newValues: cmp);
152 return success();
153 }
154 case arith::CmpFPredicate::UEQ:
155 unordered = true;
156 predicate = emitc::CmpPredicate::eq;
157 break;
158 case arith::CmpFPredicate::UGT:
159 unordered = true;
160 predicate = emitc::CmpPredicate::gt;
161 break;
162 case arith::CmpFPredicate::UGE:
163 unordered = true;
164 predicate = emitc::CmpPredicate::ge;
165 break;
166 case arith::CmpFPredicate::ULT:
167 unordered = true;
168 predicate = emitc::CmpPredicate::lt;
169 break;
170 case arith::CmpFPredicate::ULE:
171 unordered = true;
172 predicate = emitc::CmpPredicate::le;
173 break;
174 case arith::CmpFPredicate::UNE:
175 unordered = true;
176 predicate = emitc::CmpPredicate::ne;
177 break;
178 case arith::CmpFPredicate::UNO: {
179 // unordered, i.e. either operand is nan
180 auto cmp = createCheckIsUnordered(rewriter, loc: op.getLoc(), first: adaptor.getLhs(),
181 second: adaptor.getRhs());
182 rewriter.replaceOp(op, newValues: cmp);
183 return success();
184 }
185 case arith::CmpFPredicate::AlwaysTrue: {
186 auto constant =
187 emitc::ConstantOp::create(builder&: rewriter, location: op.getLoc(), resultType0: rewriter.getI1Type(),
188 value: rewriter.getBoolAttr(/*value=*/true));
189 rewriter.replaceOp(op, newOp: constant);
190 return success();
191 }
192 }
193
194 // Compare the values naively
195 auto cmpResult =
196 emitc::CmpOp::create(builder&: rewriter, location: op.getLoc(), resultType0: op.getType(), predicate,
197 lhs: adaptor.getLhs(), rhs: adaptor.getRhs());
198
199 // Adjust the results for unordered/ordered semantics
200 if (unordered) {
201 auto isUnordered = createCheckIsUnordered(
202 rewriter, loc: op.getLoc(), first: adaptor.getLhs(), second: adaptor.getRhs());
203 rewriter.replaceOpWithNewOp<emitc::LogicalOrOp>(op, args: op.getType(),
204 args&: isUnordered, args&: cmpResult);
205 return success();
206 }
207
208 auto isOrdered = createCheckIsOrdered(rewriter, loc: op.getLoc(),
209 first: adaptor.getLhs(), second: adaptor.getRhs());
210 rewriter.replaceOpWithNewOp<emitc::LogicalAndOp>(op, args: op.getType(),
211 args&: isOrdered, args&: cmpResult);
212 return success();
213 }
214
215private:
216 /// Return a value that is true if \p operand is NaN.
217 Value isNaN(ConversionPatternRewriter &rewriter, Location loc,
218 Value operand) const {
219 // A value is NaN exactly when it compares unequal to itself.
220 return emitc::CmpOp::create(builder&: rewriter, location: loc, resultType0: rewriter.getI1Type(),
221 predicate: emitc::CmpPredicate::ne, lhs: operand, rhs: operand);
222 }
223
224 /// Return a value that is true if \p operand is not NaN.
225 Value isNotNaN(ConversionPatternRewriter &rewriter, Location loc,
226 Value operand) const {
227 // A value is not NaN exactly when it compares equal to itself.
228 return emitc::CmpOp::create(builder&: rewriter, location: loc, resultType0: rewriter.getI1Type(),
229 predicate: emitc::CmpPredicate::eq, lhs: operand, rhs: operand);
230 }
231
232 /// Return a value that is true if the operands \p first and \p second are
233 /// unordered (i.e., at least one of them is NaN).
234 Value createCheckIsUnordered(ConversionPatternRewriter &rewriter,
235 Location loc, Value first, Value second) const {
236 auto firstIsNaN = isNaN(rewriter, loc, operand: first);
237 auto secondIsNaN = isNaN(rewriter, loc, operand: second);
238 return emitc::LogicalOrOp::create(builder&: rewriter, location: loc, resultType0: rewriter.getI1Type(),
239 lhs: firstIsNaN, rhs: secondIsNaN);
240 }
241
242 /// Return a value that is true if the operands \p first and \p second are
243 /// both ordered (i.e., none one of them is NaN).
244 Value createCheckIsOrdered(ConversionPatternRewriter &rewriter, Location loc,
245 Value first, Value second) const {
246 auto firstIsNotNaN = isNotNaN(rewriter, loc, operand: first);
247 auto secondIsNotNaN = isNotNaN(rewriter, loc, operand: second);
248 return emitc::LogicalAndOp::create(builder&: rewriter, location: loc, resultType0: rewriter.getI1Type(),
249 lhs: firstIsNotNaN, rhs: secondIsNotNaN);
250 }
251};
252
253class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
254public:
255 using Base::Base;
256
257 bool needsUnsignedCmp(arith::CmpIPredicate pred) const {
258 switch (pred) {
259 case arith::CmpIPredicate::eq:
260 case arith::CmpIPredicate::ne:
261 case arith::CmpIPredicate::slt:
262 case arith::CmpIPredicate::sle:
263 case arith::CmpIPredicate::sgt:
264 case arith::CmpIPredicate::sge:
265 return false;
266 case arith::CmpIPredicate::ult:
267 case arith::CmpIPredicate::ule:
268 case arith::CmpIPredicate::ugt:
269 case arith::CmpIPredicate::uge:
270 return true;
271 }
272 llvm_unreachable("unknown cmpi predicate kind");
273 }
274
275 emitc::CmpPredicate toEmitCPred(arith::CmpIPredicate pred) const {
276 switch (pred) {
277 case arith::CmpIPredicate::eq:
278 return emitc::CmpPredicate::eq;
279 case arith::CmpIPredicate::ne:
280 return emitc::CmpPredicate::ne;
281 case arith::CmpIPredicate::slt:
282 case arith::CmpIPredicate::ult:
283 return emitc::CmpPredicate::lt;
284 case arith::CmpIPredicate::sle:
285 case arith::CmpIPredicate::ule:
286 return emitc::CmpPredicate::le;
287 case arith::CmpIPredicate::sgt:
288 case arith::CmpIPredicate::ugt:
289 return emitc::CmpPredicate::gt;
290 case arith::CmpIPredicate::sge:
291 case arith::CmpIPredicate::uge:
292 return emitc::CmpPredicate::ge;
293 }
294 llvm_unreachable("unknown cmpi predicate kind");
295 }
296
297 LogicalResult
298 matchAndRewrite(arith::CmpIOp op, OpAdaptor adaptor,
299 ConversionPatternRewriter &rewriter) const override {
300
301 Type type = adaptor.getLhs().getType();
302 if (!type || !(isa<IntegerType>(Val: type) || emitc::isPointerWideType(type))) {
303 return rewriter.notifyMatchFailure(
304 arg&: op, msg: "expected integer or size_t/ssize_t/ptrdiff_t type");
305 }
306
307 bool needsUnsigned = needsUnsignedCmp(pred: op.getPredicate());
308 emitc::CmpPredicate pred = toEmitCPred(pred: op.getPredicate());
309
310 Type arithmeticType = adaptIntegralTypeSignedness(ty: type, needsUnsigned);
311 Value lhs = adaptValueType(val: adaptor.getLhs(), rewriter, ty: arithmeticType);
312 Value rhs = adaptValueType(val: adaptor.getRhs(), rewriter, ty: arithmeticType);
313
314 rewriter.replaceOpWithNewOp<emitc::CmpOp>(op, args: op.getType(), args&: pred, args&: lhs, args&: rhs);
315 return success();
316 }
317};
318
319class NegFOpConversion : public OpConversionPattern<arith::NegFOp> {
320public:
321 using Base::Base;
322
323 LogicalResult
324 matchAndRewrite(arith::NegFOp op, OpAdaptor adaptor,
325 ConversionPatternRewriter &rewriter) const override {
326
327 auto adaptedOp = adaptor.getOperand();
328 auto adaptedOpType = adaptedOp.getType();
329
330 if (isa<TensorType>(Val: adaptedOpType) || isa<VectorType>(Val: adaptedOpType)) {
331 return rewriter.notifyMatchFailure(
332 arg: op.getLoc(),
333 msg: "negf currently only supports scalar types, not vectors or tensors");
334 }
335
336 if (!emitc::isSupportedFloatType(type: adaptedOpType)) {
337 return rewriter.notifyMatchFailure(
338 arg: op.getLoc(), msg: "floating-point type is not supported by EmitC");
339 }
340
341 rewriter.replaceOpWithNewOp<emitc::UnaryMinusOp>(op, args&: adaptedOpType,
342 args&: adaptedOp);
343 return success();
344 }
345};
346
347template <typename ArithOp, bool castToUnsigned>
348class CastConversion : public OpConversionPattern<ArithOp> {
349public:
350 using OpConversionPattern<ArithOp>::OpConversionPattern;
351
352 LogicalResult
353 matchAndRewrite(ArithOp op, typename ArithOp::Adaptor adaptor,
354 ConversionPatternRewriter &rewriter) const override {
355
356 Type opReturnType = this->getTypeConverter()->convertType(op.getType());
357 if (!opReturnType || !(isa<IntegerType>(Val: opReturnType) ||
358 emitc::isPointerWideType(type: opReturnType)))
359 return rewriter.notifyMatchFailure(
360 op, "expected integer or size_t/ssize_t/ptrdiff_t result type");
361
362 if (adaptor.getOperands().size() != 1) {
363 return rewriter.notifyMatchFailure(
364 op, "CastConversion only supports unary ops");
365 }
366
367 Type operandType = adaptor.getIn().getType();
368 if (!operandType || !(isa<IntegerType>(Val: operandType) ||
369 emitc::isPointerWideType(type: operandType)))
370 return rewriter.notifyMatchFailure(
371 op, "expected integer or size_t/ssize_t/ptrdiff_t operand type");
372
373 // Signed (sign-extending) casts from i1 are not supported.
374 if (operandType.isInteger(width: 1) && !castToUnsigned)
375 return rewriter.notifyMatchFailure(op,
376 "operation not supported on i1 type");
377
378 // to-i1 conversions: arith semantics want truncation, whereas (bool)(v) is
379 // equivalent to (v != 0). Implementing as (bool)(v & 0x01) gives
380 // truncation.
381 if (opReturnType.isInteger(width: 1)) {
382 Type attrType = (emitc::isPointerWideType(type: operandType))
383 ? rewriter.getIndexType()
384 : operandType;
385 auto constOne = emitc::ConstantOp::create(
386 rewriter, op.getLoc(), operandType, rewriter.getOneAttr(type: attrType));
387 auto oneAndOperand = emitc::BitwiseAndOp::create(
388 rewriter, op.getLoc(), operandType, adaptor.getIn(), constOne);
389 rewriter.replaceOpWithNewOp<emitc::CastOp>(op, opReturnType,
390 oneAndOperand);
391 return success();
392 }
393
394 bool isTruncation =
395 (isa<IntegerType>(Val: operandType) && isa<IntegerType>(Val: opReturnType) &&
396 operandType.getIntOrFloatBitWidth() >
397 opReturnType.getIntOrFloatBitWidth());
398 bool doUnsigned = castToUnsigned || isTruncation;
399
400 // Adapt the signedness of the result (bitwidth-preserving cast)
401 // This is needed e.g., if the return type is signless.
402 Type castDestType = adaptIntegralTypeSignedness(ty: opReturnType, needsUnsigned: doUnsigned);
403
404 // Adapt the signedness of the operand (bitwidth-preserving cast)
405 Type castSrcType = adaptIntegralTypeSignedness(ty: operandType, needsUnsigned: doUnsigned);
406 Value actualOp = adaptValueType(adaptor.getIn(), rewriter, castSrcType);
407
408 // Actual cast (may change bitwidth)
409 auto cast =
410 emitc::CastOp::create(rewriter, op.getLoc(), castDestType, actualOp);
411
412 // Cast to the expected output type
413 auto result = adaptValueType(cast, rewriter, opReturnType);
414
415 rewriter.replaceOp(op, result);
416 return success();
417 }
418};
419
420template <typename ArithOp>
421class UnsignedCastConversion : public CastConversion<ArithOp, true> {
422 using CastConversion<ArithOp, true>::CastConversion;
423};
424
425template <typename ArithOp>
426class SignedCastConversion : public CastConversion<ArithOp, false> {
427 using CastConversion<ArithOp, false>::CastConversion;
428};
429
430template <typename ArithOp, typename EmitCOp>
431class ArithOpConversion final : public OpConversionPattern<ArithOp> {
432public:
433 using OpConversionPattern<ArithOp>::OpConversionPattern;
434
435 LogicalResult
436 matchAndRewrite(ArithOp arithOp, typename ArithOp::Adaptor adaptor,
437 ConversionPatternRewriter &rewriter) const override {
438
439 Type newTy = this->getTypeConverter()->convertType(arithOp.getType());
440 if (!newTy)
441 return rewriter.notifyMatchFailure(arithOp,
442 "converting result type failed");
443 rewriter.template replaceOpWithNewOp<EmitCOp>(arithOp, newTy,
444 adaptor.getOperands());
445
446 return success();
447 }
448};
449
450template <class ArithOp, class EmitCOp>
451class BinaryUIOpConversion final : public OpConversionPattern<ArithOp> {
452public:
453 using OpConversionPattern<ArithOp>::OpConversionPattern;
454
455 LogicalResult
456 matchAndRewrite(ArithOp uiBinOp, typename ArithOp::Adaptor adaptor,
457 ConversionPatternRewriter &rewriter) const override {
458 Type newRetTy = this->getTypeConverter()->convertType(uiBinOp.getType());
459 if (!newRetTy)
460 return rewriter.notifyMatchFailure(uiBinOp,
461 "converting result type failed");
462 if (!isa<IntegerType, emitc::SizeTType>(Val: newRetTy)) {
463 return rewriter.notifyMatchFailure(uiBinOp, "unsupported result type");
464 }
465 Type unsignedType =
466 adaptIntegralTypeSignedness(ty: newRetTy, /*needsUnsigned=*/true);
467 if (!unsignedType)
468 return rewriter.notifyMatchFailure(uiBinOp,
469 "converting result type failed");
470 Value lhsAdapted = adaptValueType(adaptor.getLhs(), rewriter, unsignedType);
471 Value rhsAdapted = adaptValueType(adaptor.getRhs(), rewriter, unsignedType);
472
473 auto newDivOp = EmitCOp::create(rewriter, uiBinOp.getLoc(), unsignedType,
474 ArrayRef<Value>{lhsAdapted, rhsAdapted});
475 Value resultAdapted = adaptValueType(newDivOp, rewriter, newRetTy);
476 rewriter.replaceOp(uiBinOp, resultAdapted);
477 return success();
478 }
479};
480
481template <typename ArithOp, typename EmitCOp>
482class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
483public:
484 using OpConversionPattern<ArithOp>::OpConversionPattern;
485
486 LogicalResult
487 matchAndRewrite(ArithOp op, typename ArithOp::Adaptor adaptor,
488 ConversionPatternRewriter &rewriter) const override {
489
490 Type type = this->getTypeConverter()->convertType(op.getType());
491 if (!type || !(isa<IntegerType>(Val: type) || emitc::isPointerWideType(type))) {
492 return rewriter.notifyMatchFailure(
493 op, "expected integer or size_t/ssize_t/ptrdiff_t type");
494 }
495
496 if (type.isInteger(width: 1)) {
497 // arith expects wrap-around arithmethic, which doesn't happen on `bool`.
498 return rewriter.notifyMatchFailure(op, "i1 type is not implemented");
499 }
500
501 Type arithmeticType = type;
502 if ((type.isSignlessInteger() || type.isSignedInteger()) &&
503 !bitEnumContainsAll(op.getOverflowFlags(),
504 arith::IntegerOverflowFlags::nsw)) {
505 // If the C type is signed and the op doesn't guarantee "No Signed Wrap",
506 // we compute in unsigned integers to avoid UB.
507 arithmeticType = rewriter.getIntegerType(width: type.getIntOrFloatBitWidth(),
508 /*isSigned=*/false);
509 }
510
511 Value lhs = adaptValueType(adaptor.getLhs(), rewriter, arithmeticType);
512 Value rhs = adaptValueType(adaptor.getRhs(), rewriter, arithmeticType);
513
514 Value arithmeticResult =
515 EmitCOp::create(rewriter, op.getLoc(), arithmeticType, lhs, rhs);
516
517 Value result = adaptValueType(val: arithmeticResult, rewriter, ty: type);
518
519 rewriter.replaceOp(op, result);
520 return success();
521 }
522};
523
524template <typename ArithOp, typename EmitCOp>
525class BitwiseOpConversion : public OpConversionPattern<ArithOp> {
526public:
527 using OpConversionPattern<ArithOp>::OpConversionPattern;
528
529 LogicalResult
530 matchAndRewrite(ArithOp op, typename ArithOp::Adaptor adaptor,
531 ConversionPatternRewriter &rewriter) const override {
532
533 Type type = this->getTypeConverter()->convertType(op.getType());
534 if (!isa_and_nonnull<IntegerType>(Val: type)) {
535 return rewriter.notifyMatchFailure(
536 op,
537 "expected integer type, vector/tensor support not yet implemented");
538 }
539
540 // Bitwise ops can be performed directly on booleans
541 if (type.isInteger(width: 1)) {
542 rewriter.replaceOpWithNewOp<EmitCOp>(op, type, adaptor.getLhs(),
543 adaptor.getRhs());
544 return success();
545 }
546
547 // Bitwise ops are defined by the C standard on unsigned operands.
548 Type arithmeticType =
549 adaptIntegralTypeSignedness(ty: type, /*needsUnsigned=*/true);
550
551 Value lhs = adaptValueType(adaptor.getLhs(), rewriter, arithmeticType);
552 Value rhs = adaptValueType(adaptor.getRhs(), rewriter, arithmeticType);
553
554 Value arithmeticResult =
555 EmitCOp::create(rewriter, op.getLoc(), arithmeticType, lhs, rhs);
556
557 Value result = adaptValueType(val: arithmeticResult, rewriter, ty: type);
558
559 rewriter.replaceOp(op, result);
560 return success();
561 }
562};
563
564template <typename ArithOp, typename EmitCOp, bool isUnsignedOp>
565class ShiftOpConversion : public OpConversionPattern<ArithOp> {
566public:
567 using OpConversionPattern<ArithOp>::OpConversionPattern;
568
569 LogicalResult
570 matchAndRewrite(ArithOp op, typename ArithOp::Adaptor adaptor,
571 ConversionPatternRewriter &rewriter) const override {
572
573 Type type = this->getTypeConverter()->convertType(op.getType());
574 if (!type || !(isa<IntegerType>(Val: type) || emitc::isPointerWideType(type))) {
575 return rewriter.notifyMatchFailure(
576 op, "expected integer or size_t/ssize_t/ptrdiff_t type");
577 }
578
579 if (type.isInteger(width: 1)) {
580 return rewriter.notifyMatchFailure(op, "i1 type is not implemented");
581 }
582
583 Type arithmeticType = adaptIntegralTypeSignedness(ty: type, needsUnsigned: isUnsignedOp);
584
585 Value lhs = adaptValueType(adaptor.getLhs(), rewriter, arithmeticType);
586 // Shift amount interpreted as unsigned per Arith dialect spec.
587 Type rhsType = adaptIntegralTypeSignedness(adaptor.getRhs().getType(),
588 /*needsUnsigned=*/true);
589 Value rhs = adaptValueType(adaptor.getRhs(), rewriter, rhsType);
590
591 // Add a runtime check for overflow
592 Value width;
593 if (emitc::isPointerWideType(type)) {
594 Value eight = emitc::ConstantOp::create(rewriter, op.getLoc(), rhsType,
595 rewriter.getIndexAttr(value: 8));
596 emitc::CallOpaqueOp sizeOfCall = emitc::CallOpaqueOp::create(
597 rewriter, op.getLoc(), rhsType, "sizeof", ArrayRef<Value>{eight});
598 width = emitc::MulOp::create(rewriter, op.getLoc(), rhsType, eight,
599 sizeOfCall.getResult(i: 0));
600 } else {
601 width = emitc::ConstantOp::create(
602 rewriter, op.getLoc(), rhsType,
603 rewriter.getIntegerAttr(type: rhsType, value: type.getIntOrFloatBitWidth()));
604 }
605
606 Value excessCheck =
607 emitc::CmpOp::create(rewriter, op.getLoc(), rewriter.getI1Type(),
608 emitc::CmpPredicate::lt, rhs, width);
609
610 // Any concrete value is a valid refinement of poison.
611 Value poison = emitc::ConstantOp::create(
612 rewriter, op.getLoc(), arithmeticType,
613 (isa<IntegerType>(Val: arithmeticType)
614 ? rewriter.getIntegerAttr(type: arithmeticType, value: 0)
615 : rewriter.getIndexAttr(value: 0)));
616
617 emitc::ExpressionOp ternary =
618 emitc::ExpressionOp::create(rewriter, op.getLoc(), arithmeticType,
619 ValueRange({lhs, rhs, excessCheck, poison}),
620 /*do_not_inline=*/false);
621 Block &bodyBlock = ternary.createBody();
622 auto currentPoint = rewriter.getInsertionPoint();
623 rewriter.setInsertionPointToStart(&bodyBlock);
624 Value arithmeticResult =
625 EmitCOp::create(rewriter, op.getLoc(), arithmeticType,
626 bodyBlock.getArgument(i: 0), bodyBlock.getArgument(i: 1));
627 Value resultOrPoison = emitc::ConditionalOp::create(
628 rewriter, op.getLoc(), arithmeticType, bodyBlock.getArgument(i: 2),
629 arithmeticResult, bodyBlock.getArgument(i: 3));
630 emitc::YieldOp::create(rewriter, op.getLoc(), resultOrPoison);
631 rewriter.setInsertionPoint(op->getBlock(), currentPoint);
632
633 Value result = adaptValueType(val: ternary, rewriter, ty: type);
634
635 rewriter.replaceOp(op, result);
636 return success();
637 }
638};
639
640template <typename ArithOp, typename EmitCOp>
641class SignedShiftOpConversion final
642 : public ShiftOpConversion<ArithOp, EmitCOp, false> {
643 using ShiftOpConversion<ArithOp, EmitCOp, false>::ShiftOpConversion;
644};
645
646template <typename ArithOp, typename EmitCOp>
647class UnsignedShiftOpConversion final
648 : public ShiftOpConversion<ArithOp, EmitCOp, true> {
649 using ShiftOpConversion<ArithOp, EmitCOp, true>::ShiftOpConversion;
650};
651
652class SelectOpConversion : public OpConversionPattern<arith::SelectOp> {
653public:
654 using Base::Base;
655
656 LogicalResult
657 matchAndRewrite(arith::SelectOp selectOp, OpAdaptor adaptor,
658 ConversionPatternRewriter &rewriter) const override {
659
660 Type dstType = getTypeConverter()->convertType(t: selectOp.getType());
661 if (!dstType)
662 return rewriter.notifyMatchFailure(arg&: selectOp, msg: "type conversion failed");
663
664 if (!adaptor.getCondition().getType().isInteger(width: 1))
665 return rewriter.notifyMatchFailure(
666 arg&: selectOp,
667 msg: "can only be converted if condition is a scalar of type i1");
668
669 rewriter.replaceOpWithNewOp<emitc::ConditionalOp>(op: selectOp, args&: dstType,
670 args: adaptor.getOperands());
671
672 return success();
673 }
674};
675
676// Floating-point to integer conversions.
677template <typename CastOp>
678class FtoICastOpConversion : public OpConversionPattern<CastOp> {
679public:
680 FtoICastOpConversion(const TypeConverter &typeConverter, MLIRContext *context)
681 : OpConversionPattern<CastOp>(typeConverter, context) {}
682
683 LogicalResult
684 matchAndRewrite(CastOp castOp, typename CastOp::Adaptor adaptor,
685 ConversionPatternRewriter &rewriter) const override {
686
687 Type operandType = adaptor.getIn().getType();
688 if (!emitc::isSupportedFloatType(type: operandType))
689 return rewriter.notifyMatchFailure(castOp,
690 "unsupported cast source type");
691
692 Type dstType = this->getTypeConverter()->convertType(castOp.getType());
693 if (!dstType)
694 return rewriter.notifyMatchFailure(castOp, "type conversion failed");
695
696 // Float-to-i1 casts are not supported: any value with 0 < value < 1 must be
697 // truncated to 0, whereas a boolean conversion would return true.
698 if (!emitc::isSupportedIntegerType(type: dstType) || dstType.isInteger(width: 1))
699 return rewriter.notifyMatchFailure(castOp,
700 "unsupported cast destination type");
701
702 // Convert to unsigned if it's the "ui" variant
703 // Signless is interpreted as signed, so no need to cast for "si"
704 Type actualResultType = dstType;
705 if (isa<arith::FPToUIOp>(castOp)) {
706 actualResultType =
707 rewriter.getIntegerType(width: dstType.getIntOrFloatBitWidth(),
708 /*isSigned=*/false);
709 }
710
711 Value result =
712 emitc::CastOp::create(rewriter, castOp.getLoc(), actualResultType,
713 adaptor.getOperands().front(), /*pure=*/false);
714
715 if (isa<arith::FPToUIOp>(castOp)) {
716 result =
717 emitc::CastOp::create(rewriter, castOp.getLoc(), dstType, result);
718 }
719 rewriter.replaceOp(castOp, result);
720
721 return success();
722 }
723};
724
725// Integer to floating-point conversions.
726template <typename CastOp>
727class ItoFCastOpConversion : public OpConversionPattern<CastOp> {
728public:
729 ItoFCastOpConversion(const TypeConverter &typeConverter, MLIRContext *context)
730 : OpConversionPattern<CastOp>(typeConverter, context) {}
731
732 LogicalResult
733 matchAndRewrite(CastOp castOp, typename CastOp::Adaptor adaptor,
734 ConversionPatternRewriter &rewriter) const override {
735 // Vectors in particular are not supported
736 Type operandType = adaptor.getIn().getType();
737 if (!emitc::isSupportedIntegerType(type: operandType))
738 return rewriter.notifyMatchFailure(castOp,
739 "unsupported cast source type");
740
741 Type dstType = this->getTypeConverter()->convertType(castOp.getType());
742 if (!dstType)
743 return rewriter.notifyMatchFailure(castOp, "type conversion failed");
744
745 if (!emitc::isSupportedFloatType(type: dstType))
746 return rewriter.notifyMatchFailure(castOp,
747 "unsupported cast destination type");
748
749 // Convert to unsigned if it's the "ui" variant
750 // Signless is interpreted as signed, so no need to cast for "si"
751 Type actualOperandType = operandType;
752 if (isa<arith::UIToFPOp>(castOp)) {
753 actualOperandType =
754 rewriter.getIntegerType(width: operandType.getIntOrFloatBitWidth(),
755 /*isSigned=*/false);
756 }
757 Value fpCastOperand = adaptor.getIn();
758 if (actualOperandType != operandType) {
759 fpCastOperand = emitc::CastOp::create(rewriter, castOp.getLoc(),
760 actualOperandType, fpCastOperand);
761 }
762 rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType, fpCastOperand);
763
764 return success();
765 }
766};
767
768// Floating-point to floating-point conversions.
769template <typename CastOp>
770class FpCastOpConversion : public OpConversionPattern<CastOp> {
771public:
772 FpCastOpConversion(const TypeConverter &typeConverter, MLIRContext *context)
773 : OpConversionPattern<CastOp>(typeConverter, context) {}
774
775 LogicalResult
776 matchAndRewrite(CastOp castOp, typename CastOp::Adaptor adaptor,
777 ConversionPatternRewriter &rewriter) const override {
778 // Vectors in particular are not supported.
779 Type operandType = adaptor.getIn().getType();
780 if (!emitc::isSupportedFloatType(type: operandType))
781 return rewriter.notifyMatchFailure(castOp,
782 "unsupported cast source type");
783 if (auto roundingModeOp =
784 dyn_cast<arith::ArithRoundingModeInterface>(*castOp)) {
785 // Only supporting default rounding mode as of now.
786 if (roundingModeOp.getRoundingModeAttr())
787 return rewriter.notifyMatchFailure(castOp, "unsupported rounding mode");
788 }
789
790 Type dstType = this->getTypeConverter()->convertType(castOp.getType());
791 if (!dstType)
792 return rewriter.notifyMatchFailure(castOp, "type conversion failed");
793
794 if (!emitc::isSupportedFloatType(type: dstType))
795 return rewriter.notifyMatchFailure(castOp,
796 "unsupported cast destination type");
797
798 Value fpCastOperand = adaptor.getIn();
799 rewriter.replaceOpWithNewOp<emitc::CastOp>(castOp, dstType, fpCastOperand);
800
801 return success();
802 }
803};
804
805} // namespace
806
807//===----------------------------------------------------------------------===//
808// Pattern population
809//===----------------------------------------------------------------------===//
810
811void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
812 RewritePatternSet &patterns) {
813 MLIRContext *ctx = patterns.getContext();
814
815 mlir::populateEmitCSizeTTypeConversions(converter&: typeConverter);
816
817 // clang-format off
818 patterns.add<
819 ArithConstantOpConversionPattern,
820 ArithOpConversion<arith::AddFOp, emitc::AddOp>,
821 ArithOpConversion<arith::DivFOp, emitc::DivOp>,
822 ArithOpConversion<arith::DivSIOp, emitc::DivOp>,
823 ArithOpConversion<arith::MulFOp, emitc::MulOp>,
824 ArithOpConversion<arith::RemSIOp, emitc::RemOp>,
825 ArithOpConversion<arith::SubFOp, emitc::SubOp>,
826 BinaryUIOpConversion<arith::DivUIOp, emitc::DivOp>,
827 BinaryUIOpConversion<arith::RemUIOp, emitc::RemOp>,
828 IntegerOpConversion<arith::AddIOp, emitc::AddOp>,
829 IntegerOpConversion<arith::MulIOp, emitc::MulOp>,
830 IntegerOpConversion<arith::SubIOp, emitc::SubOp>,
831 BitwiseOpConversion<arith::AndIOp, emitc::BitwiseAndOp>,
832 BitwiseOpConversion<arith::OrIOp, emitc::BitwiseOrOp>,
833 BitwiseOpConversion<arith::XOrIOp, emitc::BitwiseXorOp>,
834 UnsignedShiftOpConversion<arith::ShLIOp, emitc::BitwiseLeftShiftOp>,
835 SignedShiftOpConversion<arith::ShRSIOp, emitc::BitwiseRightShiftOp>,
836 UnsignedShiftOpConversion<arith::ShRUIOp, emitc::BitwiseRightShiftOp>,
837 CmpFOpConversion,
838 CmpIOpConversion,
839 NegFOpConversion,
840 SelectOpConversion,
841 // Truncation is guaranteed for unsigned types.
842 UnsignedCastConversion<arith::TruncIOp>,
843 SignedCastConversion<arith::ExtSIOp>,
844 UnsignedCastConversion<arith::ExtUIOp>,
845 SignedCastConversion<arith::IndexCastOp>,
846 UnsignedCastConversion<arith::IndexCastUIOp>,
847 ItoFCastOpConversion<arith::SIToFPOp>,
848 ItoFCastOpConversion<arith::UIToFPOp>,
849 FtoICastOpConversion<arith::FPToSIOp>,
850 FtoICastOpConversion<arith::FPToUIOp>,
851 FpCastOpConversion<arith::ExtFOp>,
852 FpCastOpConversion<arith::TruncFOp>
853 >(arg&: typeConverter, args&: ctx);
854 // clang-format on
855}
856