1 | //===- EmulateNarrowType.cpp - Narrow type emulation ----*- C++ |
---|---|
2 | //-*-===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #include "mlir/Dialect/Arith/Transforms/Passes.h" |
11 | |
12 | #include "mlir/Dialect/Arith/IR/Arith.h" |
13 | #include "mlir/Dialect/Arith/Transforms/NarrowTypeEmulationConverter.h" |
14 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
15 | #include "mlir/Dialect/Func/Transforms/FuncConversions.h" |
16 | #include "mlir/IR/BuiltinTypes.h" |
17 | #include "mlir/IR/TypeUtilities.h" |
18 | #include "mlir/Support/LogicalResult.h" |
19 | #include "mlir/Transforms/DialectConversion.h" |
20 | #include "llvm/ADT/APInt.h" |
21 | #include "llvm/Support/FormatVariadic.h" |
22 | #include "llvm/Support/MathExtras.h" |
23 | #include <cassert> |
24 | |
25 | using namespace mlir; |
26 | |
27 | //===----------------------------------------------------------------------===// |
28 | // Public Interface Definition |
29 | //===----------------------------------------------------------------------===// |
30 | |
31 | arith::NarrowTypeEmulationConverter::NarrowTypeEmulationConverter( |
32 | unsigned targetBitwidth) |
33 | : loadStoreBitwidth(targetBitwidth) { |
34 | assert(llvm::isPowerOf2_32(targetBitwidth) && |
35 | "Only power-of-two integers are supported"); |
36 | |
37 | // Allow unknown types. |
38 | addConversion(callback: [](Type ty) -> std::optional<Type> { return ty; }); |
39 | |
40 | // Function case. |
41 | addConversion(callback: [this](FunctionType ty) -> std::optional<Type> { |
42 | SmallVector<Type> inputs; |
43 | if (failed(convertTypes(types: ty.getInputs(), results&: inputs))) |
44 | return std::nullopt; |
45 | |
46 | SmallVector<Type> results; |
47 | if (failed(convertTypes(types: ty.getResults(), results))) |
48 | return std::nullopt; |
49 | |
50 | return FunctionType::get(ty.getContext(), inputs, results); |
51 | }); |
52 | } |
53 | |
54 | void arith::populateArithNarrowTypeEmulationPatterns( |
55 | NarrowTypeEmulationConverter &typeConverter, RewritePatternSet &patterns) { |
56 | // Populate `func.*` conversion patterns. |
57 | populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns, |
58 | typeConverter); |
59 | populateCallOpTypeConversionPattern(patterns, converter&: typeConverter); |
60 | populateReturnOpTypeConversionPattern(patterns, converter&: typeConverter); |
61 | } |
62 |