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 OwnedTargetMachine to manage llvm:TargetMachine #115911

Merged
merged 1 commit into from
Sep 24, 2023

Conversation

nebulark
Copy link
Contributor

LLVMRustDisposeTargetMachine taking a &mut could be undefined behaviour.
Wrapping it with a struct and using pointers instead avoids this problem.
In addition the TargetMachine is now automatically freed via the Wrappers drop impl. This should fix some memory leaks when
create_informational_target_machine was used, e.g.

pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
let target_machine = create_informational_target_machine(sess);
supported_target_features(sess)
.iter()
.filter_map(|&(feature, gate)| {
if sess.is_nightly_build() || allow_unstable || gate.is_none() {
Some(feature)
} else {
None
}
})
.filter(|feature| {
// check that all features in a given smallvec are enabled
for llvm_feature in to_llvm_features(sess, feature) {
let cstr = SmallCStr::new(llvm_feature);
if !unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) } {
return false;
}
}
true
})
.map(|feature| Symbol::intern(feature))
.collect()
}

r? @Nilstrieb

@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 Sep 17, 2023
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@Nilstrieb Nilstrieb left a comment

Choose a reason for hiding this comment

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

Thanks! have a few comments for small improvements, but overall this looks good and should improve the code.

compiler/rustc_codegen_llvm/src/back/write.rs Show resolved Hide resolved
use crate::{errors::LlvmError, llvm};

#[repr(transparent)]
pub struct TargetMachineWrapper {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about the best name for this. I think TargetMachineWrapper isn't the best name, but it's also not bad. TargetMachine would be confusing. If you can think of something better, do rename it, but if you can't that's fine too and we can keep it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some ideas might be

  • TargetMachineBox (it kindo of behaves like a box, but its not copyable )
  • TargetMachineLLVM (it the the for the associated type TargetMachine for the LLVM backend.
  • TargetMachineOwned / OwnedTargetMachine
  • UniqueTargetMachine / TargetMachineUnique

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed it to TargetMachineBox. I think that fits what I am going for and should good give a good intuition of what it does. Also clarified this in the comments.

Copy link
Member

Choose a reason for hiding this comment

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

I like OwnedTargetMachine more than TargetMachineBox, but TargetMachineBox is fine too. If you want to, you can rename it again, or if you think it's fine and don't want to you can just use @bors r=Nilstrieb and approve the PR.

compiler/rustc_codegen_llvm/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_codegen_llvm/src/context.rs Show resolved Hide resolved
@nebulark
Copy link
Contributor Author

Left some comments. Will be implementing those tomorrow.

@nebulark
Copy link
Contributor Author

Squashed commits

@rustbot review

@nebulark nebulark marked this pull request as ready for review September 24, 2023 10:08
@Nilstrieb
Copy link
Member

r=me with or without another rename
@bors delegate+

@bors
Copy link
Contributor

bors commented Sep 24, 2023

✌️ @nebulark, you can now approve this pull request!

If @Nilstrieb told you to "r=me" after making some further change, please make that change, then do @bors r=@Nilstrieb

@rust-log-analyzer

This comment has been minimized.

@nebulark nebulark changed the title wrap llvm::TargetMachine and use pointers instead of &'static mut Add OwnedTargetMachine to manage llvm:TargetMachine Sep 24, 2023
instead of &'static mut and provides safe interface to create/dispose
it.
@nebulark
Copy link
Contributor Author

@bors r=@Nilstrieb

@bors
Copy link
Contributor

bors commented Sep 24, 2023

📌 Commit 3409ca6 has been approved by Nilstrieb

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 Sep 24, 2023
@bors
Copy link
Contributor

bors commented Sep 24, 2023

⌛ Testing commit 3409ca6 with merge 37390d6...

@bors
Copy link
Contributor

bors commented Sep 24, 2023

☀️ Test successful - checks-actions
Approved by: Nilstrieb
Pushing 37390d6 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Sep 24, 2023
@bors bors merged commit 37390d6 into rust-lang:master Sep 24, 2023
12 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 24, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (37390d6): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.8% [0.6%, 4.0%] 4
Regressions ❌
(secondary)
5.1% [0.9%, 9.0%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-8.2%, -1.5%] 9
All ❌✅ (primary) 2.8% [0.6%, 4.0%] 4

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 632.214s -> 631.113s (-0.17%)
Artifact size: 317.15 MiB -> 317.17 MiB (0.01%)

@nebulark nebulark deleted the refactor_targetmachine branch May 3, 2024 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

None yet

6 participants