[llvm] [LangRef] inline asm: the instructions are treated opaquely (PR #157080)

Ralf Jung via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 15 06:49:47 PDT 2025


RalfJung wrote:

> @RalfJung how about this: `The compiler may analyze the contents of the inline asm string in order to optimize surrounding code, but the compiler will always emit the inline asm string verbatim. Additionally, the compiler will not use any analysis of the inline asm string to optimize the code in violation of any flags, operand constraints or attributes placed on the inline asm call.`

That's not enough for self-patching code. For instance, if the asm block writes a constant to a given register, it is crucial that the compiler is *not* allowed to use this information for constant-propagation (since the code could actually be changed later to use a different constant).

If we do allow any kind of analysis of the template string, we have to restrict it to something like magic comments, and we should link to a place where such magic comments will be documented.

So, something like: "The compiler's understanding of the semantics of the
expression comes only from the list of operand constraints and the flags -- not
the contents of the template string. In particular, no optimizations or analyses
will be performed based on the contents of that string.  The only exception to
this are declarations embedded in magic comments as documented $here."

Maybe we should also add that this is explicitly intended to allow self-modifying code?
"This ensures correct program behavior if the assembly code emitted by this
expression is altered later, e.g. via self-modifying code. The code may be changed arbitrarily
as long as the requirements of operand constraints and flags (and, if applicable, magic comments)
are still upheld".

>  For each of these suggestions, if the user used, for instance, GCC, which does not support this backend feature in its AMDGPU backend, would your suggested directive be ignored, or would the user encounter a compiler error?

C has existing facilities for adjusting the code to the abilities of the current compiler.

https://github.com/llvm/llvm-project/pull/157080


More information about the llvm-commits mailing list