Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hstk30-hw opened this issue Nov 20, 2023 · 1 comment

Comments

@hstk30-hw
Copy link
Contributor

hstk30-hw commented Nov 20, 2023

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
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 20, 2023

@llvm/issue-subscribers-clang-codegen

Author: None (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

@hstk30-hw hstk30-hw added the clang Clang issues not falling into any other category label Dec 28, 2023
hstk30-hw added a commit that referenced this issue Jan 1, 2024
Fix this issue #72843 .

For naked function, assembly might be using an argument, or otherwise
rely on the frame layout, so don't transformConstExprCastCall
@EugeneZelenko EugeneZelenko added llvm:instcombine and removed clang Clang issues not falling into any other category clang:codegen labels Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants