| 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/Transforms/NarrowTypeEmulationConverter.h" |
| 13 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 14 | #include "mlir/Dialect/Func/Transforms/FuncConversions.h" |
| 15 | #include "mlir/IR/BuiltinTypes.h" |
| 16 | #include "mlir/IR/TypeUtilities.h" |
| 17 | #include "mlir/Transforms/DialectConversion.h" |
| 18 | #include "llvm/Support/MathExtras.h" |
| 19 | #include <cassert> |
| 20 | |
| 21 | using namespace mlir; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Public Interface Definition |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | arith::NarrowTypeEmulationConverter::NarrowTypeEmulationConverter( |
| 28 | unsigned targetBitwidth) |
| 29 | : loadStoreBitwidth(targetBitwidth) { |
| 30 | assert(llvm::isPowerOf2_32(targetBitwidth) && |
| 31 | "Only power-of-two integers are supported"); |
| 32 | |
| 33 | // Allow unknown types. |
| 34 | addConversion(callback: [](Type ty) -> std::optional<Type> { return ty; }); |
| 35 | |
| 36 | // Function case. |
| 37 | addConversion(callback: [this](FunctionType ty) -> std::optional<Type> { |
| 38 | SmallVector<Type> inputs; |
| 39 | if (failed(Result: convertTypes(types: ty.getInputs(), results&: inputs))) |
| 40 | return nullptr; |
| 41 | |
| 42 | SmallVector<Type> results; |
| 43 | if (failed(Result: convertTypes(types: ty.getResults(), results))) |
| 44 | return nullptr; |
| 45 | |
| 46 | return FunctionType::get(context: ty.getContext(), inputs, results); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | void arith::populateArithNarrowTypeEmulationPatterns( |
| 51 | const NarrowTypeEmulationConverter &typeConverter, |
| 52 | RewritePatternSet &patterns) { |
| 53 | // Populate `func.*` conversion patterns. |
| 54 | populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns, |
| 55 | converter: typeConverter); |
| 56 | populateCallOpTypeConversionPattern(patterns, converter: typeConverter); |
| 57 | populateReturnOpTypeConversionPattern(patterns, converter: typeConverter); |
| 58 | } |
| 59 |
