Skip to content

[CIR] Refactor IntType constraints #138106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ def PtrStrideOp : CIR_Op<"ptr_stride",
```
}];

let arguments = (ins CIR_PointerType:$base, PrimitiveInt:$stride);
let arguments = (ins
CIR_PointerType:$base,
CIR_AnyFundamentalIntType:$stride
);

let results = (outs CIR_PointerType:$result);

let assemblyFormat = [{
Expand Down
113 changes: 113 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRTypeConstraints.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//===----------------------------------------------------------------------===//
//
// 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 defines the CIR dialect type constraints.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD
#define CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD

include "mlir/IR/Constraints.td"
include "mlir/IR/CommonTypeConstraints.td"

class CIR_IsTypePred<code type> : CPred<"::mlir::isa<" # type # ">($_self)">;

class CIR_TypeBase<code type, string summary = "">
: Type<CIR_IsTypePred<type>, summary, type>;

class CIR_CastSelfToType<code type, Pred pred>
: SubstLeaves<"$_self", "::mlir::cast<" # type # ">($_self)", pred>;

class CIR_CastedSelfsToType<code type, list<Pred> preds>
: And<!foreach(pred, preds, CIR_CastSelfToType<type, pred>)>;

class CIR_ConfinedType<Type type, list<Pred> preds, string summary = "">
: Type<And<[type.predicate, CIR_CastedSelfsToType<type.cppType, preds>]>,
summary, type.cppType>;

//===----------------------------------------------------------------------===//
// IntType predicates
//===----------------------------------------------------------------------===//

def CIR_AnyIntType : CIR_TypeBase<"::cir::IntType", "integer type">;

def CIR_AnyUIntType : CIR_ConfinedType<CIR_AnyIntType, [
CPred<"$_self.isUnsigned()">], "unsigned integer type">;

def CIR_AnySIntType : CIR_ConfinedType<CIR_AnyIntType, [
CPred<"$_self.isSigned()">], "signed integer type">;

class CIR_HasWidthPred<int width> : CPred<"$_self.getWidth() == " # width>;

def CIR_HasFundamentalIntWidthPred
: CPred<"::cir::isValidFundamentalIntWidth($_self.getWidth())">;

class CIR_IntOfWidthsPred<list<int> widths>
: Or<!foreach(width, widths, CIR_HasWidthPred<width>)>;

class CIR_IntOfWidths<list<int> widths>
: CIR_ConfinedType<CIR_AnyIntType, [CIR_IntOfWidthsPred<widths>],
"integer type of widths " # !interleave(widths, "/")>;

class CIR_SIntOfWidths<list<int> widths>
: CIR_ConfinedType<CIR_AnySIntType, [CIR_IntOfWidthsPred<widths>],
"signed integer type of widths " # !interleave(widths, "/")>;

class CIR_UIntOfWidths<list<int> widths>
: CIR_ConfinedType<CIR_AnyUIntType, [CIR_IntOfWidthsPred<widths>],
"unsigned integer type of widths " # !interleave(widths, "/")>;

class CIR_UInt<int width>
: CIR_ConfinedType<CIR_AnyUIntType, [CIR_HasWidthPred<width>],
width # "-bit unsigned integer">,
BuildableType<"$_builder.getType<" # cppType # ">(" #
width # ", /*isSigned=*/false)">;

def CIR_UInt1 : CIR_UInt<1>;
def CIR_UInt8 : CIR_UInt<8>;
def CIR_UInt16 : CIR_UInt<16>;
def CIR_UInt32 : CIR_UInt<32>;
def CIR_UInt64 : CIR_UInt<64>;
def CIR_UInt128 : CIR_UInt<128>;

class CIR_SInt<int width>
: CIR_ConfinedType<CIR_AnySIntType, [CIR_HasWidthPred<width>],
width # "-bit signed integer">,
BuildableType<"$_builder.getType<" # cppType # ">(" #
width # ", /*isSigned=*/true)">;

def CIR_SInt1 : CIR_SInt<1>;
def CIR_SInt8 : CIR_SInt<8>;
def CIR_SInt16 : CIR_SInt<16>;
def CIR_SInt32 : CIR_SInt<32>;
def CIR_SInt64 : CIR_SInt<64>;
def CIR_SInt128 : CIR_SInt<128>;

// Fundamental integer types represent standard source-level integer types that
// have a specified set of admissible bitwidths (8, 16, 32, 64).

def CIR_AnyFundamentalIntType
: CIR_ConfinedType<CIR_AnyIntType, [CIR_HasFundamentalIntWidthPred],
"fundamental integer type"> {
let cppFunctionName = "isFundamentalIntType";
}

def CIR_AnyFundamentalUIntType
: CIR_ConfinedType<CIR_AnyUIntType, [CIR_HasFundamentalIntWidthPred],
"fundamental unsigned integer type"> {
let cppFunctionName = "isFundamentalUIntType";
}

def CIR_AnyFundamentalSIntType
: CIR_ConfinedType<CIR_AnySIntType, [CIR_HasFundamentalIntWidthPred],
"fundamental signed integer type"> {
let cppFunctionName = "isFundamentalSIntType";
}

#endif // CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD
8 changes: 8 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace detail {
struct RecordTypeStorage;
} // namespace detail

bool isValidFundamentalIntWidth(unsigned width);

bool isAnyFloatingPointType(mlir::Type t);
bool isFPOrFPVectorTy(mlir::Type);

Expand All @@ -33,6 +35,12 @@ bool isFPOrFPVectorTy(mlir::Type);
// CIR Dialect Tablegen'd Types
//===----------------------------------------------------------------------===//

namespace cir {

#include "clang/CIR/Dialect/IR/CIRTypeConstraints.h.inc"

} // namespace cir

#define GET_TYPEDEF_CLASSES
#include "clang/CIR/Dialect/IR/CIROpsTypes.h.inc"

Expand Down
76 changes: 11 additions & 65 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define MLIR_CIR_DIALECT_CIR_TYPES

include "clang/CIR/Dialect/IR/CIRDialect.td"
include "clang/CIR/Dialect/IR/CIRTypeConstraints.td"
include "clang/CIR/Interfaces/CIRFPTypeInterface.td"
include "mlir/Interfaces/DataLayoutInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
Expand Down Expand Up @@ -41,7 +42,7 @@ def CIR_IntType : CIR_Type<"Int", "int",
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`,
fundamental integer types. Said types are: `signed char`, `short`, `int`,
`long`, `long long`, and their unsigned variations.
}];
let parameters = (ins "unsigned":$width, "bool":$isSigned);
Expand All @@ -55,81 +56,26 @@ def CIR_IntType : CIR_Type<"Int", "int",
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());
/// Return true if this is a fundamental integer type (i.e. signed or
/// unsigned integer types whose bit width is 8, 16, 32, or 64).
bool isFundamental() const {
return isFundamentalIntType(*this);
}
bool isSignedPrimitive() const {
return isPrimitive() && isSigned();
bool isSignedFundamental() const {
return isFundamentalSIntType(*this);
}
bool isUnsignedFundamental() const {
return isFundamentalUIntType(*this);
}

/// Returns a minimum bitwidth of cir::IntType
static unsigned minBitwidth() { return 1; }
/// Returns a maximum bitwidth of cir::IntType
static unsigned maxBitwidth() { return 128; }

/// Returns true if cir::IntType that represents a primitive integer type
/// can be constructed from the provided bitwidth.
static bool isValidPrimitiveIntBitwidth(unsigned width) {
return width == 8 || width == 16 || width == 32 || width == 64;
}
}];
let genVerifyDecl = 1;
}

