[llvm] 84616a3 - IR: Add Module::getLongDoubleFormat helper (#214943)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 03:01:06 PDT 2026


Author: Matt Arsenault
Date: 2026-08-11T12:01:00+02:00
New Revision: 84616a32ca1976a92d872b7237257a9f793e4151

URL: https://github.com/llvm/llvm-project/commit/84616a32ca1976a92d872b7237257a9f793e4151
DIFF: https://github.com/llvm/llvm-project/commit/84616a32ca1976a92d872b7237257a9f793e4151.diff

LOG: IR: Add Module::getLongDoubleFormat helper (#214943)

Add a helper function to return the effective long double type
for a module, accounting for the "long-double-type" module flag,
and defaulting to the triple's default.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>

Added: 
    

Modified: 
    clang/lib/CodeGen/CodeGenModule.cpp
    llvm/include/llvm/IR/Module.h
    llvm/lib/IR/Module.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 5ed5385c90f26..e17bd72c2f052 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1411,12 +1411,8 @@ void CodeGenModule::Release() {
     else if (flt == &llvm::APFloat::IEEEsingle())
       Format = llvm::LongDoubleFormat::IEEEsingle;
 
-    if (Format) {
-      getModule().addModuleFlag(
-          llvm::Module::Error, "long-double-type",
-          llvm::MDString::get(VMContext,
-                              llvm::getLongDoubleFormatName(*Format)));
-    }
+    if (Format)
+      getModule().setLongDoubleFormat(*Format);
   }
 
   if (getTriple().isOSzOS()) {

diff  --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 16dad6ad446fb..53927e96a232e 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -1049,6 +1049,18 @@ class LLVM_ABI Module {
   void setCodeModel(CodeModel::Model CL);
   /// @}
 
+  /// @}
+  /// @name Utility functions for querying the long double format
+  /// @{
+
+  /// Returns the long double format from the "long-double-type" module flag,
+  /// or the triple default when the flag is absent.
+  LongDoubleFormat getLongDoubleFormat() const;
+
+  /// Set the long double format.
+  void setLongDoubleFormat(LongDoubleFormat Format);
+  /// @}
+
   /// @}
   /// @name Utility function for querying the floating-point ABI
   /// @{

diff  --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index 3dcc9ec5dac3f..2ae76b80ccd57 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -682,6 +682,22 @@ void Module::setCodeModel(CodeModel::Model CL) {
   addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
 }
 
+LongDoubleFormat Module::getLongDoubleFormat() const {
+  if (auto *Val =
+          dyn_cast_or_null<MDString>(getModuleFlag("long-double-type"))) {
+    if (std::optional<LongDoubleFormat> Format =
+            parseLongDoubleFormat(Val->getString()))
+      return *Format;
+  }
+
+  return getTargetTriple().getDefaultLongDoubleFormat();
+}
+
+void Module::setLongDoubleFormat(LongDoubleFormat Format) {
+  addModuleFlag(ModFlagBehavior::Error, "long-double-type",
+                MDString::get(getContext(), getLongDoubleFormatName(Format)));
+}
+
 FloatABI::ABIType Module::getFloatABI() const {
   if (auto *Val = dyn_cast_or_null<MDString>(getModuleFlag("float-abi")))
     return FloatABI::parseABIType(Val->getString()).value_or(FloatABI::Default);


        


More information about the llvm-commits mailing list