This is an archive of the discontinued LLVM Phabricator instance.

Allow variables with asm labels in naked functions
ClosedPublic

Authored by nikola on Sep 2 2016, 7:35 AM.

Details

Reviewers
hans
compnerd
rnk
Summary

I think the current mode is too restrictive, it will emit error for any statement inside a naked function. Code I'm trying to compile for ARM declares registers as variables to improve readability and passes them as input operands to inline assembly.

register uint32_t Something asm("rax");

Diff Detail

Event Timeline

nikola updated this revision to Diff 70161.Sep 2 2016, 7:35 AM
nikola retitled this revision from to Allow variables with asm labels in naked functions.
nikola updated this object.
nikola added reviewers: hans, rnk, compnerd.
nikola added a subscriber: cfe-commits.
hans edited edge metadata.Sep 2 2016, 9:41 AM

I think this is reasonable. Just a few comments:

lib/Sema/SemaDecl.cpp
11796

I think we'll need to check for multiple declarators here:

register int x asm("eax"), y asm("ebx");

And do we need to check for an initializer? For example, the following should not be allowed:

register int x asm("eax") = g();

It would also be nice to have a comment explaining why these are Ok but not other declarations.

rnk edited edge metadata.Sep 2 2016, 9:57 AM

Won't the mid-level optimizer turn these register variables into undefs, and we'll end up with this kind of IR?

void f() {
  int register var asm ("eax") ;
  asm volatile ("add %%eax, %0\n\tret" : : "r"(var));
}
->
define void @"\01?f@@YAXXZ"() local_unnamed_addr #0 {
entry:
  tail call void asm sideeffect "add %eax, $0\0A\09ret", "{eax},~{dirflag},~{fpsr},~{flags}"(i32 undef) #1, !srcloc !2
  ret void
}

I guess it's OK so long as we don't codegen undef to anything, but it could go badly.

nikola updated this revision to Diff 70985.Sep 12 2016, 3:03 AM
nikola edited edge metadata.

This should address Hans' comments, as for the code get I have no idea. I was hoping someone more knowledgeable would tell me if this made sense or not?

hans accepted this revision.Sep 12 2016, 10:54 AM
hans edited edge metadata.

lgtm

I'm a little bit worried that we've started down a slippery slope of deciding what is and isn't allowed in naked functions (for example, some initializers could be allowed if we're sure they don't require stack). But I suppose this isn't making anything worse, and if it helps compile real-world code, that's good.

This revision is now accepted and ready to land.Sep 12 2016, 10:54 AM
nikola closed this revision.Sep 13 2016, 12:11 AM

r281298