<p>In <a href="https://flatassembler.github.io/AEC_specification.html#Assembly" rel="nofollow noreferrer">my programming language that compiles to WebAssembly</a>, I am using the following syntax for inline assembly:</p>
<pre><code>Function sqrt(Decimal32 x) Which Returns Decimal32 Does {
  If USE_WEBASSEMBLY_SQRT_INSTRUCTION Then {
    Return asm_f32(
        R&quot;multiline((f32.sqrt
    (f32.load
        %x ;;The compiler will replace &quot;%x&quot; with assembly code representing a pointer to the variable &quot;x&quot;.
    )
))multiline&quot;);
  }
  EndIf;
</code></pre>
<p>So, I am using C++-like multiline strings, and you can reference all variables in the scope within inline assembly by prefixing them with <code>%</code> (so, there is no way to limit which variables the inline assembly has access to and whether some should be read-only).</p>
<p>So, what are some other approaches to inline assembly? What are their pros and cons?</p>

<syntax><webassembly><assembly>
What are some different approaches for inline assembly, and what are their pros and cons?
<p>In Rust, inline assembly is introduced through the <a href="https://doc.rust-lang.org/reference/inline-assembly.html" rel="noreferrer"><code>asm!</code> macro</a>. The instructions are specified as separate strings with variables represented as <code>{foo}</code> within the instructions, and then defined in the end along with their storage classes and linked Rust variables.</p>
<p>Here's the example from the linked docs:</p>
<pre class="lang-rust prettyprint-override"><code>use std::arch::asm;

// Multiply x by 6 using shifts and adds
let mut x: u64 = 4;
unsafe {
    asm!(
        &quot;mov {tmp}, {x}&quot;,
        &quot;shl {tmp}, 1&quot;,
        &quot;shl {x}, 2&quot;,
        &quot;add {x}, {tmp}&quot;,
        x = inout(reg) x,
        tmp = out(reg) _,
    );
}
assert_eq!(x, 4 * 6);
</code></pre>

CC BY-SA 4.0
2023-06-28T09:31:01.303Z
2023-06-28T09:31:01.303Z
abel1502
CC BY-SA 4.0
2023-06-28T12:38:10.480Z
An actual optimizer [uses `lea`, not `shl`](https://godbolt.org/z/bYPq1fz4T) :P
Bbrk24
CC BY-SA 4.0
2023-06-28T12:56:20.253Z
@Bbrk24 The example is taken as is from the rust docs - you should submit this as an issue to them. Although I suspect this optimization is probably carried out at a later stage and likely affects the inline assembly as well
abel1502
CC BY-SA 4.0
2023-07-28T11:40:05.083Z
Inline assembler tends to be for situations when you know something that the compiler doesn't, so the optimiser might be well advised not to touch this.
gnasher729
CC BY-SA 4.0
2023-07-28T13:22:05.487Z
@gnasher729 I know that in C++ inline assembly is optimized by default, unless you prefix it with `volatile`. Since (most) optimizations are conservative, there's no harm in doing them unless there's some hardware-specific side effects of your code, which the compiler really has no clue of
abel1502
CC BY-SA 4.0
2023-07-28T14:58:10.250Z
@abel1502, you should say which C++ compiler you're talking about, as that obviously doesn't apply to standard C++...
Toby Speight
CC BY-SA 4.0
2023-07-28T15:57:52.570Z
@TobySpeight Fair point - I'm referring to gcc and clang as the de-facto standard. I've actually just checked the docs, and MSVC explicitly states they never optimize inline assembly, so this might be a less common approach than I believed
abel1502
<p>You want to keep it simple for the application developer.</p>
<p>The best IMHO is &quot;intrinsics&quot;. Usually I don't really want to use assembler, what I want is to use a feature of the processor that isn't available from a high level language. Intrinsics are used very similar to ordinary functions. The compiler knows what these functions do and how to translate them to machine code, just as it knows how to translate + - etc. into machine code. If it generates code for different machines, it knows how to do this, so I can use the same intrinsic on PowerPC, x86, or ARM without any changes to my own code.</p>
<p>For example processors tend to have an instruction that counts the number of bits that are set in a 64 bit integer. Many compilers have an intrinsic &quot;popcount&quot; which returns the number of bits that are set. On any machine. Integrated into the optimisation process. And the compiler can translate popcount (100) -&gt; 3 because 100 = 0x40 + 0x20 + 0x04. It can remove the instructions from loops, can move it around to the most efficient place and so on. And of course it's easy to use, and my code stays readable.</p>
<p>And it's very easy to learn. Every time I use a different method, I'll have to dig out some manual or some sample code. Obviously you are restricted to things that the compiler's authors foresaw. The Rust example for multiplying by six using shift + add cannot be done with in intrinsic. (But I have seen intrinsics for rotate instead of shift instructions which are not part of any language).</p>
<p>Now if your code needs to run on a brand new architecture, and an intrinsic was supported on architecture A, B, and C, but not on the brand new X, then if you are lucky the intrinsic function just doesn't exist. Which means that for your brand new architecture you can write an ordinary function that does what the intrinsic does, just likely a lot slower. Take popcount() as an example: It's not hard to implement by hand, it's just not very efficient. Probably 20-30 instructions for 64 bit. And using it may be sub-optimal, but at least it works. Or there may be an instruction &quot;approximate square root of 1/x&quot; which is a nice building block for a very fast square root. Obviously you can just write a function that calls 1 / sqrt(x). You throw away the advantages, but at least it works. YMMV.</p>

CC BY-SA 4.0
2023-07-28T11:48:11.560Z
2023-07-28T21:39:40.193Z
2023-07-28T21:39:40.193Z
gnasher729
CC BY-SA 4.0
2023-07-28T14:56:35.703Z
The downside to intrinsics comes when you're targeting a relatively new ISA - you might have to contribute the implementation for the new intrinsic if it's not already been done.  Inline assembly gives greater control over the instruction sequence emitted (albeit at the cost of having to hand-optimise), so having both available is the best option, IMO.
Toby Speight
CC BY-SA 4.0
2023-07-29T14:00:33.157Z
You still need an escape hatch for unsupported intrinsics. What if intrinsics could be implemented as assembly snippets? pseudocode: `#define popcount __asm__("popcount %x" : "=r" (int return) : "r" (int arg0) : "pure")`
Criticize SE actions means ban
<p>The options I've seen are:</p>
<ol>
<li>Inline asm with dedicated syntax.</li>
<li>Variants of text based asm.</li>
<li>Intrinsics</li>
<li>Inline asm with uniform syntax.</li>
</ol>
<p>(1) is usually only feasible if there is a limited amount of processors you're writing the compiler for, as following asm conventions for each processor is essentially parsing a separate language. Repeating that for every processor takes a lot of effort. In addition, if this inline asm isn't smart enough in detecting clobbers etc, then it will generate worse asm than something like (2). Since inline asm is only used when perf is paramount, it's a bad idea to have it perform badly.</p>
<p>(2) This is what GCC/Clang does: slap some text in there to just shuffle away to the assembler. Typically there are cryptic ways to indicate how to pass data in and out of the asm block (the constraints). Rust and others are looking at making those constraints a little less cryptic, while keeping the whole thing with asm passed as text. The downside is that text just isn't a great solution. If you have asm in your language through text, you should really do verification or it will break at codegen if you use something like LLVM. But verifying it is quite a lot of work so...</p>
<p>(3) This is what Microsoft picked for x64 (after using 1 previously). Intrinsics work well with the compiler if the compiler understands the intrinsics. So good optimization and it's more high level than asm, making it easier to use them cross platform. That said, it's a lot less control than asm. The way I hear it people in general prefer (1) and (2) over intrinsics.</p>
<p>(4) I haven't seen this much. I know some fledgling languages are doing this, including my own C3 language. The idea is to have a sufficiently simple grammar for the asm so that &quot;all&quot; types of asm fit the same grammar. The frontend then knows clobbers etc for all instructions and generate valid (checked) text asm for the backend (alternatively the machine code directly for custom backends). The advantage is that each processor type is reduced to a set of instruction names and corresponding argument patterns and clobbers. It's more work than (2) if no text analysis is done, but it's less work than (1) to implement and support different targets.</p>

CC BY-SA 4.0
2023-07-08T00:24:11.773Z
2023-07-08T00:24:11.773Z
Nuoji
# What are some different approaches for inline assembly, and what are their pros and cons?
Posted by: username_0 on 2023-06-28
In my programming language that compiles to WebAssembly, I am using the following syntax for inline assembly:
Function sqrt(Decimal32 x) Which Returns Decimal32 Does {
  If USE_WEBASSEMBLY_SQRT_INSTRUCTION Then {
    Return asm_f32(
        R"multiline((f32.sqrt
    (f32.load
        %x ;;The compiler will replace "%x" with assembly code representing a pointer to the variable "x".
    )
))multiline");
  }
  EndIf;

So, I am using C++-like multiline strings, and you can reference all variables in the scope within inline assembly by prefixing them with % (so, there is no way to limit which variables the inline assembly has access to and whether some should be read-only).
So, what are some other approaches to inline assembly? What are their pros and cons?
**Tags:** syntax webassembly assembly
---
## 3 answers
### Answer by username_6 on 2023-07-08. Score: 4 | Accepted answer
The options I've seen are:

1. Inline asm with dedicated syntax.
2. Variants of text based asm.
3. Intrinsics
4. Inline asm with uniform syntax.

(1) is usually only feasible if there is a limited amount of processors you're writing the compiler for, as following asm conventions for each processor is essentially parsing a separate language. Repeating that for every processor takes a lot of effort. In addition, if this inline asm isn't smart enough in detecting clobbers etc, then it will generate worse asm than something like (2). Since inline asm is only used when perf is paramount, it's a bad idea to have it perform badly.

(2) This is what GCC/Clang does: slap some text in there to just shuffle away to the assembler. Typically there are cryptic ways to indicate how to pass data in and out of the asm block (the constraints). Rust and others are looking at making those constraints a little less cryptic, while keeping the whole thing with asm passed as text. The downside is that text just isn't a great solution. If you have asm in your language through text, you should really do verification or it will break at codegen if you use something like LLVM. But verifying it is quite a lot of work so...

(3) This is what Microsoft picked for x64 (after using 1 previously). Intrinsics work well with the compiler if the compiler understands the intrinsics. So good optimization and it's more high level than asm, making it easier to use them cross platform. That said, it's a lot less control than asm. The way I hear it people in general prefer (1) and (2) over intrinsics.

(4) I haven't seen this much. I know some fledgling languages are doing this, including my own C3 language. The idea is to have a sufficiently simple grammar for the asm so that "all" types of asm fit the same grammar. The frontend then knows clobbers etc for all instructions and generate valid (checked) text asm for the backend (alternatively the machine code directly for custom backends). The advantage is that each processor type is reduced to a set of instruction names and corresponding argument patterns and clobbers. It's more work than (2) if no text analysis is done, but it's less work than (1) to implement and support different targets.
---
### Answer by username_1 on 2023-06-28. Score: 6
In Rust, inline assembly is introduced through the asm! macro. The instructions are specified as separate strings with variables represented as {foo} within the instructions, and then defined in the end along with their storage classes and linked Rust variables.
Here's the example from the linked docs:
use std::arch::asm;

// Multiply x by 6 using shifts and adds
let mut x: u64 = 4;
unsafe {
    asm!(
        "mov {tmp}, {x}",
        "shl {tmp}, 1",
        "shl {x}, 2",
        "add {x}, {tmp}",
        x = inout(reg) x,
        tmp = out(reg) _,
    );
}
assert_eq!(x, 4 * 6);
> **Comments:**
> **username_2** (2023-06-28): An actual optimizer [uses `lea`, not `shl`](https://godbolt.org/z/bYPq1fz4T) :P  
> **username_1** (2023-06-28): @Bbrk24 The example is taken as is from the rust docs - you should submit this as an issue to them. Although I suspect this optimization is probably carried out at a later stage and likely affects the inline assembly as well  
> **username_3** (2023-07-28): Inline assembler tends to be for situations when you know something that the compiler doesn't, so the optimiser might be well advised not to touch this.  
> **username_1** (2023-07-28): @gnasher729 I know that in C++ inline assembly is optimized by default, unless you prefix it with `volatile`. Since (most) optimizations are conservative, there's no harm in doing them unless there's some hardware-specific side effects of your code, which the compiler really has no clue of  
> **username_4** (2023-07-28): @abel1502, you should say which C++ compiler you're talking about, as that obviously doesn't apply to standard C++...  
> **username_1** (2023-07-28): @TobySpeight Fair point - I'm referring to gcc and clang as the de-facto standard. I've actually just checked the docs, and MSVC explicitly states they never optimize inline assembly, so this might be a less common approach than I believed  
---
### Answer by username_3 on 2023-07-28. Score: 1
You want to keep it simple for the application developer.

The best IMHO is "intrinsics". Usually I don't really want to use assembler, what I want is to use a feature of the processor that isn't available from a high level language. Intrinsics are used very similar to ordinary functions. The compiler knows what these functions do and how to translate them to machine code, just as it knows how to translate + - etc. into machine code. If it generates code for different machines, it knows how to do this, so I can use the same intrinsic on PowerPC, x86, or ARM without any changes to my own code.

For example processors tend to have an instruction that counts the number of bits that are set in a 64 bit integer. Many compilers have an intrinsic "popcount" which returns the number of bits that are set. On any machine. Integrated into the optimisation process. And the compiler can translate popcount (100) -> 3 because 100 = 0x40 + 0x20 + 0x04. It can remove the instructions from loops, can move it around to the most efficient place and so on. And of course it's easy to use, and my code stays readable.

And it's very easy to learn. Every time I use a different method, I'll have to dig out some manual or some sample code. Obviously you are restricted to things that the compiler's authors foresaw. The Rust example for multiplying by six using shift + add cannot be done with in intrinsic. (But I have seen intrinsics for rotate instead of shift instructions which are not part of any language).

Now if your code needs to run on a brand new architecture, and an intrinsic was supported on architecture A, B, and C, but not on the brand new X, then if you are lucky the intrinsic function just doesn't exist. Which means that for your brand new architecture you can write an ordinary function that does what the intrinsic does, just likely a lot slower. Take popcount() as an example: It's not hard to implement by hand, it's just not very efficient. Probably 20-30 instructions for 64 bit. And using it may be sub-optimal, but at least it works. Or there may be an instruction "approximate square root of 1/x" which is a nice building block for a very fast square root. Obviously you can just write a function that calls 1 / sqrt(x). You throw away the advantages, but at least it works. YMMV.
> **Comments:**
> **username_4** (2023-07-28): The downside to intrinsics comes when you're targeting a relatively new ISA - you might have to contribute the implementation for the new intrinsic if it's not already been done.  Inline assembly gives greater control over the instruction sequence emitted (albeit at the cost of having to hand-optimise), so having both available is the best option, IMO.  
> **username_5** (2023-07-29): You still need an escape hatch for unsupported intrinsics. What if intrinsics could be implemented as assembly snippets? pseudocode: `#define popcount __asm__("popcount %x" : "=r" (int return) : "r" (int arg0) : "pure")`  
---
