From 10938fec086a8cf50804c135c82d8bd7c5d4a4cd Mon Sep 17 00:00:00 2001 From: Sirui Mu Date: Wed, 17 Jan 2024 03:30:51 +0800 Subject: [PATCH] [CIR] Fix int constant type verification (#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) : !cir.int // Not verified: !cir.int differs from !cir.int ``` The corresponding test is also wrong but fail to be detected. This patch fixes this issue. --- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 2 +- clang/test/CIR/IR/int.cir | 8 ++++---- clang/test/CIR/IR/invalid.cir | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 7b71b7d61cda..eaeb1ddc5bee 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -275,7 +275,7 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType, return success(); } - if (attrType.isa()) { + if (attrType.isa()) { auto at = attrType.cast(); if (at.getType() != opType) { return op->emitOpError("result type (") diff --git a/clang/test/CIR/IR/int.cir b/clang/test/CIR/IR/int.cir index 233198e4e335..3acaacd011f7 100644 --- a/clang/test/CIR/IR/int.cir +++ b/clang/test/CIR/IR/int.cir @@ -8,10 +8,10 @@ !s32i = !cir.int !s64i = !cir.int -!u8i = !cir.int -!u16i = !cir.int -!u32i = !cir.int -!u64i = !cir.int +!u8i = !cir.int +!u16i = !cir.int +!u32i = !cir.int +!u64i = !cir.int cir.func @validIntTypesAndAttributes() -> () { diff --git a/clang/test/CIR/IR/invalid.cir b/clang/test/CIR/IR/invalid.cir index 990129536431..5bbe3dd3536e 100644 --- a/clang/test/CIR/IR/invalid.cir +++ b/clang/test/CIR/IR/invalid.cir @@ -782,3 +782,12 @@ module { } } +// ----- + +!s8i = !cir.int +!u8i = !cir.int +cir.func @const_type_mismatch() -> () { + // expected-error@+1 {{'cir.const' op result type ('!cir.int') does not match value type ('!cir.int')}} + %2 = cir.const(#cir.int<0> : !s8i) : !u8i + cir.return +}