This is an archive of the discontinued LLVM Phabricator instance.

[clang][ThreadSafety] Add __builtin_instance_member (WIP)
AbandonedPublic

Authored by tbaeder on Jun 15 2023, 1:24 AM.

Details

Summary

As discussed in https://github.com/llvm/llvm-project/issues/20777, this adds __builtin_instance_member(membername), which acts like this->membername, but in C.

This is obviously very much WIP and the patch contains several placeholders, but I wonder if this is the right approach to take here and if I should continue on this path, so I'm opening this for review.

As you can see from the test case, this works for the (one) use case it was created for.

Diff Detail

Event Timeline

tbaeder created this revision.Jun 15 2023, 1:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 1:24 AM
tbaeder requested review of this revision.Jun 15 2023, 1:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 1:24 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I'm wondering why you chose this over a direct equivalent to this, say __builtin_instance(), such that instead of __builtin_instance_member(M) one would write __builtin_instance().M or __builtin_instance()->M? While allowing to reference the same fields, it would additionally allow referencing the object itself. That's not so interesting for Thread Safety Analysis, because many attributes refer to this if no argument is given, but it might be helpful for other analyses.

It also seems cleaner C/C++ to me, as you can read the expression in the usual way, whereas in __builtin_instance_member(M), M is not a DeclRefExpr. And you could do #define THIS __builtin_instance().

Didn't remember and re-checked, using it that way makes the implementation harder I think:

tsa2.c:7:54: error: incomplete definition of type 'struct Mutex'
    7 |   int counter GUARDED_BY(__builtin_instance_member(M)->M);
      |                                                      ^
tsa2.c:6:10: note: forward declaration of 'struct Mutex'
    6 |   struct Mutex *M;
      |          ^
1 error generated.

and I didn't immediately know how to fix that.

We probably parse the attributes delayed in C++ but not in C. This probably also means you can't refer to later members in the attribute, so the mutex always needs to come first, right? Not sure if such a limitation is expected for C developers.

tbaeder abandoned this revision.Oct 11 2023, 7:16 AM

As discussed via other channels, this is going nowhere and we will take another approach, if any.