[clang] 3e97db8 - [PowerPC] Emit IR module flag for current float abi
Qiu Chaofan via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 25 02:59:35 PDT 2023
Author: Qiu Chaofan
Date: 2023-09-25T17:53:39+08:00
New Revision: 3e97db89ae8e5b4d7bb6c0bf52773c43c9e06c51
URL: https://github.com/llvm/llvm-project/commit/3e97db89ae8e5b4d7bb6c0bf52773c43c9e06c51
DIFF: https://github.com/llvm/llvm-project/commit/3e97db89ae8e5b4d7bb6c0bf52773c43c9e06c51.diff
LOG: [PowerPC] Emit IR module flag for current float abi
This is part of the efforts adding .gnu_attribute support for PowerPC.
In Clang, an extra metadata field will be added as float-abi to show
current long double format. So backend can emit .gnu_attribute section
data from this metadata.
To avoid breaking existing behavior, the module metadata will only be
emitted when this module makes use of long double.
Reviewed By: nemanjai
Differential Revision: https://reviews.llvm.org/D116016
Added:
clang/test/CodeGen/ppc64-float-abi-attr.c
Modified:
clang/lib/CodeGen/CodeGenTypes.cpp
clang/lib/CodeGen/CodeGenTypes.h
clang/lib/CodeGen/Targets/PPC.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp b/clang/lib/CodeGen/CodeGenTypes.cpp
index 30021794a0bb3d3..29f4f51dc08b5ec 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -34,6 +34,7 @@ CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
SkippedLayout = false;
+ LongDoubleReferenced = false;
}
CodeGenTypes::~CodeGenTypes() {
@@ -406,10 +407,12 @@ llvm::Type *CodeGenTypes::ConvertType(QualType T) {
Context.getLangOpts().NativeHalfType ||
!Context.getTargetInfo().useFP16ConversionIntrinsics());
break;
+ case BuiltinType::LongDouble:
+ LongDoubleReferenced = true;
+ LLVM_FALLTHROUGH;
case BuiltinType::BFloat16:
case BuiltinType::Float:
case BuiltinType::Double:
- case BuiltinType::LongDouble:
case BuiltinType::Float128:
case BuiltinType::Ibm128:
ResultType = getTypeForFormat(getLLVMContext(),
diff --git a/clang/lib/CodeGen/CodeGenTypes.h b/clang/lib/CodeGen/CodeGenTypes.h
index a0e846d9a7516cd..01c0c673795c0fa 100644
--- a/clang/lib/CodeGen/CodeGenTypes.h
+++ b/clang/lib/CodeGen/CodeGenTypes.h
@@ -84,6 +84,9 @@ class CodeGenTypes {
/// a recursive struct conversion, set this to true.
bool SkippedLayout;
+ /// True if any instance of long double types are used.
+ bool LongDoubleReferenced;
+
/// This map keeps cache of llvm::Types and maps clang::Type to
/// corresponding llvm::Type.
llvm::DenseMap<const Type *, llvm::Type *> TypeCache;
@@ -289,6 +292,7 @@ class CodeGenTypes {
/// zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
bool isZeroInitializable(const RecordDecl *RD);
+ bool isLongDoubleReferenced() const { return LongDoubleReferenced; }
bool isRecordLayoutComplete(const Type *Ty) const;
unsigned getTargetAddressSpace(QualType T) const;
};
diff --git a/clang/lib/CodeGen/Targets/PPC.cpp b/clang/lib/CodeGen/Targets/PPC.cpp
index 6f4c150ef3149e8..40dddde508c1772 100644
--- a/clang/lib/CodeGen/Targets/PPC.cpp
+++ b/clang/lib/CodeGen/Targets/PPC.cpp
@@ -620,6 +620,9 @@ 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 {
@@ -940,6 +943,24 @@ 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();
+ if (flt == &llvm::APFloat::PPCDoubleDouble())
+ CGM.getModule().addModuleFlag(llvm::Module::Error, "float-abi",
+ llvm::MDString::get(Ctx, "doubledouble"));
+ else if (flt == &llvm::APFloat::IEEEquad())
+ CGM.getModule().addModuleFlag(llvm::Module::Error, "float-abi",
+ llvm::MDString::get(Ctx, "ieeequad"));
+ else if (flt == &llvm::APFloat::IEEEdouble())
+ CGM.getModule().addModuleFlag(llvm::Module::Error, "float-abi",
+ llvm::MDString::get(Ctx, "ieeedouble"));
+ }
+}
+
bool
PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
llvm::Value *Address) const {
diff --git a/clang/test/CodeGen/ppc64-float-abi-attr.c b/clang/test/CodeGen/ppc64-float-abi-attr.c
new file mode 100644
index 000000000000000..8e0eca17b9b6cf7
--- /dev/null
+++ b/clang/test/CodeGen/ppc64-float-abi-attr.c
@@ -0,0 +1,17 @@
+// 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, !"float-abi", !"doubledouble"}
+// IEEE: ![[#]] = !{i32 1, !"float-abi", !"ieeequad"}
+// LDBL64: ![[#]] = !{i32 1, !"float-abi", !"ieeedouble"}
+// NOLDBL-NOT: ![[#]] = !{i32 1, !"float-abi"
More information about the cfe-commits
mailing list