@@ -79,77 +79,3 @@ For more details about the partitioner read the module level [documentation].
79
79
[ mono ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_monomorphize/partitioning/fn.collect_and_partition_mono_items.html
80
80
[ codegen1 ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/base/fn.codegen_crate.html
81
81
[ documentation ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_monomorphize/partitioning/index.html
82
-
83
- ## Polymorphization
84
-
85
- As mentioned above, monomorphization produces fast code, but it comes at the
86
- cost of compile time and binary size. [ MIR optimizations] [ miropt ] can help a
87
- bit with this.
88
-
89
- In addition to MIR optimizations, rustc attempts to determine when fewer
90
- copies of functions are necessary and avoid making those copies - known
91
- as "polymorphization". When a function-like item is found during
92
- monomorphization collection, the
93
- [ ` rustc_mir_monomorphize::polymorphize::unused_generic_params ` ] [ polymorph ]
94
- query is invoked, which traverses the MIR of the item to determine on which
95
- generic parameters the item might not need duplicated.
96
-
97
- Currently, polymorphization only looks for unused generic parameters. These
98
- are relatively rare in functions, but closures inherit the generic
99
- parameters of their parent function and it is common for closures to not
100
- use those inherited parameters. Without polymorphization, a copy of these
101
- closures would be created for each copy of the parent function. By
102
- creating fewer copies, less LLVM IR is generated; therefore less needs to be processed.
103
-
104
- ` unused_generic_params ` returns a ` FiniteBitSet<u64> ` where a bit is set if
105
- the generic parameter of the corresponding index is unused. Any parameters
106
- after the first sixty-four are considered used.
107
-
108
- The results of polymorphization analysis are used in the
109
- [ ` Instance::polymorphize ` ] [ inst_polymorph ] function to replace the
110
- [ ` Instance ` ] [ inst ] 's substitutions for the unused generic parameters with their
111
- identity substitutions.
112
-
113
- Consider the example below:
114
-
115
- ``` rust
116
- fn foo <A , B >() {
117
- let x : Option <B > = None ;
118
- }
119
-
120
- fn main () {
121
- foo :: <u16 , u32 >();
122
- foo :: <u64 , u32 >();
123
- }
124
- ```
125
-
126
- During monomorphization collection, ` foo ` will be collected with the
127
- substitutions ` [u16, u32] ` and ` [u64, u32] ` (from its invocations in ` main ` ).
128
- ` foo ` has the identity substitutions ` [A, B] ` (or
129
- ` [ty::Param(0), ty::Param(1)] ` ).
130
-
131
- Polymorphization will identify ` A ` as being unused and it will be replaced in
132
- the substitutions with the identity parameter before being added to the set
133
- of collected items - thereby reducing the copies from two (` [u16, u32] ` and
134
- ` [u64, u32] ` ) to one (` [A, u32] ` ).
135
-
136
- ` unused_generic_params ` will also be invoked during code generation when the
137
- symbol name for ` foo ` is being computed for use in the callsites of ` foo `
138
- (which have the regular substitutions present, otherwise there would be a
139
- symbol mismatch between the caller and the function).
140
-
141
- As a result of polymorphization, items collected during monomorphization
142
- cannot be assumed to be monomorphic.
143
-
144
- It is intended that polymorphization be extended to more advanced cases,
145
- such as where only the size/alignment of a generic parameter are required.
146
-
147
- More details on polymorphization are available in the
148
- [ master's thesis] [ thesis ] associated with polymorphization's initial
149
- implementation.
150
-
151
- [ miropt ] : ../mir/optimizations.md
152
- [ polymorph ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_monomorphize/polymorphize/fn.unused_generic_params.html
153
- [ inst ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/instance/struct.Instance.html
154
- [ inst_polymorph ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/instance/struct.Instance.html#method.polymorphize
155
- [ thesis ] : https://davidtw.co/media/masters_dissertation.pdf
0 commit comments