MLIR  19.0.0git
EmulateNarrowType.cpp
Go to the documentation of this file.
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 
11 
16 #include "mlir/IR/BuiltinTypes.h"
17 #include "mlir/IR/TypeUtilities.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 
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([](Type ty) -> std::optional<Type> { return ty; });
39 
40  // Function case.
41  addConversion([this](FunctionType ty) -> std::optional<Type> {
42  SmallVector<Type> inputs;
43  if (failed(convertTypes(ty.getInputs(), inputs)))
44  return std::nullopt;
45 
46  SmallVector<Type> results;
47  if (failed(convertTypes(ty.getResults(), results)))
48  return std::nullopt;
49 
50  return FunctionType::get(ty.getContext(), inputs, results);
51  });
52 }
53 
55  NarrowTypeEmulationConverter &typeConverter, RewritePatternSet &patterns) {
56  // Populate `func.*` conversion patterns.
57  populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns,
58  typeConverter);
59  populateCallOpTypeConversionPattern(patterns, typeConverter);
60  populateReturnOpTypeConversionPattern(patterns, typeConverter);
61 }
void addConversion(FnT &&callback)
Register a conversion function.
LogicalResult convertTypes(TypeRange types, SmallVectorImpl< Type > &results) const
Convert the given set of types, filling 'results' as necessary.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition: Types.cpp:35
Converts narrow integer or float types that are not supported by the target hardware to wider types.
NarrowTypeEmulationConverter(unsigned targetBitwidth)
void populateArithNarrowTypeEmulationPatterns(NarrowTypeEmulationConverter &typeConverter, RewritePatternSet &patterns)
Adds patterns to emulate narrow Arith and Function ops into wide supported types.
Include the generated interface declarations.
void populateCallOpTypeConversionPattern(RewritePatternSet &patterns, TypeConverter &converter)
Add a pattern to the given pattern list to convert the operand and result types of a CallOp with the ...
void populateReturnOpTypeConversionPattern(RewritePatternSet &patterns, TypeConverter &converter)
Add a pattern to the given pattern list to rewrite return ops to use operands that have been legalize...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72