// Constraints

// Unsigned integer type of a specific width.
class UInt<int width>
: Type<And<[
CPred<"::mlir::isa<::cir::IntType>($_self)">,
CPred<"::mlir::cast<::cir::IntType>($_self).isUnsigned()">,
CPred<"::mlir::cast<::cir::IntType>($_self).getWidth() == " # width>
]>, width # "-bit unsigned integer", "::cir::IntType">,
BuildableType<
"cir::IntType::get($_builder.getContext(), "
# width # ", /*isSigned=*/false)"> {
int bitwidth = width;
}

def UInt1 : UInt<1>;
def UInt8 : UInt<8>;
def UInt16 : UInt<16>;
def UInt32 : UInt<32>;
def UInt64 : UInt<64>;

// Signed integer type of a specific width.
class SInt<int width>
: Type<And<[
CPred<"::mlir::isa<::cir::IntType>($_self)">,
CPred<"::mlir::cast<::cir::IntType>($_self).isSigned()">,
CPred<"::mlir::cast<::cir::IntType>($_self).getWidth() == " # width>
]>, width # "-bit signed integer", "::cir::IntType">,
BuildableType<
"cir::IntType::get($_builder.getContext(), "
# width # ", /*isSigned=*/true)"> {
int bitwidth = width;
}

