MLIR  20.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"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/Support/FormatVariadic.h"
21 #include "llvm/Support/MathExtras.h"
22 #include <cassert>
23 
24 using namespace mlir;
25 
26 //===----------------------------------------------------------------------===//
27 // Public Interface Definition
28 //===----------------------------------------------------------------------===//
29 
31  unsigned targetBitwidth)
32  : loadStoreBitwidth(targetBitwidth) {
33  assert(llvm::isPowerOf2_32(targetBitwidth) &&
34  "Only power-of-two integers are supported");
35 
36  // Allow unknown types.
37  addConversion([](Type ty) -> std::optional<Type> { return ty; });
38 
39  // Function case.
40  addConversion([this](FunctionType ty) -> std::optional<Type> {
41  SmallVector<Type> inputs;
42  if (failed(convertTypes(ty.getInputs(), inputs)))
43  return std::nullopt;
44 
45  SmallVector<Type> results;
46  if (failed(convertTypes(ty.getResults(), results)))
47  return std::nullopt;
48 
49  return FunctionType::get(ty.getContext(), inputs, results);
50  });
51 }
52 
54  NarrowTypeEmulationConverter &typeConverter, RewritePatternSet &patterns) {
55  // Populate `func.*` conversion patterns.
56  populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(patterns,
57  typeConverter);
58  populateCallOpTypeConversionPattern(patterns, typeConverter);
59  populateReturnOpTypeConversionPattern(patterns, typeConverter);
60 }
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...