Skip to content

Commit 523b445

Browse files
Lancernlanza
authored andcommitted
[CIR] Fix int constant type verification (llvm#386)
When introducing attribute `#cir.int`, the constant type verification is not updated. If a `cir.const` operation produces an integral constant from a `#cir.int` attribute, the integer's type is not verified: ```mlir %1 = cir.const(#cir.int<0> : !cir.int<s, 8>) : !cir.int<u, 8> // Not verified: !cir.int<s, 8> differs from !cir.int<u, 8> ``` The corresponding test is also wrong but fail to be detected. This patch fixes this issue.
1 parent 160aeb9 commit 523b445

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
275275
return success();
276276
}
277277

278-
if (attrType.isa<IntegerAttr, FloatAttr>()) {
278+
if (attrType.isa<mlir::cir::IntAttr, FloatAttr>()) {
279279
auto at = attrType.cast<TypedAttr>();
280280
if (at.getType() != opType) {
281281
return op->emitOpError("result type (")

clang/test/CIR/IR/int.cir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
!s32i = !cir.int<s, 32>
99
!s64i = !cir.int<s, 64>
1010

11-
!u8i = !cir.int<s, 8>
12-
!u16i = !cir.int<s, 16>
13-
!u32i = !cir.int<s, 32>
14-
!u64i = !cir.int<s, 64>
11+
!u8i = !cir.int<u, 8>
12+
!u16i = !cir.int<u, 16>
13+
!u32i = !cir.int<u, 32>
14+
!u64i = !cir.int<u, 64>
1515

1616
cir.func @validIntTypesAndAttributes() -> () {
1717

clang/test/CIR/IR/invalid.cir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,3 +781,12 @@ module {
781781
}
782782
}
783783

784+
// -----
785+
786+
!s8i = !cir.int<s, 8>
787+
!u8i = !cir.int<u, 8>
788+
cir.func @const_type_mismatch() -> () {
789+
// expected-error@+1 {{'cir.const' op result type ('!cir.int<u, 8>') does not match value type ('!cir.int<s, 8>')}}
790+
%2 = cir.const(#cir.int<0> : !s8i) : !u8i
791+
cir.return
792+
}

0 commit comments

Comments
 (0)