| 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 | |
| 11 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 12 | #include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 13 | #include "mlir/IR/Builders.h" |
| 14 | #include "mlir/IR/BuiltinTypeInterfaces.h" |
| 15 | #include "mlir/IR/Location.h" |
| 16 | #include "mlir/IR/PatternMatch.h" |
| 17 | #include "mlir/IR/Value.h" |
| 18 | |
| 19 | using namespace mlir; |
| 20 | |
| 21 | Value mlir::getAPFloatSemanticsValue(OpBuilder &b, Location loc, |
| 22 | FloatType floatTy) { |
| 23 | int32_t sem = llvm::APFloatBase::SemanticsToEnum(Sem: floatTy.getFloatSemantics()); |
| 24 | return arith::ConstantOp::create(builder&: b, location: loc, result: b.getI32Type(), |
| 25 | value: b.getIntegerAttr(type: b.getI32Type(), value: sem)); |
| 26 | } |
| 27 | |
| 28 | Value mlir::forEachScalarValue( |
| 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>(Val: 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(builder&: rewriter, location: loc, source: operand1)->getResults(); |
| 45 | SmallVector<Value> scalars2; |
| 46 | if (!operand2) { |
| 47 | // No second operand. Create a vector of empty values. |
| 48 | scalars2.assign(NumElts: vecTy1.getNumElements(), Elt: Value()); |
| 49 | } else { |
| 50 | llvm::append_range( |
| 51 | C&: scalars2, |
| 52 | R: vector::ToElementsOp::create(builder&: rewriter, location: loc, source: operand2)->getResults()); |
| 53 | } |
| 54 | |
| 55 | // Call the function for each pair of scalar operands. |
| 56 | auto resultVecType = cast<VectorType>(Val&: resultType); |
| 57 | SmallVector<Value> results; |
| 58 | for (auto [scalar1, scalar2] : llvm::zip_equal(t&: sclars1, u&: scalars2)) { |
| 59 | Value result = fn(scalar1, scalar2, resultVecType.getElementType()); |
| 60 | results.push_back(Elt: result); |
| 61 | } |
| 62 | |
| 63 | // Package the results into a vector. |
| 64 | return vector::FromElementsOp::create( |
| 65 | builder&: rewriter, location: loc, |
| 66 | result: vecTy1.cloneWith(/*shape=*/std::nullopt, elementType: results.front().getType()), |
| 67 | elements: results); |
| 68 | } |
| 69 | |
| 70 | LogicalResult mlir::checkPreconditions(RewriterBase &rewriter, Operation *op) { |
| 71 | for (Value value : llvm::concat<Value>(Ranges: op->getOperands(), Ranges: op->getResults())) { |
| 72 | Type type = value.getType(); |
| 73 | if (auto vecTy = dyn_cast<VectorType>(Val&: type)) { |
| 74 | type = vecTy.getElementType(); |
| 75 | } |
| 76 | if (!type.isIntOrFloat()) { |
| 77 | return rewriter.notifyMatchFailure( |
| 78 | arg&: op, msg: "only integers and floats (or vectors thereof) are supported" ); |
| 79 | } |
| 80 | if (type.getIntOrFloatBitWidth() > 64) |
| 81 | return rewriter.notifyMatchFailure(arg&: op, |
| 82 | msg: "bitwidth > 64 bits is not supported" ); |
| 83 | } |
| 84 | return success(); |
| 85 | } |
| 86 | |