Skip to content

Commit 5a767e7

Browse files
mvvsmkxlauko
authored andcommitted
[CIR][CIRGen][Builtin][Type] Support for IEEE Quad (long double) added (in CIR + Direct to LLVM) (llvm#966)
Fixes llvm/clangir#931 Added type definition in CIRTypes.td, created appropriate functions for the same in CIRTypes.cpp like getPreferredAlignment, getPreferredAlignment, etc. Optionally added lowering in LowerToLLVM.cpp
1 parent b420818 commit 5a767e7

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ def CIR_FP80 : CIR_FloatType<"FP80", "f80"> {
173173
}];
174174
}
175175

176+
def CIR_FP128 : CIR_FloatType<"FP128", "f128"> {
177+
let summary = "CIR type that represents IEEEquad 128-bit floating-point format";
178+
let description = [{
179+
Floating-point type that represents the IEEEquad 128-bit floating-point format.
180+
}];
181+
}
182+
176183
def CIR_LongDouble : CIR_FloatType<"LongDouble", "long_double"> {
177184
let summary = "CIR extended-precision float type";
178185
let description = [{
@@ -195,7 +202,7 @@ def CIR_LongDouble : CIR_FloatType<"LongDouble", "long_double"> {
195202

196203
// Constraints
197204

198-
def CIR_AnyFloat: AnyTypeOf<[CIR_Single, CIR_Double, CIR_FP80, CIR_LongDouble]>;
205+
def CIR_AnyFloat: AnyTypeOf<[CIR_Single, CIR_Double, CIR_FP80, CIR_FP128, CIR_LongDouble]>;
199206
def CIR_AnyIntOrFloat: AnyTypeOf<[CIR_AnyFloat, CIR_IntType]>;
200207

201208
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
402402
if (&format == &llvm::APFloat::x87DoubleExtended())
403403
return mlir::cir::LongDoubleType::get(getContext(), typeCache.FP80Ty);
404404
if (&format == &llvm::APFloat::IEEEquad())
405-
llvm_unreachable("NYI");
405+
return mlir::cir::LongDoubleType::get(getContext(), typeCache.FP128Ty);
406406
if (&format == &llvm::APFloat::PPCDoubleDouble())
407407
llvm_unreachable("NYI");
408408

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "mlir/IR/Verifier.h"
3636
#include "clang/AST/Expr.h"
3737
#include "clang/Basic/Cuda.h"
38+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
3839
#include "clang/CIR/MissingFeatures.h"
3940

4041
#include "clang/AST/ASTConsumer.h"
@@ -74,8 +75,8 @@
7475
#include "llvm/Support/Casting.h"
7576
#include "llvm/Support/ErrorHandling.h"
7677
#include "llvm/Support/FileSystem.h"
77-
#include "llvm/Support/raw_ostream.h"
7878
#include "llvm/Support/TimeProfiler.h"
79+
#include "llvm/Support/raw_ostream.h"
7980

8081
#include <iterator>
8182
#include <numeric>
@@ -145,6 +146,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
145146
FloatTy = ::mlir::cir::SingleType::get(builder.getContext());
146147
DoubleTy = ::mlir::cir::DoubleType::get(builder.getContext());
147148
FP80Ty = ::mlir::cir::FP80Type::get(builder.getContext());
149+
FP128Ty = ::mlir::cir::FP128Type::get(builder.getContext());
148150

149151
// TODO: PointerWidthInBits
150152
PointerAlignInBytes =
@@ -196,8 +198,7 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
196198
theModule->setAttr("cir.sob",
197199
mlir::cir::SignedOverflowBehaviorAttr::get(&context, sob));
198200
auto lang = SourceLanguageAttr::get(&context, getCIRSourceLanguage());
199-
theModule->setAttr(
200-
"cir.lang", mlir::cir::LangAttr::get(&context, lang));
201+
theModule->setAttr("cir.lang", mlir::cir::LangAttr::get(&context, lang));
201202
theModule->setAttr("cir.triple", builder.getStringAttr(getTriple().str()));
202203
// Set the module name to be the name of the main file. TranslationUnitDecl
203204
// often contains invalid source locations and isn't a reliable source for the

clang/lib/CIR/CodeGen/CIRGenTypeCache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "mlir/IR/Types.h"
1818
#include "clang/AST/CharUnits.h"
1919
#include "clang/Basic/AddressSpaces.h"
20-
#include "clang/CIR/Dialect/IR/CIRTypes.h"
2120
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
21+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
2222
#include "clang/CIR/MissingFeatures.h"
2323

2424
namespace cir {
@@ -41,6 +41,7 @@ struct CIRGenTypeCache {
4141
mlir::cir::SingleType FloatTy;
4242
mlir::cir::DoubleType DoubleTy;
4343
mlir::cir::FP80Type FP80Ty;
44+
mlir::cir::FP128Type FP128Ty;
4445

4546
/// int
4647
mlir::Type UIntTy;

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,27 @@ FP80Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
763763
return 16;
764764
}
765765

766+
const llvm::fltSemantics &FP128Type::getFloatSemantics() const {
767+
return llvm::APFloat::IEEEquad();
768+
}
769+
770+
llvm::TypeSize
771+
FP128Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
772+
mlir::DataLayoutEntryListRef params) const {
773+
return llvm::TypeSize::getFixed(16);
774+
}
775+
776+
uint64_t FP128Type::getABIAlignment(const mlir::DataLayout &dataLayout,
777+
mlir::DataLayoutEntryListRef params) const {
778+
return 16;
779+
}
780+
781+
uint64_t
782+
FP128Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
783+
::mlir::DataLayoutEntryListRef params) const {
784+
return 16;
785+
}
786+
766787
const llvm::fltSemantics &LongDoubleType::getFloatSemantics() const {
767788
return mlir::cast<mlir::cir::CIRFPTypeInterface>(getUnderlying())
768789
.getFloatSemantics();
@@ -792,7 +813,7 @@ uint64_t LongDoubleType::getPreferredAlignment(
792813
LogicalResult
793814
LongDoubleType::verify(function_ref<InFlightDiagnostic()> emitError,
794815
mlir::Type underlying) {
795-
if (!mlir::isa<DoubleType, FP80Type>(underlying)) {
816+
if (!mlir::isa<DoubleType, FP80Type, FP128Type>(underlying)) {
796817
emitError() << "invalid underlying type for long double";
797818
return failure();
798819
}

0 commit comments

Comments
 (0)