Fix hang/write-back drop for inout structs bitcast - #8821
Fix hang/write-back drop for inout structs bitcast#8821Vlad Korytsko (vkorytsko) wants to merge 5 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. |
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.
| dxilutil::EmitErrorOnInstruction( | ||
| BCI, "Unsupported cast between struct types with different layouts."); | ||
| BCI->replaceAllUsesWith(UndefValue::get(BCI->getType())); | ||
| BCI->eraseFromParent(); |
There was a problem hiding this comment.
Leaving an unsafe global unsplit doesn't work because nothing downstream lowers an aggregate global.
So I'm lowering bitcast-incompatible static globals in LowerStaticGlobalIntoAlloca pass, where the cast and the copy-out reading through it are already handled correctly. The diagnostic remains only for a global that can't be localized.
| if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { | ||
| if (!isSafeBitCastForScalarRepl(BC)) | ||
| return MarkUnsafe(Info, User); |
|
@microsoft-github-policy-service agree |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Globals never run through isSafeForScalarRepl, which casts every user to an Instruction, while a global's users are commonly constant expressions, so such a global reached RewriteBitCast and was diagnosed rather than compiled. Lower it into an alloca instead, where the cast and the copy-out reading through it are already handled. isSafeBitCastForScalarRepl now takes an operator to cover the constant expression form of the cast. A global used outside an entry function is still diagnosed.
| ST = dyn_cast<StructType>(EltTy); | ||
| } | ||
|
|
||
| return SrcST->isLayoutIdentical(DstST); |
There was a problem hiding this comment.
Fixed traversal in RewriteBitCast, corresponding tests added.
isSafeBitCastForScalarRepl stops its leading element walk at a struct with no elements, but the traversal in RewriteBitCast did not, and called getElementType(0) on it. A cast between two layout-identical structs whose leading members bottom out in an empty struct passes the predicate and then asserts. Guard the loop the same way. The empty chain now falls through to the identical-layout case, which is the correct rewrite for these types.
|
btw, #4614 was accidentally fixed here by 839b33c - it is safely excluded from scalar repl in |
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.