|
10 | 10 |
|
11 | 11 | #include "mlir/Dialect/Utils/Utils.h" |
12 | 12 |
|
| 13 | +#include <cstdint> |
13 | 14 | #include <gtest/gtest.h> |
| 15 | +#include <limits> |
| 16 | +#include <llvm/ADT/APInt.h> |
| 17 | +#include <llvm/ADT/SmallVector.h> |
| 18 | +#include <memory> |
14 | 19 | #include <mlir/Dialect/Arith/IR/Arith.h> |
15 | 20 | #include <mlir/Dialect/Func/IR/FuncOps.h> |
16 | | -#include <mlir/IR/BuiltinOps.h> |
| 21 | +#include <mlir/IR/Builders.h> |
17 | 22 | #include <mlir/IR/MLIRContext.h> |
18 | | -#include <mlir/Parser/Parser.h> |
| 23 | +#include <mlir/IR/Operation.h> |
| 24 | +#include <mlir/IR/Value.h> |
19 | 25 |
|
20 | 26 | using namespace mlir; |
21 | 27 |
|
22 | 28 | class UtilsTest : public ::testing::Test { |
23 | 29 | protected: |
24 | 30 | MLIRContext context; |
| 31 | + std::unique_ptr<OpBuilder> builder; |
25 | 32 |
|
26 | 33 | void SetUp() override { |
27 | 34 | context.loadDialect<func::FuncDialect>(); |
28 | 35 | context.loadDialect<arith::ArithDialect>(); |
| 36 | + |
| 37 | + builder = std::make_unique<OpBuilder>(&context); |
| 38 | + } |
| 39 | + |
| 40 | + arith::AddFOp createAddition(double a, double b) { |
| 41 | + auto firstOperand = builder->create<arith::ConstantOp>( |
| 42 | + builder->getUnknownLoc(), builder->getF64FloatAttr(a)); |
| 43 | + auto secondOperand = builder->create<arith::ConstantOp>( |
| 44 | + builder->getUnknownLoc(), builder->getF64FloatAttr(b)); |
| 45 | + return builder->create<arith::AddFOp>(builder->getUnknownLoc(), |
| 46 | + firstOperand, secondOperand); |
29 | 47 | } |
30 | 48 | }; |
31 | 49 |
|
32 | 50 | TEST_F(UtilsTest, valueToDouble) { |
33 | | - auto moduleOp = parseSourceString<ModuleOp>( |
34 | | - "func.func @test() { arith.constant 1.234 : f64\n return }", &context); |
35 | | - ASSERT_TRUE(moduleOp); |
36 | | - |
37 | | - for (auto&& funcOp : moduleOp->getOps<func::FuncOp>()) { |
38 | | - for (auto&& constantOp : funcOp.getOps<arith::ConstantOp>()) { |
39 | | - auto value = constantOp.getResult(); |
40 | | - auto stdValue = utils::valueToDouble(value); |
41 | | - ASSERT_TRUE(stdValue.has_value()); |
42 | | - EXPECT_DOUBLE_EQ(stdValue.value(), 1.234); |
43 | | - return; |
44 | | - } |
45 | | - FAIL() << "No arith::ConstantOp found in function!"; |
46 | | - } |
47 | | - FAIL() << "No func::FuncOp found in module!"; |
| 51 | + constexpr double expectedValue = 1.234; |
| 52 | + auto op = builder->create<arith::ConstantOp>( |
| 53 | + builder->getUnknownLoc(), builder->getF64FloatAttr(expectedValue)); |
| 54 | + ASSERT_TRUE(op); |
| 55 | + |
| 56 | + auto value = op.getResult(); |
| 57 | + auto stdValue = utils::valueToDouble(value); |
| 58 | + ASSERT_TRUE(stdValue.has_value()); |
| 59 | + EXPECT_DOUBLE_EQ(stdValue.value(), expectedValue); |
| 60 | +} |
| 61 | + |
| 62 | +TEST_F(UtilsTest, valueToDoubleCastFromIntegerType) { |
| 63 | + constexpr int expectedValue = 42; |
| 64 | + auto op = builder->create<arith::ConstantOp>( |
| 65 | + builder->getUnknownLoc(), builder->getI32IntegerAttr(expectedValue)); |
| 66 | + ASSERT_TRUE(op); |
| 67 | + |
| 68 | + auto value = op.getResult(); |
| 69 | + auto stdValue = utils::valueToDouble(value); |
| 70 | + ASSERT_TRUE(stdValue.has_value()); |
| 71 | + EXPECT_DOUBLE_EQ(stdValue.value(), expectedValue); |
| 72 | +} |
| 73 | + |
| 74 | +TEST_F(UtilsTest, valueToDoubleCastFromMaxUnsignedInteger) { |
| 75 | + constexpr auto expectedValue = std::numeric_limits<uint64_t>::max(); |
| 76 | + constexpr auto bitCount = 64; |
| 77 | + auto op = builder->create<arith::ConstantOp>( |
| 78 | + builder->getUnknownLoc(), |
| 79 | + builder->getIntegerAttr(builder->getIntegerType(bitCount, false), |
| 80 | + llvm::APInt::getMaxValue(bitCount))); |
| 81 | + ASSERT_TRUE(op); |
| 82 | + |
| 83 | + auto value = op.getResult(); |
| 84 | + auto stdValue = utils::valueToDouble(value); |
| 85 | + ASSERT_TRUE(stdValue.has_value()); |
| 86 | + // cast to double will lose precision, but difference to maximum value of |
| 87 | + // int64_t is large enough that the check still makes sense |
| 88 | + EXPECT_DOUBLE_EQ(stdValue.value(), static_cast<double>(expectedValue)); |
48 | 89 | } |
49 | 90 |
|
50 | 91 | TEST_F(UtilsTest, valueToDoubleWrongType) { |
51 | | - auto moduleOp = parseSourceString<ModuleOp>( |
52 | | - "func.func @test() { arith.constant 42 : i32\n return }", &context); |
53 | | - ASSERT_TRUE(moduleOp); |
54 | | - |
55 | | - for (auto&& funcOp : moduleOp->getOps<func::FuncOp>()) { |
56 | | - for (auto&& constantOp : funcOp.getOps<arith::ConstantOp>()) { |
57 | | - auto value = constantOp.getResult(); |
58 | | - auto stdValue = utils::valueToDouble(value); |
59 | | - EXPECT_FALSE(stdValue.has_value()); |
60 | | - return; |
61 | | - } |
62 | | - FAIL() << "No arith::ConstantOp found in function!"; |
63 | | - } |
64 | | - FAIL() << "No func::FuncOp found in module!"; |
| 92 | + auto op = builder->create<arith::ConstantOp>(builder->getUnknownLoc(), |
| 93 | + builder->getStringAttr("test")); |
| 94 | + ASSERT_TRUE(op); |
| 95 | + |
| 96 | + auto value = op.getResult(); |
| 97 | + auto stdValue = utils::valueToDouble(value); |
| 98 | + EXPECT_FALSE(stdValue.has_value()); |
65 | 99 | } |
66 | 100 |
|
67 | 101 | TEST_F(UtilsTest, valueToDoubleNonStaticValue) { |
68 | | - auto moduleOp = parseSourceString<ModuleOp>("func.func @test() {\n" |
69 | | - "%0 = arith.constant 1.1 : f64\n" |
70 | | - "%1 = arith.constant 2.2 : f64\n" |
71 | | - "arith.addf %0, %1 : f64\n" |
72 | | - "return }", |
73 | | - &context); |
74 | | - ASSERT_TRUE(moduleOp); |
75 | | - |
76 | | - for (auto&& funcOp : moduleOp->getOps<func::FuncOp>()) { |
77 | | - for (auto&& addOp : funcOp.getOps<arith::AddFOp>()) { |
78 | | - auto value = addOp.getResult(); |
79 | | - auto stdValue = utils::valueToDouble(value); |
80 | | - EXPECT_FALSE(stdValue.has_value()); |
81 | | - return; |
82 | | - } |
83 | | - FAIL() << "No arith::AddFOp found in function!"; |
84 | | - } |
85 | | - FAIL() << "No func::FuncOp found in module!"; |
| 102 | + auto op = createAddition(9.5, 21.5); |
| 103 | + ASSERT_TRUE(op); |
| 104 | + |
| 105 | + auto value = op.getResult(); |
| 106 | + auto stdValue = utils::valueToDouble(value); |
| 107 | + EXPECT_FALSE(stdValue.has_value()); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(UtilsTest, valueToDoubleNonStaticValueAfterFolding) { |
| 111 | + auto op = createAddition(1.1, 2.2); |
| 112 | + ASSERT_TRUE(op); |
| 113 | + |
| 114 | + llvm::SmallVector<Value> tmp; |
| 115 | + llvm::SmallVector<Operation*> newConstants; |
| 116 | + ASSERT_TRUE(builder->tryFold(op, tmp, &newConstants).succeeded()); |
| 117 | + ASSERT_EQ(newConstants.size(), 1); |
| 118 | + auto value = newConstants[0]->getResult(0); |
| 119 | + auto stdValue = utils::valueToDouble(value); |
| 120 | + ASSERT_TRUE(stdValue.has_value()); |
| 121 | + EXPECT_DOUBLE_EQ(stdValue.value(), 3.3); |
86 | 122 | } |
0 commit comments