Skip to content

Commit

Permalink
[CIR] Fix int constant type verification (llvm#386)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lancern authored and lanza committed Apr 17, 2024
1 parent 160aeb9 commit 523b445
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
return success();
}

if (attrType.isa<IntegerAttr, FloatAttr>()) {
if (attrType.isa<mlir::cir::IntAttr, FloatAttr>()) {
auto at = attrType.cast<TypedAttr>();
if (at.getType() != opType) {
return op->emitOpError("result type (")
Expand Down
8 changes: 4 additions & 4 deletions clang/test/CIR/IR/int.cir
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
!s32i = !cir.int<s, 32>
!s64i = !cir.int<s, 64>

!u8i = !cir.int<s, 8>
!u16i = !cir.int<s, 16>
!u32i = !cir.int<s, 32>
!u64i = !cir.int<s, 64>
!u8i = !cir.int<u, 8>
!u16i = !cir.int<u, 16>
!u32i = !cir.int<u, 32>
!u64i = !cir.int<u, 64>

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

Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/IR/invalid.cir
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,12 @@ module {
}
}

// -----

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

0 comments on commit 523b445

Please sign in to comment.