-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Various codegen_llvm cleanups #138674
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
Merged
Merged
Various codegen_llvm cleanups #138674
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eef70a9
Create a safe wrapper around LLVMRustDIBuilderCreateTemplateTypeParam…
oli-obk b4acf7a
Immediately create an `Option` instead of reallocating for it later
oli-obk 6adc2c1
Deduplicate template parameter creation
oli-obk e19e4e3
Create a safe wrapper around `LLVMRustDIBuilderCreateSubroutineType`
oli-obk cc41dd4
Create a safe wrapper function around `LLVMRustDIBuilderCreateFile`
oli-obk 018032c
Create a safe wrapper around `LLVMRustDIBuilderCreateBasicType`
oli-obk 1f34b19
Avoid splitting up a layout
oli-obk f4b0984
Create a safe wrapper around `LLVMRustDIBuilderCreateMemberType`
oli-obk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ use std::borrow::Cow; | |
use std::fmt::{self, Write}; | ||
use std::hash::{Hash, Hasher}; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::Arc; | ||
use std::{iter, ptr}; | ||
|
||
use libc::{c_char, c_longlong, c_uint}; | ||
|
@@ -38,8 +39,8 @@ use crate::debuginfo::metadata::type_map::build_type_with_children; | |
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind}; | ||
use crate::llvm; | ||
use crate::llvm::debuginfo::{ | ||
DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, DebugEmissionKind, | ||
DebugNameTableKind, | ||
DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, | ||
DebugEmissionKind, DebugNameTableKind, | ||
}; | ||
use crate::value::Value; | ||
|
||
|
@@ -68,7 +69,8 @@ pub(super) const UNKNOWN_COLUMN_NUMBER: c_uint = 0; | |
|
||
const NO_SCOPE_METADATA: Option<&DIScope> = None; | ||
/// A function that returns an empty list of generic parameter debuginfo nodes. | ||
const NO_GENERICS: for<'ll> fn(&CodegenCx<'ll, '_>) -> SmallVec<&'ll DIType> = |_| SmallVec::new(); | ||
const NO_GENERICS: for<'ll> fn(&CodegenCx<'ll, '_>) -> SmallVec<Option<&'ll DIType>> = | ||
|_| SmallVec::new(); | ||
|
||
// SmallVec is used quite a bit in this module, so create a shorthand. | ||
// The actual number of elements is not so important. | ||
|
@@ -311,12 +313,7 @@ fn build_subroutine_type_di_node<'ll, 'tcx>( | |
|
||
debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id); | ||
|
||
let fn_di_node = unsafe { | ||
llvm::LLVMRustDIBuilderCreateSubroutineType( | ||
DIB(cx), | ||
create_DIArray(DIB(cx), &signature_di_nodes[..]), | ||
) | ||
}; | ||
let fn_di_node = create_subroutine_type(cx, create_DIArray(DIB(cx), &signature_di_nodes[..])); | ||
|
||
// This is actually a function pointer, so wrap it in pointer DI. | ||
let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false); | ||
|
@@ -340,6 +337,13 @@ fn build_subroutine_type_di_node<'ll, 'tcx>( | |
DINodeCreationResult::new(di_node, false) | ||
} | ||
|
||
pub(super) fn create_subroutine_type<'ll>( | ||
cx: &CodegenCx<'ll, '_>, | ||
signature: &'ll DICompositeType, | ||
) -> &'ll DICompositeType { | ||
unsafe { llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(cx), signature) } | ||
} | ||
|
||
/// Create debuginfo for `dyn SomeTrait` types. Currently these are empty structs | ||
/// we with the correct type name (e.g. "dyn SomeTrait<Foo, Item=u32> + Sync"). | ||
fn build_dyn_type_di_node<'ll, 'tcx>( | ||
|
@@ -620,42 +624,38 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi | |
let source = | ||
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref()); | ||
|
||
unsafe { | ||
llvm::LLVMRustDIBuilderCreateFile( | ||
DIB(cx), | ||
file_name.as_c_char_ptr(), | ||
file_name.len(), | ||
directory.as_c_char_ptr(), | ||
directory.len(), | ||
hash_kind, | ||
hash_value.as_c_char_ptr(), | ||
hash_value.len(), | ||
source.map_or(ptr::null(), |x| x.as_c_char_ptr()), | ||
source.map_or(0, |x| x.len()), | ||
) | ||
} | ||
create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source) | ||
} | ||
} | ||
|
||
fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile { | ||
debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| unsafe { | ||
let file_name = "<unknown>"; | ||
let directory = ""; | ||
let hash_value = ""; | ||
debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| { | ||
create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None) | ||
}) | ||
} | ||
|
||
fn create_file<'ll>( | ||
builder: &DIBuilder<'ll>, | ||
file_name: &str, | ||
directory: &str, | ||
hash_value: &str, | ||
hash_kind: llvm::ChecksumKind, | ||
source: Option<&Arc<String>>, | ||
) -> &'ll DIFile { | ||
unsafe { | ||
llvm::LLVMRustDIBuilderCreateFile( | ||
DIB(cx), | ||
builder, | ||
file_name.as_c_char_ptr(), | ||
file_name.len(), | ||
directory.as_c_char_ptr(), | ||
directory.len(), | ||
llvm::ChecksumKind::None, | ||
hash_kind, | ||
hash_value.as_c_char_ptr(), | ||
hash_value.len(), | ||
ptr::null(), | ||
0, | ||
source.map_or(ptr::null(), |x| x.as_c_char_ptr()), | ||
source.map_or(0, |x| x.len()), | ||
) | ||
}) | ||
} | ||
} | ||
|
||
trait MsvcBasicName { | ||
|
@@ -929,17 +929,13 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>( | |
}; | ||
|
||
unsafe { | ||
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile( | ||
let compile_unit_file = create_file( | ||
debug_context.builder.as_ref(), | ||
name_in_debuginfo.as_c_char_ptr(), | ||
name_in_debuginfo.len(), | ||
work_dir.as_c_char_ptr(), | ||
work_dir.len(), | ||
&name_in_debuginfo, | ||
&work_dir, | ||
"", | ||
llvm::ChecksumKind::None, | ||
ptr::null(), | ||
0, | ||
ptr::null(), | ||
0, | ||
None, | ||
); | ||
|
||
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit( | ||
|
@@ -1287,32 +1283,33 @@ fn build_union_type_di_node<'ll, 'tcx>( | |
fn build_generic_type_param_di_nodes<'ll, 'tcx>( | ||
cx: &CodegenCx<'ll, 'tcx>, | ||
ty: Ty<'tcx>, | ||
) -> SmallVec<&'ll DIType> { | ||
) -> SmallVec<Option<&'ll DIType>> { | ||
if let ty::Adt(def, args) = *ty.kind() { | ||
if args.types().next().is_some() { | ||
let generics = cx.tcx.generics_of(def.did()); | ||
let names = get_parameter_names(cx, generics); | ||
let template_params: SmallVec<_> = iter::zip(args, names) | ||
.filter_map(|(kind, name)| { | ||
kind.as_type().map(|ty| { | ||
let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty); | ||
let actual_type_di_node = type_di_node(cx, actual_type); | ||
let name = name.as_str(); | ||
unsafe { | ||
llvm::LLVMRustDIBuilderCreateTemplateTypeParameter( | ||
DIB(cx), | ||
None, | ||
name.as_c_char_ptr(), | ||
name.len(), | ||
actual_type_di_node, | ||
) | ||
} | ||
}) | ||
let generics = cx.tcx.generics_of(def.did()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This generics_of is now called unconditionally and is likely the source of the perf regression There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out that wasn't it |
||
return get_template_parameters(cx, generics, args); | ||
} | ||
|
||
return smallvec![]; | ||
} | ||
|
||
pub(super) fn get_template_parameters<'ll, 'tcx>( | ||
cx: &CodegenCx<'ll, 'tcx>, | ||
generics: &ty::Generics, | ||
args: ty::GenericArgsRef<'tcx>, | ||
) -> SmallVec<Option<&'ll DIType>> { | ||
if args.types().next().is_some() { | ||
let names = get_parameter_names(cx, generics); | ||
let template_params: SmallVec<_> = iter::zip(args, names) | ||
.filter_map(|(kind, name)| { | ||
kind.as_type().map(|ty| { | ||
let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty); | ||
let actual_type_di_node = type_di_node(cx, actual_type); | ||
Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node)) | ||
}) | ||
.collect(); | ||
}) | ||
.collect(); | ||
|
||
return template_params; | ||
} | ||
return template_params; | ||
} | ||
|
||
return smallvec![]; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe this. Empty string now, null pointer before
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.
But that wouldn't explain the query count changes