Iterator macros
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
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_dataied;
...for_each_ctx_engine(i915, ctx, ied) { execbuf.flags = ied.current_engine->flags; gem_execbuf(i915, &execbuf); }Changes I want:
1. Fromconst struct intel_execution_engine2 *e2tostruct intel_engine_data ied.
2. Replace the last argument offor_each_ctx_engine()following the change on previous step.
3. Change references frome2toied.current_engineinside 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
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_dataied;
...Quoted 7 lines
for_each_ctx_engine(i915, ctx, ied) { execbuf.flags = ied.current_engine->flags; gem_execbuf(i915, &execbuf); }Changes I want:
1. Fromconst struct intel_execution_engine2 *e2to `struct
intel_engine_dataied`.Quoted 1 line
2. Replace the last argument of
for_each_ctx_engine()following thechange on previous step.
Quoted 1 line
3. Change references from
e2toied.current_engineinside theiterator 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(...)
SQuoted 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
Signature · 1 line
Peter
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
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
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
AndrzejQuoted 20 lines
julia
Quoted 17 lines
julia > > Thank you, > > Peter > > * - https://gitlab.freedesktop.org/drm/igt-gpu-tools >--
Peter
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
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
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 passedAs 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
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 passedQuoted 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_mainint main ()
#defineigt_simple_mainint main ()#define
igt_fixture
#defineigt_subtest_group
#defineigt_dynamic(x)YACFE_ITERATOR
#defineigt_dynamic_f(x)YACFE_ITERATOR
#defineigt_subtest(x)YACFE_ITERATOR
#defineigt_subtest_with_dynamic(x)YACFE_ITERATOR#defineigt_subtest_with_dynamic_f(x)YACFE_ITERATOR#defineigt_subtest_f(x)YACFE_ITERATOR
#defineigt_until_timeout(x)YACFE_ITERATOR
#defineigt_while_interruptible(x)YACFE_ITERATOR#defineigt_list_for_each_entry_safe_reverse(x)YACFE_ITERATOR#defineigt_list_for_each_entry_safe(x)YACFE_ITERATOR#defineigt_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.hI'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
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 passedQuoted 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_mainint main ()
#defineigt_simple_mainint main ()#define
igt_fixture
#defineigt_subtest_group
#defineigt_dynamic(x)YACFE_ITERATOR
#defineigt_dynamic_f(x)YACFE_ITERATOR
#defineigt_subtest(x)YACFE_ITERATOR
#defineigt_subtest_with_dynamic(x)YACFE_ITERATOR#defineigt_subtest_with_dynamic_f(x)YACFE_ITERATOR#defineigt_subtest_f(x)YACFE_ITERATOR
#defineigt_until_timeout(x)YACFE_ITERATOR
#defineigt_while_interruptible(x)YACFE_ITERATOR#defineigt_list_for_each_entry_safe_reverse(x)YACFE_ITERATOR#defineigt_list_for_each_entry_safe(x)YACFE_ITERATOR#defineigt_list_for_each_entry(x)YACFE_ITERATOR#define_Atomic(x)xmanaged to parse over 98% of the lines in
Quoted 8 lines
Quoted 3 lines
Command line spatch --parse-c . --macro-file-builtins
igt.hI'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
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_dataied;
...for_each_ctx_engine(i915, ctx, ied) { execbuf.flags = ied.current_engine->flags; gem_execbuf(i915, &execbuf); }
Quoted 4 lines
Changes I want:
1. Fromconst struct intel_execution_engine2 *e2tostruct intel_engine_data ied.
2. Replace the last argument offor_each_ctx_engine()following the change on previous step.
3. Change references frome2toied.current_engineinside 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