Skip to contentSkip to conversation

Iterator macros

cocci Peter Senna Tschudin 4 participants 12 messages Aug 13, 2024 – Aug 14, 2024

lore
Share & tools
lore
#0 lore raw

Dear List,

I am trying to make changes to a macro iterator from igt-gpu-tools*, and I am failing to use Coccinelle. My current attempt only captures about 1/2 of call sites, and I am not really sure how to ask Coccinelle to make the changes I want. Here is the pattern I am trying to change:

From:

const struct intel_execution_engine2 *e2;
...

for_each_ctx_engine(i915, ctx, e2) { 
    execbuf.flags = e2->flags; 
    gem_execbuf(i915, &execbuf); 
} 

To:

struct intel_engine_data ied;
...

for_each_ctx_engine(i915, ctx, ied) { 
    execbuf.flags = ied.current_engine->flags; 
    gem_execbuf(i915, &execbuf); 
} 

Changes I want:
1. From const struct intel_execution_engine2 *e2 to struct intel_engine_data ied.
2. Replace the last argument of for_each_ctx_engine() following the change on previous step.
3. Change references from e2 to ied.current_engine inside the iterator statement.

Here is my starting point that finds about half of the call sites:

@r1@
iterator I =~ "for_each_ctx_engine";
statement S;
@@
* I(...)
S

I assume that I should create other rules and import I from r1 as the core of the changes I want. Can you point me to an example to help me here?

Thank you,

Peter

* - https://gitlab.freedesktop.org/drm/igt-gpu-tools

#1 lore raw
Quoted 38 lines · Peter Senna Tschudin

Dear List,

I am trying to make changes to a macro iterator from igt-gpu-tools*, and I am failing to use Coccinelle. My current attempt only captures about 1/2 of call sites, and I am not really sure how to ask Coccinelle to make the changes I want. Here is the pattern I am trying to change:

From:

const struct intel_execution_engine2 *e2;
...

for_each_ctx_engine(i915, ctx, e2) {
    execbuf.flags = e2->flags;
    gem_execbuf(i915, &execbuf);
}

To:

struct intel_engine_data ied;
...

for_each_ctx_engine(i915, ctx, ied) {
    execbuf.flags = ied.current_engine->flags;
    gem_execbuf(i915, &execbuf);
}

Changes I want:
1. From const struct intel_execution_engine2 *e2 to struct intel_engine_data ied.
2. Replace the last argument of for_each_ctx_engine() following the change on previous step.
3. Change references from e2 to ied.current_engine inside the iterator statement.

Here is my starting point that finds about half of the call sites:

@r1@
iterator I =~ "for_each_ctx_engine";
statement S;
@@
* I(...)
S

Quoted 3 lines

I assume that I should create other rules and import I from r1 as the core of the changes I want. Can you point me to an example to help me here?

Are you just looking for

iterator r.I;

If you have only one iterator name, you don't need the regexp. Just put

iterator name for_each_ctx_engine;

julia

Quoted 7 lines

Thank you,

Peter

* - https://gitlab.freedesktop.org/drm/igt-gpu-tools

#2 lore raw

Hi Julia,

Quoted 52 lines · Julia Lawall
Quoted 3 lines · Peter Senna Tschudin

Dear List,

I am trying to make changes to a macro iterator from igt-gpu-tools*, and

I am failing to use Coccinelle. My current attempt only captures about 1/2 of call sites, and I am not really sure how to ask Coccinelle to make the changes I want. Here is the pattern I am trying to change:

Quoted 16 lines

From:

const struct intel_execution_engine2 *e2;
...

for_each_ctx_engine(i915, ctx, e2) {
    execbuf.flags = e2->flags;
    gem_execbuf(i915, &execbuf);
}

To:

struct intel_engine_data ied;
...

Quoted 7 lines
for_each_ctx_engine(i915, ctx, ied) {
    execbuf.flags = ied.current_engine->flags;
    gem_execbuf(i915, &execbuf);
}

Changes I want:
1. From const struct intel_execution_engine2 *e2 to `struct

intel_engine_data ied`.

Quoted 1 line

