Skip to content

[WIP] Spiking on asm! + pointer as const #132045

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dingxiangfei2009
Copy link
Contributor

A quick a dirty implementation of #132012.
Related to #128464

This will remain as a draft. Details in discussion thread below.

@rustbot
Copy link
Collaborator

rustbot commented Oct 22, 2024

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 22, 2024
@dingxiangfei2009
Copy link
Contributor Author

dingxiangfei2009 commented Oct 22, 2024

What is working?

I am testing this change with the following smoke test

use std::arch::asm;
use std::ptr::addr_of;

#[no_mangle]
static FOO: (u8, u8) = (42, 24);

#[no_mangle]
fn inline() {
    let mut x: i64 = addr_of!(FOO) as _;
    unsafe {
        asm!(
            "lea rax, {1}",
            "movzx rax, byte ptr [rax]",
            "mov {0}, rax",
            inout(reg) x,
            const &FOO.1 as *const u8,
            out("rax") _
        )
    };
    assert_eq!(x, 24);
}

fn main() {
    inline();
}

This is however coming with a few caveats.

  • With LLVM Codegen backend, the {1:c} template is used. This however translates to a memory reference, as the disassembly correctly presents it as [FOO+1]. Other than that, objdump shows a correct machine code in the object file.
  • All our existing tests on the sym macro word are in fact all negative. Without this change, working with sym will not produce a runnable test fixture easily without tweaks on compiler flags. To get this example to work, I am using -Crelocation-model=static -Clinker=clang. A correct way to compose a proper test around this and sym macro word is still in progress as part of an ongoing investigation or exploration. 🙇

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check-tidy failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/

# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test \
           --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint,cpp:fmt
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
#    pip-compile --allow-unsafe --generate-hashes reuse-requirements.in
---
#13 2.671 Building wheels for collected packages: reuse
#13 2.672   Building wheel for reuse (pyproject.toml): started
#13 2.916   Building wheel for reuse (pyproject.toml): finished with status 'done'
#13 2.917   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132720 sha256=026f3bb0f1aa8090b861fd0a0939cb1a782396d84c8aab7875096557d637a0f6
#13 2.917   Stored in directory: /tmp/pip-ephem-wheel-cache-c9yqmgqb/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#13 2.920 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#13 3.310 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#13 3.311 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#13 3.832 Collecting virtualenv
#13 3.832 Collecting virtualenv
#13 3.873   Downloading virtualenv-20.27.0-py3-none-any.whl (3.1 MB)
#13 3.934      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.1/3.1 MB 53.2 MB/s eta 0:00:00
#13 3.991 Collecting filelock<4,>=3.12.2
#13 3.995   Downloading filelock-3.16.1-py3-none-any.whl (16 kB)
#13 4.029 Collecting platformdirs<5,>=3.9.1
#13 4.033   Downloading platformdirs-4.3.6-py3-none-any.whl (18 kB)
#13 4.051 Collecting distlib<1,>=0.3.7
#13 4.064      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 469.0/469.0 KB 75.8 MB/s eta 0:00:00
#13 4.064      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 469.0/469.0 KB 75.8 MB/s eta 0:00:00
#13 4.145 Installing collected packages: distlib, platformdirs, filelock, virtualenv
#13 4.321 Successfully installed distlib-0.3.9 filelock-3.16.1 platformdirs-4.3.6 virtualenv-20.27.0
#13 DONE 4.4s

#14 [7/8] COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
#14 DONE 0.0s
---
DirectMap4k:      198592 kB
DirectMap2M:     7141376 kB
DirectMap1G:    11534336 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 python2.7 ../x.py test            --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint,cpp:fmt
+ TIDY_PRINT_DIFF=1 python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint,cpp:fmt
    Finished `dev` profile [unoptimized] target(s) in 0.04s
##[endgroup]
downloading https://static.rust-lang.org/dist/2024-10-16/rustfmt-nightly-x86_64-unknown-linux-gnu.tar.xz
extracting /checkout/obj/build/cache/2024-10-16/rustfmt-nightly-x86_64-unknown-linux-gnu.tar.xz to /checkout/obj/build/x86_64-unknown-linux-gnu/rustfmt
---
   Compiling tidy v0.1.0 (/checkout/src/tools/tidy)
    Finished `release` profile [optimized] target(s) in 30.42s
