This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Optionally cast comparison result to non-bool
ClosedPublic

Authored by tbaeder on May 2 2023, 4:40 AM.

Details

Summary
Our comparison opcodes always produce a Boolean value and push it on the
stack. However, the result of such a comparison in C is int, so the
later code expects an integer value on the stack.

Work around this problem by casting the boolean value to int in those
cases. This is not ideal for C however. The comparison is usually
wrapped in a IntegerToBool cast anyway.

Diff Detail

Event Timeline

tbaeder created this revision.May 2 2023, 4:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 4:40 AM
tbaeder requested review of this revision.May 2 2023, 4:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 4:40 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

For C, should we instead be teaching our boolean operations to understand it might be int? I fear this will end up causing conversion problems later, such as with:

int F = 1 > 2;. We won't end up having a conversion operation there, since the RHS is already int, for the LHS.

For C, should we instead be teaching our boolean operations to understand it might be int? I fear this will end up causing conversion problems later, such as with:

int F = 1 > 2;. We won't end up having a conversion operation there, since the RHS is already int, for the LHS.

The result of 1 > 2 is int, yes. That's what this patch does - it converts the Boolean we create to the int the AST (and thus all the intepreter code inspecting it) expects.

The AST has no IntegralToBool cast in the example, but that doesn't matter for this patch; it just fixes the types on the stack to correspond to what the later code expects. I'm not opposed to adding a target type to the comparison ops, but I'm not sure if the additional complexity is worth it.

For C, should we instead be teaching our boolean operations to understand it might be int? I fear this will end up causing conversion problems later, such as with:

int F = 1 > 2;. We won't end up having a conversion operation there, since the RHS is already int, for the LHS.

The result of 1 > 2 is int, yes. That's what this patch does - it converts the Boolean we create to the int the AST (and thus all the intepreter code inspecting it) expects.

The AST has no IntegralToBool cast in the example, but that doesn't matter for this patch; it just fixes the types on the stack to correspond to what the later code expects. I'm not opposed to adding a target type to the comparison ops, but I'm not sure if the additional complexity is worth it.

That is pretty simplified, but I guess I'm concerned about situations where the next action is an assignment or other integer action in the constant expression evaluator. ARE you able to handle things like:

_Static_assert( (5 > 4) + (3 > 2) == 2, ""); in C mode (where this change is needed?)?

tbaeder updated this revision to Diff 518707.May 2 2023, 6:47 AM

For C, should we instead be teaching our boolean operations to understand it might be int? I fear this will end up causing conversion problems later, such as with:

int F = 1 > 2;. We won't end up having a conversion operation there, since the RHS is already int, for the LHS.

The result of 1 > 2 is int, yes. That's what this patch does - it converts the Boolean we create to the int the AST (and thus all the intepreter code inspecting it) expects.

The AST has no IntegralToBool cast in the example, but that doesn't matter for this patch; it just fixes the types on the stack to correspond to what the later code expects. I'm not opposed to adding a target type to the comparison ops, but I'm not sure if the additional complexity is worth it.

That is pretty simplified, but I guess I'm concerned about situations where the next action is an assignment or other integer action in the constant expression evaluator. ARE you able to handle things like:

_Static_assert( (5 > 4) + (3 > 2) == 2, ""); in C mode (where this change is needed?)?

Yes, that works :)

erichkeane accepted this revision.May 2 2023, 7:00 AM
This revision is now accepted and ready to land.May 2 2023, 7:00 AM
This revision was landed with ongoing or failed builds.May 31 2023, 4:01 AM
This revision was automatically updated to reflect the committed changes.