2. Replace the last argument of for_each_ctx_engine() following the

change on previous step.

Quoted 1 line

3. Change references from e2 to ied.current_engine inside the

iterator statement.

Quoted 10 lines

Here is my starting point that finds about half of the call sites:

@r1@
iterator I =~ "for_each_ctx_engine";
statement S;
@@
* I(...)
S

Quoted 3 lines

I assume that I should create other rules and import I from r1 as the core of the changes I want. Can you point me to an example to help me here?

Quoted 8 lines

Are you just looking for

iterator r.I;

If you have only one iterator name, you don't need the regexp. Just put

iterator name for_each_ctx_engine;

Thank you! This is much faster but captures one instance less.

Quoted 11 lines

julia

Quoted 7 lines

Thank you,

Peter

* - https://gitlab.freedesktop.org/drm/igt-gpu-tools

Signature · 1 line
                         Peter
#3 lore raw
Quoted 70 lines · Peter Senna Tschudin

Hi Julia,

On Tue, Aug 13, 2024 at 8:19 AM Julia Lawall <[email protected]> wrote:

      On Tue, 13 Aug 2024, Peter Senna Tschudin wrote:

      > Dear List,
      >
      > I am trying to make changes to a macro iterator from
      igt-gpu-tools*, and I am failing to use Coccinelle. My current
      attempt only captures about 1/2 of call sites, and I am not
      really sure how to ask Coccinelle to make the changes I want.
      Here is the pattern I am trying to change:
      >
      >
      > From:
      >
      > const struct intel_execution_engine2 *e2;
      > ...
      > for_each_ctx_engine(i915, ctx, e2) {
      >     execbuf.flags = e2->flags;
      >     gem_execbuf(i915, &execbuf);
      > }
      >
      >
      > To:
      >
      > struct intel_engine_data ied;
      > ...
      > for_each_ctx_engine(i915, ctx, ied) {
      >     execbuf.flags = ied.current_engine->flags;
      >     gem_execbuf(i915, &execbuf);
      > }
      >
      > Changes I want:
      > 1. From `const struct intel_execution_engine2 *e2` to `struct
      intel_engine_data ied`.
      > 2. Replace the last argument of for_each_ctx_engine()
      following the change on previous step.
      > 3. Change references from `e2` to `ied.current_engine` inside
      the iterator statement.
      >
      > Here is my starting point that finds about half of the call
      sites:
      >
      > @r1@
      > iterator I =~ "for_each_ctx_engine";
      > statement S;
      > @@
      > * I(...)
      > S
      >
      > I assume that I should create other rules and import I from r1
      as the
      > core of the changes I want. Can you point me to an example to
      help me
      > here?

      Are you just looking for

      iterator r.I;

      If you have only one iterator name, you don't need the regexp. 
      Just put

      iterator name for_each_ctx_engine;
Quoted 1 line

Thank you! This is much faster but captures one instance less.

You can use multiple names like

(name1@I|name2@I|name3@I)(...) S

The you can use r.I in your other rules. name1 etc would be iterator name and I would be iterator.

julia

Quoted 18 lines

      julia

      >
      > Thank you,
      >
      > Peter
      >
      > * - https://gitlab.freedesktop.org/drm/igt-gpu-tools
      >

--
                         Peter

#4 lore raw
Quoted 73 lines · Julia Lawall
Quoted 70 lines · Peter Senna Tschudin

Hi Julia,

