This is an archive of the discontinued LLVM Phabricator instance.

[Clang][OpenMP] Avoid using `IgnoreImpCasts` if possible
ClosedPublic

Authored by tianshilei1992 on May 28 2022, 3:37 PM.

Details

Summary

This patch removes all IgnoreImpCasts in Sema, and only uses it if necessary. If the expression is not of the same type as the pointer value, a cast is inserted.

Diff Detail

Event Timeline

tianshilei1992 created this revision.May 28 2022, 3:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 28 2022, 3:37 PM
tianshilei1992 requested review of this revision.May 28 2022, 3:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 28 2022, 3:37 PM

Can you add the test?

Can you add the test?

I don't have a good test off the top of my head.

I will refine some logics to set X, E, etc. to avoid more IgnoreImpCasts. However, I don't know how to deal with type promotion in the condition statement. Here we consider the following case:

if (x == e) { x = d; }

x and d can be set easily w/o need of IgnoreImpCasts. When it comes to e, things become complicated. There are basically three cases:

  1. x and e are not promoted. That is the case when x and e are of same type, and they are both at least int. In this case, we don't need IgnoreImpCasts here. If they are of the same with shorter representation, such as int16_t, they will all be promoted, which will be mentioned later.
  2. x is not promoted, but e is promoted. That is the case when x has higher ranking than e. In this case, we should not use IgnoreImpCasts.
  3. Both x and e are promoted. This case is very common, and it can be further divided into following cases:
    • x and e of the same type with lower ranking. For example, when x and e are both short, they will be promoted to int. In this case, we want IgnoreImpCasts.
    • x and e are of different types, but they can be implicitly converted. For example, x is int, while e is short. In this case, we don't want IgnoreImpCasts because e is implicitly promoted to int, which is exactly what we want.

I'm thinking we may want to avoid using IgnoreImpCasts and later create cast if necessary. However, that may cause some unnecessary casts, at least in LLVM IR, such as the following case:

%1 = load i8 *%e
%2 = cast to i32 %1
%3 = cast to i8 %2
cmpxchg i8 * %x, i8 %3

I'm not sure eventually %2 and %3 will be optimized out.

tianshilei1992 retitled this revision from [Clang][OpenMP] Replace IgnoreImpCasts with IgnoreImplicitAsWritten in atomic compare to [Clang][OpenMP] Avoid using `IgnoreImpCasts` if possible.Jun 2 2022, 10:51 AM
tianshilei1992 edited the summary of this revision. (Show Details)
This revision is now accepted and ready to land.Jun 2 2022, 10:56 AM