MLIR 23.0.0git
Utils.cpp
Go to the documentation of this file.
1//===- Utils.cpp - Utils for APFloat Conversion ---------------------------===//
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#include "Utils.h"
10
13#include "mlir/IR/Builders.h"
15#include "mlir/IR/Location.h"
17#include "mlir/IR/Value.h"
18
19using namespace mlir;
20
22 FloatType floatTy) {
23 int32_t sem = llvm::APFloatBase::SemanticsToEnum(floatTy.getFloatSemantics());
24 return arith::ConstantOp::create(b, loc, b.getI32Type(),
25 b.getIntegerAttr(b.getI32Type(), sem));
26}
27
29 mlir::RewriterBase &rewriter, Location loc, Value operand1, Value operand2,
30 Type resultType, llvm::function_ref<Value(Value, Value, Type)> fn) {
31 auto vecTy1 = dyn_cast<VectorType>(operand1.getType());
32 if (operand2) {
33 // Sanity check: Operand types must match.
34 assert(vecTy1 == dyn_cast<VectorType>(operand2.getType()) &&
35 "expected same vector types");
36 }
37 if (!vecTy1) {
38 // Not a vector. Call the function directly.
39 return fn(operand1, operand2, resultType);
40 }
41
42 // Prepare scalar operands.
43 ResultRange sclars1 =
44 vector::ToElementsOp::create(rewriter, loc, operand1)->getResults();
45 SmallVector<Value> scalars2;
46 if (!operand2) {
47 // No second operand. Create a vector of empty values.
48 scalars2.assign(vecTy1.getNumElements(), Value());
49 } else {
50 llvm::append_range(
51 scalars2,
52 vector::ToElementsOp::create(rewriter, loc, operand2)->getResults());
53 }
54
55 // Call the function for each pair of scalar operands.
56 auto resultVecType = cast<VectorType>(resultType);
57 SmallVector<Value> results;
58 for (auto [scalar1, scalar2] : llvm::zip_equal(sclars1, scalars2)) {
59 Value result = fn(scalar1, scalar2, resultVecType.getElementType());
60 results.push_back(result);
61 }
62
63 // Package the results into a vector.
64 return vector::FromElementsOp::create(
65 rewriter, loc,
66 vecTy1.cloneWith(/*shape=*/std::nullopt, results.front().getType()),
67 results);
68}
69
70LogicalResult mlir::checkPreconditions(RewriterBase &rewriter, Operation *op) {
71 for (Value value : llvm::concat<Value>(op->getOperands(), op->getResults())) {
72 Type type = value.getType();
73 if (auto vecTy = dyn_cast<VectorType>(type)) {
74 type = vecTy.getElementType();
75 }
76 if (!type.isIntOrFloat()) {
77 return rewriter.notifyMatchFailure(
78 op, "only integers and floats (or vectors thereof) are supported");
79 }
80 if (type.getIntOrFloatBitWidth() > 64)
81 return rewriter.notifyMatchFailure(op,
82 "bitwidth > 64 bits is not supported");
83 }
84 return success();
85}
return success()
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:378
result_range getResults()
Definition Operation.h:415
This class implements the result iterators for the Operation class.
Definition ValueRange.h:247
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:116
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:122
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
Include the generated interface declarations.
LogicalResult checkPreconditions(RewriterBase &rewriter, Operation *op)
Check preconditions for the conversion:
Definition Utils.cpp:70
Value getAPFloatSemanticsValue(OpBuilder &b, Location loc, FloatType floatTy)
Definition Utils.cpp:21
Value forEachScalarValue(mlir::RewriterBase &rewriter, Location loc, Value operand1, Value operand2, Type resultType, llvm::function_ref< Value(Value, Value, Type)> fn)
Given two operands of vector type and vector result type (with the same shape), call the given functi...
Definition Utils.cpp:28