| 1 | //===- TransformationalTest.cpp -- Transformational intrinsic generation --===// |
| 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/Transformational.h" |
| 10 | #include "RuntimeCallTestBase.h" |
| 11 | #include "gtest/gtest.h" |
| 12 | |
| 13 | void testGenBesselJn( |
| 14 | fir::FirOpBuilder &builder, mlir::Type realTy, llvm::StringRef fctName) { |
| 15 | mlir::Location loc = builder.getUnknownLoc(); |
| 16 | mlir::Type i32Ty = builder.getIntegerType(32); |
| 17 | mlir::Type seqTy = |
| 18 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), realTy); |
| 19 | mlir::Value result = builder.create<fir::UndefOp>(loc, seqTy); |
| 20 | mlir::Value n1 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 21 | mlir::Value n2 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 22 | mlir::Value x = builder.create<fir::UndefOp>(loc, realTy); |
| 23 | mlir::Value bn1 = builder.create<fir::UndefOp>(loc, realTy); |
| 24 | mlir::Value bn2 = builder.create<fir::UndefOp>(loc, realTy); |
| 25 | fir::runtime::genBesselJn(builder, loc, result, n1, n2, x, bn1, bn2); |
| 26 | checkCallOpFromResultBox(result, fctName, 6); |
| 27 | } |
| 28 | |
| 29 | TEST_F(RuntimeCallTest, genBesselJnTest) { |
| 30 | testGenBesselJn(*firBuilder, f32Ty, "_FortranABesselJn_4" ); |
| 31 | testGenBesselJn(*firBuilder, f64Ty, "_FortranABesselJn_8" ); |
| 32 | testGenBesselJn(*firBuilder, f80Ty, "_FortranABesselJn_10" ); |
| 33 | testGenBesselJn(*firBuilder, f128Ty, "_FortranABesselJn_16" ); |
| 34 | } |
| 35 | |
| 36 | void testGenBesselJnX0( |
| 37 | fir::FirOpBuilder &builder, mlir::Type realTy, llvm::StringRef fctName) { |
| 38 | mlir::Location loc = builder.getUnknownLoc(); |
| 39 | mlir::Type i32Ty = builder.getIntegerType(32); |
| 40 | mlir::Type seqTy = |
| 41 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), realTy); |
| 42 | mlir::Value result = builder.create<fir::UndefOp>(loc, seqTy); |
| 43 | mlir::Value n1 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 44 | mlir::Value n2 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 45 | fir::runtime::genBesselJnX0(builder, loc, realTy, result, n1, n2); |
| 46 | checkCallOpFromResultBox(result, fctName, 3); |
| 47 | } |
| 48 | |
| 49 | TEST_F(RuntimeCallTest, genBesselJnX0Test) { |
| 50 | testGenBesselJnX0(*firBuilder, f32Ty, "_FortranABesselJnX0_4" ); |
| 51 | testGenBesselJnX0(*firBuilder, f64Ty, "_FortranABesselJnX0_8" ); |
| 52 | testGenBesselJnX0(*firBuilder, f80Ty, "_FortranABesselJnX0_10" ); |
| 53 | testGenBesselJnX0(*firBuilder, f128Ty, "_FortranABesselJnX0_16" ); |
| 54 | } |
| 55 | |
| 56 | void testGenBesselYn( |
| 57 | fir::FirOpBuilder &builder, mlir::Type realTy, llvm::StringRef fctName) { |
| 58 | mlir::Location loc = builder.getUnknownLoc(); |
| 59 | mlir::Type i32Ty = builder.getIntegerType(32); |
| 60 | mlir::Type seqTy = |
| 61 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), realTy); |
| 62 | mlir::Value result = builder.create<fir::UndefOp>(loc, seqTy); |
| 63 | mlir::Value n1 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 64 | mlir::Value n2 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 65 | mlir::Value x = builder.create<fir::UndefOp>(loc, realTy); |
| 66 | mlir::Value bn1 = builder.create<fir::UndefOp>(loc, realTy); |
| 67 | mlir::Value bn2 = builder.create<fir::UndefOp>(loc, realTy); |
| 68 | fir::runtime::genBesselYn(builder, loc, result, n1, n2, x, bn1, bn2); |
| 69 | checkCallOpFromResultBox(result, fctName, 6); |
| 70 | } |
| 71 | |
| 72 | TEST_F(RuntimeCallTest, genBesselYnTest) { |
| 73 | testGenBesselYn(*firBuilder, f32Ty, "_FortranABesselYn_4" ); |
| 74 | testGenBesselYn(*firBuilder, f64Ty, "_FortranABesselYn_8" ); |
| 75 | testGenBesselYn(*firBuilder, f80Ty, "_FortranABesselYn_10" ); |
| 76 | testGenBesselYn(*firBuilder, f128Ty, "_FortranABesselYn_16" ); |
| 77 | } |
| 78 | |
| 79 | void testGenBesselYnX0( |
| 80 | fir::FirOpBuilder &builder, mlir::Type realTy, llvm::StringRef fctName) { |
| 81 | mlir::Location loc = builder.getUnknownLoc(); |
| 82 | mlir::Type i32Ty = builder.getIntegerType(32); |
| 83 | mlir::Type seqTy = |
| 84 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), realTy); |
| 85 | mlir::Value result = builder.create<fir::UndefOp>(loc, seqTy); |
| 86 | mlir::Value n1 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 87 | mlir::Value n2 = builder.create<fir::UndefOp>(loc, i32Ty); |
| 88 | fir::runtime::genBesselYnX0(builder, loc, realTy, result, n1, n2); |
| 89 | checkCallOpFromResultBox(result, fctName, 3); |
| 90 | } |
| 91 | |
| 92 | TEST_F(RuntimeCallTest, genBesselYnX0Test) { |
| 93 | testGenBesselYnX0(*firBuilder, f32Ty, "_FortranABesselYnX0_4" ); |
| 94 | testGenBesselYnX0(*firBuilder, f64Ty, "_FortranABesselYnX0_8" ); |
| 95 | testGenBesselYnX0(*firBuilder, f80Ty, "_FortranABesselYnX0_10" ); |
| 96 | testGenBesselYnX0(*firBuilder, f128Ty, "_FortranABesselYnX0_16" ); |
| 97 | } |
| 98 | |
| 99 | TEST_F(RuntimeCallTest, genCshiftTest) { |
| 100 | auto loc = firBuilder->getUnknownLoc(); |
| 101 | mlir::Type seqTy = |
| 102 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 103 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 104 | mlir::Value array = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 105 | mlir::Value shift = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 106 | mlir::Value dim = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 107 | fir::runtime::genCshift(*firBuilder, loc, result, array, shift, dim); |
| 108 | checkCallOpFromResultBox(result, "_FortranACshift" , 4); |
| 109 | } |
| 110 | |
| 111 | TEST_F(RuntimeCallTest, genCshiftVectorTest) { |
| 112 | auto loc = firBuilder->getUnknownLoc(); |
| 113 | mlir::Type seqTy = |
| 114 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 115 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 116 | mlir::Value array = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 117 | mlir::Value shift = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 118 | fir::runtime::genCshiftVector(*firBuilder, loc, result, array, shift); |
| 119 | checkCallOpFromResultBox(result, "_FortranACshiftVector" , 3); |
| 120 | } |
| 121 | |
| 122 | TEST_F(RuntimeCallTest, genEoshiftTest) { |
| 123 | auto loc = firBuilder->getUnknownLoc(); |
| 124 | mlir::Type seqTy = |
| 125 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 126 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 127 | mlir::Value array = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 128 | mlir::Value shift = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 129 | mlir::Value bound = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 130 | mlir::Value dim = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 131 | fir::runtime::genEoshift(*firBuilder, loc, result, array, shift, bound, dim); |
| 132 | checkCallOpFromResultBox(result, "_FortranAEoshift" , 5); |
| 133 | } |
| 134 | |
| 135 | TEST_F(RuntimeCallTest, genEoshiftVectorTest) { |
| 136 | auto loc = firBuilder->getUnknownLoc(); |
| 137 | mlir::Type seqTy = |
| 138 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 139 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 140 | mlir::Value array = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 141 | mlir::Value shift = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 142 | mlir::Value bound = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 143 | fir::runtime::genEoshiftVector(*firBuilder, loc, result, array, shift, bound); |
| 144 | checkCallOpFromResultBox(result, "_FortranAEoshiftVector" , 4); |
| 145 | } |
| 146 | |
| 147 | void testGenMatmul(fir::FirOpBuilder &builder, mlir::Type eleTy1, |
| 148 | mlir::Type eleTy2, llvm::StringRef funcName) { |
| 149 | auto loc = builder.getUnknownLoc(); |
| 150 | mlir::Type resultTy = |
| 151 | fir::ReferenceType::get(fir::BoxType::get(builder.getNoneType())); |
| 152 | mlir::Type seqTy1 = |
| 153 | fir::SequenceType::get(fir::SequenceType::Shape(2, 10), eleTy1); |
| 154 | mlir::Type seqTy2 = |
| 155 | fir::SequenceType::get(fir::SequenceType::Shape(2, 10), eleTy2); |
| 156 | mlir::Type boxTy1 = fir::BoxType::get(seqTy1); |
| 157 | mlir::Type boxTy2 = fir::BoxType::get(seqTy2); |
| 158 | mlir::Value result = builder.create<fir::UndefOp>(loc, resultTy); |
| 159 | mlir::Value matrixA = builder.create<fir::UndefOp>(loc, boxTy1); |
| 160 | mlir::Value matrixB = builder.create<fir::UndefOp>(loc, boxTy2); |
| 161 | fir::runtime::genMatmul(builder, loc, result, matrixA, matrixB); |
| 162 | checkCallOpFromResultBox(result, funcName, 3); |
| 163 | } |
| 164 | |
| 165 | TEST_F(RuntimeCallTest, genMatmulTest) { |
| 166 | testGenMatmul(*firBuilder, i32Ty, i16Ty, "_FortranAMatmulInteger4Integer2" ); |
| 167 | testGenMatmul(*firBuilder, i32Ty, f64Ty, "_FortranAMatmulInteger4Real8" ); |
| 168 | testGenMatmul(*firBuilder, i32Ty, c8Ty, "_FortranAMatmulInteger4Complex8" ); |
| 169 | testGenMatmul(*firBuilder, f32Ty, i16Ty, "_FortranAMatmulReal4Integer2" ); |
| 170 | testGenMatmul(*firBuilder, f32Ty, f64Ty, "_FortranAMatmulReal4Real8" ); |
| 171 | testGenMatmul(*firBuilder, f32Ty, c8Ty, "_FortranAMatmulReal4Complex8" ); |
| 172 | testGenMatmul(*firBuilder, c4Ty, i16Ty, "_FortranAMatmulComplex4Integer2" ); |
| 173 | testGenMatmul(*firBuilder, c4Ty, f64Ty, "_FortranAMatmulComplex4Real8" ); |
| 174 | testGenMatmul(*firBuilder, c4Ty, c8Ty, "_FortranAMatmulComplex4Complex8" ); |
| 175 | testGenMatmul(*firBuilder, f80Ty, f128Ty, "_FortranAMatmulReal10Real16" ); |
| 176 | testGenMatmul(*firBuilder, f80Ty, i128Ty, "_FortranAMatmulReal10Integer16" ); |
| 177 | testGenMatmul(*firBuilder, f128Ty, i128Ty, "_FortranAMatmulReal16Integer16" ); |
| 178 | testGenMatmul( |
| 179 | *firBuilder, logical1Ty, logical2Ty, "_FortranAMatmulLogical1Logical2" ); |
| 180 | testGenMatmul( |
| 181 | *firBuilder, logical4Ty, logical8Ty, "_FortranAMatmulLogical4Logical8" ); |
| 182 | } |
| 183 | |
| 184 | TEST_F(RuntimeCallTest, genPackTest) { |
| 185 | auto loc = firBuilder->getUnknownLoc(); |
| 186 | mlir::Type seqTy = |
| 187 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 188 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 189 | mlir::Value array = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 190 | mlir::Value mask = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 191 | mlir::Value vector = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 192 | fir::runtime::genPack(*firBuilder, loc, result, array, mask, vector); |
| 193 | checkCallOpFromResultBox(result, "_FortranAPack" , 4); |
| 194 | } |
| 195 | |
| 196 | TEST_F(RuntimeCallTest, genReshapeTest) { |
| 197 | auto loc = firBuilder->getUnknownLoc(); |
| 198 | mlir::Type seqTy = |
| 199 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 200 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 201 | mlir::Value source = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 202 | mlir::Value shape = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 203 | mlir::Value pad = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 204 | mlir::Value order = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 205 | fir::runtime::genReshape(*firBuilder, loc, result, source, shape, pad, order); |
| 206 | checkCallOpFromResultBox(result, "_FortranAReshape" , 5); |
| 207 | } |
| 208 | |
| 209 | TEST_F(RuntimeCallTest, genSpreadTest) { |
| 210 | auto loc = firBuilder->getUnknownLoc(); |
| 211 | mlir::Type seqTy = |
| 212 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 213 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 214 | mlir::Value source = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 215 | mlir::Value dim = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 216 | mlir::Value ncopies = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 217 | fir::runtime::genSpread(*firBuilder, loc, result, source, dim, ncopies); |
| 218 | checkCallOpFromResultBox(result, "_FortranASpread" , 4); |
| 219 | } |
| 220 | |
| 221 | TEST_F(RuntimeCallTest, genTransposeTest) { |
| 222 | auto loc = firBuilder->getUnknownLoc(); |
| 223 | mlir::Type seqTy = |
| 224 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 225 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 226 | mlir::Value source = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 227 | fir::runtime::genTranspose(*firBuilder, loc, result, source); |
| 228 | checkCallOpFromResultBox(result, "_FortranATranspose" , 2); |
| 229 | } |
| 230 | |
| 231 | TEST_F(RuntimeCallTest, genUnpack) { |
| 232 | auto loc = firBuilder->getUnknownLoc(); |
| 233 | mlir::Type seqTy = |
| 234 | fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty); |
| 235 | mlir::Value result = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 236 | mlir::Value vector = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 237 | mlir::Value mask = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 238 | mlir::Value field = firBuilder->create<fir::UndefOp>(loc, seqTy); |
| 239 | fir::runtime::genUnpack(*firBuilder, loc, result, vector, mask, field); |
| 240 | checkCallOpFromResultBox(result, "_FortranAUnpack" , 4); |
| 241 | } |
| 242 | |