[Torch] Fix verifier crashes on malformed global slot initializer IR#4612
Open
alex1xu wants to merge 1 commit into
Open
[Torch] Fix verifier crashes on malformed global slot initializer IR#4612alex1xu wants to merge 1 commit into
alex1xu wants to merge 1 commit into
Conversation
28c0944 to
3e13583
Compare
3e13583 to
ee0007d
Compare
GlobalSlotModuleInitializerOp::verify() inspected its nested InitializeGlobalSlotsOp terminator before that op's own invariants ran, so malformed IR reached unchecked casts and crashed instead of producing a diagnostic: a non-flat or non-symbol slot name hit cast<FlatSymbolRefAttr>, a slot/operand count mismatch indexed out of bounds, and a non-module parent hit cast<ModuleOp>. Move the module-wide checks to verifyRegions() so the nested op is verified before the enclosing op runs, tighten slotSymNames to FlatSymbolRefArrayAttr so the element type is enforced declaratively, and constrain the parent with HasParent<ModuleOp>.
ee0007d to
0ca73cb
Compare
Author
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.
Fixes #4411.
torch-mlir-optasserts instead of emitting a diagnostic on a malformedtorch.global_slot.module_initializer. #4411 reports one case — a non-symbolslotSymNamesentry — but the same root cause produces two related crashes on the same op.The root cause is verification ordering.
GlobalSlotModuleInitializerOpusedhasVerifier, so itsverify()runs on op entrance, before the nestedtorch.initialize.global_slotsterminator's own invariants. The verifier reaches into that terminator (getBody()->getTerminator(), thencast<FlatSymbolRefAttr>overslotSymNames) and dereferences attributes the child verifier has not validated yet. Three parseable inputs crash:cast<FlatSymbolRefAttr>(TorchOps.cpp:6595); Crash in GlobalSlotModuleInitializerOp verifier due to invalid attribute type in slotSymNames #4411's[159]and a nested ref like[@a::@b](a validSymbolRefArrayAttrelement, so the declared type did not guard it) both reach it,getSlotSymNames()[i]out of bounds (TorchOps.cpp:6634),builtin.moduleparent hitscast<ModuleOp>(getParentOp())(TorchOps.cpp:6581).Repro:
The fix closes each at the layer that owns the invariant:
verify()→verifyRegions(), so the module-wide checks run on op exit, after the nested terminator is verified; the parent's casts then only run once the child's invariants hold,slotSymNames:SymbolRefArrayAttr→FlatSymbolRefArrayAttr, enforcing the element type declaratively (covers both[159]and[@a::@b]); the custom parser only emits flat refs, so no valid IR changes,HasParent<ModuleOp>, matching the siblingtorch.initialize.global_slotsop.Testing:
test/Dialect/Torch/invalid.mliradds three negative cases under-verify-diagnostics, one per fix — a nested-ref slot name (rejected by theFlatSymbolRefArrayAttrconstraint), an operand/slot count mismatch (rejected by the terminator's own verifier, which now runs before the parent's per-slot loop), and a non-module parent (rejected by theHasParenttrait). The existingmodule_initializererror tests and theGlobalizeObjectGraph/inline-global-slots/erase-module-initializersuites pass under theverifyRegions()move. No e2e test is added — this is a verifier guard, so the lit negative tests are the correct layer.