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

patchable-function-entry: Add unstable compiler flag and attribute #124741

Merged
merged 4 commits into from
Jun 28, 2024

Conversation

nebulark
Copy link
Contributor

@nebulark nebulark commented May 5, 2024

Tracking issue: #123115

Add the -Z patchable-function-entry compiler flag and the #[patchable_function_entry(prefix_nops = m, entry_nops = n)] attribute.
Rebased and adjusted the canditate implementation to match changes in the RFC.

@rustbot
Copy link
Collaborator

rustbot commented May 5, 2024

r? @estebank

rustbot has assigned @estebank.
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 May 5, 2024
@nebulark
Copy link
Contributor Author

nebulark commented May 5, 2024

I am very unsure about the error handling in codegen_attrs.rs

Creating a new error type seemed a bit too much for those errors.
Should I rephrase the errors? Should I add tests for those?

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@nebulark nebulark force-pushed the patchable-function-entries-pr branch from a2f3275 to b093958 Compare May 7, 2024 06:12
@rust-log-analyzer

This comment has been minimized.

@nebulark nebulark force-pushed the patchable-function-entries-pr branch from b093958 to adaf72d Compare May 7, 2024 07:07
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@nebulark nebulark force-pushed the patchable-function-entries-pr branch from 8c53256 to 316bd64 Compare May 8, 2024 16:56
@maurer
Copy link
Contributor

maurer commented May 8, 2024

