[flang-commits] [flang] [draft][flang] Query backend support for quad-precision compilation decision (PR #182230)
Shunsuke Watanabe via flang-commits
flang-commits at lists.llvm.org
Tue Feb 24 01:15:31 PST 2026
================
@@ -58,17 +62,50 @@ namespace Fortran::tools {
break;
}
- // Check for kind=16 support. See flang/runtime/Float128Math/math-entries.h.
- // TODO: Take this from TargetInfo::getLongDoubleFormat for cross compilation.
-#ifdef FLANG_RUNTIME_F128_MATH_LIB
- constexpr bool f128Support = true; // use libquadmath wrappers
-#elif HAS_LDBL128
- constexpr bool f128Support = true; // use libm wrappers
-#else
- constexpr bool f128Support = false;
-#endif
-
- if constexpr (!f128Support) {
+ bool f128Support = false;
+ llvm::LLVMContext ctx;
+ std::unique_ptr<llvm::Module> dummyModule =
+ std::make_unique<llvm::Module>("quad-test", ctx);
+ dummyModule->setTargetTriple(targetMachine.getTargetTriple());
+ dummyModule->setDataLayout(targetMachine.createDataLayout());
+
+ llvm::FunctionType *dummyFTy =
+ llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), false);
+ llvm::Function *dummyF = llvm::Function::Create(dummyFTy,
+ llvm::GlobalValue::ExternalLinkage, "quad-test", dummyModule.get());
+
+ const llvm::TargetLowering *dummyTLI =
+ targetMachine.getSubtargetImpl(*dummyF)->getTargetLowering();
+
+ if (dummyTLI) {
+ llvm::EVT fp128EVT = llvm::EVT::getEVT(llvm::Type::getFP128Ty(ctx));
+
+ // Query for fp128 backend support. Based on this, determine whether
+ // compilation is possible on the frontend.
+ bool isLegal = dummyTLI->isTypeLegal(fp128EVT);
+
+ // We might also be able to determine fp128 backend support based on the
+ // LegalizeAction value. This is likely when the value is "Legal" or
+ // "LibCall". See
+ // https://llvm.org/doxygen/TargetLowering_8h_source.html#l00202.
+ llvm::TargetLowering::LegalizeAction LA =
+ dummyTLI->getOperationAction(llvm::ISD::FADD, fp128EVT);
+
+ // We might also be able to determine fp128 backend support based on the
+ // LegalizeTypeAction value. This is likely when the value is "TypeLegal".
+ // See https://llvm.org/doxygen/TargetLowering_8h_source.html#l00212.
+ llvm::TargetLowering::LegalizeTypeAction LTA =
+ dummyTLI->getTypeConversion(ctx, llvm::MVT::f128).first;
+
+ if (isLegal &&
+ (LA == llvm::TargetLowering::Legal ||
----------------
s-watanabe314 wrote:
In the [description](https://github.com/llvm/llvm-project/blob/530c938485d7c4119e8df3f57e288f9f20a183cd/llvm/include/llvm/CodeGen/TargetLowering.h#L203) of `LegalizeAction`, it states that the action is `Legal` if the type is natively supported. My initial thought was that it might be safer to explicitly ensure legality by checking both, even if `isLegal` implies it. However, as you mentioned, `isLegal == true` and `LegalizeAction == Legal` are overlapping, so checking both might be redundant. I will change the implementation to determine it solely based on `isLegal`.
https://github.com/llvm/llvm-project/pull/182230
More information about the flang-commits
mailing list