Skip to content

Commit 43f8eb1

Browse files
authored
Add test files (#1334)
1 parent d9c5573 commit 43f8eb1

File tree

3 files changed

+361
-0
lines changed

3 files changed

+361
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//===- CharacterTest.cpp -- Character runtime builder 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/Runtime/Character.h"
10+
#include "RuntimeCallTestBase.h"
11+
#include "gtest/gtest.h"
12+
#include "flang/Optimizer/Builder/Character.h"
13+
14+
TEST_F(RuntimeCallTest, genAdjustLTest) {
15+
auto loc = firBuilder->getUnknownLoc();
16+
mlir::Value result = firBuilder->create<fir::UndefOp>(loc, boxTy);
17+
mlir::Value string = firBuilder->create<fir::UndefOp>(loc, boxTy);
18+
fir::runtime::genAdjustL(*firBuilder, loc, result, string);
19+
checkCallOpFromResultBox(result, "_FortranAAdjustl", 2);
20+
}
21+
22+
TEST_F(RuntimeCallTest, genAdjustRTest) {
23+
auto loc = firBuilder->getUnknownLoc();
24+
mlir::Value result = firBuilder->create<fir::UndefOp>(loc, boxTy);
25+
mlir::Value string = firBuilder->create<fir::UndefOp>(loc, boxTy);
26+
fir::runtime::genAdjustR(*firBuilder, loc, result, string);
27+
checkCallOpFromResultBox(result, "_FortranAAdjustr", 2);
28+
}
29+
30+
void checkCharCompare1(
31+
fir::FirOpBuilder &builder, mlir::Type type, llvm::StringRef fctName) {
32+
auto loc = builder.getUnknownLoc();
33+
mlir::Type i32Ty = IntegerType::get(builder.getContext(), 32);
34+
mlir::Value lhsBuff = builder.create<fir::UndefOp>(loc, type);
35+
mlir::Value lhsLen = builder.create<fir::UndefOp>(loc, i32Ty);
36+
mlir::Value rhsBuff = builder.create<fir::UndefOp>(loc, type);
37+
mlir::Value rhsLen = builder.create<fir::UndefOp>(loc, i32Ty);
38+
mlir::Value res = fir::runtime::genCharCompare(builder, loc,
39+
mlir::arith::CmpIPredicate::eq, lhsBuff, lhsLen, rhsBuff, rhsLen);
40+
checkCallOpFromResultBox(lhsBuff, fctName, 4, /*addLocArgs=*/false);
41+
EXPECT_TRUE(mlir::isa<mlir::arith::CmpIOp>(res.getDefiningOp()));
42+
}
43+
44+
void checkCharCompare1AllTypeForKind(
45+
fir::FirOpBuilder &builder, llvm::StringRef fctName, unsigned kind) {
46+
mlir::Type charTy = fir::CharacterType::get(builder.getContext(), kind, 10);
47+
mlir::Type seqCharTy = fir::SequenceType::get(charTy, 10);
48+
mlir::Type refCharTy = fir::ReferenceType::get(charTy);
49+
mlir::Type boxCharTy = fir::BoxCharType::get(builder.getContext(), kind);
50+
mlir::Type boxTy = fir::BoxType::get(charTy);
51+
checkCharCompare1(builder, charTy, fctName);
52+
checkCharCompare1(builder, seqCharTy, fctName);
53+
checkCharCompare1(builder, refCharTy, fctName);
54+
checkCharCompare1(builder, boxCharTy, fctName);
55+
checkCharCompare1(builder, boxTy, fctName);
56+
}
57+
58+
TEST_F(RuntimeCallTest, genCharCompar1Test) {
59+
checkCharCompare1AllTypeForKind(
60+
*firBuilder, "_FortranACharacterCompareScalar1", 1);
61+
checkCharCompare1AllTypeForKind(
62+
*firBuilder, "_FortranACharacterCompareScalar2", 2);
63+
checkCharCompare1AllTypeForKind(
64+
*firBuilder, "_FortranACharacterCompareScalar4", 4);
65+
}
66+
67+
void checkCharCompare2(
68+
fir::FirOpBuilder &builder, llvm::StringRef fctName, unsigned kind) {
69+
auto loc = builder.getUnknownLoc();
70+
fir::factory::CharacterExprHelper charHelper(builder, loc);
71+
mlir::Type i32Ty = IntegerType::get(builder.getContext(), 32);
72+
mlir::Type boxCharTy = fir::BoxCharType::get(builder.getContext(), kind);
73+
mlir::Value lhsBuff = builder.create<fir::UndefOp>(loc, boxCharTy);
74+
mlir::Value lhsLen = builder.create<fir::UndefOp>(loc, i32Ty);
75+
mlir::Value rhsBuff = builder.create<fir::UndefOp>(loc, boxCharTy);
76+
mlir::Value rhsLen = builder.create<fir::UndefOp>(loc, i32Ty);
77+
fir::ExtendedValue lhs = charHelper.toExtendedValue(lhsBuff, lhsLen);
78+
fir::ExtendedValue rhs = charHelper.toExtendedValue(rhsBuff, rhsLen);
79+
mlir::Value res = fir::runtime::genCharCompare(
80+
builder, loc, mlir::arith::CmpIPredicate::eq, lhs, rhs);
81+
EXPECT_TRUE(mlir::isa<mlir::arith::CmpIOp>(res.getDefiningOp()));
82+
auto cmpOp = mlir::dyn_cast<mlir::arith::CmpIOp>(res.getDefiningOp());
83+
checkCallOp(cmpOp.lhs().getDefiningOp(), fctName, 4, /*addLocArgs=*/false);
84+
auto allocas = res.getParentBlock()->getOps<fir::AllocaOp>();
85+
EXPECT_TRUE(llvm::empty(allocas));
86+
}
87+
88+
TEST_F(RuntimeCallTest, genCharCompare2Test) {
89+
checkCharCompare2(*firBuilder, "_FortranACharacterCompareScalar1", 1);
90+
checkCharCompare2(*firBuilder, "_FortranACharacterCompareScalar2", 2);
91+
checkCharCompare2(*firBuilder, "_FortranACharacterCompareScalar4", 4);
92+
}
93+
94+
void checkGenIndex(
95+
fir::FirOpBuilder &builder, llvm::StringRef fctName, unsigned kind) {
96+
auto loc = builder.getUnknownLoc();
97+
mlir::Type i32Ty = IntegerType::get(builder.getContext(), 32);
98+
mlir::Value stringBase = builder.create<fir::UndefOp>(loc, i32Ty);
99+
mlir::Value stringLen = builder.create<fir::UndefOp>(loc, i32Ty);
100+
mlir::Value substringBase = builder.create<fir::UndefOp>(loc, i32Ty);
101+
mlir::Value substringLen = builder.create<fir::UndefOp>(loc, i32Ty);
102+
mlir::Value back = builder.create<fir::UndefOp>(loc, i32Ty);
103+
mlir::Value res = fir::runtime::genIndex(builder, loc, kind, stringBase,
104+
stringLen, substringBase, substringLen, back);
105+
checkCallOp(res.getDefiningOp(), fctName, 5, /*addLocArgs=*/false);
106+
}
107+
108+
TEST_F(RuntimeCallTest, genIndexTest) {
109+
checkGenIndex(*firBuilder, "_FortranAIndex1", 1);
110+
checkGenIndex(*firBuilder, "_FortranAIndex2", 2);
111+
checkGenIndex(*firBuilder, "_FortranAIndex4", 4);
112+
}
113+
114+
TEST_F(RuntimeCallTest, genIndexDescriptorTest) {
115+
auto loc = firBuilder->getUnknownLoc();
116+
mlir::Value resultBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
117+
mlir::Value stringBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
118+
mlir::Value substringBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
119+
mlir::Value backOpt = firBuilder->create<fir::UndefOp>(loc, boxTy);
120+
mlir::Value kind = firBuilder->create<fir::UndefOp>(loc, i32Ty);
121+
fir::runtime::genIndexDescriptor(
122+
*firBuilder, loc, resultBox, stringBox, substringBox, backOpt, kind);
123+
checkCallOpFromResultBox(resultBox, "_FortranAIndex", 5);
124+
}
125+
126+
TEST_F(RuntimeCallTest, genRepeatTest) {
127+
auto loc = firBuilder->getUnknownLoc();
128+
mlir::Value resultBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
129+
mlir::Value stringBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
130+
mlir::Value ncopies = firBuilder->create<fir::UndefOp>(loc, i32Ty);
131+
fir::runtime::genRepeat(*firBuilder, loc, resultBox, stringBox, ncopies);
132+
checkCallOpFromResultBox(resultBox, "_FortranARepeat", 3);
133+
}
134+
135+
TEST_F(RuntimeCallTest, genTrimTest) {
136+
auto loc = firBuilder->getUnknownLoc();
137+
mlir::Value resultBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
138+
mlir::Value stringBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
139+
fir::runtime::genTrim(*firBuilder, loc, resultBox, stringBox);
140+
checkCallOpFromResultBox(resultBox, "_FortranATrim", 2);
141+
}
142+
143+
TEST_F(RuntimeCallTest, genScanDescriptorTest) {
144+
auto loc = firBuilder->getUnknownLoc();
145+
mlir::Value resultBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
146+
mlir::Value stringBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
147+
mlir::Value setBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
148+
mlir::Value backBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
149+
mlir::Value kind = firBuilder->create<fir::UndefOp>(loc, i32Ty);
150+
fir::runtime::genScanDescriptor(
151+
*firBuilder, loc, resultBox, stringBox, setBox, backBox, kind);
152+
checkCallOpFromResultBox(resultBox, "_FortranAScan", 5);
153+
}
154+
155+
void checkGenScan(
156+
fir::FirOpBuilder &builder, llvm::StringRef fctName, unsigned kind) {
157+
auto loc = builder.getUnknownLoc();
158+
mlir::Type charTy = fir::CharacterType::get(builder.getContext(), kind, 10);
159+
mlir::Type boxTy = fir::BoxType::get(charTy);
160+
mlir::Type i32Ty = IntegerType::get(builder.getContext(), 32);
161+
mlir::Value stringBase = builder.create<fir::UndefOp>(loc, boxTy);
162+
mlir::Value stringLen = builder.create<fir::UndefOp>(loc, i32Ty);
163+
mlir::Value setBase = builder.create<fir::UndefOp>(loc, boxTy);
164+
mlir::Value setLen = builder.create<fir::UndefOp>(loc, i32Ty);
165+
mlir::Value back = builder.create<fir::UndefOp>(loc, i32Ty);
166+
mlir::Value res = fir::runtime::genScan(
167+
builder, loc, kind, stringBase, stringLen, setBase, setLen, back);
168+
checkCallOp(res.getDefiningOp(), fctName, 5, /*addLocArgs=*/false);
169+
}
170+
171+
TEST_F(RuntimeCallTest, genScanTest) {
172+
checkGenScan(*firBuilder, "_FortranAScan1", 1);
173+
checkGenScan(*firBuilder, "_FortranAScan2", 2);
174+
checkGenScan(*firBuilder, "_FortranAScan4", 4);
175+
}
176+
177+
TEST_F(RuntimeCallTest, genVerifyDescriptorTest) {
178+
auto loc = firBuilder->getUnknownLoc();
179+
mlir::Value resultBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
180+
mlir::Value stringBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
181+
mlir::Value setBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
182+
mlir::Value backBox = firBuilder->create<fir::UndefOp>(loc, boxTy);
183+
mlir::Value kind = firBuilder->create<fir::UndefOp>(loc, i32Ty);
184+
fir::runtime::genVerifyDescriptor(
185+
*firBuilder, loc, resultBox, stringBox, setBox, backBox, kind);
186+
checkCallOpFromResultBox(resultBox, "_FortranAVerify", 5);
187+
}
188+
189+
void checkGenVerify(
190+
fir::FirOpBuilder &builder, llvm::StringRef fctName, unsigned kind) {
191+
auto loc = builder.getUnknownLoc();
192+
mlir::Type charTy = fir::CharacterType::get(builder.getContext(), kind, 10);
193+
mlir::Type boxTy = fir::BoxType::get(charTy);
194+
mlir::Type i32Ty = IntegerType::get(builder.getContext(), 32);
195+
mlir::Value stringBase = builder.create<fir::UndefOp>(loc, boxTy);
196+
mlir::Value stringLen = builder.create<fir::UndefOp>(loc, i32Ty);
197+
mlir::Value setBase = builder.create<fir::UndefOp>(loc, boxTy);
198+
mlir::Value setLen = builder.create<fir::UndefOp>(loc, i32Ty);
199+
mlir::Value back = builder.create<fir::UndefOp>(loc, i32Ty);
200+
mlir::Value res = fir::runtime::genVerify(
201+
builder, loc, kind, stringBase, stringLen, setBase, setLen, back);
202+
checkCallOp(res.getDefiningOp(), fctName, 5, /*addLocArgs=*/false);
203+
}
204+
205+
TEST_F(RuntimeCallTest, genVerifyTest) {
206+
checkGenVerify(*firBuilder, "_FortranAVerify1", 1);
207+
checkGenVerify(*firBuilder, "_FortranAVerify2", 2);
208+
checkGenVerify(*firBuilder, "_FortranAVerify4", 4);
209+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- DerivedTest.cpp -- Derived type runtime builder 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/Runtime/Derived.h"
10+
#include "RuntimeCallTestBase.h"
11+
#include "gtest/gtest.h"
12+
13+
TEST_F(RuntimeCallTest, genDerivedTypeInitialize) {
14+
auto loc = firBuilder->getUnknownLoc();
15+
mlir::Type seqTy =
16+
fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty);
17+
mlir::Value box = firBuilder->create<fir::UndefOp>(loc, seqTy);
18+
fir::runtime::genDerivedTypeInitialize(*firBuilder, loc, box);
19+
checkCallOpFromResultBox(box, "_FortranAInitialize", 1);
20+
}
21+
22+
TEST_F(RuntimeCallTest, genDerivedTypeDestroy) {
23+
auto loc = firBuilder->getUnknownLoc();
24+
mlir::Type seqTy =
25+
fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty);
26+
mlir::Value box = firBuilder->create<fir::UndefOp>(loc, seqTy);
27+
fir::runtime::genDerivedTypeDestroy(*firBuilder, loc, box);
28+
checkCallOpFromResultBox(box, "_FortranADestroy", 1, /*addLocArg=*/false);
29+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//===- NumericTest.cpp -- Numeric intrinsic runtime builder 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/Runtime/Numeric.h"
10+
#include "RuntimeCallTestBase.h"
11+
#include "gtest/gtest.h"
12+
13+
void testGenExponent(fir::FirOpBuilder &builder, mlir::Type resultType,
14+
mlir::Type xType, llvm::StringRef fctName) {
15+
auto loc = builder.getUnknownLoc();
16+
mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
17+
mlir::Value exp = fir::runtime::genExponent(builder, loc, resultType, x);
18+
checkCallOp(exp.getDefiningOp(), fctName, 1, /*addLocArg=*/false);
19+
}
20+
21+
TEST_F(RuntimeCallTest, genExponentTest) {
22+
testGenExponent(*firBuilder, i32Ty, f32Ty, "_FortranAExponent4_4");
23+
testGenExponent(*firBuilder, i64Ty, f32Ty, "_FortranAExponent4_8");
24+
testGenExponent(*firBuilder, i32Ty, f64Ty, "_FortranAExponent8_4");
25+
testGenExponent(*firBuilder, i64Ty, f64Ty, "_FortranAExponent8_8");
26+
testGenExponent(*firBuilder, i32Ty, f80Ty, "_FortranAExponent10_4");
27+
testGenExponent(*firBuilder, i64Ty, f80Ty, "_FortranAExponent10_8");
28+
testGenExponent(*firBuilder, i32Ty, f128Ty, "_FortranAExponent16_4");
29+
testGenExponent(*firBuilder, i64Ty, f128Ty, "_FortranAExponent16_8");
30+
}
31+
32+
void testGenX(fir::FirOpBuilder &builder, mlir::Type xType,
33+
mlir::Value (*genFct)(fir::FirOpBuilder &, Location, mlir::Value),
34+
llvm::StringRef fctName) {
35+
auto loc = builder.getUnknownLoc();
36+
mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
37+
mlir::Value val = genFct(builder, loc, x);
38+
checkCallOp(val.getDefiningOp(), fctName, 1, /*addLocArg=*/false);
39+
}
40+
41+
TEST_F(RuntimeCallTest, genFractionTest) {
42+
testGenX(*firBuilder, f32Ty, fir::runtime::genFraction, "_FortranAFraction4");
43+
testGenX(*firBuilder, f64Ty, fir::runtime::genFraction, "_FortranAFraction8");
44+
testGenX(
45+
*firBuilder, f80Ty, fir::runtime::genFraction, "_FortranAFraction10");
46+
testGenX(
47+
*firBuilder, f128Ty, fir::runtime::genFraction, "_FortranAFraction16");
48+
}
49+
50+
void testGenNearest(fir::FirOpBuilder &builder, mlir::Type xType,
51+
mlir::Type sType, llvm::StringRef fctName) {
52+
auto loc = builder.getUnknownLoc();
53+
mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
54+
mlir::Value s = builder.create<fir::UndefOp>(loc, sType);
55+
mlir::Value nearest = fir::runtime::genNearest(builder, loc, x, s);
56+
checkCallOp(nearest.getDefiningOp(), fctName, 2, /*addLocArg=*/false);
57+
auto callOp = mlir::dyn_cast<fir::CallOp>(nearest.getDefiningOp());
58+
mlir::Value select = callOp.getOperands()[1];
59+
EXPECT_TRUE(mlir::isa<mlir::SelectOp>(select.getDefiningOp()));
60+
auto selectOp = mlir::dyn_cast<mlir::SelectOp>(select.getDefiningOp());
61+
mlir::Value cmp = selectOp.condition();
62+
EXPECT_TRUE(mlir::isa<mlir::arith::CmpFOp>(cmp.getDefiningOp()));
63+
auto cmpOp = mlir::dyn_cast<mlir::arith::CmpFOp>(cmp.getDefiningOp());
64+
EXPECT_EQ(s, cmpOp.lhs());
65+
}
66+
67+
TEST_F(RuntimeCallTest, genNearestTest) {
68+
testGenNearest(*firBuilder, f32Ty, f32Ty, "_FortranANearest4");
69+
testGenNearest(*firBuilder, f64Ty, f32Ty, "_FortranANearest8");
70+
testGenNearest(*firBuilder, f80Ty, f32Ty, "_FortranANearest10");
71+
testGenNearest(*firBuilder, f128Ty, f32Ty, "_FortranANearest16");
72+
}
73+
74+
TEST_F(RuntimeCallTest, genRRSpacingTest) {
75+
testGenX(
76+
*firBuilder, f32Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing4");
77+
testGenX(
78+
*firBuilder, f64Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing8");
79+
testGenX(
80+
*firBuilder, f80Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing10");
81+
testGenX(
82+
*firBuilder, f128Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing16");
83+
}
84+
85+
void testGenXI(fir::FirOpBuilder &builder, mlir::Type xType, mlir::Type iType,
86+
mlir::Value (*genFct)(
87+
fir::FirOpBuilder &, Location, mlir::Value, mlir::Value),
88+
llvm::StringRef fctName) {
89+
auto loc = builder.getUnknownLoc();
90+
mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
91+
mlir::Value i = builder.create<fir::UndefOp>(loc, iType);
92+
mlir::Value val = genFct(builder, loc, x, i);
93+
checkCallOp(val.getDefiningOp(), fctName, 2, /*addLocArg=*/false);
94+
}
95+
96+
TEST_F(RuntimeCallTest, genScaleTest) {
97+
testGenXI(
98+
*firBuilder, f32Ty, f32Ty, fir::runtime::genScale, "_FortranAScale4");
99+
testGenXI(
100+
*firBuilder, f64Ty, f32Ty, fir::runtime::genScale, "_FortranAScale8");
101+
testGenXI(
102+
*firBuilder, f80Ty, f32Ty, fir::runtime::genScale, "_FortranAScale10");
103+
testGenXI(
104+
*firBuilder, f128Ty, f32Ty, fir::runtime::genScale, "_FortranAScale16");
105+
}
106+
107+
TEST_F(RuntimeCallTest, genSetExponentTest) {
108+
testGenXI(*firBuilder, f32Ty, f32Ty, fir::runtime::genSetExponent,
109+
"_FortranASetExponent4");
110+
testGenXI(*firBuilder, f64Ty, f32Ty, fir::runtime::genSetExponent,
111+
"_FortranASetExponent8");
112+
testGenXI(*firBuilder, f80Ty, f32Ty, fir::runtime::genSetExponent,
113+
"_FortranASetExponent10");
114+
testGenXI(*firBuilder, f128Ty, f32Ty, fir::runtime::genSetExponent,
115+
"_FortranASetExponent16");
116+
}
117+
118+
TEST_F(RuntimeCallTest, genSpacingTest) {
119+
testGenX(*firBuilder, f32Ty, fir::runtime::genSpacing, "_FortranASpacing4");
120+
testGenX(*firBuilder, f64Ty, fir::runtime::genSpacing, "_FortranASpacing8");
121+
testGenX(*firBuilder, f80Ty, fir::runtime::genSpacing, "_FortranASpacing10");
122+
testGenX(*firBuilder, f128Ty, fir::runtime::genSpacing, "_FortranASpacing16");
123+
}

0 commit comments

Comments
 (0)