Skip to content

Commit dc6e480

Browse files
authored
[clang][bytecode] Cast fixed-point cmp result to int if necessary (llvm#110469)
This is the case in C.
1 parent 9f3728d commit dc6e480

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,19 +1542,30 @@ bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
15421542
return true;
15431543
};
15441544

1545+
auto MaybeCastToBool = [&](bool Result) {
1546+
if (!Result)
1547+
return false;
1548+
PrimType T = classifyPrim(E);
1549+
if (DiscardResult)
1550+
return this->emitPop(T, E);
1551+
if (T != PT_Bool)
1552+
return this->emitCast(PT_Bool, T, E);
1553+
return true;
1554+
};
1555+
15451556
switch (E->getOpcode()) {
15461557
case BO_EQ:
1547-
return this->emitEQFixedPoint(E);
1558+
return MaybeCastToBool(this->emitEQFixedPoint(E));
15481559
case BO_NE:
1549-
return this->emitNEFixedPoint(E);
1560+
return MaybeCastToBool(this->emitNEFixedPoint(E));
15501561
case BO_LT:
1551-
return this->emitLTFixedPoint(E);
1562+
return MaybeCastToBool(this->emitLTFixedPoint(E));
15521563
case BO_LE:
1553-
return this->emitLEFixedPoint(E);
1564+
return MaybeCastToBool(this->emitLEFixedPoint(E));
15541565
case BO_GT:
1555-
return this->emitGTFixedPoint(E);
1566+
return MaybeCastToBool(this->emitGTFixedPoint(E));
15561567
case BO_GE:
1557-
return this->emitGEFixedPoint(E);
1568+
return MaybeCastToBool(this->emitGEFixedPoint(E));
15581569
case BO_Add:
15591570
return ConvertResult(this->emitAddFixedPoint(E));
15601571
case BO_Sub:

clang/test/Frontend/fixed_point_comparisons.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,UNPADDED
22
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -fpadding-on-unsigned-fixed-point -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,PADDED
33

4+
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,UNPADDED
5+
// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -fpadding-on-unsigned-fixed-point -emit-llvm %s -o - -fexperimental-new-constant-interpreter | FileCheck %s --check-prefixes=CHECK,PADDED
6+
47
// Fixed point against other fixed point
58
_Bool b_eq_true = 2.5hk == 2.5uhk; // CHECK-DAG: @b_eq_true = {{.*}}global i8 1, align 1
69
_Bool b_eq_false = 2.5hk == 2.4uhk; // CHECK-DAG: @b_eq_false = {{.*}}global i8 0, align 1

0 commit comments

Comments
 (0)