On Tue, Aug 13, 2024 at 8:19 AM Julia Lawall <[email protected]> wrote:

       On Tue, 13 Aug 2024, Peter Senna Tschudin wrote:

       > Dear List,
       >
       > I am trying to make changes to a macro iterator from
       igt-gpu-tools*, and I am failing to use Coccinelle. My current
       attempt only captures about 1/2 of call sites, and I am not
       really sure how to ask Coccinelle to make the changes I want.
       Here is the pattern I am trying to change:
       >
       >
       > From:
       >
       > const struct intel_execution_engine2 *e2;
       > ...
       > for_each_ctx_engine(i915, ctx, e2) {
       >     execbuf.flags = e2->flags;
       >     gem_execbuf(i915, &execbuf);
       > }
       >
       >
       > To:
       >
       > struct intel_engine_data ied;
       > ...
       > for_each_ctx_engine(i915, ctx, ied) {
       >     execbuf.flags = ied.current_engine->flags;
       >     gem_execbuf(i915, &execbuf);
       > }
       >
       > Changes I want:
       > 1. From `const struct intel_execution_engine2 *e2` to `struct
       intel_engine_data ied`.
       > 2. Replace the last argument of for_each_ctx_engine()
       following the change on previous step.
       > 3. Change references from `e2` to `ied.current_engine` inside
       the iterator statement.
       >
       > Here is my starting point that finds about half of the call
       sites:
       >
       > @r1@
       > iterator I =~ "for_each_ctx_engine";
       > statement S;
       > @@
       > * I(...)
       > S
       >
       > I assume that I should create other rules and import I from r1
       as the
       > core of the changes I want. Can you point me to an example to
       help me
       > here?

       Are you just looking for

       iterator r.I;

       If you have only one iterator name, you don't need the regexp.
       Just put

       iterator name for_each_ctx_engine;
Quoted 7 lines
Quoted 1 line

Thank you! This is much faster but captures one instance less.

You can use multiple names like

(name1@I|name2@I|name3@I)(...) S

The you can use r.I in your other rules. name1 etc would be iterator name and I would be iterator.

I guess the issue here could be a problem with parsing igt code (it is also an issue with kernel drivers using iterators). Cocci quite often does not recognize automatically iterators or other constructs, and does not scan them at all. Try run for example:

$ spatch --parse-c tests/intel/gem_ctx_exec.c
...
NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
nb good = 385,  nb passed = 19 =========> 3.12% passed
nb good = 385,  nb bad = 204 =========> 66.45% good or passed

As I understand, lines marked as "bad" are skipped during cocci pass, am I right? Some workaround is to define iterators by hand in the script or include files.

Regards
Andrzej

Quoted 21 lines

julia

Quoted 18 lines
  

       julia

       >
       > Thank you,
       >
       > Peter
       >
       > * - https://gitlab.freedesktop.org/drm/igt-gpu-tools
       >

--
                         Peter

#5 lore raw
Quoted 96 lines · Andrzej Hajda
Quoted 74 lines · Julia Lawall
Quoted 71 lines · Peter Senna Tschudin

Hi Julia,

On Tue, Aug 13, 2024 at 8:19 AM Julia Lawall <[email protected]> wrote:

       On Tue, 13 Aug 2024, Peter Senna Tschudin wrote:

       > Dear List,
       >
       > I am trying to make changes to a macro iterator from
       igt-gpu-tools*, and I am failing to use Coccinelle. My current
       attempt only captures about 1/2 of call sites, and I am not
       really sure how to ask Coccinelle to make the changes I want.
       Here is the pattern I am trying to change:
       >
       >
       > From:
       >
       > const struct intel_execution_engine2 *e2;
       > ...
       > for_each_ctx_engine(i915, ctx, e2) {
       >     execbuf.flags = e2->flags;
       >     gem_execbuf(i915, &execbuf);
       > }
       >
       >
       > To:
       >
       > struct intel_engine_data ied;
       > ...
       > for_each_ctx_engine(i915, ctx, ied) {
       >     execbuf.flags = ied.current_engine->flags;
       >     gem_execbuf(i915, &execbuf);
       > }
       >
       > Changes I want:
       > 1. From `const struct intel_execution_engine2 *e2` to `struct
       intel_engine_data ied`.
       > 2. Replace the last argument of for_each_ctx_engine()
       following the change on previous step.
       > 3. Change references from `e2` to `ied.current_engine` inside
       the iterator statement.
       >
       > Here is my starting point that finds about half of the call
       sites:
       >
       > @r1@
       > iterator I =~ "for_each_ctx_engine";
       > statement S;
       > @@
       > * I(...)
       > S
       >
       > I assume that I should create other rules and import I from r1
       as the
       > core of the changes I want. Can you point me to an example to
       help me
       > here?

       Are you just looking for

       iterator r.I;

       If you have only one iterator name, you don't need the regexp.
       Just put

       iterator name for_each_ctx_engine;
