1 //===-- PolymorphicOpConversion.cpp ---------------------------------------===// 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/Lower/BuiltinModules.h" 10 #include "flang/Optimizer/Builder/Todo.h" 11 #include "flang/Optimizer/Dialect/FIRDialect.h" 12 #include "flang/Optimizer/Dialect/FIROps.h" 13 #include "flang/Optimizer/Dialect/FIROpsSupport.h" 14 #include "flang/Optimizer/Dialect/FIRType.h" 15 #include "flang/Optimizer/Dialect/Support/FIRContext.h" 16 #include "flang/Optimizer/Dialect/Support/KindMapping.h" 17 #include "flang/Optimizer/Support/InternalNames.h" 18 #include "flang/Optimizer/Support/TypeCode.h" 19 #include "flang/Optimizer/Support/Utils.h" 20 #include "flang/Optimizer/Transforms/Passes.h" 21 #include "flang/Runtime/derived-api.h" 22 #include "flang/Semantics/runtime-type-info.h" 23 #include "mlir/Dialect/Affine/IR/AffineOps.h" 24 #include "mlir/Dialect/Arith/IR/Arith.h" 25 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h" 26 #include "mlir/Dialect/Func/IR/FuncOps.h" 27 #include "mlir/IR/BuiltinOps.h" 28 #include "mlir/Pass/Pass.h" 29 #include "mlir/Transforms/DialectConversion.h" 30 #include "llvm/ADT/SmallSet.h" 31 #include "llvm/Support/CommandLine.h" 32 33 namespace fir { 34 #define GEN_PASS_DEF_POLYMORPHICOPCONVERSION 35 #include "flang/Optimizer/Transforms/Passes.h.inc" 36 } // namespace fir 37 38 using namespace fir; 39 using namespace mlir; 40 41 namespace { 42 43 /// SelectTypeOp converted to an if-then-else chain 44 /// 45 /// This lowers the test conditions to calls into the runtime. 46 class SelectTypeConv : public OpConversionPattern<fir::SelectTypeOp> { 47 public: 48 using OpConversionPattern<fir::SelectTypeOp>::OpConversionPattern; 49 50 SelectTypeConv(mlir::MLIRContext *ctx) 51 : mlir::OpConversionPattern<fir::SelectTypeOp>(ctx) {} 52 53 mlir::LogicalResult 54 matchAndRewrite(fir::SelectTypeOp selectType, OpAdaptor adaptor, 55 mlir::ConversionPatternRewriter &rewriter) const override; 56 57 private: 58 // Generate comparison of type descriptor addresses. 59 mlir::Value genTypeDescCompare(mlir::Location loc, mlir::Value selector, 60 mlir::Type ty, mlir::ModuleOp mod, 61 mlir::PatternRewriter &rewriter) const; 62 63 mlir::LogicalResult genTypeLadderStep(mlir::Location loc, 64 mlir::Value selector, 65 mlir::Attribute attr, mlir::Block *dest, 66 std::optional<mlir::ValueRange> destOps, 67 mlir::ModuleOp mod, 68 mlir::PatternRewriter &rewriter, 69 fir::KindMapping &kindMap) const; 70 71 llvm::SmallSet<llvm::StringRef, 4> collectAncestors(fir::TypeInfoOp dt, 72 mlir::ModuleOp mod) const; 73 }; 74 75 /// Lower `fir.dispatch` operation. A virtual call to a method in a dispatch 76 /// table. 77 struct DispatchOpConv : public OpConversionPattern<fir::DispatchOp> { 78 using OpConversionPattern<fir::DispatchOp>::OpConversionPattern; 79 80 DispatchOpConv(mlir::MLIRContext *ctx, const BindingTables &bindingTables) 81 : mlir::OpConversionPattern<fir::DispatchOp>(ctx), 82 bindingTables(bindingTables) {} 83 84 mlir::LogicalResult 85 matchAndRewrite(fir::DispatchOp dispatch, OpAdaptor adaptor, 86 mlir::ConversionPatternRewriter &rewriter) const override { 87 mlir::Location loc = dispatch.getLoc(); 88 89 if (bindingTables.empty()) 90 return emitError(loc) << "no binding tables found"; 91 92 // Get derived type information. 93 mlir::Type declaredType = 94 fir::getDerivedType(dispatch.getObject().getType().getEleTy()); 95 assert(mlir::isa<fir::RecordType>(declaredType) && "expecting fir.type"); 96 auto recordType = mlir::dyn_cast<fir::RecordType>(declaredType); 97 98 // Lookup for the binding table. 99 auto bindingsIter = bindingTables.find(recordType.getName()); 100 if (bindingsIter == bindingTables.end()) 101 return emitError(loc) 102 << "cannot find binding table for " << recordType.getName(); 103 104 // Lookup for the binding. 105 const BindingTable &bindingTable = bindingsIter->second; 106 auto bindingIter = bindingTable.find(dispatch.getMethod()); 107 if (bindingIter == bindingTable.end()) 108 return emitError(loc) 109 << "cannot find binding for " << dispatch.getMethod(); 110 unsigned bindingIdx = bindingIter->second; 111 112 mlir::Value passedObject = dispatch.getObject(); 113 114 auto module = dispatch.getOperation()->getParentOfType<mlir::ModuleOp>(); 115 Type typeDescTy; 116 std::string typeDescName = 117 NameUniquer::getTypeDescriptorName(recordType.getName()); 118 if (auto global = module.lookupSymbol<fir::GlobalOp>(typeDescName)) { 119 typeDescTy = global.getType(); 120 } 121 122 // clang-format off 123 // Before: 124 // fir.dispatch "proc1"(%11 : 125 // !fir.class<!fir.heap<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>) 126 127 // After: 128 // %12 = fir.box_tdesc %11 : (!fir.class<!fir.heap<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>) -> !fir.tdesc<none> 129 // %13 = fir.convert %12 : (!fir.tdesc<none>) -> !fir.ref<!fir.type<_QM__fortran_type_infoTderivedtype>> 130 // %14 = fir.field_index binding, !fir.type<_QM__fortran_type_infoTderivedtype> 131 // %15 = fir.coordinate_of %13, %14 : (!fir.ref<!fir.type<_QM__fortran_type_infoTderivedtype>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?x!fir.type<_QM__fortran_type_infoTbinding>>>>> 132 // %bindings = fir.load %15 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x!fir.type<_QM__fortran_type_infoTbinding>>>>> 133 // %16 = fir.box_addr %bindings : (!fir.box<!fir.ptr<!fir.array<?x!fir.type<_QM__fortran_type_infoTbinding>>>>) -> !fir.ptr<!fir.array<?x!fir.type<_QM__fortran_type_infoTbinding>>> 134 // %17 = fir.coordinate_of %16, %c0 : (!fir.ptr<!fir.array<?x!fir.type<_QM__fortran_type_infoTbinding>>>, index) -> !fir.ref<!fir.type<_QM__fortran_type_infoTbinding>> 135 // %18 = fir.field_index proc, !fir.type<_QM__fortran_type_infoTbinding> 136 // %19 = fir.coordinate_of %17, %18 : (!fir.ref<!fir.type<_QM__fortran_type_infoTbinding>>, !fir.field) -> !fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_funptr>> 137 // %20 = fir.field_index __address, !fir.type<_QM__fortran_builtinsT__builtin_c_funptr> 138 // %21 = fir.coordinate_of %19, %20 : (!fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_funptr>>, !fir.field) -> !fir.ref<i64> 139 // %22 = fir.load %21 : !fir.ref<i64> 140 // %23 = fir.convert %22 : (i64) -> (() -> ()) 141 // fir.call %23() : () -> () 142 // clang-format on 143 144 // Load the descriptor. 145 mlir::Type fieldTy = fir::FieldType::get(rewriter.getContext()); 146 mlir::Type tdescType = 147 fir::TypeDescType::get(mlir::NoneType::get(rewriter.getContext())); 148 mlir::Value boxDesc = 149 rewriter.create<fir::BoxTypeDescOp>(loc, tdescType, passedObject); 150 boxDesc = rewriter.create<fir::ConvertOp>( 151 loc, fir::ReferenceType::get(typeDescTy), boxDesc); 152 153 // Load the bindings descriptor. 154 auto bindingsCompName = Fortran::semantics::bindingDescCompName; 155 fir::RecordType typeDescRecTy = mlir::cast<fir::RecordType>(typeDescTy); 156 mlir::Value field = rewriter.create<fir::FieldIndexOp>( 157 loc, fieldTy, bindingsCompName, typeDescRecTy, mlir::ValueRange{}); 158 mlir::Type coorTy = 159 fir::ReferenceType::get(typeDescRecTy.getType(bindingsCompName)); 160 mlir::Value bindingBoxAddr = 161 rewriter.create<fir::CoordinateOp>(loc, coorTy, boxDesc, field); 162 mlir::Value bindingBox = rewriter.create<fir::LoadOp>(loc, bindingBoxAddr); 163 164 // Load the correct binding. 165 mlir::Value bindings = rewriter.create<fir::BoxAddrOp>(loc, bindingBox); 166 fir::RecordType bindingTy = fir::unwrapIfDerived( 167 mlir::cast<fir::BaseBoxType>(bindingBox.getType())); 168 mlir::Type bindingAddrTy = fir::ReferenceType::get(bindingTy); 169 mlir::Value bindingIdxVal = rewriter.create<mlir::arith::ConstantOp>( 170 loc, rewriter.getIndexType(), rewriter.getIndexAttr(bindingIdx)); 171 mlir::Value bindingAddr = rewriter.create<fir::CoordinateOp>( 172 loc, bindingAddrTy, bindings, bindingIdxVal); 173 174 // Get the function pointer. 175 auto procCompName = Fortran::semantics::procCompName; 176 mlir::Value procField = rewriter.create<fir::FieldIndexOp>( 177 loc, fieldTy, procCompName, bindingTy, mlir::ValueRange{}); 178 fir::RecordType procTy = 179 mlir::cast<fir::RecordType>(bindingTy.getType(procCompName)); 180 mlir::Type procRefTy = fir::ReferenceType::get(procTy); 181 mlir::Value procRef = rewriter.create<fir::CoordinateOp>( 182 loc, procRefTy, bindingAddr, procField); 183 184 auto addressFieldName = Fortran::lower::builtin::cptrFieldName; 185 mlir::Value addressField = rewriter.create<fir::FieldIndexOp>( 186 loc, fieldTy, addressFieldName, procTy, mlir::ValueRange{}); 187 mlir::Type addressTy = procTy.getType(addressFieldName); 188 mlir::Type addressRefTy = fir::ReferenceType::get(addressTy); 189 mlir::Value addressRef = rewriter.create<fir::CoordinateOp>( 190 loc, addressRefTy, procRef, addressField); 191 mlir::Value address = rewriter.create<fir::LoadOp>(loc, addressRef); 192 193 // Get the function type. 194 llvm::SmallVector<mlir::Type> argTypes; 195 for (mlir::Value operand : dispatch.getArgs()) 196 argTypes.push_back(operand.getType()); 197 llvm::SmallVector<mlir::Type> resTypes; 198 if (!dispatch.getResults().empty()) 199 resTypes.push_back(dispatch.getResults()[0].getType()); 200 201 mlir::Type funTy = 202 mlir::FunctionType::get(rewriter.getContext(), argTypes, resTypes); 203 mlir::Value funcPtr = rewriter.create<fir::ConvertOp>(loc, funTy, address); 204 205 // Make the call. 206 llvm::SmallVector<mlir::Value> args{funcPtr}; 207 args.append(dispatch.getArgs().begin(), dispatch.getArgs().end()); 208 rewriter.replaceOpWithNewOp<fir::CallOp>(dispatch, resTypes, nullptr, args); 209 return mlir::success(); 210 } 211 212 private: 213 BindingTables bindingTables; 214 }; 215 216 /// Convert FIR structured control flow ops to CFG ops. 217 class PolymorphicOpConversion 218 : public fir::impl::PolymorphicOpConversionBase<PolymorphicOpConversion> { 219 public: 220 mlir::LogicalResult initialize(mlir::MLIRContext *ctx) override { 221 return mlir::success(); 222 } 223 224 void runOnOperation() override { 225 auto *context = &getContext(); 226 mlir::ModuleOp mod = getOperation(); 227 mlir::RewritePatternSet patterns(context); 228 229 BindingTables bindingTables; 230 buildBindingTables(bindingTables, mod); 231 232 patterns.insert<SelectTypeConv>(context); 233 patterns.insert<DispatchOpConv>(context, bindingTables); 234 mlir::ConversionTarget target(*context); 235 target.addLegalDialect<mlir::affine::AffineDialect, 236 mlir::cf::ControlFlowDialect, FIROpsDialect, 237 mlir::func::FuncDialect>(); 238 239 // apply the patterns 240 target.addIllegalOp<SelectTypeOp>(); 241 target.addIllegalOp<DispatchOp>(); 242 target.markUnknownOpDynamicallyLegal([](Operation *) { return true; }); 243 if (mlir::failed(mlir::applyPartialConversion(getOperation(), target, 244 std::move(patterns)))) { 245 mlir::emitError(mlir::UnknownLoc::get(context), 246 "error in converting to CFG\n"); 247 signalPassFailure(); 248 } 249 } 250 }; 251 } // namespace 252 253 mlir::LogicalResult SelectTypeConv::matchAndRewrite( 254 fir::SelectTypeOp selectType, OpAdaptor adaptor, 255 mlir::ConversionPatternRewriter &rewriter) const { 256 auto operands = adaptor.getOperands(); 257 auto typeGuards = selectType.getCases(); 258 unsigned typeGuardNum = typeGuards.size(); 259 auto selector = selectType.getSelector(); 260 auto loc = selectType.getLoc(); 261 auto mod = selectType.getOperation()->getParentOfType<mlir::ModuleOp>(); 262 fir::KindMapping kindMap = fir::getKindMapping(mod); 263 264 // Order type guards so the condition and branches are done to respect the 265 // Execution of SELECT TYPE construct as described in the Fortran 2018 266 // standard 11.1.11.2 point 4. 267 // 1. If a TYPE IS type guard statement matches the selector, the block 268 // following that statement is executed. 269 // 2. Otherwise, if exactly one CLASS IS type guard statement matches the 270 // selector, the block following that statement is executed. 271 // 3. Otherwise, if several CLASS IS type guard statements match the 272 // selector, one of these statements will inevitably specify a type that 273 // is an extension of all the types specified in the others; the block 274 // following that statement is executed. 275 // 4. Otherwise, if there is a CLASS DEFAULT type guard statement, the block 276 // following that statement is executed. 277 // 5. Otherwise, no block is executed. 278 279 llvm::SmallVector<unsigned> orderedTypeGuards; 280 llvm::SmallVector<unsigned> orderedClassIsGuards; 281 unsigned defaultGuard = typeGuardNum - 1; 282 283 // The following loop go through the type guards in the fir.select_type 284 // operation and sort them into two lists. 285 // - All the TYPE IS type guard are added in order to the orderedTypeGuards 286 // list. This list is used at the end to generate the if-then-else ladder. 287 // - CLASS IS type guard are added in a separate list. If a CLASS IS type 288 // guard type extends a type already present, the type guard is inserted 289 // before in the list to respect point 3. above. Otherwise it is just 290 // added in order at the end. 291 for (unsigned t = 0; t < typeGuardNum; ++t) { 292 if (auto a = mlir::dyn_cast<fir::ExactTypeAttr>(typeGuards[t])) { 293 orderedTypeGuards.push_back(t); 294 continue; 295 } 296 297 if (auto a = mlir::dyn_cast<fir::SubclassAttr>(typeGuards[t])) { 298 if (auto recTy = mlir::dyn_cast<fir::RecordType>(a.getType())) { 299 auto dt = mod.lookupSymbol<fir::TypeInfoOp>(recTy.getName()); 300 assert(dt && "dispatch table not found"); 301 llvm::SmallSet<llvm::StringRef, 4> ancestors = 302 collectAncestors(dt, mod); 303 if (!ancestors.empty()) { 304 auto it = orderedClassIsGuards.begin(); 305 while (it != orderedClassIsGuards.end()) { 306 fir::SubclassAttr sAttr = 307 mlir::dyn_cast<fir::SubclassAttr>(typeGuards[*it]); 308 if (auto ty = mlir::dyn_cast<fir::RecordType>(sAttr.getType())) { 309 if (ancestors.contains(ty.getName())) 310 break; 311 } 312 ++it; 313 } 314 if (it != orderedClassIsGuards.end()) { 315 // Parent type is present so place it before. 316 orderedClassIsGuards.insert(it, t); 317 continue; 318 } 319 } 320 } 321 orderedClassIsGuards.push_back(t); 322 } 323 } 324 orderedTypeGuards.append(orderedClassIsGuards); 325 orderedTypeGuards.push_back(defaultGuard); 326 assert(orderedTypeGuards.size() == typeGuardNum && 327 "ordered type guard size doesn't match number of type guards"); 328 329 for (unsigned idx : orderedTypeGuards) { 330 auto *dest = selectType.getSuccessor(idx); 331 std::optional<mlir::ValueRange> destOps = 332 selectType.getSuccessorOperands(operands, idx); 333 if (mlir::dyn_cast<mlir::UnitAttr>(typeGuards[idx])) 334 rewriter.replaceOpWithNewOp<mlir::cf::BranchOp>( 335 selectType, dest, destOps.value_or(mlir::ValueRange{})); 336 else if (mlir::failed(genTypeLadderStep(loc, selector, typeGuards[idx], 337 dest, destOps, mod, rewriter, 338 kindMap))) 339 return mlir::failure(); 340 } 341 return mlir::success(); 342 } 343 344 mlir::LogicalResult SelectTypeConv::genTypeLadderStep( 345 mlir::Location loc, mlir::Value selector, mlir::Attribute attr, 346 mlir::Block *dest, std::optional<mlir::ValueRange> destOps, 347 mlir::ModuleOp mod, mlir::PatternRewriter &rewriter, 348 fir::KindMapping &kindMap) const { 349 mlir::Value cmp; 350 // TYPE IS type guard comparison are all done inlined. 351 if (auto a = mlir::dyn_cast<fir::ExactTypeAttr>(attr)) { 352 if (fir::isa_trivial(a.getType()) || 353 mlir::isa<fir::CharacterType>(a.getType())) { 354 // For type guard statement with Intrinsic type spec the type code of 355 // the descriptor is compared. 356 int code = fir::getTypeCode(a.getType(), kindMap); 357 if (code == 0) 358 return mlir::emitError(loc) 359 << "type code unavailable for " << a.getType(); 360 mlir::Value typeCode = rewriter.create<mlir::arith::ConstantOp>( 361 loc, rewriter.getI8IntegerAttr(code)); 362 mlir::Value selectorTypeCode = rewriter.create<fir::BoxTypeCodeOp>( 363 loc, rewriter.getI8Type(), selector); 364 cmp = rewriter.create<mlir::arith::CmpIOp>( 365 loc, mlir::arith::CmpIPredicate::eq, selectorTypeCode, typeCode); 366 } else { 367 // Flang inline the kind parameter in the type descriptor so we can 368 // directly check if the type descriptor addresses are identical for 369 // the TYPE IS type guard statement. 370 mlir::Value res = 371 genTypeDescCompare(loc, selector, a.getType(), mod, rewriter); 372 if (!res) 373 return mlir::failure(); 374 cmp = res; 375 } 376 // CLASS IS type guard statement is done with a runtime call. 377 } else if (auto a = mlir::dyn_cast<fir::SubclassAttr>(attr)) { 378 // Retrieve the type descriptor from the type guard statement record type. 379 assert(mlir::isa<fir::RecordType>(a.getType()) && "expect fir.record type"); 380 fir::RecordType recTy = mlir::dyn_cast<fir::RecordType>(a.getType()); 381 std::string typeDescName = 382 fir::NameUniquer::getTypeDescriptorName(recTy.getName()); 383 auto typeDescGlobal = mod.lookupSymbol<fir::GlobalOp>(typeDescName); 384 auto typeDescAddr = rewriter.create<fir::AddrOfOp>( 385 loc, fir::ReferenceType::get(typeDescGlobal.getType()), 386 typeDescGlobal.getSymbol()); 387 mlir::Type typeDescTy = ReferenceType::get(rewriter.getNoneType()); 388 mlir::Value typeDesc = 389 rewriter.create<ConvertOp>(loc, typeDescTy, typeDescAddr); 390 391 // Prepare the selector descriptor for the runtime call. 392 mlir::Type descNoneTy = fir::BoxType::get(rewriter.getNoneType()); 393 mlir::Value descSelector = 394 rewriter.create<ConvertOp>(loc, descNoneTy, selector); 395 396 // Generate runtime call. 397 llvm::StringRef fctName = RTNAME_STRING(ClassIs); 398 mlir::func::FuncOp callee; 399 { 400 // Since conversion is done in parallel for each fir.select_type 401 // operation, the runtime function insertion must be threadsafe. 402 callee = 403 fir::createFuncOp(rewriter.getUnknownLoc(), mod, fctName, 404 rewriter.getFunctionType({descNoneTy, typeDescTy}, 405 rewriter.getI1Type())); 406 } 407 cmp = rewriter 408 .create<fir::CallOp>(loc, callee, 409 mlir::ValueRange{descSelector, typeDesc}) 410 .getResult(0); 411 } 412 413 auto *thisBlock = rewriter.getInsertionBlock(); 414 auto *newBlock = 415 rewriter.createBlock(dest->getParent(), mlir::Region::iterator(dest)); 416 rewriter.setInsertionPointToEnd(thisBlock); 417 if (destOps.has_value()) 418 rewriter.create<mlir::cf::CondBranchOp>(loc, cmp, dest, destOps.value(), 419 newBlock, std::nullopt); 420 else 421 rewriter.create<mlir::cf::CondBranchOp>(loc, cmp, dest, newBlock); 422 rewriter.setInsertionPointToEnd(newBlock); 423 return mlir::success(); 424 } 425 426 // Generate comparison of type descriptor addresses. 427 mlir::Value 428 SelectTypeConv::genTypeDescCompare(mlir::Location loc, mlir::Value selector, 429 mlir::Type ty, mlir::ModuleOp mod, 430 mlir::PatternRewriter &rewriter) const { 431 assert(mlir::isa<fir::RecordType>(ty) && "expect fir.record type"); 432 fir::RecordType recTy = mlir::dyn_cast<fir::RecordType>(ty); 433 std::string typeDescName = 434 fir::NameUniquer::getTypeDescriptorName(recTy.getName()); 435 auto typeDescGlobal = mod.lookupSymbol<fir::GlobalOp>(typeDescName); 436 if (!typeDescGlobal) 437 return {}; 438 auto typeDescAddr = rewriter.create<fir::AddrOfOp>( 439 loc, fir::ReferenceType::get(typeDescGlobal.getType()), 440 typeDescGlobal.getSymbol()); 441 auto intPtrTy = rewriter.getIndexType(); 442 mlir::Type tdescType = 443 fir::TypeDescType::get(mlir::NoneType::get(rewriter.getContext())); 444 mlir::Value selectorTdescAddr = 445 rewriter.create<fir::BoxTypeDescOp>(loc, tdescType, selector); 446 auto typeDescInt = 447 rewriter.create<fir::ConvertOp>(loc, intPtrTy, typeDescAddr); 448 auto selectorTdescInt = 449 rewriter.create<fir::ConvertOp>(loc, intPtrTy, selectorTdescAddr); 450 return rewriter.create<mlir::arith::CmpIOp>( 451 loc, mlir::arith::CmpIPredicate::eq, typeDescInt, selectorTdescInt); 452 } 453 454 llvm::SmallSet<llvm::StringRef, 4> 455 SelectTypeConv::collectAncestors(fir::TypeInfoOp dt, mlir::ModuleOp mod) const { 456 llvm::SmallSet<llvm::StringRef, 4> ancestors; 457 while (auto parentName = dt.getIfParentName()) { 458 ancestors.insert(*parentName); 459 dt = mod.lookupSymbol<fir::TypeInfoOp>(*parentName); 460 assert(dt && "parent type info not generated"); 461 } 462 return ancestors; 463 } 464