Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/MQT/Utils/ConstantFolding.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace mlir::mqt {
/// Convert a floating-point or integer attribute to a double.
[[nodiscard]] std::optional<double> attributeToDouble(Attribute attr);

/// Convert a direct arithmetic constant to a double.
/// Convert a direct constant-like value to a double.
[[nodiscard]] std::optional<double> valueToDouble(Value value);

/**
Expand Down
7 changes: 3 additions & 4 deletions mlir/lib/Dialect/MQT/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "mlir/Dialect/MQT/Utils/ConstantFolding.h"

#include <llvm/ADT/SmallVector.h>
#include <mlir/Dialect/Arith/IR/Arith.h>
#include <mlir/IR/Attributes.h>
#include <mlir/IR/BuiltinAttributes.h>
#include <mlir/IR/Matchers.h>
Expand Down Expand Up @@ -41,11 +40,11 @@ std::optional<double> attributeToDouble(Attribute attr) {
}

std::optional<double> valueToDouble(Value value) {
auto constantOp = value.getDefiningOp<arith::ConstantOp>();
if (!constantOp) {
Attribute attr;
if (!matchPattern(value, m_Constant(&attr))) {
return std::nullopt;
}
return attributeToDouble(constantOp.getValue());
return attributeToDouble(attr);
}

std::optional<Attribute>
Expand Down
5 changes: 3 additions & 2 deletions mlir/unittests/Dialect/MQT/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

set(mqt_utils_target mqt-core-mlir-unittests-mqt-utils)
add_executable(${mqt_utils_target} test_constant_folding.cpp test_gate_powering.cpp)
target_link_libraries(${mqt_utils_target} PRIVATE GTest::gtest_main MLIRArithDialect
MLIRFuncDialect MLIRIR MLIRMQTUtils)
target_link_libraries(
${mqt_utils_target} PRIVATE GTest::gtest_main MLIRArithDialect MLIRFuncDialect MLIRIndexDialect
MLIRIR MLIRMQTUtils)
mqt_mlir_configure_unittest_target(${mqt_utils_target})

gtest_discover_tests(${mqt_utils_target} PROPERTIES LABELS mqt-mlir-unittests DISCOVERY_TIMEOUT 60)
12 changes: 11 additions & 1 deletion mlir/unittests/Dialect/MQT/Utils/test_constant_folding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <gtest/gtest.h>
#include <mlir/Dialect/Arith/IR/Arith.h>
#include <mlir/Dialect/Func/IR/FuncOps.h>
#include <mlir/Dialect/Index/IR/IndexDialect.h>
#include <mlir/Dialect/Index/IR/IndexOps.h>
#include <mlir/IR/Builders.h>
#include <mlir/IR/BuiltinOps.h>
#include <mlir/IR/BuiltinTypes.h>
Expand All @@ -39,7 +41,8 @@ class ConstantFoldingTest : public ::testing::Test {
std::unique_ptr<ImplicitLocOpBuilder> builder;

void SetUp() override {
context.loadDialect<arith::ArithDialect, func::FuncDialect>();
context.loadDialect<arith::ArithDialect, func::FuncDialect,
index::IndexDialect>();

auto loc = FileLineColLoc::get(&context, "<utils-test-builder>", 1, 1);
module = ModuleOp::create(loc);
Expand All @@ -66,6 +69,13 @@ TEST_F(ConstantFoldingTest, valueToDoubleCastFromInteger) {
EXPECT_DOUBLE_EQ(*stdValue, 42.0);
}

TEST_F(ConstantFoldingTest, valueToDoubleConstantLike) {
auto op = index::ConstantOp::create(*builder, 42);
const auto stdValue = mlir::mqt::valueToDouble(op.getResult());
ASSERT_TRUE(stdValue.has_value());
EXPECT_DOUBLE_EQ(*stdValue, 42.0);
}

TEST_F(ConstantFoldingTest, valueToDoubleCastFromNegativeInteger) {
auto op =
arith::ConstantOp::create(*builder, builder->getSI32IntegerAttr(-123));
Expand Down
Loading