Quoted 7 lines
Quoted 1 line

Thank you! This is much faster but captures one instance less.

You can use multiple names like

(name1@I|name2@I|name3@I)(...) S

The you can use r.I in your other rules. name1 etc would be iterator name and I would be iterator.

I guess the issue here could be a problem with parsing igt code (it is also an issue with kernel drivers using iterators).
Cocci quite often does not recognize automatically iterators or other constructs, and does not scan them at all.
Try run for example:

$ spatch --parse-c tests/intel/gem_ctx_exec.c
...
NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
nb good = 385,  nb passed = 19 =========> 3.12% passed
nb good = 385,  nb bad = 204 =========> 66.45% good or passed
Quoted 3 lines

As I understand, lines marked as "bad" are skipped during cocci pass, am I right?
Some workaround is to define iterators by hand in the script or include files.

Coccinelle has some heuristics for detecting iterators, typically tarting with "for" and having the indentation of a for loop.

In a .h file provided with --macro-file-builtins, it seems that you can use YACFE_ITERATOR, but there is currently no example of that.

Finally, if you declare an iterator name in your semantic patch, that information will be propagated to the parsing of the C code. On the other hand, Peter's initial regexp solution would not have triggered that. Perhaps his performance improvement was actually due to being able to parse the code immediately rather than iterating over some heuristics.

julia

Quoted 26 lines

Regards
Andrzej

Quoted 20 lines

julia

Quoted 17 lines
       julia

       >
       > Thank you,
       >
       > Peter
       >
       > * - https://gitlab.freedesktop.org/drm/igt-gpu-tools
       >

--
                         Peter

#6 lore raw
In reply to Andrzej Hajda
Quoted 11 lines

I guess the issue here could be a problem with parsing igt code (it is also an issue with kernel drivers using iterators).
Cocci quite often does not recognize automatically iterators or other constructs, and does not scan them at all.
Try run for example:

$ spatch --parse-c tests/intel/gem_ctx_exec.c
...
NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
nb good = 385,  nb passed = 19 =========> 3.12% passed
nb good = 385,  nb bad = 204 =========> 66.45% good or passed
Quoted 3 lines

As I understand, lines marked as "bad" are skipped during cocci pass, am I right?
Some workaround is to define iterators by hand in the script or include files.

The following file:

#define igt_main int main ()
#define igt_simple_main int main ()

#define igt_fixture
#define igt_subtest_group
#define igt_dynamic(x) YACFE_ITERATOR
#define igt_dynamic_f(x) YACFE_ITERATOR
#define igt_subtest(x) YACFE_ITERATOR
#define igt_subtest_with_dynamic(x) YACFE_ITERATOR #define igt_subtest_with_dynamic_f(x) YACFE_ITERATOR #define igt_subtest_f(x) YACFE_ITERATOR
#define igt_until_timeout(x) YACFE_ITERATOR
#define igt_while_interruptible(x) YACFE_ITERATOR #define igt_list_for_each_entry_safe_reverse(x) YACFE_ITERATOR #define igt_list_for_each_entry_safe(x) YACFE_ITERATOR #define igt_list_for_each_entry(x) YACFE_ITERATOR #define _Atomic(x) x

managed to parse over 98% of the lines in

https://gitlab.freedesktop.org/drm/igt-gpu-tools

Command line spatch --parse-c . --macro-file-builtins igt.h

I'll check on the driver file you mentioned. Thanks.

julia

#7 lore raw
Quoted 34 lines · Julia Lawall
Quoted 11 lines

I guess the issue here could be a problem with parsing igt code (it is also an issue with kernel drivers using iterators).
Cocci quite often does not recognize automatically iterators or other constructs, and does not scan them at all.
Try run for example:

$ spatch --parse-c tests/intel/gem_ctx_exec.c
...
NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
nb good = 385,  nb passed = 19 =========> 3.12% passed
nb good = 385,  nb bad = 204 =========> 66.45% good or passed
Quoted 3 lines

