Skip to content

Commit

Permalink
Update type constraints and split out standard integer type requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancern committed Apr 11, 2024
1 parent 032f41f commit 2397004
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 37 deletions.
26 changes: 13 additions & 13 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def ObjSizeOp : CIR_Op<"objsize", [Pure]> {

let arguments = (ins CIR_PointerType:$ptr, SizeInfoType:$kind,
UnitAttr:$dynamic);
let results = (outs CIR_IntType:$result);
let results = (outs StdInt:$result);

let assemblyFormat = [{
`(`
Expand Down Expand Up @@ -180,7 +180,7 @@ def PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
```
}];

let results = (outs CIR_IntType:$result);
let results = (outs StdInt:$result);
let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs);

let assemblyFormat = [{
Expand Down Expand Up @@ -208,7 +208,7 @@ def PtrStrideOp : CIR_Op<"ptr_stride",
```
}];

let arguments = (ins CIR_PointerType:$base, CIR_IntType:$stride);
let arguments = (ins CIR_PointerType:$base, StdInt:$stride);
let results = (outs CIR_PointerType:$result);

let assemblyFormat = [{
Expand Down Expand Up @@ -318,7 +318,7 @@ def AllocaOp : CIR_Op<"alloca", [
}];

let arguments = (ins
Optional<CIR_IntType>:$dynAllocSize,
Optional<StdInt>:$dynAllocSize,
TypeAttr:$allocaType,
StrAttr:$name,
UnitAttr:$init,
Expand Down Expand Up @@ -1227,7 +1227,7 @@ def CmpThreeWayOp : CIR_Op<"cmp3way", [Pure, SameTypeOperands]> {
```
}];

let results = (outs CIR_IntType:$result);
let results = (outs StdSInt:$result);
let arguments = (ins CIR_AnyType:$lhs, CIR_AnyType:$rhs,
CmpThreeWayInfoAttr:$info);

Expand All @@ -1236,7 +1236,7 @@ def CmpThreeWayOp : CIR_Op<"cmp3way", [Pure, SameTypeOperands]> {
`:` type($result) attr-dict
}];

let hasVerifier = 1;
let hasVerifier = 0;
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2097,7 +2097,7 @@ def VecInsertOp : CIR_Op<"vec.insert", [Pure,
element is returned.
}];

let arguments = (ins CIR_VectorType:$vec, AnyType:$value, CIR_IntType:$index);
let arguments = (ins CIR_VectorType:$vec, AnyType:$value, StdInt:$index);
let results = (outs CIR_VectorType:$result);

let assemblyFormat = [{
Expand All @@ -2122,7 +2122,7 @@ def VecExtractOp : CIR_Op<"vec.extract", [Pure,
from a vector object.
}];

let arguments = (ins CIR_VectorType:$vec, CIR_IntType:$index);
let arguments = (ins CIR_VectorType:$vec, StdInt:$index);
let results = (outs CIR_AnyType:$result);

let assemblyFormat = [{
Expand Down Expand Up @@ -2903,7 +2903,7 @@ def CopyOp : CIR_Op<"copy", [SameTypeOperands]> {
def MemCpyOp : CIR_Op<"libc.memcpy"> {
let arguments = (ins Arg<CIR_PointerType, "", [MemWrite]>:$dst,
Arg<CIR_PointerType, "", [MemRead]>:$src,
CIR_IntType:$len);
StdInt:$len);
let summary = "Equivalent to libc's `memcpy`";
let description = [{
Given two CIR pointers, `src` and `dst`, `cir.libc.memcpy` will copy `len`
Expand Down Expand Up @@ -3083,10 +3083,10 @@ def ExpectOp : CIR_Op<"expect",
where probability = $prob.
}];

let arguments = (ins CIR_IntType:$val,
CIR_IntType:$expected,
let arguments = (ins StdInt:$val,
StdInt:$expected,
OptionalAttr<F64Attr>:$prob);
let results = (outs CIR_IntType:$result);
let results = (outs StdInt:$result);
let assemblyFormat = [{
`(` $val`,` $expected (`,` $prob^)? `)` `:` type($val) attr-dict
}];
Expand Down Expand Up @@ -3476,7 +3476,7 @@ def AtomicAddFetch : CIR_Op<"atomic.add_fetch",
let summary = "Represents the __atomic_add_fetch builtin";
let description = [{}];
let results = (outs CIR_AnyIntOrFloat:$result);
let arguments = (ins IntOrFPPtr:$ptr, CIR_AnyIntOrFloat:$val,
let arguments = (ins StdIntOrFPPtr:$ptr, CIR_AnyIntOrFloat:$val,
Arg<MemOrder, "memory order">:$mem_order,
UnitAttr:$is_volatile);

Expand Down
39 changes: 33 additions & 6 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ 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 standard integer type (i.e. not a bit-precise
/// integer type).
bool isStandard() const {
return isValidStdIntBitwidth(getWidth());
}

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

/// Returns true if cir::IntType can be constructed from the provided bitwidth
static bool isValidBitwidth(unsigned width) {
return width >= minBitwidth()
&& width <= maxBitwidth()
&& llvm::isPowerOf2_32(width);
/// Returns true if cir::IntType that represents a standard integer type
/// can be constructed from the provided bitwidth.
static bool isValidStdIntBitwidth(unsigned width) {
return width == 8 || width == 16 || width == 32 || width == 64;
}
}];
let genVerifyDecl = 1;
Expand Down Expand Up @@ -139,6 +143,26 @@ class SIntOfWidths<list<int> widths>
"::mlir::cir::IntType"
> {}

// A type constraint that allows integer type whose width is among the specified
// list of possible widths. The signedness of the integer type is not
// constrained.
class IntOfWidths<list<int> widths>
: Type<And<[
CPred<"$_self.isa<::mlir::cir::IntType>()">,
Or<!foreach(
w, widths,
CPred<"$_self.cast<::mlir::cir::IntType>().getWidth() == " # w>
)>
]>,
!interleave(!foreach(w, widths, w # "-bit"), " or ") #
" signed or unsigned int",
"::mlir::cir::IntType"
> {}

def StdUInt : UIntOfWidths<[8, 16, 32, 64]>;
def StdSInt : SIntOfWidths<[8, 16, 32, 64]>;
def StdInt : IntOfWidths<[8, 16, 32, 64]>;

//===----------------------------------------------------------------------===//
// FloatType
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -375,7 +399,7 @@ def VoidPtr : Type<
}

// Pointer to int, float or double
def IntOrFPPtr : Type<
def StdIntOrFPPtr : Type<
And<[
CPred<"$_self.isa<::mlir::cir::PointerType>()">,
CPred<"$_self.cast<::mlir::cir::PointerType>()"
Expand Down Expand Up @@ -429,6 +453,9 @@ def IntegerVector : Type<
CPred<"$_self.isa<::mlir::cir::VectorType>()">,
CPred<"$_self.cast<::mlir::cir::VectorType>()"
".getEltType().isa<::mlir::cir::IntType>()">,
CPred<"$_self.cast<::mlir::cir::VectorType>()"
".getEltType().cast<::mlir::cir::IntType>()"
".isStandard()">
]>, "!cir.vector of !cir.int"> {
}

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
case 64:
return getUInt64Ty();
default:
llvm_unreachable("Unknown bit-width");
return mlir::cir::IntType::get(getContext(), N, false);
}
}

Expand All @@ -343,7 +343,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
case 64:
return getSInt64Ty();
default:
llvm_unreachable("Unknown bit-width");
return mlir::cir::IntType::get(getContext(), N, true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ struct CIRRecordLowering final {
// structures support.
mlir::Type getBitfieldStorageType(unsigned numBits) {
unsigned alignedBits = llvm::alignTo(numBits, astContext.getCharWidth());
if (mlir::cir::IntType::isValidBitwidth(alignedBits)) {
if (mlir::cir::IntType::isValidStdIntBitwidth(alignedBits)) {
return builder.getUIntNTy(alignedBits);
} else {
mlir::Type type = getCharType();
Expand Down
14 changes: 0 additions & 14 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,20 +944,6 @@ Block *BrCondOp::getSuccessorForOperands(ArrayRef<Attribute> operands) {
return nullptr;
}

//===----------------------------------------------------------------------===//
// CmpThreeWayOp
//===----------------------------------------------------------------------===//

LogicalResult CmpThreeWayOp::verify() {
// Type of the result must be a signed integer type.
if (!getType().isSigned()) {
emitOpError() << "result type of cir.cmp3way must be a signed integer type";
return failure();
}

return success();
}

//===----------------------------------------------------------------------===//
// SwitchOp
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/IR/invalid.cir
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ module {
!s32i = !cir.int<s, 32>
module {
cir.func @tmp(%arg0: !cir.float) {
// expected-error@+1 {{operand #0 must be Integer type}}
// expected-error@+1 {{operand #0 must be 8-bit or 16-bit or 32-bit or 64-bit signed or unsigned int}}
%0 = cir.alloca !s32i, cir.ptr <!s32i>, %arg0 : !cir.float, ["tmp"]
cir.return
}
Expand Down

0 comments on commit 2397004

Please sign in to comment.