This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Is it possible to have non-optimised block within a source file.



On June 21, 2001 Richard Moore wrote:

> I have a need to ask the compiler not to optimise certain sections of
code
> within a source file. Specifically, I don't want source insertion or
> re-ordering to be done at certain points  when in general -O2
optimisation
> is requested.
>
> a) Is there away of achieving this?
> b) if not, does anyone have a feel for the difficulty of implementing
such
> as device with perhaps a #pragma directive?
>
> Why I ask these questions is because I'd like the ability to jump from an
> in-line assembler section to a c coded block. I know that jumps in
> assembler are not visibkle to the compiler. I have a hooking mechanism
> where the jump target us switched dynamically - or rather i'd like one.
Yes
> there are other ways to implement hooks, but this mechanism gives certain
> performance advantages.

Part of Mike Stump's rely:

> Before going down that path, I'd recommend exposing the jump to the
> compiler through the use of if, or goto in the higher level language.
> Do that, and then explain the problems that develop and we can try and
> solve those.  Those are the real problems.
>
> This concept is called, stop lying to the optimizer.  The reason why
> you want to, is because everytime you do, it will hurt.  If you are
> patient and explain what your doing to the optimizer, it will not hurt
> you as often.

Richard Moore is the maintainer of a package called the Generalised
Kernel Hooks Interface (GKHI) for Linux.  This package allows a kernel
developer to insert debugging hooks into source code and then enable or
disable them at run time.  Initially this was done completely with
__asm__ code, but now he's looking for a way to include C code in the
hook.  This is proving to be difficult, as the potential solutions so
far hide some critical information from the optimizer (causing it to
hurt him).

What Richard needs is a mechanism to provide hook code that will seldom
be executed and that, when disabled, won't result in any slowdown to the
function containing the hook.  I looked at putting the hook code into an
IF block whose expression uses __builtin_expect, but even that involves
the overhead of loading a global variable to test.

Does adding the following new functionality to the compiler seem
reasonable?  __builtin_hook (suggestions for a more appropriate name are
welcome) has one argument and declares that:

  - the following hook block is either placed in a separate section or
    else moved to the end of the function, as with code in an IF block
    that __builtin_expect declares will rarely be used

  - a global label, whose name is specified by the argument, is placed
    at the location of the __builtin_hook and is followed by two
    unconditional jumps; the first jumps past the second, and the second
    jumps to the hook block; to enable the hook, a tool replaces the
    first jump with nops; a local label follows these jumps

  - the hook block ends with a jump to the label that follows the two
    jumps at the hook location

  - the optimizer leaves the jumps and labels alone and treats the hook
    code as if there is a conditional jump to it from the hook location


Example use of the new functionality, somewhere within a function:

__builtin_hook (hook1)
  {
    /* code to implement the hook */
  }


Code generated to implement it for ix86:

        .global hook1;
hook1:
        jmp hook1_after    / replace with nops to enable the hook
        jmp hook1_bp
hook1_after:


        .section .fixup
hook1_bp:
        / code to implement the hook goes here
        jmp     hook1_after
        .previous

Does this sound like something that could be done, and something that
would be reasonable to add to GCC?

Janis


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]