As I understand, lines marked as "bad" are skipped during cocci pass, am I right?
Some workaround is to define iterators by hand in the script or include files.

The following file:

#define igt_main int main ()
#define igt_simple_main int main ()

#define igt_fixture
#define igt_subtest_group
#define igt_dynamic(x) YACFE_ITERATOR
#define igt_dynamic_f(x) YACFE_ITERATOR
#define igt_subtest(x) YACFE_ITERATOR
#define igt_subtest_with_dynamic(x) YACFE_ITERATOR #define igt_subtest_with_dynamic_f(x) YACFE_ITERATOR #define igt_subtest_f(x) YACFE_ITERATOR
#define igt_until_timeout(x) YACFE_ITERATOR
#define igt_while_interruptible(x) YACFE_ITERATOR #define igt_list_for_each_entry_safe_reverse(x) YACFE_ITERATOR #define igt_list_for_each_entry_safe(x) YACFE_ITERATOR #define igt_list_for_each_entry(x) YACFE_ITERATOR #define _Atomic(x) x

Quoted 7 lines

managed to parse over 98% of the lines in

https://gitlab.freedesktop.org/drm/igt-gpu-tools

Command line spatch --parse-c . --macro-file-builtins igt.h

I'll check on the driver file you mentioned. Thanks.

Wow, great.

I wonder if it would be possible to issue warning if spatch encounters lines which it is not able to parse, to avoid confusion.

Regards
Andrzej

Quoted 2 lines

julia

#8 lore raw
Quoted 43 lines · Andrzej Hajda
Quoted 40 lines · Julia Lawall
Quoted 12 lines

I guess the issue here could be a problem with parsing igt code (it is also an
issue with kernel drivers using iterators).
Cocci quite often does not recognize automatically iterators or other constructs, and does not scan them at all.
Try run for example:

$ spatch --parse-c tests/intel/gem_ctx_exec.c
...
NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
nb good = 385,  nb passed = 19 =========> 3.12% passed
nb good = 385,  nb bad = 204 =========> 66.45% good or passed
Quoted 4 lines

As I understand, lines marked as "bad" are skipped during cocci pass, am I right?
Some workaround is to define iterators by hand in the script or include files.

The following file:

#define igt_main int main ()
#define igt_simple_main int main ()

#define igt_fixture
#define igt_subtest_group
#define igt_dynamic(x) YACFE_ITERATOR
#define igt_dynamic_f(x) YACFE_ITERATOR
#define igt_subtest(x) YACFE_ITERATOR
#define igt_subtest_with_dynamic(x) YACFE_ITERATOR #define igt_subtest_with_dynamic_f(x) YACFE_ITERATOR #define igt_subtest_f(x) YACFE_ITERATOR
#define igt_until_timeout(x) YACFE_ITERATOR
#define igt_while_interruptible(x) YACFE_ITERATOR #define igt_list_for_each_entry_safe_reverse(x) YACFE_ITERATOR #define igt_list_for_each_entry_safe(x) YACFE_ITERATOR #define igt_list_for_each_entry(x) YACFE_ITERATOR #define _Atomic(x) x

managed to parse over 98% of the lines in

https://gitlab.freedesktop.org/drm/igt-gpu-tools

Quoted 8 lines
Quoted 3 lines

Command line spatch --parse-c . --macro-file-builtins igt.h

I'll check on the driver file you mentioned. Thanks.

Wow, great.

I wonder if it would be possible to issue warning if spatch encounters lines which it is not able to parse, to avoid confusion.

The theory is that generally you don't want that, because lost of macros will trigger warnings, but are not relevant to the change that you want to make.

But there is an option --verbose-parsing that should provide more information.

julia

#9 lore raw
In reply to Andrzej Hajda
Quoted 10 lines

Try run for example:

$ spatch --parse-c tests/intel/gem_ctx_exec.c
...
NB total files = 1; perfect = 0; pbs = 1; timeout = 0; =========> 0%
nb good = 385,  nb passed = 19 =========> 3.12% passed
nb good = 385,  nb bad = 204 =========> 66.45% good or passed

