[clang] [CIR] Integral types; simple global variables (PR #118743)

David Olsen via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 5 13:34:46 PST 2024


================
@@ -0,0 +1,132 @@
+//===- CIRTypes.td - CIR dialect types ---------------------*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the CIR dialect types.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CIR_DIALECT_CIR_TYPES
+#define MLIR_CIR_DIALECT_CIR_TYPES
+
+include "clang/CIR/Dialect/IR/CIRDialect.td"
+include "mlir/Interfaces/DataLayoutInterfaces.td"
+include "mlir/IR/AttrTypeBase.td"
+
+//===----------------------------------------------------------------------===//
+// CIR Types
+//===----------------------------------------------------------------------===//
+
+class CIR_Type<string name, string typeMnemonic, list<Trait> traits = [],
+               string baseCppClass = "::mlir::Type">
+    : TypeDef<CIR_Dialect, name, traits, baseCppClass> {
+  let mnemonic = typeMnemonic;
+}
+
+//===----------------------------------------------------------------------===//
+// IntType
+//===----------------------------------------------------------------------===//
+
+def CIR_IntType : CIR_Type<"Int", "int",
+    [DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
+  let summary = "Integer type with arbitrary precision up to a fixed limit";
+  let description = [{
+    CIR type that represents integer types with arbitrary precision, including
+    standard integral types such as `int` and `long`, extended integral types
+    such as `__int128`, and arbitrary width types such as `_BitInt(n)`.
+
+    Those integer types that are directly available in C/C++ standard are called
+    primitive integer types. Said types are: `signed char`, `short`, `int`,
+    `long`, `long long`, and their unsigned variations.
+  }];
+  let parameters = (ins "unsigned":$width, "bool":$isSigned);
+  let hasCustomAssemblyFormat = 1;
+  let extraClassDeclaration = [{
+    /// Return true if this is a signed integer type.
+    bool isSigned() const { return getIsSigned(); }
+    /// Return true if this is an unsigned integer type.
+    bool isUnsigned() const { return !getIsSigned(); }
+    /// Return type alias.
+    std::string getAlias() const {
+      return (isSigned() ? 's' : 'u') + std::to_string(getWidth()) + 'i';
+    }
+    /// Return true if this is a primitive integer type (i.e. signed or unsigned
+    /// integer types whose bit width is 8, 16, 32, or 64).
+    bool isPrimitive() const {
+      return isValidPrimitiveIntBitwidth(getWidth());
+    }
+    bool isSignedPrimitive() const {
+      return isPrimitive() && isSigned();
+    }
+
+    /// Returns a minimum bitwidth of cir::IntType
+    static unsigned minBitwidth() { return 1; }
+    /// Returns a maximum bitwidth of cir::IntType
+    static unsigned maxBitwidth() { return 128; }
----------------
dkolsen-pgi wrote:

The ClangIR incubator project currently doesn't handle `_BitInt` types larger than 128 bits.  In my most recent commit I changed the code to gracefully report an error if the `_BitInt` is too large.  I opened https://github.com/llvm/clangir/issues/1212 against ClangIR to add support for larger `_BitInt` types.


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


More information about the cfe-commits mailing list