-
Notifications
You must be signed in to change notification settings - Fork 13k
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
[MIR] Refactor the MIR translator to use LLVM Builder directly #31282
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
|
@@ -26,6 +26,7 @@ use middle::lang_items::LangItem; | |
use middle::subst::{self, Substs}; | ||
use trans::base; | ||
use trans::build; | ||
use trans::builder::Builder; | ||
use trans::callee; | ||
use trans::cleanup; | ||
use trans::consts; | ||
|
@@ -45,6 +46,7 @@ use util::nodemap::{FnvHashMap, NodeMap}; | |
|
||
use arena::TypedArena; | ||
use libc::{c_uint, c_char}; | ||
use std::ops::Deref; | ||
use std::ffi::CString; | ||
use std::cell::{Cell, RefCell}; | ||
use std::vec::Vec; | ||
|
@@ -365,6 +367,9 @@ pub struct FunctionContext<'a, 'tcx: 'a> { | |
// The arena that blocks are allocated from. | ||
pub block_arena: &'a TypedArena<BlockS<'a, 'tcx>>, | ||
|
||
// The arena that landing pads are allocated from. | ||
pub lpad_arena: TypedArena<LandingPad>, | ||
|
||
// This function's enclosing crate context. | ||
pub ccx: &'a CrateContext<'a, 'tcx>, | ||
|
||
|
@@ -582,7 +587,7 @@ pub struct BlockS<'blk, 'tcx: 'blk> { | |
|
||
// If this block part of a landing pad, then this is `Some` indicating what | ||
// kind of landing pad its in, otherwise this is none. | ||
pub lpad: RefCell<Option<LandingPad>>, | ||
pub lpad: Cell<Option<&'blk LandingPad>>, | ||
|
||
// AST node-id associated with this block, if any. Used for | ||
// debugging purposes only. | ||
|
@@ -604,7 +609,7 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> { | |
llbb: llbb, | ||
terminated: Cell::new(false), | ||
unreachable: Cell::new(false), | ||
lpad: RefCell::new(None), | ||
lpad: Cell::new(None), | ||
opt_node_id: opt_node_id, | ||
fcx: fcx | ||
}) | ||
|
@@ -613,11 +618,18 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> { | |
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> { | ||
self.fcx.ccx | ||
} | ||
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> { | ||
self.fcx | ||
} | ||
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> { | ||
self.fcx.ccx.tcx() | ||
} | ||
pub fn sess(&self) -> &'blk Session { self.fcx.ccx.sess() } | ||
|
||
pub fn lpad(&self) -> Option<&'blk LandingPad> { | ||
self.lpad.get() | ||
} | ||
|
||
pub fn mir(&self) -> &'blk Mir<'tcx> { | ||
self.fcx.mir() | ||
} | ||
|
@@ -659,6 +671,109 @@ impl<'blk, 'tcx> BlockS<'blk, 'tcx> { | |
self.fcx.param_substs, | ||
value) | ||
} | ||
|
||
pub fn build(&'blk self) -> BlockAndBuilder<'blk, 'tcx> { | ||
BlockAndBuilder::new(self, OwnedBuilder::new_with_ccx(self.ccx())) | ||
} | ||
} | ||
|
||
pub struct OwnedBuilder<'blk, 'tcx: 'blk> { | ||
builder: Builder<'blk, 'tcx> | ||
} | ||
|
||
impl<'blk, 'tcx> OwnedBuilder<'blk, 'tcx> { | ||
pub fn new_with_ccx(ccx: &'blk CrateContext<'blk, 'tcx>) -> Self { | ||
// Create a fresh builder from the crate context. | ||
let llbuilder = unsafe { | ||
llvm::LLVMCreateBuilderInContext(ccx.llcx()) | ||
}; | ||
OwnedBuilder { | ||
builder: Builder { | ||
llbuilder: llbuilder, | ||
ccx: ccx, | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'blk, 'tcx> Drop for OwnedBuilder<'blk, 'tcx> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
llvm::LLVMDisposeBuilder(self.builder.llbuilder); | ||
} | ||
} | ||
} | ||
|
||
pub struct BlockAndBuilder<'blk, 'tcx: 'blk> { | ||
bcx: Block<'blk, 'tcx>, | ||
owned_builder: OwnedBuilder<'blk, 'tcx>, | ||
} | ||
|
||
impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> { | ||
pub fn new(bcx: Block<'blk, 'tcx>, owned_builder: OwnedBuilder<'blk, 'tcx>) -> Self { | ||
// Set the builder's position to this block's end. | ||
owned_builder.builder.position_at_end(bcx.llbb); | ||
BlockAndBuilder { | ||
bcx: bcx, | ||
owned_builder: owned_builder, | ||
} | ||
} | ||
|
||
pub fn with_block<F, R>(&self, f: F) -> R | ||
where F: FnOnce(Block<'blk, 'tcx>) -> R | ||
{ | ||
let result = f(self.bcx); | ||
self.position_at_end(self.bcx.llbb); | ||
result | ||
} | ||
|
||
pub fn map_block<F>(self, f: F) -> Self | ||
where F: FnOnce(Block<'blk, 'tcx>) -> Block<'blk, 'tcx> | ||
{ | ||
let BlockAndBuilder { bcx, owned_builder } = self; | ||
let bcx = f(bcx); | ||
BlockAndBuilder::new(bcx, owned_builder) | ||
} | ||
|
||
// Methods delegated to bcx | ||
|
||
pub fn ccx(&self) -> &'blk CrateContext<'blk, 'tcx> { | ||
self.bcx.ccx() | ||
} | ||
pub fn fcx(&self) -> &'blk FunctionContext<'blk, 'tcx> { | ||
self.bcx.fcx() | ||
} | ||
pub fn tcx(&self) -> &'blk ty::ctxt<'tcx> { | ||
self.bcx.tcx() | ||
} | ||
pub fn sess(&self) -> &'blk Session { | ||
self.bcx.sess() | ||
} | ||
|
||
pub fn llbb(&self) -> BasicBlockRef { | ||
self.bcx.llbb | ||
} | ||
|
||
pub fn mir(&self) -> &'blk Mir<'tcx> { | ||
self.bcx.mir() | ||
} | ||
|
||
pub fn val_to_string(&self, val: ValueRef) -> String { | ||
self.bcx.val_to_string(val) | ||
} | ||
|
||
pub fn monomorphize<T>(&self, value: &T) -> T | ||
where T: TypeFoldable<'tcx> | ||
{ | ||
self.bcx.monomorphize(value) | ||
} | ||
} | ||
|
||
impl<'blk, 'tcx> Deref for BlockAndBuilder<'blk, 'tcx> { | ||
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. Nice! |
||
type Target = Builder<'blk, 'tcx>; | ||
fn deref(&self) -> &Self::Target { | ||
&self.owned_builder.builder | ||
} | ||
} | ||
|
||
/// A structure representing an active landing pad for the duration of a basic | ||
|
Oops, something went wrong.
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.
You might want to leave off this change for a separate PR, honestly (or at least factor it out into a separate commit, so it easier to see related changes).
Code related to this field is very precondition-sensitive, I’m worried changing it like this might invalidate some of those preconditions. E.g. MSVC landing blocks, especially, are on per-block basis and it is likely to be wrong there.
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.
Ah, I see it, you’re still storing a pointer inside a
Block
. A few thoughts:BlockS
does not really matter, because we only ever pass aBlock
around, which is apub type Block<'blk, 'tcx> = &'blk BlockS<'blk, 'tcx>;
– i.e. is a reference and there’s barely any performance overhead passing a pointer to larger or smaller struct;Block
are for the old trans to use, and I wouldn’t bother much changing it (but since you’ve already changed it, its fine, I guess?).I’d still like it split up into a separate commit so it is easier to review and these changes do not mix with the Builder refactor.