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

[CIR][CIRGen][Bugfix] Emit valid type for evaluated const #456

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 1 addition & 10 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,7 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {

// Creates constant null value for integral type ty.
mlir::cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc) {
if (ty.isa<mlir::cir::PointerType>())
return getNullPtr(ty, loc);

mlir::TypedAttr attr;
if (ty.isa<mlir::cir::IntType>())
attr = mlir::cir::IntAttr::get(ty, 0);
else
llvm_unreachable("NYI");

return create<mlir::cir::ConstantOp>(loc, ty, attr);
return create<mlir::cir::ConstantOp>(loc, ty, getZeroInitAttr(ty));
}

mlir::cir::ConstantOp getZero(mlir::Location loc, mlir::Type ty) {
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ mlir::Value ScalarExprEmitter::VisitBinLAnd(const clang::BinaryOperator *E) {
}
// 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
if (!CGF.ContainsLabel(E->getRHS()))
return Builder.getBool(false, Loc);
return Builder.getNullValue(ResTy, Loc);
}

CIRGenFunction::ConditionalEvaluation eval(CGF);
Expand Down Expand Up @@ -2200,8 +2200,12 @@ mlir::Value ScalarExprEmitter::VisitBinLOr(const clang::BinaryOperator *E) {
return Builder.createZExtOrBitCast(RHSCond.getLoc(), RHSCond, ResTy);
}
// 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
if (!CGF.ContainsLabel(E->getRHS()))
return Builder.getBool(true, Loc);
if (!CGF.ContainsLabel(E->getRHS())) {
if (auto intTy = ResTy.dyn_cast<mlir::cir::IntType>())
return Builder.getConstInt(Loc, intTy, 1);
else
return Builder.getBool(true, Loc);
}
}

CIRGenFunction::ConditionalEvaluation eval(CGF);
Expand Down
20 changes: 20 additions & 0 deletions clang/test/CIR/CodeGen/evaluate-expr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

static const int g = 1;
void foo() {
if ((g != 1) && (g != 1))
return;
if ((g == 1) || (g == 1))
return;
}
// CHECK: cir.func no_proto @foo()
// CHECK: cir.scope {
// CHECK: [[ZERO:%.*]] = cir.const(#cir.int<0> : !s32i) : !s32i
// CHECK: [[FALSE:%.*]] = cir.cast(int_to_bool, [[ZERO:%.*]] : !s32i), !cir.bool
// CHECK: cir.if [[FALSE]] {
// CHECK: cir.return
// CHECK: }
// CHECK: }
// CHECK: cir.return

Loading