Skip to content

Commit 5df9f19

Browse files
authored
[fir] Sync DoLoopHelper after upstreaming
1 parent c4acf26 commit 5df9f19

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed

flang/include/flang/Optimizer/Builder/DoLoopHelper.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ class DoLoopHelper {
2929

3030
/// Build loop [\p lb, \p ub] with step \p step.
3131
/// If \p step is an empty value, 1 is used for the step.
32-
void createLoop(mlir::Value lb, mlir::Value ub, mlir::Value step,
33-
const BodyGenerator &bodyGenerator);
32+
fir::DoLoopOp createLoop(mlir::Value lb, mlir::Value ub, mlir::Value step,
33+
const BodyGenerator &bodyGenerator);
3434

3535
/// Build loop [\p lb, \p ub] with step 1.
36-
void createLoop(mlir::Value lb, mlir::Value ub,
37-
const BodyGenerator &bodyGenerator);
36+
fir::DoLoopOp createLoop(mlir::Value lb, mlir::Value ub,
37+
const BodyGenerator &bodyGenerator);
3838

3939
/// Build loop [0, \p count) with step 1.
40-
void createLoop(mlir::Value count, const BodyGenerator &bodyGenerator);
40+
fir::DoLoopOp createLoop(mlir::Value count,
41+
const BodyGenerator &bodyGenerator);
4142

4243
private:
4344
fir::FirOpBuilder &builder;

flang/lib/Optimizer/Builder/DoLoopHelper.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// DoLoopHelper implementation
1313
//===----------------------------------------------------------------------===//
1414

15-
void fir::factory::DoLoopHelper::createLoop(
15+
fir::DoLoopOp fir::factory::DoLoopHelper::createLoop(
1616
mlir::Value lb, mlir::Value ub, mlir::Value step,
1717
const BodyGenerator &bodyGenerator) {
1818
auto lbi = builder.convertToIndexType(loc, lb);
@@ -25,20 +25,21 @@ void fir::factory::DoLoopHelper::createLoop(
2525
auto index = loop.getInductionVar();
2626
bodyGenerator(builder, index);
2727
builder.restoreInsertionPoint(insertPt);
28+
return loop;
2829
}
2930

30-
void fir::factory::DoLoopHelper::createLoop(
31+
fir::DoLoopOp fir::factory::DoLoopHelper::createLoop(
3132
mlir::Value lb, mlir::Value ub, const BodyGenerator &bodyGenerator) {
32-
createLoop(lb, ub,
33+
return createLoop(lb, ub,
3334
builder.createIntegerConstant(loc, builder.getIndexType(), 1),
3435
bodyGenerator);
3536
}
3637

37-
void fir::factory::DoLoopHelper::createLoop(
38+
fir::DoLoopOp fir::factory::DoLoopHelper::createLoop(
3839
mlir::Value count, const BodyGenerator &bodyGenerator) {
3940
auto indexType = builder.getIndexType();
4041
auto zero = builder.createIntegerConstant(loc, indexType, 0);
4142
auto one = builder.createIntegerConstant(loc, count.getType(), 1);
4243
auto up = builder.create<mlir::SubIOp>(loc, count, one);
43-
createLoop(zero, up, one, bodyGenerator);
44+
return createLoop(zero, up, one, bodyGenerator);
4445
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===- DoLoopHelperTest.cpp -- DoLoopHelper unit tests --------------------===//
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 "flang/Optimizer/Builder/DoLoopHelper.h"
10+
#include "gtest/gtest.h"
11+
#include "flang/Optimizer/Support/InitFIR.h"
12+
#include "flang/Optimizer/Support/KindMapping.h"
13+
#include <string>
14+
15+
struct DoLoopHelperTest : public testing::Test {
16+
public:
17+
void SetUp() {
18+
fir::KindMapping kindMap(&context);
19+
mlir::OpBuilder builder(&context);
20+
firBuilder = new fir::FirOpBuilder(builder, kindMap);
21+
fir::support::loadDialects(context);
22+
}
23+
void TearDown() { delete firBuilder; }
24+
25+
fir::FirOpBuilder &getBuilder() { return *firBuilder; }
26+
27+
mlir::MLIRContext context;
28+
fir::FirOpBuilder *firBuilder;
29+
};
30+
31+
void checkConstantValue(const mlir::Value &value, int64_t v) {
32+
EXPECT_TRUE(mlir::isa<ConstantOp>(value.getDefiningOp()));
33+
auto cstOp = dyn_cast<ConstantOp>(value.getDefiningOp());
34+
auto valueAttr = cstOp.getValue().dyn_cast_or_null<IntegerAttr>();
35+
EXPECT_EQ(v, valueAttr.getInt());
36+
}
37+
38+
TEST_F(DoLoopHelperTest, createLoopWithCountTest) {
39+
auto firBuilder = getBuilder();
40+
fir::factory::DoLoopHelper helper(firBuilder, firBuilder.getUnknownLoc());
41+
42+
auto c10 = firBuilder.createIntegerConstant(
43+
firBuilder.getUnknownLoc(), firBuilder.getIndexType(), 10);
44+
auto loop =
45+
helper.createLoop(c10, [&](fir::FirOpBuilder &, mlir::Value index) {});
46+
checkConstantValue(loop.lowerBound(), 0);
47+
EXPECT_TRUE(mlir::isa<mlir::SubIOp>(loop.upperBound().getDefiningOp()));
48+
auto subOp = dyn_cast<mlir::SubIOp>(loop.upperBound().getDefiningOp());
49+
EXPECT_EQ(c10, subOp.lhs());
50+
checkConstantValue(subOp.rhs(), 1);
51+
checkConstantValue(loop.step(), 1);
52+
}
53+
54+
TEST_F(DoLoopHelperTest, createLoopWithLowerAndUpperBound) {
55+
auto firBuilder = getBuilder();
56+
fir::factory::DoLoopHelper helper(firBuilder, firBuilder.getUnknownLoc());
57+
58+
auto lb = firBuilder.createIntegerConstant(
59+
firBuilder.getUnknownLoc(), firBuilder.getIndexType(), 1);
60+
auto ub = firBuilder.createIntegerConstant(
61+
firBuilder.getUnknownLoc(), firBuilder.getIndexType(), 20);
62+
auto loop =
63+
helper.createLoop(lb, ub, [&](fir::FirOpBuilder &, mlir::Value index) {});
64+
checkConstantValue(loop.lowerBound(), 1);
65+
checkConstantValue(loop.upperBound(), 20);
66+
checkConstantValue(loop.step(), 1);
67+
}
68+
69+
TEST_F(DoLoopHelperTest, createLoopWithStep) {
70+
auto firBuilder = getBuilder();
71+
fir::factory::DoLoopHelper helper(firBuilder, firBuilder.getUnknownLoc());
72+
73+
auto lb = firBuilder.createIntegerConstant(
74+
firBuilder.getUnknownLoc(), firBuilder.getIndexType(), 1);
75+
auto ub = firBuilder.createIntegerConstant(
76+
firBuilder.getUnknownLoc(), firBuilder.getIndexType(), 20);
77+
auto step = firBuilder.createIntegerConstant(
78+
firBuilder.getUnknownLoc(), firBuilder.getIndexType(), 2);
79+
auto loop = helper.createLoop(
80+
lb, ub, step, [&](fir::FirOpBuilder &, mlir::Value index) {});
81+
checkConstantValue(loop.lowerBound(), 1);
82+
checkConstantValue(loop.upperBound(), 20);
83+
checkConstantValue(loop.step(), 2);
84+
}

flang/unittests/Optimizer/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
22

33
set(LIBS
4+
FIRBuilder
45
FIRCodeGen
56
FIRDialect
67
FIRSupport
78
${dialect_libs}
89
)
910

1011
add_flang_unittest(FlangOptimizerTests
12+
Builder/DoLoopHelperTest.cpp
1113
FIRContextTest.cpp
1214
InternalNamesTest.cpp
1315
KindMappingTest.cpp

0 commit comments

Comments
 (0)