def SInt1 : SInt<1>;
def SInt8 : SInt<8>;
def SInt16 : SInt<16>;
def SInt32 : SInt<32>;
def SInt64 : SInt<64>;

def PrimitiveUInt
: AnyTypeOf<[UInt8, UInt16, UInt32, UInt64], "primitive unsigned int",
"::cir::IntType">;

def PrimitiveSInt
: AnyTypeOf<[SInt8, SInt16, SInt32, SInt64], "primitive signed int",
"::cir::IntType">;

def PrimitiveInt
: AnyTypeOf<[UInt8, UInt16, UInt32, UInt64, SInt8, SInt16, SInt32, SInt64],
"primitive int", "::cir::IntType">;

//===----------------------------------------------------------------------===//
// FloatType
//===----------------------------------------------------------------------===//
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ mlir_tablegen(CIROpsAttributes.cpp.inc -gen-attrdef-defs)
mlir_tablegen(CIROpsEnums.h.inc -gen-enum-decls)
mlir_tablegen(CIROpsEnums.cpp.inc -gen-enum-defs)
add_public_tablegen_target(MLIRCIREnumsGen)

set(LLVM_TARGET_DEFINITIONS CIRTypeConstraints.td)
mlir_tablegen(CIRTypeConstraints.h.inc -gen-type-constraint-decls)
mlir_tablegen(CIRTypeConstraints.cpp.inc -gen-type-constraint-defs)
add_public_tablegen_target(MLIRCIRTypeConstraintsIncGen)
add_dependencies(mlir-headers MLIRCIRTypeConstraintsIncGen)
10 changes: 10 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ static void printFuncTypeParams(mlir::AsmPrinter &p,
// Get autogenerated stuff
//===----------------------------------------------------------------------===//

namespace cir {

#include "clang/CIR/Dialect/IR/CIRTypeConstraints.cpp.inc"

} // namespace cir

#define GET_TYPEDEF_CLASSES
#include "clang/CIR/Dialect/IR/CIROpsTypes.cpp.inc"

Expand Down Expand Up @@ -424,6 +430,10 @@ IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
return mlir::success();
}

bool cir::isValidFundamentalIntWidth(unsigned width) {
return width == 8 || width == 16 || width == 32 || width == 64;
}

//===----------------------------------------------------------------------===//
// Floating-point type definitions
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/Dialect/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_clang_library(MLIRCIR

DEPENDS
MLIRCIROpsIncGen
MLIRCIRTypeConstraintsIncGen
MLIRCIREnumsGen
MLIRCIROpInterfacesIncGen
MLIRCIRLoopOpInterfaceIncGen
Expand Down
Loading