Skip to content

Set proper alignment on constants #28920

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 2 commits into from
Oct 10, 2015
Merged

Set proper alignment on constants #28920

merged 2 commits into from
Oct 10, 2015

Conversation

dotdash
Copy link
Contributor

@dotdash dotdash commented Oct 8, 2015

For enum variants, the default alignment for a specific variant might be
lower than the alignment of the enum type itself. In such cases we, for
example, generate memcpy calls with an alignment that's higher than the
alignment of the constant we copy from.

To avoid that, we need to explicitly set the required alignment on
constants.

Fixes #28912.

@rust-highfive
Copy link
Contributor

r? @alexcrichton

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

@@ -254,11 +254,11 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
Some(&val) => return val,
None => {}
}
let ty = monomorphize::apply_param_substs(ccx.tcx(), param_substs,
&ccx.tcx().expr_ty(expr));
Copy link
Member

Choose a reason for hiding this comment

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

Alignment mishap?

@eddyb
Copy link
Member

eddyb commented Oct 8, 2015

r=me with the formatting nit fixed.

@dotdash
Copy link
Contributor Author

dotdash commented Oct 8, 2015

@bors r=eddyb 00a79c0

@@ -275,6 +275,10 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
};

let lvalue = addr_of(ccx, val, "const");
unsafe {
llvm::LLVMSetAlignment(lvalue, type_of::align_of(ccx, ty));
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 this belongs inside addr_of_mut, rather than here.

@dotdash
Copy link
Contributor Author

dotdash commented Oct 8, 2015

@bors r-

@eefriedman Good point.

@dotdash
Copy link
Contributor Author

dotdash commented Oct 9, 2015

Update as per the suggestion of @eefriedman. I also had to fix the string that identifies the gdb script section, which is used as a C string but had no terminating 0 byte. The alignment changes caused that bug to trigger an error in a codegen test.

@alexcrichton
Copy link
Member

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned alexcrichton Oct 9, 2015
@@ -54,7 +54,8 @@ pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext)
/// section.
pub fn get_or_insert_gdb_debug_scripts_section_global(ccx: &CrateContext)
-> llvm::ValueRef {
let section_var_name = "__rustc_debug_gdb_scripts_section__";
let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
let section_var_name = &c_section_var_name[..c_section_var_name.len()-1];
Copy link
Contributor

Choose a reason for hiding this comment

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

For clarity, you might want to use c_section_var_name.as_ptr() as *const _ below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For correctness even. Not sure where I lost that change (and even more confusing that it passed the test without it). Thanks!

For enum variants, the default alignment for a specific variant might be
lower than the alignment of the enum type itself. In such cases we, for
example, generate memcpy calls with an alignment that's higher than the
alignment of the constant we copy from.

To avoid that, we need to explicitly set the required alignment on
constants.

Fixes rust-lang#28912.
@dotdash
Copy link
Contributor Author

dotdash commented Oct 9, 2015

Updated to handle upgrading the alignment of constants that are shared between types with different alignment requirements.

Thanks a lot @eefriedman!

@eddyb
Copy link
Member

eddyb commented Oct 10, 2015

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 10, 2015

📌 Commit 6ad079e has been approved by eddyb

@bors
Copy link
Collaborator

bors commented Oct 10, 2015

⌛ Testing commit 6ad079e with merge 39376de...

bors added a commit that referenced this pull request Oct 10, 2015
For enum variants, the default alignment for a specific variant might be
lower than the alignment of the enum type itself. In such cases we, for
example, generate memcpy calls with an alignment that's higher than the
alignment of the constant we copy from.

To avoid that, we need to explicitly set the required alignment on
constants.

Fixes #28912.
@bors bors merged commit 6ad079e into rust-lang:master Oct 10, 2015
@bors bors mentioned this pull request Oct 10, 2015
@dotdash dotdash deleted the const_align branch January 31, 2016 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants