[llvm-branch-commits] [clang] clang: Emit "long-double-type" module flag generically (PR #210819)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Jul 20 14:47:35 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
Move emission of the "long-double-type" module flag out of PowerPC
and into generic code, so it describes the long double format for all
targets.
Co-authored-by: Claude (Claude-Opus-4.8) <noreply@<!-- -->anthropic.com>
---
Full diff: https://github.com/llvm/llvm-project/pull/210819.diff
4 Files Affected:
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+26)
- (modified) clang/lib/CodeGen/Targets/PPC.cpp (-23)
- (added) clang/test/CodeGen/long-double-type-module-flag.c (+23)
- (removed) clang/test/CodeGen/ppc64-long-double-type-attr.c (-17)
``````````diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 5f5fc4401bb4e..7942683a62fed 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -51,6 +51,7 @@
#include "clang/Lex/Preprocessor.h"
#include "llvm/ABI/IRTypeMapper.h"
#include "llvm/ABI/TargetInfo.h"
+#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
@@ -1359,6 +1360,31 @@ void CodeGenModule::Release() {
getModule().addModuleFlag(llvm::Module::Error, "wchar_size",
static_cast<uint32_t>(WCharWidth));
+ if (getTypes().isLongDoubleReferenced()) {
+ StringRef LongDoubleType;
+ switch (llvm::APFloat::SemanticsToEnum(getTarget().getLongDoubleFormat())) {
+ case llvm::APFloat::S_IEEEdouble:
+ LongDoubleType = "double";
+ break;
+ case llvm::APFloat::S_IEEEquad:
+ LongDoubleType = "fp128";
+ break;
+ case llvm::APFloat::S_x87DoubleExtended:
+ LongDoubleType = "x86_fp80";
+ break;
+ case llvm::APFloat::S_PPCDoubleDouble:
+ LongDoubleType = "ppc_fp128";
+ break;
+ default:
+ break;
+ }
+
+ if (!LongDoubleType.empty()) {
+ getModule().addModuleFlag(llvm::Module::Error, "long-double-type",
+ llvm::MDString::get(VMContext, LongDoubleType));
+ }
+ }
+
if (getTriple().isOSzOS()) {
getModule().addModuleFlag(llvm::Module::Warning,
"zos_product_major_version",
diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index 92d14261ae81f..febce86c43eb9 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -710,9 +710,6 @@ class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
llvm::Value *Address) const override;
- void emitTargetMetadata(CodeGen::CodeGenModule &CGM,
- const llvm::MapVector<GlobalDecl, StringRef>
- &MangledDeclNames) const override;
};
class PPC64TargetCodeGenInfo : public TargetCodeGenInfo {
@@ -1040,26 +1037,6 @@ PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
/*IsAIX*/ false);
}
-void PPC64_SVR4_TargetCodeGenInfo::emitTargetMetadata(
- CodeGen::CodeGenModule &CGM,
- const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {
- if (CGM.getTypes().isLongDoubleReferenced()) {
- llvm::LLVMContext &Ctx = CGM.getLLVMContext();
- const auto *flt = &CGM.getTarget().getLongDoubleFormat();
- StringRef Type;
- if (flt == &llvm::APFloat::PPCDoubleDouble())
- Type = "ppc_fp128";
- else if (flt == &llvm::APFloat::IEEEquad())
- Type = "fp128";
- else if (flt == &llvm::APFloat::IEEEdouble())
- Type = "double";
-
- if (!Type.empty())
- CGM.getModule().addModuleFlag(llvm::Module::Error, "long-double-type",
- llvm::MDString::get(Ctx, Type));
- }
-}
-
bool
PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
llvm::Value *Address) const {
diff --git a/clang/test/CodeGen/long-double-type-module-flag.c b/clang/test/CodeGen/long-double-type-module-flag.c
new file mode 100644
index 0000000000000..0266ca93ca5cf
--- /dev/null
+++ b/clang/test/CodeGen/long-double-type-module-flag.c
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefix=X86FP80
+// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefix=FP128
+// RUN: %clang_cc1 -triple arm-none-eabi %s -emit-llvm -o - | FileCheck %s --check-prefix=DOUBLE
+// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s --check-prefix=PPCFP128
+// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -mabi=ieeelongdouble -o - | FileCheck %s --check-prefix=FP128
+// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -mlong-double-64 -o - | FileCheck %s --check-prefix=DOUBLE
+
+// The flag is only emitted when long double is actually used.
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -DNOLDBL %s -emit-llvm -o - | FileCheck %s --check-prefix=NOLDBL
+
+#ifndef NOLDBL
+long double foo(long double a, long double b) {
+ return a + b;
+}
+#endif
+
+int bar() { return 1; }
+
+// X86FP80: ![[#]] = !{i32 1, !"long-double-type", !"x86_fp80"}
+// FP128: ![[#]] = !{i32 1, !"long-double-type", !"fp128"}
+// DOUBLE: ![[#]] = !{i32 1, !"long-double-type", !"double"}
+// PPCFP128: ![[#]] = !{i32 1, !"long-double-type", !"ppc_fp128"}
+// NOLDBL-NOT: !"long-double-type"
diff --git a/clang/test/CodeGen/ppc64-long-double-type-attr.c b/clang/test/CodeGen/ppc64-long-double-type-attr.c
deleted file mode 100644
index b6f3baab28f7d..0000000000000
--- a/clang/test/CodeGen/ppc64-long-double-type-attr.c
+++ /dev/null
@@ -1,17 +0,0 @@
-// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s
-// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -mabi=ieeelongdouble -o - | FileCheck %s --check-prefix=IEEE
-// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -mlong-double-64 -o - | FileCheck %s --check-prefix=LDBL64
-// RUN: %clang_cc1 -triple powerpc64le-unknown-linux-gnu %s -emit-llvm -DNOLDBL -o - | FileCheck %s --check-prefix=NOLDBL
-
-#ifndef NOLDBL
-long double foo(long double a, long double b) {
- return a + b;
-}
-#endif
-
-int bar() { return 1; }
-
-// CHECK: ![[#]] = !{i32 1, !"long-double-type", !"ppc_fp128"}
-// IEEE: ![[#]] = !{i32 1, !"long-double-type", !"fp128"}
-// LDBL64: ![[#]] = !{i32 1, !"long-double-type", !"double"}
-// NOLDBL-NOT: ![[#]] = !{i32 1, !"long-double-type"
``````````
</details>
https://github.com/llvm/llvm-project/pull/210819
More information about the llvm-branch-commits
mailing list