EmbDev.net

Forum: ARM programming with GCC/GNU tools Need Help: A small problem in Clang


von Phong P. (Company: HUT) (phongph)


Rate this post
useful
not useful
Hi everyone,

I am having a small problem as follows, please help me:

Implement, using LLVM/Clang, to count the number of memory operations 
executed in a given function (recording reads and writes of fields and 
arrays elements) of C programs. Here is an example:

            int* test(int* b, int* c) {
               ...
               for (int i=0; i<10; i++) {
                   a[i] =b[i] + c[i];
               }
               return a;
            }

After execution of the whole program, the output of the instrumentation 
should return something like this for each method: "function test: reads 
= 20; writes = 10".

Thank you very much!

von Klaus W. (mfgkw)


Rate this post
useful
not useful
Are you interested in counting access to any objects or only particular 
objects?

If only particular objects: you can use C++ to create a class behaving 
like your array, but override operators to count usage.
The rest of your program could be kept in C as it is, usually without 
modifications.

von gjlayde (Guest)


Rate this post
useful
not useful
> you can use C++ to create a class behaving
> like your array, but override operators to count usage.

That's nothing to do with instrumentation: instrumenting code does not 
require to change the sources.

von Klaus W. (mfgkw)


Rate this post
useful
not useful
... ok, but it would answer his question "how man reads/writes?"

von gjlayde (Guest)


Rate this post
useful
not useful
> ok, but it would answer his question "how man reads/writes?"

But this was not the question :-)

Apart from that the numer of reads / writes can be determined by just 
looking at the function: "function test: reads = 20; writes = 10".

The question is: How to change Clang so that Clang can determine the 
number of reads / writes for any input (program), either by statically 
analyzing the input or by instrumenting the generated code without 
changing its semantics.

In general, you don't even know the input to clang as you are writing a 
specific analysis or transformaton, the given source is just a sample 
code to explain the issue w.r.t. changing clang.

von beric (Guest)


Rate this post
useful
not useful
Static analysis only will not help you:
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <time.h>
4
5
int a[5] = { 0, }; // does this count as "write" as well? :-)
6
int b[5] = { 1, 2, 3, 4, 5 };
7
int c[5] = { 6, 7, 8, 9, 0 };
8
9
int main()
10
{
11
   int i, n;
12
   time_t t;
13
14
   /* Initialize random number generator */
15
   srand((unsigned) time(&t));
16
17
   /* pick a number */
18
   n = rand() % 5;
19
20
   /* fill a - partially */
21
   for( i = 0 ; i < n ; i++ ) {
22
      a[i] = b[i] + c[i]; // <-- now, how many times is this executed?
23
   }
24
25
  return(0);
26
}

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.