Skip to content
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

Add rustc_intrinsic_const_vector_arg attribute to allow vectors to be passed as constants #118980

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

GeorgeWort
Copy link
Contributor

This allows constant vectors using a repr(simd) type to be propagated
through to the backend by reusing the functionality used to do a similar
thing for the simd_shuffle intrinsic.

fix #118209

@rustbot
Copy link
Collaborator

rustbot commented Dec 15, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@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 Dec 15, 2023
@rustbot
Copy link
Collaborator

rustbot commented Dec 15, 2023

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @bjorn3

@bors
Copy link
Contributor

bors commented Dec 18, 2023

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

@GeorgeWort
Copy link
Contributor Author

I have narrowed the scope of this attribute to only being applicable to simd types defined in the local crate as I do not know how to check if the type is a simd type when it is not local in HIR.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 26, 2023

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

@rust-log-analyzer

This comment has been minimized.

… as constants

This allows constant vectors using a repr(simd) type to be propagated
through to the backend by reusing the functionality used to do a similar
thing for the simd_shuffle intrinsic.

fix rust-lang#118209
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-16 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
GITHUB_ACTION=__run_7
GITHUB_ACTIONS=true
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=GeorgeWort
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_ce9cd0d3-8aed-47ac-8052-fa95de63087d
GITHUB_EVENT_NAME=pull_request
---
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=7b2d9fd7b4c3ed9f0646a0152b06fd8fc36156b1
GITHUB_STATE=/home/runner/work/_temp/_runner_file_commands/save_state_ce9cd0d3-8aed-47ac-8052-fa95de63087d
GITHUB_STEP_SUMMARY=/home/runner/work/_temp/_runner_file_commands/step_summary_ce9cd0d3-8aed-47ac-8052-fa95de63087d
GITHUB_TRIGGERING_ACTOR=GeorgeWort
GITHUB_WORKFLOW_REF=rust-lang/rust/.github/workflows/ci.yml@refs/pull/118980/merge
GITHUB_WORKFLOW_SHA=7b2d9fd7b4c3ed9f0646a0152b06fd8fc36156b1
GITHUB_WORKSPACE=/home/runner/work/rust/rust
GOROOT_1_19_X64=/opt/hostedtoolcache/go/1.19.13/x64
---
#12 writing image sha256:ce54d9a40f11e7fc40a372735a6718a816fbc1be63fd981e77432b6a4d59ebda done
#12 naming to docker.io/library/rust-ci done
#12 DONE 10.0s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-16]
##[group]Clock drift check
  local time: Thu Feb 15 16:20:02 UTC 2024
  network time: Thu, 15 Feb 2024 16:20:02 GMT
  network time: Thu, 15 Feb 2024 16:20:02 GMT
##[endgroup]
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-16', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'build.optimized-compiler-builtins', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-16/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: change-id            := 99999999
---
##[endgroup]
##[group]Testing stage2 compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu)

running 164 tests
F.................................................................................F.....  88/164

failures:

---- [incremental] tests/incremental/change_pub_inherent_method_body/struct_point.rs stdout ----
---- [incremental] tests/incremental/change_pub_inherent_method_body/struct_point.rs stdout ----

error in revision `cfail2`: test compilation failed although it shouldn't!
status: exit status: 1
command: RUSTC_ICE="0" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/incremental/change_pub_inherent_method_body/struct_point.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" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "cfail2" "-C" "incremental=/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/change_pub_inherent_method_body/struct_point/struct_point.inc" "-Z" "incremental-verify-ich" "-O" "--error-format" "json" "--json" "future-incompat" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/change_pub_inherent_method_body/struct_point" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/change_pub_inherent_method_body/struct_point/auxiliary" "-Z" "query-dep-graph"
--- stderr -------------------------------
--- stderr -------------------------------
error: CGU-reuse for `struct_point-fn_calls_changed_method` is `No` but should be at least `PreLto`
   |
   |
LL | #![rustc_partition_reused(module="struct_point-fn_calls_changed_method", cfg="cfail2")]

error: aborting due to 1 previous error
------------------------------------------



---- [incremental] tests/incremental/issue-38222.rs stdout ----

error in revision `rpass2`: compilation failed!
status: exit status: 1
command: RUSTC_ICE="0" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/incremental/issue-38222.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" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "rpass2" "-C" "incremental=/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/issue-38222/issue-38222.inc" "-Z" "incremental-verify-ich" "-O" "--error-format" "json" "--json" "future-incompat" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/issue-38222/a" "-A" "internal_features" "-Crpath" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/issue-38222/auxiliary" "-Z" "query-dep-graph" "-C" "debuginfo=1"
--- stderr -------------------------------
--- stderr -------------------------------
error: CGU-reuse for `issue_38222` is `No` but should be at least `PreLto`
   |
   |
LL | #![rustc_partition_reused(module = "issue_38222", cfg = "rpass2")]

error: aborting due to 1 previous error
------------------------------------------
Build completed unsuccessfully in 0:15:28

@bors
Copy link
Contributor

bors commented Feb 16, 2024

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