##[endgroup]
fmt check
Diff in /checkout/compiler/rustc_codegen_gcc/src/asm.rs:5:
 use rustc_codegen_ssa::mir::operand::OperandValue;
 use rustc_codegen_ssa::mir::place::PlaceRef;
 use rustc_codegen_ssa::traits::{
-    AsmBuilderMethods, AsmCodegenMethods, BaseTypeCodegenMethods, BuilderMethods, ConstCodegenMethods, GlobalAsmOperandRef, InlineAsmOperandRef
+    AsmBuilderMethods, AsmCodegenMethods, BaseTypeCodegenMethods, BuilderMethods,
+    ConstCodegenMethods, GlobalAsmOperandRef, InlineAsmOperandRef,
 use rustc_middle::bug;
 use rustc_middle::ty::Instance;
Diff in /checkout/compiler/rustc_codegen_gcc/src/asm.rs:381:
Diff in /checkout/compiler/rustc_codegen_gcc/src/asm.rs:381:
                 InlineAsmOperandRef::SymStatic { def_id, offset } => {
                     let val = self.cx.get_static(def_id).get_address(None);
                     let val = if offset.bytes() > 0 {
-                        let offset = self.cx.const_int(self.int_type, offset.bytes().try_into().expect("offset is out of range"));
+                        let offset = self.cx.const_int(
+                            self.int_type,
+                            offset.bytes().try_into().expect("offset is out of range"),
                         val + offset
                     } else {
                         val
Diff in /checkout/compiler/rustc_codegen_gcc/src/asm.rs:388:
Diff in /checkout/compiler/rustc_codegen_gcc/src/asm.rs:388:
                     };
-                    inputs.push(AsmInOperand {
-                        constraint: "X".into(),
-                        val,
-                    });
-                    });
+                    inputs.push(AsmInOperand { constraint: "X".into(), rust_idx, val });
 
                 InlineAsmOperandRef::Const { .. } => {
                 InlineAsmOperandRef::Const { .. } => {
fmt error: Running `"/checkout/obj/build/x86_64-unknown-linux-gnu/rustfmt/bin/rustfmt" "--config-path" "/checkout" "--edition" "2021" "--unstable-features" "--skip-children" "--check" "/checkout/library/std/build.rs" "/checkout/library/std/benches/lib.rs" "/checkout/library/std/benches/hash/map.rs" "/checkout/library/std/benches/hash/set_ops.rs" "/checkout/library/std/benches/hash/mod.rs" "/checkout/library/std/tests/log-knows-the-names-of-variants-in-std.rs" "/checkout/library/std/tests/seq-compare.rs" "/checkout/library/std/tests/slice-from-array-issue-113238.rs" "/checkout/library/std/tests/minmax-stability-issue-23687.rs" "/checkout/library/std/tests/istr.rs" "/checkout/library/std/tests/run-time-detect.rs" "/checkout/library/std/tests/pipe_subprocess.rs" "/checkout/library/std/tests/windows.rs" "/checkout/library/std/tests/common/mod.rs" "/checkout/library/std/tests/eq-multidispatch.rs" "/checkout/library/std/tests/switch-stdout.rs" "/checkout/library/std/tests/volatile-fat-ptr.rs" "/checkout/library/std/tests/env.rs" "/checkout/library/std/tests/type-name-unsized.rs" "/checkout/library/std/tests/builtin-clone.rs" "/checkout/library/std/tests/thread.rs" "/checkout/library/std/tests/process_spawning.rs" "/checkout/library/std/tests/create_dir_all_bare.rs" "/checkout/library/std/src/sync/poison.rs" "/checkout/library/std/src/sync/lazy_lock.rs" "/checkout/library/std/src/sync/lazy_lock/tests.rs" "/checkout/library/std/src/sync/once.rs" "/checkout/library/std/src/sync/rwlock.rs" "/checkout/library/std/src/sync/barrier.rs" "/checkout/library/std/src/sync/mutex/tests.rs" "/checkout/library/std/src/sync/mpmc/array.rs" "/checkout/library/std/src/sync/mpmc/context.rs" "/checkout/library/std/src/sync/mpmc/list.rs" "/checkout/library/std/src/sync/mpmc/select.rs" "/checkout/library/std/src/sync/mpmc/counter.rs" "/checkout/library/std/src/sync/mpmc/tests.rs" "/checkout/library/std/src/sync/mpmc/zero.rs" "/checkout/library/std/src/sync/mpmc/utils.rs" "/checkout/library/std/src/sync/mpmc/error.rs" "/checkout/library/std/src/sync/mpmc/mod.rs" "/checkout/library/std/src/sync/mpmc/waker.rs" "/checkout/library/std/src/sync/once_lock.rs" "/checkout/library/std/src/sync/mutex.rs" "/checkout/library/std/src/sync/rwlock/tests.rs" "/checkout/library/std/src/sync/mpsc/tests.rs" "/checkout/library/std/src/sync/mpsc/sync_tests.rs" "/checkout/library/std/src/sync/mpsc/mod.rs" "/checkout/library/std/src/sync/reentrant_lock/tests.rs" "/checkout/library/std/src/sync/barrier/tests.rs" "/checkout/library/std/src/sync/condvar/tests.rs" "/checkout/library/std/src/sync/once/tests.rs" "/checkout/library/std/src/sync/reentrant_lock.rs" "/checkout/library/std/src/sync/condvar.rs" "/checkout/library/std/src/sync/mod.rs" "/checkout/library/std/src/sync/once_lock/tests.rs" "/checkout/library/std/src/num.rs" "/checkout/library/std/src/collections/hash/set/tests.rs" "/checkout/library/std/src/collections/hash/map/tests.rs" "/checkout/library/std/src/collections/hash/map.rs" "/checkout/library/std/src/collections/hash/set.rs" "/checkout/library/std/src/collections/hash/mod.rs" "/checkout/library/std/src/collections/mod.rs" "/checkout/compiler/rustc_codegen_gcc/src/asm.rs" "/checkout/library/unwind/src/wasm.rs"` failed.
If you're running `tidy`, try again with `--bless`. Or, if you just want to format code, run `./x.py fmt` instead.
  local time: Tue Oct 22 22:00:32 UTC 2024
  network time: Tue, 22 Oct 2024 22:00:32 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

@bors
Copy link
Collaborator

bors commented Nov 4, 2024

☔ The latest upstream changes (presumably #132581) made this pull request unmergeable. Please resolve the merge conflicts.

@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 18, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants