Skip to content
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

Fix sign handling of alias types for binary operators and casts #410

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions tools/cgeist/Lib/clang-mlir.cc
Original file line number Diff line number Diff line change
@@ -2564,7 +2564,8 @@ ValueCategory MLIRScanner::VisitBinaryOperator(clang::BinaryOperator *BO) {
auto prevTy = res.getType().cast<mlir::IntegerType>();
auto postTy = getMLIRType(BO->getType()).cast<mlir::IntegerType>();
bool signedType = true;
if (auto bit = dyn_cast<clang::BuiltinType>(&*BO->getType())) {
if (auto bit =
dyn_cast<clang::BuiltinType>(&*BO->getType().getCanonicalType())) {
if (bit->isUnsignedInteger())
signedType = false;
if (bit->isSignedInteger())
@@ -2690,7 +2691,8 @@ ValueCategory MLIRScanner::VisitBinaryOperator(clang::BinaryOperator *BO) {
}
// TODO note assumptions made here about unsigned / unordered
bool signedType = true;
if (auto bit = dyn_cast<clang::BuiltinType>(&*BO->getType())) {
if (auto bit =
dyn_cast<clang::BuiltinType>(&*BO->getType().getCanonicalType())) {
if (bit->isUnsignedInteger())
signedType = false;
if (bit->isSignedInteger())
@@ -2763,7 +2765,8 @@ ValueCategory MLIRScanner::VisitBinaryOperator(clang::BinaryOperator *BO) {
case clang::BinaryOperator::Opcode::BO_EQ:
case clang::BinaryOperator::Opcode::BO_NE: {
signedType = true;
if (auto bit = dyn_cast<clang::BuiltinType>(&*BO->getLHS()->getType())) {
if (auto bit = dyn_cast<clang::BuiltinType>(
&*BO->getLHS()->getType().getCanonicalType())) {
if (bit->isUnsignedInteger())
signedType = false;
if (bit->isSignedInteger())
@@ -2998,7 +3001,8 @@ ValueCategory MLIRScanner::VisitBinaryOperator(clang::BinaryOperator *BO) {
if (auto prevTy = dyn_cast<mlir::IntegerType>(tostore.getType())) {
if (auto postTy = dyn_cast<mlir::IntegerType>(subType)) {
bool signedType = true;
if (auto bit = dyn_cast<clang::BuiltinType>(&*BO->getType())) {
if (auto bit = dyn_cast<clang::BuiltinType>(
&*BO->getType().getCanonicalType())) {
if (bit->isUnsignedInteger())
signedType = false;
if (bit->isSignedInteger())
@@ -4174,7 +4178,8 @@ ValueCategory MLIRScanner::VisitCastExpr(CastExpr *E) {
}
auto prevTy = scalar.getType().cast<mlir::IntegerType>();
bool signedType = true;
if (auto bit = dyn_cast<clang::BuiltinType>(&*E->getSubExpr()->getType())) {
if (auto bit = dyn_cast<clang::BuiltinType>(
&*E->getSubExpr()->getType().getCanonicalType())) {
if (bit->isUnsignedInteger())
signedType = false;
if (bit->isSignedInteger())
32 changes: 32 additions & 0 deletions tools/cgeist/Test/Verification/unsigned-type-alias.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: cgeist %s --function=* -S | FileCheck %s

typedef unsigned int ui32;
typedef unsigned long long ui64;

// CHECK: func.func @shift(%[[Varg0:.*]]: i64, %[[Varg1:.*]]: i64) -> i64
// CHECK-NEXT: %[[V0:.*]] = arith.shrui %[[Varg0]], %[[Varg1]] : i64
// CHECK-NEXT: return %[[V0]] : i64
// CHECK-NEXT: }

ui64 shift(ui64 input, ui64 shift) {
return input >> shift;
}

// CHECK: func.func @ge(%[[Varg0:.*]]: i64, %[[Varg1:.*]]: i64) -> i32
// CHECK-NEXT: %[[V0:.*]] = arith.cmpi ugt, %[[Varg0]], %[[Varg1]] : i64
// CHECK-NEXT: %[[V1:.*]] = arith.extui %[[V0]] : i1 to i32
// CHECK-NEXT: return %[[V1]] : i32
// CHECK-NEXT: }

int ge(ui64 input, ui64 v) {
return input > v;
}

// CHECK-NEXT: func.func @ret(%[[Varg0:.*]]: i32) -> i64
// CHECK-NEXT: %[[V0:.*]] = arith.extui %[[Varg0]] : i32 to i64
// CHECK-NEXT: return %[[V0]] : i64
// CHECK-NEXT: }

ui64 ret(ui32 input) {
return input;
}