Skip to content

Use __asm__ attribute to specify symbol name lead the function prototype change? Is legal? #72843

Closed
@hstk30-hw

Description

@hstk30-hw

https://godbolt.org/z/hr4z7Wbv3

Has follow code, fail with compile command -O1 in arm32le.

typedef __attribute__((aligned (8))) int alignedint;

alignedint a = 11;

extern void abort (void);
extern int memcmp (const void *s1, const void *s2, unsigned int n);

__attribute__((naked))  void dumpregs () __asm__("myfunc");

__attribute__((naked))  void dumpregs ()
{
  asm(
      "mov      ip, sp\n\t"
      "stmfd    sp!, {r0-r3}\n\t"
      "mov      r0, sp\n\t"
      "stmfd    sp!, {ip, r14}\n\t"
      "bl       testfunc\n\t"
      "ldmfd    sp!, {r0, r14}\n\t"
      "mov      sp, r0\n\t"
      "bx       lr");
}

void testfunc(char* stack)
{
  alignedint __x = a;
  if (memcmp(&__x, stack+0, sizeof(alignedint)) != 0)
    abort();

  return;
}

void myfunc(alignedint) ;

int main()
{
  myfunc(a);
  return 0;
}

I see the arguments are removed in the pass InstCombine by this function

https://github.com/llvm/llvm-project/blob/27c98958c067c341dd3f65b7218c376d333fbed5/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp#L3778C1-L3781C68

I gusee there are caused by two reasons:

  1. __asm lead to myfunc function prototype changed from void myfunc(alignedint) ; to void myfunc(); , so IC opt it.
  2. I'm not sure there is a regular way to generate this IR? https://godbolt.org/z/1efKn1bje If have, maybe we should reject naked function in InstCombinerImpl::transformConstExprCastCall

Metadata

Metadata

Assignees

No one assigned

    Labels

    llvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions