[clang] [CIR] floating-point, pointer, and function types (PR #120484)

David Olsen via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 14:01:42 PST 2024


================
@@ -133,6 +143,276 @@ IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
   return mlir::success();
 }
 
+//===----------------------------------------------------------------------===//
+// Floating-point type definitions
+//===----------------------------------------------------------------------===//
+
+const llvm::fltSemantics &SingleType::getFloatSemantics() const {
+  return llvm::APFloat::IEEEsingle();
+}
+
+llvm::TypeSize
+SingleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                              mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getFixed(getWidth());
+}
+
+uint64_t
+SingleType::getABIAlignment(const mlir::DataLayout &dataLayout,
+                            mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+uint64_t
+SingleType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
+                                  ::mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+const llvm::fltSemantics &DoubleType::getFloatSemantics() const {
+  return llvm::APFloat::IEEEdouble();
+}
+
+llvm::TypeSize
+DoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                              mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getFixed(getWidth());
+}
+
+uint64_t
+DoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
+                            mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+uint64_t
+DoubleType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
+                                  ::mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+const llvm::fltSemantics &FP16Type::getFloatSemantics() const {
+  return llvm::APFloat::IEEEhalf();
+}
+
+llvm::TypeSize
+FP16Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                            mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getFixed(getWidth());
+}
+
+uint64_t FP16Type::getABIAlignment(const mlir::DataLayout &dataLayout,
+                                   mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+uint64_t
+FP16Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
+                                ::mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+const llvm::fltSemantics &BF16Type::getFloatSemantics() const {
+  return llvm::APFloat::BFloat();
+}
+
+llvm::TypeSize
+BF16Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                            mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getFixed(getWidth());
+}
+
+uint64_t BF16Type::getABIAlignment(const mlir::DataLayout &dataLayout,
+                                   mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+uint64_t
+BF16Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
+                                ::mlir::DataLayoutEntryListRef params) const {
+  return (uint64_t)(getWidth() / 8);
+}
+
+const llvm::fltSemantics &FP80Type::getFloatSemantics() const {
+  return llvm::APFloat::x87DoubleExtended();
+}
+
+llvm::TypeSize
+FP80Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                            mlir::DataLayoutEntryListRef params) const {
+  // Though only 80 bits are used for the value, the type is 128 bits in size.
+  return llvm::TypeSize::getFixed(128);
+}
+
+uint64_t FP80Type::getABIAlignment(const mlir::DataLayout &dataLayout,
+                                   mlir::DataLayoutEntryListRef params) const {
+  return 16;
+}
+
+uint64_t
+FP80Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
+                                ::mlir::DataLayoutEntryListRef params) const {
+  return 16;
+}
+
+const llvm::fltSemantics &FP128Type::getFloatSemantics() const {
+  return llvm::APFloat::IEEEquad();
+}
+
+llvm::TypeSize
+FP128Type::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                             mlir::DataLayoutEntryListRef params) const {
+  return llvm::TypeSize::getFixed(getWidth());
+}
+
+uint64_t FP128Type::getABIAlignment(const mlir::DataLayout &dataLayout,
+                                    mlir::DataLayoutEntryListRef params) const {
+  return 16;
+}
+
+uint64_t
+FP128Type::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
+                                 ::mlir::DataLayoutEntryListRef params) const {
+  return 16;
+}
+
+const llvm::fltSemantics &LongDoubleType::getFloatSemantics() const {
+  return mlir::cast<cir::CIRFPTypeInterface>(getUnderlying())
+      .getFloatSemantics();
+}
+
+llvm::TypeSize
+LongDoubleType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
+                                  mlir::DataLayoutEntryListRef params) const {
+  return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
+      .getTypeSizeInBits(dataLayout, params);
+}
+
+uint64_t
+LongDoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
+                                mlir::DataLayoutEntryListRef params) const {
+  return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
+      .getABIAlignment(dataLayout, params);
+}
+
+uint64_t LongDoubleType::getPreferredAlignment(
+    const ::mlir::DataLayout &dataLayout,
+    mlir::DataLayoutEntryListRef params) const {
+  return mlir::cast<mlir::DataLayoutTypeInterface>(getUnderlying())
+      .getPreferredAlignment(dataLayout, params);
+}
+
+LogicalResult
+LongDoubleType::verify(function_ref<InFlightDiagnostic()> emitError,
+                       mlir::Type underlying) {
+  if (!mlir::isa<DoubleType, FP80Type, FP128Type>(underlying)) {
+    emitError() << "invalid underlying type for long double";
+    return failure();
+  }
+
+  return success();
+}
+
+//===----------------------------------------------------------------------===//
+// Floating-point type helpers
+//===----------------------------------------------------------------------===//
+
+bool cir::isAnyFloatingPointType(mlir::Type t) {
+  return isa<cir::SingleType, cir::DoubleType, cir::LongDoubleType,
+             cir::FP80Type, cir::BF16Type, cir::FP16Type, cir::FP128Type>(t);
+}
+
+//===----------------------------------------------------------------------===//
+// FuncType Definitions
+//===----------------------------------------------------------------------===//
+
+FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
+  assert(results.size() == 1 && "expected exactly one result type");
+  return get(llvm::to_vector(inputs), results[0], isVarArg());
+}
+
+mlir::ParseResult parseFuncTypeArgs(mlir::AsmParser &p,
+                                    llvm::SmallVector<mlir::Type> &params,
+                                    bool &isVarArg) {
+  isVarArg = false;
+  // `(` `)`
+  if (succeeded(p.parseOptionalRParen()))
+    return mlir::success();
+
+  // `(` `...` `)`
+  if (succeeded(p.parseOptionalEllipsis())) {
+    isVarArg = true;
+    return p.parseRParen();
+  }
+
+  // type (`,` type)* (`,` `...`)?
+  mlir::Type type;
+  if (p.parseType(type))
+    return mlir::failure();
+  params.push_back(type);
+  while (succeeded(p.parseOptionalComma())) {
+    if (succeeded(p.parseOptionalEllipsis())) {
+      isVarArg = true;
+      return p.parseRParen();
+    }
+    if (p.parseType(type))
+      return mlir::failure();
+    params.push_back(type);
+  }
+
+  return p.parseRParen();
+}
+
+void printFuncTypeArgs(mlir::AsmPrinter &p, mlir::ArrayRef<mlir::Type> params,
+                       bool isVarArg) {
+  llvm::interleaveComma(params, p,
+                        [&p](mlir::Type type) { p.printType(type); });
+  if (isVarArg) {
+    if (!params.empty())
+      p << ", ";
+    p << "...";
+  }
+  p << ')';
+}
+
+llvm::ArrayRef<mlir::Type> FuncType::getReturnTypes() const {
+  return static_cast<detail::FuncTypeStorage *>(getImpl())->returnType;
+}
+
+bool FuncType::isVoid() const { return mlir::isa<VoidType>(getReturnType()); }
+
+//===----------------------------------------------------------------------===//
+// PointerType Definitions
+//===----------------------------------------------------------------------===//
+
+llvm::TypeSize
+PointerType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
+                               ::mlir::DataLayoutEntryListRef params) const {
+  // FIXME: improve this in face of address spaces
----------------
dkolsen-pgi wrote:

You are right that a MissingFeature assert would be appropriate here.  The MissingFeature asserts are not in upstream yet.  I haven't felt an urge to migrate them yet because I find them confusing, and have to look at the code a couple times to understand what is happening.  In a (hopefully not-too-distant) future commit I will look at the MissingFeature asserts in the incubator and put something similar in upstream.  But not in this commit.


https://github.com/llvm/llvm-project/pull/120484


More information about the cfe-commits mailing list