Issue 138272
Summary Add builtin/intrinsic to get current instruction pointer?
Labels new issue
Assignees
Reporter nikic
    The linux kernel has a macro like this:
```
#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
```

This is used to get the current instruction pointer for error reporting purposes. It is not used to perform any control flow transitions to the label.

This usage is problematic, because it violates LLVM's requirements for `blockaddress` usage:

> This value only has defined behavior when used as an operand to the ‘[indirectbr](https://llvm.org/docs/LangRef.html#i-indirectbr)’ or for comparisons against null. Pointer equality tests between labels addresses results in undefined behavior — though, again, comparison against null is ok, and no label is equal to the null pointer. This may be passed around as an opaque pointer sized value as long as the bits are not inspected. This allows `ptrtoint` and arithmetic to be performed on these values so long as the original value is reconstituted before the `indirectbr` instruction.

Basically, LLVM only actually supports block labels when used together with computed goto. If block labels are used without computed goto, the label can be optimized away and replaced with a dummy address. We have just enough hacks to prevent this from happening in common cases, but I feel like this is architecturally not sustainable.

I am wondering if it would make sense to add something like `__builtin_instruction_pointer()` / `@llvm.instruction.pointer()` to obtain the current instruction pointer? Where "current" is necessarily a best-effort approximation.

@nathanchance @kees Would that be suitable for the kernel use case?

cc @efriedma-quic 
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to