File/module scope inline assembly

I am trying to support an experimental DSL that uses non-C identifiers, but I want to write the implementation of the runtime support libraries for this DSL in C. The compiler is built using the LLVM v5.0.0 release branch.

To do this I thought I could simply write:

int foo() { return 42; } // The C implementation

asm(".alias nonCname foo"); // Make the non-C name be a synonym for the C implementation

If I compile with ‘-S –emit-llvm’, the LL file contains:

module asm “.alias nonCname foo”

define i32 @foo() local_unnamed_addr #0 {

ret i32 42

}

That is, the inline-assembly precedes the function definition. The resulting assembly code for the target also has the ‘.alias nonCname foo’ before the definition for the function ‘foo’. When I edit the LL file to place the inline-assembly after the function definition, it makes no difference to the emitted assembly code for the target, it is still placed first.

Our assembler requires that the symbol being aliased has been defined before the ‘.alias’ directive; but I can’t get LLVM to do this, and it aggregates all file-scoped (or module-scoped) inline assembly before the code generated for the functions and data.

Since the semantics of inline-assembly are very murky, I don’t know if this is a bug or by-design.

Is there anyway of writing C code that will preserve the relative ordering of file-scoped function, data and inline-assembly definitions?

Thanks,

MartinO

This is roughly by design in LLVM - though other compilers have other designs.

LLVM does not preserve the relative ordering of inline assembly and other IR (functions, globals, etc).

“Is there anyway of writing C code that will preserve the relative ordering of file-scoped function, data and inline-assembly definitions?” - using an LLVM-based compiler, basically: no. Sorry :confused:

gcc/clang have a syntax for that which works fine for me:

$ cat t.c
void thumb_up(void) asm(":+1:");

void thumb_up(void) {
}
$ clang -O3 -S t.c -o -
...
  .globl ":+1:"
  .p2align 4, 0x90
":+1:": ## @"\01\F0\9F\91\8D"
...

- Matthias

Hi Matthias,

That trick didn’t work for me, but it got me thinking, and the following “does” work for me:

int foo() {

asm(".alias nonCname foo");

return 42;

}

and our assembler is quite happy to accept the output with the desired outcome J

Thanks for the pointer,

MartinO