-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Support using const pointers in asm const
operand
#138618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
r? codegen
This comment has been minimized.
This comment has been minimized.
r? compiler-errors
ffba8e8
to
92c0cc4
Compare
Some changes occurred in compiler/rustc_codegen_ssa
Some changes occurred in compiler/rustc_codegen_gcc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add more doc comment explaining the significance of the difference between Interpolate
and Const
for backends. It begs the question for why we ever turn const operands into strings, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some explaination. Technically we should be able to use Const
for integers, although I am a bit conservative here and don't want to change how existing things work.
This might also be useful to have in the future if we decided to add interpolate "some CTFE string to be interpolated"
new type of operand.
☔ The latest upstream changes (presumably #140415) made this pull request unmergeable. Please resolve the merge conflicts.
@rustbot author
Reminder, once the PR becomes ready for a review, use @rustbot ready
.
92c0cc4
to
c7a2301
Compare
This comment has been minimized.
This comment has been minimized.
c7a2301
to
4240004
Compare
Some changes occurred in compiler/rustc_codegen_cranelift
cc @bjorn3
☔ The latest upstream changes (presumably #141668) made this pull request unmergeable. Please resolve the merge conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// x86_64: mov al, byte ptr [{{.*}}anon{{.*}}]
What was the expansion you observed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mov al, byte ptr [.Lanon.2e8c0013b01edcd3779e8174c2338a00.0]
This is intended for supporting passing arbitrary CTFE const into inline assembly.
`global_asm!` themselves don't need symbol names; they currently only have a symbol name during mono collecion to identify them to partitioning algorithm. However it will have shims generated under it which will need unique symbol names. The name themselves ultimately doesn't matter, so they're generated like other shim instances.
This is currently a no-op, but will be useful when const in `global_asm!` can be pointers.
81a4b3e
to
8da54fc
Compare
@bors r+ rollup
@bors rollup-
Support using const pointers in asm `const` operand Implements #128464 This adds support of const pointers for asm `const` in addition to plain integers. The inline `asm!` support is implemented using `i` constraint, and the `global_asm!` and `naked_asm!` support is implemented by inserting `symbol + offset` and make `symbol` compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm. The feature is also implemented for GCC backend but it's untested.
The job dist-i586-gnu-i586-i686-musl
failed! Check out the build log: (web) (plain enhanced) (plain)
Click to see the possible cause of the failure (guessed by this bot)
---- [ui] tests/ui/asm/const-refs-to-static.rs stdout ----
error: test compilation failed although it shouldn't!
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/asm/const-refs-to-static.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=i586-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/asm/const-refs-to-static" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/i586-unknown-linux-gnu/native/rust-test-helpers" "-Clinker=i586-unknown-linux-gnu-gcc"
stdout: none
--- stderr -------------------------------
error: invalid operand for inline asm constraint 'i'
##[error] --> /checkout/tests/ui/asm/const-refs-to-static.rs:17:20
|
LL | unsafe { asm!("/* {} */", const addr_of!(FOO)) };
| ^^^^^^^^
error: aborting due to 1 previous error
------------------------------------------
💔 Test failed - checks-actions
Looks like the i
constraint doesn't work for GOT-rebased relocation, which x86 use for PIE and we enable PIE by default. We need to either:
- Say this works only with static relocation-model on x86, or
- Generate symbol + offset ourselves, like
global_asm
implementation.
...-errors Support using const pointers in asm `const` operand Implements rust-lang#128464 This adds support of const pointers for asm `const` in addition to plain integers. The inline `asm!` support is implemented using `i` constraint, and the `global_asm!` and `naked_asm!` support is implemented by inserting `symbol + offset` and make `symbol` compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm. The feature is also implemented for GCC backend but it's untested.
...-errors Support using const pointers in asm `const` operand Implements rust-lang#128464 This adds support of const pointers for asm `const` in addition to plain integers. The inline `asm!` support is implemented using `i` constraint, and the `global_asm!` and `naked_asm!` support is implemented by inserting `symbol + offset` and make `symbol` compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm. The feature is also implemented for GCC backend but it's untested.
Oh, it failed bors but was not marked
@bors r-
☔ The latest upstream changes (presumably #144249) made this pull request unmergeable. Please resolve the merge conflicts.
Looks like the i constraint doesn't work for GOT-rebased relocation
I've been playing around with this a bit, and I think the i
operand works as long as using the symbol name would work. For example, this breaks:
#include <stdio.h> static const int FORTY_TWO = 42; int main(void) { const int *a; __asm__ ( "movabs %1, %0" : "=r" (a) : "i" (&FORTY_TWO) ); printf("%p\n", (void *)a); return 0; }
/usr/bin/ld: /tmp/ccptrDbi.o: warning: relocation in read-only section `.text'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
But when we make it rip-relative, it works:
#include <stdio.h> static const int FORTY_TWO = 42; int main(void) { const int *a; __asm__ ( "movabs %1 - ., %0" : "=r" (a) : "i" (&FORTY_TWO) ); printf("%p\n", (void *)a); return 0; }
0xec3
We can even force it to generate a GOT-entry with the address of FORTY_TWO
and then look up the value through it:
#include <stdio.h> static const int FORTY_TWO = 42; int main(void) { const int **a; __asm__ ( "leaq (%c1)@GOTPCREL(%%rip), %0" : "=r" (a) : "i" (&FORTY_TWO) ); printf("%i\n", **a); return 0; }
42
This is exactly the same scenario as when writing FORTY_TWO
would work as far as I can tell. Is there something I'm missing?
It works in x64 but not in x86.
EDIT: Looks like it works in GCC for not Clang/LLVM: https://godbolt.org/z/YodbxxMeY
Ah, that's what I get for testing it with gcc.
Uh oh!
There was an error while loading. Please reload this page.
Implements RFC#3848 with tracking issue #128464
This adds support of const pointers for asm
const
in addition to plain integers.The inline
asm!
support is implemented usingi
constraint, and theglobal_asm!
andnaked_asm!
support is implemented by insertingsymbol + offset
and makesymbol
compiler-used. For unnamed consts, it will create additional internal & hidden symbols so that they can be referenced by global_asm.The feature is also implemented for GCC backend but it's untested.