Fix hang/write-back drop for inout structs bitcast - #8821
Open
Vlad Korytsko (vkorytsko) wants to merge 2 commits into
Open
Fix hang/write-back drop for inout structs bitcast#8821Vlad Korytsko (vkorytsko) wants to merge 2 commits into
Vlad Korytsko (vkorytsko) wants to merge 2 commits into
Conversation
…ructs Casting a struct to an identical-layout struct in an inout argument makes CodeGen coerce it with a pointer bitcast. SROA_Parameter_HLSL replaced that bitcast with a one-way copy into a fresh alloca, so every write the callee made was silently discarded. That copy prepass was introduced in microsoft#1718 to fix a crash. microsoft#2745 later taught RewriteBitCast to replace the cast with its operand, keeping the aliasing copy-out needs. The prepass ran first and broke it. Redundant prepass removed.
Passing a struct to an inout parameter of a different-layout struct makes CodeGen coerce it with a pointer bitcast. RewriteBitCast has no rewrite for such a pair and returned with the bitcast still using the value being replaced, so RewriteForScalarRepl spun on a use it could not eliminate. microsoft#2745 fixed identical-layouts, not these. Keep such an alloca out of scalar replacement, leaving the inout copy-out intact, and diagnose what cannot be (like a static global).
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes SROA hangs and lost write-backs for inout arguments cast between struct types.
Changes:
- Adds bitcast safety analysis and fallback diagnostics.
- Removes obsolete identical-layout preprocessing.
- Adds HLSL 2018/2021 regression coverage.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp |
Updates SROA bitcast analysis and rewriting. |
tools/clang/test/HLSLFileCheck/hlsl/types/cast/similar_layout_structs/cast_as_func_inout_param.hlsl |
Tests mismatched layouts under HLSL 2018. |
tools/clang/test/HLSLFileCheck/hlsl/types/cast/similar_layout_structs/cast_as_func_inout_param-strictudt.hlsl |
Tests mismatched layouts under HLSL 2021. |
tools/clang/test/HLSLFileCheck/hlsl/types/cast/identical_layout_structs/cast_as_func_inout_param.hlsl |
Tests write-back for identical layouts under HLSL 2018. |
tools/clang/test/HLSLFileCheck/hlsl/types/cast/identical_layout_structs/cast_as_func_inout_param-strictudt.hlsl |
Tests write-back for identical layouts under HLSL 2021. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+2725
to
+2728
| dxilutil::EmitErrorOnInstruction( | ||
| BCI, "Unsupported cast between struct types with different layouts."); | ||
| BCI->replaceAllUsesWith(UndefValue::get(BCI->getType())); | ||
| BCI->eraseFromParent(); |
Comment on lines
1583
to
+1585
| if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { | ||
| if (!isSafeBitCastForScalarRepl(BC)) | ||
| return MarkUnsafe(Info, User); |
Author
|
@microsoft-github-policy-service agree |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix two SROA bugs casting between struct types in inout arguments
Both come from the same place: passing a struct to an inout parameter of a different struct type makes CodeGen coerce the argument with a pointer bitcast.
SROA_Helper::RewriteBitCasthas to handle that cast, and whether the two layouts areisLayoutIdenticaldecides which bug you hit.1. inout write-back dropped on an identical layout
SROA_Parameter_HLSLran a pre-pass that replaced the bitcast with a one-way copy into fresh storage; the callee wrote into the copy and nothing copied back. The fix is to delete obsolete prepass (#1718) and rely on correct aliasing-preserving rewrite (#2745).2. hang on a layout mismatch
RewriteBitCastcan redirect the cast in three cases: the destination is reachable through the source's leading elements, the layouts are identical, or the cast is unused. Otherwise the fall-through returned with the bitcast still using the value being replaced. That breaks the invariantRewriteForScalarRepldrives on - every user must be erased or stop using the value - so it spins forever (asserts in Debug in bothRewriteBitCasttype mismatch check andRewriteForScalarReplinfinite loop guard).New
isSafeBitCastForScalarReplmirrors the three shapes the rewrite supports, so analysis and rewrite agree on what is handleable. Anything else keeps the alloca whole, which preserves the bitcast and theinoutcopy-out. For values that check does not cover the dead end becomes a diagnostic that also drops the use (static global variable).Note: this is the first error emission in this module, however I'm not sure how to gracefully handle static global case.
Did not find any open issues related to this fixes. #4614 reports hang in the same pass, but from an empty base struct, and is not fixed here. #1718 and #2745 were revalidated, additional test cases were added.