-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
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
base: main
Are you sure you want to change the base?
Conversation
|
r? codegen |
This comment has been minimized.
This comment has been minimized.
|
r? compiler-errors |
|
Some changes occurred in compiler/rustc_codegen_ssa Some changes occurred in compiler/rustc_codegen_gcc |
| out_place: Option<PlaceRef<'tcx, B::Value>>, | ||
| }, | ||
| Const { | ||
| Interpolate { |
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 |
This comment has been minimized.
This comment has been minimized.
|
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. |
tests/assembly-llvm/asm/x86-types.rs
Outdated
| // and on x86 PIC symbol can't be constant. | ||
| // x86_64-LABEL: const_ptr: | ||
| // x86_64: #APP | ||
| // x86_64: mov al, byte ptr [{{.*}}anon{{.*}}] |
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]
|
☔ The latest upstream changes (presumably #144249) made this pull request unmergeable. Please resolve the merge conflicts. |
I've been playing around with this a bit, and I think the #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;
}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;
}We can even force it to generate a GOT-entry with the address of #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;
}This is exactly the same scenario as when writing |
|
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. |
This is currently a no-op, but will be useful when const in `global_asm!` can be pointers.
8da54fc to
5b342bc
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
5b342bc to
5710932
Compare
This comment has been minimized.
This comment has been minimized.
| }; | ||
|
|
||
| let offset = offset.bytes(); | ||
| if offset != 0 { format!("{symbol}+{offset}") } else { symbol } |
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.
What about negative offset? Should it be symbol-offset instead of the current symbol+-offset?
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.
Hmm, offset is unsigned, so I elected to just emit +{offset}. If the offset is negative, CTFE will just produce 2's complement values as unsigned (which is okay for the wrapping semantics, and note that you have to use wrapping semantics to get pointer out of bound in the first place).
Assemblers seem to be happy with +0xFFFFFFFFFFFFFFFF instead of -1; do you think it's worth changing the generated code so we always treat offset as signed?
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.
IMO -1 seems a lot safer than +0xFFFFFFFFFFFFFFFF since we don't have to worry about the bitwidth of the addition.
5710932 to
085397e
Compare
|
Quite a few changes for this new version:
|
This comment has been minimized.
This comment has been minimized.
Currently global_asm already have symbol names when using v0 scheme, this makes them obtain symbols with legacy scheme too.
This gives the asm-const code the basic ability to deal wiht pointer and provenances, which lays the ground work for asm_const_ptr. Note that `SymStatic` is not removed, as it supports `#[thread_local]` statics where CTFE does not.
With the previous commit, now we can see there are some code duplication for the handling of `GlobalAlloc` inside backends. Do some clean up to unify them.
CTFE pointers created via type ID, `without_provenance` or pointers to const ZSTs can now be codegenned with all 3 backends. These pointers are generated in the same way as integers.
The backend now fully supports codegen of const pointers, remove the block inside typeck behind a new feature gate. Tests are also added.
085397e to
45aa236
Compare
Implements RFC#3848 with tracking issue #128464
This adds support of const pointers for asm
constin addition to plain integers.The inline
asm!support is implemented usingiconstraint, and theglobal_asm!andnaked_asm!support is implemented by insertingsymbol + offsetand makesymbolcompiler-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.