As author of the RFC and the two commits cherry-picked over, this LGTM (there's a bit of grunge in the git history with an unresolved conflict in the rebase, but it gets cleaned up by the last commit).

for item in l {
if let Some((sym, lit)) = item.name_value_literal() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did we switch off name_value_literal()? It looks like you've re-implemented it by first extracting out the symbol manually (even if name_value_literal() doesn't work for some reason, you should consider symbol_or_empty() rather than the first two steps). I do see that you can no longer directly call it on the item and would need to use if let Some((sym, lit)) = item.meta_item().and_then(|mi| mi.name_value_literal()) {, but it seems like it'd be a lot shorter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for suggesting symbol_or_empty! I found name_or_empty, which was probably what you meant and it allowed me to get rid of that ugly span parsing.
I also took the oppurtunity add better error diagnostics, when parsing fails.

@nebulark nebulark force-pushed the patchable-function-entries-pr branch from 316bd64 to df751da Compare May 10, 2024 07:55
@rust-log-analyzer

This comment has been minimized.

@nebulark nebulark force-pushed the patchable-function-entries-pr branch from 21f138e to bd412f6 Compare June 3, 2024 17:10
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@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.

The cg_llvm and cg_ssa changes to the actual codegen, and the attendant FileCheck, look good to me. I'd like it if a T-compiler reviewer gave their formal opinion on the diagnostics code given I'm not as familiar with most of the diagnostics code, and the diagnostics in the codegen parts are usually quite... sparse.

compiler/rustc_codegen_ssa/src/codegen_attrs.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_ssa/src/codegen_attrs.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jun 25, 2024

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

`-Z patchable-function-entry` works like `-fpatchable-function-entry`
on clang/gcc. The arguments are total nop count and function offset.

See MCP rust-lang/compiler-team#704
See [RFC](https://github.com/maurer/rust-rfcs/blob/patchable-function-entry/text/0000-patchable-function-entry.md) (yet to be numbered)

TODO before submission:
* Needs an RFC
* Improve error reporting for malformed attributes
@nebulark nebulark force-pushed the patchable-function-entries-pr branch from b91a3aa to 4f5069f Compare June 25, 2024 16:41
@rust-log-analyzer

This comment has been minimized.

@nebulark nebulark force-pushed the patchable-function-entries-pr branch from 4f5069f to 7c56398 Compare June 25, 2024 17:00
let mut entry = None;
for item in l {
let Some(meta_item) = item.meta_item() else {
tcx.dcx().span_err(item.span(), "Expected name value pair.");
Copy link
Contributor

Choose a reason for hiding this comment

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

For future reference, the message format doesn't use title-case nor periods. Maybe we should, but let's remain consistent with what we already have:

Suggested change
tcx.dcx().span_err(item.span(), "Expected name value pair.");
tcx.dcx().span_err(item.span(), "expected name value pair");

Comment on lines 488 to 495
tcx.dcx().span_err(
item.span(),
format!(
"Unexpected parameter name. Allowed names: {}, {}",
sym::prefix_nops,
sym::entry_nops
),
);
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
tcx.dcx().span_err(
item.span(),
format!(
"Unexpected parameter name. Allowed names: {}, {}",
sym::prefix_nops,
sym::entry_nops
),
);
tcx.dcx().struct_span_err(
item.span(),
"unexpected parameter name"
).span_label(
item.span(),
format!(
"expected {} or {}",
sym::prefix_nops,
sym::entry_nops
),
).emit();

};

let Some(name_value_lit) = meta_item.name_value_literal() else {
tcx.dcx().span_err(item.span(), "Expected name value pair.");
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
tcx.dcx().span_err(item.span(), "Expected name value pair.");
tcx.dcx().span_err(item.span(), "expected name value pair");

Comment on lines 502 to 505
tcx.dcx().span_err(
name_value_lit.span,
"Expected integer value between 0 and 255.",
);
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
tcx.dcx().span_err(
name_value_lit.span,
"Expected integer value between 0 and 255.",
);
tcx.dcx().struct_span_err(
name_value_lit.span,
"integer value out of range",
).span_label(
name_value_lit.span,
"value must be between `0` and `255`",
).emit();

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 error message would also be triggered when passing in a string, so the suggested wording might be a bit weird in that case. Do you think it makes sense to make the check for the string seperate again or is this just a minor issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is worth it to be more accurate whenever possible. you can make this be

Suggested change
tcx.dcx().span_err(
name_value_lit.span,
"Expected integer value between 0 and 255.",
);
let mut err = tcx.dcx().struct_span_err(
name_value_lit.span,
"invalid literal value",
);
if is_numeric {
err.span_label(
name_value_lit.span,
"value must be between `0` and `255`"
);
} else if is_str {
err.span_label(
name_value_lit.span,
"some appropriate message"
);
}
// Are there other cases to handle? can we hit this error without a numeric or str literal?
err.emit();

}

if let (None, None) = (prefix, entry) {
tcx.dcx().span_err(attr.span, "Must specify at least one parameter.");
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
tcx.dcx().span_err(attr.span, "Must specify at least one parameter.");
tcx.dcx().span_err(attr.span, "must specify at least one parameter");

@@ -813,6 +813,10 @@ fn test_unstable_options_tracking_hash() {
tracked!(packed_bundled_libs, true);
tracked!(panic_abort_tests, true);
tracked!(panic_in_drop, PanicStrategy::Abort);
tracked!(
patchable_function_entry,
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total >= prefix")
Copy link
Contributor

Choose a reason for hiding this comment

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

The message in expect needs to be the description of the condition that wasn't met.

Suggested change
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total >= prefix")
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total < prefix")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't know if I understood you correctly, but to get a value the condition 'total >=prefix' must be true. The panic would happen if that condition is false. So I'd think it would be correct as written?

Copy link
Contributor

Choose a reason for hiding this comment

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

What will happen if total < prefix is that at runtime we'll get a panic that looks like the following:

thread 'main' panicked at compiler/rustc_interface/src/tests.rs:818:8:
total >= prefix

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7e6fd3fe91aec1a58fdd5b198388ec23

Instead we should provide additional context beyond that. Having said that, this is a nitpick to make it easier for people to understand that happened in this test, it's not a deal breaker.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total >= prefix")
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total must be greater than prefix")

Copy link
Member

Choose a reason for hiding this comment

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

maybe?

Copy link
Member

Choose a reason for hiding this comment

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

or just unwrap, this is just a test, really, and the condition is trivially obviously true.

@workingjubilee
Copy link
Member

cool, thanks!

@bors r=estebank,workingjubilee rollup

@bors
Copy link
Contributor

bors commented Jun 27, 2024

📌 Commit 8d246b0 has been approved by estebank,workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 27, 2024
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 28, 2024
…s-pr, r=estebank,workingjubilee

patchable-function-entry: Add unstable compiler flag and attribute

Tracking issue: rust-lang#123115

Add the -Z patchable-function-entry compiler flag and the #[patchable_function_entry(prefix_nops = m, entry_nops = n)] attribute.
Rebased and adjusted the canditate implementation to match changes in the RFC.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 28, 2024
…s-pr, r=estebank,workingjubilee

patchable-function-entry: Add unstable compiler flag and attribute

Tracking issue: rust-lang#123115

Add the -Z patchable-function-entry compiler flag and the #[patchable_function_entry(prefix_nops = m, entry_nops = n)] attribute.
Rebased and adjusted the canditate implementation to match changes in the RFC.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 28, 2024
…s-pr, r=estebank,workingjubilee

patchable-function-entry: Add unstable compiler flag and attribute

Tracking issue: rust-lang#123115

Add the -Z patchable-function-entry compiler flag and the #[patchable_function_entry(prefix_nops = m, entry_nops = n)] attribute.
Rebased and adjusted the canditate implementation to match changes in the RFC.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 28, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#124741 (patchable-function-entry: Add unstable compiler flag and attribute)
 - rust-lang#126470 (make cargo submodule optional)
 - rust-lang#126701 (ignore `llvm::Lld` if lld is not enabled)
 - rust-lang#126956 (core: avoid `extern type`s in formatting infrastructure)
 - rust-lang#126970 (Simplify `str::clone_into`)
 - rust-lang#127058 (Tighten `fn_decl_span` for async blocks)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 28, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#124741 (patchable-function-entry: Add unstable compiler flag and attribute)
 - rust-lang#126470 (make cargo submodule optional)
 - rust-lang#126956 (core: avoid `extern type`s in formatting infrastructure)
 - rust-lang#126970 (Simplify `str::clone_into`)
 - rust-lang#127022 (Support fetching `Attribute` of items.)
 - rust-lang#127058 (Tighten `fn_decl_span` for async blocks)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0262932 into rust-lang:master Jun 28, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jun 28, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jun 28, 2024
Rollup merge of rust-lang#124741 - nebulark:patchable-function-entries-pr, r=estebank,workingjubilee

patchable-function-entry: Add unstable compiler flag and attribute

Tracking issue: rust-lang#123115

Add the -Z patchable-function-entry compiler flag and the #[patchable_function_entry(prefix_nops = m, entry_nops = n)] attribute.
Rebased and adjusted the canditate implementation to match changes in the RFC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

8 participants