[llvm-dev] [RFC] Potential extension to asm statement functionality

via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 12 12:32:13 PST 2019

This tactic has been tried before, I'm pretty sure.  It's an attempt to do simple instrumentation without having to hack the compiler itself (you do some manual coding or preprocessing, and poof you get useful info in your object file).  But our compiler is too clever.

Some sort of "No Touchee!" constraint would help this use-case a lot.

From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Snider, Todd via llvm-dev
Sent: Tuesday, February 12, 2019 3:06 PM
To: Eli Friedman; llvm-dev at lists.llvm.org
Subject: Re: [llvm-dev] [RFC] Potential extension to asm statement functionality

The team I am working with is using asm statements containing label definitions as a way of instrumentation so that when an application is loaded into their debug and test framework, the labels will cause breakpoints to be set at strategic points where they can query the state of the processor that the application is running on.

~ Todd

From: Eli Friedman [mailto:efriedma at quicinc.com]
Sent: Tuesday, February 12, 2019 1:36 PM
To: Snider, Todd; llvm-dev at lists.llvm.org
Subject: [EXTERNAL] RE: [llvm-dev] [RFC] Potential extension to asm statement functionality

Can you go into a bit more detail about why someone would want to do this, as opposed to just writing a file scope inline asm, or a separate file in assembly?  I can't think of any practical use for the fact that the label is "inside" the function body.


From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of Snider, Todd via llvm-dev
Sent: Tuesday, February 12, 2019 10:18 AM
To: llvm-dev at lists.llvm.org
Subject: [EXT] [llvm-dev] [RFC] Potential extension to asm statement functionality

Suppose a programmer wants to inject their own global label definition into the body of a function with some guarantee that it will not be removed by the compiler.

One way to do this is to define a global label with an asm statement knowing that the asm statement will not be invoked until after the compiler's optimization passes have run, but the following case demonstrates that a label defined with an asm statement is still susceptible to being duplicated:

#include <stdint.h>
uint32_t f(uint32_t x);
uint32_t g(uint32_t x);

uint32_t f(uint32_t x) {
    uint32_t returnValue = g(x);

    if (returnValue > 0U) {
        returnValue = 0x40000000;
    else {
        returnValue = 0x80000000;
    __asm __volatile__ ("\t.global my_hook_fcn\nmy_hook_fcn:\n");
    return returnValue;

uint32_t g(uint32_t x) {
    return x >> 1U;

If the above definition of f() is compiled with optimization at level 1 or higher, the TailDuplication optimization pass will duplicate and move the asm statement and return up into the if block and the else block before the asm statement is invoked. When the now duplicate asm statements are later invoked, the compiler will detect a symbol redefinition error.

To address this situation, the asm statement functionality could be extended to comprehend whether it contains a label definition, and if it does, to disallow duplication of the asm statement.

There are a couple of different approaches that could be taken to implement this:

  1.  Parse the content of the assembly string argument to the asm statement in the compiler front-end (during EmitAsmStmt(), for example) to determine if it contains a label definition, and if it does set the isNotDuplicable flag on the INLINEASM record that is created to represent the asm statement in the IR. To date, there is no precedence for processing the content of the assembly string argument until the asm statement is invoked before the integrated  assembler starts processing the generated machine code.
  2.  Add a label constraint to the input and output operand syntax for asm statements. i.e.
__asm __volatile__ ("\t.global\t%0\n%0:\n" : "lbl" (my_hook_fcn));
The "lbl" constraint would tell the compiler to mark the asm statement as isNotDuplicable when an INLINEASM record is created to represent it

There are alternatives to using an asm statement for this purpose, but I wondered if such an extension would be useful/valuable to the wider community.

Thoughts? Opinions?

Todd Snider

Compiler Group
Texas Instruments Incorporated
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190212/c970cfc4/attachment.html>

More information about the llvm-dev mailing list