You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit enhances documentation and fixes several issues in the MIR transform optimization passes (compiler/rustc_mir_transform). ## Documentation improvements ### ConstParamHasTy interaction (#127030) - Extensively documented the drop shim builder param-env issue that affects types with const parameters - Added detailed explanation of root cause: `build_drop_shim` uses `drop_in_place` intrinsic's DefId instead of dropped type's DefId, causing param-env to lack `ConstArgHasType` predicates - Documented workaround in inline.rs (preventing drop glue inlining for types with const params until monomorphization) - Added corresponding documentation in shim.rs at the problematic typing_env construction site - Outlined three potential fix approaches with implementation details ### Destination propagation (dest_prop.rs) - Documented storage statement handling: clarified why storage statements are currently deleted and provided TODO with specific implementation steps for merging storage ranges instead - Enhanced subtyping check documentation: explained soundness requirements for exact type equality and referenced issue #112651 ### Global Value Numbering (gvn.rs) - Dramatically improved pointer identity issue documentation (issue #79738) at 4 locations, explaining: * CTFE address aliasing problems * Pointer provenance loss during const prop * Interior pointer identity issues * Safety net assertions - Enhanced codegen uniformity documentation: explained why only GlobalAlloc::Memory works in Indirect constants and provided 3-step improvement plan for future work ### Dataflow constant propagation (dataflow_const_prop.rs) - Clarified tail call termination behavior: documented that tail calls naturally terminate dataflow analysis, which is correct ### Inlining (inline.rs) - Documented tail call constraints in two locations: explained why tail calls aren't inlined (complex transformations required, may defeat performance purpose) - Added note explaining why single-caller detection isn't implemented (requires inter-procedural analysis not currently available) ## Code improvements ### GVN subtype handling fix - Fixed subtype checking to use `relate_types` with `Variance::Covariant` - Now correctly handles assignments with valid subtyping relationships, including Subtype casts inserted by add_subtyping_projections pass - Uses same logic as MIR validator for consistency ### Inline heuristics enhancement - Added 50% bonus threshold for single-block functions (likely trivial getters/setters) - Improves inlining decisions for very small, high-value functions ## New files ### compiler/rustc_mir_transform/README.md Created comprehensive developer guide covering: - Key optimization passes (dest_prop, GVN, dataflow const prop, inlining) - How to add new passes - Testing commands - Pass ordering considerations - Known limitations (ConstParamHasTy issue) - Common patterns and performance considerations All changes maintain backward compatibility and follow existing conventions. No functional changes to optimization behavior except for the GVN subtype fix, which enables optimizations that were previously incorrectly rejected.
- Late passes (final cleanup, code size reduction)
58
+
59
+
Order matters! For example:
60
+
-`SimplifyCfg` before `GVN` (cleaner CFG)
61
+
-`GVN` before `DeadStoreElimination` (more values identified)
62
+
-`SimplifyLocals` after most passes (remove unused locals)
63
+
64
+
## Known Limitations and Issues
65
+
66
+
### ConstParamHasTy and Drop Shim Builder (#127030)
67
+
Drop glue generation for types with const parameters has a param-env construction issue:
68
+
-**Problem**: `build_drop_shim` (in `shim.rs`) constructs its typing environment using the `drop_in_place` intrinsic's DefId, not the dropped type's DefId
69
+
-**Impact**: The param-env lacks `ConstArgHasType` predicates, causing panics when MIR generation needs const param types
70
+
-**Workaround**: Inlining of drop glue is disabled for types containing const params until they're fully monomorphized (see `inline.rs:746`)
71
+
-**Proper fix**: Requires synthesizing a typing environment that merges predicates from both drop_in_place and the dropped type
72
+
73
+
This affects types like `struct Foo<const N: usize> { ... }` with Drop implementations.
74
+
75
+
## Common Patterns
76
+
77
+
### Visiting MIR
78
+
- Use `rustc_middle::mir::visit::Visitor` for read-only traversal
79
+
- Use `MutVisitor` for in-place modifications
80
+
- Call `visit_body_preserves_cfg` to keep the CFG structure
0 commit comments