As I understand, lines marked as "bad" are skipped during cocci pass, am I right?
Some workaround is to define iterators by hand in the script or include files.

With the definitions I mentioned, this file is at 100%.

julia

#10 lore raw
In reply to Peter Senna Tschudin
Quoted 60 lines · Peter Senna Tschudin

Hi Julia,

On Tue, Aug 13, 2024 at 8:19 AM Julia Lawall <[email protected]> wrote:

      On Tue, 13 Aug 2024, Peter Senna Tschudin wrote:

      > Dear List,
      >
      > I am trying to make changes to a macro iterator from igt-gpu-tools*, and I am failing to use Coccinelle. My current attempt only captures about 1/2 of call sites, and I am not really sure how
      to ask Coccinelle to make the changes I want. Here is the pattern I am trying to change:
      >
      >
      > From:
      >
      > const struct intel_execution_engine2 *e2;
      > ...
      > for_each_ctx_engine(i915, ctx, e2) {
      >     execbuf.flags = e2->flags;
      >     gem_execbuf(i915, &execbuf);
      > }
      >
      >
      > To:
      >
      > struct intel_engine_data ied;
      > ...
      > for_each_ctx_engine(i915, ctx, ied) {
      >     execbuf.flags = ied.current_engine->flags;
      >     gem_execbuf(i915, &execbuf);
      > }
      >
      > Changes I want:
      > 1. From `const struct intel_execution_engine2 *e2` to `struct intel_engine_data ied`.
      > 2. Replace the last argument of for_each_ctx_engine() following the change on previous step.
      > 3. Change references from `e2` to `ied.current_engine` inside the iterator statement.
      >
      > Here is my starting point that finds about half of the call sites:
      >
      > @r1@
      > iterator I =~ "for_each_ctx_engine";
      > statement S;
      > @@
      > * I(...)
      > S
      >
      > I assume that I should create other rules and import I from r1 as the
      > core of the changes I want. Can you point me to an example to help me
      > here?

      Are you just looking for

      iterator r.I;

      If you have only one iterator name, you don't need the regexp.  Just put

      iterator name for_each_ctx_engine;
Quoted 1 line

Thank you! This is much faster but captures one instance less.

Which one are you missing? I downloaded the code and tried it, and I got exactly the same results with both semantic patches, with the one with iterator name being 3x faster (19 seconds -> 6 seconds).

julia

Quoted 18 lines

      julia

      >
      > Thank you,
      >
      > Peter
      >
      > * - https://gitlab.freedesktop.org/drm/igt-gpu-tools
      >

--
                         Peter

#11 lore raw
In reply to Peter Senna Tschudin
Quoted 24 lines · Peter Senna Tschudin

Dear List,

I am trying to make changes to a macro iterator from igt-gpu-tools*, and I am failing to use Coccinelle. My current attempt only captures about 1/2 of call sites, and I am not really sure how to ask Coccinelle to make the changes I want. Here is the pattern I am trying to change:

From:

const struct intel_execution_engine2 *e2;
...

for_each_ctx_engine(i915, ctx, e2) {
    execbuf.flags = e2->flags;
    gem_execbuf(i915, &execbuf);
}

To:

struct intel_engine_data ied;
...

for_each_ctx_engine(i915, ctx, ied) {
    execbuf.flags = ied.current_engine->flags;
    gem_execbuf(i915, &execbuf);
}
Quoted 4 lines

Changes I want:
1. From const struct intel_execution_engine2 *e2 to struct intel_engine_data ied.
2. Replace the last argument of for_each_ctx_engine() following the change on previous step.
3. Change references from e2 to ied.current_engine inside the iterator statement.

I think that this gets at what you want. It gives many warnings, but you can ignore them:

@@
iterator name for_each_ctx_engine;
identifier e2;
symbol ied;
@@
- const struct intel_execution_engine2 *e2;
+ struct intel_engine_data ied;
<+...
for_each_ctx_engine(...,
- e2
+ ied.current_engine
) { <...
- e2
+ ied

      ...>
    }

...+>

julia