6 #include <gtest/gtest.h>
21 template<
typename JlmOperation,
typename MlirOperation>
22 struct IntegerBinaryOpTest
24 using JlmOpType = JlmOperation;
25 using MlirOpType = MlirOperation;
30 template<
typename JlmOperation,
typename MlirOperation>
32 TestIntegerBinaryOperation()
35 using namespace mlir::rvsdg;
37 const size_t nbits = 64;
38 const uint64_t val1 = 2;
39 const uint64_t val2 = 3;
42 auto graph = &rvsdgModule->Rvsdg();
49 auto binaryOp = JlmOperation(nbits);
53 std::cout <<
"Convert to MLIR" << std::endl;
58 std::cout <<
"Validate MLIR" << std::endl;
59 auto & omegaRegion = omega.getRegion();
60 auto & omegaBlock = omegaRegion.front();
62 for (
auto & op : omegaBlock.getOperations())
64 auto mlirBinaryOp = ::mlir::dyn_cast<MlirOperation>(&op);
68 mlirBinaryOp.getOperand(0).getType().template dyn_cast<::mlir::IntegerType>();
70 mlirBinaryOp.getOperand(1).getType().template dyn_cast<::mlir::IntegerType>();
71 EXPECT_NE(inputBitType1,
nullptr);
72 EXPECT_EQ(inputBitType1.getWidth(), nbits);
73 EXPECT_NE(inputBitType2,
nullptr);
74 EXPECT_EQ(inputBitType2.getWidth(), nbits);
76 mlirBinaryOp.getResult().getType().template dyn_cast<::mlir::IntegerType>();
77 EXPECT_NE(outputBitType,
nullptr);
78 EXPECT_EQ(outputBitType.getWidth(), nbits);
85 std::cout <<
"Converting MLIR to RVSDG" << std::endl;
86 std::unique_ptr<mlir::Block> rootBlock = std::make_unique<mlir::Block>();
87 rootBlock->push_back(omega);
89 auto region = &convertedRvsdgModule->Rvsdg().GetRootRegion();
94 EXPECT_EQ(region->numNodes(), 3);
95 bool foundBinaryOp =
false;
96 for (
auto & node : region->Nodes())
98 auto convertedBinaryOp =
dynamic_cast<const JlmOperation *
>(&node.GetOperation());
99 if (convertedBinaryOp)
101 EXPECT_EQ(convertedBinaryOp->nresults(), 1);
102 EXPECT_EQ(convertedBinaryOp->narguments(), 2);
103 auto inputBitType1 = jlm::util::assertedCast<const jlm::rvsdg::BitType>(
104 convertedBinaryOp->argument(0).get());
105 EXPECT_EQ(inputBitType1->nbits(), nbits);
106 auto inputBitType2 = jlm::util::assertedCast<const jlm::rvsdg::BitType>(
107 convertedBinaryOp->argument(1).get());
108 EXPECT_EQ(inputBitType2->nbits(), nbits);
109 auto outputBitType = jlm::util::assertedCast<const jlm::rvsdg::BitType>(
110 convertedBinaryOp->result(0).get());
111 EXPECT_EQ(outputBitType->nbits(), nbits);
112 foundBinaryOp =
true;
115 EXPECT_TRUE(foundBinaryOp);
121 #define REGISTER_INT_BINARY_OP_TEST(JLM_OP, MLIR_NS, MLIR_OP, TEST_NAME) \
122 TEST(IntegerOperationConversionTests, TEST_NAME) \
124 return TestIntegerBinaryOperation< \
125 jlm::llvm::Integer##JLM_OP##Operation, \
126 ::mlir::MLIR_NS::MLIR_OP>(); \
145 template<
typename JlmOperation>
146 struct IntegerComparisonOpTest
148 using JlmOpType = JlmOperation;
149 ::mlir::arith::CmpIPredicate predicate;
154 template<
typename JlmOperation>
156 TestIntegerComparisonOperation(
const IntegerComparisonOpTest<JlmOperation> & test)
159 using namespace mlir::rvsdg;
161 const size_t nbits = 64;
162 const uint64_t val1 = 2;
163 const uint64_t val2 = 3;
166 auto graph = &rvsdgModule->Rvsdg();
173 auto compOp = JlmOperation(nbits);
177 std::cout <<
"Convert to MLIR" << std::endl;
182 std::cout <<
"Validate MLIR" << std::endl;
183 auto & omegaRegion = omega.getRegion();
184 auto & omegaBlock = omegaRegion.front();
185 bool opFound =
false;
186 for (
auto & op : omegaBlock.getOperations())
188 auto mlirCompOp = ::mlir::dyn_cast<::mlir::arith::CmpIOp>(&op);
191 auto inputBitType1 = mlirCompOp.getOperand(0).getType().dyn_cast<::mlir::IntegerType>();
192 auto inputBitType2 = mlirCompOp.getOperand(1).getType().dyn_cast<::mlir::IntegerType>();
193 EXPECT_NE(inputBitType1,
nullptr);
194 EXPECT_EQ(inputBitType1.getWidth(), nbits);
195 EXPECT_NE(inputBitType2,
nullptr);
196 EXPECT_EQ(inputBitType2.getWidth(), nbits);
199 auto outputType = mlirCompOp.getResult().getType().dyn_cast<::mlir::IntegerType>();
200 EXPECT_NE(outputType,
nullptr);
201 EXPECT_EQ(outputType.getWidth(), 1);
204 EXPECT_EQ(mlirCompOp.getPredicate(), test.predicate);
208 EXPECT_TRUE(opFound);
211 std::cout <<
"Converting MLIR to RVSDG" << std::endl;
212 std::unique_ptr<mlir::Block> rootBlock = std::make_unique<mlir::Block>();
213 rootBlock->push_back(omega);
215 auto region = &convertedRvsdgModule->Rvsdg().GetRootRegion();
220 EXPECT_EQ(region->numNodes(), 3);
221 bool foundCompOp =
false;
222 for (
auto & node : region->Nodes())
224 auto convertedCompOp =
dynamic_cast<const JlmOperation *
>(&node.GetOperation());
227 EXPECT_EQ(convertedCompOp->nresults(), 1);
228 EXPECT_EQ(convertedCompOp->narguments(), 2);
229 auto inputBitType1 = jlm::util::assertedCast<const jlm::rvsdg::BitType>(
230 convertedCompOp->argument(0).get());
231 EXPECT_EQ(inputBitType1->nbits(), nbits);
232 auto inputBitType2 = jlm::util::assertedCast<const jlm::rvsdg::BitType>(
233 convertedCompOp->argument(1).get());
234 EXPECT_EQ(inputBitType2->nbits(), nbits);
238 jlm::util::assertedCast<const jlm::rvsdg::BitType>(convertedCompOp->result(0).get());
239 EXPECT_EQ(outputBitType->nbits(), 1);
244 EXPECT_TRUE(foundCompOp);
250 #define REGISTER_INT_COMP_OP_TEST(JLM_OP, PREDICATE, TEST_NAME) \
251 TEST(IntegerOperationConversionTests, TEST_NAME) \
253 IntegerComparisonOpTest<jlm::llvm::Integer##JLM_OP##Operation> test = { \
254 ::mlir::arith::CmpIPredicate::PREDICATE, \
257 return TestIntegerComparisonOperation(test); \
#define REGISTER_INT_COMP_OP_TEST(JLM_OP, PREDICATE, TEST_NAME)
#define REGISTER_INT_BINARY_OP_TEST(JLM_OP, MLIR_NS, MLIR_OP, TEST_NAME)
static std::unique_ptr< LlvmRvsdgModule > Create(const util::FilePath &sourceFileName, const std::string &targetTriple, const std::string &dataLayout)
::mlir::rvsdg::OmegaNode ConvertModule(const llvm::LlvmRvsdgModule &rvsdgModule)
static std::unique_ptr< llvm::LlvmRvsdgModule > CreateAndConvert(std::unique_ptr<::mlir::Block > &block)
static Output & create(Region ®ion, BitValueRepresentation value)
static SimpleNode & Create(Region ®ion, std::unique_ptr< Operation > operation, const std::vector< rvsdg::Output * > &operands)
Global memory state passed between functions.