@Dylan-DPC Dylan-DPC 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 19, 2024
@Dylan-DPC
Copy link
Member

@GeorgeWort can you address the CI test failure? once that's done you can comment with @rustbot ready to mark this as ready for review and we can push this forward

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff is much bigger than needed. See #121225 for a PR that makes some other intrinsics require a constant argument; this should share that same check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact the even better alternative is probably to remove the simd_shuffle case entirely, and just add the new attribute to simd_shuffle.

let mut copied_constant_arguments = vec![];
'make_args: for (i, arg) in first_args.iter().enumerate() {
let mut op = self.codegen_operand(bx, &arg.node);
let mut op = if const_vec_arg_indexes.contains(&i) {
// Force the specified argument to be constant by using const-qualification to promote any complex rvalues to constant.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is misleading. You are not promoting anything here, and you shouldn't. You are not forcing anything to be a constant either, you are relying on the fact that typecheck already already ensured that this is a constant.

fn foo2(a: i8x2, b: i8);
}

#[rustc_intrinsic_const_vector_arg(0)] //~ ERROR attribute should be applied to functions in `extern "unadjusted"` modules
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this argument needs to be about const vectors?

If this were to become a general rustc_intrinsic_const_arg, then it could also be used for simd_insert/extract.

@RalfJung
Copy link
Member

r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned wesleywiser Feb 21, 2024
{
match attr.meta_item_list() {
Some(items) => {
items.into_iter().for_each(|item: rustc_ast::NestedMetaItem| match item {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
items.into_iter().for_each(|item: rustc_ast::NestedMetaItem| match item {
items.into_iter().for_each(|item| match item {

Comment on lines +1688 to +1691
rustc_ast::NestedMetaItem::Lit(rustc_ast::MetaItemLit {
kind: rustc_ast::LitKind::Int(index, _),
..
}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer using item.lit()

kind: rustc_ast::LitKind::Int(index, _),
..
}) => {
if index >= args.len() as u128 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid checking the index separately from indexing, use

Suggested change
if index >= args.len() as u128 {
let Some(arg) = args.get(index.get().try_into().unwrap()) {

Comment on lines +868 to +876
let (llval, ty) = self.early_evaluate_const_vector(bx, constant);
let llval = llval.unwrap_or_else(|| {
bx.tcx().dcx().emit_err(errors::ShuffleIndicesEvaluation {
span: constant.span,
});
// We've errored, so we don't have to produce working code.
let llty = bx.backend_type(bx.layout_of(ty));
bx.const_undef(llty)
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deduplicate this with the other use sites

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, just do this inside early_evaluate_const_vector like it was done before this PR and remove ShuffleIndicesEvaluation

Comment on lines +921 to +932
if let Some(def) = def
&& let Some(attr) =
bx.tcx().get_attr(def.def_id(), sym::rustc_intrinsic_const_vector_arg)
{
attr.meta_item_list()
.iter()
.flatten()
.map(|item: &NestedMetaItem| match item {
NestedMetaItem::Lit(MetaItemLit {
kind: LitKind::Int(index, _), ..
}) => index.get() as usize,
_ => span_bug!(item.span(), "attribute argument must be an integer"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a helper for this and use it in both attr parsing sites

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here can you explain to me why bx.tcx().get_attr on line 923 causes the failure I'm seeing in CI?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_attr within the same crate will create a dependency edge from the calling query to the HIR. It's not a query. Since this is only allowed on intrinsics, we could (after #120675) store it in the ty::IntrinsicDef struct, so it gets encoded in metadata (so is checked across crates, too), and so it doesn't cause this undesirable dep graph edge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This attribute is intended for architecture specific intrinsics as used here rather than rust-intrinsics like simd_shuffle so I don't think that could be used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should all at some point go through the intrinsic query, they just don't do it yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, is there an issue/PR for this that I can mark this PR as blocked on?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#120675 is what needs to land first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that means that all of the intrinsic functions in files like this will need to be marked with #[rustc_intrinsic]? Is there already a PR for this or is that work that will need to be done to enable features such as those provided in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't be necessary immediately. We can move incrementally. The current system will keep on working, it's just that the attribute will be processed early to be returned by the intrinsic query as a field. It doesn't matter how the intrinsic is declared.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively you can encode this information in codegen_fn_attrs, I think that should also avoid the dep graph edges

@@ -2082,6 +2087,112 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
false
}

fn check_rustc_intrinsic_const_vector_arg(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unnecessary. Just ICEing the compiler when the attribute is misused is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it okay to ICE here while other rustc intrinsics have a check function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other intrinsics don't need these check functions. Some people just really like adding them.^^ But it's a bunch of extra work to write and maintain that code, and not really for any good reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original reason the platform intrinsics had checks in the first place was to allow exposing extern "platform-intrinsics" on stable: https://rust-lang.github.io/rfcs/1199-simd-infrastructure.html That plan didn't end up making it and stdarch was introduced later. And much later portable-simd was introduced which internally used platform-intrinsics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

Rust cannot pass a vector constant directly to LLVM
10 participants